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"
26 VLOG_DEFINE_THIS_MODULE(match);
29 /* Converts the flow in 'flow' into a match in 'match', with the given
32 match_init(struct match *match,
33 const struct flow *flow, const struct flow_wildcards *wc)
37 match_zero_wildcarded_fields(match);
40 /* Converts a flow into a match. It sets the wildcard masks based on
41 * the packet contents. It will not set the mask for fields that do not
42 * make sense for the packet type. */
44 match_wc_init(struct match *match, const struct flow *flow)
46 struct flow_wildcards *wc;
51 memset(&wc->masks, 0x0, sizeof wc->masks);
53 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
56 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
59 if (flow->skb_priority) {
60 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
64 memset(&wc->masks.skb_mark, 0xff, sizeof wc->masks.skb_mark);
67 for (i = 0; i < FLOW_N_REGS; i++) {
69 memset(&wc->masks.regs[i], 0xff, sizeof wc->masks.regs[i]);
73 if (flow->tunnel.ip_dst) {
74 if (flow->tunnel.flags & FLOW_TNL_F_KEY) {
75 memset(&wc->masks.tunnel.tun_id, 0xff, sizeof wc->masks.tunnel.tun_id);
77 memset(&wc->masks.tunnel.ip_src, 0xff, sizeof wc->masks.tunnel.ip_src);
78 memset(&wc->masks.tunnel.ip_dst, 0xff, sizeof wc->masks.tunnel.ip_dst);
79 memset(&wc->masks.tunnel.flags, 0xff, sizeof wc->masks.tunnel.flags);
80 memset(&wc->masks.tunnel.ip_tos, 0xff, sizeof wc->masks.tunnel.ip_tos);
81 memset(&wc->masks.tunnel.ip_ttl, 0xff, sizeof wc->masks.tunnel.ip_ttl);
82 } else if (flow->tunnel.tun_id) {
83 memset(&wc->masks.tunnel.tun_id, 0xff, sizeof wc->masks.tunnel.tun_id);
86 memset(&wc->masks.metadata, 0xff, sizeof wc->masks.metadata);
87 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
88 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
89 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
90 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
92 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
93 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
94 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
95 memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
96 } else if (flow->dl_type == htons(ETH_TYPE_IP) ||
97 (flow->dl_type == htons(ETH_TYPE_ARP)) ||
98 (flow->dl_type == htons(ETH_TYPE_RARP))) {
99 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
100 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
103 if (flow->dl_type == htons(ETH_TYPE_ARP) ||
104 flow->dl_type == htons(ETH_TYPE_RARP)) {
105 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
106 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
109 if (is_ip_any(flow)) {
110 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
111 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
115 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
118 if (flow->nw_proto == IPPROTO_ICMP || flow->nw_proto == IPPROTO_ICMPV6 ||
119 (flow->tp_src || flow->tp_dst)) {
120 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
121 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
124 if (flow->nw_proto == IPPROTO_ICMPV6) {
125 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
126 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
132 /* Converts the flow in 'flow' into an exact-match match in 'match'. */
134 match_init_exact(struct match *match, const struct flow *flow)
136 ovs_be64 tun_id = flow->tunnel.tun_id;
139 match->flow.skb_priority = 0;
140 match->flow.skb_mark = 0;
141 memset(&match->flow.tunnel, 0, sizeof match->flow.tunnel);
142 match->flow.tunnel.tun_id = tun_id;
143 flow_wildcards_init_exact(&match->wc);
146 /* Initializes 'match' as a "catch-all" match that matches every packet. */
148 match_init_catchall(struct match *match)
150 memset(&match->flow, 0, sizeof match->flow);
151 flow_wildcards_init_catchall(&match->wc);
154 /* For each bit or field wildcarded in 'match', sets the corresponding bit or
155 * field in 'flow' to all-0-bits. It is important to maintain this invariant
156 * in a match that might be inserted into a classifier.
158 * It is never necessary to call this function directly for a match that is
159 * initialized or modified only by match_*() functions. It is useful to
160 * restore the invariant in a match whose 'wc' member is modified by hand.
163 match_zero_wildcarded_fields(struct match *match)
165 flow_zero_wildcards(&match->flow, &match->wc);
169 match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
171 match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
175 match_set_reg_masked(struct match *match, unsigned int reg_idx,
176 uint32_t value, uint32_t mask)
178 ovs_assert(reg_idx < FLOW_N_REGS);
179 flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
180 match->flow.regs[reg_idx] = value & mask;
184 match_set_metadata(struct match *match, ovs_be64 metadata)
186 match_set_metadata_masked(match, metadata, htonll(UINT64_MAX));
190 match_set_metadata_masked(struct match *match,
191 ovs_be64 metadata, ovs_be64 mask)
193 match->wc.masks.metadata = mask;
194 match->flow.metadata = metadata & mask;
198 match_set_tun_id(struct match *match, ovs_be64 tun_id)
200 match_set_tun_id_masked(match, tun_id, htonll(UINT64_MAX));
204 match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
206 match->wc.masks.tunnel.tun_id = mask;
207 match->flow.tunnel.tun_id = tun_id & mask;
211 match_set_tun_src(struct match *match, ovs_be32 src)
213 match_set_tun_src_masked(match, src, htonl(UINT32_MAX));
217 match_set_tun_src_masked(struct match *match, ovs_be32 src, ovs_be32 mask)
219 match->wc.masks.tunnel.ip_src = mask;
220 match->flow.tunnel.ip_src = src & mask;
224 match_set_tun_dst(struct match *match, ovs_be32 dst)
226 match_set_tun_dst_masked(match, dst, htonl(UINT32_MAX));
230 match_set_tun_dst_masked(struct match *match, ovs_be32 dst, ovs_be32 mask)
232 match->wc.masks.tunnel.ip_dst = mask;
233 match->flow.tunnel.ip_dst = dst & mask;
237 match_set_tun_ttl(struct match *match, uint8_t ttl)
239 match_set_tun_ttl_masked(match, ttl, UINT8_MAX);
243 match_set_tun_ttl_masked(struct match *match, uint8_t ttl, uint8_t mask)
245 match->wc.masks.tunnel.ip_ttl = mask;
246 match->flow.tunnel.ip_ttl = ttl & mask;
250 match_set_tun_tos(struct match *match, uint8_t tos)
252 match_set_tun_tos_masked(match, tos, UINT8_MAX);
256 match_set_tun_tos_masked(struct match *match, uint8_t tos, uint8_t mask)
258 match->wc.masks.tunnel.ip_tos = mask;
259 match->flow.tunnel.ip_tos = tos & mask;
263 match_set_tun_flags(struct match *match, uint16_t flags)
265 match_set_tun_flags_masked(match, flags, UINT16_MAX);
269 match_set_tun_flags_masked(struct match *match, uint16_t flags, uint16_t mask)
271 match->wc.masks.tunnel.flags = mask;
272 match->flow.tunnel.flags = flags & mask;
276 match_set_in_port(struct match *match, uint16_t ofp_port)
278 match->wc.masks.in_port = UINT16_MAX;
279 match->flow.in_port = ofp_port;
283 match_set_skb_priority(struct match *match, uint32_t skb_priority)
285 match->wc.masks.skb_priority = UINT32_MAX;
286 match->flow.skb_priority = skb_priority;
290 match_set_skb_mark(struct match *match, uint32_t skb_mark)
292 match->wc.masks.skb_mark = UINT32_MAX;
293 match->flow.skb_mark = skb_mark;
297 match_set_dl_type(struct match *match, ovs_be16 dl_type)
299 match->wc.masks.dl_type = htons(UINT16_MAX);
300 match->flow.dl_type = dl_type;
303 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
304 * exactly. 'mask_dst' is set to all 1s. */
306 set_eth(const uint8_t value_src[ETH_ADDR_LEN],
307 uint8_t value_dst[ETH_ADDR_LEN],
308 uint8_t mask_dst[ETH_ADDR_LEN])
310 memcpy(value_dst, value_src, ETH_ADDR_LEN);
311 memset(mask_dst, 0xff, ETH_ADDR_LEN);
314 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
315 * after each byte is ANDed with the appropriate byte in 'mask_src'.
316 * 'mask_dst' is set to 'mask_src' */
318 set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
319 const uint8_t mask_src[ETH_ADDR_LEN],
320 uint8_t value_dst[ETH_ADDR_LEN],
321 uint8_t mask_dst[ETH_ADDR_LEN])
325 for (i = 0; i < ETH_ADDR_LEN; i++) {
326 value_dst[i] = value_src[i] & mask_src[i];
327 mask_dst[i] = mask_src[i];
331 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
334 match_set_dl_src(struct match *match, const uint8_t dl_src[ETH_ADDR_LEN])
336 set_eth(dl_src, match->flow.dl_src, match->wc.masks.dl_src);
339 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
340 * after each byte is ANDed with the appropriate byte in 'mask'. */
342 match_set_dl_src_masked(struct match *match,
343 const uint8_t dl_src[ETH_ADDR_LEN],
344 const uint8_t mask[ETH_ADDR_LEN])
346 set_eth_masked(dl_src, mask, match->flow.dl_src, match->wc.masks.dl_src);
349 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
352 match_set_dl_dst(struct match *match, const uint8_t dl_dst[ETH_ADDR_LEN])
354 set_eth(dl_dst, match->flow.dl_dst, match->wc.masks.dl_dst);
357 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
358 * byte is ANDed with the appropriate byte in 'mask'.
360 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
361 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
363 match_set_dl_dst_masked(struct match *match,
364 const uint8_t dl_dst[ETH_ADDR_LEN],
365 const uint8_t mask[ETH_ADDR_LEN])
367 set_eth_masked(dl_dst, mask, match->flow.dl_dst, match->wc.masks.dl_dst);
371 match_set_dl_tci(struct match *match, ovs_be16 tci)
373 match_set_dl_tci_masked(match, tci, htons(0xffff));
377 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
379 match->flow.vlan_tci = tci & mask;
380 match->wc.masks.vlan_tci = mask;
383 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
384 * wildcarded, then 'match' will match a packet regardless of whether it has an
385 * 802.1Q header or not. */
387 match_set_any_vid(struct match *match)
389 if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
390 match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
391 match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
393 match_set_dl_tci_masked(match, htons(0), htons(0));
397 /* Modifies 'match' depending on 'dl_vlan':
399 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
400 * without an 802.1Q header.
402 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
403 * VID equals the low 12 bits of 'dl_vlan'.
406 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
408 flow_set_dl_vlan(&match->flow, dl_vlan);
409 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
410 match->wc.masks.vlan_tci = htons(UINT16_MAX);
412 match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
416 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
417 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
420 match_set_vlan_vid(struct match *match, ovs_be16 vid)
422 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
426 /* Sets the VLAN VID that 'flow' 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
428 * plus CFI), with the corresponding 'mask'. */
430 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
432 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
433 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
436 flow_set_vlan_vid(&match->flow, vid & mask);
437 match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
440 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
441 * wildcarded, then 'match' will match a packet regardless of whether it has an
442 * 802.1Q header or not. */
444 match_set_any_pcp(struct match *match)
446 if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
447 match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
448 match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
450 match_set_dl_tci_masked(match, htons(0), htons(0));
454 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
455 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
457 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
459 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
460 match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
463 /* Modifies 'match' so that the MPLS label is wildcarded. */
465 match_set_any_mpls_label(struct match *match)
467 match->wc.masks.mpls_lse &= ~htonl(MPLS_LABEL_MASK);
468 flow_set_mpls_label(&match->flow, htonl(0));
471 /* Modifies 'match' so that it matches only packets with an MPLS header whose
472 * label equals the low 20 bits of 'mpls_label'. */
474 match_set_mpls_label(struct match *match, ovs_be32 mpls_label)
476 match->wc.masks.mpls_lse |= htonl(MPLS_LABEL_MASK);
477 flow_set_mpls_label(&match->flow, mpls_label);
480 /* Modifies 'match' so that the MPLS TC is wildcarded. */
482 match_set_any_mpls_tc(struct match *match)
484 match->wc.masks.mpls_lse &= ~htonl(MPLS_TC_MASK);
485 flow_set_mpls_tc(&match->flow, 0);
488 /* Modifies 'match' so that it matches only packets with an MPLS header whose
489 * Traffic Class equals the low 3 bits of 'mpls_tc'. */
491 match_set_mpls_tc(struct match *match, uint8_t mpls_tc)
493 match->wc.masks.mpls_lse |= htonl(MPLS_TC_MASK);
494 flow_set_mpls_tc(&match->flow, mpls_tc);
497 /* Modifies 'match' so that the MPLS stack flag is wildcarded. */
499 match_set_any_mpls_bos(struct match *match)
501 match->wc.masks.mpls_lse &= ~htonl(MPLS_BOS_MASK);
502 flow_set_mpls_bos(&match->flow, 0);
505 /* Modifies 'match' so that it matches only packets with an MPLS header whose
506 * Stack Flag equals the lower bit of 'mpls_bos' */
508 match_set_mpls_bos(struct match *match, uint8_t mpls_bos)
510 match->wc.masks.mpls_lse |= htonl(MPLS_BOS_MASK);
511 flow_set_mpls_bos(&match->flow, mpls_bos);
515 match_set_tp_src(struct match *match, ovs_be16 tp_src)
517 match_set_tp_src_masked(match, tp_src, htons(UINT16_MAX));
521 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
523 match->flow.tp_src = port & mask;
524 match->wc.masks.tp_src = mask;
528 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
530 match_set_tp_dst_masked(match, tp_dst, htons(UINT16_MAX));
534 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
536 match->flow.tp_dst = port & mask;
537 match->wc.masks.tp_dst = mask;
541 match_set_nw_proto(struct match *match, uint8_t nw_proto)
543 match->flow.nw_proto = nw_proto;
544 match->wc.masks.nw_proto = UINT8_MAX;
548 match_set_nw_src(struct match *match, ovs_be32 nw_src)
550 match->flow.nw_src = nw_src;
551 match->wc.masks.nw_src = htonl(UINT32_MAX);
555 match_set_nw_src_masked(struct match *match,
556 ovs_be32 nw_src, ovs_be32 mask)
558 match->flow.nw_src = nw_src & mask;
559 match->wc.masks.nw_src = mask;
563 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
565 match->flow.nw_dst = nw_dst;
566 match->wc.masks.nw_dst = htonl(UINT32_MAX);
570 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
572 match->flow.nw_dst = ip & mask;
573 match->wc.masks.nw_dst = mask;
577 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
579 match->wc.masks.nw_tos |= IP_DSCP_MASK;
580 match->flow.nw_tos &= ~IP_DSCP_MASK;
581 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
585 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
587 match->wc.masks.nw_tos |= IP_ECN_MASK;
588 match->flow.nw_tos &= ~IP_ECN_MASK;
589 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
593 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
595 match->wc.masks.nw_ttl = UINT8_MAX;
596 match->flow.nw_ttl = nw_ttl;
600 match_set_nw_frag(struct match *match, uint8_t nw_frag)
602 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
603 match->flow.nw_frag = nw_frag;
607 match_set_nw_frag_masked(struct match *match,
608 uint8_t nw_frag, uint8_t mask)
610 match->flow.nw_frag = nw_frag & mask;
611 match->wc.masks.nw_frag = mask;
615 match_set_icmp_type(struct match *match, uint8_t icmp_type)
617 match_set_tp_src(match, htons(icmp_type));
621 match_set_icmp_code(struct match *match, uint8_t icmp_code)
623 match_set_tp_dst(match, htons(icmp_code));
627 match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN])
629 memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN);
630 memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN);
634 match_set_arp_sha_masked(struct match *match,
635 const uint8_t arp_sha[ETH_ADDR_LEN],
636 const uint8_t mask[ETH_ADDR_LEN])
638 set_eth_masked(arp_sha, mask,
639 match->flow.arp_sha, match->wc.masks.arp_sha);
643 match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN])
645 memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN);
646 memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN);
650 match_set_arp_tha_masked(struct match *match,
651 const uint8_t arp_tha[ETH_ADDR_LEN],
652 const uint8_t mask[ETH_ADDR_LEN])
654 set_eth_masked(arp_tha, mask,
655 match->flow.arp_tha, match->wc.masks.arp_tha);
659 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
661 match->flow.ipv6_src = *src;
662 match->wc.masks.ipv6_src = in6addr_exact;
666 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
667 const struct in6_addr *mask)
669 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
670 match->wc.masks.ipv6_src = *mask;
674 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
676 match->flow.ipv6_dst = *dst;
677 match->wc.masks.ipv6_dst = in6addr_exact;
681 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
682 const struct in6_addr *mask)
684 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
685 match->wc.masks.ipv6_dst = *mask;
689 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
691 match->wc.masks.ipv6_label = htonl(UINT32_MAX);
692 match->flow.ipv6_label = ipv6_label;
697 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
700 match->flow.ipv6_label = ipv6_label & mask;
701 match->wc.masks.ipv6_label = mask;
705 match_set_nd_target(struct match *match, const struct in6_addr *target)
707 match->flow.nd_target = *target;
708 match->wc.masks.nd_target = in6addr_exact;
712 match_set_nd_target_masked(struct match *match,
713 const struct in6_addr *target,
714 const struct in6_addr *mask)
716 match->flow.nd_target = ipv6_addr_bitand(target, mask);
717 match->wc.masks.nd_target = *mask;
720 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
721 * values for fixed fields, otherwise false. */
723 match_equal(const struct match *a, const struct match *b)
725 return (flow_wildcards_equal(&a->wc, &b->wc)
726 && flow_equal(&a->flow, &b->flow));
729 /* Returns a hash value for the flow and wildcards in 'match', starting from
732 match_hash(const struct match *match, uint32_t basis)
734 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
738 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
739 const uint8_t mask[6])
741 if (!eth_addr_is_zero(mask)) {
742 ds_put_format(s, "%s=", name);
743 eth_format_masked(eth, mask, s);
749 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
753 ds_put_format(s, "%s=", name);
754 ip_format_masked(ip, netmask, s);
760 format_ipv6_netmask(struct ds *s, const char *name,
761 const struct in6_addr *addr,
762 const struct in6_addr *netmask)
764 if (!ipv6_mask_is_any(netmask)) {
765 ds_put_format(s, "%s=", name);
766 print_ipv6_masked(s, addr, netmask);
773 format_be16_masked(struct ds *s, const char *name,
774 ovs_be16 value, ovs_be16 mask)
776 if (mask != htons(0)) {
777 ds_put_format(s, "%s=", name);
778 if (mask == htons(UINT16_MAX)) {
779 ds_put_format(s, "%"PRIu16, ntohs(value));
781 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
782 ntohs(value), ntohs(mask));
789 format_flow_tunnel(struct ds *s, const struct match *match)
791 const struct flow_wildcards *wc = &match->wc;
792 const struct flow_tnl *tnl = &match->flow.tunnel;
794 switch (wc->masks.tunnel.tun_id) {
797 case CONSTANT_HTONLL(UINT64_MAX):
798 ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(tnl->tun_id));
801 ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
803 ntohll(wc->masks.tunnel.tun_id));
806 format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
807 format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
809 if (wc->masks.tunnel.ip_tos) {
810 ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
812 if (wc->masks.tunnel.ip_ttl) {
813 ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
815 if (wc->masks.tunnel.flags) {
816 format_flags(s, flow_tun_flag_to_string, tnl->flags, '|');
822 flow_format_mpls(const struct flow *flow, struct ds *s)
824 if (flow->dl_type == htons(ETH_TYPE_MPLS)) {
825 ds_put_cstr(s, "mpls");
826 } else if (flow->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
827 ds_put_cstr(s, "mplsm");
832 ds_put_format(s, "(label:%"PRIu32",tc:%d,ttl:%d,bos:%d),",
833 mpls_lse_to_label(flow->mpls_lse),
834 mpls_lse_to_tc(flow->mpls_lse),
835 mpls_lse_to_ttl(flow->mpls_lse),
836 mpls_lse_to_bos(flow->mpls_lse));
839 /* Appends a string representation of 'match' to 's'. If 'priority' is
840 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
842 match_format(const struct match *match, struct ds *s, unsigned int priority)
844 const struct flow_wildcards *wc = &match->wc;
845 size_t start_len = s->length;
846 const struct flow *f = &match->flow;
847 bool skip_type = false;
848 bool skip_proto = false;
852 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 19);
854 if (priority != OFP_DEFAULT_PRIORITY) {
855 ds_put_format(s, "priority=%u,", priority);
858 if (wc->masks.skb_mark) {
859 ds_put_format(s, "skb_mark=%#"PRIx32",", f->skb_mark);
862 if (wc->masks.skb_priority) {
863 ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority);
866 if (wc->masks.dl_type) {
868 if (f->dl_type == htons(ETH_TYPE_IP)) {
869 if (wc->masks.nw_proto) {
871 if (f->nw_proto == IPPROTO_ICMP) {
872 ds_put_cstr(s, "icmp,");
873 } else if (f->nw_proto == IPPROTO_TCP) {
874 ds_put_cstr(s, "tcp,");
875 } else if (f->nw_proto == IPPROTO_UDP) {
876 ds_put_cstr(s, "udp,");
878 ds_put_cstr(s, "ip,");
882 ds_put_cstr(s, "ip,");
884 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
885 if (wc->masks.nw_proto) {
887 if (f->nw_proto == IPPROTO_ICMPV6) {
888 ds_put_cstr(s, "icmp6,");
889 } else if (f->nw_proto == IPPROTO_TCP) {
890 ds_put_cstr(s, "tcp6,");
891 } else if (f->nw_proto == IPPROTO_UDP) {
892 ds_put_cstr(s, "udp6,");
894 ds_put_cstr(s, "ipv6,");
898 ds_put_cstr(s, "ipv6,");
900 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
901 ds_put_cstr(s, "arp,");
902 } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
903 ds_put_cstr(s, "rarp,");
904 } else if (f->mpls_depth) {
905 flow_format_mpls(f, s);
910 for (i = 0; i < FLOW_N_REGS; i++) {
911 switch (wc->masks.regs[i]) {
915 ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
918 ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
919 i, f->regs[i], wc->masks.regs[i]);
924 format_flow_tunnel(s, match);
926 switch (wc->masks.metadata) {
929 case CONSTANT_HTONLL(UINT64_MAX):
930 ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata));
933 ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",",
934 ntohll(f->metadata), ntohll(wc->masks.metadata));
937 if (wc->masks.in_port) {
938 ds_put_cstr(s, "in_port=");
939 ofputil_format_port(f->in_port, s);
942 if (wc->masks.vlan_tci) {
943 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
944 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
945 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
947 if (cfi && f->vlan_tci & htons(VLAN_CFI)
948 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
949 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
950 && (vid_mask || pcp_mask)) {
952 ds_put_format(s, "dl_vlan=%"PRIu16",",
953 vlan_tci_to_vid(f->vlan_tci));
956 ds_put_format(s, "dl_vlan_pcp=%d,",
957 vlan_tci_to_pcp(f->vlan_tci));
959 } else if (wc->masks.vlan_tci == htons(0xffff)) {
960 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
962 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
963 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
966 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
967 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
968 if (!skip_type && wc->masks.dl_type) {
969 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
971 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
972 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
973 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
974 if (wc->masks.ipv6_label) {
975 if (wc->masks.ipv6_label == htonl(UINT32_MAX)) {
976 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
977 ntohl(f->ipv6_label));
979 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
980 ntohl(f->ipv6_label),
981 ntohl(wc->masks.ipv6_label));
984 } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
985 f->dl_type == htons(ETH_TYPE_RARP)) {
986 format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
987 format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
989 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
990 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
992 if (!skip_proto && wc->masks.nw_proto) {
993 if (f->dl_type == htons(ETH_TYPE_ARP) ||
994 f->dl_type == htons(ETH_TYPE_RARP)) {
995 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
997 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
1000 if (f->dl_type == htons(ETH_TYPE_ARP) ||
1001 f->dl_type == htons(ETH_TYPE_RARP)) {
1002 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
1003 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
1005 if (wc->masks.nw_tos & IP_DSCP_MASK) {
1006 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
1008 if (wc->masks.nw_tos & IP_ECN_MASK) {
1009 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
1011 if (wc->masks.nw_ttl) {
1012 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
1014 if (wc->masks.mpls_lse & htonl(MPLS_LABEL_MASK)) {
1015 ds_put_format(s, "mpls_label=%"PRIu32",",
1016 mpls_lse_to_label(f->mpls_lse));
1018 if (wc->masks.mpls_lse & htonl(MPLS_TC_MASK)) {
1019 ds_put_format(s, "mpls_tc=%"PRIu8",",
1020 mpls_lse_to_tc(f->mpls_lse));
1022 if (wc->masks.mpls_lse & htonl(MPLS_BOS_MASK)) {
1023 ds_put_format(s, "mpls_bos=%"PRIu8",",
1024 mpls_lse_to_bos(f->mpls_lse));
1026 switch (wc->masks.nw_frag) {
1027 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
1028 ds_put_format(s, "nw_frag=%s,",
1029 f->nw_frag & FLOW_NW_FRAG_ANY
1030 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
1031 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
1034 case FLOW_NW_FRAG_ANY:
1035 ds_put_format(s, "nw_frag=%s,",
1036 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
1039 case FLOW_NW_FRAG_LATER:
1040 ds_put_format(s, "nw_frag=%s,",
1041 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
1044 if (f->dl_type == htons(ETH_TYPE_IP) &&
1045 f->nw_proto == IPPROTO_ICMP) {
1046 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1047 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1048 } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
1049 f->nw_proto == IPPROTO_ICMPV6) {
1050 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1051 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1052 format_ipv6_netmask(s, "nd_target", &f->nd_target,
1053 &wc->masks.nd_target);
1054 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
1055 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
1057 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
1058 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
1061 if (s->length > start_len && ds_last(s) == ',') {
1066 /* Converts 'match' to a string and returns the string. If 'priority' is
1067 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1068 * must free the string (with free()). */
1070 match_to_string(const struct match *match, unsigned int priority)
1072 struct ds s = DS_EMPTY_INITIALIZER;
1073 match_format(match, &s, priority);
1074 return ds_steal_cstr(&s);
1078 match_print(const struct match *match)
1080 char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
1085 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1086 * with minimatch_destroy(). */
1088 minimatch_init(struct minimatch *dst, const struct match *src)
1090 miniflow_init(&dst->flow, &src->flow);
1091 minimask_init(&dst->mask, &src->wc);
1094 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1095 * with minimatch_destroy(). */
1097 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1099 miniflow_clone(&dst->flow, &src->flow);
1100 minimask_clone(&dst->mask, &src->mask);
1103 /* Frees any memory owned by 'match'. Does not free the storage in which
1104 * 'match' itself resides; the caller is responsible for that. */
1106 minimatch_destroy(struct minimatch *match)
1108 miniflow_destroy(&match->flow);
1109 minimask_destroy(&match->mask);
1112 /* Initializes 'dst' as a copy of 'src'. */
1114 minimatch_expand(const struct minimatch *src, struct match *dst)
1116 miniflow_expand(&src->flow, &dst->flow);
1117 minimask_expand(&src->mask, &dst->wc);
1120 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
1122 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1124 return (miniflow_equal(&a->flow, &b->flow)
1125 && minimask_equal(&a->mask, &b->mask));
1128 /* Returns a hash value for 'match', given 'basis'. */
1130 minimatch_hash(const struct minimatch *match, uint32_t basis)
1132 return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
1135 /* Appends a string representation of 'match' to 's'. If 'priority' is
1136 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1138 minimatch_format(const struct minimatch *match, struct ds *s,
1139 unsigned int priority)
1141 struct match megamatch;
1143 minimatch_expand(match, &megamatch);
1144 match_format(&megamatch, s, priority);
1147 /* Converts 'match' to a string and returns the string. If 'priority' is
1148 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1149 * must free the string (with free()). */
1151 minimatch_to_string(const struct minimatch *match, unsigned int priority)
1153 struct match megamatch;
1155 minimatch_expand(match, &megamatch);
1156 return match_to_string(&megamatch, priority);