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"
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 for (i = 0; i < FLOW_N_REGS; i++) {
61 memset(&wc->masks.regs[i], 0xff, sizeof wc->masks.regs[i]);
65 if (flow->tunnel.ip_dst) {
66 if (flow->tunnel.flags & FLOW_TNL_F_KEY) {
67 memset(&wc->masks.tunnel.tun_id, 0xff, sizeof wc->masks.tunnel.tun_id);
69 memset(&wc->masks.tunnel.ip_src, 0xff, sizeof wc->masks.tunnel.ip_src);
70 memset(&wc->masks.tunnel.ip_dst, 0xff, sizeof wc->masks.tunnel.ip_dst);
71 memset(&wc->masks.tunnel.flags, 0xff, sizeof wc->masks.tunnel.flags);
72 memset(&wc->masks.tunnel.ip_tos, 0xff, sizeof wc->masks.tunnel.ip_tos);
73 memset(&wc->masks.tunnel.ip_ttl, 0xff, sizeof wc->masks.tunnel.ip_ttl);
74 } else if (flow->tunnel.tun_id) {
75 memset(&wc->masks.tunnel.tun_id, 0xff, sizeof wc->masks.tunnel.tun_id);
78 memset(&wc->masks.metadata, 0xff, sizeof wc->masks.metadata);
79 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
80 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
81 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
82 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
84 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
85 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
86 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
87 memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
88 } else if (flow->dl_type == htons(ETH_TYPE_IP) ||
89 (flow->dl_type == htons(ETH_TYPE_ARP)) ||
90 (flow->dl_type == htons(ETH_TYPE_RARP))) {
91 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
92 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
95 if (flow->dl_type == htons(ETH_TYPE_ARP) ||
96 flow->dl_type == htons(ETH_TYPE_RARP)) {
97 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
98 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
101 if (flow->dl_type == htons(ETH_TYPE_IPV6) ||
102 flow->dl_type == htons(ETH_TYPE_IP)) {
103 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
104 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
108 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
111 if (flow->nw_proto == IPPROTO_ICMP || flow->nw_proto == IPPROTO_ICMPV6 ||
112 (flow->tp_src || flow->tp_dst)) {
113 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
114 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
117 if (flow->nw_proto == IPPROTO_ICMPV6) {
118 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
119 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
125 /* Converts the flow in 'flow' into an exact-match match in 'match'. */
127 match_init_exact(struct match *match, const struct flow *flow)
129 ovs_be64 tun_id = flow->tunnel.tun_id;
132 match->flow.skb_priority = 0;
133 match->flow.skb_mark = 0;
134 memset(&match->flow.tunnel, 0, sizeof match->flow.tunnel);
135 match->flow.tunnel.tun_id = tun_id;
136 flow_wildcards_init_exact(&match->wc);
139 /* Initializes 'match' as a "catch-all" match that matches every packet. */
141 match_init_catchall(struct match *match)
143 memset(&match->flow, 0, sizeof match->flow);
144 flow_wildcards_init_catchall(&match->wc);
147 /* For each bit or field wildcarded in 'match', sets the corresponding bit or
148 * field in 'flow' to all-0-bits. It is important to maintain this invariant
149 * in a match that might be inserted into a classifier.
151 * It is never necessary to call this function directly for a match that is
152 * initialized or modified only by match_*() functions. It is useful to
153 * restore the invariant in a match whose 'wc' member is modified by hand.
156 match_zero_wildcarded_fields(struct match *match)
158 flow_zero_wildcards(&match->flow, &match->wc);
162 match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
164 match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
168 match_set_reg_masked(struct match *match, unsigned int reg_idx,
169 uint32_t value, uint32_t mask)
171 assert(reg_idx < FLOW_N_REGS);
172 flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
173 match->flow.regs[reg_idx] = value & mask;
177 match_set_metadata(struct match *match, ovs_be64 metadata)
179 match_set_metadata_masked(match, metadata, htonll(UINT64_MAX));
183 match_set_metadata_masked(struct match *match,
184 ovs_be64 metadata, ovs_be64 mask)
186 match->wc.masks.metadata = mask;
187 match->flow.metadata = metadata & mask;
191 match_set_tun_id(struct match *match, ovs_be64 tun_id)
193 match_set_tun_id_masked(match, tun_id, htonll(UINT64_MAX));
197 match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
199 match->wc.masks.tunnel.tun_id = mask;
200 match->flow.tunnel.tun_id = tun_id & mask;
204 match_set_tun_src(struct match *match, ovs_be32 src)
206 match_set_tun_src_masked(match, src, htonl(UINT32_MAX));
210 match_set_tun_src_masked(struct match *match, ovs_be32 src, ovs_be32 mask)
212 match->wc.masks.tunnel.ip_src = mask;
213 match->flow.tunnel.ip_src = src & mask;
217 match_set_tun_dst(struct match *match, ovs_be32 dst)
219 match_set_tun_dst_masked(match, dst, htonl(UINT32_MAX));
223 match_set_tun_dst_masked(struct match *match, ovs_be32 dst, ovs_be32 mask)
225 match->wc.masks.tunnel.ip_dst = mask;
226 match->flow.tunnel.ip_dst = dst & mask;
230 match_set_tun_ttl(struct match *match, uint8_t ttl)
232 match_set_tun_ttl_masked(match, ttl, UINT8_MAX);
236 match_set_tun_ttl_masked(struct match *match, uint8_t ttl, uint8_t mask)
238 match->wc.masks.tunnel.ip_ttl = mask;
239 match->flow.tunnel.ip_ttl = ttl & mask;
243 match_set_tun_tos(struct match *match, uint8_t tos)
245 match_set_tun_tos_masked(match, tos, UINT8_MAX);
249 match_set_tun_tos_masked(struct match *match, uint8_t tos, uint8_t mask)
251 match->wc.masks.tunnel.ip_tos = mask;
252 match->flow.tunnel.ip_tos = tos & mask;
256 match_set_tun_flags(struct match *match, uint16_t flags)
258 match_set_tun_flags_masked(match, flags, UINT16_MAX);
262 match_set_tun_flags_masked(struct match *match, uint16_t flags, uint16_t mask)
264 match->wc.masks.tunnel.flags = mask;
265 match->flow.tunnel.flags = flags & mask;
269 match_set_in_port(struct match *match, uint16_t ofp_port)
271 match->wc.masks.in_port = UINT16_MAX;
272 match->flow.in_port = ofp_port;
276 match_set_dl_type(struct match *match, ovs_be16 dl_type)
278 match->wc.masks.dl_type = htons(UINT16_MAX);
279 match->flow.dl_type = dl_type;
282 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
283 * exactly. 'mask_dst' is set to all 1s. */
285 set_eth(const uint8_t value_src[ETH_ADDR_LEN],
286 uint8_t value_dst[ETH_ADDR_LEN],
287 uint8_t mask_dst[ETH_ADDR_LEN])
289 memcpy(value_dst, value_src, ETH_ADDR_LEN);
290 memset(mask_dst, 0xff, ETH_ADDR_LEN);
293 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
294 * after each byte is ANDed with the appropriate byte in 'mask_src'.
295 * 'mask_dst' is set to 'mask_src' */
297 set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
298 const uint8_t mask_src[ETH_ADDR_LEN],
299 uint8_t value_dst[ETH_ADDR_LEN],
300 uint8_t mask_dst[ETH_ADDR_LEN])
304 for (i = 0; i < ETH_ADDR_LEN; i++) {
305 value_dst[i] = value_src[i] & mask_src[i];
306 mask_dst[i] = mask_src[i];
310 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
313 match_set_dl_src(struct match *match, const uint8_t dl_src[ETH_ADDR_LEN])
315 set_eth(dl_src, match->flow.dl_src, match->wc.masks.dl_src);
318 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
319 * after each byte is ANDed with the appropriate byte in 'mask'. */
321 match_set_dl_src_masked(struct match *match,
322 const uint8_t dl_src[ETH_ADDR_LEN],
323 const uint8_t mask[ETH_ADDR_LEN])
325 set_eth_masked(dl_src, mask, match->flow.dl_src, match->wc.masks.dl_src);
328 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
331 match_set_dl_dst(struct match *match, const uint8_t dl_dst[ETH_ADDR_LEN])
333 set_eth(dl_dst, match->flow.dl_dst, match->wc.masks.dl_dst);
336 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
337 * byte is ANDed with the appropriate byte in 'mask'.
339 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
340 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
342 match_set_dl_dst_masked(struct match *match,
343 const uint8_t dl_dst[ETH_ADDR_LEN],
344 const uint8_t mask[ETH_ADDR_LEN])
346 set_eth_masked(dl_dst, mask, match->flow.dl_dst, match->wc.masks.dl_dst);
350 match_set_dl_tci(struct match *match, ovs_be16 tci)
352 match_set_dl_tci_masked(match, tci, htons(0xffff));
356 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
358 match->flow.vlan_tci = tci & mask;
359 match->wc.masks.vlan_tci = mask;
362 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
363 * wildcarded, then 'match' will match a packet regardless of whether it has an
364 * 802.1Q header or not. */
366 match_set_any_vid(struct match *match)
368 if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
369 match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
370 match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
372 match_set_dl_tci_masked(match, htons(0), htons(0));
376 /* Modifies 'match' depending on 'dl_vlan':
378 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
379 * without an 802.1Q header.
381 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
382 * VID equals the low 12 bits of 'dl_vlan'.
385 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
387 flow_set_dl_vlan(&match->flow, dl_vlan);
388 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
389 match->wc.masks.vlan_tci = htons(UINT16_MAX);
391 match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
395 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
396 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
399 match_set_vlan_vid(struct match *match, ovs_be16 vid)
401 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
405 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
406 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
407 * plus CFI), with the corresponding 'mask'. */
409 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
411 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
412 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
415 flow_set_vlan_vid(&match->flow, vid & mask);
416 match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
419 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
420 * wildcarded, then 'match' will match a packet regardless of whether it has an
421 * 802.1Q header or not. */
423 match_set_any_pcp(struct match *match)
425 if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
426 match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
427 match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
429 match_set_dl_tci_masked(match, htons(0), htons(0));
433 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
434 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
436 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
438 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
439 match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
443 match_set_tp_src(struct match *match, ovs_be16 tp_src)
445 match_set_tp_src_masked(match, tp_src, htons(UINT16_MAX));
449 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
451 match->flow.tp_src = port & mask;
452 match->wc.masks.tp_src = mask;
456 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
458 match_set_tp_dst_masked(match, tp_dst, htons(UINT16_MAX));
462 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
464 match->flow.tp_dst = port & mask;
465 match->wc.masks.tp_dst = mask;
469 match_set_nw_proto(struct match *match, uint8_t nw_proto)
471 match->flow.nw_proto = nw_proto;
472 match->wc.masks.nw_proto = UINT8_MAX;
476 match_set_nw_src(struct match *match, ovs_be32 nw_src)
478 match->flow.nw_src = nw_src;
479 match->wc.masks.nw_src = htonl(UINT32_MAX);
483 match_set_nw_src_masked(struct match *match,
484 ovs_be32 nw_src, ovs_be32 mask)
486 match->flow.nw_src = nw_src & mask;
487 match->wc.masks.nw_src = mask;
491 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
493 match->flow.nw_dst = nw_dst;
494 match->wc.masks.nw_dst = htonl(UINT32_MAX);
498 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
500 match->flow.nw_dst = ip & mask;
501 match->wc.masks.nw_dst = mask;
505 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
507 match->wc.masks.nw_tos |= IP_DSCP_MASK;
508 match->flow.nw_tos &= ~IP_DSCP_MASK;
509 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
513 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
515 match->wc.masks.nw_tos |= IP_ECN_MASK;
516 match->flow.nw_tos &= ~IP_ECN_MASK;
517 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
521 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
523 match->wc.masks.nw_ttl = UINT8_MAX;
524 match->flow.nw_ttl = nw_ttl;
528 match_set_nw_frag(struct match *match, uint8_t nw_frag)
530 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
531 match->flow.nw_frag = nw_frag;
535 match_set_nw_frag_masked(struct match *match,
536 uint8_t nw_frag, uint8_t mask)
538 match->flow.nw_frag = nw_frag & mask;
539 match->wc.masks.nw_frag = mask;
543 match_set_icmp_type(struct match *match, uint8_t icmp_type)
545 match_set_tp_src(match, htons(icmp_type));
549 match_set_icmp_code(struct match *match, uint8_t icmp_code)
551 match_set_tp_dst(match, htons(icmp_code));
555 match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN])
557 memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN);
558 memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN);
562 match_set_arp_sha_masked(struct match *match,
563 const uint8_t arp_sha[ETH_ADDR_LEN],
564 const uint8_t mask[ETH_ADDR_LEN])
566 set_eth_masked(arp_sha, mask,
567 match->flow.arp_sha, match->wc.masks.arp_sha);
571 match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN])
573 memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN);
574 memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN);
578 match_set_arp_tha_masked(struct match *match,
579 const uint8_t arp_tha[ETH_ADDR_LEN],
580 const uint8_t mask[ETH_ADDR_LEN])
582 set_eth_masked(arp_tha, mask,
583 match->flow.arp_tha, match->wc.masks.arp_tha);
587 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
589 match->flow.ipv6_src = *src;
590 match->wc.masks.ipv6_src = in6addr_exact;
594 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
595 const struct in6_addr *mask)
597 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
598 match->wc.masks.ipv6_src = *mask;
602 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
604 match->flow.ipv6_dst = *dst;
605 match->wc.masks.ipv6_dst = in6addr_exact;
609 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
610 const struct in6_addr *mask)
612 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
613 match->wc.masks.ipv6_dst = *mask;
617 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
619 match->wc.masks.ipv6_label = htonl(UINT32_MAX);
620 match->flow.ipv6_label = ipv6_label;
625 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
628 match->flow.ipv6_label = ipv6_label & mask;
629 match->wc.masks.ipv6_label = mask;
633 match_set_nd_target(struct match *match, const struct in6_addr *target)
635 match->flow.nd_target = *target;
636 match->wc.masks.nd_target = in6addr_exact;
640 match_set_nd_target_masked(struct match *match,
641 const struct in6_addr *target,
642 const struct in6_addr *mask)
644 match->flow.nd_target = ipv6_addr_bitand(target, mask);
645 match->wc.masks.nd_target = *mask;
648 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
649 * values for fixed fields, otherwise false. */
651 match_equal(const struct match *a, const struct match *b)
653 return (flow_wildcards_equal(&a->wc, &b->wc)
654 && flow_equal(&a->flow, &b->flow));
657 /* Returns a hash value for the flow and wildcards in 'match', starting from
660 match_hash(const struct match *match, uint32_t basis)
662 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
666 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
667 const uint8_t mask[6])
669 if (!eth_addr_is_zero(mask)) {
670 ds_put_format(s, "%s=", name);
671 eth_format_masked(eth, mask, s);
677 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
681 ds_put_format(s, "%s=", name);
682 ip_format_masked(ip, netmask, s);
688 format_ipv6_netmask(struct ds *s, const char *name,
689 const struct in6_addr *addr,
690 const struct in6_addr *netmask)
692 if (!ipv6_mask_is_any(netmask)) {
693 ds_put_format(s, "%s=", name);
694 print_ipv6_masked(s, addr, netmask);
701 format_be16_masked(struct ds *s, const char *name,
702 ovs_be16 value, ovs_be16 mask)
704 if (mask != htons(0)) {
705 ds_put_format(s, "%s=", name);
706 if (mask == htons(UINT16_MAX)) {
707 ds_put_format(s, "%"PRIu16, ntohs(value));
709 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
710 ntohs(value), ntohs(mask));
717 format_flow_tunnel(struct ds *s, const struct match *match)
719 const struct flow_wildcards *wc = &match->wc;
720 const struct flow_tnl *tnl = &match->flow.tunnel;
722 switch (wc->masks.tunnel.tun_id) {
725 case CONSTANT_HTONLL(UINT64_MAX):
726 ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(tnl->tun_id));
729 ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
731 ntohll(wc->masks.tunnel.tun_id));
734 format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
735 format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
737 if (wc->masks.tunnel.ip_tos) {
738 ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
740 if (wc->masks.tunnel.ip_ttl) {
741 ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
743 if (wc->masks.tunnel.flags) {
744 format_flags(s, flow_tun_flag_to_string, tnl->flags, '|');
749 /* Appends a string representation of 'match' to 's'. If 'priority' is
750 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
752 match_format(const struct match *match, struct ds *s, unsigned int priority)
754 const struct flow_wildcards *wc = &match->wc;
755 size_t start_len = s->length;
756 const struct flow *f = &match->flow;
757 bool skip_type = false;
758 bool skip_proto = false;
762 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 18);
764 if (priority != OFP_DEFAULT_PRIORITY) {
765 ds_put_format(s, "priority=%u,", priority);
768 if (wc->masks.dl_type) {
770 if (f->dl_type == htons(ETH_TYPE_IP)) {
771 if (wc->masks.nw_proto) {
773 if (f->nw_proto == IPPROTO_ICMP) {
774 ds_put_cstr(s, "icmp,");
775 } else if (f->nw_proto == IPPROTO_TCP) {
776 ds_put_cstr(s, "tcp,");
777 } else if (f->nw_proto == IPPROTO_UDP) {
778 ds_put_cstr(s, "udp,");
780 ds_put_cstr(s, "ip,");
784 ds_put_cstr(s, "ip,");
786 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
787 if (wc->masks.nw_proto) {
789 if (f->nw_proto == IPPROTO_ICMPV6) {
790 ds_put_cstr(s, "icmp6,");
791 } else if (f->nw_proto == IPPROTO_TCP) {
792 ds_put_cstr(s, "tcp6,");
793 } else if (f->nw_proto == IPPROTO_UDP) {
794 ds_put_cstr(s, "udp6,");
796 ds_put_cstr(s, "ipv6,");
800 ds_put_cstr(s, "ipv6,");
802 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
803 ds_put_cstr(s, "arp,");
804 } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
805 ds_put_cstr(s, "rarp,");
810 for (i = 0; i < FLOW_N_REGS; i++) {
811 switch (wc->masks.regs[i]) {
815 ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
818 ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
819 i, f->regs[i], wc->masks.regs[i]);
824 format_flow_tunnel(s, match);
826 switch (wc->masks.metadata) {
829 case CONSTANT_HTONLL(UINT64_MAX):
830 ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata));
833 ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",",
834 ntohll(f->metadata), ntohll(wc->masks.metadata));
837 if (wc->masks.in_port) {
838 ds_put_format(s, "in_port=%"PRIu16",", f->in_port);
840 if (wc->masks.vlan_tci) {
841 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
842 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
843 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
845 if (cfi && f->vlan_tci & htons(VLAN_CFI)
846 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
847 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
848 && (vid_mask || pcp_mask)) {
850 ds_put_format(s, "dl_vlan=%"PRIu16",",
851 vlan_tci_to_vid(f->vlan_tci));
854 ds_put_format(s, "dl_vlan_pcp=%d,",
855 vlan_tci_to_pcp(f->vlan_tci));
857 } else if (wc->masks.vlan_tci == htons(0xffff)) {
858 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
860 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
861 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
864 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
865 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
866 if (!skip_type && wc->masks.dl_type) {
867 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
869 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
870 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
871 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
872 if (wc->masks.ipv6_label) {
873 if (wc->masks.ipv6_label == htonl(UINT32_MAX)) {
874 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
875 ntohl(f->ipv6_label));
877 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
878 ntohl(f->ipv6_label),
879 ntohl(wc->masks.ipv6_label));
882 } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
883 f->dl_type == htons(ETH_TYPE_RARP)) {
884 format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
885 format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
887 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
888 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
890 if (!skip_proto && wc->masks.nw_proto) {
891 if (f->dl_type == htons(ETH_TYPE_ARP) ||
892 f->dl_type == htons(ETH_TYPE_RARP)) {
893 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
895 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
898 if (f->dl_type == htons(ETH_TYPE_ARP) ||
899 f->dl_type == htons(ETH_TYPE_RARP)) {
900 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
901 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
903 if (wc->masks.nw_tos & IP_DSCP_MASK) {
904 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
906 if (wc->masks.nw_tos & IP_ECN_MASK) {
907 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
909 if (wc->masks.nw_ttl) {
910 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
912 switch (wc->masks.nw_frag) {
913 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
914 ds_put_format(s, "nw_frag=%s,",
915 f->nw_frag & FLOW_NW_FRAG_ANY
916 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
917 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
920 case FLOW_NW_FRAG_ANY:
921 ds_put_format(s, "nw_frag=%s,",
922 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
925 case FLOW_NW_FRAG_LATER:
926 ds_put_format(s, "nw_frag=%s,",
927 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
930 if (f->dl_type == htons(ETH_TYPE_IP) &&
931 f->nw_proto == IPPROTO_ICMP) {
932 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
933 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
934 } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
935 f->nw_proto == IPPROTO_ICMPV6) {
936 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
937 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
938 format_ipv6_netmask(s, "nd_target", &f->nd_target,
939 &wc->masks.nd_target);
940 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
941 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
943 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
944 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
947 if (s->length > start_len && ds_last(s) == ',') {
952 /* Converts 'match' to a string and returns the string. If 'priority' is
953 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
954 * must free the string (with free()). */
956 match_to_string(const struct match *match, unsigned int priority)
958 struct ds s = DS_EMPTY_INITIALIZER;
959 match_format(match, &s, priority);
960 return ds_steal_cstr(&s);
964 match_print(const struct match *match)
966 char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
971 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
972 * with minimatch_destroy(). */
974 minimatch_init(struct minimatch *dst, const struct match *src)
976 miniflow_init(&dst->flow, &src->flow);
977 minimask_init(&dst->mask, &src->wc);
980 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
981 * with minimatch_destroy(). */
983 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
985 miniflow_clone(&dst->flow, &src->flow);
986 minimask_clone(&dst->mask, &src->mask);
989 /* Frees any memory owned by 'match'. Does not free the storage in which
990 * 'match' itself resides; the caller is responsible for that. */
992 minimatch_destroy(struct minimatch *match)
994 miniflow_destroy(&match->flow);
995 minimask_destroy(&match->mask);
998 /* Initializes 'dst' as a copy of 'src'. */
1000 minimatch_expand(const struct minimatch *src, struct match *dst)
1002 miniflow_expand(&src->flow, &dst->flow);
1003 minimask_expand(&src->mask, &dst->wc);
1006 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
1008 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1010 return (miniflow_equal(&a->flow, &b->flow)
1011 && minimask_equal(&a->mask, &b->mask));
1014 /* Returns a hash value for 'match', given 'basis'. */
1016 minimatch_hash(const struct minimatch *match, uint32_t basis)
1018 return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
1021 /* Appends a string representation of 'match' to 's'. If 'priority' is
1022 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1024 minimatch_format(const struct minimatch *match, struct ds *s,
1025 unsigned int priority)
1027 struct match megamatch;
1029 minimatch_expand(match, &megamatch);
1030 match_format(&megamatch, s, priority);
1033 /* Converts 'match' to a string and returns the string. If 'priority' is
1034 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1035 * must free the string (with free()). */
1037 minimatch_to_string(const struct minimatch *match, unsigned int priority)
1039 struct match megamatch;
1041 minimatch_expand(match, &megamatch);
1042 return match_to_string(&megamatch, priority);