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