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)
40 ovs_be64 tun_id = flow->tunnel.tun_id;
43 match->flow.skb_priority = 0;
44 memset(&match->flow.tunnel, 0, sizeof match->flow.tunnel);
45 match->flow.tunnel.tun_id = tun_id;
46 flow_wildcards_init_exact(&match->wc);
49 /* Initializes 'match' as a "catch-all" match that matches every packet. */
51 match_init_catchall(struct match *match)
53 memset(&match->flow, 0, sizeof match->flow);
54 flow_wildcards_init_catchall(&match->wc);
57 /* For each bit or field wildcarded in 'match', sets the corresponding bit or
58 * field in 'flow' to all-0-bits. It is important to maintain this invariant
59 * in a match that might be inserted into a classifier.
61 * It is never necessary to call this function directly for a match that is
62 * initialized or modified only by match_*() functions. It is useful to
63 * restore the invariant in a match whose 'wc' member is modified by hand.
66 match_zero_wildcarded_fields(struct match *match)
68 flow_zero_wildcards(&match->flow, &match->wc);
72 match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
74 match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
78 match_set_reg_masked(struct match *match, unsigned int reg_idx,
79 uint32_t value, uint32_t mask)
81 assert(reg_idx < FLOW_N_REGS);
82 flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
83 match->flow.regs[reg_idx] = value & mask;
87 match_set_metadata(struct match *match, ovs_be64 metadata)
89 match_set_metadata_masked(match, metadata, htonll(UINT64_MAX));
93 match_set_metadata_masked(struct match *match,
94 ovs_be64 metadata, ovs_be64 mask)
96 match->wc.masks.metadata = mask;
97 match->flow.metadata = metadata & mask;
101 match_set_tun_id(struct match *match, ovs_be64 tun_id)
103 match_set_tun_id_masked(match, tun_id, htonll(UINT64_MAX));
107 match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
109 match->wc.masks.tunnel.tun_id = mask;
110 match->flow.tunnel.tun_id = tun_id & mask;
114 match_set_in_port(struct match *match, uint16_t ofp_port)
116 match->wc.masks.in_port = UINT16_MAX;
117 match->flow.in_port = ofp_port;
121 match_set_dl_type(struct match *match, ovs_be16 dl_type)
123 match->wc.masks.dl_type = htons(UINT16_MAX);
124 match->flow.dl_type = dl_type;
127 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
128 * exactly. 'mask_dst' is set to all 1s. */
130 set_eth(const uint8_t value_src[ETH_ADDR_LEN],
131 uint8_t value_dst[ETH_ADDR_LEN],
132 uint8_t mask_dst[ETH_ADDR_LEN])
134 memcpy(value_dst, value_src, ETH_ADDR_LEN);
135 memset(mask_dst, 0xff, ETH_ADDR_LEN);
138 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
139 * after each byte is ANDed with the appropriate byte in 'mask_src'.
140 * 'mask_dst' is set to 'mask_src' */
142 set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
143 const uint8_t mask_src[ETH_ADDR_LEN],
144 uint8_t value_dst[ETH_ADDR_LEN],
145 uint8_t mask_dst[ETH_ADDR_LEN])
149 for (i = 0; i < ETH_ADDR_LEN; i++) {
150 value_dst[i] = value_src[i] & mask_src[i];
151 mask_dst[i] = mask_src[i];
155 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
158 match_set_dl_src(struct match *match, const uint8_t dl_src[ETH_ADDR_LEN])
160 set_eth(dl_src, match->flow.dl_src, match->wc.masks.dl_src);
163 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
164 * after each byte is ANDed with the appropriate byte in 'mask'. */
166 match_set_dl_src_masked(struct match *match,
167 const uint8_t dl_src[ETH_ADDR_LEN],
168 const uint8_t mask[ETH_ADDR_LEN])
170 set_eth_masked(dl_src, mask, match->flow.dl_src, match->wc.masks.dl_src);
173 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
176 match_set_dl_dst(struct match *match, const uint8_t dl_dst[ETH_ADDR_LEN])
178 set_eth(dl_dst, match->flow.dl_dst, match->wc.masks.dl_dst);
181 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
182 * byte is ANDed with the appropriate byte in 'mask'.
184 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
185 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
187 match_set_dl_dst_masked(struct match *match,
188 const uint8_t dl_dst[ETH_ADDR_LEN],
189 const uint8_t mask[ETH_ADDR_LEN])
191 set_eth_masked(dl_dst, mask, match->flow.dl_dst, match->wc.masks.dl_dst);
195 match_set_dl_tci(struct match *match, ovs_be16 tci)
197 match_set_dl_tci_masked(match, tci, htons(0xffff));
201 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
203 match->flow.vlan_tci = tci & mask;
204 match->wc.masks.vlan_tci = mask;
207 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
208 * wildcarded, then 'match' will match a packet regardless of whether it has an
209 * 802.1Q header or not. */
211 match_set_any_vid(struct match *match)
213 if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
214 match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
215 match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
217 match_set_dl_tci_masked(match, htons(0), htons(0));
221 /* Modifies 'match' depending on 'dl_vlan':
223 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
224 * without an 802.1Q header.
226 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
227 * VID equals the low 12 bits of 'dl_vlan'.
230 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
232 flow_set_dl_vlan(&match->flow, dl_vlan);
233 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
234 match->wc.masks.vlan_tci = htons(UINT16_MAX);
236 match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
240 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
241 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
244 match_set_vlan_vid(struct match *match, ovs_be16 vid)
246 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
250 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
251 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
252 * plus CFI), with the corresponding 'mask'. */
254 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
256 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
257 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
260 flow_set_vlan_vid(&match->flow, vid & mask);
261 match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
264 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
265 * wildcarded, then 'match' will match a packet regardless of whether it has an
266 * 802.1Q header or not. */
268 match_set_any_pcp(struct match *match)
270 if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
271 match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
272 match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
274 match_set_dl_tci_masked(match, htons(0), htons(0));
278 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
279 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
281 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
283 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
284 match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
288 match_set_tp_src(struct match *match, ovs_be16 tp_src)
290 match_set_tp_src_masked(match, tp_src, htons(UINT16_MAX));
294 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
296 match->flow.tp_src = port & mask;
297 match->wc.masks.tp_src = mask;
301 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
303 match_set_tp_dst_masked(match, tp_dst, htons(UINT16_MAX));
307 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
309 match->flow.tp_dst = port & mask;
310 match->wc.masks.tp_dst = mask;
314 match_set_nw_proto(struct match *match, uint8_t nw_proto)
316 match->flow.nw_proto = nw_proto;
317 match->wc.masks.nw_proto = UINT8_MAX;
321 match_set_nw_src(struct match *match, ovs_be32 nw_src)
323 match->flow.nw_src = nw_src;
324 match->wc.masks.nw_src = htonl(UINT32_MAX);
328 match_set_nw_src_masked(struct match *match,
329 ovs_be32 nw_src, ovs_be32 mask)
331 match->flow.nw_src = nw_src & mask;
332 match->wc.masks.nw_src = mask;
336 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
338 match->flow.nw_dst = nw_dst;
339 match->wc.masks.nw_dst = htonl(UINT32_MAX);
343 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
345 match->flow.nw_dst = ip & mask;
346 match->wc.masks.nw_dst = mask;
350 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
352 match->wc.masks.nw_tos |= IP_DSCP_MASK;
353 match->flow.nw_tos &= ~IP_DSCP_MASK;
354 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
358 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
360 match->wc.masks.nw_tos |= IP_ECN_MASK;
361 match->flow.nw_tos &= ~IP_ECN_MASK;
362 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
366 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
368 match->wc.masks.nw_ttl = UINT8_MAX;
369 match->flow.nw_ttl = nw_ttl;
373 match_set_nw_frag(struct match *match, uint8_t nw_frag)
375 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
376 match->flow.nw_frag = nw_frag;
380 match_set_nw_frag_masked(struct match *match,
381 uint8_t nw_frag, uint8_t mask)
383 match->flow.nw_frag = nw_frag & mask;
384 match->wc.masks.nw_frag = mask;
388 match_set_icmp_type(struct match *match, uint8_t icmp_type)
390 match_set_tp_src(match, htons(icmp_type));
394 match_set_icmp_code(struct match *match, uint8_t icmp_code)
396 match_set_tp_dst(match, htons(icmp_code));
400 match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN])
402 memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN);
403 memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN);
407 match_set_arp_sha_masked(struct match *match,
408 const uint8_t arp_sha[ETH_ADDR_LEN],
409 const uint8_t mask[ETH_ADDR_LEN])
411 set_eth_masked(arp_sha, mask,
412 match->flow.arp_sha, match->wc.masks.arp_sha);
416 match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN])
418 memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN);
419 memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN);
423 match_set_arp_tha_masked(struct match *match,
424 const uint8_t arp_tha[ETH_ADDR_LEN],
425 const uint8_t mask[ETH_ADDR_LEN])
427 set_eth_masked(arp_tha, mask,
428 match->flow.arp_tha, match->wc.masks.arp_tha);
432 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
434 match->flow.ipv6_src = *src;
435 match->wc.masks.ipv6_src = in6addr_exact;
439 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
440 const struct in6_addr *mask)
442 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
443 match->wc.masks.ipv6_src = *mask;
447 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
449 match->flow.ipv6_dst = *dst;
450 match->wc.masks.ipv6_dst = in6addr_exact;
454 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
455 const struct in6_addr *mask)
457 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
458 match->wc.masks.ipv6_dst = *mask;
462 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
464 match->wc.masks.ipv6_label = htonl(UINT32_MAX);
465 match->flow.ipv6_label = ipv6_label;
470 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
473 match->flow.ipv6_label = ipv6_label & mask;
474 match->wc.masks.ipv6_label = mask;
478 match_set_nd_target(struct match *match, const struct in6_addr *target)
480 match->flow.nd_target = *target;
481 match->wc.masks.nd_target = in6addr_exact;
485 match_set_nd_target_masked(struct match *match,
486 const struct in6_addr *target,
487 const struct in6_addr *mask)
489 match->flow.nd_target = ipv6_addr_bitand(target, mask);
490 match->wc.masks.nd_target = *mask;
493 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
494 * values for fixed fields, otherwise false. */
496 match_equal(const struct match *a, const struct match *b)
498 return (flow_wildcards_equal(&a->wc, &b->wc)
499 && flow_equal(&a->flow, &b->flow));
502 /* Returns a hash value for the flow and wildcards in 'match', starting from
505 match_hash(const struct match *match, uint32_t basis)
507 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
511 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
512 const uint8_t mask[6])
514 if (!eth_addr_is_zero(mask)) {
515 ds_put_format(s, "%s=", name);
516 eth_format_masked(eth, mask, s);
522 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
526 ds_put_format(s, "%s=", name);
527 ip_format_masked(ip, netmask, s);
533 format_ipv6_netmask(struct ds *s, const char *name,
534 const struct in6_addr *addr,
535 const struct in6_addr *netmask)
537 if (!ipv6_mask_is_any(netmask)) {
538 ds_put_format(s, "%s=", name);
539 print_ipv6_masked(s, addr, netmask);
546 format_be16_masked(struct ds *s, const char *name,
547 ovs_be16 value, ovs_be16 mask)
549 if (mask != htons(0)) {
550 ds_put_format(s, "%s=", name);
551 if (mask == htons(UINT16_MAX)) {
552 ds_put_format(s, "%"PRIu16, ntohs(value));
554 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
555 ntohs(value), ntohs(mask));
561 /* Appends a string representation of 'match' to 's'. If 'priority' is
562 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
564 match_format(const struct match *match, struct ds *s, unsigned int priority)
566 const struct flow_wildcards *wc = &match->wc;
567 size_t start_len = s->length;
568 const struct flow *f = &match->flow;
569 bool skip_type = false;
570 bool skip_proto = false;
574 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 17);
576 if (priority != OFP_DEFAULT_PRIORITY) {
577 ds_put_format(s, "priority=%u,", priority);
580 if (wc->masks.dl_type) {
582 if (f->dl_type == htons(ETH_TYPE_IP)) {
583 if (wc->masks.nw_proto) {
585 if (f->nw_proto == IPPROTO_ICMP) {
586 ds_put_cstr(s, "icmp,");
587 } else if (f->nw_proto == IPPROTO_TCP) {
588 ds_put_cstr(s, "tcp,");
589 } else if (f->nw_proto == IPPROTO_UDP) {
590 ds_put_cstr(s, "udp,");
592 ds_put_cstr(s, "ip,");
596 ds_put_cstr(s, "ip,");
598 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
599 if (wc->masks.nw_proto) {
601 if (f->nw_proto == IPPROTO_ICMPV6) {
602 ds_put_cstr(s, "icmp6,");
603 } else if (f->nw_proto == IPPROTO_TCP) {
604 ds_put_cstr(s, "tcp6,");
605 } else if (f->nw_proto == IPPROTO_UDP) {
606 ds_put_cstr(s, "udp6,");
608 ds_put_cstr(s, "ipv6,");
612 ds_put_cstr(s, "ipv6,");
614 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
615 ds_put_cstr(s, "arp,");
620 for (i = 0; i < FLOW_N_REGS; i++) {
621 switch (wc->masks.regs[i]) {
625 ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
628 ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
629 i, f->regs[i], wc->masks.regs[i]);
633 switch (wc->masks.tunnel.tun_id) {
636 case CONSTANT_HTONLL(UINT64_MAX):
637 ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(f->tunnel.tun_id));
640 ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
641 ntohll(f->tunnel.tun_id),
642 ntohll(wc->masks.tunnel.tun_id));
645 switch (wc->masks.metadata) {
648 case CONSTANT_HTONLL(UINT64_MAX):
649 ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata));
652 ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",",
653 ntohll(f->metadata), ntohll(wc->masks.metadata));
656 if (wc->masks.in_port) {
657 ds_put_format(s, "in_port=%"PRIu16",", f->in_port);
659 if (wc->masks.vlan_tci) {
660 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
661 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
662 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
664 if (cfi && f->vlan_tci & htons(VLAN_CFI)
665 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
666 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
667 && (vid_mask || pcp_mask)) {
669 ds_put_format(s, "dl_vlan=%"PRIu16",",
670 vlan_tci_to_vid(f->vlan_tci));
673 ds_put_format(s, "dl_vlan_pcp=%d,",
674 vlan_tci_to_pcp(f->vlan_tci));
676 } else if (wc->masks.vlan_tci == htons(0xffff)) {
677 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
679 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
680 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
683 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
684 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
685 if (!skip_type && wc->masks.dl_type) {
686 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
688 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
689 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
690 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
691 if (wc->masks.ipv6_label) {
692 if (wc->masks.ipv6_label == htonl(UINT32_MAX)) {
693 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
694 ntohl(f->ipv6_label));
696 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
697 ntohl(f->ipv6_label),
698 ntohl(wc->masks.ipv6_label));
702 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
703 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
705 if (!skip_proto && wc->masks.nw_proto) {
706 if (f->dl_type == htons(ETH_TYPE_ARP)) {
707 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
709 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
712 if (f->dl_type == htons(ETH_TYPE_ARP)) {
713 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
714 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
716 if (wc->masks.nw_tos & IP_DSCP_MASK) {
717 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
719 if (wc->masks.nw_tos & IP_ECN_MASK) {
720 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
722 if (wc->masks.nw_ttl) {
723 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
725 switch (wc->masks.nw_frag) {
726 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
727 ds_put_format(s, "nw_frag=%s,",
728 f->nw_frag & FLOW_NW_FRAG_ANY
729 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
730 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
733 case FLOW_NW_FRAG_ANY:
734 ds_put_format(s, "nw_frag=%s,",
735 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
738 case FLOW_NW_FRAG_LATER:
739 ds_put_format(s, "nw_frag=%s,",
740 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
743 if (f->nw_proto == IPPROTO_ICMP) {
744 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
745 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
746 } else if (f->nw_proto == IPPROTO_ICMPV6) {
747 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
748 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
749 format_ipv6_netmask(s, "nd_target", &f->nd_target,
750 &wc->masks.nd_target);
751 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
752 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
754 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
755 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
758 if (s->length > start_len && ds_last(s) == ',') {
763 /* Converts 'match' to a string and returns the string. If 'priority' is
764 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
765 * must free the string (with free()). */
767 match_to_string(const struct match *match, unsigned int priority)
769 struct ds s = DS_EMPTY_INITIALIZER;
770 match_format(match, &s, priority);
771 return ds_steal_cstr(&s);
775 match_print(const struct match *match)
777 char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
782 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
783 * with minimatch_destroy(). */
785 minimatch_init(struct minimatch *dst, const struct match *src)
787 miniflow_init(&dst->flow, &src->flow);
788 minimask_init(&dst->mask, &src->wc);
791 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
792 * with minimatch_destroy(). */
794 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
796 miniflow_clone(&dst->flow, &src->flow);
797 minimask_clone(&dst->mask, &src->mask);
800 /* Frees any memory owned by 'match'. Does not free the storage in which
801 * 'match' itself resides; the caller is responsible for that. */
803 minimatch_destroy(struct minimatch *match)
805 miniflow_destroy(&match->flow);
806 minimask_destroy(&match->mask);
809 /* Initializes 'dst' as a copy of 'src'. */
811 minimatch_expand(const struct minimatch *src, struct match *dst)
813 miniflow_expand(&src->flow, &dst->flow);
814 minimask_expand(&src->mask, &dst->wc);
817 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
819 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
821 return (miniflow_equal(&a->flow, &b->flow)
822 && minimask_equal(&a->mask, &b->mask));
825 /* Returns a hash value for 'match', given 'basis'. */
827 minimatch_hash(const struct minimatch *match, uint32_t basis)
829 return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
832 /* Appends a string representation of 'match' to 's'. If 'priority' is
833 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
835 minimatch_format(const struct minimatch *match, struct ds *s,
836 unsigned int priority)
838 struct match megamatch;
840 minimatch_expand(match, &megamatch);
841 match_format(&megamatch, s, priority);
844 /* Converts 'match' to a string and returns the string. If 'priority' is
845 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
846 * must free the string (with free()). */
848 minimatch_to_string(const struct minimatch *match, unsigned int priority)
850 struct match megamatch;
852 minimatch_expand(match, &megamatch);
853 return match_to_string(&megamatch, priority);