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_dl_type(struct match *match, ovs_be16 dl_type)
290 match->wc.masks.dl_type = OVS_BE16_MAX;
291 match->flow.dl_type = dl_type;
294 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
295 * exactly. 'mask_dst' is set to all 1s. */
297 set_eth(const uint8_t value_src[ETH_ADDR_LEN],
298 uint8_t value_dst[ETH_ADDR_LEN],
299 uint8_t mask_dst[ETH_ADDR_LEN])
301 memcpy(value_dst, value_src, ETH_ADDR_LEN);
302 memset(mask_dst, 0xff, ETH_ADDR_LEN);
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' */
309 set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
310 const uint8_t mask_src[ETH_ADDR_LEN],
311 uint8_t value_dst[ETH_ADDR_LEN],
312 uint8_t mask_dst[ETH_ADDR_LEN])
316 for (i = 0; i < ETH_ADDR_LEN; i++) {
317 value_dst[i] = value_src[i] & mask_src[i];
318 mask_dst[i] = mask_src[i];
322 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
325 match_set_dl_src(struct match *match, const uint8_t dl_src[ETH_ADDR_LEN])
327 set_eth(dl_src, match->flow.dl_src, match->wc.masks.dl_src);
330 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
331 * after each byte is ANDed with the appropriate byte in 'mask'. */
333 match_set_dl_src_masked(struct match *match,
334 const uint8_t dl_src[ETH_ADDR_LEN],
335 const uint8_t mask[ETH_ADDR_LEN])
337 set_eth_masked(dl_src, mask, match->flow.dl_src, match->wc.masks.dl_src);
340 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
343 match_set_dl_dst(struct match *match, const uint8_t dl_dst[ETH_ADDR_LEN])
345 set_eth(dl_dst, match->flow.dl_dst, match->wc.masks.dl_dst);
348 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
349 * byte is ANDed with the appropriate byte in 'mask'.
351 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
352 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
354 match_set_dl_dst_masked(struct match *match,
355 const uint8_t dl_dst[ETH_ADDR_LEN],
356 const uint8_t mask[ETH_ADDR_LEN])
358 set_eth_masked(dl_dst, mask, match->flow.dl_dst, match->wc.masks.dl_dst);
362 match_set_dl_tci(struct match *match, ovs_be16 tci)
364 match_set_dl_tci_masked(match, tci, htons(0xffff));
368 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
370 match->flow.vlan_tci = tci & mask;
371 match->wc.masks.vlan_tci = mask;
374 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
375 * wildcarded, then 'match' will match a packet regardless of whether it has an
376 * 802.1Q header or not. */
378 match_set_any_vid(struct match *match)
380 if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
381 match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
382 match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
384 match_set_dl_tci_masked(match, htons(0), htons(0));
388 /* Modifies 'match' depending on 'dl_vlan':
390 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
391 * without an 802.1Q header.
393 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
394 * VID equals the low 12 bits of 'dl_vlan'.
397 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
399 flow_set_dl_vlan(&match->flow, dl_vlan);
400 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
401 match->wc.masks.vlan_tci = OVS_BE16_MAX;
403 match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
407 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
408 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
411 match_set_vlan_vid(struct match *match, ovs_be16 vid)
413 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
417 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
418 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
419 * plus CFI), with the corresponding 'mask'. */
421 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
423 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
424 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
427 flow_set_vlan_vid(&match->flow, vid & mask);
428 match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
431 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
432 * wildcarded, then 'match' will match a packet regardless of whether it has an
433 * 802.1Q header or not. */
435 match_set_any_pcp(struct match *match)
437 if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
438 match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
439 match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
441 match_set_dl_tci_masked(match, htons(0), htons(0));
445 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
446 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
448 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
450 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
451 match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
454 /* Modifies 'match' so that the MPLS label 'idx' matches 'lse' exactly. */
456 match_set_mpls_lse(struct match *match, int idx, ovs_be32 lse)
458 match->wc.masks.mpls_lse[idx] = OVS_BE32_MAX;
459 match->flow.mpls_lse[idx] = lse;
462 /* Modifies 'match' so that the MPLS label is wildcarded. */
464 match_set_any_mpls_label(struct match *match, int idx)
466 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_LABEL_MASK);
467 flow_set_mpls_label(&match->flow, idx, htonl(0));
470 /* Modifies 'match' so that it matches only packets with an MPLS header whose
471 * label equals the low 20 bits of 'mpls_label'. */
473 match_set_mpls_label(struct match *match, int idx, ovs_be32 mpls_label)
475 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_LABEL_MASK);
476 flow_set_mpls_label(&match->flow, idx, mpls_label);
479 /* Modifies 'match' so that the MPLS TC is wildcarded. */
481 match_set_any_mpls_tc(struct match *match, int idx)
483 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TC_MASK);
484 flow_set_mpls_tc(&match->flow, idx, 0);
487 /* Modifies 'match' so that it matches only packets with an MPLS header whose
488 * Traffic Class equals the low 3 bits of 'mpls_tc'. */
490 match_set_mpls_tc(struct match *match, int idx, uint8_t mpls_tc)
492 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TC_MASK);
493 flow_set_mpls_tc(&match->flow, idx, mpls_tc);
496 /* Modifies 'match' so that the MPLS stack flag is wildcarded. */
498 match_set_any_mpls_bos(struct match *match, int idx)
500 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_BOS_MASK);
501 flow_set_mpls_bos(&match->flow, idx, 0);
504 /* Modifies 'match' so that it matches only packets with an MPLS header whose
505 * Stack Flag equals the lower bit of 'mpls_bos' */
507 match_set_mpls_bos(struct match *match, int idx, uint8_t mpls_bos)
509 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_BOS_MASK);
510 flow_set_mpls_bos(&match->flow, idx, mpls_bos);
513 /* Modifies 'match' so that the MPLS LSE is wildcarded. */
515 match_set_any_mpls_lse(struct match *match, int idx)
517 match->wc.masks.mpls_lse[idx] = htonl(0);
518 flow_set_mpls_lse(&match->flow, idx, htonl(0));
522 match_set_tp_src(struct match *match, ovs_be16 tp_src)
524 match_set_tp_src_masked(match, tp_src, OVS_BE16_MAX);
528 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
530 match->flow.tp_src = port & mask;
531 match->wc.masks.tp_src = mask;
535 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
537 match_set_tp_dst_masked(match, tp_dst, OVS_BE16_MAX);
541 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
543 match->flow.tp_dst = port & mask;
544 match->wc.masks.tp_dst = mask;
548 match_set_tcp_flags(struct match *match, ovs_be16 flags)
550 match_set_tcp_flags_masked(match, flags, OVS_BE16_MAX);
554 match_set_tcp_flags_masked(struct match *match, ovs_be16 flags, ovs_be16 mask)
556 match->flow.tcp_flags = flags & mask;
557 match->wc.masks.tcp_flags = mask;
561 match_set_nw_proto(struct match *match, uint8_t nw_proto)
563 match->flow.nw_proto = nw_proto;
564 match->wc.masks.nw_proto = UINT8_MAX;
568 match_set_nw_src(struct match *match, ovs_be32 nw_src)
570 match->flow.nw_src = nw_src;
571 match->wc.masks.nw_src = OVS_BE32_MAX;
575 match_set_nw_src_masked(struct match *match,
576 ovs_be32 nw_src, ovs_be32 mask)
578 match->flow.nw_src = nw_src & mask;
579 match->wc.masks.nw_src = mask;
583 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
585 match->flow.nw_dst = nw_dst;
586 match->wc.masks.nw_dst = OVS_BE32_MAX;
590 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
592 match->flow.nw_dst = ip & mask;
593 match->wc.masks.nw_dst = mask;
597 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
599 match->wc.masks.nw_tos |= IP_DSCP_MASK;
600 match->flow.nw_tos &= ~IP_DSCP_MASK;
601 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
605 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
607 match->wc.masks.nw_tos |= IP_ECN_MASK;
608 match->flow.nw_tos &= ~IP_ECN_MASK;
609 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
613 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
615 match->wc.masks.nw_ttl = UINT8_MAX;
616 match->flow.nw_ttl = nw_ttl;
620 match_set_nw_frag(struct match *match, uint8_t nw_frag)
622 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
623 match->flow.nw_frag = nw_frag;
627 match_set_nw_frag_masked(struct match *match,
628 uint8_t nw_frag, uint8_t mask)
630 match->flow.nw_frag = nw_frag & mask;
631 match->wc.masks.nw_frag = mask;
635 match_set_icmp_type(struct match *match, uint8_t icmp_type)
637 match_set_tp_src(match, htons(icmp_type));
641 match_set_icmp_code(struct match *match, uint8_t icmp_code)
643 match_set_tp_dst(match, htons(icmp_code));
647 match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN])
649 memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN);
650 memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN);
654 match_set_arp_sha_masked(struct match *match,
655 const uint8_t arp_sha[ETH_ADDR_LEN],
656 const uint8_t mask[ETH_ADDR_LEN])
658 set_eth_masked(arp_sha, mask,
659 match->flow.arp_sha, match->wc.masks.arp_sha);
663 match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN])
665 memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN);
666 memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN);
670 match_set_arp_tha_masked(struct match *match,
671 const uint8_t arp_tha[ETH_ADDR_LEN],
672 const uint8_t mask[ETH_ADDR_LEN])
674 set_eth_masked(arp_tha, mask,
675 match->flow.arp_tha, match->wc.masks.arp_tha);
679 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
681 match->flow.ipv6_src = *src;
682 match->wc.masks.ipv6_src = in6addr_exact;
686 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
687 const struct in6_addr *mask)
689 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
690 match->wc.masks.ipv6_src = *mask;
694 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
696 match->flow.ipv6_dst = *dst;
697 match->wc.masks.ipv6_dst = in6addr_exact;
701 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
702 const struct in6_addr *mask)
704 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
705 match->wc.masks.ipv6_dst = *mask;
709 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
711 match->wc.masks.ipv6_label = OVS_BE32_MAX;
712 match->flow.ipv6_label = ipv6_label;
717 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
720 match->flow.ipv6_label = ipv6_label & mask;
721 match->wc.masks.ipv6_label = mask;
725 match_set_nd_target(struct match *match, const struct in6_addr *target)
727 match->flow.nd_target = *target;
728 match->wc.masks.nd_target = in6addr_exact;
732 match_set_nd_target_masked(struct match *match,
733 const struct in6_addr *target,
734 const struct in6_addr *mask)
736 match->flow.nd_target = ipv6_addr_bitand(target, mask);
737 match->wc.masks.nd_target = *mask;
740 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
741 * values for fixed fields, otherwise false. */
743 match_equal(const struct match *a, const struct match *b)
745 return (flow_wildcards_equal(&a->wc, &b->wc)
746 && flow_equal(&a->flow, &b->flow));
749 /* Returns a hash value for the flow and wildcards in 'match', starting from
752 match_hash(const struct match *match, uint32_t basis)
754 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
758 match_has_default_recirc_id(const struct match *m)
760 return m->flow.recirc_id == 0 && (m->wc.masks.recirc_id == UINT32_MAX ||
761 m->wc.masks.recirc_id == 0);
765 match_has_default_dp_hash(const struct match *m)
767 return ((m->flow.dp_hash | m->wc.masks.dp_hash) == 0);
770 /* Return true if the hidden fields of the match are set to the default values.
771 * The default values equals to those set up by match_init_hidden_fields(). */
773 match_has_default_hidden_fields(const struct match *m)
775 return match_has_default_recirc_id(m) && match_has_default_dp_hash(m);
779 match_init_hidden_fields(struct match *m)
781 match_set_recirc_id(m, 0);
782 match_set_dp_hash_masked(m, 0, 0);
786 format_eth_masked(struct ds *s, const char *name,
787 const uint8_t eth[ETH_ADDR_LEN],
788 const uint8_t mask[ETH_ADDR_LEN])
790 if (!eth_addr_is_zero(mask)) {
791 ds_put_format(s, "%s=", name);
792 eth_format_masked(eth, mask, s);
798 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
802 ds_put_format(s, "%s=", name);
803 ip_format_masked(ip, netmask, s);
809 format_ipv6_netmask(struct ds *s, const char *name,
810 const struct in6_addr *addr,
811 const struct in6_addr *netmask)
813 if (!ipv6_mask_is_any(netmask)) {
814 ds_put_format(s, "%s=", name);
815 print_ipv6_masked(s, addr, netmask);
821 format_be16_masked(struct ds *s, const char *name,
822 ovs_be16 value, ovs_be16 mask)
824 if (mask != htons(0)) {
825 ds_put_format(s, "%s=", name);
826 if (mask == OVS_BE16_MAX) {
827 ds_put_format(s, "%"PRIu16, ntohs(value));
829 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
830 ntohs(value), ntohs(mask));
837 format_be32_masked(struct ds *s, const char *name,
838 ovs_be32 value, ovs_be32 mask)
840 if (mask != htonl(0)) {
841 ds_put_format(s, "%s=", name);
842 if (mask == OVS_BE32_MAX) {
843 ds_put_format(s, "%"PRIu32, ntohl(value));
845 ds_put_format(s, "0x%"PRIx32"/0x%"PRIx32,
846 ntohl(value), ntohl(mask));
853 format_uint32_masked(struct ds *s, const char *name,
854 uint32_t value, uint32_t mask)
857 ds_put_format(s, "%s=%#"PRIx32, name, value);
858 if (mask != UINT32_MAX) {
859 ds_put_format(s, "/%#"PRIx32, mask);
866 format_be64_masked(struct ds *s, const char *name,
867 ovs_be64 value, ovs_be64 mask)
869 if (mask != htonll(0)) {
870 ds_put_format(s, "%s=%#"PRIx64, name, ntohll(value));
871 if (mask != OVS_BE64_MAX) {
872 ds_put_format(s, "/%#"PRIx64, ntohll(mask));
879 format_flow_tunnel(struct ds *s, const struct match *match)
881 const struct flow_wildcards *wc = &match->wc;
882 const struct flow_tnl *tnl = &match->flow.tunnel;
884 format_be64_masked(s, "tun_id", tnl->tun_id, wc->masks.tunnel.tun_id);
885 format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
886 format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
888 if (wc->masks.tunnel.gbp_id) {
889 format_be16_masked(s, "tun_gbp_id", tnl->gbp_id,
890 wc->masks.tunnel.gbp_id);
893 if (wc->masks.tunnel.gbp_flags) {
894 ds_put_format(s, "tun_gbp_flags=%#"PRIx8",", tnl->gbp_flags);
897 if (wc->masks.tunnel.ip_tos) {
898 ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
900 if (wc->masks.tunnel.ip_ttl) {
901 ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
903 if (wc->masks.tunnel.flags) {
904 format_flags_masked(s, "tun_flags", flow_tun_flag_to_string,
906 wc->masks.tunnel.flags & FLOW_TNL_F_MASK,
910 tun_metadata_match_format(s, match);
913 /* Appends a string representation of 'match' to 's'. If 'priority' is
914 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
916 match_format(const struct match *match, struct ds *s, int priority)
918 const struct flow_wildcards *wc = &match->wc;
919 size_t start_len = s->length;
920 const struct flow *f = &match->flow;
921 bool skip_type = false;
922 bool skip_proto = false;
926 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 33);
928 if (priority != OFP_DEFAULT_PRIORITY) {
929 ds_put_format(s, "priority=%d,", priority);
932 format_uint32_masked(s, "pkt_mark", f->pkt_mark, wc->masks.pkt_mark);
934 if (wc->masks.recirc_id) {
935 format_uint32_masked(s, "recirc_id", f->recirc_id,
936 wc->masks.recirc_id);
939 if (wc->masks.dp_hash) {
940 format_uint32_masked(s, "dp_hash", f->dp_hash,
944 if (wc->masks.conj_id) {
945 ds_put_format(s, "conj_id=%"PRIu32",", f->conj_id);
948 if (wc->masks.skb_priority) {
949 ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority);
952 if (wc->masks.actset_output) {
953 ds_put_cstr(s, "actset_output=");
954 ofputil_format_port(f->actset_output, s);
958 if (wc->masks.dl_type) {
960 if (f->dl_type == htons(ETH_TYPE_IP)) {
961 if (wc->masks.nw_proto) {
963 if (f->nw_proto == IPPROTO_ICMP) {
964 ds_put_cstr(s, "icmp,");
965 } else if (f->nw_proto == IPPROTO_IGMP) {
966 ds_put_cstr(s, "igmp,");
967 } else if (f->nw_proto == IPPROTO_TCP) {
968 ds_put_cstr(s, "tcp,");
969 } else if (f->nw_proto == IPPROTO_UDP) {
970 ds_put_cstr(s, "udp,");
971 } else if (f->nw_proto == IPPROTO_SCTP) {
972 ds_put_cstr(s, "sctp,");
974 ds_put_cstr(s, "ip,");
978 ds_put_cstr(s, "ip,");
980 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
981 if (wc->masks.nw_proto) {
983 if (f->nw_proto == IPPROTO_ICMPV6) {
984 ds_put_cstr(s, "icmp6,");
985 } else if (f->nw_proto == IPPROTO_TCP) {
986 ds_put_cstr(s, "tcp6,");
987 } else if (f->nw_proto == IPPROTO_UDP) {
988 ds_put_cstr(s, "udp6,");
989 } else if (f->nw_proto == IPPROTO_SCTP) {
990 ds_put_cstr(s, "sctp6,");
992 ds_put_cstr(s, "ipv6,");
996 ds_put_cstr(s, "ipv6,");
998 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
999 ds_put_cstr(s, "arp,");
1000 } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
1001 ds_put_cstr(s, "rarp,");
1002 } else if (f->dl_type == htons(ETH_TYPE_MPLS)) {
1003 ds_put_cstr(s, "mpls,");
1004 } else if (f->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
1005 ds_put_cstr(s, "mplsm,");
1010 for (i = 0; i < FLOW_N_REGS; i++) {
1011 #define REGNAME_LEN 20
1012 char regname[REGNAME_LEN];
1013 if (snprintf(regname, REGNAME_LEN, "reg%d", i) >= REGNAME_LEN) {
1014 strcpy(regname, "reg?");
1016 format_uint32_masked(s, regname, f->regs[i], wc->masks.regs[i]);
1019 format_flow_tunnel(s, match);
1021 format_be64_masked(s, "metadata", f->metadata, wc->masks.metadata);
1023 if (wc->masks.in_port.ofp_port) {
1024 ds_put_cstr(s, "in_port=");
1025 ofputil_format_port(f->in_port.ofp_port, s);
1026 ds_put_char(s, ',');
1028 if (wc->masks.vlan_tci) {
1029 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
1030 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
1031 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
1033 if (cfi && f->vlan_tci & htons(VLAN_CFI)
1034 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
1035 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
1036 && (vid_mask || pcp_mask)) {
1038 ds_put_format(s, "dl_vlan=%"PRIu16",",
1039 vlan_tci_to_vid(f->vlan_tci));
1042 ds_put_format(s, "dl_vlan_pcp=%d,",
1043 vlan_tci_to_pcp(f->vlan_tci));
1045 } else if (wc->masks.vlan_tci == htons(0xffff)) {
1046 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
1048 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
1049 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
1052 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
1053 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
1054 if (!skip_type && wc->masks.dl_type) {
1055 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
1057 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
1058 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
1059 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
1060 if (wc->masks.ipv6_label) {
1061 if (wc->masks.ipv6_label == OVS_BE32_MAX) {
1062 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
1063 ntohl(f->ipv6_label));
1065 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
1066 ntohl(f->ipv6_label),
1067 ntohl(wc->masks.ipv6_label));
1070 } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
1071 f->dl_type == htons(ETH_TYPE_RARP)) {
1072 format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
1073 format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
1075 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
1076 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
1078 if (!skip_proto && wc->masks.nw_proto) {
1079 if (f->dl_type == htons(ETH_TYPE_ARP) ||
1080 f->dl_type == htons(ETH_TYPE_RARP)) {
1081 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
1083 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
1086 if (f->dl_type == htons(ETH_TYPE_ARP) ||
1087 f->dl_type == htons(ETH_TYPE_RARP)) {
1088 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
1089 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
1091 if (wc->masks.nw_tos & IP_DSCP_MASK) {
1092 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
1094 if (wc->masks.nw_tos & IP_ECN_MASK) {
1095 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
1097 if (wc->masks.nw_ttl) {
1098 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
1100 if (wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
1101 ds_put_format(s, "mpls_label=%"PRIu32",",
1102 mpls_lse_to_label(f->mpls_lse[0]));
1104 if (wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
1105 ds_put_format(s, "mpls_tc=%"PRIu8",",
1106 mpls_lse_to_tc(f->mpls_lse[0]));
1108 if (wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK)) {
1109 ds_put_format(s, "mpls_ttl=%"PRIu8",",
1110 mpls_lse_to_ttl(f->mpls_lse[0]));
1112 if (wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
1113 ds_put_format(s, "mpls_bos=%"PRIu8",",
1114 mpls_lse_to_bos(f->mpls_lse[0]));
1116 format_be32_masked(s, "mpls_lse1", f->mpls_lse[1], wc->masks.mpls_lse[1]);
1117 format_be32_masked(s, "mpls_lse2", f->mpls_lse[2], wc->masks.mpls_lse[2]);
1119 switch (wc->masks.nw_frag) {
1120 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
1121 ds_put_format(s, "nw_frag=%s,",
1122 f->nw_frag & FLOW_NW_FRAG_ANY
1123 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
1124 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
1127 case FLOW_NW_FRAG_ANY:
1128 ds_put_format(s, "nw_frag=%s,",
1129 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
1132 case FLOW_NW_FRAG_LATER:
1133 ds_put_format(s, "nw_frag=%s,",
1134 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
1137 if (f->dl_type == htons(ETH_TYPE_IP) &&
1138 f->nw_proto == IPPROTO_ICMP) {
1139 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1140 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1141 } else if (f->dl_type == htons(ETH_TYPE_IP) &&
1142 f->nw_proto == IPPROTO_IGMP) {
1143 format_be16_masked(s, "igmp_type", f->tp_src, wc->masks.tp_src);
1144 format_be16_masked(s, "igmp_code", f->tp_dst, wc->masks.tp_dst);
1145 } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
1146 f->nw_proto == IPPROTO_ICMPV6) {
1147 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1148 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1149 format_ipv6_netmask(s, "nd_target", &f->nd_target,
1150 &wc->masks.nd_target);
1151 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
1152 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
1154 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
1155 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
1157 if (is_ip_any(f) && f->nw_proto == IPPROTO_TCP && wc->masks.tcp_flags) {
1158 format_flags_masked(s, "tcp_flags", packet_tcp_flag_to_string,
1159 ntohs(f->tcp_flags), TCP_FLAGS(wc->masks.tcp_flags),
1160 TCP_FLAGS(OVS_BE16_MAX));
1163 if (s->length > start_len) {
1168 /* Converts 'match' to a string and returns the string. If 'priority' is
1169 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1170 * must free the string (with free()). */
1172 match_to_string(const struct match *match, int priority)
1174 struct ds s = DS_EMPTY_INITIALIZER;
1175 match_format(match, &s, priority);
1176 return ds_steal_cstr(&s);
1180 match_print(const struct match *match)
1182 char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
1187 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1188 * with minimatch_destroy(). */
1190 minimatch_init(struct minimatch *dst, const struct match *src)
1192 struct miniflow tmp;
1194 miniflow_map_init(&tmp, &src->wc.masks);
1195 /* Allocate two consecutive miniflows. */
1196 miniflow_alloc(dst->flows, 2, &tmp);
1197 miniflow_init(dst->flow, &src->flow);
1198 minimask_init(dst->mask, &src->wc);
1201 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1202 * with minimatch_destroy(). */
1204 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1206 /* Allocate two consecutive miniflows. */
1207 size_t data_size = miniflow_alloc(dst->flows, 2, &src->mask->masks);
1209 memcpy(dst->flow->values, src->flow->values, data_size);
1210 memcpy(dst->mask->masks.values, src->mask->masks.values, data_size);
1213 /* Initializes 'dst' with the data in 'src', destroying 'src'. The caller must
1214 * eventually free 'dst' with minimatch_destroy(). */
1216 minimatch_move(struct minimatch *dst, struct minimatch *src)
1218 dst->flow = src->flow;
1219 dst->mask = src->mask;
1222 /* Frees any memory owned by 'match'. Does not free the storage in which
1223 * 'match' itself resides; the caller is responsible for that. */
1225 minimatch_destroy(struct minimatch *match)
1230 /* Initializes 'dst' as a copy of 'src'. */
1232 minimatch_expand(const struct minimatch *src, struct match *dst)
1234 miniflow_expand(src->flow, &dst->flow);
1235 minimask_expand(src->mask, &dst->wc);
1236 memset(&dst->tun_md, 0, sizeof dst->tun_md);
1239 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
1241 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1243 return minimask_equal(a->mask, b->mask)
1244 && miniflow_equal(a->flow, b->flow);
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'.
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. */
1254 minimatch_matches_flow(const struct minimatch *match,
1255 const struct flow *target)
1257 const uint64_t *target_u64 = (const uint64_t *) target;
1258 const uint64_t *flowp = match->flow->values;
1259 const uint64_t *maskp = match->mask->masks.values;
1262 MAP_FOR_EACH_INDEX(idx, match->flow->map) {
1263 if ((*flowp++ ^ target_u64[idx]) & *maskp++) {
1271 /* Appends a string representation of 'match' to 's'. If 'priority' is
1272 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1274 minimatch_format(const struct minimatch *match, struct ds *s, int priority)
1276 struct match megamatch;
1278 minimatch_expand(match, &megamatch);
1279 match_format(&megamatch, s, priority);
1282 /* Converts 'match' to a string and returns the string. If 'priority' is
1283 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1284 * must free the string (with free()). */
1286 minimatch_to_string(const struct minimatch *match, int priority)
1288 struct match megamatch;
1290 minimatch_expand(match, &megamatch);
1291 return match_to_string(&megamatch, priority);