2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "openvswitch/match.h"
21 #include "byte-order.h"
23 #include "openvswitch/dynamic-string.h"
24 #include "openvswitch/ofp-util.h"
26 #include "tun-metadata.h"
28 /* Converts the flow in 'flow' into a match in 'match', with the given
31 match_init(struct match *match,
32 const struct flow *flow, const struct flow_wildcards *wc)
36 match_zero_wildcarded_fields(match);
37 memset(&match->tun_md, 0, sizeof match->tun_md);
40 /* Converts a flow into a match. It sets the wildcard masks based on
41 * the packet contents. It will not set the mask for fields that do not
42 * make sense for the packet type. */
44 match_wc_init(struct match *match, const struct flow *flow)
48 flow_wildcards_init_for_packet(&match->wc, flow);
49 WC_MASK_FIELD(&match->wc, regs);
50 WC_MASK_FIELD(&match->wc, metadata);
52 memset(&match->tun_md, 0, sizeof match->tun_md);
55 /* Initializes 'match' as a "catch-all" match that matches every packet. */
57 match_init_catchall(struct match *match)
59 memset(&match->flow, 0, sizeof match->flow);
60 flow_wildcards_init_catchall(&match->wc);
61 memset(&match->tun_md, 0, sizeof match->tun_md);
64 /* For each bit or field wildcarded in 'match', sets the corresponding bit or
65 * field in 'flow' to all-0-bits. It is important to maintain this invariant
66 * in a match that might be inserted into a classifier.
68 * It is never necessary to call this function directly for a match that is
69 * initialized or modified only by match_*() functions. It is useful to
70 * restore the invariant in a match whose 'wc' member is modified by hand.
73 match_zero_wildcarded_fields(struct match *match)
75 flow_zero_wildcards(&match->flow, &match->wc);
79 match_set_dp_hash(struct match *match, uint32_t value)
81 match_set_dp_hash_masked(match, value, UINT32_MAX);
85 match_set_dp_hash_masked(struct match *match, uint32_t value, uint32_t mask)
87 match->wc.masks.dp_hash = mask;
88 match->flow.dp_hash = value & mask;
92 match_set_recirc_id(struct match *match, uint32_t value)
94 match->flow.recirc_id = value;
95 match->wc.masks.recirc_id = UINT32_MAX;
99 match_set_conj_id(struct match *match, uint32_t value)
101 match->flow.conj_id = value;
102 match->wc.masks.conj_id = UINT32_MAX;
106 match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
108 match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
112 match_set_reg_masked(struct match *match, unsigned int reg_idx,
113 uint32_t value, uint32_t mask)
115 ovs_assert(reg_idx < FLOW_N_REGS);
116 flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
117 match->flow.regs[reg_idx] = value & mask;
121 match_set_xreg(struct match *match, unsigned int xreg_idx, uint64_t value)
123 match_set_xreg_masked(match, xreg_idx, value, UINT64_MAX);
127 match_set_xreg_masked(struct match *match, unsigned int xreg_idx,
128 uint64_t value, uint64_t mask)
130 ovs_assert(xreg_idx < FLOW_N_XREGS);
131 flow_wildcards_set_xreg_mask(&match->wc, xreg_idx, mask);
132 flow_set_xreg(&match->flow, xreg_idx, value & mask);
136 match_set_actset_output(struct match *match, ofp_port_t actset_output)
138 match->wc.masks.actset_output = u16_to_ofp(UINT16_MAX);
139 match->flow.actset_output = actset_output;
143 match_set_metadata(struct match *match, ovs_be64 metadata)
145 match_set_metadata_masked(match, metadata, OVS_BE64_MAX);
149 match_set_metadata_masked(struct match *match,
150 ovs_be64 metadata, ovs_be64 mask)
152 match->wc.masks.metadata = mask;
153 match->flow.metadata = metadata & mask;
157 match_set_tun_id(struct match *match, ovs_be64 tun_id)
159 match_set_tun_id_masked(match, tun_id, OVS_BE64_MAX);
163 match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
165 match->wc.masks.tunnel.tun_id = mask;
166 match->flow.tunnel.tun_id = tun_id & mask;
170 match_set_tun_src(struct match *match, ovs_be32 src)
172 match_set_tun_src_masked(match, src, OVS_BE32_MAX);
176 match_set_tun_src_masked(struct match *match, ovs_be32 src, ovs_be32 mask)
178 match->wc.masks.tunnel.ip_src = mask;
179 match->flow.tunnel.ip_src = src & mask;
183 match_set_tun_dst(struct match *match, ovs_be32 dst)
185 match_set_tun_dst_masked(match, dst, OVS_BE32_MAX);
189 match_set_tun_dst_masked(struct match *match, ovs_be32 dst, ovs_be32 mask)
191 match->wc.masks.tunnel.ip_dst = mask;
192 match->flow.tunnel.ip_dst = dst & mask;
196 match_set_tun_ipv6_src(struct match *match, const struct in6_addr *src)
198 match->flow.tunnel.ipv6_src = *src;
199 match->wc.masks.tunnel.ipv6_src = in6addr_exact;
203 match_set_tun_ipv6_src_masked(struct match *match, const struct in6_addr *src,
204 const struct in6_addr *mask)
206 match->flow.tunnel.ipv6_src = ipv6_addr_bitand(src, mask);
207 match->wc.masks.tunnel.ipv6_src = *mask;
211 match_set_tun_ipv6_dst(struct match *match, const struct in6_addr *dst)
213 match->flow.tunnel.ipv6_dst = *dst;
214 match->wc.masks.tunnel.ipv6_dst = in6addr_exact;
218 match_set_tun_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
219 const struct in6_addr *mask)
221 match->flow.tunnel.ipv6_dst = ipv6_addr_bitand(dst, mask);
222 match->wc.masks.tunnel.ipv6_dst = *mask;
226 match_set_tun_ttl(struct match *match, uint8_t ttl)
228 match_set_tun_ttl_masked(match, ttl, UINT8_MAX);
232 match_set_tun_ttl_masked(struct match *match, uint8_t ttl, uint8_t mask)
234 match->wc.masks.tunnel.ip_ttl = mask;
235 match->flow.tunnel.ip_ttl = ttl & mask;
239 match_set_tun_tos(struct match *match, uint8_t tos)
241 match_set_tun_tos_masked(match, tos, UINT8_MAX);
245 match_set_tun_tos_masked(struct match *match, uint8_t tos, uint8_t mask)
247 match->wc.masks.tunnel.ip_tos = mask;
248 match->flow.tunnel.ip_tos = tos & mask;
252 match_set_tun_flags(struct match *match, uint16_t flags)
254 match_set_tun_flags_masked(match, flags, UINT16_MAX);
258 match_set_tun_flags_masked(struct match *match, uint16_t flags, uint16_t mask)
260 mask &= FLOW_TNL_PUB_F_MASK;
262 match->wc.masks.tunnel.flags = mask;
263 match->flow.tunnel.flags = flags & mask;
267 match_set_tun_gbp_id_masked(struct match *match, ovs_be16 gbp_id, ovs_be16 mask)
269 match->wc.masks.tunnel.gbp_id = mask;
270 match->flow.tunnel.gbp_id = gbp_id & mask;
274 match_set_tun_gbp_id(struct match *match, ovs_be16 gbp_id)
276 match_set_tun_gbp_id_masked(match, gbp_id, OVS_BE16_MAX);
280 match_set_tun_gbp_flags_masked(struct match *match, uint8_t flags, uint8_t mask)
282 match->wc.masks.tunnel.gbp_flags = mask;
283 match->flow.tunnel.gbp_flags = flags & mask;
287 match_set_tun_gbp_flags(struct match *match, uint8_t flags)
289 match_set_tun_gbp_flags_masked(match, flags, UINT8_MAX);
293 match_set_in_port(struct match *match, ofp_port_t ofp_port)
295 match->wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
296 match->flow.in_port.ofp_port = ofp_port;
300 match_set_skb_priority(struct match *match, uint32_t skb_priority)
302 match->wc.masks.skb_priority = UINT32_MAX;
303 match->flow.skb_priority = skb_priority;
307 match_set_pkt_mark(struct match *match, uint32_t pkt_mark)
309 match_set_pkt_mark_masked(match, pkt_mark, UINT32_MAX);
313 match_set_pkt_mark_masked(struct match *match, uint32_t pkt_mark, uint32_t mask)
315 match->flow.pkt_mark = pkt_mark & mask;
316 match->wc.masks.pkt_mark = mask;
320 match_set_ct_state(struct match *match, uint32_t ct_state)
322 match_set_ct_state_masked(match, ct_state, UINT32_MAX);
326 match_set_ct_state_masked(struct match *match, uint32_t ct_state, uint32_t mask)
328 match->flow.ct_state = ct_state & mask & UINT16_MAX;
329 match->wc.masks.ct_state = mask & UINT16_MAX;
333 match_set_ct_zone(struct match *match, uint16_t ct_zone)
335 match->flow.ct_zone = ct_zone;
336 match->wc.masks.ct_zone = UINT16_MAX;
340 match_set_ct_mark(struct match *match, uint32_t ct_mark)
342 match_set_ct_mark_masked(match, ct_mark, UINT32_MAX);
346 match_set_ct_mark_masked(struct match *match, uint32_t ct_mark,
349 match->flow.ct_mark = ct_mark & mask;
350 match->wc.masks.ct_mark = mask;
354 match_set_ct_label(struct match *match, ovs_u128 ct_label)
358 mask.u64.lo = UINT64_MAX;
359 mask.u64.hi = UINT64_MAX;
360 match_set_ct_label_masked(match, ct_label, mask);
364 match_set_ct_label_masked(struct match *match, ovs_u128 value, ovs_u128 mask)
366 match->flow.ct_label.u64.lo = value.u64.lo & mask.u64.lo;
367 match->flow.ct_label.u64.hi = value.u64.hi & mask.u64.hi;
368 match->wc.masks.ct_label = mask;
372 match_set_dl_type(struct match *match, ovs_be16 dl_type)
374 match->wc.masks.dl_type = OVS_BE16_MAX;
375 match->flow.dl_type = dl_type;
378 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
379 * exactly. 'mask_dst' is set to all 1s. */
381 set_eth(const struct eth_addr value_src,
382 struct eth_addr *value_dst,
383 struct eth_addr *mask_dst)
385 *value_dst = value_src;
386 *mask_dst = eth_addr_exact;
389 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
390 * after each byte is ANDed with the appropriate byte in 'mask_src'.
391 * 'mask_dst' is set to 'mask_src' */
393 set_eth_masked(const struct eth_addr value_src,
394 const struct eth_addr mask_src,
395 struct eth_addr *value_dst, struct eth_addr *mask_dst)
399 for (i = 0; i < ARRAY_SIZE(value_dst->be16); i++) {
400 value_dst->be16[i] = value_src.be16[i] & mask_src.be16[i];
402 *mask_dst = mask_src;
405 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
408 match_set_dl_src(struct match *match, const struct eth_addr dl_src)
410 set_eth(dl_src, &match->flow.dl_src, &match->wc.masks.dl_src);
413 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
414 * after each byte is ANDed with the appropriate byte in 'mask'. */
416 match_set_dl_src_masked(struct match *match,
417 const struct eth_addr dl_src,
418 const struct eth_addr mask)
420 set_eth_masked(dl_src, mask, &match->flow.dl_src, &match->wc.masks.dl_src);
423 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
426 match_set_dl_dst(struct match *match, const struct eth_addr dl_dst)
428 set_eth(dl_dst, &match->flow.dl_dst, &match->wc.masks.dl_dst);
431 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
432 * byte is ANDed with the appropriate byte in 'mask'.
434 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
435 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
437 match_set_dl_dst_masked(struct match *match,
438 const struct eth_addr dl_dst,
439 const struct eth_addr mask)
441 set_eth_masked(dl_dst, mask, &match->flow.dl_dst, &match->wc.masks.dl_dst);
445 match_set_dl_tci(struct match *match, ovs_be16 tci)
447 match_set_dl_tci_masked(match, tci, htons(0xffff));
451 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
453 match->flow.vlan_tci = tci & mask;
454 match->wc.masks.vlan_tci = mask;
457 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
458 * wildcarded, then 'match' will match a packet regardless of whether it has an
459 * 802.1Q header or not. */
461 match_set_any_vid(struct match *match)
463 if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
464 match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
465 match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
467 match_set_dl_tci_masked(match, htons(0), htons(0));
471 /* Modifies 'match' depending on 'dl_vlan':
473 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
474 * without an 802.1Q header.
476 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
477 * VID equals the low 12 bits of 'dl_vlan'.
480 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
482 flow_set_dl_vlan(&match->flow, dl_vlan);
483 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
484 match->wc.masks.vlan_tci = OVS_BE16_MAX;
486 match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
490 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
491 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
494 match_set_vlan_vid(struct match *match, ovs_be16 vid)
496 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
500 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
501 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
502 * plus CFI), with the corresponding 'mask'. */
504 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
506 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
507 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
510 flow_set_vlan_vid(&match->flow, vid & mask);
511 match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
514 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
515 * wildcarded, then 'match' will match a packet regardless of whether it has an
516 * 802.1Q header or not. */
518 match_set_any_pcp(struct match *match)
520 if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
521 match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
522 match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
524 match_set_dl_tci_masked(match, htons(0), htons(0));
528 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
529 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
531 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
533 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
534 match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
537 /* Modifies 'match' so that the MPLS label 'idx' matches 'lse' exactly. */
539 match_set_mpls_lse(struct match *match, int idx, ovs_be32 lse)
541 match->wc.masks.mpls_lse[idx] = OVS_BE32_MAX;
542 match->flow.mpls_lse[idx] = lse;
545 /* Modifies 'match' so that the MPLS label is wildcarded. */
547 match_set_any_mpls_label(struct match *match, int idx)
549 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_LABEL_MASK);
550 flow_set_mpls_label(&match->flow, idx, htonl(0));
553 /* Modifies 'match' so that it matches only packets with an MPLS header whose
554 * label equals the low 20 bits of 'mpls_label'. */
556 match_set_mpls_label(struct match *match, int idx, ovs_be32 mpls_label)
558 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_LABEL_MASK);
559 flow_set_mpls_label(&match->flow, idx, mpls_label);
562 /* Modifies 'match' so that the MPLS TC is wildcarded. */
564 match_set_any_mpls_tc(struct match *match, int idx)
566 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TC_MASK);
567 flow_set_mpls_tc(&match->flow, idx, 0);
570 /* Modifies 'match' so that it matches only packets with an MPLS header whose
571 * Traffic Class equals the low 3 bits of 'mpls_tc'. */
573 match_set_mpls_tc(struct match *match, int idx, uint8_t mpls_tc)
575 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TC_MASK);
576 flow_set_mpls_tc(&match->flow, idx, mpls_tc);
579 /* Modifies 'match' so that the MPLS stack flag is wildcarded. */
581 match_set_any_mpls_bos(struct match *match, int idx)
583 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_BOS_MASK);
584 flow_set_mpls_bos(&match->flow, idx, 0);
587 /* Modifies 'match' so that it matches only packets with an MPLS header whose
588 * Stack Flag equals the lower bit of 'mpls_bos' */
590 match_set_mpls_bos(struct match *match, int idx, uint8_t mpls_bos)
592 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_BOS_MASK);
593 flow_set_mpls_bos(&match->flow, idx, mpls_bos);
596 /* Modifies 'match' so that the TTL of MPLS label 'idx' is wildcarded. */
598 match_set_any_mpls_ttl(struct match *match, int idx)
600 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TTL_MASK);
601 flow_set_mpls_ttl(&match->flow, idx, 0);
604 /* Modifies 'match' so that it matches only packets in which the TTL of MPLS
605 * label 'idx' equals 'mpls_ttl'. */
607 match_set_mpls_ttl(struct match *match, int idx, uint8_t mpls_ttl)
609 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TTL_MASK);
610 flow_set_mpls_ttl(&match->flow, idx, mpls_ttl);
613 /* Modifies 'match' so that the MPLS LSE is wildcarded. */
615 match_set_any_mpls_lse(struct match *match, int idx)
617 match->wc.masks.mpls_lse[idx] = htonl(0);
618 flow_set_mpls_lse(&match->flow, idx, htonl(0));
622 match_set_tp_src(struct match *match, ovs_be16 tp_src)
624 match_set_tp_src_masked(match, tp_src, OVS_BE16_MAX);
628 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
630 match->flow.tp_src = port & mask;
631 match->wc.masks.tp_src = mask;
635 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
637 match_set_tp_dst_masked(match, tp_dst, OVS_BE16_MAX);
641 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
643 match->flow.tp_dst = port & mask;
644 match->wc.masks.tp_dst = mask;
648 match_set_tcp_flags(struct match *match, ovs_be16 flags)
650 match_set_tcp_flags_masked(match, flags, OVS_BE16_MAX);
654 match_set_tcp_flags_masked(struct match *match, ovs_be16 flags, ovs_be16 mask)
656 match->flow.tcp_flags = flags & mask;
657 match->wc.masks.tcp_flags = mask;
661 match_set_nw_proto(struct match *match, uint8_t nw_proto)
663 match->flow.nw_proto = nw_proto;
664 match->wc.masks.nw_proto = UINT8_MAX;
668 match_set_nw_src(struct match *match, ovs_be32 nw_src)
670 match->flow.nw_src = nw_src;
671 match->wc.masks.nw_src = OVS_BE32_MAX;
675 match_set_nw_src_masked(struct match *match,
676 ovs_be32 nw_src, ovs_be32 mask)
678 match->flow.nw_src = nw_src & mask;
679 match->wc.masks.nw_src = mask;
683 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
685 match->flow.nw_dst = nw_dst;
686 match->wc.masks.nw_dst = OVS_BE32_MAX;
690 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
692 match->flow.nw_dst = ip & mask;
693 match->wc.masks.nw_dst = mask;
697 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
699 match->wc.masks.nw_tos |= IP_DSCP_MASK;
700 match->flow.nw_tos &= ~IP_DSCP_MASK;
701 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
705 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
707 match->wc.masks.nw_tos |= IP_ECN_MASK;
708 match->flow.nw_tos &= ~IP_ECN_MASK;
709 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
713 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
715 match->wc.masks.nw_ttl = UINT8_MAX;
716 match->flow.nw_ttl = nw_ttl;
720 match_set_nw_frag(struct match *match, uint8_t nw_frag)
722 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
723 match->flow.nw_frag = nw_frag;
727 match_set_nw_frag_masked(struct match *match,
728 uint8_t nw_frag, uint8_t mask)
730 match->flow.nw_frag = nw_frag & mask;
731 match->wc.masks.nw_frag = mask;
735 match_set_icmp_type(struct match *match, uint8_t icmp_type)
737 match_set_tp_src(match, htons(icmp_type));
741 match_set_icmp_code(struct match *match, uint8_t icmp_code)
743 match_set_tp_dst(match, htons(icmp_code));
747 match_set_arp_sha(struct match *match, const struct eth_addr sha)
749 match->flow.arp_sha = sha;
750 match->wc.masks.arp_sha = eth_addr_exact;
754 match_set_arp_sha_masked(struct match *match,
755 const struct eth_addr arp_sha,
756 const struct eth_addr mask)
758 set_eth_masked(arp_sha, mask,
759 &match->flow.arp_sha, &match->wc.masks.arp_sha);
763 match_set_arp_tha(struct match *match, const struct eth_addr tha)
765 match->flow.arp_tha = tha;
766 match->wc.masks.arp_tha = eth_addr_exact;
770 match_set_arp_tha_masked(struct match *match,
771 const struct eth_addr arp_tha,
772 const struct eth_addr mask)
774 set_eth_masked(arp_tha, mask,
775 &match->flow.arp_tha, &match->wc.masks.arp_tha);
779 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
781 match->flow.ipv6_src = *src;
782 match->wc.masks.ipv6_src = in6addr_exact;
786 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
787 const struct in6_addr *mask)
789 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
790 match->wc.masks.ipv6_src = *mask;
794 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
796 match->flow.ipv6_dst = *dst;
797 match->wc.masks.ipv6_dst = in6addr_exact;
801 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
802 const struct in6_addr *mask)
804 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
805 match->wc.masks.ipv6_dst = *mask;
809 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
811 match->wc.masks.ipv6_label = OVS_BE32_MAX;
812 match->flow.ipv6_label = ipv6_label;
817 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
820 match->flow.ipv6_label = ipv6_label & mask;
821 match->wc.masks.ipv6_label = mask;
825 match_set_nd_target(struct match *match, const struct in6_addr *target)
827 match->flow.nd_target = *target;
828 match->wc.masks.nd_target = in6addr_exact;
832 match_set_nd_target_masked(struct match *match,
833 const struct in6_addr *target,
834 const struct in6_addr *mask)
836 match->flow.nd_target = ipv6_addr_bitand(target, mask);
837 match->wc.masks.nd_target = *mask;
840 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
841 * values for fixed fields, otherwise false. */
843 match_equal(const struct match *a, const struct match *b)
845 return (flow_wildcards_equal(&a->wc, &b->wc)
846 && flow_equal(&a->flow, &b->flow));
849 /* Returns a hash value for the flow and wildcards in 'match', starting from
852 match_hash(const struct match *match, uint32_t basis)
854 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
858 match_has_default_recirc_id(const struct match *m)
860 return m->flow.recirc_id == 0 && (m->wc.masks.recirc_id == UINT32_MAX ||
861 m->wc.masks.recirc_id == 0);
865 match_has_default_dp_hash(const struct match *m)
867 return ((m->flow.dp_hash | m->wc.masks.dp_hash) == 0);
870 /* Return true if the hidden fields of the match are set to the default values.
871 * The default values equals to those set up by match_init_hidden_fields(). */
873 match_has_default_hidden_fields(const struct match *m)
875 return match_has_default_recirc_id(m) && match_has_default_dp_hash(m);
879 match_init_hidden_fields(struct match *m)
881 match_set_recirc_id(m, 0);
882 match_set_dp_hash_masked(m, 0, 0);
886 format_eth_masked(struct ds *s, const char *name,
887 const struct eth_addr eth, const struct eth_addr mask)
889 if (!eth_addr_is_zero(mask)) {
890 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
891 eth_format_masked(eth, &mask, s);
897 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
901 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
902 ip_format_masked(ip, netmask, s);
908 format_ipv6_netmask(struct ds *s, const char *name,
909 const struct in6_addr *addr,
910 const struct in6_addr *netmask)
912 if (!ipv6_mask_is_any(netmask)) {
913 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
914 ipv6_format_masked(addr, netmask, s);
920 format_uint16_masked(struct ds *s, const char *name,
921 uint16_t value, uint16_t mask)
924 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
925 if (mask == UINT16_MAX) {
926 ds_put_format(s, "%"PRIu16, value);
928 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16, value, mask);
935 format_be16_masked(struct ds *s, const char *name,
936 ovs_be16 value, ovs_be16 mask)
938 if (mask != htons(0)) {
939 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
940 if (mask == OVS_BE16_MAX) {
941 ds_put_format(s, "%"PRIu16, ntohs(value));
943 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
944 ntohs(value), ntohs(mask));
951 format_be32_masked(struct ds *s, const char *name,
952 ovs_be32 value, ovs_be32 mask)
954 if (mask != htonl(0)) {
955 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
956 if (mask == OVS_BE32_MAX) {
957 ds_put_format(s, "%"PRIu32, ntohl(value));
959 ds_put_format(s, "0x%"PRIx32"/0x%"PRIx32,
960 ntohl(value), ntohl(mask));
967 format_uint32_masked(struct ds *s, const char *name,
968 uint32_t value, uint32_t mask)
971 ds_put_format(s, "%s%s=%s%#"PRIx32,
972 colors.param, name, colors.end, value);
973 if (mask != UINT32_MAX) {
974 ds_put_format(s, "/%#"PRIx32, mask);
981 format_be64_masked(struct ds *s, const char *name,
982 ovs_be64 value, ovs_be64 mask)
984 if (mask != htonll(0)) {
985 ds_put_format(s, "%s%s=%s%#"PRIx64,
986 colors.param, name, colors.end, ntohll(value));
987 if (mask != OVS_BE64_MAX) {
988 ds_put_format(s, "/%#"PRIx64, ntohll(mask));
995 format_flow_tunnel(struct ds *s, const struct match *match)
997 const struct flow_wildcards *wc = &match->wc;
998 const struct flow_tnl *tnl = &match->flow.tunnel;
1000 format_be64_masked(s, "tun_id", tnl->tun_id, wc->masks.tunnel.tun_id);
1001 format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
1002 format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
1003 format_ipv6_netmask(s, "tun_ipv6_src", &tnl->ipv6_src,
1004 &wc->masks.tunnel.ipv6_src);
1005 format_ipv6_netmask(s, "tun_ipv6_dst", &tnl->ipv6_dst,
1006 &wc->masks.tunnel.ipv6_dst);
1008 if (wc->masks.tunnel.gbp_id) {
1009 format_be16_masked(s, "tun_gbp_id", tnl->gbp_id,
1010 wc->masks.tunnel.gbp_id);
1013 if (wc->masks.tunnel.gbp_flags) {
1014 ds_put_format(s, "tun_gbp_flags=%#"PRIx8",", tnl->gbp_flags);
1017 if (wc->masks.tunnel.ip_tos) {
1018 ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
1020 if (wc->masks.tunnel.ip_ttl) {
1021 ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
1023 if (wc->masks.tunnel.flags) {
1024 format_flags_masked(s, "tun_flags", flow_tun_flag_to_string,
1026 wc->masks.tunnel.flags & FLOW_TNL_F_MASK,
1028 ds_put_char(s, ',');
1030 tun_metadata_match_format(s, match);
1034 format_ct_label_masked(struct ds *s, const ovs_u128 *key, const ovs_u128 *mask)
1036 if (!ovs_u128_is_zero(*mask)) {
1037 ovs_be128 value = hton128(*key);
1038 ds_put_format(s, "%sct_label=%s", colors.param, colors.end);
1039 ds_put_hex(s, &value, sizeof value);
1040 if (!is_all_ones(mask, sizeof(*mask))) {
1041 value = hton128(*mask);
1042 ds_put_char(s, '/');
1043 ds_put_hex(s, &value, sizeof value);
1045 ds_put_char(s, ',');
1049 /* Appends a string representation of 'match' to 's'. If 'priority' is
1050 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1052 match_format(const struct match *match, struct ds *s, int priority)
1054 const struct flow_wildcards *wc = &match->wc;
1055 size_t start_len = s->length;
1056 const struct flow *f = &match->flow;
1057 bool skip_type = false;
1059 bool skip_proto = false;
1063 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 35);
1065 if (priority != OFP_DEFAULT_PRIORITY) {
1066 ds_put_format(s, "%spriority=%s%d,",
1067 colors.special, colors.end, priority);
1070 format_uint32_masked(s, "pkt_mark", f->pkt_mark, wc->masks.pkt_mark);
1072 if (wc->masks.recirc_id) {
1073 format_uint32_masked(s, "recirc_id", f->recirc_id,
1074 wc->masks.recirc_id);
1077 if (wc->masks.dp_hash) {
1078 format_uint32_masked(s, "dp_hash", f->dp_hash,
1082 if (wc->masks.conj_id) {
1083 ds_put_format(s, "%sconj_id%s=%"PRIu32",",
1084 colors.param, colors.end, f->conj_id);
1087 if (wc->masks.skb_priority) {
1088 ds_put_format(s, "%sskb_priority=%s%#"PRIx32",",
1089 colors.param, colors.end, f->skb_priority);
1092 if (wc->masks.actset_output) {
1093 ds_put_format(s, "%sactset_output=%s", colors.param, colors.end);
1094 ofputil_format_port(f->actset_output, s);
1095 ds_put_char(s, ',');
1098 if (wc->masks.ct_state) {
1099 if (wc->masks.ct_state == UINT16_MAX) {
1100 ds_put_format(s, "%sct_state=%s", colors.param, colors.end);
1102 format_flags(s, ct_state_to_string, f->ct_state, '|');
1104 ds_put_cstr(s, "0"); /* No state. */
1107 format_flags_masked(s, "ct_state", ct_state_to_string,
1108 f->ct_state, wc->masks.ct_state, UINT16_MAX);
1110 ds_put_char(s, ',');
1113 if (wc->masks.ct_zone) {
1114 format_uint16_masked(s, "ct_zone", f->ct_zone, wc->masks.ct_zone);
1117 if (wc->masks.ct_mark) {
1118 format_uint32_masked(s, "ct_mark", f->ct_mark, wc->masks.ct_mark);
1121 if (!ovs_u128_is_zero(wc->masks.ct_label)) {
1122 format_ct_label_masked(s, &f->ct_label, &wc->masks.ct_label);
1125 if (wc->masks.dl_type) {
1127 if (f->dl_type == htons(ETH_TYPE_IP)) {
1128 if (wc->masks.nw_proto) {
1130 if (f->nw_proto == IPPROTO_ICMP) {
1131 ds_put_format(s, "%sicmp%s,", colors.value, colors.end);
1132 } else if (f->nw_proto == IPPROTO_IGMP) {
1133 ds_put_format(s, "%sigmp%s,", colors.value, colors.end);
1134 } else if (f->nw_proto == IPPROTO_TCP) {
1135 ds_put_format(s, "%stcp%s,", colors.value, colors.end);
1136 } else if (f->nw_proto == IPPROTO_UDP) {
1137 ds_put_format(s, "%sudp%s,", colors.value, colors.end);
1138 } else if (f->nw_proto == IPPROTO_SCTP) {
1139 ds_put_format(s, "%ssctp%s,", colors.value, colors.end);
1141 ds_put_format(s, "%sip%s,", colors.value, colors.end);
1145 ds_put_format(s, "%sip%s,", colors.value, colors.end);
1147 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
1148 if (wc->masks.nw_proto) {
1150 if (f->nw_proto == IPPROTO_ICMPV6) {
1151 ds_put_format(s, "%sicmp6%s,", colors.value, colors.end);
1152 } else if (f->nw_proto == IPPROTO_TCP) {
1153 ds_put_format(s, "%stcp6%s,", colors.value, colors.end);
1154 } else if (f->nw_proto == IPPROTO_UDP) {
1155 ds_put_format(s, "%sudp6%s,", colors.value, colors.end);
1156 } else if (f->nw_proto == IPPROTO_SCTP) {
1157 ds_put_format(s, "%ssctp6%s,", colors.value, colors.end);
1159 ds_put_format(s, "%sipv6%s,", colors.value, colors.end);
1163 ds_put_format(s, "%sipv6%s,", colors.value, colors.end);
1165 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
1166 ds_put_format(s, "%sarp%s,", colors.value, colors.end);
1167 } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
1168 ds_put_format(s, "%srarp%s,", colors.value, colors.end);
1169 } else if (f->dl_type == htons(ETH_TYPE_MPLS)) {
1170 ds_put_format(s, "%smpls%s,", colors.value, colors.end);
1171 } else if (f->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
1172 ds_put_format(s, "%smplsm%s,", colors.value, colors.end);
1177 for (i = 0; i < FLOW_N_REGS; i++) {
1178 #define REGNAME_LEN 20
1179 char regname[REGNAME_LEN];
1180 if (snprintf(regname, REGNAME_LEN, "reg%d", i) >= REGNAME_LEN) {
1181 strcpy(regname, "reg?");
1183 format_uint32_masked(s, regname, f->regs[i], wc->masks.regs[i]);
1186 format_flow_tunnel(s, match);
1188 format_be64_masked(s, "metadata", f->metadata, wc->masks.metadata);
1190 if (wc->masks.in_port.ofp_port) {
1191 ds_put_format(s, "%sin_port=%s", colors.param, colors.end);
1192 ofputil_format_port(f->in_port.ofp_port, s);
1193 ds_put_char(s, ',');
1195 if (wc->masks.vlan_tci) {
1196 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
1197 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
1198 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
1200 if (cfi && f->vlan_tci & htons(VLAN_CFI)
1201 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
1202 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
1203 && (vid_mask || pcp_mask)) {
1205 ds_put_format(s, "%sdl_vlan=%s%"PRIu16",", colors.param,
1206 colors.end, vlan_tci_to_vid(f->vlan_tci));
1209 ds_put_format(s, "%sdl_vlan_pcp=%s%d,", colors.param,
1210 colors.end, vlan_tci_to_pcp(f->vlan_tci));
1212 } else if (wc->masks.vlan_tci == htons(0xffff)) {
1213 ds_put_format(s, "%svlan_tci=%s0x%04"PRIx16",", colors.param,
1214 colors.end, ntohs(f->vlan_tci));
1216 ds_put_format(s, "%svlan_tci=%s0x%04"PRIx16"/0x%04"PRIx16",",
1217 colors.param, colors.end,
1218 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
1221 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
1222 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
1223 if (!skip_type && wc->masks.dl_type) {
1224 ds_put_format(s, "%sdl_type=%s0x%04"PRIx16",",
1225 colors.param, colors.end, ntohs(f->dl_type));
1227 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
1228 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
1229 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
1230 if (wc->masks.ipv6_label) {
1231 if (wc->masks.ipv6_label == OVS_BE32_MAX) {
1232 ds_put_format(s, "%sipv6_label=%s0x%05"PRIx32",",
1233 colors.param, colors.end,
1234 ntohl(f->ipv6_label));
1236 ds_put_format(s, "%sipv6_label=%s0x%05"PRIx32"/0x%05"PRIx32",",
1237 colors.param, colors.end, ntohl(f->ipv6_label),
1238 ntohl(wc->masks.ipv6_label));
1241 } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
1242 f->dl_type == htons(ETH_TYPE_RARP)) {
1243 format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
1244 format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
1246 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
1247 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
1249 if (!skip_proto && wc->masks.nw_proto) {
1250 if (f->dl_type == htons(ETH_TYPE_ARP) ||
1251 f->dl_type == htons(ETH_TYPE_RARP)) {
1252 ds_put_format(s, "%sarp_op=%s%"PRIu8",",
1253 colors.param, colors.end, f->nw_proto);
1255 ds_put_format(s, "%snw_proto=%s%"PRIu8",",
1256 colors.param, colors.end, f->nw_proto);
1259 if (f->dl_type == htons(ETH_TYPE_ARP) ||
1260 f->dl_type == htons(ETH_TYPE_RARP)) {
1261 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
1262 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
1264 if (wc->masks.nw_tos & IP_DSCP_MASK) {
1265 ds_put_format(s, "%snw_tos=%s%"PRIu8",",
1266 colors.param, colors.end, f->nw_tos & IP_DSCP_MASK);
1268 if (wc->masks.nw_tos & IP_ECN_MASK) {
1269 ds_put_format(s, "%snw_ecn=%s%"PRIu8",",
1270 colors.param, colors.end, f->nw_tos & IP_ECN_MASK);
1272 if (wc->masks.nw_ttl) {
1273 ds_put_format(s, "%snw_ttl=%s%"PRIu8",",
1274 colors.param, colors.end, f->nw_ttl);
1276 if (wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
1277 ds_put_format(s, "%smpls_label=%s%"PRIu32",", colors.param,
1278 colors.end, mpls_lse_to_label(f->mpls_lse[0]));
1280 if (wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
1281 ds_put_format(s, "%smpls_tc=%s%"PRIu8",", colors.param, colors.end,
1282 mpls_lse_to_tc(f->mpls_lse[0]));
1284 if (wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK)) {
1285 ds_put_format(s, "%smpls_ttl=%s%"PRIu8",", colors.param, colors.end,
1286 mpls_lse_to_ttl(f->mpls_lse[0]));
1288 if (wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
1289 ds_put_format(s, "%smpls_bos=%s%"PRIu8",", colors.param, colors.end,
1290 mpls_lse_to_bos(f->mpls_lse[0]));
1292 format_be32_masked(s, "mpls_lse1", f->mpls_lse[1], wc->masks.mpls_lse[1]);
1293 format_be32_masked(s, "mpls_lse2", f->mpls_lse[2], wc->masks.mpls_lse[2]);
1295 switch (wc->masks.nw_frag) {
1296 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
1297 ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
1298 f->nw_frag & FLOW_NW_FRAG_ANY
1299 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
1300 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
1303 case FLOW_NW_FRAG_ANY:
1304 ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
1305 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
1308 case FLOW_NW_FRAG_LATER:
1309 ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
1310 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
1313 if (f->dl_type == htons(ETH_TYPE_IP) &&
1314 f->nw_proto == IPPROTO_ICMP) {
1315 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1316 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1317 } else if (f->dl_type == htons(ETH_TYPE_IP) &&
1318 f->nw_proto == IPPROTO_IGMP) {
1319 format_be16_masked(s, "igmp_type", f->tp_src, wc->masks.tp_src);
1320 format_be16_masked(s, "igmp_code", f->tp_dst, wc->masks.tp_dst);
1321 } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
1322 f->nw_proto == IPPROTO_ICMPV6) {
1323 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1324 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1325 format_ipv6_netmask(s, "nd_target", &f->nd_target,
1326 &wc->masks.nd_target);
1327 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
1328 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
1330 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
1331 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
1333 if (is_ip_any(f) && f->nw_proto == IPPROTO_TCP && wc->masks.tcp_flags) {
1334 format_flags_masked(s, "tcp_flags", packet_tcp_flag_to_string,
1335 ntohs(f->tcp_flags), TCP_FLAGS(wc->masks.tcp_flags),
1336 TCP_FLAGS(OVS_BE16_MAX));
1339 if (s->length > start_len) {
1344 /* Converts 'match' to a string and returns the string. If 'priority' is
1345 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1346 * must free the string (with free()). */
1348 match_to_string(const struct match *match, int priority)
1350 struct ds s = DS_EMPTY_INITIALIZER;
1351 match_format(match, &s, priority);
1352 return ds_steal_cstr(&s);
1356 match_print(const struct match *match)
1358 char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
1363 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1364 * with minimatch_destroy(). */
1366 minimatch_init(struct minimatch *dst, const struct match *src)
1368 struct miniflow tmp;
1370 miniflow_map_init(&tmp, &src->wc.masks);
1371 /* Allocate two consecutive miniflows. */
1372 miniflow_alloc(dst->flows, 2, &tmp);
1373 miniflow_init(dst->flow, &src->flow);
1374 minimask_init(dst->mask, &src->wc);
1377 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1378 * with minimatch_destroy(). */
1380 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1382 /* Allocate two consecutive miniflows. */
1383 size_t data_size = miniflow_alloc(dst->flows, 2, &src->mask->masks);
1385 memcpy(miniflow_values(dst->flow),
1386 miniflow_get_values(src->flow), data_size);
1387 memcpy(miniflow_values(&dst->mask->masks),
1388 miniflow_get_values(&src->mask->masks), data_size);
1391 /* Initializes 'dst' with the data in 'src', destroying 'src'. The caller must
1392 * eventually free 'dst' with minimatch_destroy(). */
1394 minimatch_move(struct minimatch *dst, struct minimatch *src)
1396 dst->flow = src->flow;
1397 dst->mask = src->mask;
1400 /* Frees any memory owned by 'match'. Does not free the storage in which
1401 * 'match' itself resides; the caller is responsible for that. */
1403 minimatch_destroy(struct minimatch *match)
1408 /* Initializes 'dst' as a copy of 'src'. */
1410 minimatch_expand(const struct minimatch *src, struct match *dst)
1412 miniflow_expand(src->flow, &dst->flow);
1413 minimask_expand(src->mask, &dst->wc);
1414 memset(&dst->tun_md, 0, sizeof dst->tun_md);
1417 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
1419 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1421 return minimask_equal(a->mask, b->mask)
1422 && miniflow_equal(a->flow, b->flow);
1425 /* Returns true if 'target' satisifies 'match', that is, if each bit for which
1426 * 'match' specifies a particular value has the correct value in 'target'.
1428 * This function is equivalent to miniflow_equal_flow_in_minimask(&match->flow,
1429 * target, &match->mask) but it is faster because of the invariant that
1430 * match->flow.map and match->mask.map are the same. */
1432 minimatch_matches_flow(const struct minimatch *match,
1433 const struct flow *target)
1435 const uint64_t *flowp = miniflow_get_values(match->flow);
1436 const uint64_t *maskp = miniflow_get_values(&match->mask->masks);
1439 FLOWMAP_FOR_EACH_INDEX(idx, match->flow->map) {
1440 if ((*flowp++ ^ flow_u64_value(target, idx)) & *maskp++) {
1448 /* Appends a string representation of 'match' to 's'. If 'priority' is
1449 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1451 minimatch_format(const struct minimatch *match, struct ds *s, int priority)
1453 struct match megamatch;
1455 minimatch_expand(match, &megamatch);
1456 match_format(&megamatch, s, priority);
1459 /* Converts 'match' to a string and returns the string. If 'priority' is
1460 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1461 * must free the string (with free()). */
1463 minimatch_to_string(const struct minimatch *match, int priority)
1465 struct match megamatch;
1467 minimatch_expand(match, &megamatch);
1468 return match_to_string(&megamatch, priority);