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_dl_type(struct match *match, ovs_be16 dl_type)
310 match->wc.masks.dl_type = OVS_BE16_MAX;
311 match->flow.dl_type = dl_type;
314 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
315 * exactly. 'mask_dst' is set to all 1s. */
317 set_eth(const struct eth_addr value_src,
318 struct eth_addr *value_dst,
319 struct eth_addr *mask_dst)
321 *value_dst = value_src;
322 *mask_dst = eth_addr_exact;
325 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
326 * after each byte is ANDed with the appropriate byte in 'mask_src'.
327 * 'mask_dst' is set to 'mask_src' */
329 set_eth_masked(const struct eth_addr value_src,
330 const struct eth_addr mask_src,
331 struct eth_addr *value_dst, struct eth_addr *mask_dst)
335 for (i = 0; i < ARRAY_SIZE(value_dst->be16); i++) {
336 value_dst->be16[i] = value_src.be16[i] & mask_src.be16[i];
338 *mask_dst = mask_src;
341 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
344 match_set_dl_src(struct match *match, const struct eth_addr dl_src)
346 set_eth(dl_src, &match->flow.dl_src, &match->wc.masks.dl_src);
349 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
350 * after each byte is ANDed with the appropriate byte in 'mask'. */
352 match_set_dl_src_masked(struct match *match,
353 const struct eth_addr dl_src,
354 const struct eth_addr mask)
356 set_eth_masked(dl_src, mask, &match->flow.dl_src, &match->wc.masks.dl_src);
359 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
362 match_set_dl_dst(struct match *match, const struct eth_addr dl_dst)
364 set_eth(dl_dst, &match->flow.dl_dst, &match->wc.masks.dl_dst);
367 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
368 * byte is ANDed with the appropriate byte in 'mask'.
370 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
371 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
373 match_set_dl_dst_masked(struct match *match,
374 const struct eth_addr dl_dst,
375 const struct eth_addr mask)
377 set_eth_masked(dl_dst, mask, &match->flow.dl_dst, &match->wc.masks.dl_dst);
381 match_set_dl_tci(struct match *match, ovs_be16 tci)
383 match_set_dl_tci_masked(match, tci, htons(0xffff));
387 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
389 match->flow.vlan_tci = tci & mask;
390 match->wc.masks.vlan_tci = mask;
393 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
394 * wildcarded, then 'match' will match a packet regardless of whether it has an
395 * 802.1Q header or not. */
397 match_set_any_vid(struct match *match)
399 if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
400 match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
401 match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
403 match_set_dl_tci_masked(match, htons(0), htons(0));
407 /* Modifies 'match' depending on 'dl_vlan':
409 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
410 * without an 802.1Q header.
412 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
413 * VID equals the low 12 bits of 'dl_vlan'.
416 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
418 flow_set_dl_vlan(&match->flow, dl_vlan);
419 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
420 match->wc.masks.vlan_tci = OVS_BE16_MAX;
422 match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
426 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
427 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
430 match_set_vlan_vid(struct match *match, ovs_be16 vid)
432 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
436 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
437 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
438 * plus CFI), with the corresponding 'mask'. */
440 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
442 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
443 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
446 flow_set_vlan_vid(&match->flow, vid & mask);
447 match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
450 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
451 * wildcarded, then 'match' will match a packet regardless of whether it has an
452 * 802.1Q header or not. */
454 match_set_any_pcp(struct match *match)
456 if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
457 match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
458 match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
460 match_set_dl_tci_masked(match, htons(0), htons(0));
464 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
465 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
467 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
469 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
470 match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
473 /* Modifies 'match' so that the MPLS label 'idx' matches 'lse' exactly. */
475 match_set_mpls_lse(struct match *match, int idx, ovs_be32 lse)
477 match->wc.masks.mpls_lse[idx] = OVS_BE32_MAX;
478 match->flow.mpls_lse[idx] = lse;
481 /* Modifies 'match' so that the MPLS label is wildcarded. */
483 match_set_any_mpls_label(struct match *match, int idx)
485 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_LABEL_MASK);
486 flow_set_mpls_label(&match->flow, idx, htonl(0));
489 /* Modifies 'match' so that it matches only packets with an MPLS header whose
490 * label equals the low 20 bits of 'mpls_label'. */
492 match_set_mpls_label(struct match *match, int idx, ovs_be32 mpls_label)
494 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_LABEL_MASK);
495 flow_set_mpls_label(&match->flow, idx, mpls_label);
498 /* Modifies 'match' so that the MPLS TC is wildcarded. */
500 match_set_any_mpls_tc(struct match *match, int idx)
502 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TC_MASK);
503 flow_set_mpls_tc(&match->flow, idx, 0);
506 /* Modifies 'match' so that it matches only packets with an MPLS header whose
507 * Traffic Class equals the low 3 bits of 'mpls_tc'. */
509 match_set_mpls_tc(struct match *match, int idx, uint8_t mpls_tc)
511 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TC_MASK);
512 flow_set_mpls_tc(&match->flow, idx, mpls_tc);
515 /* Modifies 'match' so that the MPLS stack flag is wildcarded. */
517 match_set_any_mpls_bos(struct match *match, int idx)
519 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_BOS_MASK);
520 flow_set_mpls_bos(&match->flow, idx, 0);
523 /* Modifies 'match' so that it matches only packets with an MPLS header whose
524 * Stack Flag equals the lower bit of 'mpls_bos' */
526 match_set_mpls_bos(struct match *match, int idx, uint8_t mpls_bos)
528 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_BOS_MASK);
529 flow_set_mpls_bos(&match->flow, idx, mpls_bos);
532 /* Modifies 'match' so that the MPLS LSE is wildcarded. */
534 match_set_any_mpls_lse(struct match *match, int idx)
536 match->wc.masks.mpls_lse[idx] = htonl(0);
537 flow_set_mpls_lse(&match->flow, idx, htonl(0));
541 match_set_tp_src(struct match *match, ovs_be16 tp_src)
543 match_set_tp_src_masked(match, tp_src, OVS_BE16_MAX);
547 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
549 match->flow.tp_src = port & mask;
550 match->wc.masks.tp_src = mask;
554 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
556 match_set_tp_dst_masked(match, tp_dst, OVS_BE16_MAX);
560 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
562 match->flow.tp_dst = port & mask;
563 match->wc.masks.tp_dst = mask;
567 match_set_tcp_flags(struct match *match, ovs_be16 flags)
569 match_set_tcp_flags_masked(match, flags, OVS_BE16_MAX);
573 match_set_tcp_flags_masked(struct match *match, ovs_be16 flags, ovs_be16 mask)
575 match->flow.tcp_flags = flags & mask;
576 match->wc.masks.tcp_flags = mask;
580 match_set_nw_proto(struct match *match, uint8_t nw_proto)
582 match->flow.nw_proto = nw_proto;
583 match->wc.masks.nw_proto = UINT8_MAX;
587 match_set_nw_src(struct match *match, ovs_be32 nw_src)
589 match->flow.nw_src = nw_src;
590 match->wc.masks.nw_src = OVS_BE32_MAX;
594 match_set_nw_src_masked(struct match *match,
595 ovs_be32 nw_src, ovs_be32 mask)
597 match->flow.nw_src = nw_src & mask;
598 match->wc.masks.nw_src = mask;
602 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
604 match->flow.nw_dst = nw_dst;
605 match->wc.masks.nw_dst = OVS_BE32_MAX;
609 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
611 match->flow.nw_dst = ip & mask;
612 match->wc.masks.nw_dst = mask;
616 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
618 match->wc.masks.nw_tos |= IP_DSCP_MASK;
619 match->flow.nw_tos &= ~IP_DSCP_MASK;
620 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
624 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
626 match->wc.masks.nw_tos |= IP_ECN_MASK;
627 match->flow.nw_tos &= ~IP_ECN_MASK;
628 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
632 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
634 match->wc.masks.nw_ttl = UINT8_MAX;
635 match->flow.nw_ttl = nw_ttl;
639 match_set_nw_frag(struct match *match, uint8_t nw_frag)
641 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
642 match->flow.nw_frag = nw_frag;
646 match_set_nw_frag_masked(struct match *match,
647 uint8_t nw_frag, uint8_t mask)
649 match->flow.nw_frag = nw_frag & mask;
650 match->wc.masks.nw_frag = mask;
654 match_set_icmp_type(struct match *match, uint8_t icmp_type)
656 match_set_tp_src(match, htons(icmp_type));
660 match_set_icmp_code(struct match *match, uint8_t icmp_code)
662 match_set_tp_dst(match, htons(icmp_code));
666 match_set_arp_sha(struct match *match, const struct eth_addr sha)
668 match->flow.arp_sha = sha;
669 match->wc.masks.arp_sha = eth_addr_exact;
673 match_set_arp_sha_masked(struct match *match,
674 const struct eth_addr arp_sha,
675 const struct eth_addr mask)
677 set_eth_masked(arp_sha, mask,
678 &match->flow.arp_sha, &match->wc.masks.arp_sha);
682 match_set_arp_tha(struct match *match, const struct eth_addr tha)
684 match->flow.arp_tha = tha;
685 match->wc.masks.arp_tha = eth_addr_exact;
689 match_set_arp_tha_masked(struct match *match,
690 const struct eth_addr arp_tha,
691 const struct eth_addr mask)
693 set_eth_masked(arp_tha, mask,
694 &match->flow.arp_tha, &match->wc.masks.arp_tha);
698 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
700 match->flow.ipv6_src = *src;
701 match->wc.masks.ipv6_src = in6addr_exact;
705 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
706 const struct in6_addr *mask)
708 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
709 match->wc.masks.ipv6_src = *mask;
713 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
715 match->flow.ipv6_dst = *dst;
716 match->wc.masks.ipv6_dst = in6addr_exact;
720 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
721 const struct in6_addr *mask)
723 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
724 match->wc.masks.ipv6_dst = *mask;
728 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
730 match->wc.masks.ipv6_label = OVS_BE32_MAX;
731 match->flow.ipv6_label = ipv6_label;
736 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
739 match->flow.ipv6_label = ipv6_label & mask;
740 match->wc.masks.ipv6_label = mask;
744 match_set_nd_target(struct match *match, const struct in6_addr *target)
746 match->flow.nd_target = *target;
747 match->wc.masks.nd_target = in6addr_exact;
751 match_set_nd_target_masked(struct match *match,
752 const struct in6_addr *target,
753 const struct in6_addr *mask)
755 match->flow.nd_target = ipv6_addr_bitand(target, mask);
756 match->wc.masks.nd_target = *mask;
759 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
760 * values for fixed fields, otherwise false. */
762 match_equal(const struct match *a, const struct match *b)
764 return (flow_wildcards_equal(&a->wc, &b->wc)
765 && flow_equal(&a->flow, &b->flow));
768 /* Returns a hash value for the flow and wildcards in 'match', starting from
771 match_hash(const struct match *match, uint32_t basis)
773 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
777 match_has_default_recirc_id(const struct match *m)
779 return m->flow.recirc_id == 0 && (m->wc.masks.recirc_id == UINT32_MAX ||
780 m->wc.masks.recirc_id == 0);
784 match_has_default_dp_hash(const struct match *m)
786 return ((m->flow.dp_hash | m->wc.masks.dp_hash) == 0);
789 /* Return true if the hidden fields of the match are set to the default values.
790 * The default values equals to those set up by match_init_hidden_fields(). */
792 match_has_default_hidden_fields(const struct match *m)
794 return match_has_default_recirc_id(m) && match_has_default_dp_hash(m);
798 match_init_hidden_fields(struct match *m)
800 match_set_recirc_id(m, 0);
801 match_set_dp_hash_masked(m, 0, 0);
805 format_eth_masked(struct ds *s, const char *name,
806 const struct eth_addr eth, const struct eth_addr mask)
808 if (!eth_addr_is_zero(mask)) {
809 ds_put_format(s, "%s=", name);
810 eth_format_masked(eth, &mask, s);
816 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
820 ds_put_format(s, "%s=", name);
821 ip_format_masked(ip, netmask, s);
827 format_ipv6_netmask(struct ds *s, const char *name,
828 const struct in6_addr *addr,
829 const struct in6_addr *netmask)
831 if (!ipv6_mask_is_any(netmask)) {
832 ds_put_format(s, "%s=", name);
833 print_ipv6_masked(s, addr, netmask);
839 format_uint16_masked(struct ds *s, const char *name,
840 uint16_t value, uint16_t mask)
843 ds_put_format(s, "%s=", name);
844 if (mask == UINT16_MAX) {
845 ds_put_format(s, "%"PRIu16, value);
847 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16, value, mask);
854 format_be16_masked(struct ds *s, const char *name,
855 ovs_be16 value, ovs_be16 mask)
857 if (mask != htons(0)) {
858 ds_put_format(s, "%s=", name);
859 if (mask == OVS_BE16_MAX) {
860 ds_put_format(s, "%"PRIu16, ntohs(value));
862 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
863 ntohs(value), ntohs(mask));
870 format_be32_masked(struct ds *s, const char *name,
871 ovs_be32 value, ovs_be32 mask)
873 if (mask != htonl(0)) {
874 ds_put_format(s, "%s=", name);
875 if (mask == OVS_BE32_MAX) {
876 ds_put_format(s, "%"PRIu32, ntohl(value));
878 ds_put_format(s, "0x%"PRIx32"/0x%"PRIx32,
879 ntohl(value), ntohl(mask));
886 format_uint32_masked(struct ds *s, const char *name,
887 uint32_t value, uint32_t mask)
890 ds_put_format(s, "%s=%#"PRIx32, name, value);
891 if (mask != UINT32_MAX) {
892 ds_put_format(s, "/%#"PRIx32, mask);
899 format_be64_masked(struct ds *s, const char *name,
900 ovs_be64 value, ovs_be64 mask)
902 if (mask != htonll(0)) {
903 ds_put_format(s, "%s=%#"PRIx64, name, ntohll(value));
904 if (mask != OVS_BE64_MAX) {
905 ds_put_format(s, "/%#"PRIx64, ntohll(mask));
912 format_flow_tunnel(struct ds *s, const struct match *match)
914 const struct flow_wildcards *wc = &match->wc;
915 const struct flow_tnl *tnl = &match->flow.tunnel;
917 format_be64_masked(s, "tun_id", tnl->tun_id, wc->masks.tunnel.tun_id);
918 format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
919 format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
921 if (wc->masks.tunnel.gbp_id) {
922 format_be16_masked(s, "tun_gbp_id", tnl->gbp_id,
923 wc->masks.tunnel.gbp_id);
926 if (wc->masks.tunnel.gbp_flags) {
927 ds_put_format(s, "tun_gbp_flags=%#"PRIx8",", tnl->gbp_flags);
930 if (wc->masks.tunnel.ip_tos) {
931 ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
933 if (wc->masks.tunnel.ip_ttl) {
934 ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
936 if (wc->masks.tunnel.flags) {
937 format_flags_masked(s, "tun_flags", flow_tun_flag_to_string,
939 wc->masks.tunnel.flags & FLOW_TNL_F_MASK,
943 tun_metadata_match_format(s, match);
946 /* Appends a string representation of 'match' to 's'. If 'priority' is
947 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
949 match_format(const struct match *match, struct ds *s, int priority)
951 const struct flow_wildcards *wc = &match->wc;
952 size_t start_len = s->length;
953 const struct flow *f = &match->flow;
954 bool skip_type = false;
955 bool skip_proto = false;
959 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 34);
961 if (priority != OFP_DEFAULT_PRIORITY) {
962 ds_put_format(s, "priority=%d,", priority);
965 format_uint32_masked(s, "pkt_mark", f->pkt_mark, wc->masks.pkt_mark);
967 if (wc->masks.recirc_id) {
968 format_uint32_masked(s, "recirc_id", f->recirc_id,
969 wc->masks.recirc_id);
972 if (wc->masks.dp_hash) {
973 format_uint32_masked(s, "dp_hash", f->dp_hash,
977 if (wc->masks.conj_id) {
978 ds_put_format(s, "conj_id=%"PRIu32",", f->conj_id);
981 if (wc->masks.skb_priority) {
982 ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority);
985 if (wc->masks.actset_output) {
986 ds_put_cstr(s, "actset_output=");
987 ofputil_format_port(f->actset_output, s);
991 if (wc->masks.ct_state) {
992 if (wc->masks.ct_state == UINT16_MAX) {
993 ds_put_cstr(s, "ct_state=");
995 format_flags(s, ct_state_to_string, f->ct_state, '|');
997 ds_put_cstr(s, "0"); /* No state. */
1000 format_flags_masked(s, "ct_state", ct_state_to_string,
1001 f->ct_state, wc->masks.ct_state, UINT16_MAX);
1003 ds_put_char(s, ',');
1006 if (wc->masks.ct_zone) {
1007 format_uint16_masked(s, "ct_zone", f->ct_zone, wc->masks.ct_zone);
1010 if (wc->masks.dl_type) {
1012 if (f->dl_type == htons(ETH_TYPE_IP)) {
1013 if (wc->masks.nw_proto) {
1015 if (f->nw_proto == IPPROTO_ICMP) {
1016 ds_put_cstr(s, "icmp,");
1017 } else if (f->nw_proto == IPPROTO_IGMP) {
1018 ds_put_cstr(s, "igmp,");
1019 } else if (f->nw_proto == IPPROTO_TCP) {
1020 ds_put_cstr(s, "tcp,");
1021 } else if (f->nw_proto == IPPROTO_UDP) {
1022 ds_put_cstr(s, "udp,");
1023 } else if (f->nw_proto == IPPROTO_SCTP) {
1024 ds_put_cstr(s, "sctp,");
1026 ds_put_cstr(s, "ip,");
1030 ds_put_cstr(s, "ip,");
1032 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
1033 if (wc->masks.nw_proto) {
1035 if (f->nw_proto == IPPROTO_ICMPV6) {
1036 ds_put_cstr(s, "icmp6,");
1037 } else if (f->nw_proto == IPPROTO_TCP) {
1038 ds_put_cstr(s, "tcp6,");
1039 } else if (f->nw_proto == IPPROTO_UDP) {
1040 ds_put_cstr(s, "udp6,");
1041 } else if (f->nw_proto == IPPROTO_SCTP) {
1042 ds_put_cstr(s, "sctp6,");
1044 ds_put_cstr(s, "ipv6,");
1048 ds_put_cstr(s, "ipv6,");
1050 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
1051 ds_put_cstr(s, "arp,");
1052 } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
1053 ds_put_cstr(s, "rarp,");
1054 } else if (f->dl_type == htons(ETH_TYPE_MPLS)) {
1055 ds_put_cstr(s, "mpls,");
1056 } else if (f->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
1057 ds_put_cstr(s, "mplsm,");
1062 for (i = 0; i < FLOW_N_REGS; i++) {
1063 #define REGNAME_LEN 20
1064 char regname[REGNAME_LEN];
1065 if (snprintf(regname, REGNAME_LEN, "reg%d", i) >= REGNAME_LEN) {
1066 strcpy(regname, "reg?");
1068 format_uint32_masked(s, regname, f->regs[i], wc->masks.regs[i]);
1071 format_flow_tunnel(s, match);
1073 format_be64_masked(s, "metadata", f->metadata, wc->masks.metadata);
1075 if (wc->masks.in_port.ofp_port) {
1076 ds_put_cstr(s, "in_port=");
1077 ofputil_format_port(f->in_port.ofp_port, s);
1078 ds_put_char(s, ',');
1080 if (wc->masks.vlan_tci) {
1081 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
1082 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
1083 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
1085 if (cfi && f->vlan_tci & htons(VLAN_CFI)
1086 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
1087 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
1088 && (vid_mask || pcp_mask)) {
1090 ds_put_format(s, "dl_vlan=%"PRIu16",",
1091 vlan_tci_to_vid(f->vlan_tci));
1094 ds_put_format(s, "dl_vlan_pcp=%d,",
1095 vlan_tci_to_pcp(f->vlan_tci));
1097 } else if (wc->masks.vlan_tci == htons(0xffff)) {
1098 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
1100 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
1101 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
1104 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
1105 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
1106 if (!skip_type && wc->masks.dl_type) {
1107 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
1109 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
1110 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
1111 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
1112 if (wc->masks.ipv6_label) {
1113 if (wc->masks.ipv6_label == OVS_BE32_MAX) {
1114 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
1115 ntohl(f->ipv6_label));
1117 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
1118 ntohl(f->ipv6_label),
1119 ntohl(wc->masks.ipv6_label));
1122 } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
1123 f->dl_type == htons(ETH_TYPE_RARP)) {
1124 format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
1125 format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
1127 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
1128 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
1130 if (!skip_proto && wc->masks.nw_proto) {
1131 if (f->dl_type == htons(ETH_TYPE_ARP) ||
1132 f->dl_type == htons(ETH_TYPE_RARP)) {
1133 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
1135 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
1138 if (f->dl_type == htons(ETH_TYPE_ARP) ||
1139 f->dl_type == htons(ETH_TYPE_RARP)) {
1140 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
1141 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
1143 if (wc->masks.nw_tos & IP_DSCP_MASK) {
1144 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
1146 if (wc->masks.nw_tos & IP_ECN_MASK) {
1147 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
1149 if (wc->masks.nw_ttl) {
1150 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
1152 if (wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
1153 ds_put_format(s, "mpls_label=%"PRIu32",",
1154 mpls_lse_to_label(f->mpls_lse[0]));
1156 if (wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
1157 ds_put_format(s, "mpls_tc=%"PRIu8",",
1158 mpls_lse_to_tc(f->mpls_lse[0]));
1160 if (wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK)) {
1161 ds_put_format(s, "mpls_ttl=%"PRIu8",",
1162 mpls_lse_to_ttl(f->mpls_lse[0]));
1164 if (wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
1165 ds_put_format(s, "mpls_bos=%"PRIu8",",
1166 mpls_lse_to_bos(f->mpls_lse[0]));
1168 format_be32_masked(s, "mpls_lse1", f->mpls_lse[1], wc->masks.mpls_lse[1]);
1169 format_be32_masked(s, "mpls_lse2", f->mpls_lse[2], wc->masks.mpls_lse[2]);
1171 switch (wc->masks.nw_frag) {
1172 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
1173 ds_put_format(s, "nw_frag=%s,",
1174 f->nw_frag & FLOW_NW_FRAG_ANY
1175 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
1176 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
1179 case FLOW_NW_FRAG_ANY:
1180 ds_put_format(s, "nw_frag=%s,",
1181 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
1184 case FLOW_NW_FRAG_LATER:
1185 ds_put_format(s, "nw_frag=%s,",
1186 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
1189 if (f->dl_type == htons(ETH_TYPE_IP) &&
1190 f->nw_proto == IPPROTO_ICMP) {
1191 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1192 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1193 } else if (f->dl_type == htons(ETH_TYPE_IP) &&
1194 f->nw_proto == IPPROTO_IGMP) {
1195 format_be16_masked(s, "igmp_type", f->tp_src, wc->masks.tp_src);
1196 format_be16_masked(s, "igmp_code", f->tp_dst, wc->masks.tp_dst);
1197 } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
1198 f->nw_proto == IPPROTO_ICMPV6) {
1199 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1200 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1201 format_ipv6_netmask(s, "nd_target", &f->nd_target,
1202 &wc->masks.nd_target);
1203 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
1204 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
1206 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
1207 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
1209 if (is_ip_any(f) && f->nw_proto == IPPROTO_TCP && wc->masks.tcp_flags) {
1210 format_flags_masked(s, "tcp_flags", packet_tcp_flag_to_string,
1211 ntohs(f->tcp_flags), TCP_FLAGS(wc->masks.tcp_flags),
1212 TCP_FLAGS(OVS_BE16_MAX));
1215 if (s->length > start_len) {
1220 /* Converts 'match' to a string and returns the string. If 'priority' is
1221 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1222 * must free the string (with free()). */
1224 match_to_string(const struct match *match, int priority)
1226 struct ds s = DS_EMPTY_INITIALIZER;
1227 match_format(match, &s, priority);
1228 return ds_steal_cstr(&s);
1232 match_print(const struct match *match)
1234 char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
1239 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1240 * with minimatch_destroy(). */
1242 minimatch_init(struct minimatch *dst, const struct match *src)
1244 struct miniflow tmp;
1246 miniflow_map_init(&tmp, &src->wc.masks);
1247 /* Allocate two consecutive miniflows. */
1248 miniflow_alloc(dst->flows, 2, &tmp);
1249 miniflow_init(dst->flow, &src->flow);
1250 minimask_init(dst->mask, &src->wc);
1253 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1254 * with minimatch_destroy(). */
1256 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1258 /* Allocate two consecutive miniflows. */
1259 size_t data_size = miniflow_alloc(dst->flows, 2, &src->mask->masks);
1261 memcpy(miniflow_values(dst->flow),
1262 miniflow_get_values(src->flow), data_size);
1263 memcpy(miniflow_values(&dst->mask->masks),
1264 miniflow_get_values(&src->mask->masks), data_size);
1267 /* Initializes 'dst' with the data in 'src', destroying 'src'. The caller must
1268 * eventually free 'dst' with minimatch_destroy(). */
1270 minimatch_move(struct minimatch *dst, struct minimatch *src)
1272 dst->flow = src->flow;
1273 dst->mask = src->mask;
1276 /* Frees any memory owned by 'match'. Does not free the storage in which
1277 * 'match' itself resides; the caller is responsible for that. */
1279 minimatch_destroy(struct minimatch *match)
1284 /* Initializes 'dst' as a copy of 'src'. */
1286 minimatch_expand(const struct minimatch *src, struct match *dst)
1288 miniflow_expand(src->flow, &dst->flow);
1289 minimask_expand(src->mask, &dst->wc);
1290 memset(&dst->tun_md, 0, sizeof dst->tun_md);
1293 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
1295 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1297 return minimask_equal(a->mask, b->mask)
1298 && miniflow_equal(a->flow, b->flow);
1301 /* Returns true if 'target' satisifies 'match', that is, if each bit for which
1302 * 'match' specifies a particular value has the correct value in 'target'.
1304 * This function is equivalent to miniflow_equal_flow_in_minimask(&match->flow,
1305 * target, &match->mask) but it is faster because of the invariant that
1306 * match->flow.map and match->mask.map are the same. */
1308 minimatch_matches_flow(const struct minimatch *match,
1309 const struct flow *target)
1311 const uint64_t *flowp = miniflow_get_values(match->flow);
1312 const uint64_t *maskp = miniflow_get_values(&match->mask->masks);
1315 FLOWMAP_FOR_EACH_INDEX(idx, match->flow->map) {
1316 if ((*flowp++ ^ flow_u64_value(target, idx)) & *maskp++) {
1324 /* Appends a string representation of 'match' to 's'. If 'priority' is
1325 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1327 minimatch_format(const struct minimatch *match, struct ds *s, int priority)
1329 struct match megamatch;
1331 minimatch_expand(match, &megamatch);
1332 match_format(&megamatch, s, priority);
1335 /* Converts 'match' to a string and returns the string. If 'priority' is
1336 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1337 * must free the string (with free()). */
1339 minimatch_to_string(const struct minimatch *match, int priority)
1341 struct match megamatch;
1343 minimatch_expand(match, &megamatch);
1344 return match_to_string(&megamatch, priority);