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