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