2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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"
25 /* Converts the flow in 'flow' into a match in 'match', with the given
28 match_init(struct match *match,
29 const struct flow *flow, const struct flow_wildcards *wc)
33 match_zero_wildcarded_fields(match);
36 /* Converts a flow into a match. It sets the wildcard masks based on
37 * the packet contents. It will not set the mask for fields that do not
38 * make sense for the packet type. */
40 match_wc_init(struct match *match, const struct flow *flow)
42 struct flow_wildcards *wc;
47 memset(&wc->masks, 0x0, sizeof wc->masks);
49 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
52 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
55 if (flow->skb_priority) {
56 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
60 memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark);
63 for (i = 0; i < FLOW_N_REGS; i++) {
65 memset(&wc->masks.regs[i], 0xff, sizeof wc->masks.regs[i]);
69 if (flow->tunnel.ip_dst) {
70 if (flow->tunnel.flags & FLOW_TNL_F_KEY) {
71 memset(&wc->masks.tunnel.tun_id, 0xff, sizeof wc->masks.tunnel.tun_id);
73 memset(&wc->masks.tunnel.ip_src, 0xff, sizeof wc->masks.tunnel.ip_src);
74 memset(&wc->masks.tunnel.ip_dst, 0xff, sizeof wc->masks.tunnel.ip_dst);
75 memset(&wc->masks.tunnel.flags, 0xff, sizeof wc->masks.tunnel.flags);
76 memset(&wc->masks.tunnel.ip_tos, 0xff, sizeof wc->masks.tunnel.ip_tos);
77 memset(&wc->masks.tunnel.ip_ttl, 0xff, sizeof wc->masks.tunnel.ip_ttl);
78 } else if (flow->tunnel.tun_id) {
79 memset(&wc->masks.tunnel.tun_id, 0xff, sizeof wc->masks.tunnel.tun_id);
82 memset(&wc->masks.metadata, 0xff, sizeof wc->masks.metadata);
83 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
84 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
85 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
86 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
88 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
89 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
90 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
91 memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
92 } else if (flow->dl_type == htons(ETH_TYPE_IP) ||
93 (flow->dl_type == htons(ETH_TYPE_ARP)) ||
94 (flow->dl_type == htons(ETH_TYPE_RARP))) {
95 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
96 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
97 } else if (eth_type_mpls(flow->dl_type)) {
98 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
101 if (flow->dl_type == htons(ETH_TYPE_ARP) ||
102 flow->dl_type == htons(ETH_TYPE_RARP)) {
103 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
104 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
107 if (is_ip_any(flow)) {
108 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
109 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
112 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
113 if (flow->nw_frag & FLOW_NW_FRAG_LATER) {
114 /* No transport layer header in later fragments. */
119 if (flow->nw_proto == IPPROTO_ICMP ||
120 flow->nw_proto == IPPROTO_ICMPV6 ||
121 (flow->tp_src || flow->tp_dst)) {
122 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
123 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
126 if (flow->nw_proto == IPPROTO_ICMPV6) {
127 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
128 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
135 /* Initializes 'match' as a "catch-all" match that matches every packet. */
137 match_init_catchall(struct match *match)
139 memset(&match->flow, 0, sizeof match->flow);
140 flow_wildcards_init_catchall(&match->wc);
143 /* For each bit or field wildcarded in 'match', sets the corresponding bit or
144 * field in 'flow' to all-0-bits. It is important to maintain this invariant
145 * in a match that might be inserted into a classifier.
147 * It is never necessary to call this function directly for a match that is
148 * initialized or modified only by match_*() functions. It is useful to
149 * restore the invariant in a match whose 'wc' member is modified by hand.
152 match_zero_wildcarded_fields(struct match *match)
154 flow_zero_wildcards(&match->flow, &match->wc);
158 match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
160 match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
164 match_set_reg_masked(struct match *match, unsigned int reg_idx,
165 uint32_t value, uint32_t mask)
167 ovs_assert(reg_idx < FLOW_N_REGS);
168 flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
169 match->flow.regs[reg_idx] = value & mask;
173 match_set_metadata(struct match *match, ovs_be64 metadata)
175 match_set_metadata_masked(match, metadata, OVS_BE64_MAX);
179 match_set_metadata_masked(struct match *match,
180 ovs_be64 metadata, ovs_be64 mask)
182 match->wc.masks.metadata = mask;
183 match->flow.metadata = metadata & mask;
187 match_set_tun_id(struct match *match, ovs_be64 tun_id)
189 match_set_tun_id_masked(match, tun_id, OVS_BE64_MAX);
193 match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
195 match->wc.masks.tunnel.tun_id = mask;
196 match->flow.tunnel.tun_id = tun_id & mask;
200 match_set_tun_src(struct match *match, ovs_be32 src)
202 match_set_tun_src_masked(match, src, OVS_BE32_MAX);
206 match_set_tun_src_masked(struct match *match, ovs_be32 src, ovs_be32 mask)
208 match->wc.masks.tunnel.ip_src = mask;
209 match->flow.tunnel.ip_src = src & mask;
213 match_set_tun_dst(struct match *match, ovs_be32 dst)
215 match_set_tun_dst_masked(match, dst, OVS_BE32_MAX);
219 match_set_tun_dst_masked(struct match *match, ovs_be32 dst, ovs_be32 mask)
221 match->wc.masks.tunnel.ip_dst = mask;
222 match->flow.tunnel.ip_dst = dst & mask;
226 match_set_tun_ttl(struct match *match, uint8_t ttl)
228 match_set_tun_ttl_masked(match, ttl, UINT8_MAX);
232 match_set_tun_ttl_masked(struct match *match, uint8_t ttl, uint8_t mask)
234 match->wc.masks.tunnel.ip_ttl = mask;
235 match->flow.tunnel.ip_ttl = ttl & mask;
239 match_set_tun_tos(struct match *match, uint8_t tos)
241 match_set_tun_tos_masked(match, tos, UINT8_MAX);
245 match_set_tun_tos_masked(struct match *match, uint8_t tos, uint8_t mask)
247 match->wc.masks.tunnel.ip_tos = mask;
248 match->flow.tunnel.ip_tos = tos & mask;
252 match_set_tun_flags(struct match *match, uint16_t flags)
254 match_set_tun_flags_masked(match, flags, UINT16_MAX);
258 match_set_tun_flags_masked(struct match *match, uint16_t flags, uint16_t mask)
260 match->wc.masks.tunnel.flags = mask;
261 match->flow.tunnel.flags = flags & mask;
265 match_set_in_port(struct match *match, ofp_port_t ofp_port)
267 match->wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
268 match->flow.in_port.ofp_port = ofp_port;
272 match_set_skb_priority(struct match *match, uint32_t skb_priority)
274 match->wc.masks.skb_priority = UINT32_MAX;
275 match->flow.skb_priority = skb_priority;
279 match_set_pkt_mark(struct match *match, uint32_t pkt_mark)
281 match_set_pkt_mark_masked(match, pkt_mark, UINT32_MAX);
285 match_set_pkt_mark_masked(struct match *match, uint32_t pkt_mark, uint32_t mask)
287 match->flow.pkt_mark = pkt_mark & mask;
288 match->wc.masks.pkt_mark = mask;
292 match_set_dl_type(struct match *match, ovs_be16 dl_type)
294 match->wc.masks.dl_type = OVS_BE16_MAX;
295 match->flow.dl_type = dl_type;
298 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
299 * exactly. 'mask_dst' is set to all 1s. */
301 set_eth(const uint8_t value_src[ETH_ADDR_LEN],
302 uint8_t value_dst[ETH_ADDR_LEN],
303 uint8_t mask_dst[ETH_ADDR_LEN])
305 memcpy(value_dst, value_src, ETH_ADDR_LEN);
306 memset(mask_dst, 0xff, ETH_ADDR_LEN);
309 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
310 * after each byte is ANDed with the appropriate byte in 'mask_src'.
311 * 'mask_dst' is set to 'mask_src' */
313 set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
314 const uint8_t mask_src[ETH_ADDR_LEN],
315 uint8_t value_dst[ETH_ADDR_LEN],
316 uint8_t mask_dst[ETH_ADDR_LEN])
320 for (i = 0; i < ETH_ADDR_LEN; i++) {
321 value_dst[i] = value_src[i] & mask_src[i];
322 mask_dst[i] = mask_src[i];
326 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
329 match_set_dl_src(struct match *match, const uint8_t dl_src[ETH_ADDR_LEN])
331 set_eth(dl_src, match->flow.dl_src, match->wc.masks.dl_src);
334 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
335 * after each byte is ANDed with the appropriate byte in 'mask'. */
337 match_set_dl_src_masked(struct match *match,
338 const uint8_t dl_src[ETH_ADDR_LEN],
339 const uint8_t mask[ETH_ADDR_LEN])
341 set_eth_masked(dl_src, mask, match->flow.dl_src, match->wc.masks.dl_src);
344 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
347 match_set_dl_dst(struct match *match, const uint8_t dl_dst[ETH_ADDR_LEN])
349 set_eth(dl_dst, match->flow.dl_dst, match->wc.masks.dl_dst);
352 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
353 * byte is ANDed with the appropriate byte in 'mask'.
355 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
356 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
358 match_set_dl_dst_masked(struct match *match,
359 const uint8_t dl_dst[ETH_ADDR_LEN],
360 const uint8_t mask[ETH_ADDR_LEN])
362 set_eth_masked(dl_dst, mask, match->flow.dl_dst, match->wc.masks.dl_dst);
366 match_set_dl_tci(struct match *match, ovs_be16 tci)
368 match_set_dl_tci_masked(match, tci, htons(0xffff));
372 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
374 match->flow.vlan_tci = tci & mask;
375 match->wc.masks.vlan_tci = mask;
378 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
379 * wildcarded, then 'match' will match a packet regardless of whether it has an
380 * 802.1Q header or not. */
382 match_set_any_vid(struct match *match)
384 if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
385 match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
386 match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
388 match_set_dl_tci_masked(match, htons(0), htons(0));
392 /* Modifies 'match' depending on 'dl_vlan':
394 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
395 * without an 802.1Q header.
397 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
398 * VID equals the low 12 bits of 'dl_vlan'.
401 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
403 flow_set_dl_vlan(&match->flow, dl_vlan);
404 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
405 match->wc.masks.vlan_tci = OVS_BE16_MAX;
407 match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
411 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
412 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
415 match_set_vlan_vid(struct match *match, ovs_be16 vid)
417 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
421 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
422 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
423 * plus CFI), with the corresponding 'mask'. */
425 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
427 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
428 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
431 flow_set_vlan_vid(&match->flow, vid & mask);
432 match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
435 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
436 * wildcarded, then 'match' will match a packet regardless of whether it has an
437 * 802.1Q header or not. */
439 match_set_any_pcp(struct match *match)
441 if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
442 match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
443 match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
445 match_set_dl_tci_masked(match, htons(0), htons(0));
449 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
450 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
452 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
454 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
455 match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
458 /* Modifies 'match' so that the MPLS label is wildcarded. */
460 match_set_any_mpls_label(struct match *match)
462 match->wc.masks.mpls_lse &= ~htonl(MPLS_LABEL_MASK);
463 flow_set_mpls_label(&match->flow, htonl(0));
466 /* Modifies 'match' so that it matches only packets with an MPLS header whose
467 * label equals the low 20 bits of 'mpls_label'. */
469 match_set_mpls_label(struct match *match, ovs_be32 mpls_label)
471 match->wc.masks.mpls_lse |= htonl(MPLS_LABEL_MASK);
472 flow_set_mpls_label(&match->flow, mpls_label);
475 /* Modifies 'match' so that the MPLS TC is wildcarded. */
477 match_set_any_mpls_tc(struct match *match)
479 match->wc.masks.mpls_lse &= ~htonl(MPLS_TC_MASK);
480 flow_set_mpls_tc(&match->flow, 0);
483 /* Modifies 'match' so that it matches only packets with an MPLS header whose
484 * Traffic Class equals the low 3 bits of 'mpls_tc'. */
486 match_set_mpls_tc(struct match *match, uint8_t mpls_tc)
488 match->wc.masks.mpls_lse |= htonl(MPLS_TC_MASK);
489 flow_set_mpls_tc(&match->flow, mpls_tc);
492 /* Modifies 'match' so that the MPLS stack flag is wildcarded. */
494 match_set_any_mpls_bos(struct match *match)
496 match->wc.masks.mpls_lse &= ~htonl(MPLS_BOS_MASK);
497 flow_set_mpls_bos(&match->flow, 0);
500 /* Modifies 'match' so that it matches only packets with an MPLS header whose
501 * Stack Flag equals the lower bit of 'mpls_bos' */
503 match_set_mpls_bos(struct match *match, uint8_t mpls_bos)
505 match->wc.masks.mpls_lse |= htonl(MPLS_BOS_MASK);
506 flow_set_mpls_bos(&match->flow, mpls_bos);
510 match_set_tp_src(struct match *match, ovs_be16 tp_src)
512 match_set_tp_src_masked(match, tp_src, OVS_BE16_MAX);
516 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
518 match->flow.tp_src = port & mask;
519 match->wc.masks.tp_src = mask;
523 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
525 match_set_tp_dst_masked(match, tp_dst, OVS_BE16_MAX);
529 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
531 match->flow.tp_dst = port & mask;
532 match->wc.masks.tp_dst = mask;
536 match_set_nw_proto(struct match *match, uint8_t nw_proto)
538 match->flow.nw_proto = nw_proto;
539 match->wc.masks.nw_proto = UINT8_MAX;
543 match_set_nw_src(struct match *match, ovs_be32 nw_src)
545 match->flow.nw_src = nw_src;
546 match->wc.masks.nw_src = OVS_BE32_MAX;
550 match_set_nw_src_masked(struct match *match,
551 ovs_be32 nw_src, ovs_be32 mask)
553 match->flow.nw_src = nw_src & mask;
554 match->wc.masks.nw_src = mask;
558 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
560 match->flow.nw_dst = nw_dst;
561 match->wc.masks.nw_dst = OVS_BE32_MAX;
565 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
567 match->flow.nw_dst = ip & mask;
568 match->wc.masks.nw_dst = mask;
572 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
574 match->wc.masks.nw_tos |= IP_DSCP_MASK;
575 match->flow.nw_tos &= ~IP_DSCP_MASK;
576 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
580 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
582 match->wc.masks.nw_tos |= IP_ECN_MASK;
583 match->flow.nw_tos &= ~IP_ECN_MASK;
584 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
588 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
590 match->wc.masks.nw_ttl = UINT8_MAX;
591 match->flow.nw_ttl = nw_ttl;
595 match_set_nw_frag(struct match *match, uint8_t nw_frag)
597 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
598 match->flow.nw_frag = nw_frag;
602 match_set_nw_frag_masked(struct match *match,
603 uint8_t nw_frag, uint8_t mask)
605 match->flow.nw_frag = nw_frag & mask;
606 match->wc.masks.nw_frag = mask;
610 match_set_icmp_type(struct match *match, uint8_t icmp_type)
612 match_set_tp_src(match, htons(icmp_type));
616 match_set_icmp_code(struct match *match, uint8_t icmp_code)
618 match_set_tp_dst(match, htons(icmp_code));
622 match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN])
624 memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN);
625 memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN);
629 match_set_arp_sha_masked(struct match *match,
630 const uint8_t arp_sha[ETH_ADDR_LEN],
631 const uint8_t mask[ETH_ADDR_LEN])
633 set_eth_masked(arp_sha, mask,
634 match->flow.arp_sha, match->wc.masks.arp_sha);
638 match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN])
640 memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN);
641 memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN);
645 match_set_arp_tha_masked(struct match *match,
646 const uint8_t arp_tha[ETH_ADDR_LEN],
647 const uint8_t mask[ETH_ADDR_LEN])
649 set_eth_masked(arp_tha, mask,
650 match->flow.arp_tha, match->wc.masks.arp_tha);
654 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
656 match->flow.ipv6_src = *src;
657 match->wc.masks.ipv6_src = in6addr_exact;
661 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
662 const struct in6_addr *mask)
664 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
665 match->wc.masks.ipv6_src = *mask;
669 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
671 match->flow.ipv6_dst = *dst;
672 match->wc.masks.ipv6_dst = in6addr_exact;
676 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
677 const struct in6_addr *mask)
679 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
680 match->wc.masks.ipv6_dst = *mask;
684 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
686 match->wc.masks.ipv6_label = OVS_BE32_MAX;
687 match->flow.ipv6_label = ipv6_label;
692 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
695 match->flow.ipv6_label = ipv6_label & mask;
696 match->wc.masks.ipv6_label = mask;
700 match_set_nd_target(struct match *match, const struct in6_addr *target)
702 match->flow.nd_target = *target;
703 match->wc.masks.nd_target = in6addr_exact;
707 match_set_nd_target_masked(struct match *match,
708 const struct in6_addr *target,
709 const struct in6_addr *mask)
711 match->flow.nd_target = ipv6_addr_bitand(target, mask);
712 match->wc.masks.nd_target = *mask;
715 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
716 * values for fixed fields, otherwise false. */
718 match_equal(const struct match *a, const struct match *b)
720 return (flow_wildcards_equal(&a->wc, &b->wc)
721 && flow_equal(&a->flow, &b->flow));
724 /* Returns a hash value for the flow and wildcards in 'match', starting from
727 match_hash(const struct match *match, uint32_t basis)
729 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
733 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
734 const uint8_t mask[6])
736 if (!eth_addr_is_zero(mask)) {
737 ds_put_format(s, "%s=", name);
738 eth_format_masked(eth, mask, s);
744 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
748 ds_put_format(s, "%s=", name);
749 ip_format_masked(ip, netmask, s);
755 format_ipv6_netmask(struct ds *s, const char *name,
756 const struct in6_addr *addr,
757 const struct in6_addr *netmask)
759 if (!ipv6_mask_is_any(netmask)) {
760 ds_put_format(s, "%s=", name);
761 print_ipv6_masked(s, addr, netmask);
768 format_be16_masked(struct ds *s, const char *name,
769 ovs_be16 value, ovs_be16 mask)
771 if (mask != htons(0)) {
772 ds_put_format(s, "%s=", name);
773 if (mask == OVS_BE16_MAX) {
774 ds_put_format(s, "%"PRIu16, ntohs(value));
776 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
777 ntohs(value), ntohs(mask));
784 format_flow_tunnel(struct ds *s, const struct match *match)
786 const struct flow_wildcards *wc = &match->wc;
787 const struct flow_tnl *tnl = &match->flow.tunnel;
789 switch (wc->masks.tunnel.tun_id) {
793 ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(tnl->tun_id));
796 ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
798 ntohll(wc->masks.tunnel.tun_id));
801 format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
802 format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
804 if (wc->masks.tunnel.ip_tos) {
805 ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
807 if (wc->masks.tunnel.ip_ttl) {
808 ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
810 if (wc->masks.tunnel.flags) {
811 format_flags(s, flow_tun_flag_to_string, tnl->flags, '|');
816 /* Appends a string representation of 'match' to 's'. If 'priority' is
817 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
819 match_format(const struct match *match, struct ds *s, unsigned int priority)
821 const struct flow_wildcards *wc = &match->wc;
822 size_t start_len = s->length;
823 const struct flow *f = &match->flow;
824 bool skip_type = false;
825 bool skip_proto = false;
829 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 21);
831 if (priority != OFP_DEFAULT_PRIORITY) {
832 ds_put_format(s, "priority=%u,", priority);
835 switch (wc->masks.pkt_mark) {
839 ds_put_format(s, "pkt_mark=%#"PRIx32",", f->pkt_mark);
842 ds_put_format(s, "pkt_mark=%#"PRIx32"/%#"PRIx32",",
843 f->pkt_mark, wc->masks.pkt_mark);
847 if (wc->masks.skb_priority) {
848 ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority);
851 if (wc->masks.dl_type) {
853 if (f->dl_type == htons(ETH_TYPE_IP)) {
854 if (wc->masks.nw_proto) {
856 if (f->nw_proto == IPPROTO_ICMP) {
857 ds_put_cstr(s, "icmp,");
858 } else if (f->nw_proto == IPPROTO_TCP) {
859 ds_put_cstr(s, "tcp,");
860 } else if (f->nw_proto == IPPROTO_UDP) {
861 ds_put_cstr(s, "udp,");
862 } else if (f->nw_proto == IPPROTO_SCTP) {
863 ds_put_cstr(s, "sctp,");
865 ds_put_cstr(s, "ip,");
869 ds_put_cstr(s, "ip,");
871 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
872 if (wc->masks.nw_proto) {
874 if (f->nw_proto == IPPROTO_ICMPV6) {
875 ds_put_cstr(s, "icmp6,");
876 } else if (f->nw_proto == IPPROTO_TCP) {
877 ds_put_cstr(s, "tcp6,");
878 } else if (f->nw_proto == IPPROTO_UDP) {
879 ds_put_cstr(s, "udp6,");
880 } else if (f->nw_proto == IPPROTO_SCTP) {
881 ds_put_cstr(s, "sctp6,");
883 ds_put_cstr(s, "ipv6,");
887 ds_put_cstr(s, "ipv6,");
889 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
890 ds_put_cstr(s, "arp,");
891 } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
892 ds_put_cstr(s, "rarp,");
893 } else if (f->dl_type == htons(ETH_TYPE_MPLS)) {
894 ds_put_cstr(s, "mpls,");
895 } else if (f->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
896 ds_put_cstr(s, "mplsm,");
901 for (i = 0; i < FLOW_N_REGS; i++) {
902 switch (wc->masks.regs[i]) {
906 ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
909 ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
910 i, f->regs[i], wc->masks.regs[i]);
915 format_flow_tunnel(s, match);
917 switch (wc->masks.metadata) {
921 ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata));
924 ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",",
925 ntohll(f->metadata), ntohll(wc->masks.metadata));
928 if (wc->masks.in_port.ofp_port) {
929 ds_put_cstr(s, "in_port=");
930 ofputil_format_port(f->in_port.ofp_port, s);
933 if (wc->masks.vlan_tci) {
934 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
935 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
936 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
938 if (cfi && f->vlan_tci & htons(VLAN_CFI)
939 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
940 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
941 && (vid_mask || pcp_mask)) {
943 ds_put_format(s, "dl_vlan=%"PRIu16",",
944 vlan_tci_to_vid(f->vlan_tci));
947 ds_put_format(s, "dl_vlan_pcp=%d,",
948 vlan_tci_to_pcp(f->vlan_tci));
950 } else if (wc->masks.vlan_tci == htons(0xffff)) {
951 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
953 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
954 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
957 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
958 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
959 if (!skip_type && wc->masks.dl_type) {
960 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
962 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
963 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
964 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
965 if (wc->masks.ipv6_label) {
966 if (wc->masks.ipv6_label == OVS_BE32_MAX) {
967 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
968 ntohl(f->ipv6_label));
970 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
971 ntohl(f->ipv6_label),
972 ntohl(wc->masks.ipv6_label));
975 } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
976 f->dl_type == htons(ETH_TYPE_RARP)) {
977 format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
978 format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
980 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
981 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
983 if (!skip_proto && wc->masks.nw_proto) {
984 if (f->dl_type == htons(ETH_TYPE_ARP) ||
985 f->dl_type == htons(ETH_TYPE_RARP)) {
986 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
988 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
991 if (f->dl_type == htons(ETH_TYPE_ARP) ||
992 f->dl_type == htons(ETH_TYPE_RARP)) {
993 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
994 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
996 if (wc->masks.nw_tos & IP_DSCP_MASK) {
997 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
999 if (wc->masks.nw_tos & IP_ECN_MASK) {
1000 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
1002 if (wc->masks.nw_ttl) {
1003 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
1005 if (wc->masks.mpls_lse & htonl(MPLS_LABEL_MASK)) {
1006 ds_put_format(s, "mpls_label=%"PRIu32",",
1007 mpls_lse_to_label(f->mpls_lse));
1009 if (wc->masks.mpls_lse & htonl(MPLS_TC_MASK)) {
1010 ds_put_format(s, "mpls_tc=%"PRIu8",",
1011 mpls_lse_to_tc(f->mpls_lse));
1013 if (wc->masks.mpls_lse & htonl(MPLS_TTL_MASK)) {
1014 ds_put_format(s, "mpls_ttl=%"PRIu8",",
1015 mpls_lse_to_ttl(f->mpls_lse));
1017 if (wc->masks.mpls_lse & htonl(MPLS_BOS_MASK)) {
1018 ds_put_format(s, "mpls_bos=%"PRIu8",",
1019 mpls_lse_to_bos(f->mpls_lse));
1021 switch (wc->masks.nw_frag) {
1022 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
1023 ds_put_format(s, "nw_frag=%s,",
1024 f->nw_frag & FLOW_NW_FRAG_ANY
1025 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
1026 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
1029 case FLOW_NW_FRAG_ANY:
1030 ds_put_format(s, "nw_frag=%s,",
1031 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
1034 case FLOW_NW_FRAG_LATER:
1035 ds_put_format(s, "nw_frag=%s,",
1036 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
1039 if (f->dl_type == htons(ETH_TYPE_IP) &&
1040 f->nw_proto == IPPROTO_ICMP) {
1041 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1042 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1043 } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
1044 f->nw_proto == IPPROTO_ICMPV6) {
1045 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1046 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1047 format_ipv6_netmask(s, "nd_target", &f->nd_target,
1048 &wc->masks.nd_target);
1049 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
1050 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
1052 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
1053 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
1056 if (s->length > start_len && ds_last(s) == ',') {
1061 /* Converts 'match' to a string and returns the string. If 'priority' is
1062 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1063 * must free the string (with free()). */
1065 match_to_string(const struct match *match, unsigned int priority)
1067 struct ds s = DS_EMPTY_INITIALIZER;
1068 match_format(match, &s, priority);
1069 return ds_steal_cstr(&s);
1073 match_print(const struct match *match)
1075 char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
1080 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1081 * with minimatch_destroy(). */
1083 minimatch_init(struct minimatch *dst, const struct match *src)
1085 minimask_init(&dst->mask, &src->wc);
1086 miniflow_init_with_minimask(&dst->flow, &src->flow, &dst->mask);
1089 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1090 * with minimatch_destroy(). */
1092 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1094 miniflow_clone(&dst->flow, &src->flow);
1095 minimask_clone(&dst->mask, &src->mask);
1098 /* Initializes 'dst' with the data in 'src', destroying 'src'. The caller must
1099 * eventually free 'dst' with minimatch_destroy(). */
1101 minimatch_move(struct minimatch *dst, struct minimatch *src)
1103 miniflow_move(&dst->flow, &src->flow);
1104 minimask_move(&dst->mask, &src->mask);
1107 /* Frees any memory owned by 'match'. Does not free the storage in which
1108 * 'match' itself resides; the caller is responsible for that. */
1110 minimatch_destroy(struct minimatch *match)
1112 miniflow_destroy(&match->flow);
1113 minimask_destroy(&match->mask);
1116 /* Initializes 'dst' as a copy of 'src'. */
1118 minimatch_expand(const struct minimatch *src, struct match *dst)
1120 miniflow_expand(&src->flow, &dst->flow);
1121 minimask_expand(&src->mask, &dst->wc);
1124 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
1126 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1128 return (miniflow_equal(&a->flow, &b->flow)
1129 && minimask_equal(&a->mask, &b->mask));
1132 /* Returns a hash value for 'match', given 'basis'. */
1134 minimatch_hash(const struct minimatch *match, uint32_t basis)
1136 return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
1139 /* Returns true if 'target' satisifies 'match', that is, if each bit for which
1140 * 'match' specifies a particular value has the correct value in 'target'.
1142 * This function is equivalent to miniflow_equal_flow_in_minimask(&match->flow,
1143 * target, &match->mask) but it is faster because of the invariant that
1144 * match->flow.map and match->mask.map are the same. */
1146 minimatch_matches_flow(const struct minimatch *match,
1147 const struct flow *target)
1149 const uint32_t *target_u32 = (const uint32_t *) target;
1150 const uint32_t *flowp = match->flow.values;
1151 const uint32_t *maskp = match->mask.masks.values;
1154 for (i = 0; i < MINI_N_MAPS; i++) {
1157 for (map = match->flow.map[i]; map; map = zero_rightmost_1bit(map)) {
1158 if ((*flowp++ ^ target_u32[raw_ctz(map)]) & *maskp++) {
1168 /* Appends a string representation of 'match' to 's'. If 'priority' is
1169 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1171 minimatch_format(const struct minimatch *match, struct ds *s,
1172 unsigned int priority)
1174 struct match megamatch;
1176 minimatch_expand(match, &megamatch);
1177 match_format(&megamatch, s, priority);
1180 /* Converts 'match' to a string and returns the string. If 'priority' is
1181 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1182 * must free the string (with free()). */
1184 minimatch_to_string(const struct minimatch *match, unsigned int priority)
1186 struct match megamatch;
1188 minimatch_expand(match, &megamatch);
1189 return match_to_string(&megamatch, priority);