2 * Copyright (c) 2009, 2010, 2011, 2012 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 (flow->dl_type == htons(ETH_TYPE_IPV6) ||
110 flow->dl_type == htons(ETH_TYPE_IP)) {
111 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
112 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
116 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
119 if (flow->nw_proto == IPPROTO_ICMP || flow->nw_proto == IPPROTO_ICMPV6 ||
120 (flow->tp_src || flow->tp_dst)) {
121 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
122 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
125 if (flow->nw_proto == IPPROTO_ICMPV6) {
126 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
127 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
133 /* Converts the flow in 'flow' into an exact-match match in 'match'. */
135 match_init_exact(struct match *match, const struct flow *flow)
137 ovs_be64 tun_id = flow->tunnel.tun_id;
140 match->flow.skb_priority = 0;
141 match->flow.skb_mark = 0;
142 memset(&match->flow.tunnel, 0, sizeof match->flow.tunnel);
143 match->flow.tunnel.tun_id = tun_id;
144 flow_wildcards_init_exact(&match->wc);
147 /* Initializes 'match' as a "catch-all" match that matches every packet. */
149 match_init_catchall(struct match *match)
151 memset(&match->flow, 0, sizeof match->flow);
152 flow_wildcards_init_catchall(&match->wc);
155 /* For each bit or field wildcarded in 'match', sets the corresponding bit or
156 * field in 'flow' to all-0-bits. It is important to maintain this invariant
157 * in a match that might be inserted into a classifier.
159 * It is never necessary to call this function directly for a match that is
160 * initialized or modified only by match_*() functions. It is useful to
161 * restore the invariant in a match whose 'wc' member is modified by hand.
164 match_zero_wildcarded_fields(struct match *match)
166 flow_zero_wildcards(&match->flow, &match->wc);
170 match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
172 match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
176 match_set_reg_masked(struct match *match, unsigned int reg_idx,
177 uint32_t value, uint32_t mask)
179 ovs_assert(reg_idx < FLOW_N_REGS);
180 flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
181 match->flow.regs[reg_idx] = value & mask;
185 match_set_metadata(struct match *match, ovs_be64 metadata)
187 match_set_metadata_masked(match, metadata, htonll(UINT64_MAX));
191 match_set_metadata_masked(struct match *match,
192 ovs_be64 metadata, ovs_be64 mask)
194 match->wc.masks.metadata = mask;
195 match->flow.metadata = metadata & mask;
199 match_set_tun_id(struct match *match, ovs_be64 tun_id)
201 match_set_tun_id_masked(match, tun_id, htonll(UINT64_MAX));
205 match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
207 match->wc.masks.tunnel.tun_id = mask;
208 match->flow.tunnel.tun_id = tun_id & mask;
212 match_set_tun_src(struct match *match, ovs_be32 src)
214 match_set_tun_src_masked(match, src, htonl(UINT32_MAX));
218 match_set_tun_src_masked(struct match *match, ovs_be32 src, ovs_be32 mask)
220 match->wc.masks.tunnel.ip_src = mask;
221 match->flow.tunnel.ip_src = src & mask;
225 match_set_tun_dst(struct match *match, ovs_be32 dst)
227 match_set_tun_dst_masked(match, dst, htonl(UINT32_MAX));
231 match_set_tun_dst_masked(struct match *match, ovs_be32 dst, ovs_be32 mask)
233 match->wc.masks.tunnel.ip_dst = mask;
234 match->flow.tunnel.ip_dst = dst & mask;
238 match_set_tun_ttl(struct match *match, uint8_t ttl)
240 match_set_tun_ttl_masked(match, ttl, UINT8_MAX);
244 match_set_tun_ttl_masked(struct match *match, uint8_t ttl, uint8_t mask)
246 match->wc.masks.tunnel.ip_ttl = mask;
247 match->flow.tunnel.ip_ttl = ttl & mask;
251 match_set_tun_tos(struct match *match, uint8_t tos)
253 match_set_tun_tos_masked(match, tos, UINT8_MAX);
257 match_set_tun_tos_masked(struct match *match, uint8_t tos, uint8_t mask)
259 match->wc.masks.tunnel.ip_tos = mask;
260 match->flow.tunnel.ip_tos = tos & mask;
264 match_set_tun_flags(struct match *match, uint16_t flags)
266 match_set_tun_flags_masked(match, flags, UINT16_MAX);
270 match_set_tun_flags_masked(struct match *match, uint16_t flags, uint16_t mask)
272 match->wc.masks.tunnel.flags = mask;
273 match->flow.tunnel.flags = flags & mask;
277 match_set_in_port(struct match *match, uint16_t ofp_port)
279 match->wc.masks.in_port = UINT16_MAX;
280 match->flow.in_port = ofp_port;
284 match_set_skb_priority(struct match *match, uint32_t skb_priority)
286 match->wc.masks.skb_priority = UINT32_MAX;
287 match->flow.skb_priority = skb_priority;
291 match_set_skb_mark(struct match *match, uint32_t skb_mark)
293 match->wc.masks.skb_mark = UINT32_MAX;
294 match->flow.skb_mark = skb_mark;
298 match_set_dl_type(struct match *match, ovs_be16 dl_type)
300 match->wc.masks.dl_type = htons(UINT16_MAX);
301 match->flow.dl_type = dl_type;
304 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
305 * exactly. 'mask_dst' is set to all 1s. */
307 set_eth(const uint8_t value_src[ETH_ADDR_LEN],
308 uint8_t value_dst[ETH_ADDR_LEN],
309 uint8_t mask_dst[ETH_ADDR_LEN])
311 memcpy(value_dst, value_src, ETH_ADDR_LEN);
312 memset(mask_dst, 0xff, ETH_ADDR_LEN);
315 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
316 * after each byte is ANDed with the appropriate byte in 'mask_src'.
317 * 'mask_dst' is set to 'mask_src' */
319 set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
320 const uint8_t mask_src[ETH_ADDR_LEN],
321 uint8_t value_dst[ETH_ADDR_LEN],
322 uint8_t mask_dst[ETH_ADDR_LEN])
326 for (i = 0; i < ETH_ADDR_LEN; i++) {
327 value_dst[i] = value_src[i] & mask_src[i];
328 mask_dst[i] = mask_src[i];
332 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
335 match_set_dl_src(struct match *match, const uint8_t dl_src[ETH_ADDR_LEN])
337 set_eth(dl_src, match->flow.dl_src, match->wc.masks.dl_src);
340 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
341 * after each byte is ANDed with the appropriate byte in 'mask'. */
343 match_set_dl_src_masked(struct match *match,
344 const uint8_t dl_src[ETH_ADDR_LEN],
345 const uint8_t mask[ETH_ADDR_LEN])
347 set_eth_masked(dl_src, mask, match->flow.dl_src, match->wc.masks.dl_src);
350 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
353 match_set_dl_dst(struct match *match, const uint8_t dl_dst[ETH_ADDR_LEN])
355 set_eth(dl_dst, match->flow.dl_dst, match->wc.masks.dl_dst);
358 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
359 * byte is ANDed with the appropriate byte in 'mask'.
361 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
362 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
364 match_set_dl_dst_masked(struct match *match,
365 const uint8_t dl_dst[ETH_ADDR_LEN],
366 const uint8_t mask[ETH_ADDR_LEN])
368 set_eth_masked(dl_dst, mask, match->flow.dl_dst, match->wc.masks.dl_dst);
372 match_set_dl_tci(struct match *match, ovs_be16 tci)
374 match_set_dl_tci_masked(match, tci, htons(0xffff));
378 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
380 match->flow.vlan_tci = tci & mask;
381 match->wc.masks.vlan_tci = mask;
384 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
385 * wildcarded, then 'match' will match a packet regardless of whether it has an
386 * 802.1Q header or not. */
388 match_set_any_vid(struct match *match)
390 if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
391 match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
392 match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
394 match_set_dl_tci_masked(match, htons(0), htons(0));
398 /* Modifies 'match' depending on 'dl_vlan':
400 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
401 * without an 802.1Q header.
403 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
404 * VID equals the low 12 bits of 'dl_vlan'.
407 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
409 flow_set_dl_vlan(&match->flow, dl_vlan);
410 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
411 match->wc.masks.vlan_tci = htons(UINT16_MAX);
413 match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
417 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
418 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
421 match_set_vlan_vid(struct match *match, ovs_be16 vid)
423 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
427 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
428 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
429 * plus CFI), with the corresponding 'mask'. */
431 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
433 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
434 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
437 flow_set_vlan_vid(&match->flow, vid & mask);
438 match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
441 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
442 * wildcarded, then 'match' will match a packet regardless of whether it has an
443 * 802.1Q header or not. */
445 match_set_any_pcp(struct match *match)
447 if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
448 match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
449 match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
451 match_set_dl_tci_masked(match, htons(0), htons(0));
455 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
456 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
458 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
460 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
461 match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
465 match_set_tp_src(struct match *match, ovs_be16 tp_src)
467 match_set_tp_src_masked(match, tp_src, htons(UINT16_MAX));
471 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
473 match->flow.tp_src = port & mask;
474 match->wc.masks.tp_src = mask;
478 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
480 match_set_tp_dst_masked(match, tp_dst, htons(UINT16_MAX));
484 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
486 match->flow.tp_dst = port & mask;
487 match->wc.masks.tp_dst = mask;
491 match_set_nw_proto(struct match *match, uint8_t nw_proto)
493 match->flow.nw_proto = nw_proto;
494 match->wc.masks.nw_proto = UINT8_MAX;
498 match_set_nw_src(struct match *match, ovs_be32 nw_src)
500 match->flow.nw_src = nw_src;
501 match->wc.masks.nw_src = htonl(UINT32_MAX);
505 match_set_nw_src_masked(struct match *match,
506 ovs_be32 nw_src, ovs_be32 mask)
508 match->flow.nw_src = nw_src & mask;
509 match->wc.masks.nw_src = mask;
513 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
515 match->flow.nw_dst = nw_dst;
516 match->wc.masks.nw_dst = htonl(UINT32_MAX);
520 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
522 match->flow.nw_dst = ip & mask;
523 match->wc.masks.nw_dst = mask;
527 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
529 match->wc.masks.nw_tos |= IP_DSCP_MASK;
530 match->flow.nw_tos &= ~IP_DSCP_MASK;
531 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
535 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
537 match->wc.masks.nw_tos |= IP_ECN_MASK;
538 match->flow.nw_tos &= ~IP_ECN_MASK;
539 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
543 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
545 match->wc.masks.nw_ttl = UINT8_MAX;
546 match->flow.nw_ttl = nw_ttl;
550 match_set_nw_frag(struct match *match, uint8_t nw_frag)
552 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
553 match->flow.nw_frag = nw_frag;
557 match_set_nw_frag_masked(struct match *match,
558 uint8_t nw_frag, uint8_t mask)
560 match->flow.nw_frag = nw_frag & mask;
561 match->wc.masks.nw_frag = mask;
565 match_set_icmp_type(struct match *match, uint8_t icmp_type)
567 match_set_tp_src(match, htons(icmp_type));
571 match_set_icmp_code(struct match *match, uint8_t icmp_code)
573 match_set_tp_dst(match, htons(icmp_code));
577 match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN])
579 memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN);
580 memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN);
584 match_set_arp_sha_masked(struct match *match,
585 const uint8_t arp_sha[ETH_ADDR_LEN],
586 const uint8_t mask[ETH_ADDR_LEN])
588 set_eth_masked(arp_sha, mask,
589 match->flow.arp_sha, match->wc.masks.arp_sha);
593 match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN])
595 memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN);
596 memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN);
600 match_set_arp_tha_masked(struct match *match,
601 const uint8_t arp_tha[ETH_ADDR_LEN],
602 const uint8_t mask[ETH_ADDR_LEN])
604 set_eth_masked(arp_tha, mask,
605 match->flow.arp_tha, match->wc.masks.arp_tha);
609 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
611 match->flow.ipv6_src = *src;
612 match->wc.masks.ipv6_src = in6addr_exact;
616 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
617 const struct in6_addr *mask)
619 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
620 match->wc.masks.ipv6_src = *mask;
624 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
626 match->flow.ipv6_dst = *dst;
627 match->wc.masks.ipv6_dst = in6addr_exact;
631 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
632 const struct in6_addr *mask)
634 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
635 match->wc.masks.ipv6_dst = *mask;
639 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
641 match->wc.masks.ipv6_label = htonl(UINT32_MAX);
642 match->flow.ipv6_label = ipv6_label;
647 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
650 match->flow.ipv6_label = ipv6_label & mask;
651 match->wc.masks.ipv6_label = mask;
655 match_set_nd_target(struct match *match, const struct in6_addr *target)
657 match->flow.nd_target = *target;
658 match->wc.masks.nd_target = in6addr_exact;
662 match_set_nd_target_masked(struct match *match,
663 const struct in6_addr *target,
664 const struct in6_addr *mask)
666 match->flow.nd_target = ipv6_addr_bitand(target, mask);
667 match->wc.masks.nd_target = *mask;
670 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
671 * values for fixed fields, otherwise false. */
673 match_equal(const struct match *a, const struct match *b)
675 return (flow_wildcards_equal(&a->wc, &b->wc)
676 && flow_equal(&a->flow, &b->flow));
679 /* Returns a hash value for the flow and wildcards in 'match', starting from
682 match_hash(const struct match *match, uint32_t basis)
684 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
688 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
689 const uint8_t mask[6])
691 if (!eth_addr_is_zero(mask)) {
692 ds_put_format(s, "%s=", name);
693 eth_format_masked(eth, mask, s);
699 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
703 ds_put_format(s, "%s=", name);
704 ip_format_masked(ip, netmask, s);
710 format_ipv6_netmask(struct ds *s, const char *name,
711 const struct in6_addr *addr,
712 const struct in6_addr *netmask)
714 if (!ipv6_mask_is_any(netmask)) {
715 ds_put_format(s, "%s=", name);
716 print_ipv6_masked(s, addr, netmask);
723 format_be16_masked(struct ds *s, const char *name,
724 ovs_be16 value, ovs_be16 mask)
726 if (mask != htons(0)) {
727 ds_put_format(s, "%s=", name);
728 if (mask == htons(UINT16_MAX)) {
729 ds_put_format(s, "%"PRIu16, ntohs(value));
731 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
732 ntohs(value), ntohs(mask));
739 format_flow_tunnel(struct ds *s, const struct match *match)
741 const struct flow_wildcards *wc = &match->wc;
742 const struct flow_tnl *tnl = &match->flow.tunnel;
744 switch (wc->masks.tunnel.tun_id) {
747 case CONSTANT_HTONLL(UINT64_MAX):
748 ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(tnl->tun_id));
751 ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
753 ntohll(wc->masks.tunnel.tun_id));
756 format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
757 format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
759 if (wc->masks.tunnel.ip_tos) {
760 ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
762 if (wc->masks.tunnel.ip_ttl) {
763 ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
765 if (wc->masks.tunnel.flags) {
766 format_flags(s, flow_tun_flag_to_string, tnl->flags, '|');
771 /* Appends a string representation of 'match' to 's'. If 'priority' is
772 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
774 match_format(const struct match *match, struct ds *s, unsigned int priority)
776 const struct flow_wildcards *wc = &match->wc;
777 size_t start_len = s->length;
778 const struct flow *f = &match->flow;
779 bool skip_type = false;
780 bool skip_proto = false;
784 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 18);
786 if (priority != OFP_DEFAULT_PRIORITY) {
787 ds_put_format(s, "priority=%u,", priority);
790 if (wc->masks.skb_mark) {
791 ds_put_format(s, "skb_mark=%#"PRIx32",", f->skb_mark);
794 if (wc->masks.skb_priority) {
795 ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority);
798 if (wc->masks.dl_type) {
800 if (f->dl_type == htons(ETH_TYPE_IP)) {
801 if (wc->masks.nw_proto) {
803 if (f->nw_proto == IPPROTO_ICMP) {
804 ds_put_cstr(s, "icmp,");
805 } else if (f->nw_proto == IPPROTO_TCP) {
806 ds_put_cstr(s, "tcp,");
807 } else if (f->nw_proto == IPPROTO_UDP) {
808 ds_put_cstr(s, "udp,");
810 ds_put_cstr(s, "ip,");
814 ds_put_cstr(s, "ip,");
816 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
817 if (wc->masks.nw_proto) {
819 if (f->nw_proto == IPPROTO_ICMPV6) {
820 ds_put_cstr(s, "icmp6,");
821 } else if (f->nw_proto == IPPROTO_TCP) {
822 ds_put_cstr(s, "tcp6,");
823 } else if (f->nw_proto == IPPROTO_UDP) {
824 ds_put_cstr(s, "udp6,");
826 ds_put_cstr(s, "ipv6,");
830 ds_put_cstr(s, "ipv6,");
832 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
833 ds_put_cstr(s, "arp,");
834 } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
835 ds_put_cstr(s, "rarp,");
840 for (i = 0; i < FLOW_N_REGS; i++) {
841 switch (wc->masks.regs[i]) {
845 ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
848 ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
849 i, f->regs[i], wc->masks.regs[i]);
854 format_flow_tunnel(s, match);
856 switch (wc->masks.metadata) {
859 case CONSTANT_HTONLL(UINT64_MAX):
860 ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata));
863 ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",",
864 ntohll(f->metadata), ntohll(wc->masks.metadata));
867 if (wc->masks.in_port) {
868 ds_put_cstr(s, "in_port=");
869 ofputil_format_port(f->in_port, s);
872 if (wc->masks.vlan_tci) {
873 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
874 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
875 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
877 if (cfi && f->vlan_tci & htons(VLAN_CFI)
878 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
879 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
880 && (vid_mask || pcp_mask)) {
882 ds_put_format(s, "dl_vlan=%"PRIu16",",
883 vlan_tci_to_vid(f->vlan_tci));
886 ds_put_format(s, "dl_vlan_pcp=%d,",
887 vlan_tci_to_pcp(f->vlan_tci));
889 } else if (wc->masks.vlan_tci == htons(0xffff)) {
890 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
892 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
893 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
896 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
897 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
898 if (!skip_type && wc->masks.dl_type) {
899 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
901 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
902 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
903 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
904 if (wc->masks.ipv6_label) {
905 if (wc->masks.ipv6_label == htonl(UINT32_MAX)) {
906 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
907 ntohl(f->ipv6_label));
909 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
910 ntohl(f->ipv6_label),
911 ntohl(wc->masks.ipv6_label));
914 } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
915 f->dl_type == htons(ETH_TYPE_RARP)) {
916 format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
917 format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
919 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
920 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
922 if (!skip_proto && wc->masks.nw_proto) {
923 if (f->dl_type == htons(ETH_TYPE_ARP) ||
924 f->dl_type == htons(ETH_TYPE_RARP)) {
925 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
927 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
930 if (f->dl_type == htons(ETH_TYPE_ARP) ||
931 f->dl_type == htons(ETH_TYPE_RARP)) {
932 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
933 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
935 if (wc->masks.nw_tos & IP_DSCP_MASK) {
936 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
938 if (wc->masks.nw_tos & IP_ECN_MASK) {
939 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
941 if (wc->masks.nw_ttl) {
942 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
944 switch (wc->masks.nw_frag) {
945 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
946 ds_put_format(s, "nw_frag=%s,",
947 f->nw_frag & FLOW_NW_FRAG_ANY
948 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
949 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
952 case FLOW_NW_FRAG_ANY:
953 ds_put_format(s, "nw_frag=%s,",
954 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
957 case FLOW_NW_FRAG_LATER:
958 ds_put_format(s, "nw_frag=%s,",
959 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
962 if (f->dl_type == htons(ETH_TYPE_IP) &&
963 f->nw_proto == IPPROTO_ICMP) {
964 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
965 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
966 } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
967 f->nw_proto == IPPROTO_ICMPV6) {
968 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
969 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
970 format_ipv6_netmask(s, "nd_target", &f->nd_target,
971 &wc->masks.nd_target);
972 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
973 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
975 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
976 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
979 if (s->length > start_len && ds_last(s) == ',') {
984 /* Converts 'match' to a string and returns the string. If 'priority' is
985 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
986 * must free the string (with free()). */
988 match_to_string(const struct match *match, unsigned int priority)
990 struct ds s = DS_EMPTY_INITIALIZER;
991 match_format(match, &s, priority);
992 return ds_steal_cstr(&s);
996 match_print(const struct match *match)
998 char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
1003 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1004 * with minimatch_destroy(). */
1006 minimatch_init(struct minimatch *dst, const struct match *src)
1008 miniflow_init(&dst->flow, &src->flow);
1009 minimask_init(&dst->mask, &src->wc);
1012 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1013 * with minimatch_destroy(). */
1015 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1017 miniflow_clone(&dst->flow, &src->flow);
1018 minimask_clone(&dst->mask, &src->mask);
1021 /* Frees any memory owned by 'match'. Does not free the storage in which
1022 * 'match' itself resides; the caller is responsible for that. */
1024 minimatch_destroy(struct minimatch *match)
1026 miniflow_destroy(&match->flow);
1027 minimask_destroy(&match->mask);
1030 /* Initializes 'dst' as a copy of 'src'. */
1032 minimatch_expand(const struct minimatch *src, struct match *dst)
1034 miniflow_expand(&src->flow, &dst->flow);
1035 minimask_expand(&src->mask, &dst->wc);
1038 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
1040 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1042 return (miniflow_equal(&a->flow, &b->flow)
1043 && minimask_equal(&a->mask, &b->mask));
1046 /* Returns a hash value for 'match', given 'basis'. */
1048 minimatch_hash(const struct minimatch *match, uint32_t basis)
1050 return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
1053 /* Appends a string representation of 'match' to 's'. If 'priority' is
1054 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1056 minimatch_format(const struct minimatch *match, struct ds *s,
1057 unsigned int priority)
1059 struct match megamatch;
1061 minimatch_expand(match, &megamatch);
1062 match_format(&megamatch, s, priority);
1065 /* Converts 'match' to a string and returns the string. If 'priority' is
1066 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1067 * must free the string (with free()). */
1069 minimatch_to_string(const struct minimatch *match, unsigned int priority)
1071 struct match megamatch;
1073 minimatch_expand(match, &megamatch);
1074 return match_to_string(&megamatch, priority);