2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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.
20 #include "byte-order.h"
21 #include "dynamic-string.h"
24 #include "tun-metadata.h"
26 /* Converts the flow in 'flow' into a match in 'match', with the given
29 match_init(struct match *match,
30 const struct flow *flow, const struct flow_wildcards *wc)
34 match_zero_wildcarded_fields(match);
35 memset(&match->tun_md, 0, sizeof match->tun_md);
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. */
42 match_wc_init(struct match *match, const struct flow *flow)
46 flow_wildcards_init_for_packet(&match->wc, flow);
47 WC_MASK_FIELD(&match->wc, regs);
48 WC_MASK_FIELD(&match->wc, metadata);
50 memset(&match->tun_md, 0, sizeof match->tun_md);
53 /* Initializes 'match' as a "catch-all" match that matches every packet. */
55 match_init_catchall(struct match *match)
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);
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.
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.
71 match_zero_wildcarded_fields(struct match *match)
73 flow_zero_wildcards(&match->flow, &match->wc);
77 match_set_dp_hash(struct match *match, uint32_t value)
79 match_set_dp_hash_masked(match, value, UINT32_MAX);
83 match_set_dp_hash_masked(struct match *match, uint32_t value, uint32_t mask)
85 match->wc.masks.dp_hash = mask;
86 match->flow.dp_hash = value & mask;
90 match_set_recirc_id(struct match *match, uint32_t value)
92 match->flow.recirc_id = value;
93 match->wc.masks.recirc_id = UINT32_MAX;
97 match_set_conj_id(struct match *match, uint32_t value)
99 match->flow.conj_id = value;
100 match->wc.masks.conj_id = UINT32_MAX;
104 match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
106 match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
110 match_set_reg_masked(struct match *match, unsigned int reg_idx,
111 uint32_t value, uint32_t mask)
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;
119 match_set_xreg(struct match *match, unsigned int xreg_idx, uint64_t value)
121 match_set_xreg_masked(match, xreg_idx, value, UINT64_MAX);
125 match_set_xreg_masked(struct match *match, unsigned int xreg_idx,
126 uint64_t value, uint64_t mask)
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);
134 match_set_actset_output(struct match *match, ofp_port_t actset_output)
136 match->wc.masks.actset_output = u16_to_ofp(UINT16_MAX);
137 match->flow.actset_output = actset_output;
141 match_set_metadata(struct match *match, ovs_be64 metadata)
143 match_set_metadata_masked(match, metadata, OVS_BE64_MAX);
147 match_set_metadata_masked(struct match *match,
148 ovs_be64 metadata, ovs_be64 mask)
150 match->wc.masks.metadata = mask;
151 match->flow.metadata = metadata & mask;
155 match_set_tun_id(struct match *match, ovs_be64 tun_id)
157 match_set_tun_id_masked(match, tun_id, OVS_BE64_MAX);
161 match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
163 match->wc.masks.tunnel.tun_id = mask;
164 match->flow.tunnel.tun_id = tun_id & mask;
168 match_set_tun_src(struct match *match, ovs_be32 src)
170 match_set_tun_src_masked(match, src, OVS_BE32_MAX);
174 match_set_tun_src_masked(struct match *match, ovs_be32 src, ovs_be32 mask)
176 match->wc.masks.tunnel.ip_src = mask;
177 match->flow.tunnel.ip_src = src & mask;
181 match_set_tun_dst(struct match *match, ovs_be32 dst)
183 match_set_tun_dst_masked(match, dst, OVS_BE32_MAX);
187 match_set_tun_dst_masked(struct match *match, ovs_be32 dst, ovs_be32 mask)
189 match->wc.masks.tunnel.ip_dst = mask;
190 match->flow.tunnel.ip_dst = dst & mask;
194 match_set_tun_ttl(struct match *match, uint8_t ttl)
196 match_set_tun_ttl_masked(match, ttl, UINT8_MAX);
200 match_set_tun_ttl_masked(struct match *match, uint8_t ttl, uint8_t mask)
202 match->wc.masks.tunnel.ip_ttl = mask;
203 match->flow.tunnel.ip_ttl = ttl & mask;
207 match_set_tun_tos(struct match *match, uint8_t tos)
209 match_set_tun_tos_masked(match, tos, UINT8_MAX);
213 match_set_tun_tos_masked(struct match *match, uint8_t tos, uint8_t mask)
215 match->wc.masks.tunnel.ip_tos = mask;
216 match->flow.tunnel.ip_tos = tos & mask;
220 match_set_tun_flags(struct match *match, uint16_t flags)
222 match_set_tun_flags_masked(match, flags, UINT16_MAX);
226 match_set_tun_flags_masked(struct match *match, uint16_t flags, uint16_t mask)
228 mask &= FLOW_TNL_PUB_F_MASK;
230 match->wc.masks.tunnel.flags = mask;
231 match->flow.tunnel.flags = flags & mask;
235 match_set_tun_gbp_id_masked(struct match *match, ovs_be16 gbp_id, ovs_be16 mask)
237 match->wc.masks.tunnel.gbp_id = mask;
238 match->flow.tunnel.gbp_id = gbp_id & mask;
242 match_set_tun_gbp_id(struct match *match, ovs_be16 gbp_id)
244 match_set_tun_gbp_id_masked(match, gbp_id, OVS_BE16_MAX);
248 match_set_tun_gbp_flags_masked(struct match *match, uint8_t flags, uint8_t mask)
250 match->wc.masks.tunnel.gbp_flags = mask;
251 match->flow.tunnel.gbp_flags = flags & mask;
255 match_set_tun_gbp_flags(struct match *match, uint8_t flags)
257 match_set_tun_gbp_flags_masked(match, flags, UINT8_MAX);
261 match_set_in_port(struct match *match, ofp_port_t ofp_port)
263 match->wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
264 match->flow.in_port.ofp_port = ofp_port;
268 match_set_skb_priority(struct match *match, uint32_t skb_priority)
270 match->wc.masks.skb_priority = UINT32_MAX;
271 match->flow.skb_priority = skb_priority;
275 match_set_pkt_mark(struct match *match, uint32_t pkt_mark)
277 match_set_pkt_mark_masked(match, pkt_mark, UINT32_MAX);
281 match_set_pkt_mark_masked(struct match *match, uint32_t pkt_mark, uint32_t mask)
283 match->flow.pkt_mark = pkt_mark & mask;
284 match->wc.masks.pkt_mark = mask;
288 match_set_ct_state(struct match *match, uint32_t ct_state)
290 match_set_ct_state_masked(match, ct_state, UINT32_MAX);
294 match_set_ct_state_masked(struct match *match, uint32_t ct_state, uint32_t mask)
296 match->flow.ct_state = ct_state & mask & UINT16_MAX;
297 match->wc.masks.ct_state = mask & UINT16_MAX;
301 match_set_ct_zone(struct match *match, uint16_t ct_zone)
303 match->flow.ct_zone = ct_zone;
304 match->wc.masks.ct_zone = UINT16_MAX;
308 match_set_ct_mark(struct match *match, uint32_t ct_mark)
310 match_set_ct_mark_masked(match, ct_mark, UINT32_MAX);
314 match_set_ct_mark_masked(struct match *match, uint32_t ct_mark,
317 match->flow.ct_mark = ct_mark & mask;
318 match->wc.masks.ct_mark = mask;
322 match_set_dl_type(struct match *match, ovs_be16 dl_type)
324 match->wc.masks.dl_type = OVS_BE16_MAX;
325 match->flow.dl_type = dl_type;
328 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
329 * exactly. 'mask_dst' is set to all 1s. */
331 set_eth(const struct eth_addr value_src,
332 struct eth_addr *value_dst,
333 struct eth_addr *mask_dst)
335 *value_dst = value_src;
336 *mask_dst = eth_addr_exact;
339 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
340 * after each byte is ANDed with the appropriate byte in 'mask_src'.
341 * 'mask_dst' is set to 'mask_src' */
343 set_eth_masked(const struct eth_addr value_src,
344 const struct eth_addr mask_src,
345 struct eth_addr *value_dst, struct eth_addr *mask_dst)
349 for (i = 0; i < ARRAY_SIZE(value_dst->be16); i++) {
350 value_dst->be16[i] = value_src.be16[i] & mask_src.be16[i];
352 *mask_dst = mask_src;
355 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
358 match_set_dl_src(struct match *match, const struct eth_addr dl_src)
360 set_eth(dl_src, &match->flow.dl_src, &match->wc.masks.dl_src);
363 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
364 * after each byte is ANDed with the appropriate byte in 'mask'. */
366 match_set_dl_src_masked(struct match *match,
367 const struct eth_addr dl_src,
368 const struct eth_addr mask)
370 set_eth_masked(dl_src, mask, &match->flow.dl_src, &match->wc.masks.dl_src);
373 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
376 match_set_dl_dst(struct match *match, const struct eth_addr dl_dst)
378 set_eth(dl_dst, &match->flow.dl_dst, &match->wc.masks.dl_dst);
381 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
382 * byte is ANDed with the appropriate byte in 'mask'.
384 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
385 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
387 match_set_dl_dst_masked(struct match *match,
388 const struct eth_addr dl_dst,
389 const struct eth_addr mask)
391 set_eth_masked(dl_dst, mask, &match->flow.dl_dst, &match->wc.masks.dl_dst);
395 match_set_dl_tci(struct match *match, ovs_be16 tci)
397 match_set_dl_tci_masked(match, tci, htons(0xffff));
401 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
403 match->flow.vlan_tci = tci & mask;
404 match->wc.masks.vlan_tci = mask;
407 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
408 * wildcarded, then 'match' will match a packet regardless of whether it has an
409 * 802.1Q header or not. */
411 match_set_any_vid(struct match *match)
413 if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
414 match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
415 match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
417 match_set_dl_tci_masked(match, htons(0), htons(0));
421 /* Modifies 'match' depending on 'dl_vlan':
423 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
424 * without an 802.1Q header.
426 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
427 * VID equals the low 12 bits of 'dl_vlan'.
430 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
432 flow_set_dl_vlan(&match->flow, dl_vlan);
433 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
434 match->wc.masks.vlan_tci = OVS_BE16_MAX;
436 match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
440 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
441 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
444 match_set_vlan_vid(struct match *match, ovs_be16 vid)
446 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
450 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
451 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
452 * plus CFI), with the corresponding 'mask'. */
454 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
456 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
457 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
460 flow_set_vlan_vid(&match->flow, vid & mask);
461 match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
464 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
465 * wildcarded, then 'match' will match a packet regardless of whether it has an
466 * 802.1Q header or not. */
468 match_set_any_pcp(struct match *match)
470 if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
471 match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
472 match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
474 match_set_dl_tci_masked(match, htons(0), htons(0));
478 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
479 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
481 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
483 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
484 match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
487 /* Modifies 'match' so that the MPLS label 'idx' matches 'lse' exactly. */
489 match_set_mpls_lse(struct match *match, int idx, ovs_be32 lse)
491 match->wc.masks.mpls_lse[idx] = OVS_BE32_MAX;
492 match->flow.mpls_lse[idx] = lse;
495 /* Modifies 'match' so that the MPLS label is wildcarded. */
497 match_set_any_mpls_label(struct match *match, int idx)
499 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_LABEL_MASK);
500 flow_set_mpls_label(&match->flow, idx, htonl(0));
503 /* Modifies 'match' so that it matches only packets with an MPLS header whose
504 * label equals the low 20 bits of 'mpls_label'. */
506 match_set_mpls_label(struct match *match, int idx, ovs_be32 mpls_label)
508 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_LABEL_MASK);
509 flow_set_mpls_label(&match->flow, idx, mpls_label);
512 /* Modifies 'match' so that the MPLS TC is wildcarded. */
514 match_set_any_mpls_tc(struct match *match, int idx)
516 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TC_MASK);
517 flow_set_mpls_tc(&match->flow, idx, 0);
520 /* Modifies 'match' so that it matches only packets with an MPLS header whose
521 * Traffic Class equals the low 3 bits of 'mpls_tc'. */
523 match_set_mpls_tc(struct match *match, int idx, uint8_t mpls_tc)
525 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TC_MASK);
526 flow_set_mpls_tc(&match->flow, idx, mpls_tc);
529 /* Modifies 'match' so that the MPLS stack flag is wildcarded. */
531 match_set_any_mpls_bos(struct match *match, int idx)
533 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_BOS_MASK);
534 flow_set_mpls_bos(&match->flow, idx, 0);
537 /* Modifies 'match' so that it matches only packets with an MPLS header whose
538 * Stack Flag equals the lower bit of 'mpls_bos' */
540 match_set_mpls_bos(struct match *match, int idx, uint8_t mpls_bos)
542 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_BOS_MASK);
543 flow_set_mpls_bos(&match->flow, idx, mpls_bos);
546 /* Modifies 'match' so that the MPLS LSE is wildcarded. */
548 match_set_any_mpls_lse(struct match *match, int idx)
550 match->wc.masks.mpls_lse[idx] = htonl(0);
551 flow_set_mpls_lse(&match->flow, idx, htonl(0));
555 match_set_tp_src(struct match *match, ovs_be16 tp_src)
557 match_set_tp_src_masked(match, tp_src, OVS_BE16_MAX);
561 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
563 match->flow.tp_src = port & mask;
564 match->wc.masks.tp_src = mask;
568 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
570 match_set_tp_dst_masked(match, tp_dst, OVS_BE16_MAX);
574 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
576 match->flow.tp_dst = port & mask;
577 match->wc.masks.tp_dst = mask;
581 match_set_tcp_flags(struct match *match, ovs_be16 flags)
583 match_set_tcp_flags_masked(match, flags, OVS_BE16_MAX);
587 match_set_tcp_flags_masked(struct match *match, ovs_be16 flags, ovs_be16 mask)
589 match->flow.tcp_flags = flags & mask;
590 match->wc.masks.tcp_flags = mask;
594 match_set_nw_proto(struct match *match, uint8_t nw_proto)
596 match->flow.nw_proto = nw_proto;
597 match->wc.masks.nw_proto = UINT8_MAX;
601 match_set_nw_src(struct match *match, ovs_be32 nw_src)
603 match->flow.nw_src = nw_src;
604 match->wc.masks.nw_src = OVS_BE32_MAX;
608 match_set_nw_src_masked(struct match *match,
609 ovs_be32 nw_src, ovs_be32 mask)
611 match->flow.nw_src = nw_src & mask;
612 match->wc.masks.nw_src = mask;
616 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
618 match->flow.nw_dst = nw_dst;
619 match->wc.masks.nw_dst = OVS_BE32_MAX;
623 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
625 match->flow.nw_dst = ip & mask;
626 match->wc.masks.nw_dst = mask;
630 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
632 match->wc.masks.nw_tos |= IP_DSCP_MASK;
633 match->flow.nw_tos &= ~IP_DSCP_MASK;
634 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
638 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
640 match->wc.masks.nw_tos |= IP_ECN_MASK;
641 match->flow.nw_tos &= ~IP_ECN_MASK;
642 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
646 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
648 match->wc.masks.nw_ttl = UINT8_MAX;
649 match->flow.nw_ttl = nw_ttl;
653 match_set_nw_frag(struct match *match, uint8_t nw_frag)
655 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
656 match->flow.nw_frag = nw_frag;
660 match_set_nw_frag_masked(struct match *match,
661 uint8_t nw_frag, uint8_t mask)
663 match->flow.nw_frag = nw_frag & mask;
664 match->wc.masks.nw_frag = mask;
668 match_set_icmp_type(struct match *match, uint8_t icmp_type)
670 match_set_tp_src(match, htons(icmp_type));
674 match_set_icmp_code(struct match *match, uint8_t icmp_code)
676 match_set_tp_dst(match, htons(icmp_code));
680 match_set_arp_sha(struct match *match, const struct eth_addr sha)
682 match->flow.arp_sha = sha;
683 match->wc.masks.arp_sha = eth_addr_exact;
687 match_set_arp_sha_masked(struct match *match,
688 const struct eth_addr arp_sha,
689 const struct eth_addr mask)
691 set_eth_masked(arp_sha, mask,
692 &match->flow.arp_sha, &match->wc.masks.arp_sha);
696 match_set_arp_tha(struct match *match, const struct eth_addr tha)
698 match->flow.arp_tha = tha;
699 match->wc.masks.arp_tha = eth_addr_exact;
703 match_set_arp_tha_masked(struct match *match,
704 const struct eth_addr arp_tha,
705 const struct eth_addr mask)
707 set_eth_masked(arp_tha, mask,
708 &match->flow.arp_tha, &match->wc.masks.arp_tha);
712 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
714 match->flow.ipv6_src = *src;
715 match->wc.masks.ipv6_src = in6addr_exact;
719 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
720 const struct in6_addr *mask)
722 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
723 match->wc.masks.ipv6_src = *mask;
727 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
729 match->flow.ipv6_dst = *dst;
730 match->wc.masks.ipv6_dst = in6addr_exact;
734 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
735 const struct in6_addr *mask)
737 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
738 match->wc.masks.ipv6_dst = *mask;
742 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
744 match->wc.masks.ipv6_label = OVS_BE32_MAX;
745 match->flow.ipv6_label = ipv6_label;
750 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
753 match->flow.ipv6_label = ipv6_label & mask;
754 match->wc.masks.ipv6_label = mask;
758 match_set_nd_target(struct match *match, const struct in6_addr *target)
760 match->flow.nd_target = *target;
761 match->wc.masks.nd_target = in6addr_exact;
765 match_set_nd_target_masked(struct match *match,
766 const struct in6_addr *target,
767 const struct in6_addr *mask)
769 match->flow.nd_target = ipv6_addr_bitand(target, mask);
770 match->wc.masks.nd_target = *mask;
773 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
774 * values for fixed fields, otherwise false. */
776 match_equal(const struct match *a, const struct match *b)
778 return (flow_wildcards_equal(&a->wc, &b->wc)
779 && flow_equal(&a->flow, &b->flow));
782 /* Returns a hash value for the flow and wildcards in 'match', starting from
785 match_hash(const struct match *match, uint32_t basis)
787 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
791 match_has_default_recirc_id(const struct match *m)
793 return m->flow.recirc_id == 0 && (m->wc.masks.recirc_id == UINT32_MAX ||
794 m->wc.masks.recirc_id == 0);
798 match_has_default_dp_hash(const struct match *m)
800 return ((m->flow.dp_hash | m->wc.masks.dp_hash) == 0);
803 /* Return true if the hidden fields of the match are set to the default values.
804 * The default values equals to those set up by match_init_hidden_fields(). */
806 match_has_default_hidden_fields(const struct match *m)
808 return match_has_default_recirc_id(m) && match_has_default_dp_hash(m);
812 match_init_hidden_fields(struct match *m)
814 match_set_recirc_id(m, 0);
815 match_set_dp_hash_masked(m, 0, 0);
819 format_eth_masked(struct ds *s, const char *name,
820 const struct eth_addr eth, const struct eth_addr mask)
822 if (!eth_addr_is_zero(mask)) {
823 ds_put_format(s, "%s=", name);
824 eth_format_masked(eth, &mask, s);
830 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
834 ds_put_format(s, "%s=", name);
835 ip_format_masked(ip, netmask, s);
841 format_ipv6_netmask(struct ds *s, const char *name,
842 const struct in6_addr *addr,
843 const struct in6_addr *netmask)
845 if (!ipv6_mask_is_any(netmask)) {
846 ds_put_format(s, "%s=", name);
847 print_ipv6_masked(s, addr, netmask);
853 format_uint16_masked(struct ds *s, const char *name,
854 uint16_t value, uint16_t mask)
857 ds_put_format(s, "%s=", name);
858 if (mask == UINT16_MAX) {
859 ds_put_format(s, "%"PRIu16, value);
861 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16, value, mask);
868 format_be16_masked(struct ds *s, const char *name,
869 ovs_be16 value, ovs_be16 mask)
871 if (mask != htons(0)) {
872 ds_put_format(s, "%s=", name);
873 if (mask == OVS_BE16_MAX) {
874 ds_put_format(s, "%"PRIu16, ntohs(value));
876 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
877 ntohs(value), ntohs(mask));
884 format_be32_masked(struct ds *s, const char *name,
885 ovs_be32 value, ovs_be32 mask)
887 if (mask != htonl(0)) {
888 ds_put_format(s, "%s=", name);
889 if (mask == OVS_BE32_MAX) {
890 ds_put_format(s, "%"PRIu32, ntohl(value));
892 ds_put_format(s, "0x%"PRIx32"/0x%"PRIx32,
893 ntohl(value), ntohl(mask));
900 format_uint32_masked(struct ds *s, const char *name,
901 uint32_t value, uint32_t mask)
904 ds_put_format(s, "%s=%#"PRIx32, name, value);
905 if (mask != UINT32_MAX) {
906 ds_put_format(s, "/%#"PRIx32, mask);
913 format_be64_masked(struct ds *s, const char *name,
914 ovs_be64 value, ovs_be64 mask)
916 if (mask != htonll(0)) {
917 ds_put_format(s, "%s=%#"PRIx64, name, ntohll(value));
918 if (mask != OVS_BE64_MAX) {
919 ds_put_format(s, "/%#"PRIx64, ntohll(mask));
926 format_flow_tunnel(struct ds *s, const struct match *match)
928 const struct flow_wildcards *wc = &match->wc;
929 const struct flow_tnl *tnl = &match->flow.tunnel;
931 format_be64_masked(s, "tun_id", tnl->tun_id, wc->masks.tunnel.tun_id);
932 format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
933 format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
935 if (wc->masks.tunnel.gbp_id) {
936 format_be16_masked(s, "tun_gbp_id", tnl->gbp_id,
937 wc->masks.tunnel.gbp_id);
940 if (wc->masks.tunnel.gbp_flags) {
941 ds_put_format(s, "tun_gbp_flags=%#"PRIx8",", tnl->gbp_flags);
944 if (wc->masks.tunnel.ip_tos) {
945 ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
947 if (wc->masks.tunnel.ip_ttl) {
948 ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
950 if (wc->masks.tunnel.flags) {
951 format_flags_masked(s, "tun_flags", flow_tun_flag_to_string,
953 wc->masks.tunnel.flags & FLOW_TNL_F_MASK,
957 tun_metadata_match_format(s, match);
960 /* Appends a string representation of 'match' to 's'. If 'priority' is
961 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
963 match_format(const struct match *match, struct ds *s, int priority)
965 const struct flow_wildcards *wc = &match->wc;
966 size_t start_len = s->length;
967 const struct flow *f = &match->flow;
968 bool skip_type = false;
969 bool skip_proto = false;
973 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 34);
975 if (priority != OFP_DEFAULT_PRIORITY) {
976 ds_put_format(s, "priority=%d,", priority);
979 format_uint32_masked(s, "pkt_mark", f->pkt_mark, wc->masks.pkt_mark);
981 if (wc->masks.recirc_id) {
982 format_uint32_masked(s, "recirc_id", f->recirc_id,
983 wc->masks.recirc_id);
986 if (wc->masks.dp_hash) {
987 format_uint32_masked(s, "dp_hash", f->dp_hash,
991 if (wc->masks.conj_id) {
992 ds_put_format(s, "conj_id=%"PRIu32",", f->conj_id);
995 if (wc->masks.skb_priority) {
996 ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority);
999 if (wc->masks.actset_output) {
1000 ds_put_cstr(s, "actset_output=");
1001 ofputil_format_port(f->actset_output, s);
1002 ds_put_char(s, ',');
1005 if (wc->masks.ct_state) {
1006 if (wc->masks.ct_state == UINT16_MAX) {
1007 ds_put_cstr(s, "ct_state=");
1009 format_flags(s, ct_state_to_string, f->ct_state, '|');
1011 ds_put_cstr(s, "0"); /* No state. */
1014 format_flags_masked(s, "ct_state", ct_state_to_string,
1015 f->ct_state, wc->masks.ct_state, UINT16_MAX);
1017 ds_put_char(s, ',');
1020 if (wc->masks.ct_zone) {
1021 format_uint16_masked(s, "ct_zone", f->ct_zone, wc->masks.ct_zone);
1024 if (wc->masks.ct_mark) {
1025 format_uint32_masked(s, "ct_mark", f->ct_mark, wc->masks.ct_mark);
1028 if (wc->masks.dl_type) {
1030 if (f->dl_type == htons(ETH_TYPE_IP)) {
1031 if (wc->masks.nw_proto) {
1033 if (f->nw_proto == IPPROTO_ICMP) {
1034 ds_put_cstr(s, "icmp,");
1035 } else if (f->nw_proto == IPPROTO_IGMP) {
1036 ds_put_cstr(s, "igmp,");
1037 } else if (f->nw_proto == IPPROTO_TCP) {
1038 ds_put_cstr(s, "tcp,");
1039 } else if (f->nw_proto == IPPROTO_UDP) {
1040 ds_put_cstr(s, "udp,");
1041 } else if (f->nw_proto == IPPROTO_SCTP) {
1042 ds_put_cstr(s, "sctp,");
1044 ds_put_cstr(s, "ip,");
1048 ds_put_cstr(s, "ip,");
1050 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
1051 if (wc->masks.nw_proto) {
1053 if (f->nw_proto == IPPROTO_ICMPV6) {
1054 ds_put_cstr(s, "icmp6,");
1055 } else if (f->nw_proto == IPPROTO_TCP) {
1056 ds_put_cstr(s, "tcp6,");
1057 } else if (f->nw_proto == IPPROTO_UDP) {
1058 ds_put_cstr(s, "udp6,");
1059 } else if (f->nw_proto == IPPROTO_SCTP) {
1060 ds_put_cstr(s, "sctp6,");
1062 ds_put_cstr(s, "ipv6,");
1066 ds_put_cstr(s, "ipv6,");
1068 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
1069 ds_put_cstr(s, "arp,");
1070 } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
1071 ds_put_cstr(s, "rarp,");
1072 } else if (f->dl_type == htons(ETH_TYPE_MPLS)) {
1073 ds_put_cstr(s, "mpls,");
1074 } else if (f->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
1075 ds_put_cstr(s, "mplsm,");
1080 for (i = 0; i < FLOW_N_REGS; i++) {
1081 #define REGNAME_LEN 20
1082 char regname[REGNAME_LEN];
1083 if (snprintf(regname, REGNAME_LEN, "reg%d", i) >= REGNAME_LEN) {
1084 strcpy(regname, "reg?");
1086 format_uint32_masked(s, regname, f->regs[i], wc->masks.regs[i]);
1089 format_flow_tunnel(s, match);
1091 format_be64_masked(s, "metadata", f->metadata, wc->masks.metadata);
1093 if (wc->masks.in_port.ofp_port) {
1094 ds_put_cstr(s, "in_port=");
1095 ofputil_format_port(f->in_port.ofp_port, s);
1096 ds_put_char(s, ',');
1098 if (wc->masks.vlan_tci) {
1099 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
1100 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
1101 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
1103 if (cfi && f->vlan_tci & htons(VLAN_CFI)
1104 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
1105 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
1106 && (vid_mask || pcp_mask)) {
1108 ds_put_format(s, "dl_vlan=%"PRIu16",",
1109 vlan_tci_to_vid(f->vlan_tci));
1112 ds_put_format(s, "dl_vlan_pcp=%d,",
1113 vlan_tci_to_pcp(f->vlan_tci));
1115 } else if (wc->masks.vlan_tci == htons(0xffff)) {
1116 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
1118 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
1119 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
1122 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
1123 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
1124 if (!skip_type && wc->masks.dl_type) {
1125 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
1127 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
1128 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
1129 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
1130 if (wc->masks.ipv6_label) {
1131 if (wc->masks.ipv6_label == OVS_BE32_MAX) {
1132 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
1133 ntohl(f->ipv6_label));
1135 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
1136 ntohl(f->ipv6_label),
1137 ntohl(wc->masks.ipv6_label));
1140 } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
1141 f->dl_type == htons(ETH_TYPE_RARP)) {
1142 format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
1143 format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
1145 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
1146 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
1148 if (!skip_proto && wc->masks.nw_proto) {
1149 if (f->dl_type == htons(ETH_TYPE_ARP) ||
1150 f->dl_type == htons(ETH_TYPE_RARP)) {
1151 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
1153 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
1156 if (f->dl_type == htons(ETH_TYPE_ARP) ||
1157 f->dl_type == htons(ETH_TYPE_RARP)) {
1158 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
1159 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
1161 if (wc->masks.nw_tos & IP_DSCP_MASK) {
1162 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
1164 if (wc->masks.nw_tos & IP_ECN_MASK) {
1165 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
1167 if (wc->masks.nw_ttl) {
1168 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
1170 if (wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
1171 ds_put_format(s, "mpls_label=%"PRIu32",",
1172 mpls_lse_to_label(f->mpls_lse[0]));
1174 if (wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
1175 ds_put_format(s, "mpls_tc=%"PRIu8",",
1176 mpls_lse_to_tc(f->mpls_lse[0]));
1178 if (wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK)) {
1179 ds_put_format(s, "mpls_ttl=%"PRIu8",",
1180 mpls_lse_to_ttl(f->mpls_lse[0]));
1182 if (wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
1183 ds_put_format(s, "mpls_bos=%"PRIu8",",
1184 mpls_lse_to_bos(f->mpls_lse[0]));
1186 format_be32_masked(s, "mpls_lse1", f->mpls_lse[1], wc->masks.mpls_lse[1]);
1187 format_be32_masked(s, "mpls_lse2", f->mpls_lse[2], wc->masks.mpls_lse[2]);
1189 switch (wc->masks.nw_frag) {
1190 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
1191 ds_put_format(s, "nw_frag=%s,",
1192 f->nw_frag & FLOW_NW_FRAG_ANY
1193 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
1194 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
1197 case FLOW_NW_FRAG_ANY:
1198 ds_put_format(s, "nw_frag=%s,",
1199 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
1202 case FLOW_NW_FRAG_LATER:
1203 ds_put_format(s, "nw_frag=%s,",
1204 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
1207 if (f->dl_type == htons(ETH_TYPE_IP) &&
1208 f->nw_proto == IPPROTO_ICMP) {
1209 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1210 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1211 } else if (f->dl_type == htons(ETH_TYPE_IP) &&
1212 f->nw_proto == IPPROTO_IGMP) {
1213 format_be16_masked(s, "igmp_type", f->tp_src, wc->masks.tp_src);
1214 format_be16_masked(s, "igmp_code", f->tp_dst, wc->masks.tp_dst);
1215 } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
1216 f->nw_proto == IPPROTO_ICMPV6) {
1217 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1218 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1219 format_ipv6_netmask(s, "nd_target", &f->nd_target,
1220 &wc->masks.nd_target);
1221 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
1222 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
1224 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
1225 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
1227 if (is_ip_any(f) && f->nw_proto == IPPROTO_TCP && wc->masks.tcp_flags) {
1228 format_flags_masked(s, "tcp_flags", packet_tcp_flag_to_string,
1229 ntohs(f->tcp_flags), TCP_FLAGS(wc->masks.tcp_flags),
1230 TCP_FLAGS(OVS_BE16_MAX));
1233 if (s->length > start_len) {
1238 /* Converts 'match' to a string and returns the string. If 'priority' is
1239 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1240 * must free the string (with free()). */
1242 match_to_string(const struct match *match, int priority)
1244 struct ds s = DS_EMPTY_INITIALIZER;
1245 match_format(match, &s, priority);
1246 return ds_steal_cstr(&s);
1250 match_print(const struct match *match)
1252 char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
1257 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1258 * with minimatch_destroy(). */
1260 minimatch_init(struct minimatch *dst, const struct match *src)
1262 struct miniflow tmp;
1264 miniflow_map_init(&tmp, &src->wc.masks);
1265 /* Allocate two consecutive miniflows. */
1266 miniflow_alloc(dst->flows, 2, &tmp);
1267 miniflow_init(dst->flow, &src->flow);
1268 minimask_init(dst->mask, &src->wc);
1271 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1272 * with minimatch_destroy(). */
1274 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1276 /* Allocate two consecutive miniflows. */
1277 size_t data_size = miniflow_alloc(dst->flows, 2, &src->mask->masks);
1279 memcpy(miniflow_values(dst->flow),
1280 miniflow_get_values(src->flow), data_size);
1281 memcpy(miniflow_values(&dst->mask->masks),
1282 miniflow_get_values(&src->mask->masks), data_size);
1285 /* Initializes 'dst' with the data in 'src', destroying 'src'. The caller must
1286 * eventually free 'dst' with minimatch_destroy(). */
1288 minimatch_move(struct minimatch *dst, struct minimatch *src)
1290 dst->flow = src->flow;
1291 dst->mask = src->mask;
1294 /* Frees any memory owned by 'match'. Does not free the storage in which
1295 * 'match' itself resides; the caller is responsible for that. */
1297 minimatch_destroy(struct minimatch *match)
1302 /* Initializes 'dst' as a copy of 'src'. */
1304 minimatch_expand(const struct minimatch *src, struct match *dst)
1306 miniflow_expand(src->flow, &dst->flow);
1307 minimask_expand(src->mask, &dst->wc);
1308 memset(&dst->tun_md, 0, sizeof dst->tun_md);
1311 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
1313 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1315 return minimask_equal(a->mask, b->mask)
1316 && miniflow_equal(a->flow, b->flow);
1319 /* Returns true if 'target' satisifies 'match', that is, if each bit for which
1320 * 'match' specifies a particular value has the correct value in 'target'.
1322 * This function is equivalent to miniflow_equal_flow_in_minimask(&match->flow,
1323 * target, &match->mask) but it is faster because of the invariant that
1324 * match->flow.map and match->mask.map are the same. */
1326 minimatch_matches_flow(const struct minimatch *match,
1327 const struct flow *target)
1329 const uint64_t *flowp = miniflow_get_values(match->flow);
1330 const uint64_t *maskp = miniflow_get_values(&match->mask->masks);
1333 FLOWMAP_FOR_EACH_INDEX(idx, match->flow->map) {
1334 if ((*flowp++ ^ flow_u64_value(target, idx)) & *maskp++) {
1342 /* Appends a string representation of 'match' to 's'. If 'priority' is
1343 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1345 minimatch_format(const struct minimatch *match, struct ds *s, int priority)
1347 struct match megamatch;
1349 minimatch_expand(match, &megamatch);
1350 match_format(&megamatch, s, priority);
1353 /* Converts 'match' to a string and returns the string. If 'priority' is
1354 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1355 * must free the string (with free()). */
1357 minimatch_to_string(const struct minimatch *match, int priority)
1359 struct match megamatch;
1361 minimatch_expand(match, &megamatch);
1362 return match_to_string(&megamatch, priority);