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 || flow->tunnel.tun_id) {
66 memset(&wc->masks.tunnel.tun_id, 0xff, sizeof wc->masks.tunnel.tun_id);
67 memset(&wc->masks.tunnel.ip_src, 0xff, sizeof wc->masks.tunnel.ip_src);
68 memset(&wc->masks.tunnel.ip_dst, 0xff, sizeof wc->masks.tunnel.ip_dst);
69 memset(&wc->masks.tunnel.flags, 0xff, sizeof wc->masks.tunnel.flags);
70 memset(&wc->masks.tunnel.ip_tos, 0xff, sizeof wc->masks.tunnel.ip_tos);
71 memset(&wc->masks.tunnel.ip_ttl, 0xff, sizeof wc->masks.tunnel.ip_ttl);
73 memset(&wc->masks.metadata, 0xff, sizeof wc->masks.metadata);
74 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
75 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
76 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
77 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
79 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
80 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
81 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
82 memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
83 } else if (flow->dl_type == htons(ETH_TYPE_IP) ||
84 (flow->dl_type == htons(ETH_TYPE_ARP)) ||
85 (flow->dl_type == htons(ETH_TYPE_RARP))) {
86 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
87 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
90 if (flow->dl_type == htons(ETH_TYPE_ARP) ||
91 flow->dl_type == htons(ETH_TYPE_RARP)) {
92 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
93 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
96 if (flow->dl_type == htons(ETH_TYPE_IPV6) ||
97 flow->dl_type == htons(ETH_TYPE_IP)) {
98 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
99 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
103 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
106 if (flow->nw_proto == IPPROTO_ICMP || flow->nw_proto == IPPROTO_ICMPV6 ||
107 (flow->tp_src || flow->tp_dst)) {
108 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
109 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
112 if (flow->nw_proto == IPPROTO_ICMPV6) {
113 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
114 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
120 /* Converts the flow in 'flow' into an exact-match match in 'match'. */
122 match_init_exact(struct match *match, const struct flow *flow)
124 ovs_be64 tun_id = flow->tunnel.tun_id;
127 match->flow.skb_priority = 0;
128 match->flow.skb_mark = 0;
129 memset(&match->flow.tunnel, 0, sizeof match->flow.tunnel);
130 match->flow.tunnel.tun_id = tun_id;
131 flow_wildcards_init_exact(&match->wc);
134 /* Initializes 'match' as a "catch-all" match that matches every packet. */
136 match_init_catchall(struct match *match)
138 memset(&match->flow, 0, sizeof match->flow);
139 flow_wildcards_init_catchall(&match->wc);
142 /* For each bit or field wildcarded in 'match', sets the corresponding bit or
143 * field in 'flow' to all-0-bits. It is important to maintain this invariant
144 * in a match that might be inserted into a classifier.
146 * It is never necessary to call this function directly for a match that is
147 * initialized or modified only by match_*() functions. It is useful to
148 * restore the invariant in a match whose 'wc' member is modified by hand.
151 match_zero_wildcarded_fields(struct match *match)
153 flow_zero_wildcards(&match->flow, &match->wc);
157 match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
159 match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
163 match_set_reg_masked(struct match *match, unsigned int reg_idx,
164 uint32_t value, uint32_t mask)
166 assert(reg_idx < FLOW_N_REGS);
167 flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
168 match->flow.regs[reg_idx] = value & mask;
172 match_set_metadata(struct match *match, ovs_be64 metadata)
174 match_set_metadata_masked(match, metadata, htonll(UINT64_MAX));
178 match_set_metadata_masked(struct match *match,
179 ovs_be64 metadata, ovs_be64 mask)
181 match->wc.masks.metadata = mask;
182 match->flow.metadata = metadata & mask;
186 match_set_tun_id(struct match *match, ovs_be64 tun_id)
188 match_set_tun_id_masked(match, tun_id, htonll(UINT64_MAX));
192 match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
194 match->wc.masks.tunnel.tun_id = mask;
195 match->flow.tunnel.tun_id = tun_id & mask;
199 match_set_in_port(struct match *match, uint16_t ofp_port)
201 match->wc.masks.in_port = UINT16_MAX;
202 match->flow.in_port = ofp_port;
206 match_set_dl_type(struct match *match, ovs_be16 dl_type)
208 match->wc.masks.dl_type = htons(UINT16_MAX);
209 match->flow.dl_type = dl_type;
212 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
213 * exactly. 'mask_dst' is set to all 1s. */
215 set_eth(const uint8_t value_src[ETH_ADDR_LEN],
216 uint8_t value_dst[ETH_ADDR_LEN],
217 uint8_t mask_dst[ETH_ADDR_LEN])
219 memcpy(value_dst, value_src, ETH_ADDR_LEN);
220 memset(mask_dst, 0xff, ETH_ADDR_LEN);
223 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
224 * after each byte is ANDed with the appropriate byte in 'mask_src'.
225 * 'mask_dst' is set to 'mask_src' */
227 set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
228 const uint8_t mask_src[ETH_ADDR_LEN],
229 uint8_t value_dst[ETH_ADDR_LEN],
230 uint8_t mask_dst[ETH_ADDR_LEN])
234 for (i = 0; i < ETH_ADDR_LEN; i++) {
235 value_dst[i] = value_src[i] & mask_src[i];
236 mask_dst[i] = mask_src[i];
240 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
243 match_set_dl_src(struct match *match, const uint8_t dl_src[ETH_ADDR_LEN])
245 set_eth(dl_src, match->flow.dl_src, match->wc.masks.dl_src);
248 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
249 * after each byte is ANDed with the appropriate byte in 'mask'. */
251 match_set_dl_src_masked(struct match *match,
252 const uint8_t dl_src[ETH_ADDR_LEN],
253 const uint8_t mask[ETH_ADDR_LEN])
255 set_eth_masked(dl_src, mask, match->flow.dl_src, match->wc.masks.dl_src);
258 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
261 match_set_dl_dst(struct match *match, const uint8_t dl_dst[ETH_ADDR_LEN])
263 set_eth(dl_dst, match->flow.dl_dst, match->wc.masks.dl_dst);
266 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
267 * byte is ANDed with the appropriate byte in 'mask'.
269 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
270 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
272 match_set_dl_dst_masked(struct match *match,
273 const uint8_t dl_dst[ETH_ADDR_LEN],
274 const uint8_t mask[ETH_ADDR_LEN])
276 set_eth_masked(dl_dst, mask, match->flow.dl_dst, match->wc.masks.dl_dst);
280 match_set_dl_tci(struct match *match, ovs_be16 tci)
282 match_set_dl_tci_masked(match, tci, htons(0xffff));
286 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
288 match->flow.vlan_tci = tci & mask;
289 match->wc.masks.vlan_tci = mask;
292 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
293 * wildcarded, then 'match' will match a packet regardless of whether it has an
294 * 802.1Q header or not. */
296 match_set_any_vid(struct match *match)
298 if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
299 match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
300 match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
302 match_set_dl_tci_masked(match, htons(0), htons(0));
306 /* Modifies 'match' depending on 'dl_vlan':
308 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
309 * without an 802.1Q header.
311 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
312 * VID equals the low 12 bits of 'dl_vlan'.
315 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
317 flow_set_dl_vlan(&match->flow, dl_vlan);
318 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
319 match->wc.masks.vlan_tci = htons(UINT16_MAX);
321 match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
325 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
326 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
329 match_set_vlan_vid(struct match *match, ovs_be16 vid)
331 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
335 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
336 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
337 * plus CFI), with the corresponding 'mask'. */
339 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
341 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
342 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
345 flow_set_vlan_vid(&match->flow, vid & mask);
346 match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
349 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
350 * wildcarded, then 'match' will match a packet regardless of whether it has an
351 * 802.1Q header or not. */
353 match_set_any_pcp(struct match *match)
355 if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
356 match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
357 match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
359 match_set_dl_tci_masked(match, htons(0), htons(0));
363 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
364 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
366 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
368 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
369 match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
373 match_set_tp_src(struct match *match, ovs_be16 tp_src)
375 match_set_tp_src_masked(match, tp_src, htons(UINT16_MAX));
379 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
381 match->flow.tp_src = port & mask;
382 match->wc.masks.tp_src = mask;
386 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
388 match_set_tp_dst_masked(match, tp_dst, htons(UINT16_MAX));
392 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
394 match->flow.tp_dst = port & mask;
395 match->wc.masks.tp_dst = mask;
399 match_set_nw_proto(struct match *match, uint8_t nw_proto)
401 match->flow.nw_proto = nw_proto;
402 match->wc.masks.nw_proto = UINT8_MAX;
406 match_set_nw_src(struct match *match, ovs_be32 nw_src)
408 match->flow.nw_src = nw_src;
409 match->wc.masks.nw_src = htonl(UINT32_MAX);
413 match_set_nw_src_masked(struct match *match,
414 ovs_be32 nw_src, ovs_be32 mask)
416 match->flow.nw_src = nw_src & mask;
417 match->wc.masks.nw_src = mask;
421 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
423 match->flow.nw_dst = nw_dst;
424 match->wc.masks.nw_dst = htonl(UINT32_MAX);
428 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
430 match->flow.nw_dst = ip & mask;
431 match->wc.masks.nw_dst = mask;
435 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
437 match->wc.masks.nw_tos |= IP_DSCP_MASK;
438 match->flow.nw_tos &= ~IP_DSCP_MASK;
439 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
443 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
445 match->wc.masks.nw_tos |= IP_ECN_MASK;
446 match->flow.nw_tos &= ~IP_ECN_MASK;
447 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
451 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
453 match->wc.masks.nw_ttl = UINT8_MAX;
454 match->flow.nw_ttl = nw_ttl;
458 match_set_nw_frag(struct match *match, uint8_t nw_frag)
460 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
461 match->flow.nw_frag = nw_frag;
465 match_set_nw_frag_masked(struct match *match,
466 uint8_t nw_frag, uint8_t mask)
468 match->flow.nw_frag = nw_frag & mask;
469 match->wc.masks.nw_frag = mask;
473 match_set_icmp_type(struct match *match, uint8_t icmp_type)
475 match_set_tp_src(match, htons(icmp_type));
479 match_set_icmp_code(struct match *match, uint8_t icmp_code)
481 match_set_tp_dst(match, htons(icmp_code));
485 match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN])
487 memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN);
488 memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN);
492 match_set_arp_sha_masked(struct match *match,
493 const uint8_t arp_sha[ETH_ADDR_LEN],
494 const uint8_t mask[ETH_ADDR_LEN])
496 set_eth_masked(arp_sha, mask,
497 match->flow.arp_sha, match->wc.masks.arp_sha);
501 match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN])
503 memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN);
504 memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN);
508 match_set_arp_tha_masked(struct match *match,
509 const uint8_t arp_tha[ETH_ADDR_LEN],
510 const uint8_t mask[ETH_ADDR_LEN])
512 set_eth_masked(arp_tha, mask,
513 match->flow.arp_tha, match->wc.masks.arp_tha);
517 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
519 match->flow.ipv6_src = *src;
520 match->wc.masks.ipv6_src = in6addr_exact;
524 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
525 const struct in6_addr *mask)
527 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
528 match->wc.masks.ipv6_src = *mask;
532 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
534 match->flow.ipv6_dst = *dst;
535 match->wc.masks.ipv6_dst = in6addr_exact;
539 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
540 const struct in6_addr *mask)
542 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
543 match->wc.masks.ipv6_dst = *mask;
547 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
549 match->wc.masks.ipv6_label = htonl(UINT32_MAX);
550 match->flow.ipv6_label = ipv6_label;
555 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
558 match->flow.ipv6_label = ipv6_label & mask;
559 match->wc.masks.ipv6_label = mask;
563 match_set_nd_target(struct match *match, const struct in6_addr *target)
565 match->flow.nd_target = *target;
566 match->wc.masks.nd_target = in6addr_exact;
570 match_set_nd_target_masked(struct match *match,
571 const struct in6_addr *target,
572 const struct in6_addr *mask)
574 match->flow.nd_target = ipv6_addr_bitand(target, mask);
575 match->wc.masks.nd_target = *mask;
578 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
579 * values for fixed fields, otherwise false. */
581 match_equal(const struct match *a, const struct match *b)
583 return (flow_wildcards_equal(&a->wc, &b->wc)
584 && flow_equal(&a->flow, &b->flow));
587 /* Returns a hash value for the flow and wildcards in 'match', starting from
590 match_hash(const struct match *match, uint32_t basis)
592 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
596 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
597 const uint8_t mask[6])
599 if (!eth_addr_is_zero(mask)) {
600 ds_put_format(s, "%s=", name);
601 eth_format_masked(eth, mask, s);
607 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
611 ds_put_format(s, "%s=", name);
612 ip_format_masked(ip, netmask, s);
618 format_ipv6_netmask(struct ds *s, const char *name,
619 const struct in6_addr *addr,
620 const struct in6_addr *netmask)
622 if (!ipv6_mask_is_any(netmask)) {
623 ds_put_format(s, "%s=", name);
624 print_ipv6_masked(s, addr, netmask);
631 format_be16_masked(struct ds *s, const char *name,
632 ovs_be16 value, ovs_be16 mask)
634 if (mask != htons(0)) {
635 ds_put_format(s, "%s=", name);
636 if (mask == htons(UINT16_MAX)) {
637 ds_put_format(s, "%"PRIu16, ntohs(value));
639 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
640 ntohs(value), ntohs(mask));
646 /* Appends a string representation of 'match' to 's'. If 'priority' is
647 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
649 match_format(const struct match *match, struct ds *s, unsigned int priority)
651 const struct flow_wildcards *wc = &match->wc;
652 size_t start_len = s->length;
653 const struct flow *f = &match->flow;
654 bool skip_type = false;
655 bool skip_proto = false;
659 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 18);
661 if (priority != OFP_DEFAULT_PRIORITY) {
662 ds_put_format(s, "priority=%u,", priority);
665 if (wc->masks.dl_type) {
667 if (f->dl_type == htons(ETH_TYPE_IP)) {
668 if (wc->masks.nw_proto) {
670 if (f->nw_proto == IPPROTO_ICMP) {
671 ds_put_cstr(s, "icmp,");
672 } else if (f->nw_proto == IPPROTO_TCP) {
673 ds_put_cstr(s, "tcp,");
674 } else if (f->nw_proto == IPPROTO_UDP) {
675 ds_put_cstr(s, "udp,");
677 ds_put_cstr(s, "ip,");
681 ds_put_cstr(s, "ip,");
683 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
684 if (wc->masks.nw_proto) {
686 if (f->nw_proto == IPPROTO_ICMPV6) {
687 ds_put_cstr(s, "icmp6,");
688 } else if (f->nw_proto == IPPROTO_TCP) {
689 ds_put_cstr(s, "tcp6,");
690 } else if (f->nw_proto == IPPROTO_UDP) {
691 ds_put_cstr(s, "udp6,");
693 ds_put_cstr(s, "ipv6,");
697 ds_put_cstr(s, "ipv6,");
699 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
700 ds_put_cstr(s, "arp,");
701 } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
702 ds_put_cstr(s, "rarp,");
707 for (i = 0; i < FLOW_N_REGS; i++) {
708 switch (wc->masks.regs[i]) {
712 ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
715 ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
716 i, f->regs[i], wc->masks.regs[i]);
720 switch (wc->masks.tunnel.tun_id) {
723 case CONSTANT_HTONLL(UINT64_MAX):
724 ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(f->tunnel.tun_id));
727 ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
728 ntohll(f->tunnel.tun_id),
729 ntohll(wc->masks.tunnel.tun_id));
732 switch (wc->masks.metadata) {
735 case CONSTANT_HTONLL(UINT64_MAX):
736 ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata));
739 ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",",
740 ntohll(f->metadata), ntohll(wc->masks.metadata));
743 if (wc->masks.in_port) {
744 ds_put_format(s, "in_port=%"PRIu16",", f->in_port);
746 if (wc->masks.vlan_tci) {
747 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
748 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
749 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
751 if (cfi && f->vlan_tci & htons(VLAN_CFI)
752 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
753 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
754 && (vid_mask || pcp_mask)) {
756 ds_put_format(s, "dl_vlan=%"PRIu16",",
757 vlan_tci_to_vid(f->vlan_tci));
760 ds_put_format(s, "dl_vlan_pcp=%d,",
761 vlan_tci_to_pcp(f->vlan_tci));
763 } else if (wc->masks.vlan_tci == htons(0xffff)) {
764 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
766 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
767 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
770 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
771 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
772 if (!skip_type && wc->masks.dl_type) {
773 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
775 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
776 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
777 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
778 if (wc->masks.ipv6_label) {
779 if (wc->masks.ipv6_label == htonl(UINT32_MAX)) {
780 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
781 ntohl(f->ipv6_label));
783 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
784 ntohl(f->ipv6_label),
785 ntohl(wc->masks.ipv6_label));
788 } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
789 f->dl_type == htons(ETH_TYPE_RARP)) {
790 format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
791 format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
793 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
794 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
796 if (!skip_proto && wc->masks.nw_proto) {
797 if (f->dl_type == htons(ETH_TYPE_ARP) ||
798 f->dl_type == htons(ETH_TYPE_RARP)) {
799 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
801 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
804 if (f->dl_type == htons(ETH_TYPE_ARP) ||
805 f->dl_type == htons(ETH_TYPE_RARP)) {
806 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
807 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
809 if (wc->masks.nw_tos & IP_DSCP_MASK) {
810 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
812 if (wc->masks.nw_tos & IP_ECN_MASK) {
813 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
815 if (wc->masks.nw_ttl) {
816 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
818 switch (wc->masks.nw_frag) {
819 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
820 ds_put_format(s, "nw_frag=%s,",
821 f->nw_frag & FLOW_NW_FRAG_ANY
822 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
823 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
826 case FLOW_NW_FRAG_ANY:
827 ds_put_format(s, "nw_frag=%s,",
828 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
831 case FLOW_NW_FRAG_LATER:
832 ds_put_format(s, "nw_frag=%s,",
833 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
836 if (f->dl_type == htons(ETH_TYPE_IP) &&
837 f->nw_proto == IPPROTO_ICMP) {
838 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
839 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
840 } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
841 f->nw_proto == IPPROTO_ICMPV6) {
842 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
843 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
844 format_ipv6_netmask(s, "nd_target", &f->nd_target,
845 &wc->masks.nd_target);
846 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
847 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
849 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
850 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
853 if (s->length > start_len && ds_last(s) == ',') {
858 /* Converts 'match' to a string and returns the string. If 'priority' is
859 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
860 * must free the string (with free()). */
862 match_to_string(const struct match *match, unsigned int priority)
864 struct ds s = DS_EMPTY_INITIALIZER;
865 match_format(match, &s, priority);
866 return ds_steal_cstr(&s);
870 match_print(const struct match *match)
872 char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
877 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
878 * with minimatch_destroy(). */
880 minimatch_init(struct minimatch *dst, const struct match *src)
882 miniflow_init(&dst->flow, &src->flow);
883 minimask_init(&dst->mask, &src->wc);
886 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
887 * with minimatch_destroy(). */
889 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
891 miniflow_clone(&dst->flow, &src->flow);
892 minimask_clone(&dst->mask, &src->mask);
895 /* Frees any memory owned by 'match'. Does not free the storage in which
896 * 'match' itself resides; the caller is responsible for that. */
898 minimatch_destroy(struct minimatch *match)
900 miniflow_destroy(&match->flow);
901 minimask_destroy(&match->mask);
904 /* Initializes 'dst' as a copy of 'src'. */
906 minimatch_expand(const struct minimatch *src, struct match *dst)
908 miniflow_expand(&src->flow, &dst->flow);
909 minimask_expand(&src->mask, &dst->wc);
912 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
914 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
916 return (miniflow_equal(&a->flow, &b->flow)
917 && minimask_equal(&a->mask, &b->mask));
920 /* Returns a hash value for 'match', given 'basis'. */
922 minimatch_hash(const struct minimatch *match, uint32_t basis)
924 return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
927 /* Appends a string representation of 'match' to 's'. If 'priority' is
928 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
930 minimatch_format(const struct minimatch *match, struct ds *s,
931 unsigned int priority)
933 struct match megamatch;
935 minimatch_expand(match, &megamatch);
936 match_format(&megamatch, s, priority);
939 /* Converts 'match' to a string and returns the string. If 'priority' is
940 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
941 * must free the string (with free()). */
943 minimatch_to_string(const struct minimatch *match, unsigned int priority)
945 struct match megamatch;
947 minimatch_expand(match, &megamatch);
948 return match_to_string(&megamatch, priority);