2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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.
20 #include "byte-order.h"
21 #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 a flow into a match. It sets the wildcard masks based on
37 * the packet contents. It will not set the mask for fields that do not
38 * make sense for the packet type. */
40 match_wc_init(struct match *match, const struct flow *flow)
44 flow_wildcards_init_for_packet(&match->wc, flow);
45 WC_MASK_FIELD(&match->wc, regs);
46 WC_MASK_FIELD(&match->wc, metadata);
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_dp_hash(struct match *match, uint32_t value)
74 match_set_dp_hash_masked(match, value, UINT32_MAX);
78 match_set_dp_hash_masked(struct match *match, uint32_t value, uint32_t mask)
80 match->wc.masks.dp_hash = mask;
81 match->flow.dp_hash = value & mask;
85 match_set_recirc_id(struct match *match, uint32_t value)
87 match->flow.recirc_id = value;
88 match->wc.masks.recirc_id = UINT32_MAX;
92 match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
94 match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
98 match_set_reg_masked(struct match *match, unsigned int reg_idx,
99 uint32_t value, uint32_t mask)
101 ovs_assert(reg_idx < FLOW_N_REGS);
102 flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
103 match->flow.regs[reg_idx] = value & mask;
107 match_set_xreg(struct match *match, unsigned int xreg_idx, uint64_t value)
109 match_set_xreg_masked(match, xreg_idx, value, UINT64_MAX);
113 match_set_xreg_masked(struct match *match, unsigned int xreg_idx,
114 uint64_t value, uint64_t mask)
116 ovs_assert(xreg_idx < FLOW_N_XREGS);
117 flow_wildcards_set_xreg_mask(&match->wc, xreg_idx, mask);
118 flow_set_xreg(&match->flow, xreg_idx, value & mask);
122 match_set_metadata(struct match *match, ovs_be64 metadata)
124 match_set_metadata_masked(match, metadata, OVS_BE64_MAX);
128 match_set_metadata_masked(struct match *match,
129 ovs_be64 metadata, ovs_be64 mask)
131 match->wc.masks.metadata = mask;
132 match->flow.metadata = metadata & mask;
136 match_set_tun_id(struct match *match, ovs_be64 tun_id)
138 match_set_tun_id_masked(match, tun_id, OVS_BE64_MAX);
142 match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
144 match->wc.masks.tunnel.tun_id = mask;
145 match->flow.tunnel.tun_id = tun_id & mask;
149 match_set_tun_src(struct match *match, ovs_be32 src)
151 match_set_tun_src_masked(match, src, OVS_BE32_MAX);
155 match_set_tun_src_masked(struct match *match, ovs_be32 src, ovs_be32 mask)
157 match->wc.masks.tunnel.ip_src = mask;
158 match->flow.tunnel.ip_src = src & mask;
162 match_set_tun_dst(struct match *match, ovs_be32 dst)
164 match_set_tun_dst_masked(match, dst, OVS_BE32_MAX);
168 match_set_tun_dst_masked(struct match *match, ovs_be32 dst, ovs_be32 mask)
170 match->wc.masks.tunnel.ip_dst = mask;
171 match->flow.tunnel.ip_dst = dst & mask;
175 match_set_tun_ttl(struct match *match, uint8_t ttl)
177 match_set_tun_ttl_masked(match, ttl, UINT8_MAX);
181 match_set_tun_ttl_masked(struct match *match, uint8_t ttl, uint8_t mask)
183 match->wc.masks.tunnel.ip_ttl = mask;
184 match->flow.tunnel.ip_ttl = ttl & mask;
188 match_set_tun_tos(struct match *match, uint8_t tos)
190 match_set_tun_tos_masked(match, tos, UINT8_MAX);
194 match_set_tun_tos_masked(struct match *match, uint8_t tos, uint8_t mask)
196 match->wc.masks.tunnel.ip_tos = mask;
197 match->flow.tunnel.ip_tos = tos & mask;
201 match_set_tun_flags(struct match *match, uint16_t flags)
203 match_set_tun_flags_masked(match, flags, UINT16_MAX);
207 match_set_tun_flags_masked(struct match *match, uint16_t flags, uint16_t mask)
209 match->wc.masks.tunnel.flags = mask;
210 match->flow.tunnel.flags = flags & mask;
214 match_set_in_port(struct match *match, ofp_port_t ofp_port)
216 match->wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
217 match->flow.in_port.ofp_port = ofp_port;
221 match_set_skb_priority(struct match *match, uint32_t skb_priority)
223 match->wc.masks.skb_priority = UINT32_MAX;
224 match->flow.skb_priority = skb_priority;
228 match_set_pkt_mark(struct match *match, uint32_t pkt_mark)
230 match_set_pkt_mark_masked(match, pkt_mark, UINT32_MAX);
234 match_set_pkt_mark_masked(struct match *match, uint32_t pkt_mark, uint32_t mask)
236 match->flow.pkt_mark = pkt_mark & mask;
237 match->wc.masks.pkt_mark = mask;
241 match_set_dl_type(struct match *match, ovs_be16 dl_type)
243 match->wc.masks.dl_type = OVS_BE16_MAX;
244 match->flow.dl_type = dl_type;
247 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
248 * exactly. 'mask_dst' is set to all 1s. */
250 set_eth(const uint8_t value_src[ETH_ADDR_LEN],
251 uint8_t value_dst[ETH_ADDR_LEN],
252 uint8_t mask_dst[ETH_ADDR_LEN])
254 memcpy(value_dst, value_src, ETH_ADDR_LEN);
255 memset(mask_dst, 0xff, ETH_ADDR_LEN);
258 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
259 * after each byte is ANDed with the appropriate byte in 'mask_src'.
260 * 'mask_dst' is set to 'mask_src' */
262 set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
263 const uint8_t mask_src[ETH_ADDR_LEN],
264 uint8_t value_dst[ETH_ADDR_LEN],
265 uint8_t mask_dst[ETH_ADDR_LEN])
269 for (i = 0; i < ETH_ADDR_LEN; i++) {
270 value_dst[i] = value_src[i] & mask_src[i];
271 mask_dst[i] = mask_src[i];
275 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
278 match_set_dl_src(struct match *match, const uint8_t dl_src[ETH_ADDR_LEN])
280 set_eth(dl_src, match->flow.dl_src, match->wc.masks.dl_src);
283 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
284 * after each byte is ANDed with the appropriate byte in 'mask'. */
286 match_set_dl_src_masked(struct match *match,
287 const uint8_t dl_src[ETH_ADDR_LEN],
288 const uint8_t mask[ETH_ADDR_LEN])
290 set_eth_masked(dl_src, mask, match->flow.dl_src, match->wc.masks.dl_src);
293 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
296 match_set_dl_dst(struct match *match, const uint8_t dl_dst[ETH_ADDR_LEN])
298 set_eth(dl_dst, match->flow.dl_dst, match->wc.masks.dl_dst);
301 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
302 * byte is ANDed with the appropriate byte in 'mask'.
304 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
305 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
307 match_set_dl_dst_masked(struct match *match,
308 const uint8_t dl_dst[ETH_ADDR_LEN],
309 const uint8_t mask[ETH_ADDR_LEN])
311 set_eth_masked(dl_dst, mask, match->flow.dl_dst, match->wc.masks.dl_dst);
315 match_set_dl_tci(struct match *match, ovs_be16 tci)
317 match_set_dl_tci_masked(match, tci, htons(0xffff));
321 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
323 match->flow.vlan_tci = tci & mask;
324 match->wc.masks.vlan_tci = mask;
327 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
328 * wildcarded, then 'match' will match a packet regardless of whether it has an
329 * 802.1Q header or not. */
331 match_set_any_vid(struct match *match)
333 if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
334 match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
335 match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
337 match_set_dl_tci_masked(match, htons(0), htons(0));
341 /* Modifies 'match' depending on 'dl_vlan':
343 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
344 * without an 802.1Q header.
346 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
347 * VID equals the low 12 bits of 'dl_vlan'.
350 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
352 flow_set_dl_vlan(&match->flow, dl_vlan);
353 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
354 match->wc.masks.vlan_tci = OVS_BE16_MAX;
356 match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
360 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
361 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
364 match_set_vlan_vid(struct match *match, ovs_be16 vid)
366 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
370 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
371 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
372 * plus CFI), with the corresponding 'mask'. */
374 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
376 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
377 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
380 flow_set_vlan_vid(&match->flow, vid & mask);
381 match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
384 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
385 * wildcarded, then 'match' will match a packet regardless of whether it has an
386 * 802.1Q header or not. */
388 match_set_any_pcp(struct match *match)
390 if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
391 match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
392 match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
394 match_set_dl_tci_masked(match, htons(0), htons(0));
398 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
399 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
401 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
403 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
404 match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
407 /* Modifies 'match' so that the MPLS label 'idx' matches 'lse' exactly. */
409 match_set_mpls_lse(struct match *match, int idx, ovs_be32 lse)
411 match->wc.masks.mpls_lse[idx] = OVS_BE32_MAX;
412 match->flow.mpls_lse[idx] = lse;
415 /* Modifies 'match' so that the MPLS label is wildcarded. */
417 match_set_any_mpls_label(struct match *match, int idx)
419 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_LABEL_MASK);
420 flow_set_mpls_label(&match->flow, idx, htonl(0));
423 /* Modifies 'match' so that it matches only packets with an MPLS header whose
424 * label equals the low 20 bits of 'mpls_label'. */
426 match_set_mpls_label(struct match *match, int idx, ovs_be32 mpls_label)
428 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_LABEL_MASK);
429 flow_set_mpls_label(&match->flow, idx, mpls_label);
432 /* Modifies 'match' so that the MPLS TC is wildcarded. */
434 match_set_any_mpls_tc(struct match *match, int idx)
436 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TC_MASK);
437 flow_set_mpls_tc(&match->flow, idx, 0);
440 /* Modifies 'match' so that it matches only packets with an MPLS header whose
441 * Traffic Class equals the low 3 bits of 'mpls_tc'. */
443 match_set_mpls_tc(struct match *match, int idx, uint8_t mpls_tc)
445 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TC_MASK);
446 flow_set_mpls_tc(&match->flow, idx, mpls_tc);
449 /* Modifies 'match' so that the MPLS stack flag is wildcarded. */
451 match_set_any_mpls_bos(struct match *match, int idx)
453 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_BOS_MASK);
454 flow_set_mpls_bos(&match->flow, idx, 0);
457 /* Modifies 'match' so that it matches only packets with an MPLS header whose
458 * Stack Flag equals the lower bit of 'mpls_bos' */
460 match_set_mpls_bos(struct match *match, int idx, uint8_t mpls_bos)
462 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_BOS_MASK);
463 flow_set_mpls_bos(&match->flow, idx, mpls_bos);
466 /* Modifies 'match' so that the MPLS LSE is wildcarded. */
468 match_set_any_mpls_lse(struct match *match, int idx)
470 match->wc.masks.mpls_lse[idx] = htonl(0);
471 flow_set_mpls_lse(&match->flow, idx, htonl(0));
475 match_set_tp_src(struct match *match, ovs_be16 tp_src)
477 match_set_tp_src_masked(match, tp_src, OVS_BE16_MAX);
481 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
483 match->flow.tp_src = port & mask;
484 match->wc.masks.tp_src = mask;
488 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
490 match_set_tp_dst_masked(match, tp_dst, OVS_BE16_MAX);
494 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
496 match->flow.tp_dst = port & mask;
497 match->wc.masks.tp_dst = mask;
501 match_set_tcp_flags(struct match *match, ovs_be16 flags)
503 match_set_tcp_flags_masked(match, flags, OVS_BE16_MAX);
507 match_set_tcp_flags_masked(struct match *match, ovs_be16 flags, ovs_be16 mask)
509 match->flow.tcp_flags = flags & mask;
510 match->wc.masks.tcp_flags = mask;
514 match_set_nw_proto(struct match *match, uint8_t nw_proto)
516 match->flow.nw_proto = nw_proto;
517 match->wc.masks.nw_proto = UINT8_MAX;
521 match_set_nw_src(struct match *match, ovs_be32 nw_src)
523 match->flow.nw_src = nw_src;
524 match->wc.masks.nw_src = OVS_BE32_MAX;
528 match_set_nw_src_masked(struct match *match,
529 ovs_be32 nw_src, ovs_be32 mask)
531 match->flow.nw_src = nw_src & mask;
532 match->wc.masks.nw_src = mask;
536 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
538 match->flow.nw_dst = nw_dst;
539 match->wc.masks.nw_dst = OVS_BE32_MAX;
543 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
545 match->flow.nw_dst = ip & mask;
546 match->wc.masks.nw_dst = mask;
550 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
552 match->wc.masks.nw_tos |= IP_DSCP_MASK;
553 match->flow.nw_tos &= ~IP_DSCP_MASK;
554 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
558 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
560 match->wc.masks.nw_tos |= IP_ECN_MASK;
561 match->flow.nw_tos &= ~IP_ECN_MASK;
562 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
566 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
568 match->wc.masks.nw_ttl = UINT8_MAX;
569 match->flow.nw_ttl = nw_ttl;
573 match_set_nw_frag(struct match *match, uint8_t nw_frag)
575 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
576 match->flow.nw_frag = nw_frag;
580 match_set_nw_frag_masked(struct match *match,
581 uint8_t nw_frag, uint8_t mask)
583 match->flow.nw_frag = nw_frag & mask;
584 match->wc.masks.nw_frag = mask;
588 match_set_icmp_type(struct match *match, uint8_t icmp_type)
590 match_set_tp_src(match, htons(icmp_type));
594 match_set_icmp_code(struct match *match, uint8_t icmp_code)
596 match_set_tp_dst(match, htons(icmp_code));
600 match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN])
602 memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN);
603 memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN);
607 match_set_arp_sha_masked(struct match *match,
608 const uint8_t arp_sha[ETH_ADDR_LEN],
609 const uint8_t mask[ETH_ADDR_LEN])
611 set_eth_masked(arp_sha, mask,
612 match->flow.arp_sha, match->wc.masks.arp_sha);
616 match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN])
618 memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN);
619 memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN);
623 match_set_arp_tha_masked(struct match *match,
624 const uint8_t arp_tha[ETH_ADDR_LEN],
625 const uint8_t mask[ETH_ADDR_LEN])
627 set_eth_masked(arp_tha, mask,
628 match->flow.arp_tha, match->wc.masks.arp_tha);
632 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
634 match->flow.ipv6_src = *src;
635 match->wc.masks.ipv6_src = in6addr_exact;
639 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
640 const struct in6_addr *mask)
642 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
643 match->wc.masks.ipv6_src = *mask;
647 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
649 match->flow.ipv6_dst = *dst;
650 match->wc.masks.ipv6_dst = in6addr_exact;
654 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
655 const struct in6_addr *mask)
657 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
658 match->wc.masks.ipv6_dst = *mask;
662 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
664 match->wc.masks.ipv6_label = OVS_BE32_MAX;
665 match->flow.ipv6_label = ipv6_label;
670 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
673 match->flow.ipv6_label = ipv6_label & mask;
674 match->wc.masks.ipv6_label = mask;
678 match_set_nd_target(struct match *match, const struct in6_addr *target)
680 match->flow.nd_target = *target;
681 match->wc.masks.nd_target = in6addr_exact;
685 match_set_nd_target_masked(struct match *match,
686 const struct in6_addr *target,
687 const struct in6_addr *mask)
689 match->flow.nd_target = ipv6_addr_bitand(target, mask);
690 match->wc.masks.nd_target = *mask;
693 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
694 * values for fixed fields, otherwise false. */
696 match_equal(const struct match *a, const struct match *b)
698 return (flow_wildcards_equal(&a->wc, &b->wc)
699 && flow_equal(&a->flow, &b->flow));
702 /* Returns a hash value for the flow and wildcards in 'match', starting from
705 match_hash(const struct match *match, uint32_t basis)
707 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
711 match_has_default_recirc_id(const struct match *m)
713 return m->flow.recirc_id == 0 && (m->wc.masks.recirc_id == UINT32_MAX ||
714 m->wc.masks.recirc_id == 0);
718 match_has_default_dp_hash(const struct match *m)
720 return ((m->flow.dp_hash | m->wc.masks.dp_hash) == 0);
723 /* Return true if the hidden fields of the match are set to the default values.
724 * The default values equals to those set up by match_init_hidden_fields(). */
726 match_has_default_hidden_fields(const struct match *m)
728 return match_has_default_recirc_id(m) && match_has_default_dp_hash(m);
732 match_init_hidden_fields(struct match *m)
734 match_set_recirc_id(m, 0);
735 match_set_dp_hash_masked(m, 0, 0);
739 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
740 const uint8_t mask[6])
742 if (!eth_addr_is_zero(mask)) {
743 ds_put_format(s, "%s=", name);
744 eth_format_masked(eth, mask, s);
750 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
754 ds_put_format(s, "%s=", name);
755 ip_format_masked(ip, netmask, s);
761 format_ipv6_netmask(struct ds *s, const char *name,
762 const struct in6_addr *addr,
763 const struct in6_addr *netmask)
765 if (!ipv6_mask_is_any(netmask)) {
766 ds_put_format(s, "%s=", name);
767 print_ipv6_masked(s, addr, netmask);
773 format_be16_masked(struct ds *s, const char *name,
774 ovs_be16 value, ovs_be16 mask)
776 if (mask != htons(0)) {
777 ds_put_format(s, "%s=", name);
778 if (mask == OVS_BE16_MAX) {
779 ds_put_format(s, "%"PRIu16, ntohs(value));
781 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
782 ntohs(value), ntohs(mask));
789 format_be32_masked(struct ds *s, const char *name,
790 ovs_be32 value, ovs_be32 mask)
792 if (mask != htonl(0)) {
793 ds_put_format(s, "%s=", name);
794 if (mask == OVS_BE32_MAX) {
795 ds_put_format(s, "%"PRIu32, ntohl(value));
797 ds_put_format(s, "0x%"PRIx32"/0x%"PRIx32,
798 ntohl(value), ntohl(mask));
805 format_uint32_masked(struct ds *s, const char *name,
806 uint32_t value, uint32_t mask)
809 ds_put_format(s, "%s=%#"PRIx32, name, value);
810 if (mask != UINT32_MAX) {
811 ds_put_format(s, "/%#"PRIx32, mask);
818 format_be64_masked(struct ds *s, const char *name,
819 ovs_be64 value, ovs_be64 mask)
821 if (mask != htonll(0)) {
822 ds_put_format(s, "%s=%#"PRIx64, name, ntohll(value));
823 if (mask != OVS_BE64_MAX) {
824 ds_put_format(s, "/%#"PRIx64, ntohll(mask));
831 format_flow_tunnel(struct ds *s, const struct match *match)
833 const struct flow_wildcards *wc = &match->wc;
834 const struct flow_tnl *tnl = &match->flow.tunnel;
836 format_be64_masked(s, "tun_id", tnl->tun_id, wc->masks.tunnel.tun_id);
837 format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
838 format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
840 if (wc->masks.tunnel.ip_tos) {
841 ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
843 if (wc->masks.tunnel.ip_ttl) {
844 ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
846 if (wc->masks.tunnel.flags) {
847 format_flags(s, flow_tun_flag_to_string, tnl->flags, '|');
852 /* Appends a string representation of 'match' to 's'. If 'priority' is
853 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
855 match_format(const struct match *match, struct ds *s, unsigned int priority)
857 const struct flow_wildcards *wc = &match->wc;
858 size_t start_len = s->length;
859 const struct flow *f = &match->flow;
860 bool skip_type = false;
861 bool skip_proto = false;
865 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 27);
867 if (priority != OFP_DEFAULT_PRIORITY) {
868 ds_put_format(s, "priority=%u,", priority);
871 format_uint32_masked(s, "pkt_mark", f->pkt_mark, wc->masks.pkt_mark);
873 if (wc->masks.recirc_id) {
874 format_uint32_masked(s, "recirc_id", f->recirc_id,
875 wc->masks.recirc_id);
878 if (f->dp_hash && wc->masks.dp_hash) {
879 format_uint32_masked(s, "dp_hash", f->dp_hash,
883 if (wc->masks.skb_priority) {
884 ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority);
887 if (wc->masks.dl_type) {
889 if (f->dl_type == htons(ETH_TYPE_IP)) {
890 if (wc->masks.nw_proto) {
892 if (f->nw_proto == IPPROTO_ICMP) {
893 ds_put_cstr(s, "icmp,");
894 } else if (f->nw_proto == IPPROTO_IGMP) {
895 ds_put_cstr(s, "igmp,");
896 } else if (f->nw_proto == IPPROTO_TCP) {
897 ds_put_cstr(s, "tcp,");
898 } else if (f->nw_proto == IPPROTO_UDP) {
899 ds_put_cstr(s, "udp,");
900 } else if (f->nw_proto == IPPROTO_SCTP) {
901 ds_put_cstr(s, "sctp,");
903 ds_put_cstr(s, "ip,");
907 ds_put_cstr(s, "ip,");
909 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
910 if (wc->masks.nw_proto) {
912 if (f->nw_proto == IPPROTO_ICMPV6) {
913 ds_put_cstr(s, "icmp6,");
914 } else if (f->nw_proto == IPPROTO_TCP) {
915 ds_put_cstr(s, "tcp6,");
916 } else if (f->nw_proto == IPPROTO_UDP) {
917 ds_put_cstr(s, "udp6,");
918 } else if (f->nw_proto == IPPROTO_SCTP) {
919 ds_put_cstr(s, "sctp6,");
921 ds_put_cstr(s, "ipv6,");
925 ds_put_cstr(s, "ipv6,");
927 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
928 ds_put_cstr(s, "arp,");
929 } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
930 ds_put_cstr(s, "rarp,");
931 } else if (f->dl_type == htons(ETH_TYPE_MPLS)) {
932 ds_put_cstr(s, "mpls,");
933 } else if (f->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
934 ds_put_cstr(s, "mplsm,");
939 for (i = 0; i < FLOW_N_REGS; i++) {
940 #define REGNAME_LEN 20
941 char regname[REGNAME_LEN];
942 if (snprintf(regname, REGNAME_LEN, "reg%d", i) >= REGNAME_LEN) {
943 strcpy(regname, "reg?");
945 format_uint32_masked(s, regname, f->regs[i], wc->masks.regs[i]);
948 format_flow_tunnel(s, match);
950 format_be64_masked(s, "metadata", f->metadata, wc->masks.metadata);
952 if (wc->masks.in_port.ofp_port) {
953 ds_put_cstr(s, "in_port=");
954 ofputil_format_port(f->in_port.ofp_port, s);
957 if (wc->masks.vlan_tci) {
958 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
959 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
960 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
962 if (cfi && f->vlan_tci & htons(VLAN_CFI)
963 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
964 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
965 && (vid_mask || pcp_mask)) {
967 ds_put_format(s, "dl_vlan=%"PRIu16",",
968 vlan_tci_to_vid(f->vlan_tci));
971 ds_put_format(s, "dl_vlan_pcp=%d,",
972 vlan_tci_to_pcp(f->vlan_tci));
974 } else if (wc->masks.vlan_tci == htons(0xffff)) {
975 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
977 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
978 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
981 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
982 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
983 if (!skip_type && wc->masks.dl_type) {
984 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
986 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
987 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
988 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
989 if (wc->masks.ipv6_label) {
990 if (wc->masks.ipv6_label == OVS_BE32_MAX) {
991 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
992 ntohl(f->ipv6_label));
994 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
995 ntohl(f->ipv6_label),
996 ntohl(wc->masks.ipv6_label));
999 } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
1000 f->dl_type == htons(ETH_TYPE_RARP)) {
1001 format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
1002 format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
1004 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
1005 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
1007 if (!skip_proto && wc->masks.nw_proto) {
1008 if (f->dl_type == htons(ETH_TYPE_ARP) ||
1009 f->dl_type == htons(ETH_TYPE_RARP)) {
1010 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
1012 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
1015 if (f->dl_type == htons(ETH_TYPE_ARP) ||
1016 f->dl_type == htons(ETH_TYPE_RARP)) {
1017 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
1018 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
1020 if (wc->masks.nw_tos & IP_DSCP_MASK) {
1021 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
1023 if (wc->masks.nw_tos & IP_ECN_MASK) {
1024 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
1026 if (wc->masks.nw_ttl) {
1027 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
1029 if (wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
1030 ds_put_format(s, "mpls_label=%"PRIu32",",
1031 mpls_lse_to_label(f->mpls_lse[0]));
1033 if (wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
1034 ds_put_format(s, "mpls_tc=%"PRIu8",",
1035 mpls_lse_to_tc(f->mpls_lse[0]));
1037 if (wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK)) {
1038 ds_put_format(s, "mpls_ttl=%"PRIu8",",
1039 mpls_lse_to_ttl(f->mpls_lse[0]));
1041 if (wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
1042 ds_put_format(s, "mpls_bos=%"PRIu8",",
1043 mpls_lse_to_bos(f->mpls_lse[0]));
1045 format_be32_masked(s, "mpls_lse1", f->mpls_lse[1], wc->masks.mpls_lse[1]);
1046 format_be32_masked(s, "mpls_lse2", f->mpls_lse[2], wc->masks.mpls_lse[2]);
1048 switch (wc->masks.nw_frag) {
1049 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
1050 ds_put_format(s, "nw_frag=%s,",
1051 f->nw_frag & FLOW_NW_FRAG_ANY
1052 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
1053 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
1056 case FLOW_NW_FRAG_ANY:
1057 ds_put_format(s, "nw_frag=%s,",
1058 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
1061 case FLOW_NW_FRAG_LATER:
1062 ds_put_format(s, "nw_frag=%s,",
1063 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
1066 if (f->dl_type == htons(ETH_TYPE_IP) &&
1067 f->nw_proto == IPPROTO_ICMP) {
1068 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1069 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1070 } else if (f->dl_type == htons(ETH_TYPE_IP) &&
1071 f->nw_proto == IPPROTO_IGMP) {
1072 format_be16_masked(s, "igmp_type", f->tp_src, wc->masks.tp_src);
1073 format_be16_masked(s, "igmp_code", f->tp_dst, wc->masks.tp_dst);
1074 } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
1075 f->nw_proto == IPPROTO_ICMPV6) {
1076 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1077 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1078 format_ipv6_netmask(s, "nd_target", &f->nd_target,
1079 &wc->masks.nd_target);
1080 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
1081 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
1083 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
1084 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
1086 if (is_ip_any(f) && f->nw_proto == IPPROTO_TCP && wc->masks.tcp_flags) {
1087 uint16_t mask = TCP_FLAGS(wc->masks.tcp_flags);
1089 if (mask == TCP_FLAGS(OVS_BE16_MAX)) {
1090 ds_put_cstr(s, "tcp_flags=");
1092 format_flags(s, packet_tcp_flag_to_string, ntohs(f->tcp_flags),
1095 ds_put_cstr(s, "0"); /* Zero flags. */
1098 format_flags_masked(s, "tcp_flags", packet_tcp_flag_to_string,
1099 ntohs(f->tcp_flags), mask);
1103 if (s->length > start_len && ds_last(s) == ',') {
1108 /* Converts 'match' to a string and returns the string. If 'priority' is
1109 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1110 * must free the string (with free()). */
1112 match_to_string(const struct match *match, unsigned int priority)
1114 struct ds s = DS_EMPTY_INITIALIZER;
1115 match_format(match, &s, priority);
1116 return ds_steal_cstr(&s);
1120 match_print(const struct match *match)
1122 char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
1127 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1128 * with minimatch_destroy(). */
1130 minimatch_init(struct minimatch *dst, const struct match *src)
1132 minimask_init(&dst->mask, &src->wc);
1133 miniflow_init_with_minimask(&dst->flow, &src->flow, &dst->mask);
1136 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1137 * with minimatch_destroy(). */
1139 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1141 miniflow_clone(&dst->flow, &src->flow);
1142 minimask_clone(&dst->mask, &src->mask);
1145 /* Initializes 'dst' with the data in 'src', destroying 'src'. The caller must
1146 * eventually free 'dst' with minimatch_destroy(). */
1148 minimatch_move(struct minimatch *dst, struct minimatch *src)
1150 miniflow_move(&dst->flow, &src->flow);
1151 minimask_move(&dst->mask, &src->mask);
1154 /* Frees any memory owned by 'match'. Does not free the storage in which
1155 * 'match' itself resides; the caller is responsible for that. */
1157 minimatch_destroy(struct minimatch *match)
1159 miniflow_destroy(&match->flow);
1160 minimask_destroy(&match->mask);
1163 /* Initializes 'dst' as a copy of 'src'. */
1165 minimatch_expand(const struct minimatch *src, struct match *dst)
1167 miniflow_expand(&src->flow, &dst->flow);
1168 minimask_expand(&src->mask, &dst->wc);
1171 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
1173 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1175 return (miniflow_equal(&a->flow, &b->flow)
1176 && minimask_equal(&a->mask, &b->mask));
1179 /* Returns true if 'target' satisifies 'match', that is, if each bit for which
1180 * 'match' specifies a particular value has the correct value in 'target'.
1182 * This function is equivalent to miniflow_equal_flow_in_minimask(&match->flow,
1183 * target, &match->mask) but it is faster because of the invariant that
1184 * match->flow.map and match->mask.map are the same. */
1186 minimatch_matches_flow(const struct minimatch *match,
1187 const struct flow *target)
1189 const uint32_t *target_u32 = (const uint32_t *) target;
1190 const uint32_t *flowp = miniflow_get_u32_values(&match->flow);
1191 const uint32_t *maskp = miniflow_get_u32_values(&match->mask.masks);
1194 for (map = match->flow.map; map; map = zero_rightmost_1bit(map)) {
1195 if ((*flowp++ ^ target_u32[raw_ctz(map)]) & *maskp++) {
1203 /* Appends a string representation of 'match' to 's'. If 'priority' is
1204 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1206 minimatch_format(const struct minimatch *match, struct ds *s,
1207 unsigned int priority)
1209 struct match megamatch;
1211 minimatch_expand(match, &megamatch);
1212 match_format(&megamatch, s, priority);
1215 /* Converts 'match' to a string and returns the string. If 'priority' is
1216 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1217 * must free the string (with free()). */
1219 minimatch_to_string(const struct minimatch *match, unsigned int priority)
1221 struct match megamatch;
1223 minimatch_expand(match, &megamatch);
1224 return match_to_string(&megamatch, priority);