2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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)
42 struct flow_wildcards *wc;
47 memset(&wc->masks, 0x0, sizeof wc->masks);
49 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
52 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
55 if (flow->skb_priority) {
56 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
60 memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark);
63 for (i = 0; i < FLOW_N_REGS; i++) {
65 memset(&wc->masks.regs[i], 0xff, sizeof wc->masks.regs[i]);
69 if (flow->tunnel.ip_dst) {
70 if (flow->tunnel.flags & FLOW_TNL_F_KEY) {
71 memset(&wc->masks.tunnel.tun_id, 0xff, sizeof wc->masks.tunnel.tun_id);
73 memset(&wc->masks.tunnel.ip_src, 0xff, sizeof wc->masks.tunnel.ip_src);
74 memset(&wc->masks.tunnel.ip_dst, 0xff, sizeof wc->masks.tunnel.ip_dst);
75 memset(&wc->masks.tunnel.flags, 0xff, sizeof wc->masks.tunnel.flags);
76 memset(&wc->masks.tunnel.ip_tos, 0xff, sizeof wc->masks.tunnel.ip_tos);
77 memset(&wc->masks.tunnel.ip_ttl, 0xff, sizeof wc->masks.tunnel.ip_ttl);
78 } else if (flow->tunnel.tun_id) {
79 memset(&wc->masks.tunnel.tun_id, 0xff, sizeof wc->masks.tunnel.tun_id);
82 memset(&wc->masks.metadata, 0xff, sizeof wc->masks.metadata);
83 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
84 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
85 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
86 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
88 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
89 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
90 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
91 memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
92 } else if (flow->dl_type == htons(ETH_TYPE_IP) ||
93 (flow->dl_type == htons(ETH_TYPE_ARP)) ||
94 (flow->dl_type == htons(ETH_TYPE_RARP))) {
95 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
96 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
97 } else if (eth_type_mpls(flow->dl_type)) {
98 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
101 if (flow->dl_type == htons(ETH_TYPE_ARP) ||
102 flow->dl_type == htons(ETH_TYPE_RARP)) {
103 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
104 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
107 if (is_ip_any(flow)) {
108 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
109 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
112 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
113 if (flow->nw_frag & FLOW_NW_FRAG_LATER) {
114 /* No transport layer header in later fragments. */
119 if (flow->nw_proto == IPPROTO_ICMP ||
120 flow->nw_proto == IPPROTO_ICMPV6 ||
121 (flow->tp_src || flow->tp_dst)) {
122 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
123 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
125 if (flow->nw_proto == IPPROTO_TCP) {
126 memset(&wc->masks.tcp_flags, 0xff, sizeof wc->masks.tcp_flags);
129 if (flow->nw_proto == IPPROTO_ICMPV6) {
130 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
131 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
138 /* Initializes 'match' as a "catch-all" match that matches every packet. */
140 match_init_catchall(struct match *match)
142 memset(&match->flow, 0, sizeof match->flow);
143 flow_wildcards_init_catchall(&match->wc);
146 /* For each bit or field wildcarded in 'match', sets the corresponding bit or
147 * field in 'flow' to all-0-bits. It is important to maintain this invariant
148 * in a match that might be inserted into a classifier.
150 * It is never necessary to call this function directly for a match that is
151 * initialized or modified only by match_*() functions. It is useful to
152 * restore the invariant in a match whose 'wc' member is modified by hand.
155 match_zero_wildcarded_fields(struct match *match)
157 flow_zero_wildcards(&match->flow, &match->wc);
161 match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
163 match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
167 match_set_reg_masked(struct match *match, unsigned int reg_idx,
168 uint32_t value, uint32_t mask)
170 ovs_assert(reg_idx < FLOW_N_REGS);
171 flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
172 match->flow.regs[reg_idx] = value & mask;
176 match_set_metadata(struct match *match, ovs_be64 metadata)
178 match_set_metadata_masked(match, metadata, OVS_BE64_MAX);
182 match_set_metadata_masked(struct match *match,
183 ovs_be64 metadata, ovs_be64 mask)
185 match->wc.masks.metadata = mask;
186 match->flow.metadata = metadata & mask;
190 match_set_tun_id(struct match *match, ovs_be64 tun_id)
192 match_set_tun_id_masked(match, tun_id, OVS_BE64_MAX);
196 match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
198 match->wc.masks.tunnel.tun_id = mask;
199 match->flow.tunnel.tun_id = tun_id & mask;
203 match_set_tun_src(struct match *match, ovs_be32 src)
205 match_set_tun_src_masked(match, src, OVS_BE32_MAX);
209 match_set_tun_src_masked(struct match *match, ovs_be32 src, ovs_be32 mask)
211 match->wc.masks.tunnel.ip_src = mask;
212 match->flow.tunnel.ip_src = src & mask;
216 match_set_tun_dst(struct match *match, ovs_be32 dst)
218 match_set_tun_dst_masked(match, dst, OVS_BE32_MAX);
222 match_set_tun_dst_masked(struct match *match, ovs_be32 dst, ovs_be32 mask)
224 match->wc.masks.tunnel.ip_dst = mask;
225 match->flow.tunnel.ip_dst = dst & mask;
229 match_set_tun_ttl(struct match *match, uint8_t ttl)
231 match_set_tun_ttl_masked(match, ttl, UINT8_MAX);
235 match_set_tun_ttl_masked(struct match *match, uint8_t ttl, uint8_t mask)
237 match->wc.masks.tunnel.ip_ttl = mask;
238 match->flow.tunnel.ip_ttl = ttl & mask;
242 match_set_tun_tos(struct match *match, uint8_t tos)
244 match_set_tun_tos_masked(match, tos, UINT8_MAX);
248 match_set_tun_tos_masked(struct match *match, uint8_t tos, uint8_t mask)
250 match->wc.masks.tunnel.ip_tos = mask;
251 match->flow.tunnel.ip_tos = tos & mask;
255 match_set_tun_flags(struct match *match, uint16_t flags)
257 match_set_tun_flags_masked(match, flags, UINT16_MAX);
261 match_set_tun_flags_masked(struct match *match, uint16_t flags, uint16_t mask)
263 match->wc.masks.tunnel.flags = mask;
264 match->flow.tunnel.flags = flags & mask;
268 match_set_in_port(struct match *match, ofp_port_t ofp_port)
270 match->wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
271 match->flow.in_port.ofp_port = ofp_port;
275 match_set_skb_priority(struct match *match, uint32_t skb_priority)
277 match->wc.masks.skb_priority = UINT32_MAX;
278 match->flow.skb_priority = skb_priority;
282 match_set_pkt_mark(struct match *match, uint32_t pkt_mark)
284 match_set_pkt_mark_masked(match, pkt_mark, UINT32_MAX);
288 match_set_pkt_mark_masked(struct match *match, uint32_t pkt_mark, uint32_t mask)
290 match->flow.pkt_mark = pkt_mark & mask;
291 match->wc.masks.pkt_mark = mask;
295 match_set_dl_type(struct match *match, ovs_be16 dl_type)
297 match->wc.masks.dl_type = OVS_BE16_MAX;
298 match->flow.dl_type = dl_type;
301 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
302 * exactly. 'mask_dst' is set to all 1s. */
304 set_eth(const uint8_t value_src[ETH_ADDR_LEN],
305 uint8_t value_dst[ETH_ADDR_LEN],
306 uint8_t mask_dst[ETH_ADDR_LEN])
308 memcpy(value_dst, value_src, ETH_ADDR_LEN);
309 memset(mask_dst, 0xff, ETH_ADDR_LEN);
312 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
313 * after each byte is ANDed with the appropriate byte in 'mask_src'.
314 * 'mask_dst' is set to 'mask_src' */
316 set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
317 const uint8_t mask_src[ETH_ADDR_LEN],
318 uint8_t value_dst[ETH_ADDR_LEN],
319 uint8_t mask_dst[ETH_ADDR_LEN])
323 for (i = 0; i < ETH_ADDR_LEN; i++) {
324 value_dst[i] = value_src[i] & mask_src[i];
325 mask_dst[i] = mask_src[i];
329 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
332 match_set_dl_src(struct match *match, const uint8_t dl_src[ETH_ADDR_LEN])
334 set_eth(dl_src, match->flow.dl_src, match->wc.masks.dl_src);
337 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
338 * after each byte is ANDed with the appropriate byte in 'mask'. */
340 match_set_dl_src_masked(struct match *match,
341 const uint8_t dl_src[ETH_ADDR_LEN],
342 const uint8_t mask[ETH_ADDR_LEN])
344 set_eth_masked(dl_src, mask, match->flow.dl_src, match->wc.masks.dl_src);
347 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
350 match_set_dl_dst(struct match *match, const uint8_t dl_dst[ETH_ADDR_LEN])
352 set_eth(dl_dst, match->flow.dl_dst, match->wc.masks.dl_dst);
355 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
356 * byte is ANDed with the appropriate byte in 'mask'.
358 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
359 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
361 match_set_dl_dst_masked(struct match *match,
362 const uint8_t dl_dst[ETH_ADDR_LEN],
363 const uint8_t mask[ETH_ADDR_LEN])
365 set_eth_masked(dl_dst, mask, match->flow.dl_dst, match->wc.masks.dl_dst);
369 match_set_dl_tci(struct match *match, ovs_be16 tci)
371 match_set_dl_tci_masked(match, tci, htons(0xffff));
375 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
377 match->flow.vlan_tci = tci & mask;
378 match->wc.masks.vlan_tci = mask;
381 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
382 * wildcarded, then 'match' will match a packet regardless of whether it has an
383 * 802.1Q header or not. */
385 match_set_any_vid(struct match *match)
387 if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
388 match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
389 match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
391 match_set_dl_tci_masked(match, htons(0), htons(0));
395 /* Modifies 'match' depending on 'dl_vlan':
397 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
398 * without an 802.1Q header.
400 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
401 * VID equals the low 12 bits of 'dl_vlan'.
404 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
406 flow_set_dl_vlan(&match->flow, dl_vlan);
407 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
408 match->wc.masks.vlan_tci = OVS_BE16_MAX;
410 match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
414 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
415 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
418 match_set_vlan_vid(struct match *match, ovs_be16 vid)
420 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
424 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
425 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
426 * plus CFI), with the corresponding 'mask'. */
428 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
430 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
431 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
434 flow_set_vlan_vid(&match->flow, vid & mask);
435 match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
438 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
439 * wildcarded, then 'match' will match a packet regardless of whether it has an
440 * 802.1Q header or not. */
442 match_set_any_pcp(struct match *match)
444 if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
445 match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
446 match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
448 match_set_dl_tci_masked(match, htons(0), htons(0));
452 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
453 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
455 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
457 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
458 match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
461 /* Modifies 'match' so that the MPLS label is wildcarded. */
463 match_set_any_mpls_label(struct match *match)
465 match->wc.masks.mpls_lse &= ~htonl(MPLS_LABEL_MASK);
466 flow_set_mpls_label(&match->flow, htonl(0));
469 /* Modifies 'match' so that it matches only packets with an MPLS header whose
470 * label equals the low 20 bits of 'mpls_label'. */
472 match_set_mpls_label(struct match *match, ovs_be32 mpls_label)
474 match->wc.masks.mpls_lse |= htonl(MPLS_LABEL_MASK);
475 flow_set_mpls_label(&match->flow, mpls_label);
478 /* Modifies 'match' so that the MPLS TC is wildcarded. */
480 match_set_any_mpls_tc(struct match *match)
482 match->wc.masks.mpls_lse &= ~htonl(MPLS_TC_MASK);
483 flow_set_mpls_tc(&match->flow, 0);
486 /* Modifies 'match' so that it matches only packets with an MPLS header whose
487 * Traffic Class equals the low 3 bits of 'mpls_tc'. */
489 match_set_mpls_tc(struct match *match, uint8_t mpls_tc)
491 match->wc.masks.mpls_lse |= htonl(MPLS_TC_MASK);
492 flow_set_mpls_tc(&match->flow, mpls_tc);
495 /* Modifies 'match' so that the MPLS stack flag is wildcarded. */
497 match_set_any_mpls_bos(struct match *match)
499 match->wc.masks.mpls_lse &= ~htonl(MPLS_BOS_MASK);
500 flow_set_mpls_bos(&match->flow, 0);
503 /* Modifies 'match' so that it matches only packets with an MPLS header whose
504 * Stack Flag equals the lower bit of 'mpls_bos' */
506 match_set_mpls_bos(struct match *match, uint8_t mpls_bos)
508 match->wc.masks.mpls_lse |= htonl(MPLS_BOS_MASK);
509 flow_set_mpls_bos(&match->flow, mpls_bos);
513 match_set_tp_src(struct match *match, ovs_be16 tp_src)
515 match_set_tp_src_masked(match, tp_src, OVS_BE16_MAX);
519 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
521 match->flow.tp_src = port & mask;
522 match->wc.masks.tp_src = mask;
526 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
528 match_set_tp_dst_masked(match, tp_dst, OVS_BE16_MAX);
532 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
534 match->flow.tp_dst = port & mask;
535 match->wc.masks.tp_dst = mask;
539 match_set_tcp_flags(struct match *match, ovs_be16 flags)
541 match_set_tcp_flags_masked(match, flags, OVS_BE16_MAX);
545 match_set_tcp_flags_masked(struct match *match, ovs_be16 flags, ovs_be16 mask)
547 match->flow.tcp_flags = flags & mask;
548 match->wc.masks.tcp_flags = mask;
552 match_set_nw_proto(struct match *match, uint8_t nw_proto)
554 match->flow.nw_proto = nw_proto;
555 match->wc.masks.nw_proto = UINT8_MAX;
559 match_set_nw_src(struct match *match, ovs_be32 nw_src)
561 match->flow.nw_src = nw_src;
562 match->wc.masks.nw_src = OVS_BE32_MAX;
566 match_set_nw_src_masked(struct match *match,
567 ovs_be32 nw_src, ovs_be32 mask)
569 match->flow.nw_src = nw_src & mask;
570 match->wc.masks.nw_src = mask;
574 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
576 match->flow.nw_dst = nw_dst;
577 match->wc.masks.nw_dst = OVS_BE32_MAX;
581 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
583 match->flow.nw_dst = ip & mask;
584 match->wc.masks.nw_dst = mask;
588 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
590 match->wc.masks.nw_tos |= IP_DSCP_MASK;
591 match->flow.nw_tos &= ~IP_DSCP_MASK;
592 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
596 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
598 match->wc.masks.nw_tos |= IP_ECN_MASK;
599 match->flow.nw_tos &= ~IP_ECN_MASK;
600 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
604 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
606 match->wc.masks.nw_ttl = UINT8_MAX;
607 match->flow.nw_ttl = nw_ttl;
611 match_set_nw_frag(struct match *match, uint8_t nw_frag)
613 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
614 match->flow.nw_frag = nw_frag;
618 match_set_nw_frag_masked(struct match *match,
619 uint8_t nw_frag, uint8_t mask)
621 match->flow.nw_frag = nw_frag & mask;
622 match->wc.masks.nw_frag = mask;
626 match_set_icmp_type(struct match *match, uint8_t icmp_type)
628 match_set_tp_src(match, htons(icmp_type));
632 match_set_icmp_code(struct match *match, uint8_t icmp_code)
634 match_set_tp_dst(match, htons(icmp_code));
638 match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN])
640 memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN);
641 memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN);
645 match_set_arp_sha_masked(struct match *match,
646 const uint8_t arp_sha[ETH_ADDR_LEN],
647 const uint8_t mask[ETH_ADDR_LEN])
649 set_eth_masked(arp_sha, mask,
650 match->flow.arp_sha, match->wc.masks.arp_sha);
654 match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN])
656 memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN);
657 memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN);
661 match_set_arp_tha_masked(struct match *match,
662 const uint8_t arp_tha[ETH_ADDR_LEN],
663 const uint8_t mask[ETH_ADDR_LEN])
665 set_eth_masked(arp_tha, mask,
666 match->flow.arp_tha, match->wc.masks.arp_tha);
670 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
672 match->flow.ipv6_src = *src;
673 match->wc.masks.ipv6_src = in6addr_exact;
677 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
678 const struct in6_addr *mask)
680 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
681 match->wc.masks.ipv6_src = *mask;
685 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
687 match->flow.ipv6_dst = *dst;
688 match->wc.masks.ipv6_dst = in6addr_exact;
692 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
693 const struct in6_addr *mask)
695 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
696 match->wc.masks.ipv6_dst = *mask;
700 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
702 match->wc.masks.ipv6_label = OVS_BE32_MAX;
703 match->flow.ipv6_label = ipv6_label;
708 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
711 match->flow.ipv6_label = ipv6_label & mask;
712 match->wc.masks.ipv6_label = mask;
716 match_set_nd_target(struct match *match, const struct in6_addr *target)
718 match->flow.nd_target = *target;
719 match->wc.masks.nd_target = in6addr_exact;
723 match_set_nd_target_masked(struct match *match,
724 const struct in6_addr *target,
725 const struct in6_addr *mask)
727 match->flow.nd_target = ipv6_addr_bitand(target, mask);
728 match->wc.masks.nd_target = *mask;
731 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
732 * values for fixed fields, otherwise false. */
734 match_equal(const struct match *a, const struct match *b)
736 return (flow_wildcards_equal(&a->wc, &b->wc)
737 && flow_equal(&a->flow, &b->flow));
740 /* Returns a hash value for the flow and wildcards in 'match', starting from
743 match_hash(const struct match *match, uint32_t basis)
745 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
749 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
750 const uint8_t mask[6])
752 if (!eth_addr_is_zero(mask)) {
753 ds_put_format(s, "%s=", name);
754 eth_format_masked(eth, mask, s);
760 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
764 ds_put_format(s, "%s=", name);
765 ip_format_masked(ip, netmask, s);
771 format_ipv6_netmask(struct ds *s, const char *name,
772 const struct in6_addr *addr,
773 const struct in6_addr *netmask)
775 if (!ipv6_mask_is_any(netmask)) {
776 ds_put_format(s, "%s=", name);
777 print_ipv6_masked(s, addr, netmask);
784 format_be16_masked(struct ds *s, const char *name,
785 ovs_be16 value, ovs_be16 mask)
787 if (mask != htons(0)) {
788 ds_put_format(s, "%s=", name);
789 if (mask == OVS_BE16_MAX) {
790 ds_put_format(s, "%"PRIu16, ntohs(value));
792 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
793 ntohs(value), ntohs(mask));
800 format_flow_tunnel(struct ds *s, const struct match *match)
802 const struct flow_wildcards *wc = &match->wc;
803 const struct flow_tnl *tnl = &match->flow.tunnel;
805 switch (wc->masks.tunnel.tun_id) {
809 ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(tnl->tun_id));
812 ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
814 ntohll(wc->masks.tunnel.tun_id));
817 format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
818 format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
820 if (wc->masks.tunnel.ip_tos) {
821 ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
823 if (wc->masks.tunnel.ip_ttl) {
824 ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
826 if (wc->masks.tunnel.flags) {
827 format_flags(s, flow_tun_flag_to_string, tnl->flags, '|');
832 /* Appends a string representation of 'match' to 's'. If 'priority' is
833 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
835 match_format(const struct match *match, struct ds *s, unsigned int priority)
837 const struct flow_wildcards *wc = &match->wc;
838 size_t start_len = s->length;
839 const struct flow *f = &match->flow;
840 bool skip_type = false;
841 bool skip_proto = false;
845 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 22);
847 if (priority != OFP_DEFAULT_PRIORITY) {
848 ds_put_format(s, "priority=%u,", priority);
851 switch (wc->masks.pkt_mark) {
855 ds_put_format(s, "pkt_mark=%#"PRIx32",", f->pkt_mark);
858 ds_put_format(s, "pkt_mark=%#"PRIx32"/%#"PRIx32",",
859 f->pkt_mark, wc->masks.pkt_mark);
863 if (wc->masks.skb_priority) {
864 ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority);
867 if (wc->masks.dl_type) {
869 if (f->dl_type == htons(ETH_TYPE_IP)) {
870 if (wc->masks.nw_proto) {
872 if (f->nw_proto == IPPROTO_ICMP) {
873 ds_put_cstr(s, "icmp,");
874 } else if (f->nw_proto == IPPROTO_TCP) {
875 ds_put_cstr(s, "tcp,");
876 } else if (f->nw_proto == IPPROTO_UDP) {
877 ds_put_cstr(s, "udp,");
878 } else if (f->nw_proto == IPPROTO_SCTP) {
879 ds_put_cstr(s, "sctp,");
881 ds_put_cstr(s, "ip,");
885 ds_put_cstr(s, "ip,");
887 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
888 if (wc->masks.nw_proto) {
890 if (f->nw_proto == IPPROTO_ICMPV6) {
891 ds_put_cstr(s, "icmp6,");
892 } else if (f->nw_proto == IPPROTO_TCP) {
893 ds_put_cstr(s, "tcp6,");
894 } else if (f->nw_proto == IPPROTO_UDP) {
895 ds_put_cstr(s, "udp6,");
896 } else if (f->nw_proto == IPPROTO_SCTP) {
897 ds_put_cstr(s, "sctp6,");
899 ds_put_cstr(s, "ipv6,");
903 ds_put_cstr(s, "ipv6,");
905 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
906 ds_put_cstr(s, "arp,");
907 } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
908 ds_put_cstr(s, "rarp,");
909 } else if (f->dl_type == htons(ETH_TYPE_MPLS)) {
910 ds_put_cstr(s, "mpls,");
911 } else if (f->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
912 ds_put_cstr(s, "mplsm,");
917 for (i = 0; i < FLOW_N_REGS; i++) {
918 switch (wc->masks.regs[i]) {
922 ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
925 ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
926 i, f->regs[i], wc->masks.regs[i]);
931 format_flow_tunnel(s, match);
933 switch (wc->masks.metadata) {
937 ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata));
940 ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",",
941 ntohll(f->metadata), ntohll(wc->masks.metadata));
944 if (wc->masks.in_port.ofp_port) {
945 ds_put_cstr(s, "in_port=");
946 ofputil_format_port(f->in_port.ofp_port, s);
949 if (wc->masks.vlan_tci) {
950 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
951 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
952 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
954 if (cfi && f->vlan_tci & htons(VLAN_CFI)
955 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
956 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
957 && (vid_mask || pcp_mask)) {
959 ds_put_format(s, "dl_vlan=%"PRIu16",",
960 vlan_tci_to_vid(f->vlan_tci));
963 ds_put_format(s, "dl_vlan_pcp=%d,",
964 vlan_tci_to_pcp(f->vlan_tci));
966 } else if (wc->masks.vlan_tci == htons(0xffff)) {
967 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
969 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
970 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
973 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
974 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
975 if (!skip_type && wc->masks.dl_type) {
976 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
978 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
979 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
980 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
981 if (wc->masks.ipv6_label) {
982 if (wc->masks.ipv6_label == OVS_BE32_MAX) {
983 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
984 ntohl(f->ipv6_label));
986 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
987 ntohl(f->ipv6_label),
988 ntohl(wc->masks.ipv6_label));
991 } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
992 f->dl_type == htons(ETH_TYPE_RARP)) {
993 format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
994 format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
996 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
997 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
999 if (!skip_proto && wc->masks.nw_proto) {
1000 if (f->dl_type == htons(ETH_TYPE_ARP) ||
1001 f->dl_type == htons(ETH_TYPE_RARP)) {
1002 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
1004 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
1007 if (f->dl_type == htons(ETH_TYPE_ARP) ||
1008 f->dl_type == htons(ETH_TYPE_RARP)) {
1009 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
1010 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
1012 if (wc->masks.nw_tos & IP_DSCP_MASK) {
1013 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
1015 if (wc->masks.nw_tos & IP_ECN_MASK) {
1016 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
1018 if (wc->masks.nw_ttl) {
1019 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
1021 if (wc->masks.mpls_lse & htonl(MPLS_LABEL_MASK)) {
1022 ds_put_format(s, "mpls_label=%"PRIu32",",
1023 mpls_lse_to_label(f->mpls_lse));
1025 if (wc->masks.mpls_lse & htonl(MPLS_TC_MASK)) {
1026 ds_put_format(s, "mpls_tc=%"PRIu8",",
1027 mpls_lse_to_tc(f->mpls_lse));
1029 if (wc->masks.mpls_lse & htonl(MPLS_TTL_MASK)) {
1030 ds_put_format(s, "mpls_ttl=%"PRIu8",",
1031 mpls_lse_to_ttl(f->mpls_lse));
1033 if (wc->masks.mpls_lse & htonl(MPLS_BOS_MASK)) {
1034 ds_put_format(s, "mpls_bos=%"PRIu8",",
1035 mpls_lse_to_bos(f->mpls_lse));
1037 switch (wc->masks.nw_frag) {
1038 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
1039 ds_put_format(s, "nw_frag=%s,",
1040 f->nw_frag & FLOW_NW_FRAG_ANY
1041 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
1042 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
1045 case FLOW_NW_FRAG_ANY:
1046 ds_put_format(s, "nw_frag=%s,",
1047 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
1050 case FLOW_NW_FRAG_LATER:
1051 ds_put_format(s, "nw_frag=%s,",
1052 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
1055 if (f->dl_type == htons(ETH_TYPE_IP) &&
1056 f->nw_proto == IPPROTO_ICMP) {
1057 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1058 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1059 } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
1060 f->nw_proto == IPPROTO_ICMPV6) {
1061 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1062 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1063 format_ipv6_netmask(s, "nd_target", &f->nd_target,
1064 &wc->masks.nd_target);
1065 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
1066 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
1068 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
1069 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
1071 if (is_ip_any(f) && f->nw_proto == IPPROTO_TCP && wc->masks.tcp_flags) {
1072 if (wc->masks.tcp_flags == htons(UINT16_MAX)) {
1073 ds_put_format(s, "tcp_flags=0x%03"PRIx16",", ntohs(f->tcp_flags));
1075 ds_put_format(s, "tcp_flags=0x%03"PRIx16"/0x%03"PRIx16",",
1076 ntohs(f->tcp_flags), ntohs(wc->masks.tcp_flags));
1080 if (s->length > start_len && ds_last(s) == ',') {
1085 /* Converts 'match' to a string and returns the string. If 'priority' is
1086 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1087 * must free the string (with free()). */
1089 match_to_string(const struct match *match, unsigned int priority)
1091 struct ds s = DS_EMPTY_INITIALIZER;
1092 match_format(match, &s, priority);
1093 return ds_steal_cstr(&s);
1097 match_print(const struct match *match)
1099 char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
1104 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1105 * with minimatch_destroy(). */
1107 minimatch_init(struct minimatch *dst, const struct match *src)
1109 minimask_init(&dst->mask, &src->wc);
1110 miniflow_init_with_minimask(&dst->flow, &src->flow, &dst->mask);
1113 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1114 * with minimatch_destroy(). */
1116 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1118 miniflow_clone(&dst->flow, &src->flow);
1119 minimask_clone(&dst->mask, &src->mask);
1122 /* Initializes 'dst' with the data in 'src', destroying 'src'. The caller must
1123 * eventually free 'dst' with minimatch_destroy(). */
1125 minimatch_move(struct minimatch *dst, struct minimatch *src)
1127 miniflow_move(&dst->flow, &src->flow);
1128 minimask_move(&dst->mask, &src->mask);
1131 /* Frees any memory owned by 'match'. Does not free the storage in which
1132 * 'match' itself resides; the caller is responsible for that. */
1134 minimatch_destroy(struct minimatch *match)
1136 miniflow_destroy(&match->flow);
1137 minimask_destroy(&match->mask);
1140 /* Initializes 'dst' as a copy of 'src'. */
1142 minimatch_expand(const struct minimatch *src, struct match *dst)
1144 miniflow_expand(&src->flow, &dst->flow);
1145 minimask_expand(&src->mask, &dst->wc);
1148 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
1150 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1152 return (miniflow_equal(&a->flow, &b->flow)
1153 && minimask_equal(&a->mask, &b->mask));
1156 /* Returns a hash value for 'match', given 'basis'. */
1158 minimatch_hash(const struct minimatch *match, uint32_t basis)
1160 return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
1163 /* Returns true if 'target' satisifies 'match', that is, if each bit for which
1164 * 'match' specifies a particular value has the correct value in 'target'.
1166 * This function is equivalent to miniflow_equal_flow_in_minimask(&match->flow,
1167 * target, &match->mask) but it is faster because of the invariant that
1168 * match->flow.map and match->mask.map are the same. */
1170 minimatch_matches_flow(const struct minimatch *match,
1171 const struct flow *target)
1173 const uint32_t *target_u32 = (const uint32_t *) target;
1174 const uint32_t *flowp = match->flow.values;
1175 const uint32_t *maskp = match->mask.masks.values;
1178 for (i = 0; i < MINI_N_MAPS; i++) {
1181 for (map = match->flow.map[i]; map; map = zero_rightmost_1bit(map)) {
1182 if ((*flowp++ ^ target_u32[raw_ctz(map)]) & *maskp++) {
1192 /* Appends a string representation of 'match' to 's'. If 'priority' is
1193 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1195 minimatch_format(const struct minimatch *match, struct ds *s,
1196 unsigned int priority)
1198 struct match megamatch;
1200 minimatch_expand(match, &megamatch);
1201 match_format(&megamatch, s, priority);
1204 /* Converts 'match' to a string and returns the string. If 'priority' is
1205 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1206 * must free the string (with free()). */
1208 minimatch_to_string(const struct minimatch *match, unsigned int priority)
1210 struct match megamatch;
1212 minimatch_expand(match, &megamatch);
1213 return match_to_string(&megamatch, priority);