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