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.
21 #include "byte-order.h"
22 #include "dynamic-string.h"
27 VLOG_DEFINE_THIS_MODULE(match);
30 /* Converts the flow in 'flow' into a match in 'match', with the given
33 match_init(struct match *match,
34 const struct flow *flow, const struct flow_wildcards *wc)
38 match_zero_wildcarded_fields(match);
41 /* Converts a flow into a match. It sets the wildcard masks based on
42 * the packet contents. It will not set the mask for fields that do not
43 * make sense for the packet type. */
45 match_wc_init(struct match *match, const struct flow *flow)
47 struct flow_wildcards *wc;
52 memset(&wc->masks, 0x0, sizeof wc->masks);
54 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
57 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
60 if (flow->skb_priority) {
61 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
65 memset(&wc->masks.skb_mark, 0xff, sizeof wc->masks.skb_mark);
68 for (i = 0; i < FLOW_N_REGS; i++) {
70 memset(&wc->masks.regs[i], 0xff, sizeof wc->masks.regs[i]);
74 if (flow->tunnel.ip_dst) {
75 if (flow->tunnel.flags & FLOW_TNL_F_KEY) {
76 memset(&wc->masks.tunnel.tun_id, 0xff, sizeof wc->masks.tunnel.tun_id);
78 memset(&wc->masks.tunnel.ip_src, 0xff, sizeof wc->masks.tunnel.ip_src);
79 memset(&wc->masks.tunnel.ip_dst, 0xff, sizeof wc->masks.tunnel.ip_dst);
80 memset(&wc->masks.tunnel.flags, 0xff, sizeof wc->masks.tunnel.flags);
81 memset(&wc->masks.tunnel.ip_tos, 0xff, sizeof wc->masks.tunnel.ip_tos);
82 memset(&wc->masks.tunnel.ip_ttl, 0xff, sizeof wc->masks.tunnel.ip_ttl);
83 } else if (flow->tunnel.tun_id) {
84 memset(&wc->masks.tunnel.tun_id, 0xff, sizeof wc->masks.tunnel.tun_id);
87 memset(&wc->masks.metadata, 0xff, sizeof wc->masks.metadata);
88 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
89 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
90 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
91 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
93 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
94 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
95 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
96 memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
97 } else if (flow->dl_type == htons(ETH_TYPE_IP) ||
98 (flow->dl_type == htons(ETH_TYPE_ARP)) ||
99 (flow->dl_type == htons(ETH_TYPE_RARP))) {
100 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
101 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
104 if (flow->dl_type == htons(ETH_TYPE_ARP) ||
105 flow->dl_type == htons(ETH_TYPE_RARP)) {
106 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
107 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
110 if (flow->dl_type == htons(ETH_TYPE_IPV6) ||
111 flow->dl_type == htons(ETH_TYPE_IP)) {
112 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
113 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
117 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
120 if (flow->nw_proto == IPPROTO_ICMP || 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);
134 /* Converts the flow in 'flow' into an exact-match match in 'match'. */
136 match_init_exact(struct match *match, const struct flow *flow)
138 ovs_be64 tun_id = flow->tunnel.tun_id;
141 match->flow.skb_priority = 0;
142 match->flow.skb_mark = 0;
143 memset(&match->flow.tunnel, 0, sizeof match->flow.tunnel);
144 match->flow.tunnel.tun_id = tun_id;
145 flow_wildcards_init_exact(&match->wc);
148 /* Initializes 'match' as a "catch-all" match that matches every packet. */
150 match_init_catchall(struct match *match)
152 memset(&match->flow, 0, sizeof match->flow);
153 flow_wildcards_init_catchall(&match->wc);
156 /* For each bit or field wildcarded in 'match', sets the corresponding bit or
157 * field in 'flow' to all-0-bits. It is important to maintain this invariant
158 * in a match that might be inserted into a classifier.
160 * It is never necessary to call this function directly for a match that is
161 * initialized or modified only by match_*() functions. It is useful to
162 * restore the invariant in a match whose 'wc' member is modified by hand.
165 match_zero_wildcarded_fields(struct match *match)
167 flow_zero_wildcards(&match->flow, &match->wc);
171 match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
173 match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
177 match_set_reg_masked(struct match *match, unsigned int reg_idx,
178 uint32_t value, uint32_t mask)
180 assert(reg_idx < FLOW_N_REGS);
181 flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
182 match->flow.regs[reg_idx] = value & mask;
186 match_set_metadata(struct match *match, ovs_be64 metadata)
188 match_set_metadata_masked(match, metadata, htonll(UINT64_MAX));
192 match_set_metadata_masked(struct match *match,
193 ovs_be64 metadata, ovs_be64 mask)
195 match->wc.masks.metadata = mask;
196 match->flow.metadata = metadata & mask;
200 match_set_tun_id(struct match *match, ovs_be64 tun_id)
202 match_set_tun_id_masked(match, tun_id, htonll(UINT64_MAX));
206 match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
208 match->wc.masks.tunnel.tun_id = mask;
209 match->flow.tunnel.tun_id = tun_id & mask;
213 match_set_tun_src(struct match *match, ovs_be32 src)
215 match_set_tun_src_masked(match, src, htonl(UINT32_MAX));
219 match_set_tun_src_masked(struct match *match, ovs_be32 src, ovs_be32 mask)
221 match->wc.masks.tunnel.ip_src = mask;
222 match->flow.tunnel.ip_src = src & mask;
226 match_set_tun_dst(struct match *match, ovs_be32 dst)
228 match_set_tun_dst_masked(match, dst, htonl(UINT32_MAX));
232 match_set_tun_dst_masked(struct match *match, ovs_be32 dst, ovs_be32 mask)
234 match->wc.masks.tunnel.ip_dst = mask;
235 match->flow.tunnel.ip_dst = dst & mask;
239 match_set_tun_ttl(struct match *match, uint8_t ttl)
241 match_set_tun_ttl_masked(match, ttl, UINT8_MAX);
245 match_set_tun_ttl_masked(struct match *match, uint8_t ttl, uint8_t mask)
247 match->wc.masks.tunnel.ip_ttl = mask;
248 match->flow.tunnel.ip_ttl = ttl & mask;
252 match_set_tun_tos(struct match *match, uint8_t tos)
254 match_set_tun_tos_masked(match, tos, UINT8_MAX);
258 match_set_tun_tos_masked(struct match *match, uint8_t tos, uint8_t mask)
260 match->wc.masks.tunnel.ip_tos = mask;
261 match->flow.tunnel.ip_tos = tos & mask;
265 match_set_tun_flags(struct match *match, uint16_t flags)
267 match_set_tun_flags_masked(match, flags, UINT16_MAX);
271 match_set_tun_flags_masked(struct match *match, uint16_t flags, uint16_t mask)
273 match->wc.masks.tunnel.flags = mask;
274 match->flow.tunnel.flags = flags & mask;
278 match_set_in_port(struct match *match, uint16_t ofp_port)
280 match->wc.masks.in_port = UINT16_MAX;
281 match->flow.in_port = ofp_port;
285 match_set_skb_priority(struct match *match, uint32_t skb_priority)
287 match->wc.masks.skb_priority = UINT32_MAX;
288 match->flow.skb_priority = skb_priority;
292 match_set_skb_mark(struct match *match, uint32_t skb_mark)
294 match->wc.masks.skb_mark = UINT32_MAX;
295 match->flow.skb_mark = skb_mark;
299 match_set_dl_type(struct match *match, ovs_be16 dl_type)
301 match->wc.masks.dl_type = htons(UINT16_MAX);
302 match->flow.dl_type = dl_type;
305 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
306 * exactly. 'mask_dst' is set to all 1s. */
308 set_eth(const uint8_t value_src[ETH_ADDR_LEN],
309 uint8_t value_dst[ETH_ADDR_LEN],
310 uint8_t mask_dst[ETH_ADDR_LEN])
312 memcpy(value_dst, value_src, ETH_ADDR_LEN);
313 memset(mask_dst, 0xff, ETH_ADDR_LEN);
316 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
317 * after each byte is ANDed with the appropriate byte in 'mask_src'.
318 * 'mask_dst' is set to 'mask_src' */
320 set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
321 const uint8_t mask_src[ETH_ADDR_LEN],
322 uint8_t value_dst[ETH_ADDR_LEN],
323 uint8_t mask_dst[ETH_ADDR_LEN])
327 for (i = 0; i < ETH_ADDR_LEN; i++) {
328 value_dst[i] = value_src[i] & mask_src[i];
329 mask_dst[i] = mask_src[i];
333 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
336 match_set_dl_src(struct match *match, const uint8_t dl_src[ETH_ADDR_LEN])
338 set_eth(dl_src, match->flow.dl_src, match->wc.masks.dl_src);
341 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
342 * after each byte is ANDed with the appropriate byte in 'mask'. */
344 match_set_dl_src_masked(struct match *match,
345 const uint8_t dl_src[ETH_ADDR_LEN],
346 const uint8_t mask[ETH_ADDR_LEN])
348 set_eth_masked(dl_src, mask, match->flow.dl_src, match->wc.masks.dl_src);
351 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
354 match_set_dl_dst(struct match *match, const uint8_t dl_dst[ETH_ADDR_LEN])
356 set_eth(dl_dst, match->flow.dl_dst, match->wc.masks.dl_dst);
359 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
360 * byte is ANDed with the appropriate byte in 'mask'.
362 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
363 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
365 match_set_dl_dst_masked(struct match *match,
366 const uint8_t dl_dst[ETH_ADDR_LEN],
367 const uint8_t mask[ETH_ADDR_LEN])
369 set_eth_masked(dl_dst, mask, match->flow.dl_dst, match->wc.masks.dl_dst);
373 match_set_dl_tci(struct match *match, ovs_be16 tci)
375 match_set_dl_tci_masked(match, tci, htons(0xffff));
379 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
381 match->flow.vlan_tci = tci & mask;
382 match->wc.masks.vlan_tci = mask;
385 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
386 * wildcarded, then 'match' will match a packet regardless of whether it has an
387 * 802.1Q header or not. */
389 match_set_any_vid(struct match *match)
391 if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
392 match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
393 match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
395 match_set_dl_tci_masked(match, htons(0), htons(0));
399 /* Modifies 'match' depending on 'dl_vlan':
401 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
402 * without an 802.1Q header.
404 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
405 * VID equals the low 12 bits of 'dl_vlan'.
408 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
410 flow_set_dl_vlan(&match->flow, dl_vlan);
411 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
412 match->wc.masks.vlan_tci = htons(UINT16_MAX);
414 match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
418 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
419 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
422 match_set_vlan_vid(struct match *match, ovs_be16 vid)
424 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
428 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
429 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
430 * plus CFI), with the corresponding 'mask'. */
432 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
434 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
435 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
438 flow_set_vlan_vid(&match->flow, vid & mask);
439 match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
442 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
443 * wildcarded, then 'match' will match a packet regardless of whether it has an
444 * 802.1Q header or not. */
446 match_set_any_pcp(struct match *match)
448 if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
449 match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
450 match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
452 match_set_dl_tci_masked(match, htons(0), htons(0));
456 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
457 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
459 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
461 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
462 match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
466 match_set_tp_src(struct match *match, ovs_be16 tp_src)
468 match_set_tp_src_masked(match, tp_src, htons(UINT16_MAX));
472 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
474 match->flow.tp_src = port & mask;
475 match->wc.masks.tp_src = mask;
479 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
481 match_set_tp_dst_masked(match, tp_dst, htons(UINT16_MAX));
485 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
487 match->flow.tp_dst = port & mask;
488 match->wc.masks.tp_dst = mask;
492 match_set_nw_proto(struct match *match, uint8_t nw_proto)
494 match->flow.nw_proto = nw_proto;
495 match->wc.masks.nw_proto = UINT8_MAX;
499 match_set_nw_src(struct match *match, ovs_be32 nw_src)
501 match->flow.nw_src = nw_src;
502 match->wc.masks.nw_src = htonl(UINT32_MAX);
506 match_set_nw_src_masked(struct match *match,
507 ovs_be32 nw_src, ovs_be32 mask)
509 match->flow.nw_src = nw_src & mask;
510 match->wc.masks.nw_src = mask;
514 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
516 match->flow.nw_dst = nw_dst;
517 match->wc.masks.nw_dst = htonl(UINT32_MAX);
521 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
523 match->flow.nw_dst = ip & mask;
524 match->wc.masks.nw_dst = mask;
528 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
530 match->wc.masks.nw_tos |= IP_DSCP_MASK;
531 match->flow.nw_tos &= ~IP_DSCP_MASK;
532 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
536 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
538 match->wc.masks.nw_tos |= IP_ECN_MASK;
539 match->flow.nw_tos &= ~IP_ECN_MASK;
540 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
544 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
546 match->wc.masks.nw_ttl = UINT8_MAX;
547 match->flow.nw_ttl = nw_ttl;
551 match_set_nw_frag(struct match *match, uint8_t nw_frag)
553 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
554 match->flow.nw_frag = nw_frag;
558 match_set_nw_frag_masked(struct match *match,
559 uint8_t nw_frag, uint8_t mask)
561 match->flow.nw_frag = nw_frag & mask;
562 match->wc.masks.nw_frag = mask;
566 match_set_icmp_type(struct match *match, uint8_t icmp_type)
568 match_set_tp_src(match, htons(icmp_type));
572 match_set_icmp_code(struct match *match, uint8_t icmp_code)
574 match_set_tp_dst(match, htons(icmp_code));
578 match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN])
580 memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN);
581 memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN);
585 match_set_arp_sha_masked(struct match *match,
586 const uint8_t arp_sha[ETH_ADDR_LEN],
587 const uint8_t mask[ETH_ADDR_LEN])
589 set_eth_masked(arp_sha, mask,
590 match->flow.arp_sha, match->wc.masks.arp_sha);
594 match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN])
596 memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN);
597 memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN);
601 match_set_arp_tha_masked(struct match *match,
602 const uint8_t arp_tha[ETH_ADDR_LEN],
603 const uint8_t mask[ETH_ADDR_LEN])
605 set_eth_masked(arp_tha, mask,
606 match->flow.arp_tha, match->wc.masks.arp_tha);
610 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
612 match->flow.ipv6_src = *src;
613 match->wc.masks.ipv6_src = in6addr_exact;
617 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
618 const struct in6_addr *mask)
620 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
621 match->wc.masks.ipv6_src = *mask;
625 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
627 match->flow.ipv6_dst = *dst;
628 match->wc.masks.ipv6_dst = in6addr_exact;
632 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
633 const struct in6_addr *mask)
635 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
636 match->wc.masks.ipv6_dst = *mask;
640 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
642 match->wc.masks.ipv6_label = htonl(UINT32_MAX);
643 match->flow.ipv6_label = ipv6_label;
648 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
651 match->flow.ipv6_label = ipv6_label & mask;
652 match->wc.masks.ipv6_label = mask;
656 match_set_nd_target(struct match *match, const struct in6_addr *target)
658 match->flow.nd_target = *target;
659 match->wc.masks.nd_target = in6addr_exact;
663 match_set_nd_target_masked(struct match *match,
664 const struct in6_addr *target,
665 const struct in6_addr *mask)
667 match->flow.nd_target = ipv6_addr_bitand(target, mask);
668 match->wc.masks.nd_target = *mask;
671 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
672 * values for fixed fields, otherwise false. */
674 match_equal(const struct match *a, const struct match *b)
676 return (flow_wildcards_equal(&a->wc, &b->wc)
677 && flow_equal(&a->flow, &b->flow));
680 /* Returns a hash value for the flow and wildcards in 'match', starting from
683 match_hash(const struct match *match, uint32_t basis)
685 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
689 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
690 const uint8_t mask[6])
692 if (!eth_addr_is_zero(mask)) {
693 ds_put_format(s, "%s=", name);
694 eth_format_masked(eth, mask, s);
700 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
704 ds_put_format(s, "%s=", name);
705 ip_format_masked(ip, netmask, s);
711 format_ipv6_netmask(struct ds *s, const char *name,
712 const struct in6_addr *addr,
713 const struct in6_addr *netmask)
715 if (!ipv6_mask_is_any(netmask)) {
716 ds_put_format(s, "%s=", name);
717 print_ipv6_masked(s, addr, netmask);
724 format_be16_masked(struct ds *s, const char *name,
725 ovs_be16 value, ovs_be16 mask)
727 if (mask != htons(0)) {
728 ds_put_format(s, "%s=", name);
729 if (mask == htons(UINT16_MAX)) {
730 ds_put_format(s, "%"PRIu16, ntohs(value));
732 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
733 ntohs(value), ntohs(mask));
740 format_flow_tunnel(struct ds *s, const struct match *match)
742 const struct flow_wildcards *wc = &match->wc;
743 const struct flow_tnl *tnl = &match->flow.tunnel;
745 switch (wc->masks.tunnel.tun_id) {
748 case CONSTANT_HTONLL(UINT64_MAX):
749 ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(tnl->tun_id));
752 ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
754 ntohll(wc->masks.tunnel.tun_id));
757 format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
758 format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
760 if (wc->masks.tunnel.ip_tos) {
761 ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
763 if (wc->masks.tunnel.ip_ttl) {
764 ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
766 if (wc->masks.tunnel.flags) {
767 format_flags(s, flow_tun_flag_to_string, tnl->flags, '|');
772 /* Appends a string representation of 'match' to 's'. If 'priority' is
773 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
775 match_format(const struct match *match, struct ds *s, unsigned int priority)
777 const struct flow_wildcards *wc = &match->wc;
778 size_t start_len = s->length;
779 const struct flow *f = &match->flow;
780 bool skip_type = false;
781 bool skip_proto = false;
785 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 18);
787 if (priority != OFP_DEFAULT_PRIORITY) {
788 ds_put_format(s, "priority=%u,", priority);
791 if (wc->masks.skb_mark) {
792 ds_put_format(s, "skb_mark=%#"PRIx32",", f->skb_mark);
795 if (wc->masks.skb_priority) {
796 ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority);
799 if (wc->masks.dl_type) {
801 if (f->dl_type == htons(ETH_TYPE_IP)) {
802 if (wc->masks.nw_proto) {
804 if (f->nw_proto == IPPROTO_ICMP) {
805 ds_put_cstr(s, "icmp,");
806 } else if (f->nw_proto == IPPROTO_TCP) {
807 ds_put_cstr(s, "tcp,");
808 } else if (f->nw_proto == IPPROTO_UDP) {
809 ds_put_cstr(s, "udp,");
811 ds_put_cstr(s, "ip,");
815 ds_put_cstr(s, "ip,");
817 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
818 if (wc->masks.nw_proto) {
820 if (f->nw_proto == IPPROTO_ICMPV6) {
821 ds_put_cstr(s, "icmp6,");
822 } else if (f->nw_proto == IPPROTO_TCP) {
823 ds_put_cstr(s, "tcp6,");
824 } else if (f->nw_proto == IPPROTO_UDP) {
825 ds_put_cstr(s, "udp6,");
827 ds_put_cstr(s, "ipv6,");
831 ds_put_cstr(s, "ipv6,");
833 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
834 ds_put_cstr(s, "arp,");
835 } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
836 ds_put_cstr(s, "rarp,");
841 for (i = 0; i < FLOW_N_REGS; i++) {
842 switch (wc->masks.regs[i]) {
846 ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
849 ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
850 i, f->regs[i], wc->masks.regs[i]);
855 format_flow_tunnel(s, match);
857 switch (wc->masks.metadata) {
860 case CONSTANT_HTONLL(UINT64_MAX):
861 ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata));
864 ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",",
865 ntohll(f->metadata), ntohll(wc->masks.metadata));
868 if (wc->masks.in_port) {
869 ds_put_cstr(s, "in_port=");
870 ofputil_format_port(f->in_port, s);
873 if (wc->masks.vlan_tci) {
874 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
875 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
876 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
878 if (cfi && f->vlan_tci & htons(VLAN_CFI)
879 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
880 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
881 && (vid_mask || pcp_mask)) {
883 ds_put_format(s, "dl_vlan=%"PRIu16",",
884 vlan_tci_to_vid(f->vlan_tci));
887 ds_put_format(s, "dl_vlan_pcp=%d,",
888 vlan_tci_to_pcp(f->vlan_tci));
890 } else if (wc->masks.vlan_tci == htons(0xffff)) {
891 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
893 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
894 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
897 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
898 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
899 if (!skip_type && wc->masks.dl_type) {
900 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
902 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
903 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
904 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
905 if (wc->masks.ipv6_label) {
906 if (wc->masks.ipv6_label == htonl(UINT32_MAX)) {
907 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
908 ntohl(f->ipv6_label));
910 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
911 ntohl(f->ipv6_label),
912 ntohl(wc->masks.ipv6_label));
915 } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
916 f->dl_type == htons(ETH_TYPE_RARP)) {
917 format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
918 format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
920 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
921 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
923 if (!skip_proto && wc->masks.nw_proto) {
924 if (f->dl_type == htons(ETH_TYPE_ARP) ||
925 f->dl_type == htons(ETH_TYPE_RARP)) {
926 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
928 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
931 if (f->dl_type == htons(ETH_TYPE_ARP) ||
932 f->dl_type == htons(ETH_TYPE_RARP)) {
933 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
934 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
936 if (wc->masks.nw_tos & IP_DSCP_MASK) {
937 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
939 if (wc->masks.nw_tos & IP_ECN_MASK) {
940 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
942 if (wc->masks.nw_ttl) {
943 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
945 switch (wc->masks.nw_frag) {
946 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
947 ds_put_format(s, "nw_frag=%s,",
948 f->nw_frag & FLOW_NW_FRAG_ANY
949 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
950 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
953 case FLOW_NW_FRAG_ANY:
954 ds_put_format(s, "nw_frag=%s,",
955 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
958 case FLOW_NW_FRAG_LATER:
959 ds_put_format(s, "nw_frag=%s,",
960 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
963 if (f->dl_type == htons(ETH_TYPE_IP) &&
964 f->nw_proto == IPPROTO_ICMP) {
965 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
966 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
967 } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
968 f->nw_proto == IPPROTO_ICMPV6) {
969 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
970 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
971 format_ipv6_netmask(s, "nd_target", &f->nd_target,
972 &wc->masks.nd_target);
973 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
974 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
976 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
977 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
980 if (s->length > start_len && ds_last(s) == ',') {
985 /* Converts 'match' to a string and returns the string. If 'priority' is
986 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
987 * must free the string (with free()). */
989 match_to_string(const struct match *match, unsigned int priority)
991 struct ds s = DS_EMPTY_INITIALIZER;
992 match_format(match, &s, priority);
993 return ds_steal_cstr(&s);
997 match_print(const struct match *match)
999 char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
1004 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1005 * with minimatch_destroy(). */
1007 minimatch_init(struct minimatch *dst, const struct match *src)
1009 miniflow_init(&dst->flow, &src->flow);
1010 minimask_init(&dst->mask, &src->wc);
1013 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1014 * with minimatch_destroy(). */
1016 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1018 miniflow_clone(&dst->flow, &src->flow);
1019 minimask_clone(&dst->mask, &src->mask);
1022 /* Frees any memory owned by 'match'. Does not free the storage in which
1023 * 'match' itself resides; the caller is responsible for that. */
1025 minimatch_destroy(struct minimatch *match)
1027 miniflow_destroy(&match->flow);
1028 minimask_destroy(&match->mask);
1031 /* Initializes 'dst' as a copy of 'src'. */
1033 minimatch_expand(const struct minimatch *src, struct match *dst)
1035 miniflow_expand(&src->flow, &dst->flow);
1036 minimask_expand(&src->mask, &dst->wc);
1039 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
1041 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1043 return (miniflow_equal(&a->flow, &b->flow)
1044 && minimask_equal(&a->mask, &b->mask));
1047 /* Returns a hash value for 'match', given 'basis'. */
1049 minimatch_hash(const struct minimatch *match, uint32_t basis)
1051 return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
1054 /* Appends a string representation of 'match' to 's'. If 'priority' is
1055 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1057 minimatch_format(const struct minimatch *match, struct ds *s,
1058 unsigned int priority)
1060 struct match megamatch;
1062 minimatch_expand(match, &megamatch);
1063 match_format(&megamatch, s, priority);
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 minimatch_to_string(const struct minimatch *match, unsigned int priority)
1072 struct match megamatch;
1074 minimatch_expand(match, &megamatch);
1075 return match_to_string(&megamatch, priority);