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"
25 /* Converts the flow in 'flow' into a match in 'match', with the given
28 match_init(struct match *match,
29 const struct flow *flow, const struct flow_wildcards *wc)
33 match_zero_wildcarded_fields(match);
36 /* Converts the flow in 'flow' into an exact-match match in 'match'. */
38 match_init_exact(struct match *match, const struct flow *flow)
41 match->flow.skb_priority = 0;
42 flow_wildcards_init_exact(&match->wc);
45 /* Initializes 'match' as a "catch-all" match that matches every packet. */
47 match_init_catchall(struct match *match)
49 memset(&match->flow, 0, sizeof match->flow);
50 flow_wildcards_init_catchall(&match->wc);
53 /* For each bit or field wildcarded in 'match', sets the corresponding bit or
54 * field in 'flow' to all-0-bits. It is important to maintain this invariant
55 * in a match that might be inserted into a classifier.
57 * It is never necessary to call this function directly for a match that is
58 * initialized or modified only by match_*() functions. It is useful to
59 * restore the invariant in a match whose 'wc' member is modified by hand.
62 match_zero_wildcarded_fields(struct match *match)
64 flow_zero_wildcards(&match->flow, &match->wc);
68 match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
70 match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
74 match_set_reg_masked(struct match *match, unsigned int reg_idx,
75 uint32_t value, uint32_t mask)
77 assert(reg_idx < FLOW_N_REGS);
78 flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
79 match->flow.regs[reg_idx] = value & mask;
83 match_set_metadata(struct match *match, ovs_be64 metadata)
85 match_set_metadata_masked(match, metadata, htonll(UINT64_MAX));
89 match_set_metadata_masked(struct match *match,
90 ovs_be64 metadata, ovs_be64 mask)
92 match->wc.masks.metadata = mask;
93 match->flow.metadata = metadata & mask;
97 match_set_tun_id(struct match *match, ovs_be64 tun_id)
99 match_set_tun_id_masked(match, tun_id, htonll(UINT64_MAX));
103 match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
105 match->wc.masks.tun_id = mask;
106 match->flow.tun_id = tun_id & mask;
110 match_set_in_port(struct match *match, uint16_t ofp_port)
112 match->wc.masks.in_port = UINT16_MAX;
113 match->flow.in_port = ofp_port;
117 match_set_dl_type(struct match *match, ovs_be16 dl_type)
119 match->wc.masks.dl_type = htons(UINT16_MAX);
120 match->flow.dl_type = dl_type;
123 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
124 * exactly. 'mask_dst' is set to all 1s. */
126 set_eth(const uint8_t value_src[ETH_ADDR_LEN],
127 uint8_t value_dst[ETH_ADDR_LEN],
128 uint8_t mask_dst[ETH_ADDR_LEN])
130 memcpy(value_dst, value_src, ETH_ADDR_LEN);
131 memset(mask_dst, 0xff, ETH_ADDR_LEN);
134 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
135 * after each byte is ANDed with the appropriate byte in 'mask_src'.
136 * 'mask_dst' is set to 'mask_src' */
138 set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
139 const uint8_t mask_src[ETH_ADDR_LEN],
140 uint8_t value_dst[ETH_ADDR_LEN],
141 uint8_t mask_dst[ETH_ADDR_LEN])
145 for (i = 0; i < ETH_ADDR_LEN; i++) {
146 value_dst[i] = value_src[i] & mask_src[i];
147 mask_dst[i] = mask_src[i];
151 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
154 match_set_dl_src(struct match *match, const uint8_t dl_src[ETH_ADDR_LEN])
156 set_eth(dl_src, match->flow.dl_src, match->wc.masks.dl_src);
159 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
160 * after each byte is ANDed with the appropriate byte in 'mask'. */
162 match_set_dl_src_masked(struct match *match,
163 const uint8_t dl_src[ETH_ADDR_LEN],
164 const uint8_t mask[ETH_ADDR_LEN])
166 set_eth_masked(dl_src, mask, match->flow.dl_src, match->wc.masks.dl_src);
169 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
172 match_set_dl_dst(struct match *match, const uint8_t dl_dst[ETH_ADDR_LEN])
174 set_eth(dl_dst, match->flow.dl_dst, match->wc.masks.dl_dst);
177 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
178 * byte is ANDed with the appropriate byte in 'mask'.
180 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
181 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
183 match_set_dl_dst_masked(struct match *match,
184 const uint8_t dl_dst[ETH_ADDR_LEN],
185 const uint8_t mask[ETH_ADDR_LEN])
187 set_eth_masked(dl_dst, mask, match->flow.dl_dst, match->wc.masks.dl_dst);
191 match_set_dl_tci(struct match *match, ovs_be16 tci)
193 match_set_dl_tci_masked(match, tci, htons(0xffff));
197 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
199 match->flow.vlan_tci = tci & mask;
200 match->wc.masks.vlan_tci = mask;
203 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
204 * wildcarded, then 'match' will match a packet regardless of whether it has an
205 * 802.1Q header or not. */
207 match_set_any_vid(struct match *match)
209 if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
210 match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
211 match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
213 match_set_dl_tci_masked(match, htons(0), htons(0));
217 /* Modifies 'match' depending on 'dl_vlan':
219 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
220 * without an 802.1Q header.
222 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
223 * VID equals the low 12 bits of 'dl_vlan'.
226 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
228 flow_set_dl_vlan(&match->flow, dl_vlan);
229 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
230 match->wc.masks.vlan_tci = htons(UINT16_MAX);
232 match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
236 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
237 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
240 match_set_vlan_vid(struct match *match, ovs_be16 vid)
242 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
246 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
247 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
248 * plus CFI), with the corresponding 'mask'. */
250 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
252 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
253 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
256 flow_set_vlan_vid(&match->flow, vid & mask);
257 match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
260 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
261 * wildcarded, then 'match' will match a packet regardless of whether it has an
262 * 802.1Q header or not. */
264 match_set_any_pcp(struct match *match)
266 if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
267 match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
268 match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
270 match_set_dl_tci_masked(match, htons(0), htons(0));
274 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
275 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
277 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
279 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
280 match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
284 match_set_tp_src(struct match *match, ovs_be16 tp_src)
286 match_set_tp_src_masked(match, tp_src, htons(UINT16_MAX));
290 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
292 match->flow.tp_src = port & mask;
293 match->wc.masks.tp_src = mask;
297 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
299 match_set_tp_dst_masked(match, tp_dst, htons(UINT16_MAX));
303 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
305 match->flow.tp_dst = port & mask;
306 match->wc.masks.tp_dst = mask;
310 match_set_nw_proto(struct match *match, uint8_t nw_proto)
312 match->flow.nw_proto = nw_proto;
313 match->wc.masks.nw_proto = UINT8_MAX;
317 match_set_nw_src(struct match *match, ovs_be32 nw_src)
319 match->flow.nw_src = nw_src;
320 match->wc.masks.nw_src = htonl(UINT32_MAX);
324 match_set_nw_src_masked(struct match *match,
325 ovs_be32 nw_src, ovs_be32 mask)
327 match->flow.nw_src = nw_src & mask;
328 match->wc.masks.nw_src = mask;
332 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
334 match->flow.nw_dst = nw_dst;
335 match->wc.masks.nw_dst = htonl(UINT32_MAX);
339 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
341 match->flow.nw_dst = ip & mask;
342 match->wc.masks.nw_dst = mask;
346 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
348 match->wc.masks.nw_tos |= IP_DSCP_MASK;
349 match->flow.nw_tos &= ~IP_DSCP_MASK;
350 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
354 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
356 match->wc.masks.nw_tos |= IP_ECN_MASK;
357 match->flow.nw_tos &= ~IP_ECN_MASK;
358 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
362 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
364 match->wc.masks.nw_ttl = UINT8_MAX;
365 match->flow.nw_ttl = nw_ttl;
369 match_set_nw_frag(struct match *match, uint8_t nw_frag)
371 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
372 match->flow.nw_frag = nw_frag;
376 match_set_nw_frag_masked(struct match *match,
377 uint8_t nw_frag, uint8_t mask)
379 match->flow.nw_frag = nw_frag & mask;
380 match->wc.masks.nw_frag = mask;
384 match_set_icmp_type(struct match *match, uint8_t icmp_type)
386 match_set_tp_src(match, htons(icmp_type));
390 match_set_icmp_code(struct match *match, uint8_t icmp_code)
392 match_set_tp_dst(match, htons(icmp_code));
396 match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN])
398 memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN);
399 memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN);
403 match_set_arp_sha_masked(struct match *match,
404 const uint8_t arp_sha[ETH_ADDR_LEN],
405 const uint8_t mask[ETH_ADDR_LEN])
407 set_eth_masked(arp_sha, mask,
408 match->flow.arp_sha, match->wc.masks.arp_sha);
412 match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN])
414 memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN);
415 memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN);
419 match_set_arp_tha_masked(struct match *match,
420 const uint8_t arp_tha[ETH_ADDR_LEN],
421 const uint8_t mask[ETH_ADDR_LEN])
423 set_eth_masked(arp_tha, mask,
424 match->flow.arp_tha, match->wc.masks.arp_tha);
428 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
430 match->flow.ipv6_src = *src;
431 match->wc.masks.ipv6_src = in6addr_exact;
435 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
436 const struct in6_addr *mask)
438 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
439 match->wc.masks.ipv6_src = *mask;
443 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
445 match->flow.ipv6_dst = *dst;
446 match->wc.masks.ipv6_dst = in6addr_exact;
450 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
451 const struct in6_addr *mask)
453 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
454 match->wc.masks.ipv6_dst = *mask;
458 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
460 match->wc.masks.ipv6_label = htonl(UINT32_MAX);
461 match->flow.ipv6_label = ipv6_label;
466 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
469 match->flow.ipv6_label = ipv6_label & mask;
470 match->wc.masks.ipv6_label = mask;
474 match_set_nd_target(struct match *match, const struct in6_addr *target)
476 match->flow.nd_target = *target;
477 match->wc.masks.nd_target = in6addr_exact;
481 match_set_nd_target_masked(struct match *match,
482 const struct in6_addr *target,
483 const struct in6_addr *mask)
485 match->flow.nd_target = ipv6_addr_bitand(target, mask);
486 match->wc.masks.nd_target = *mask;
489 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
490 * values for fixed fields, otherwise false. */
492 match_equal(const struct match *a, const struct match *b)
494 return (flow_wildcards_equal(&a->wc, &b->wc)
495 && flow_equal(&a->flow, &b->flow));
498 /* Returns a hash value for the flow and wildcards in 'match', starting from
501 match_hash(const struct match *match, uint32_t basis)
503 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
507 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
508 const uint8_t mask[6])
510 if (!eth_addr_is_zero(mask)) {
511 ds_put_format(s, "%s=", name);
512 eth_format_masked(eth, mask, s);
518 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
522 ds_put_format(s, "%s=", name);
523 ip_format_masked(ip, netmask, s);
529 format_ipv6_netmask(struct ds *s, const char *name,
530 const struct in6_addr *addr,
531 const struct in6_addr *netmask)
533 if (!ipv6_mask_is_any(netmask)) {
534 ds_put_format(s, "%s=", name);
535 print_ipv6_masked(s, addr, netmask);
542 format_be16_masked(struct ds *s, const char *name,
543 ovs_be16 value, ovs_be16 mask)
545 if (mask != htons(0)) {
546 ds_put_format(s, "%s=", name);
547 if (mask == htons(UINT16_MAX)) {
548 ds_put_format(s, "%"PRIu16, ntohs(value));
550 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
551 ntohs(value), ntohs(mask));
557 /* Appends a string representation of 'match' to 's'. If 'priority' is
558 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
560 match_format(const struct match *match, struct ds *s, unsigned int priority)
562 const struct flow_wildcards *wc = &match->wc;
563 size_t start_len = s->length;
564 const struct flow *f = &match->flow;
565 bool skip_type = false;
566 bool skip_proto = false;
570 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 17);
572 if (priority != OFP_DEFAULT_PRIORITY) {
573 ds_put_format(s, "priority=%u,", priority);
576 if (wc->masks.dl_type) {
578 if (f->dl_type == htons(ETH_TYPE_IP)) {
579 if (wc->masks.nw_proto) {
581 if (f->nw_proto == IPPROTO_ICMP) {
582 ds_put_cstr(s, "icmp,");
583 } else if (f->nw_proto == IPPROTO_TCP) {
584 ds_put_cstr(s, "tcp,");
585 } else if (f->nw_proto == IPPROTO_UDP) {
586 ds_put_cstr(s, "udp,");
588 ds_put_cstr(s, "ip,");
592 ds_put_cstr(s, "ip,");
594 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
595 if (wc->masks.nw_proto) {
597 if (f->nw_proto == IPPROTO_ICMPV6) {
598 ds_put_cstr(s, "icmp6,");
599 } else if (f->nw_proto == IPPROTO_TCP) {
600 ds_put_cstr(s, "tcp6,");
601 } else if (f->nw_proto == IPPROTO_UDP) {
602 ds_put_cstr(s, "udp6,");
604 ds_put_cstr(s, "ipv6,");
608 ds_put_cstr(s, "ipv6,");
610 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
611 ds_put_cstr(s, "arp,");
616 for (i = 0; i < FLOW_N_REGS; i++) {
617 switch (wc->masks.regs[i]) {
621 ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
624 ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
625 i, f->regs[i], wc->masks.regs[i]);
629 switch (wc->masks.tun_id) {
632 case CONSTANT_HTONLL(UINT64_MAX):
633 ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(f->tun_id));
636 ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
637 ntohll(f->tun_id), ntohll(wc->masks.tun_id));
640 switch (wc->masks.metadata) {
643 case CONSTANT_HTONLL(UINT64_MAX):
644 ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata));
647 ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",",
648 ntohll(f->metadata), ntohll(wc->masks.metadata));
651 if (wc->masks.in_port) {
652 ds_put_format(s, "in_port=%"PRIu16",", f->in_port);
654 if (wc->masks.vlan_tci) {
655 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
656 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
657 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
659 if (cfi && f->vlan_tci & htons(VLAN_CFI)
660 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
661 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
662 && (vid_mask || pcp_mask)) {
664 ds_put_format(s, "dl_vlan=%"PRIu16",",
665 vlan_tci_to_vid(f->vlan_tci));
668 ds_put_format(s, "dl_vlan_pcp=%d,",
669 vlan_tci_to_pcp(f->vlan_tci));
671 } else if (wc->masks.vlan_tci == htons(0xffff)) {
672 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
674 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
675 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
678 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
679 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
680 if (!skip_type && wc->masks.dl_type) {
681 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
683 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
684 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
685 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
686 if (wc->masks.ipv6_label) {
687 if (wc->masks.ipv6_label == htonl(UINT32_MAX)) {
688 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
689 ntohl(f->ipv6_label));
691 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
692 ntohl(f->ipv6_label),
693 ntohl(wc->masks.ipv6_label));
697 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
698 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
700 if (!skip_proto && wc->masks.nw_proto) {
701 if (f->dl_type == htons(ETH_TYPE_ARP)) {
702 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
704 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
707 if (f->dl_type == htons(ETH_TYPE_ARP)) {
708 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
709 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
711 if (wc->masks.nw_tos & IP_DSCP_MASK) {
712 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
714 if (wc->masks.nw_tos & IP_ECN_MASK) {
715 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
717 if (wc->masks.nw_ttl) {
718 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
720 switch (wc->masks.nw_frag) {
721 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
722 ds_put_format(s, "nw_frag=%s,",
723 f->nw_frag & FLOW_NW_FRAG_ANY
724 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
725 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
728 case FLOW_NW_FRAG_ANY:
729 ds_put_format(s, "nw_frag=%s,",
730 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
733 case FLOW_NW_FRAG_LATER:
734 ds_put_format(s, "nw_frag=%s,",
735 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
738 if (f->nw_proto == IPPROTO_ICMP) {
739 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
740 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
741 } else if (f->nw_proto == IPPROTO_ICMPV6) {
742 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
743 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
744 format_ipv6_netmask(s, "nd_target", &f->nd_target,
745 &wc->masks.nd_target);
746 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
747 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
749 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
750 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
753 if (s->length > start_len && ds_last(s) == ',') {
758 /* Converts 'match' to a string and returns the string. If 'priority' is
759 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
760 * must free the string (with free()). */
762 match_to_string(const struct match *match, unsigned int priority)
764 struct ds s = DS_EMPTY_INITIALIZER;
765 match_format(match, &s, priority);
766 return ds_steal_cstr(&s);
770 match_print(const struct match *match)
772 char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
777 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
778 * with minimatch_destroy(). */
780 minimatch_init(struct minimatch *dst, const struct match *src)
782 miniflow_init(&dst->flow, &src->flow);
783 minimask_init(&dst->mask, &src->wc);
786 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
787 * with minimatch_destroy(). */
789 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
791 miniflow_clone(&dst->flow, &src->flow);
792 minimask_clone(&dst->mask, &src->mask);
795 /* Frees any memory owned by 'match'. Does not free the storage in which
796 * 'match' itself resides; the caller is responsible for that. */
798 minimatch_destroy(struct minimatch *match)
800 miniflow_destroy(&match->flow);
801 minimask_destroy(&match->mask);
804 /* Initializes 'dst' as a copy of 'src'. */
806 minimatch_expand(const struct minimatch *src, struct match *dst)
808 miniflow_expand(&src->flow, &dst->flow);
809 minimask_expand(&src->mask, &dst->wc);
812 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
814 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
816 return (miniflow_equal(&a->flow, &b->flow)
817 && minimask_equal(&a->mask, &b->mask));
820 /* Returns a hash value for 'match', given 'basis'. */
822 minimatch_hash(const struct minimatch *match, uint32_t basis)
824 return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
827 /* Appends a string representation of 'match' to 's'. If 'priority' is
828 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
830 minimatch_format(const struct minimatch *match, struct ds *s,
831 unsigned int priority)
833 struct match megamatch;
835 minimatch_expand(match, &megamatch);
836 match_format(&megamatch, s, priority);
839 /* Converts 'match' to a string and returns the string. If 'priority' is
840 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
841 * must free the string (with free()). */
843 minimatch_to_string(const struct minimatch *match, unsigned int priority)
845 struct match megamatch;
847 minimatch_expand(match, &megamatch);
848 return match_to_string(&megamatch, priority);