1154b204c2452a7594ebebc97d74d4a2de112d49
[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
783 static void
784 format_be16_masked(struct ds *s, const char *name,
785                    ovs_be16 value, ovs_be16 mask)
786 {
787     if (mask != htons(0)) {
788         ds_put_format(s, "%s=", name);
789         if (mask == OVS_BE16_MAX) {
790             ds_put_format(s, "%"PRIu16, ntohs(value));
791         } else {
792             ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
793                           ntohs(value), ntohs(mask));
794         }
795         ds_put_char(s, ',');
796     }
797 }
798
799 static void
800 format_flow_tunnel(struct ds *s, const struct match *match)
801 {
802     const struct flow_wildcards *wc = &match->wc;
803     const struct flow_tnl *tnl = &match->flow.tunnel;
804
805     switch (wc->masks.tunnel.tun_id) {
806     case 0:
807         break;
808     case OVS_BE64_MAX:
809         ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(tnl->tun_id));
810         break;
811     default:
812         ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
813                       ntohll(tnl->tun_id),
814                       ntohll(wc->masks.tunnel.tun_id));
815         break;
816     }
817     format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
818     format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
819
820     if (wc->masks.tunnel.ip_tos) {
821         ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
822     }
823     if (wc->masks.tunnel.ip_ttl) {
824         ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
825     }
826     if (wc->masks.tunnel.flags) {
827         format_flags(s, flow_tun_flag_to_string, tnl->flags, '|');
828         ds_put_char(s, ',');
829     }
830 }
831
832 /* Appends a string representation of 'match' to 's'.  If 'priority' is
833  * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
834 void
835 match_format(const struct match *match, struct ds *s, unsigned int priority)
836 {
837     const struct flow_wildcards *wc = &match->wc;
838     size_t start_len = s->length;
839     const struct flow *f = &match->flow;
840     bool skip_type = false;
841     bool skip_proto = false;
842
843     int i;
844
845     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 23);
846
847     if (priority != OFP_DEFAULT_PRIORITY) {
848         ds_put_format(s, "priority=%u,", priority);
849     }
850
851     switch (wc->masks.pkt_mark) {
852     case 0:
853         break;
854     case UINT32_MAX:
855         ds_put_format(s, "pkt_mark=%#"PRIx32",", f->pkt_mark);
856         break;
857     default:
858         ds_put_format(s, "pkt_mark=%#"PRIx32"/%#"PRIx32",",
859                       f->pkt_mark, wc->masks.pkt_mark);
860         break;
861     }
862
863     if (wc->masks.skb_priority) {
864         ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority);
865     }
866
867     if (wc->masks.dl_type) {
868         skip_type = true;
869         if (f->dl_type == htons(ETH_TYPE_IP)) {
870             if (wc->masks.nw_proto) {
871                 skip_proto = true;
872                 if (f->nw_proto == IPPROTO_ICMP) {
873                     ds_put_cstr(s, "icmp,");
874                 } else if (f->nw_proto == IPPROTO_TCP) {
875                     ds_put_cstr(s, "tcp,");
876                 } else if (f->nw_proto == IPPROTO_UDP) {
877                     ds_put_cstr(s, "udp,");
878                 } else if (f->nw_proto == IPPROTO_SCTP) {
879                     ds_put_cstr(s, "sctp,");
880                 } else {
881                     ds_put_cstr(s, "ip,");
882                     skip_proto = false;
883                 }
884             } else {
885                 ds_put_cstr(s, "ip,");
886             }
887         } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
888             if (wc->masks.nw_proto) {
889                 skip_proto = true;
890                 if (f->nw_proto == IPPROTO_ICMPV6) {
891                     ds_put_cstr(s, "icmp6,");
892                 } else if (f->nw_proto == IPPROTO_TCP) {
893                     ds_put_cstr(s, "tcp6,");
894                 } else if (f->nw_proto == IPPROTO_UDP) {
895                     ds_put_cstr(s, "udp6,");
896                 } else if (f->nw_proto == IPPROTO_SCTP) {
897                     ds_put_cstr(s, "sctp6,");
898                 } else {
899                     ds_put_cstr(s, "ipv6,");
900                     skip_proto = false;
901                 }
902             } else {
903                 ds_put_cstr(s, "ipv6,");
904             }
905         } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
906             ds_put_cstr(s, "arp,");
907         } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
908             ds_put_cstr(s, "rarp,");
909         } else if (f->dl_type == htons(ETH_TYPE_MPLS)) {
910             ds_put_cstr(s, "mpls,");
911         } else if (f->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
912             ds_put_cstr(s, "mplsm,");
913         } else {
914             skip_type = false;
915         }
916     }
917     for (i = 0; i < FLOW_N_REGS; i++) {
918         switch (wc->masks.regs[i]) {
919         case 0:
920             break;
921         case UINT32_MAX:
922             ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
923             break;
924         default:
925             ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
926                           i, f->regs[i], wc->masks.regs[i]);
927             break;
928         }
929     }
930
931     format_flow_tunnel(s, match);
932
933     switch (wc->masks.metadata) {
934     case 0:
935         break;
936     case OVS_BE64_MAX:
937         ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata));
938         break;
939     default:
940         ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",",
941                       ntohll(f->metadata), ntohll(wc->masks.metadata));
942         break;
943     }
944     if (wc->masks.in_port.ofp_port) {
945         ds_put_cstr(s, "in_port=");
946         ofputil_format_port(f->in_port.ofp_port, s);
947         ds_put_char(s, ',');
948     }
949     if (wc->masks.vlan_tci) {
950         ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
951         ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
952         ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
953
954         if (cfi && f->vlan_tci & htons(VLAN_CFI)
955             && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
956             && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
957             && (vid_mask || pcp_mask)) {
958             if (vid_mask) {
959                 ds_put_format(s, "dl_vlan=%"PRIu16",",
960                               vlan_tci_to_vid(f->vlan_tci));
961             }
962             if (pcp_mask) {
963                 ds_put_format(s, "dl_vlan_pcp=%d,",
964                               vlan_tci_to_pcp(f->vlan_tci));
965             }
966         } else if (wc->masks.vlan_tci == htons(0xffff)) {
967             ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
968         } else {
969             ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
970                           ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
971         }
972     }
973     format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
974     format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
975     if (!skip_type && wc->masks.dl_type) {
976         ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
977     }
978     if (f->dl_type == htons(ETH_TYPE_IPV6)) {
979         format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
980         format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
981         if (wc->masks.ipv6_label) {
982             if (wc->masks.ipv6_label == OVS_BE32_MAX) {
983                 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
984                               ntohl(f->ipv6_label));
985             } else {
986                 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
987                               ntohl(f->ipv6_label),
988                               ntohl(wc->masks.ipv6_label));
989             }
990         }
991     } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
992                f->dl_type == htons(ETH_TYPE_RARP)) {
993         format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
994         format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
995     } else {
996         format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
997         format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
998     }
999     if (!skip_proto && wc->masks.nw_proto) {
1000         if (f->dl_type == htons(ETH_TYPE_ARP) ||
1001             f->dl_type == htons(ETH_TYPE_RARP)) {
1002             ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
1003         } else {
1004             ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
1005         }
1006     }
1007     if (f->dl_type == htons(ETH_TYPE_ARP) ||
1008         f->dl_type == htons(ETH_TYPE_RARP)) {
1009         format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
1010         format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
1011     }
1012     if (wc->masks.nw_tos & IP_DSCP_MASK) {
1013         ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
1014     }
1015     if (wc->masks.nw_tos & IP_ECN_MASK) {
1016         ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
1017     }
1018     if (wc->masks.nw_ttl) {
1019         ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
1020     }
1021     if (wc->masks.mpls_lse & htonl(MPLS_LABEL_MASK)) {
1022         ds_put_format(s, "mpls_label=%"PRIu32",",
1023                  mpls_lse_to_label(f->mpls_lse));
1024     }
1025     if (wc->masks.mpls_lse & htonl(MPLS_TC_MASK)) {
1026         ds_put_format(s, "mpls_tc=%"PRIu8",",
1027                  mpls_lse_to_tc(f->mpls_lse));
1028     }
1029     if (wc->masks.mpls_lse & htonl(MPLS_TTL_MASK)) {
1030         ds_put_format(s, "mpls_ttl=%"PRIu8",",
1031                  mpls_lse_to_ttl(f->mpls_lse));
1032     }
1033     if (wc->masks.mpls_lse & htonl(MPLS_BOS_MASK)) {
1034         ds_put_format(s, "mpls_bos=%"PRIu8",",
1035                  mpls_lse_to_bos(f->mpls_lse));
1036     }
1037     switch (wc->masks.nw_frag) {
1038     case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
1039         ds_put_format(s, "nw_frag=%s,",
1040                       f->nw_frag & FLOW_NW_FRAG_ANY
1041                       ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
1042                       : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
1043         break;
1044
1045     case FLOW_NW_FRAG_ANY:
1046         ds_put_format(s, "nw_frag=%s,",
1047                       f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
1048         break;
1049
1050     case FLOW_NW_FRAG_LATER:
1051         ds_put_format(s, "nw_frag=%s,",
1052                       f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
1053         break;
1054     }
1055     if (f->dl_type == htons(ETH_TYPE_IP) &&
1056         f->nw_proto == IPPROTO_ICMP) {
1057         format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1058         format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1059     } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
1060                f->nw_proto == IPPROTO_ICMPV6) {
1061         format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1062         format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1063         format_ipv6_netmask(s, "nd_target", &f->nd_target,
1064                             &wc->masks.nd_target);
1065         format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
1066         format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
1067     } else {
1068         format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
1069         format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
1070     }
1071     if (is_ip_any(f) && f->nw_proto == IPPROTO_TCP && wc->masks.tcp_flags) {
1072         uint16_t mask = TCP_FLAGS(wc->masks.tcp_flags);
1073         if (mask == TCP_FLAGS(OVS_BE16_MAX)) {
1074             ds_put_format(s, "tcp_flags=0x%03"PRIx16",", ntohs(f->tcp_flags));
1075         } else {
1076             format_flags_masked(s, "tcp_flags", packet_tcp_flag_to_string,
1077                                 ntohs(f->tcp_flags), mask);
1078         }
1079     }
1080
1081     if (s->length > start_len && ds_last(s) == ',') {
1082         s->length--;
1083     }
1084 }
1085
1086 /* Converts 'match' to a string and returns the string.  If 'priority' is
1087  * different from OFP_DEFAULT_PRIORITY, includes it in the string.  The caller
1088  * must free the string (with free()). */
1089 char *
1090 match_to_string(const struct match *match, unsigned int priority)
1091 {
1092     struct ds s = DS_EMPTY_INITIALIZER;
1093     match_format(match, &s, priority);
1094     return ds_steal_cstr(&s);
1095 }
1096
1097 void
1098 match_print(const struct match *match)
1099 {
1100     char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
1101     puts(s);
1102     free(s);
1103 }
1104 \f
1105 /* Initializes 'dst' as a copy of 'src'.  The caller must eventually free 'dst'
1106  * with minimatch_destroy(). */
1107 void
1108 minimatch_init(struct minimatch *dst, const struct match *src)
1109 {
1110     minimask_init(&dst->mask, &src->wc);
1111     miniflow_init_with_minimask(&dst->flow, &src->flow, &dst->mask);
1112 }
1113
1114 /* Initializes 'dst' as a copy of 'src'.  The caller must eventually free 'dst'
1115  * with minimatch_destroy(). */
1116 void
1117 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1118 {
1119     miniflow_clone(&dst->flow, &src->flow);
1120     minimask_clone(&dst->mask, &src->mask);
1121 }
1122
1123 /* Initializes 'dst' with the data in 'src', destroying 'src'.  The caller must
1124  * eventually free 'dst' with minimatch_destroy(). */
1125 void
1126 minimatch_move(struct minimatch *dst, struct minimatch *src)
1127 {
1128     miniflow_move(&dst->flow, &src->flow);
1129     minimask_move(&dst->mask, &src->mask);
1130 }
1131
1132 /* Frees any memory owned by 'match'.  Does not free the storage in which
1133  * 'match' itself resides; the caller is responsible for that. */
1134 void
1135 minimatch_destroy(struct minimatch *match)
1136 {
1137     miniflow_destroy(&match->flow);
1138     minimask_destroy(&match->mask);
1139 }
1140
1141 /* Initializes 'dst' as a copy of 'src'. */
1142 void
1143 minimatch_expand(const struct minimatch *src, struct match *dst)
1144 {
1145     miniflow_expand(&src->flow, &dst->flow);
1146     minimask_expand(&src->mask, &dst->wc);
1147 }
1148
1149 /* Returns true if 'a' and 'b' match the same packets, false otherwise.  */
1150 bool
1151 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1152 {
1153     return (miniflow_equal(&a->flow, &b->flow)
1154             && minimask_equal(&a->mask, &b->mask));
1155 }
1156
1157 /* Returns a hash value for 'match', given 'basis'. */
1158 uint32_t
1159 minimatch_hash(const struct minimatch *match, uint32_t basis)
1160 {
1161     return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
1162 }
1163
1164 /* Returns true if 'target' satisifies 'match', that is, if each bit for which
1165  * 'match' specifies a particular value has the correct value in 'target'.
1166  *
1167  * This function is equivalent to miniflow_equal_flow_in_minimask(&match->flow,
1168  * target, &match->mask) but it is faster because of the invariant that
1169  * match->flow.map and match->mask.map are the same. */
1170 bool
1171 minimatch_matches_flow(const struct minimatch *match,
1172                        const struct flow *target)
1173 {
1174     const uint32_t *target_u32 = (const uint32_t *) target;
1175     const uint32_t *flowp = match->flow.values;
1176     const uint32_t *maskp = match->mask.masks.values;
1177     uint64_t map;
1178
1179     for (map = match->flow.map; map; map = zero_rightmost_1bit(map)) {
1180         if ((*flowp++ ^ target_u32[raw_ctz(map)]) & *maskp++) {
1181             return false;
1182         }
1183     }
1184
1185     return true;
1186 }
1187
1188 /* Returns a hash value for the bits of range [start, end) in 'minimatch',
1189  * given 'basis'.
1190  *
1191  * The hash values returned by this function are the same as those returned by
1192  * flow_hash_in_minimask_range(), only the form of the arguments differ. */
1193 uint32_t
1194 minimatch_hash_range(const struct minimatch *match, uint8_t start, uint8_t end,
1195                      uint32_t *basis)
1196 {
1197     const uint32_t *p;
1198     uint64_t map = miniflow_get_map_in_range(&match->mask.masks, start, end,
1199                                              &p);
1200     const ptrdiff_t df = match->mask.masks.values - match->flow.values;
1201     uint32_t hash = *basis;
1202
1203     for (; map; map = zero_rightmost_1bit(map)) {
1204         if (*p) {
1205             hash = mhash_add(hash, *(p - df) & *p);
1206         }
1207         p++;
1208     }
1209     *basis = hash; /* Allow continuation from the unfinished value. */
1210     return mhash_finish(hash, (p - match->mask.masks.values) * 4);
1211 }
1212
1213 /* Appends a string representation of 'match' to 's'.  If 'priority' is
1214  * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1215 void
1216 minimatch_format(const struct minimatch *match, struct ds *s,
1217                  unsigned int priority)
1218 {
1219     struct match megamatch;
1220
1221     minimatch_expand(match, &megamatch);
1222     match_format(&megamatch, s, priority);
1223 }
1224
1225 /* Converts 'match' to a string and returns the string.  If 'priority' is
1226  * different from OFP_DEFAULT_PRIORITY, includes it in the string.  The caller
1227  * must free the string (with free()). */
1228 char *
1229 minimatch_to_string(const struct minimatch *match, unsigned int priority)
1230 {
1231     struct match megamatch;
1232
1233     minimatch_expand(match, &megamatch);
1234     return match_to_string(&megamatch, priority);
1235 }