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_actset_output(struct match
*match
, ofp_port_t actset_output
)
124 match
->wc
.masks
.actset_output
= u16_to_ofp(UINT16_MAX
);
125 match
->flow
.actset_output
= actset_output
;
129 match_set_metadata(struct match
*match
, ovs_be64 metadata
)
131 match_set_metadata_masked(match
, metadata
, OVS_BE64_MAX
);
135 match_set_metadata_masked(struct match
*match
,
136 ovs_be64 metadata
, ovs_be64 mask
)
138 match
->wc
.masks
.metadata
= mask
;
139 match
->flow
.metadata
= metadata
& mask
;
143 match_set_tun_id(struct match
*match
, ovs_be64 tun_id
)
145 match_set_tun_id_masked(match
, tun_id
, OVS_BE64_MAX
);
149 match_set_tun_id_masked(struct match
*match
, ovs_be64 tun_id
, ovs_be64 mask
)
151 match
->wc
.masks
.tunnel
.tun_id
= mask
;
152 match
->flow
.tunnel
.tun_id
= tun_id
& mask
;
156 match_set_tun_src(struct match
*match
, ovs_be32 src
)
158 match_set_tun_src_masked(match
, src
, OVS_BE32_MAX
);
162 match_set_tun_src_masked(struct match
*match
, ovs_be32 src
, ovs_be32 mask
)
164 match
->wc
.masks
.tunnel
.ip_src
= mask
;
165 match
->flow
.tunnel
.ip_src
= src
& mask
;
169 match_set_tun_dst(struct match
*match
, ovs_be32 dst
)
171 match_set_tun_dst_masked(match
, dst
, OVS_BE32_MAX
);
175 match_set_tun_dst_masked(struct match
*match
, ovs_be32 dst
, ovs_be32 mask
)
177 match
->wc
.masks
.tunnel
.ip_dst
= mask
;
178 match
->flow
.tunnel
.ip_dst
= dst
& mask
;
182 match_set_tun_ttl(struct match
*match
, uint8_t ttl
)
184 match_set_tun_ttl_masked(match
, ttl
, UINT8_MAX
);
188 match_set_tun_ttl_masked(struct match
*match
, uint8_t ttl
, uint8_t mask
)
190 match
->wc
.masks
.tunnel
.ip_ttl
= mask
;
191 match
->flow
.tunnel
.ip_ttl
= ttl
& mask
;
195 match_set_tun_tos(struct match
*match
, uint8_t tos
)
197 match_set_tun_tos_masked(match
, tos
, UINT8_MAX
);
201 match_set_tun_tos_masked(struct match
*match
, uint8_t tos
, uint8_t mask
)
203 match
->wc
.masks
.tunnel
.ip_tos
= mask
;
204 match
->flow
.tunnel
.ip_tos
= tos
& mask
;
208 match_set_tun_flags(struct match
*match
, uint16_t flags
)
210 match_set_tun_flags_masked(match
, flags
, UINT16_MAX
);
214 match_set_tun_flags_masked(struct match
*match
, uint16_t flags
, uint16_t mask
)
216 match
->wc
.masks
.tunnel
.flags
= mask
;
217 match
->flow
.tunnel
.flags
= flags
& mask
;
221 match_set_in_port(struct match
*match
, ofp_port_t ofp_port
)
223 match
->wc
.masks
.in_port
.ofp_port
= u16_to_ofp(UINT16_MAX
);
224 match
->flow
.in_port
.ofp_port
= ofp_port
;
228 match_set_skb_priority(struct match
*match
, uint32_t skb_priority
)
230 match
->wc
.masks
.skb_priority
= UINT32_MAX
;
231 match
->flow
.skb_priority
= skb_priority
;
235 match_set_pkt_mark(struct match
*match
, uint32_t pkt_mark
)
237 match_set_pkt_mark_masked(match
, pkt_mark
, UINT32_MAX
);
241 match_set_pkt_mark_masked(struct match
*match
, uint32_t pkt_mark
, uint32_t mask
)
243 match
->flow
.pkt_mark
= pkt_mark
& mask
;
244 match
->wc
.masks
.pkt_mark
= mask
;
248 match_set_dl_type(struct match
*match
, ovs_be16 dl_type
)
250 match
->wc
.masks
.dl_type
= OVS_BE16_MAX
;
251 match
->flow
.dl_type
= dl_type
;
254 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
255 * exactly. 'mask_dst' is set to all 1s. */
257 set_eth(const uint8_t value_src
[ETH_ADDR_LEN
],
258 uint8_t value_dst
[ETH_ADDR_LEN
],
259 uint8_t mask_dst
[ETH_ADDR_LEN
])
261 memcpy(value_dst
, value_src
, ETH_ADDR_LEN
);
262 memset(mask_dst
, 0xff, ETH_ADDR_LEN
);
265 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
266 * after each byte is ANDed with the appropriate byte in 'mask_src'.
267 * 'mask_dst' is set to 'mask_src' */
269 set_eth_masked(const uint8_t value_src
[ETH_ADDR_LEN
],
270 const uint8_t mask_src
[ETH_ADDR_LEN
],
271 uint8_t value_dst
[ETH_ADDR_LEN
],
272 uint8_t mask_dst
[ETH_ADDR_LEN
])
276 for (i
= 0; i
< ETH_ADDR_LEN
; i
++) {
277 value_dst
[i
] = value_src
[i
] & mask_src
[i
];
278 mask_dst
[i
] = mask_src
[i
];
282 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
285 match_set_dl_src(struct match
*match
, const uint8_t dl_src
[ETH_ADDR_LEN
])
287 set_eth(dl_src
, match
->flow
.dl_src
, match
->wc
.masks
.dl_src
);
290 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
291 * after each byte is ANDed with the appropriate byte in 'mask'. */
293 match_set_dl_src_masked(struct match
*match
,
294 const uint8_t dl_src
[ETH_ADDR_LEN
],
295 const uint8_t mask
[ETH_ADDR_LEN
])
297 set_eth_masked(dl_src
, mask
, match
->flow
.dl_src
, match
->wc
.masks
.dl_src
);
300 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
303 match_set_dl_dst(struct match
*match
, const uint8_t dl_dst
[ETH_ADDR_LEN
])
305 set_eth(dl_dst
, match
->flow
.dl_dst
, match
->wc
.masks
.dl_dst
);
308 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
309 * byte is ANDed with the appropriate byte in 'mask'.
311 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
312 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
314 match_set_dl_dst_masked(struct match
*match
,
315 const uint8_t dl_dst
[ETH_ADDR_LEN
],
316 const uint8_t mask
[ETH_ADDR_LEN
])
318 set_eth_masked(dl_dst
, mask
, match
->flow
.dl_dst
, match
->wc
.masks
.dl_dst
);
322 match_set_dl_tci(struct match
*match
, ovs_be16 tci
)
324 match_set_dl_tci_masked(match
, tci
, htons(0xffff));
328 match_set_dl_tci_masked(struct match
*match
, ovs_be16 tci
, ovs_be16 mask
)
330 match
->flow
.vlan_tci
= tci
& mask
;
331 match
->wc
.masks
.vlan_tci
= mask
;
334 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
335 * wildcarded, then 'match' will match a packet regardless of whether it has an
336 * 802.1Q header or not. */
338 match_set_any_vid(struct match
*match
)
340 if (match
->wc
.masks
.vlan_tci
& htons(VLAN_PCP_MASK
)) {
341 match
->wc
.masks
.vlan_tci
&= ~htons(VLAN_VID_MASK
);
342 match
->flow
.vlan_tci
&= ~htons(VLAN_VID_MASK
);
344 match_set_dl_tci_masked(match
, htons(0), htons(0));
348 /* Modifies 'match' depending on 'dl_vlan':
350 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
351 * without an 802.1Q header.
353 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
354 * VID equals the low 12 bits of 'dl_vlan'.
357 match_set_dl_vlan(struct match
*match
, ovs_be16 dl_vlan
)
359 flow_set_dl_vlan(&match
->flow
, dl_vlan
);
360 if (dl_vlan
== htons(OFP10_VLAN_NONE
)) {
361 match
->wc
.masks
.vlan_tci
= OVS_BE16_MAX
;
363 match
->wc
.masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
367 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
368 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
371 match_set_vlan_vid(struct match
*match
, ovs_be16 vid
)
373 match_set_vlan_vid_masked(match
, vid
, htons(VLAN_VID_MASK
| VLAN_CFI
));
377 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
378 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
379 * plus CFI), with the corresponding 'mask'. */
381 match_set_vlan_vid_masked(struct match
*match
, ovs_be16 vid
, ovs_be16 mask
)
383 ovs_be16 pcp_mask
= htons(VLAN_PCP_MASK
);
384 ovs_be16 vid_mask
= htons(VLAN_VID_MASK
| VLAN_CFI
);
387 flow_set_vlan_vid(&match
->flow
, vid
& mask
);
388 match
->wc
.masks
.vlan_tci
= mask
| (match
->wc
.masks
.vlan_tci
& pcp_mask
);
391 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
392 * wildcarded, then 'match' will match a packet regardless of whether it has an
393 * 802.1Q header or not. */
395 match_set_any_pcp(struct match
*match
)
397 if (match
->wc
.masks
.vlan_tci
& htons(VLAN_VID_MASK
)) {
398 match
->wc
.masks
.vlan_tci
&= ~htons(VLAN_PCP_MASK
);
399 match
->flow
.vlan_tci
&= ~htons(VLAN_PCP_MASK
);
401 match_set_dl_tci_masked(match
, htons(0), htons(0));
405 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
406 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
408 match_set_dl_vlan_pcp(struct match
*match
, uint8_t dl_vlan_pcp
)
410 flow_set_vlan_pcp(&match
->flow
, dl_vlan_pcp
);
411 match
->wc
.masks
.vlan_tci
|= htons(VLAN_CFI
| VLAN_PCP_MASK
);
414 /* Modifies 'match' so that the MPLS label 'idx' matches 'lse' exactly. */
416 match_set_mpls_lse(struct match
*match
, int idx
, ovs_be32 lse
)
418 match
->wc
.masks
.mpls_lse
[idx
] = OVS_BE32_MAX
;
419 match
->flow
.mpls_lse
[idx
] = lse
;
422 /* Modifies 'match' so that the MPLS label is wildcarded. */
424 match_set_any_mpls_label(struct match
*match
, int idx
)
426 match
->wc
.masks
.mpls_lse
[idx
] &= ~htonl(MPLS_LABEL_MASK
);
427 flow_set_mpls_label(&match
->flow
, idx
, htonl(0));
430 /* Modifies 'match' so that it matches only packets with an MPLS header whose
431 * label equals the low 20 bits of 'mpls_label'. */
433 match_set_mpls_label(struct match
*match
, int idx
, ovs_be32 mpls_label
)
435 match
->wc
.masks
.mpls_lse
[idx
] |= htonl(MPLS_LABEL_MASK
);
436 flow_set_mpls_label(&match
->flow
, idx
, mpls_label
);
439 /* Modifies 'match' so that the MPLS TC is wildcarded. */
441 match_set_any_mpls_tc(struct match
*match
, int idx
)
443 match
->wc
.masks
.mpls_lse
[idx
] &= ~htonl(MPLS_TC_MASK
);
444 flow_set_mpls_tc(&match
->flow
, idx
, 0);
447 /* Modifies 'match' so that it matches only packets with an MPLS header whose
448 * Traffic Class equals the low 3 bits of 'mpls_tc'. */
450 match_set_mpls_tc(struct match
*match
, int idx
, uint8_t mpls_tc
)
452 match
->wc
.masks
.mpls_lse
[idx
] |= htonl(MPLS_TC_MASK
);
453 flow_set_mpls_tc(&match
->flow
, idx
, mpls_tc
);
456 /* Modifies 'match' so that the MPLS stack flag is wildcarded. */
458 match_set_any_mpls_bos(struct match
*match
, int idx
)
460 match
->wc
.masks
.mpls_lse
[idx
] &= ~htonl(MPLS_BOS_MASK
);
461 flow_set_mpls_bos(&match
->flow
, idx
, 0);
464 /* Modifies 'match' so that it matches only packets with an MPLS header whose
465 * Stack Flag equals the lower bit of 'mpls_bos' */
467 match_set_mpls_bos(struct match
*match
, int idx
, uint8_t mpls_bos
)
469 match
->wc
.masks
.mpls_lse
[idx
] |= htonl(MPLS_BOS_MASK
);
470 flow_set_mpls_bos(&match
->flow
, idx
, mpls_bos
);
473 /* Modifies 'match' so that the MPLS LSE is wildcarded. */
475 match_set_any_mpls_lse(struct match
*match
, int idx
)
477 match
->wc
.masks
.mpls_lse
[idx
] = htonl(0);
478 flow_set_mpls_lse(&match
->flow
, idx
, htonl(0));
482 match_set_tp_src(struct match
*match
, ovs_be16 tp_src
)
484 match_set_tp_src_masked(match
, tp_src
, OVS_BE16_MAX
);
488 match_set_tp_src_masked(struct match
*match
, ovs_be16 port
, ovs_be16 mask
)
490 match
->flow
.tp_src
= port
& mask
;
491 match
->wc
.masks
.tp_src
= mask
;
495 match_set_tp_dst(struct match
*match
, ovs_be16 tp_dst
)
497 match_set_tp_dst_masked(match
, tp_dst
, OVS_BE16_MAX
);
501 match_set_tp_dst_masked(struct match
*match
, ovs_be16 port
, ovs_be16 mask
)
503 match
->flow
.tp_dst
= port
& mask
;
504 match
->wc
.masks
.tp_dst
= mask
;
508 match_set_tcp_flags(struct match
*match
, ovs_be16 flags
)
510 match_set_tcp_flags_masked(match
, flags
, OVS_BE16_MAX
);
514 match_set_tcp_flags_masked(struct match
*match
, ovs_be16 flags
, ovs_be16 mask
)
516 match
->flow
.tcp_flags
= flags
& mask
;
517 match
->wc
.masks
.tcp_flags
= mask
;
521 match_set_nw_proto(struct match
*match
, uint8_t nw_proto
)
523 match
->flow
.nw_proto
= nw_proto
;
524 match
->wc
.masks
.nw_proto
= UINT8_MAX
;
528 match_set_nw_src(struct match
*match
, ovs_be32 nw_src
)
530 match
->flow
.nw_src
= nw_src
;
531 match
->wc
.masks
.nw_src
= OVS_BE32_MAX
;
535 match_set_nw_src_masked(struct match
*match
,
536 ovs_be32 nw_src
, ovs_be32 mask
)
538 match
->flow
.nw_src
= nw_src
& mask
;
539 match
->wc
.masks
.nw_src
= mask
;
543 match_set_nw_dst(struct match
*match
, ovs_be32 nw_dst
)
545 match
->flow
.nw_dst
= nw_dst
;
546 match
->wc
.masks
.nw_dst
= OVS_BE32_MAX
;
550 match_set_nw_dst_masked(struct match
*match
, ovs_be32 ip
, ovs_be32 mask
)
552 match
->flow
.nw_dst
= ip
& mask
;
553 match
->wc
.masks
.nw_dst
= mask
;
557 match_set_nw_dscp(struct match
*match
, uint8_t nw_dscp
)
559 match
->wc
.masks
.nw_tos
|= IP_DSCP_MASK
;
560 match
->flow
.nw_tos
&= ~IP_DSCP_MASK
;
561 match
->flow
.nw_tos
|= nw_dscp
& IP_DSCP_MASK
;
565 match_set_nw_ecn(struct match
*match
, uint8_t nw_ecn
)
567 match
->wc
.masks
.nw_tos
|= IP_ECN_MASK
;
568 match
->flow
.nw_tos
&= ~IP_ECN_MASK
;
569 match
->flow
.nw_tos
|= nw_ecn
& IP_ECN_MASK
;
573 match_set_nw_ttl(struct match
*match
, uint8_t nw_ttl
)
575 match
->wc
.masks
.nw_ttl
= UINT8_MAX
;
576 match
->flow
.nw_ttl
= nw_ttl
;
580 match_set_nw_frag(struct match
*match
, uint8_t nw_frag
)
582 match
->wc
.masks
.nw_frag
|= FLOW_NW_FRAG_MASK
;
583 match
->flow
.nw_frag
= nw_frag
;
587 match_set_nw_frag_masked(struct match
*match
,
588 uint8_t nw_frag
, uint8_t mask
)
590 match
->flow
.nw_frag
= nw_frag
& mask
;
591 match
->wc
.masks
.nw_frag
= mask
;
595 match_set_icmp_type(struct match
*match
, uint8_t icmp_type
)
597 match_set_tp_src(match
, htons(icmp_type
));
601 match_set_icmp_code(struct match
*match
, uint8_t icmp_code
)
603 match_set_tp_dst(match
, htons(icmp_code
));
607 match_set_arp_sha(struct match
*match
, const uint8_t sha
[ETH_ADDR_LEN
])
609 memcpy(match
->flow
.arp_sha
, sha
, ETH_ADDR_LEN
);
610 memset(match
->wc
.masks
.arp_sha
, UINT8_MAX
, ETH_ADDR_LEN
);
614 match_set_arp_sha_masked(struct match
*match
,
615 const uint8_t arp_sha
[ETH_ADDR_LEN
],
616 const uint8_t mask
[ETH_ADDR_LEN
])
618 set_eth_masked(arp_sha
, mask
,
619 match
->flow
.arp_sha
, match
->wc
.masks
.arp_sha
);
623 match_set_arp_tha(struct match
*match
, const uint8_t tha
[ETH_ADDR_LEN
])
625 memcpy(match
->flow
.arp_tha
, tha
, ETH_ADDR_LEN
);
626 memset(match
->wc
.masks
.arp_tha
, UINT8_MAX
, ETH_ADDR_LEN
);
630 match_set_arp_tha_masked(struct match
*match
,
631 const uint8_t arp_tha
[ETH_ADDR_LEN
],
632 const uint8_t mask
[ETH_ADDR_LEN
])
634 set_eth_masked(arp_tha
, mask
,
635 match
->flow
.arp_tha
, match
->wc
.masks
.arp_tha
);
639 match_set_ipv6_src(struct match
*match
, const struct in6_addr
*src
)
641 match
->flow
.ipv6_src
= *src
;
642 match
->wc
.masks
.ipv6_src
= in6addr_exact
;
646 match_set_ipv6_src_masked(struct match
*match
, const struct in6_addr
*src
,
647 const struct in6_addr
*mask
)
649 match
->flow
.ipv6_src
= ipv6_addr_bitand(src
, mask
);
650 match
->wc
.masks
.ipv6_src
= *mask
;
654 match_set_ipv6_dst(struct match
*match
, const struct in6_addr
*dst
)
656 match
->flow
.ipv6_dst
= *dst
;
657 match
->wc
.masks
.ipv6_dst
= in6addr_exact
;
661 match_set_ipv6_dst_masked(struct match
*match
, const struct in6_addr
*dst
,
662 const struct in6_addr
*mask
)
664 match
->flow
.ipv6_dst
= ipv6_addr_bitand(dst
, mask
);
665 match
->wc
.masks
.ipv6_dst
= *mask
;
669 match_set_ipv6_label(struct match
*match
, ovs_be32 ipv6_label
)
671 match
->wc
.masks
.ipv6_label
= OVS_BE32_MAX
;
672 match
->flow
.ipv6_label
= ipv6_label
;
677 match_set_ipv6_label_masked(struct match
*match
, ovs_be32 ipv6_label
,
680 match
->flow
.ipv6_label
= ipv6_label
& mask
;
681 match
->wc
.masks
.ipv6_label
= mask
;
685 match_set_nd_target(struct match
*match
, const struct in6_addr
*target
)
687 match
->flow
.nd_target
= *target
;
688 match
->wc
.masks
.nd_target
= in6addr_exact
;
692 match_set_nd_target_masked(struct match
*match
,
693 const struct in6_addr
*target
,
694 const struct in6_addr
*mask
)
696 match
->flow
.nd_target
= ipv6_addr_bitand(target
, mask
);
697 match
->wc
.masks
.nd_target
= *mask
;
700 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
701 * values for fixed fields, otherwise false. */
703 match_equal(const struct match
*a
, const struct match
*b
)
705 return (flow_wildcards_equal(&a
->wc
, &b
->wc
)
706 && flow_equal(&a
->flow
, &b
->flow
));
709 /* Returns a hash value for the flow and wildcards in 'match', starting from
712 match_hash(const struct match
*match
, uint32_t basis
)
714 return flow_wildcards_hash(&match
->wc
, flow_hash(&match
->flow
, basis
));
718 match_has_default_recirc_id(const struct match
*m
)
720 return m
->flow
.recirc_id
== 0 && (m
->wc
.masks
.recirc_id
== UINT32_MAX
||
721 m
->wc
.masks
.recirc_id
== 0);
725 match_has_default_dp_hash(const struct match
*m
)
727 return ((m
->flow
.dp_hash
| m
->wc
.masks
.dp_hash
) == 0);
730 /* Return true if the hidden fields of the match are set to the default values.
731 * The default values equals to those set up by match_init_hidden_fields(). */
733 match_has_default_hidden_fields(const struct match
*m
)
735 return match_has_default_recirc_id(m
) && match_has_default_dp_hash(m
);
739 match_init_hidden_fields(struct match
*m
)
741 match_set_recirc_id(m
, 0);
742 match_set_dp_hash_masked(m
, 0, 0);
746 format_eth_masked(struct ds
*s
, const char *name
,
747 const uint8_t eth
[ETH_ADDR_LEN
],
748 const uint8_t mask
[ETH_ADDR_LEN
])
750 if (!eth_addr_is_zero(mask
)) {
751 ds_put_format(s
, "%s=", name
);
752 eth_format_masked(eth
, mask
, s
);
758 format_ip_netmask(struct ds
*s
, const char *name
, ovs_be32 ip
,
762 ds_put_format(s
, "%s=", name
);
763 ip_format_masked(ip
, netmask
, s
);
769 format_ipv6_netmask(struct ds
*s
, const char *name
,
770 const struct in6_addr
*addr
,
771 const struct in6_addr
*netmask
)
773 if (!ipv6_mask_is_any(netmask
)) {
774 ds_put_format(s
, "%s=", name
);
775 print_ipv6_masked(s
, addr
, netmask
);
781 format_be16_masked(struct ds
*s
, const char *name
,
782 ovs_be16 value
, ovs_be16 mask
)
784 if (mask
!= htons(0)) {
785 ds_put_format(s
, "%s=", name
);
786 if (mask
== OVS_BE16_MAX
) {
787 ds_put_format(s
, "%"PRIu16
, ntohs(value
));
789 ds_put_format(s
, "0x%"PRIx16
"/0x%"PRIx16
,
790 ntohs(value
), ntohs(mask
));
797 format_be32_masked(struct ds
*s
, const char *name
,
798 ovs_be32 value
, ovs_be32 mask
)
800 if (mask
!= htonl(0)) {
801 ds_put_format(s
, "%s=", name
);
802 if (mask
== OVS_BE32_MAX
) {
803 ds_put_format(s
, "%"PRIu32
, ntohl(value
));
805 ds_put_format(s
, "0x%"PRIx32
"/0x%"PRIx32
,
806 ntohl(value
), ntohl(mask
));
813 format_uint32_masked(struct ds
*s
, const char *name
,
814 uint32_t value
, uint32_t mask
)
817 ds_put_format(s
, "%s=%#"PRIx32
, name
, value
);
818 if (mask
!= UINT32_MAX
) {
819 ds_put_format(s
, "/%#"PRIx32
, mask
);
826 format_be64_masked(struct ds
*s
, const char *name
,
827 ovs_be64 value
, ovs_be64 mask
)
829 if (mask
!= htonll(0)) {
830 ds_put_format(s
, "%s=%#"PRIx64
, name
, ntohll(value
));
831 if (mask
!= OVS_BE64_MAX
) {
832 ds_put_format(s
, "/%#"PRIx64
, ntohll(mask
));
839 format_flow_tunnel(struct ds
*s
, const struct match
*match
)
841 const struct flow_wildcards
*wc
= &match
->wc
;
842 const struct flow_tnl
*tnl
= &match
->flow
.tunnel
;
844 format_be64_masked(s
, "tun_id", tnl
->tun_id
, wc
->masks
.tunnel
.tun_id
);
845 format_ip_netmask(s
, "tun_src", tnl
->ip_src
, wc
->masks
.tunnel
.ip_src
);
846 format_ip_netmask(s
, "tun_dst", tnl
->ip_dst
, wc
->masks
.tunnel
.ip_dst
);
848 if (wc
->masks
.tunnel
.ip_tos
) {
849 ds_put_format(s
, "tun_tos=%"PRIx8
",", tnl
->ip_tos
);
851 if (wc
->masks
.tunnel
.ip_ttl
) {
852 ds_put_format(s
, "tun_ttl=%"PRIu8
",", tnl
->ip_ttl
);
854 if (wc
->masks
.tunnel
.flags
) {
855 format_flags(s
, flow_tun_flag_to_string
, tnl
->flags
, '|');
860 /* Appends a string representation of 'match' to 's'. If 'priority' is
861 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
863 match_format(const struct match
*match
, struct ds
*s
, int priority
)
865 const struct flow_wildcards
*wc
= &match
->wc
;
866 size_t start_len
= s
->length
;
867 const struct flow
*f
= &match
->flow
;
868 bool skip_type
= false;
869 bool skip_proto
= false;
873 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 28);
875 if (priority
!= OFP_DEFAULT_PRIORITY
) {
876 ds_put_format(s
, "priority=%d,", priority
);
879 format_uint32_masked(s
, "pkt_mark", f
->pkt_mark
, wc
->masks
.pkt_mark
);
881 if (wc
->masks
.recirc_id
) {
882 format_uint32_masked(s
, "recirc_id", f
->recirc_id
,
883 wc
->masks
.recirc_id
);
886 if (f
->dp_hash
&& wc
->masks
.dp_hash
) {
887 format_uint32_masked(s
, "dp_hash", f
->dp_hash
,
891 if (wc
->masks
.skb_priority
) {
892 ds_put_format(s
, "skb_priority=%#"PRIx32
",", f
->skb_priority
);
895 if (wc
->masks
.actset_output
) {
896 ds_put_cstr(s
, "actset_output=");
897 ofputil_format_port(f
->actset_output
, s
);
901 if (wc
->masks
.dl_type
) {
903 if (f
->dl_type
== htons(ETH_TYPE_IP
)) {
904 if (wc
->masks
.nw_proto
) {
906 if (f
->nw_proto
== IPPROTO_ICMP
) {
907 ds_put_cstr(s
, "icmp,");
908 } else if (f
->nw_proto
== IPPROTO_IGMP
) {
909 ds_put_cstr(s
, "igmp,");
910 } else if (f
->nw_proto
== IPPROTO_TCP
) {
911 ds_put_cstr(s
, "tcp,");
912 } else if (f
->nw_proto
== IPPROTO_UDP
) {
913 ds_put_cstr(s
, "udp,");
914 } else if (f
->nw_proto
== IPPROTO_SCTP
) {
915 ds_put_cstr(s
, "sctp,");
917 ds_put_cstr(s
, "ip,");
921 ds_put_cstr(s
, "ip,");
923 } else if (f
->dl_type
== htons(ETH_TYPE_IPV6
)) {
924 if (wc
->masks
.nw_proto
) {
926 if (f
->nw_proto
== IPPROTO_ICMPV6
) {
927 ds_put_cstr(s
, "icmp6,");
928 } else if (f
->nw_proto
== IPPROTO_TCP
) {
929 ds_put_cstr(s
, "tcp6,");
930 } else if (f
->nw_proto
== IPPROTO_UDP
) {
931 ds_put_cstr(s
, "udp6,");
932 } else if (f
->nw_proto
== IPPROTO_SCTP
) {
933 ds_put_cstr(s
, "sctp6,");
935 ds_put_cstr(s
, "ipv6,");
939 ds_put_cstr(s
, "ipv6,");
941 } else if (f
->dl_type
== htons(ETH_TYPE_ARP
)) {
942 ds_put_cstr(s
, "arp,");
943 } else if (f
->dl_type
== htons(ETH_TYPE_RARP
)) {
944 ds_put_cstr(s
, "rarp,");
945 } else if (f
->dl_type
== htons(ETH_TYPE_MPLS
)) {
946 ds_put_cstr(s
, "mpls,");
947 } else if (f
->dl_type
== htons(ETH_TYPE_MPLS_MCAST
)) {
948 ds_put_cstr(s
, "mplsm,");
953 for (i
= 0; i
< FLOW_N_REGS
; i
++) {
954 #define REGNAME_LEN 20
955 char regname
[REGNAME_LEN
];
956 if (snprintf(regname
, REGNAME_LEN
, "reg%d", i
) >= REGNAME_LEN
) {
957 strcpy(regname
, "reg?");
959 format_uint32_masked(s
, regname
, f
->regs
[i
], wc
->masks
.regs
[i
]);
962 format_flow_tunnel(s
, match
);
964 format_be64_masked(s
, "metadata", f
->metadata
, wc
->masks
.metadata
);
966 if (wc
->masks
.in_port
.ofp_port
) {
967 ds_put_cstr(s
, "in_port=");
968 ofputil_format_port(f
->in_port
.ofp_port
, s
);
971 if (wc
->masks
.vlan_tci
) {
972 ovs_be16 vid_mask
= wc
->masks
.vlan_tci
& htons(VLAN_VID_MASK
);
973 ovs_be16 pcp_mask
= wc
->masks
.vlan_tci
& htons(VLAN_PCP_MASK
);
974 ovs_be16 cfi
= wc
->masks
.vlan_tci
& htons(VLAN_CFI
);
976 if (cfi
&& f
->vlan_tci
& htons(VLAN_CFI
)
977 && (!vid_mask
|| vid_mask
== htons(VLAN_VID_MASK
))
978 && (!pcp_mask
|| pcp_mask
== htons(VLAN_PCP_MASK
))
979 && (vid_mask
|| pcp_mask
)) {
981 ds_put_format(s
, "dl_vlan=%"PRIu16
",",
982 vlan_tci_to_vid(f
->vlan_tci
));
985 ds_put_format(s
, "dl_vlan_pcp=%d,",
986 vlan_tci_to_pcp(f
->vlan_tci
));
988 } else if (wc
->masks
.vlan_tci
== htons(0xffff)) {
989 ds_put_format(s
, "vlan_tci=0x%04"PRIx16
",", ntohs(f
->vlan_tci
));
991 ds_put_format(s
, "vlan_tci=0x%04"PRIx16
"/0x%04"PRIx16
",",
992 ntohs(f
->vlan_tci
), ntohs(wc
->masks
.vlan_tci
));
995 format_eth_masked(s
, "dl_src", f
->dl_src
, wc
->masks
.dl_src
);
996 format_eth_masked(s
, "dl_dst", f
->dl_dst
, wc
->masks
.dl_dst
);
997 if (!skip_type
&& wc
->masks
.dl_type
) {
998 ds_put_format(s
, "dl_type=0x%04"PRIx16
",", ntohs(f
->dl_type
));
1000 if (f
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1001 format_ipv6_netmask(s
, "ipv6_src", &f
->ipv6_src
, &wc
->masks
.ipv6_src
);
1002 format_ipv6_netmask(s
, "ipv6_dst", &f
->ipv6_dst
, &wc
->masks
.ipv6_dst
);
1003 if (wc
->masks
.ipv6_label
) {
1004 if (wc
->masks
.ipv6_label
== OVS_BE32_MAX
) {
1005 ds_put_format(s
, "ipv6_label=0x%05"PRIx32
",",
1006 ntohl(f
->ipv6_label
));
1008 ds_put_format(s
, "ipv6_label=0x%05"PRIx32
"/0x%05"PRIx32
",",
1009 ntohl(f
->ipv6_label
),
1010 ntohl(wc
->masks
.ipv6_label
));
1013 } else if (f
->dl_type
== htons(ETH_TYPE_ARP
) ||
1014 f
->dl_type
== htons(ETH_TYPE_RARP
)) {
1015 format_ip_netmask(s
, "arp_spa", f
->nw_src
, wc
->masks
.nw_src
);
1016 format_ip_netmask(s
, "arp_tpa", f
->nw_dst
, wc
->masks
.nw_dst
);
1018 format_ip_netmask(s
, "nw_src", f
->nw_src
, wc
->masks
.nw_src
);
1019 format_ip_netmask(s
, "nw_dst", f
->nw_dst
, wc
->masks
.nw_dst
);
1021 if (!skip_proto
&& wc
->masks
.nw_proto
) {
1022 if (f
->dl_type
== htons(ETH_TYPE_ARP
) ||
1023 f
->dl_type
== htons(ETH_TYPE_RARP
)) {
1024 ds_put_format(s
, "arp_op=%"PRIu8
",", f
->nw_proto
);
1026 ds_put_format(s
, "nw_proto=%"PRIu8
",", f
->nw_proto
);
1029 if (f
->dl_type
== htons(ETH_TYPE_ARP
) ||
1030 f
->dl_type
== htons(ETH_TYPE_RARP
)) {
1031 format_eth_masked(s
, "arp_sha", f
->arp_sha
, wc
->masks
.arp_sha
);
1032 format_eth_masked(s
, "arp_tha", f
->arp_tha
, wc
->masks
.arp_tha
);
1034 if (wc
->masks
.nw_tos
& IP_DSCP_MASK
) {
1035 ds_put_format(s
, "nw_tos=%"PRIu8
",", f
->nw_tos
& IP_DSCP_MASK
);
1037 if (wc
->masks
.nw_tos
& IP_ECN_MASK
) {
1038 ds_put_format(s
, "nw_ecn=%"PRIu8
",", f
->nw_tos
& IP_ECN_MASK
);
1040 if (wc
->masks
.nw_ttl
) {
1041 ds_put_format(s
, "nw_ttl=%"PRIu8
",", f
->nw_ttl
);
1043 if (wc
->masks
.mpls_lse
[0] & htonl(MPLS_LABEL_MASK
)) {
1044 ds_put_format(s
, "mpls_label=%"PRIu32
",",
1045 mpls_lse_to_label(f
->mpls_lse
[0]));
1047 if (wc
->masks
.mpls_lse
[0] & htonl(MPLS_TC_MASK
)) {
1048 ds_put_format(s
, "mpls_tc=%"PRIu8
",",
1049 mpls_lse_to_tc(f
->mpls_lse
[0]));
1051 if (wc
->masks
.mpls_lse
[0] & htonl(MPLS_TTL_MASK
)) {
1052 ds_put_format(s
, "mpls_ttl=%"PRIu8
",",
1053 mpls_lse_to_ttl(f
->mpls_lse
[0]));
1055 if (wc
->masks
.mpls_lse
[0] & htonl(MPLS_BOS_MASK
)) {
1056 ds_put_format(s
, "mpls_bos=%"PRIu8
",",
1057 mpls_lse_to_bos(f
->mpls_lse
[0]));
1059 format_be32_masked(s
, "mpls_lse1", f
->mpls_lse
[1], wc
->masks
.mpls_lse
[1]);
1060 format_be32_masked(s
, "mpls_lse2", f
->mpls_lse
[2], wc
->masks
.mpls_lse
[2]);
1062 switch (wc
->masks
.nw_frag
) {
1063 case FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
:
1064 ds_put_format(s
, "nw_frag=%s,",
1065 f
->nw_frag
& FLOW_NW_FRAG_ANY
1066 ? (f
->nw_frag
& FLOW_NW_FRAG_LATER
? "later" : "first")
1067 : (f
->nw_frag
& FLOW_NW_FRAG_LATER
? "<error>" : "no"));
1070 case FLOW_NW_FRAG_ANY
:
1071 ds_put_format(s
, "nw_frag=%s,",
1072 f
->nw_frag
& FLOW_NW_FRAG_ANY
? "yes" : "no");
1075 case FLOW_NW_FRAG_LATER
:
1076 ds_put_format(s
, "nw_frag=%s,",
1077 f
->nw_frag
& FLOW_NW_FRAG_LATER
? "later" : "not_later");
1080 if (f
->dl_type
== htons(ETH_TYPE_IP
) &&
1081 f
->nw_proto
== IPPROTO_ICMP
) {
1082 format_be16_masked(s
, "icmp_type", f
->tp_src
, wc
->masks
.tp_src
);
1083 format_be16_masked(s
, "icmp_code", f
->tp_dst
, wc
->masks
.tp_dst
);
1084 } else if (f
->dl_type
== htons(ETH_TYPE_IP
) &&
1085 f
->nw_proto
== IPPROTO_IGMP
) {
1086 format_be16_masked(s
, "igmp_type", f
->tp_src
, wc
->masks
.tp_src
);
1087 format_be16_masked(s
, "igmp_code", f
->tp_dst
, wc
->masks
.tp_dst
);
1088 } else if (f
->dl_type
== htons(ETH_TYPE_IPV6
) &&
1089 f
->nw_proto
== IPPROTO_ICMPV6
) {
1090 format_be16_masked(s
, "icmp_type", f
->tp_src
, wc
->masks
.tp_src
);
1091 format_be16_masked(s
, "icmp_code", f
->tp_dst
, wc
->masks
.tp_dst
);
1092 format_ipv6_netmask(s
, "nd_target", &f
->nd_target
,
1093 &wc
->masks
.nd_target
);
1094 format_eth_masked(s
, "nd_sll", f
->arp_sha
, wc
->masks
.arp_sha
);
1095 format_eth_masked(s
, "nd_tll", f
->arp_tha
, wc
->masks
.arp_tha
);
1097 format_be16_masked(s
, "tp_src", f
->tp_src
, wc
->masks
.tp_src
);
1098 format_be16_masked(s
, "tp_dst", f
->tp_dst
, wc
->masks
.tp_dst
);
1100 if (is_ip_any(f
) && f
->nw_proto
== IPPROTO_TCP
&& wc
->masks
.tcp_flags
) {
1101 uint16_t mask
= TCP_FLAGS(wc
->masks
.tcp_flags
);
1103 if (mask
== TCP_FLAGS(OVS_BE16_MAX
)) {
1104 ds_put_cstr(s
, "tcp_flags=");
1106 format_flags(s
, packet_tcp_flag_to_string
, ntohs(f
->tcp_flags
),
1109 ds_put_cstr(s
, "0"); /* Zero flags. */
1112 format_flags_masked(s
, "tcp_flags", packet_tcp_flag_to_string
,
1113 ntohs(f
->tcp_flags
), mask
);
1117 if (s
->length
> start_len
) {
1122 /* Converts 'match' to a string and returns the string. If 'priority' is
1123 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1124 * must free the string (with free()). */
1126 match_to_string(const struct match
*match
, int priority
)
1128 struct ds s
= DS_EMPTY_INITIALIZER
;
1129 match_format(match
, &s
, priority
);
1130 return ds_steal_cstr(&s
);
1134 match_print(const struct match
*match
)
1136 char *s
= match_to_string(match
, OFP_DEFAULT_PRIORITY
);
1141 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1142 * with minimatch_destroy(). */
1144 minimatch_init(struct minimatch
*dst
, const struct match
*src
)
1146 minimask_init(&dst
->mask
, &src
->wc
);
1147 miniflow_init_with_minimask(&dst
->flow
, &src
->flow
, &dst
->mask
);
1150 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1151 * with minimatch_destroy(). */
1153 minimatch_clone(struct minimatch
*dst
, const struct minimatch
*src
)
1155 miniflow_clone(&dst
->flow
, &src
->flow
);
1156 minimask_clone(&dst
->mask
, &src
->mask
);
1159 /* Initializes 'dst' with the data in 'src', destroying 'src'. The caller must
1160 * eventually free 'dst' with minimatch_destroy(). */
1162 minimatch_move(struct minimatch
*dst
, struct minimatch
*src
)
1164 miniflow_move(&dst
->flow
, &src
->flow
);
1165 minimask_move(&dst
->mask
, &src
->mask
);
1168 /* Frees any memory owned by 'match'. Does not free the storage in which
1169 * 'match' itself resides; the caller is responsible for that. */
1171 minimatch_destroy(struct minimatch
*match
)
1173 miniflow_destroy(&match
->flow
);
1174 minimask_destroy(&match
->mask
);
1177 /* Initializes 'dst' as a copy of 'src'. */
1179 minimatch_expand(const struct minimatch
*src
, struct match
*dst
)
1181 miniflow_expand(&src
->flow
, &dst
->flow
);
1182 minimask_expand(&src
->mask
, &dst
->wc
);
1185 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
1187 minimatch_equal(const struct minimatch
*a
, const struct minimatch
*b
)
1189 return (miniflow_equal(&a
->flow
, &b
->flow
)
1190 && minimask_equal(&a
->mask
, &b
->mask
));
1193 /* Returns true if 'target' satisifies 'match', that is, if each bit for which
1194 * 'match' specifies a particular value has the correct value in 'target'.
1196 * This function is equivalent to miniflow_equal_flow_in_minimask(&match->flow,
1197 * target, &match->mask) but it is faster because of the invariant that
1198 * match->flow.map and match->mask.map are the same. */
1200 minimatch_matches_flow(const struct minimatch
*match
,
1201 const struct flow
*target
)
1203 const uint32_t *target_u32
= (const uint32_t *) target
;
1204 const uint32_t *flowp
= miniflow_get_u32_values(&match
->flow
);
1205 const uint32_t *maskp
= miniflow_get_u32_values(&match
->mask
.masks
);
1208 for (map
= match
->flow
.map
; map
; map
= zero_rightmost_1bit(map
)) {
1209 if ((*flowp
++ ^ target_u32
[raw_ctz(map
)]) & *maskp
++) {
1217 /* Appends a string representation of 'match' to 's'. If 'priority' is
1218 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1220 minimatch_format(const struct minimatch
*match
, struct ds
*s
, int priority
)
1222 struct match megamatch
;
1224 minimatch_expand(match
, &megamatch
);
1225 match_format(&megamatch
, s
, priority
);
1228 /* Converts 'match' to a string and returns the string. If 'priority' is
1229 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1230 * must free the string (with free()). */
1232 minimatch_to_string(const struct minimatch
*match
, int priority
)
1234 struct match megamatch
;
1236 minimatch_expand(match
, &megamatch
);
1237 return match_to_string(&megamatch
, priority
);