2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 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.
18 #include "openvswitch/match.h"
21 #include "byte-order.h"
23 #include "openvswitch/dynamic-string.h"
24 #include "openvswitch/ofp-util.h"
26 #include "tun-metadata.h"
28 /* Converts the flow in 'flow' into a match in 'match', with the given
31 match_init(struct match
*match
,
32 const struct flow
*flow
, const struct flow_wildcards
*wc
)
36 match_zero_wildcarded_fields(match
);
37 memset(&match
->tun_md
, 0, sizeof match
->tun_md
);
40 /* Converts a flow into a match. It sets the wildcard masks based on
41 * the packet contents. It will not set the mask for fields that do not
42 * make sense for the packet type. */
44 match_wc_init(struct match
*match
, const struct flow
*flow
)
48 flow_wildcards_init_for_packet(&match
->wc
, flow
);
49 WC_MASK_FIELD(&match
->wc
, regs
);
50 WC_MASK_FIELD(&match
->wc
, metadata
);
52 memset(&match
->tun_md
, 0, sizeof match
->tun_md
);
55 /* Initializes 'match' as a "catch-all" match that matches every packet. */
57 match_init_catchall(struct match
*match
)
59 memset(&match
->flow
, 0, sizeof match
->flow
);
60 flow_wildcards_init_catchall(&match
->wc
);
61 memset(&match
->tun_md
, 0, sizeof match
->tun_md
);
64 /* For each bit or field wildcarded in 'match', sets the corresponding bit or
65 * field in 'flow' to all-0-bits. It is important to maintain this invariant
66 * in a match that might be inserted into a classifier.
68 * It is never necessary to call this function directly for a match that is
69 * initialized or modified only by match_*() functions. It is useful to
70 * restore the invariant in a match whose 'wc' member is modified by hand.
73 match_zero_wildcarded_fields(struct match
*match
)
75 flow_zero_wildcards(&match
->flow
, &match
->wc
);
79 match_set_dp_hash(struct match
*match
, uint32_t value
)
81 match_set_dp_hash_masked(match
, value
, UINT32_MAX
);
85 match_set_dp_hash_masked(struct match
*match
, uint32_t value
, uint32_t mask
)
87 match
->wc
.masks
.dp_hash
= mask
;
88 match
->flow
.dp_hash
= value
& mask
;
92 match_set_recirc_id(struct match
*match
, uint32_t value
)
94 match
->flow
.recirc_id
= value
;
95 match
->wc
.masks
.recirc_id
= UINT32_MAX
;
99 match_set_conj_id(struct match
*match
, uint32_t value
)
101 match
->flow
.conj_id
= value
;
102 match
->wc
.masks
.conj_id
= UINT32_MAX
;
106 match_set_reg(struct match
*match
, unsigned int reg_idx
, uint32_t value
)
108 match_set_reg_masked(match
, reg_idx
, value
, UINT32_MAX
);
112 match_set_reg_masked(struct match
*match
, unsigned int reg_idx
,
113 uint32_t value
, uint32_t mask
)
115 ovs_assert(reg_idx
< FLOW_N_REGS
);
116 flow_wildcards_set_reg_mask(&match
->wc
, reg_idx
, mask
);
117 match
->flow
.regs
[reg_idx
] = value
& mask
;
121 match_set_xreg(struct match
*match
, unsigned int xreg_idx
, uint64_t value
)
123 match_set_xreg_masked(match
, xreg_idx
, value
, UINT64_MAX
);
127 match_set_xreg_masked(struct match
*match
, unsigned int xreg_idx
,
128 uint64_t value
, uint64_t mask
)
130 ovs_assert(xreg_idx
< FLOW_N_XREGS
);
131 flow_wildcards_set_xreg_mask(&match
->wc
, xreg_idx
, mask
);
132 flow_set_xreg(&match
->flow
, xreg_idx
, value
& mask
);
136 match_set_xxreg(struct match
*match
, unsigned int xxreg_idx
, ovs_u128 value
)
138 match_set_xxreg_masked(match
, xxreg_idx
, value
, OVS_U128_MAX
);
142 match_set_xxreg_masked(struct match
*match
, unsigned int xxreg_idx
,
143 ovs_u128 value
, ovs_u128 mask
)
145 ovs_assert(xxreg_idx
< FLOW_N_XXREGS
);
146 flow_wildcards_set_xxreg_mask(&match
->wc
, xxreg_idx
, mask
);
147 flow_set_xxreg(&match
->flow
, xxreg_idx
, ovs_u128_and(value
, mask
));
151 match_set_actset_output(struct match
*match
, ofp_port_t actset_output
)
153 match
->wc
.masks
.actset_output
= u16_to_ofp(UINT16_MAX
);
154 match
->flow
.actset_output
= actset_output
;
158 match_set_metadata(struct match
*match
, ovs_be64 metadata
)
160 match_set_metadata_masked(match
, metadata
, OVS_BE64_MAX
);
164 match_set_metadata_masked(struct match
*match
,
165 ovs_be64 metadata
, ovs_be64 mask
)
167 match
->wc
.masks
.metadata
= mask
;
168 match
->flow
.metadata
= metadata
& mask
;
172 match_set_tun_id(struct match
*match
, ovs_be64 tun_id
)
174 match_set_tun_id_masked(match
, tun_id
, OVS_BE64_MAX
);
178 match_set_tun_id_masked(struct match
*match
, ovs_be64 tun_id
, ovs_be64 mask
)
180 match
->wc
.masks
.tunnel
.tun_id
= mask
;
181 match
->flow
.tunnel
.tun_id
= tun_id
& mask
;
185 match_set_tun_src(struct match
*match
, ovs_be32 src
)
187 match_set_tun_src_masked(match
, src
, OVS_BE32_MAX
);
191 match_set_tun_src_masked(struct match
*match
, ovs_be32 src
, ovs_be32 mask
)
193 match
->wc
.masks
.tunnel
.ip_src
= mask
;
194 match
->flow
.tunnel
.ip_src
= src
& mask
;
198 match_set_tun_dst(struct match
*match
, ovs_be32 dst
)
200 match_set_tun_dst_masked(match
, dst
, OVS_BE32_MAX
);
204 match_set_tun_dst_masked(struct match
*match
, ovs_be32 dst
, ovs_be32 mask
)
206 match
->wc
.masks
.tunnel
.ip_dst
= mask
;
207 match
->flow
.tunnel
.ip_dst
= dst
& mask
;
211 match_set_tun_ipv6_src(struct match
*match
, const struct in6_addr
*src
)
213 match
->flow
.tunnel
.ipv6_src
= *src
;
214 match
->wc
.masks
.tunnel
.ipv6_src
= in6addr_exact
;
218 match_set_tun_ipv6_src_masked(struct match
*match
, const struct in6_addr
*src
,
219 const struct in6_addr
*mask
)
221 match
->flow
.tunnel
.ipv6_src
= ipv6_addr_bitand(src
, mask
);
222 match
->wc
.masks
.tunnel
.ipv6_src
= *mask
;
226 match_set_tun_ipv6_dst(struct match
*match
, const struct in6_addr
*dst
)
228 match
->flow
.tunnel
.ipv6_dst
= *dst
;
229 match
->wc
.masks
.tunnel
.ipv6_dst
= in6addr_exact
;
233 match_set_tun_ipv6_dst_masked(struct match
*match
, const struct in6_addr
*dst
,
234 const struct in6_addr
*mask
)
236 match
->flow
.tunnel
.ipv6_dst
= ipv6_addr_bitand(dst
, mask
);
237 match
->wc
.masks
.tunnel
.ipv6_dst
= *mask
;
241 match_set_tun_ttl(struct match
*match
, uint8_t ttl
)
243 match_set_tun_ttl_masked(match
, ttl
, UINT8_MAX
);
247 match_set_tun_ttl_masked(struct match
*match
, uint8_t ttl
, uint8_t mask
)
249 match
->wc
.masks
.tunnel
.ip_ttl
= mask
;
250 match
->flow
.tunnel
.ip_ttl
= ttl
& mask
;
254 match_set_tun_tos(struct match
*match
, uint8_t tos
)
256 match_set_tun_tos_masked(match
, tos
, UINT8_MAX
);
260 match_set_tun_tos_masked(struct match
*match
, uint8_t tos
, uint8_t mask
)
262 match
->wc
.masks
.tunnel
.ip_tos
= mask
;
263 match
->flow
.tunnel
.ip_tos
= tos
& mask
;
267 match_set_tun_flags(struct match
*match
, uint16_t flags
)
269 match_set_tun_flags_masked(match
, flags
, UINT16_MAX
);
273 match_set_tun_flags_masked(struct match
*match
, uint16_t flags
, uint16_t mask
)
275 mask
&= FLOW_TNL_PUB_F_MASK
;
277 match
->wc
.masks
.tunnel
.flags
= mask
;
278 match
->flow
.tunnel
.flags
= flags
& mask
;
282 match_set_tun_tp_dst(struct match
*match
, ovs_be16 tp_dst
)
284 match_set_tun_tp_dst_masked(match
, tp_dst
, OVS_BE16_MAX
);
288 match_set_tun_tp_dst_masked(struct match
*match
, ovs_be16 port
, ovs_be16 mask
)
290 match
->wc
.masks
.tunnel
.tp_dst
= mask
;
291 match
->flow
.tunnel
.tp_dst
= port
& mask
;
295 match_set_tun_gbp_id_masked(struct match
*match
, ovs_be16 gbp_id
, ovs_be16 mask
)
297 match
->wc
.masks
.tunnel
.gbp_id
= mask
;
298 match
->flow
.tunnel
.gbp_id
= gbp_id
& mask
;
302 match_set_tun_gbp_id(struct match
*match
, ovs_be16 gbp_id
)
304 match_set_tun_gbp_id_masked(match
, gbp_id
, OVS_BE16_MAX
);
308 match_set_tun_gbp_flags_masked(struct match
*match
, uint8_t flags
, uint8_t mask
)
310 match
->wc
.masks
.tunnel
.gbp_flags
= mask
;
311 match
->flow
.tunnel
.gbp_flags
= flags
& mask
;
315 match_set_tun_gbp_flags(struct match
*match
, uint8_t flags
)
317 match_set_tun_gbp_flags_masked(match
, flags
, UINT8_MAX
);
321 match_set_in_port(struct match
*match
, ofp_port_t ofp_port
)
323 match
->wc
.masks
.in_port
.ofp_port
= u16_to_ofp(UINT16_MAX
);
324 match
->flow
.in_port
.ofp_port
= ofp_port
;
328 match_set_skb_priority(struct match
*match
, uint32_t skb_priority
)
330 match
->wc
.masks
.skb_priority
= UINT32_MAX
;
331 match
->flow
.skb_priority
= skb_priority
;
335 match_set_pkt_mark(struct match
*match
, uint32_t pkt_mark
)
337 match_set_pkt_mark_masked(match
, pkt_mark
, UINT32_MAX
);
341 match_set_pkt_mark_masked(struct match
*match
, uint32_t pkt_mark
, uint32_t mask
)
343 match
->flow
.pkt_mark
= pkt_mark
& mask
;
344 match
->wc
.masks
.pkt_mark
= mask
;
348 match_set_ct_state(struct match
*match
, uint32_t ct_state
)
350 match_set_ct_state_masked(match
, ct_state
, UINT32_MAX
);
354 match_set_ct_state_masked(struct match
*match
, uint32_t ct_state
, uint32_t mask
)
356 match
->flow
.ct_state
= ct_state
& mask
& UINT8_MAX
;
357 match
->wc
.masks
.ct_state
= mask
& UINT8_MAX
;
361 match_set_ct_zone(struct match
*match
, uint16_t ct_zone
)
363 match
->flow
.ct_zone
= ct_zone
;
364 match
->wc
.masks
.ct_zone
= UINT16_MAX
;
368 match_set_ct_mark(struct match
*match
, uint32_t ct_mark
)
370 match_set_ct_mark_masked(match
, ct_mark
, UINT32_MAX
);
374 match_set_ct_mark_masked(struct match
*match
, uint32_t ct_mark
,
377 match
->flow
.ct_mark
= ct_mark
& mask
;
378 match
->wc
.masks
.ct_mark
= mask
;
382 match_set_ct_label(struct match
*match
, ovs_u128 ct_label
)
386 mask
.u64
.lo
= UINT64_MAX
;
387 mask
.u64
.hi
= UINT64_MAX
;
388 match_set_ct_label_masked(match
, ct_label
, mask
);
392 match_set_ct_label_masked(struct match
*match
, ovs_u128 value
, ovs_u128 mask
)
394 match
->flow
.ct_label
.u64
.lo
= value
.u64
.lo
& mask
.u64
.lo
;
395 match
->flow
.ct_label
.u64
.hi
= value
.u64
.hi
& mask
.u64
.hi
;
396 match
->wc
.masks
.ct_label
= mask
;
400 match_set_ct_nw_src(struct match
*match
, ovs_be32 ct_nw_src
)
402 match
->flow
.ct_nw_src
= ct_nw_src
;
403 match
->wc
.masks
.ct_nw_src
= OVS_BE32_MAX
;
407 match_set_ct_nw_src_masked(struct match
*match
, ovs_be32 ct_nw_src
,
410 match
->flow
.ct_nw_src
= ct_nw_src
& mask
;
411 match
->wc
.masks
.ct_nw_src
= mask
;
415 match_set_ct_nw_dst(struct match
*match
, ovs_be32 ct_nw_dst
)
417 match
->flow
.ct_nw_dst
= ct_nw_dst
;
418 match
->wc
.masks
.ct_nw_dst
= OVS_BE32_MAX
;
422 match_set_ct_nw_dst_masked(struct match
*match
, ovs_be32 ct_nw_dst
,
425 match
->flow
.ct_nw_dst
= ct_nw_dst
& mask
;
426 match
->wc
.masks
.ct_nw_dst
= mask
;
430 match_set_ct_nw_proto(struct match
*match
, uint8_t ct_nw_proto
)
432 match
->flow
.ct_nw_proto
= ct_nw_proto
;
433 match
->wc
.masks
.ct_nw_proto
= UINT8_MAX
;
437 match_set_ct_tp_src(struct match
*match
, ovs_be16 ct_tp_src
)
439 match_set_ct_tp_src_masked(match
, ct_tp_src
, OVS_BE16_MAX
);
443 match_set_ct_tp_src_masked(struct match
*match
, ovs_be16 port
, ovs_be16 mask
)
445 match
->flow
.ct_tp_src
= port
& mask
;
446 match
->wc
.masks
.ct_tp_src
= mask
;
450 match_set_ct_tp_dst(struct match
*match
, ovs_be16 ct_tp_dst
)
452 match_set_ct_tp_dst_masked(match
, ct_tp_dst
, OVS_BE16_MAX
);
456 match_set_ct_tp_dst_masked(struct match
*match
, ovs_be16 port
, ovs_be16 mask
)
458 match
->flow
.ct_tp_dst
= port
& mask
;
459 match
->wc
.masks
.ct_tp_dst
= mask
;
463 match_set_ct_ipv6_src(struct match
*match
, const struct in6_addr
*src
)
465 match
->flow
.ct_ipv6_src
= *src
;
466 match
->wc
.masks
.ct_ipv6_src
= in6addr_exact
;
470 match_set_ct_ipv6_src_masked(struct match
*match
, const struct in6_addr
*src
,
471 const struct in6_addr
*mask
)
473 match
->flow
.ct_ipv6_src
= ipv6_addr_bitand(src
, mask
);
474 match
->wc
.masks
.ct_ipv6_src
= *mask
;
478 match_set_ct_ipv6_dst(struct match
*match
, const struct in6_addr
*dst
)
480 match
->flow
.ct_ipv6_dst
= *dst
;
481 match
->wc
.masks
.ct_ipv6_dst
= in6addr_exact
;
485 match_set_ct_ipv6_dst_masked(struct match
*match
, const struct in6_addr
*dst
,
486 const struct in6_addr
*mask
)
488 match
->flow
.ct_ipv6_dst
= ipv6_addr_bitand(dst
, mask
);
489 match
->wc
.masks
.ct_ipv6_dst
= *mask
;
493 match_set_packet_type(struct match
*match
, ovs_be32 packet_type
)
495 match
->flow
.packet_type
= packet_type
;
496 match
->wc
.masks
.packet_type
= OVS_BE32_MAX
;
500 match_set_dl_type(struct match
*match
, ovs_be16 dl_type
)
502 match
->wc
.masks
.dl_type
= OVS_BE16_MAX
;
503 match
->flow
.dl_type
= dl_type
;
506 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
507 * exactly. 'mask_dst' is set to all 1s. */
509 set_eth(const struct eth_addr value_src
,
510 struct eth_addr
*value_dst
,
511 struct eth_addr
*mask_dst
)
513 *value_dst
= value_src
;
514 *mask_dst
= eth_addr_exact
;
517 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
518 * after each byte is ANDed with the appropriate byte in 'mask_src'.
519 * 'mask_dst' is set to 'mask_src' */
521 set_eth_masked(const struct eth_addr value_src
,
522 const struct eth_addr mask_src
,
523 struct eth_addr
*value_dst
, struct eth_addr
*mask_dst
)
527 for (i
= 0; i
< ARRAY_SIZE(value_dst
->be16
); i
++) {
528 value_dst
->be16
[i
] = value_src
.be16
[i
] & mask_src
.be16
[i
];
530 *mask_dst
= mask_src
;
533 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
536 match_set_dl_src(struct match
*match
, const struct eth_addr dl_src
)
538 set_eth(dl_src
, &match
->flow
.dl_src
, &match
->wc
.masks
.dl_src
);
541 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
542 * after each byte is ANDed with the appropriate byte in 'mask'. */
544 match_set_dl_src_masked(struct match
*match
,
545 const struct eth_addr dl_src
,
546 const struct eth_addr mask
)
548 set_eth_masked(dl_src
, mask
, &match
->flow
.dl_src
, &match
->wc
.masks
.dl_src
);
551 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
554 match_set_dl_dst(struct match
*match
, const struct eth_addr dl_dst
)
556 set_eth(dl_dst
, &match
->flow
.dl_dst
, &match
->wc
.masks
.dl_dst
);
559 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
560 * byte is ANDed with the appropriate byte in 'mask'.
562 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
563 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
565 match_set_dl_dst_masked(struct match
*match
,
566 const struct eth_addr dl_dst
,
567 const struct eth_addr mask
)
569 set_eth_masked(dl_dst
, mask
, &match
->flow
.dl_dst
, &match
->wc
.masks
.dl_dst
);
573 match_set_dl_tci(struct match
*match
, ovs_be16 tci
)
575 match_set_dl_tci_masked(match
, tci
, htons(0xffff));
579 match_set_dl_tci_masked(struct match
*match
, ovs_be16 tci
, ovs_be16 mask
)
581 match
->flow
.vlans
[0].tci
= tci
& mask
;
582 match
->wc
.masks
.vlans
[0].tci
= mask
;
585 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
586 * wildcarded, then 'match' will match a packet regardless of whether it has an
587 * 802.1Q header or not. */
589 match_set_any_vid(struct match
*match
)
591 if (match
->wc
.masks
.vlans
[0].tci
& htons(VLAN_PCP_MASK
)) {
592 match
->wc
.masks
.vlans
[0].tci
&= ~htons(VLAN_VID_MASK
);
593 match
->flow
.vlans
[0].tci
&= ~htons(VLAN_VID_MASK
);
595 match_set_dl_tci_masked(match
, htons(0), htons(0));
599 /* Modifies 'match' depending on 'dl_vlan':
601 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
602 * without an 802.1Q header.
604 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
605 * VID equals the low 12 bits of 'dl_vlan'.
608 match_set_dl_vlan(struct match
*match
, ovs_be16 dl_vlan
)
610 flow_set_dl_vlan(&match
->flow
, dl_vlan
);
611 if (dl_vlan
== htons(OFP10_VLAN_NONE
)) {
612 match
->wc
.masks
.vlans
[0].tci
= OVS_BE16_MAX
;
614 match
->wc
.masks
.vlans
[0].tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
618 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
619 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
622 match_set_vlan_vid(struct match
*match
, ovs_be16 vid
)
624 match_set_vlan_vid_masked(match
, vid
, htons(VLAN_VID_MASK
| VLAN_CFI
));
628 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
629 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
630 * plus CFI), with the corresponding 'mask'. */
632 match_set_vlan_vid_masked(struct match
*match
, ovs_be16 vid
, ovs_be16 mask
)
634 ovs_be16 pcp_mask
= htons(VLAN_PCP_MASK
);
635 ovs_be16 vid_mask
= htons(VLAN_VID_MASK
| VLAN_CFI
);
638 flow_set_vlan_vid(&match
->flow
, vid
& mask
);
639 match
->wc
.masks
.vlans
[0].tci
=
640 mask
| (match
->wc
.masks
.vlans
[0].tci
& pcp_mask
);
643 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
644 * wildcarded, then 'match' will match a packet regardless of whether it has an
645 * 802.1Q header or not. */
647 match_set_any_pcp(struct match
*match
)
649 if (match
->wc
.masks
.vlans
[0].tci
& htons(VLAN_VID_MASK
)) {
650 match
->wc
.masks
.vlans
[0].tci
&= ~htons(VLAN_PCP_MASK
);
651 match
->flow
.vlans
[0].tci
&= ~htons(VLAN_PCP_MASK
);
653 match_set_dl_tci_masked(match
, htons(0), htons(0));
657 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
658 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
660 match_set_dl_vlan_pcp(struct match
*match
, uint8_t dl_vlan_pcp
)
662 flow_set_vlan_pcp(&match
->flow
, dl_vlan_pcp
);
663 match
->wc
.masks
.vlans
[0].tci
|= htons(VLAN_CFI
| VLAN_PCP_MASK
);
666 /* Modifies 'match' so that the MPLS label 'idx' matches 'lse' exactly. */
668 match_set_mpls_lse(struct match
*match
, int idx
, ovs_be32 lse
)
670 match
->wc
.masks
.mpls_lse
[idx
] = OVS_BE32_MAX
;
671 match
->flow
.mpls_lse
[idx
] = lse
;
674 /* Modifies 'match' so that the MPLS label is wildcarded. */
676 match_set_any_mpls_label(struct match
*match
, int idx
)
678 match
->wc
.masks
.mpls_lse
[idx
] &= ~htonl(MPLS_LABEL_MASK
);
679 flow_set_mpls_label(&match
->flow
, idx
, htonl(0));
682 /* Modifies 'match' so that it matches only packets with an MPLS header whose
683 * label equals the low 20 bits of 'mpls_label'. */
685 match_set_mpls_label(struct match
*match
, int idx
, ovs_be32 mpls_label
)
687 match
->wc
.masks
.mpls_lse
[idx
] |= htonl(MPLS_LABEL_MASK
);
688 flow_set_mpls_label(&match
->flow
, idx
, mpls_label
);
691 /* Modifies 'match' so that the MPLS TC is wildcarded. */
693 match_set_any_mpls_tc(struct match
*match
, int idx
)
695 match
->wc
.masks
.mpls_lse
[idx
] &= ~htonl(MPLS_TC_MASK
);
696 flow_set_mpls_tc(&match
->flow
, idx
, 0);
699 /* Modifies 'match' so that it matches only packets with an MPLS header whose
700 * Traffic Class equals the low 3 bits of 'mpls_tc'. */
702 match_set_mpls_tc(struct match
*match
, int idx
, uint8_t mpls_tc
)
704 match
->wc
.masks
.mpls_lse
[idx
] |= htonl(MPLS_TC_MASK
);
705 flow_set_mpls_tc(&match
->flow
, idx
, mpls_tc
);
708 /* Modifies 'match' so that the MPLS stack flag is wildcarded. */
710 match_set_any_mpls_bos(struct match
*match
, int idx
)
712 match
->wc
.masks
.mpls_lse
[idx
] &= ~htonl(MPLS_BOS_MASK
);
713 flow_set_mpls_bos(&match
->flow
, idx
, 0);
716 /* Modifies 'match' so that it matches only packets with an MPLS header whose
717 * Stack Flag equals the lower bit of 'mpls_bos' */
719 match_set_mpls_bos(struct match
*match
, int idx
, uint8_t mpls_bos
)
721 match
->wc
.masks
.mpls_lse
[idx
] |= htonl(MPLS_BOS_MASK
);
722 flow_set_mpls_bos(&match
->flow
, idx
, mpls_bos
);
725 /* Modifies 'match' so that the TTL of MPLS label 'idx' is wildcarded. */
727 match_set_any_mpls_ttl(struct match
*match
, int idx
)
729 match
->wc
.masks
.mpls_lse
[idx
] &= ~htonl(MPLS_TTL_MASK
);
730 flow_set_mpls_ttl(&match
->flow
, idx
, 0);
733 /* Modifies 'match' so that it matches only packets in which the TTL of MPLS
734 * label 'idx' equals 'mpls_ttl'. */
736 match_set_mpls_ttl(struct match
*match
, int idx
, uint8_t mpls_ttl
)
738 match
->wc
.masks
.mpls_lse
[idx
] |= htonl(MPLS_TTL_MASK
);
739 flow_set_mpls_ttl(&match
->flow
, idx
, mpls_ttl
);
742 /* Modifies 'match' so that the MPLS LSE is wildcarded. */
744 match_set_any_mpls_lse(struct match
*match
, int idx
)
746 match
->wc
.masks
.mpls_lse
[idx
] = htonl(0);
747 flow_set_mpls_lse(&match
->flow
, idx
, htonl(0));
751 match_set_tp_src(struct match
*match
, ovs_be16 tp_src
)
753 match_set_tp_src_masked(match
, tp_src
, OVS_BE16_MAX
);
757 match_set_tp_src_masked(struct match
*match
, ovs_be16 port
, ovs_be16 mask
)
759 match
->flow
.tp_src
= port
& mask
;
760 match
->wc
.masks
.tp_src
= mask
;
764 match_set_tp_dst(struct match
*match
, ovs_be16 tp_dst
)
766 match_set_tp_dst_masked(match
, tp_dst
, OVS_BE16_MAX
);
770 match_set_tp_dst_masked(struct match
*match
, ovs_be16 port
, ovs_be16 mask
)
772 match
->flow
.tp_dst
= port
& mask
;
773 match
->wc
.masks
.tp_dst
= mask
;
777 match_set_tcp_flags(struct match
*match
, ovs_be16 flags
)
779 match_set_tcp_flags_masked(match
, flags
, OVS_BE16_MAX
);
783 match_set_tcp_flags_masked(struct match
*match
, ovs_be16 flags
, ovs_be16 mask
)
785 match
->flow
.tcp_flags
= flags
& mask
;
786 match
->wc
.masks
.tcp_flags
= mask
;
790 match_set_nw_proto(struct match
*match
, uint8_t nw_proto
)
792 match
->flow
.nw_proto
= nw_proto
;
793 match
->wc
.masks
.nw_proto
= UINT8_MAX
;
797 match_set_nw_src(struct match
*match
, ovs_be32 nw_src
)
799 match
->flow
.nw_src
= nw_src
;
800 match
->wc
.masks
.nw_src
= OVS_BE32_MAX
;
804 match_set_nw_src_masked(struct match
*match
,
805 ovs_be32 nw_src
, ovs_be32 mask
)
807 match
->flow
.nw_src
= nw_src
& mask
;
808 match
->wc
.masks
.nw_src
= mask
;
812 match_set_nw_dst(struct match
*match
, ovs_be32 nw_dst
)
814 match
->flow
.nw_dst
= nw_dst
;
815 match
->wc
.masks
.nw_dst
= OVS_BE32_MAX
;
819 match_set_nw_dst_masked(struct match
*match
, ovs_be32 ip
, ovs_be32 mask
)
821 match
->flow
.nw_dst
= ip
& mask
;
822 match
->wc
.masks
.nw_dst
= mask
;
826 match_set_nw_dscp(struct match
*match
, uint8_t nw_dscp
)
828 match
->wc
.masks
.nw_tos
|= IP_DSCP_MASK
;
829 match
->flow
.nw_tos
&= ~IP_DSCP_MASK
;
830 match
->flow
.nw_tos
|= nw_dscp
& IP_DSCP_MASK
;
834 match_set_nw_ecn(struct match
*match
, uint8_t nw_ecn
)
836 match
->wc
.masks
.nw_tos
|= IP_ECN_MASK
;
837 match
->flow
.nw_tos
&= ~IP_ECN_MASK
;
838 match
->flow
.nw_tos
|= nw_ecn
& IP_ECN_MASK
;
842 match_set_nw_ttl(struct match
*match
, uint8_t nw_ttl
)
844 match
->wc
.masks
.nw_ttl
= UINT8_MAX
;
845 match
->flow
.nw_ttl
= nw_ttl
;
849 match_set_nw_frag(struct match
*match
, uint8_t nw_frag
)
851 match
->wc
.masks
.nw_frag
|= FLOW_NW_FRAG_MASK
;
852 match
->flow
.nw_frag
= nw_frag
;
856 match_set_nw_frag_masked(struct match
*match
,
857 uint8_t nw_frag
, uint8_t mask
)
859 match
->flow
.nw_frag
= nw_frag
& mask
;
860 match
->wc
.masks
.nw_frag
= mask
;
864 match_set_icmp_type(struct match
*match
, uint8_t icmp_type
)
866 match_set_tp_src(match
, htons(icmp_type
));
870 match_set_icmp_code(struct match
*match
, uint8_t icmp_code
)
872 match_set_tp_dst(match
, htons(icmp_code
));
876 match_set_arp_sha(struct match
*match
, const struct eth_addr sha
)
878 match
->flow
.arp_sha
= sha
;
879 match
->wc
.masks
.arp_sha
= eth_addr_exact
;
883 match_set_arp_sha_masked(struct match
*match
,
884 const struct eth_addr arp_sha
,
885 const struct eth_addr mask
)
887 set_eth_masked(arp_sha
, mask
,
888 &match
->flow
.arp_sha
, &match
->wc
.masks
.arp_sha
);
892 match_set_arp_tha(struct match
*match
, const struct eth_addr tha
)
894 match
->flow
.arp_tha
= tha
;
895 match
->wc
.masks
.arp_tha
= eth_addr_exact
;
899 match_set_arp_tha_masked(struct match
*match
,
900 const struct eth_addr arp_tha
,
901 const struct eth_addr mask
)
903 set_eth_masked(arp_tha
, mask
,
904 &match
->flow
.arp_tha
, &match
->wc
.masks
.arp_tha
);
908 match_set_ipv6_src(struct match
*match
, const struct in6_addr
*src
)
910 match
->flow
.ipv6_src
= *src
;
911 match
->wc
.masks
.ipv6_src
= in6addr_exact
;
915 match_set_ipv6_src_masked(struct match
*match
, const struct in6_addr
*src
,
916 const struct in6_addr
*mask
)
918 match
->flow
.ipv6_src
= ipv6_addr_bitand(src
, mask
);
919 match
->wc
.masks
.ipv6_src
= *mask
;
923 match_set_ipv6_dst(struct match
*match
, const struct in6_addr
*dst
)
925 match
->flow
.ipv6_dst
= *dst
;
926 match
->wc
.masks
.ipv6_dst
= in6addr_exact
;
930 match_set_ipv6_dst_masked(struct match
*match
, const struct in6_addr
*dst
,
931 const struct in6_addr
*mask
)
933 match
->flow
.ipv6_dst
= ipv6_addr_bitand(dst
, mask
);
934 match
->wc
.masks
.ipv6_dst
= *mask
;
938 match_set_ipv6_label(struct match
*match
, ovs_be32 ipv6_label
)
940 match
->wc
.masks
.ipv6_label
= OVS_BE32_MAX
;
941 match
->flow
.ipv6_label
= ipv6_label
;
946 match_set_ipv6_label_masked(struct match
*match
, ovs_be32 ipv6_label
,
949 match
->flow
.ipv6_label
= ipv6_label
& mask
;
950 match
->wc
.masks
.ipv6_label
= mask
;
954 match_set_nd_target(struct match
*match
, const struct in6_addr
*target
)
956 match
->flow
.nd_target
= *target
;
957 match
->wc
.masks
.nd_target
= in6addr_exact
;
961 match_set_nd_target_masked(struct match
*match
,
962 const struct in6_addr
*target
,
963 const struct in6_addr
*mask
)
965 match
->flow
.nd_target
= ipv6_addr_bitand(target
, mask
);
966 match
->wc
.masks
.nd_target
= *mask
;
969 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
970 * values for fixed fields, otherwise false. */
972 match_equal(const struct match
*a
, const struct match
*b
)
974 return (flow_wildcards_equal(&a
->wc
, &b
->wc
)
975 && flow_equal(&a
->flow
, &b
->flow
));
978 /* Returns a hash value for the flow and wildcards in 'match', starting from
981 match_hash(const struct match
*match
, uint32_t basis
)
983 return flow_wildcards_hash(&match
->wc
, flow_hash(&match
->flow
, basis
));
987 match_has_default_recirc_id(const struct match
*m
)
989 return m
->flow
.recirc_id
== 0 && (m
->wc
.masks
.recirc_id
== UINT32_MAX
||
990 m
->wc
.masks
.recirc_id
== 0);
994 match_has_default_dp_hash(const struct match
*m
)
996 return ((m
->flow
.dp_hash
| m
->wc
.masks
.dp_hash
) == 0);
999 /* Return true if the hidden fields of the match are set to the default values.
1000 * The default values equals to those set up by match_init_hidden_fields(). */
1002 match_has_default_hidden_fields(const struct match
*m
)
1004 return match_has_default_recirc_id(m
) && match_has_default_dp_hash(m
);
1008 match_init_hidden_fields(struct match
*m
)
1010 match_set_recirc_id(m
, 0);
1011 match_set_dp_hash_masked(m
, 0, 0);
1015 format_eth_masked(struct ds
*s
, const char *name
,
1016 const struct eth_addr eth
, const struct eth_addr mask
)
1018 if (!eth_addr_is_zero(mask
)) {
1019 ds_put_format(s
, "%s%s=%s", colors
.param
, name
, colors
.end
);
1020 eth_format_masked(eth
, &mask
, s
);
1021 ds_put_char(s
, ',');
1026 format_ip_netmask(struct ds
*s
, const char *name
, ovs_be32 ip
,
1030 ds_put_format(s
, "%s%s=%s", colors
.param
, name
, colors
.end
);
1031 ip_format_masked(ip
, netmask
, s
);
1032 ds_put_char(s
, ',');
1037 format_ipv6_netmask(struct ds
*s
, const char *name
,
1038 const struct in6_addr
*addr
,
1039 const struct in6_addr
*netmask
)
1041 if (!ipv6_mask_is_any(netmask
)) {
1042 ds_put_format(s
, "%s%s=%s", colors
.param
, name
, colors
.end
);
1043 ipv6_format_masked(addr
, netmask
, s
);
1044 ds_put_char(s
, ',');
1049 format_uint16_masked(struct ds
*s
, const char *name
,
1050 uint16_t value
, uint16_t mask
)
1053 ds_put_format(s
, "%s%s=%s", colors
.param
, name
, colors
.end
);
1054 if (mask
== UINT16_MAX
) {
1055 ds_put_format(s
, "%"PRIu16
, value
);
1057 ds_put_format(s
, "0x%"PRIx16
"/0x%"PRIx16
, value
, mask
);
1059 ds_put_char(s
, ',');
1064 format_be16_masked(struct ds
*s
, const char *name
,
1065 ovs_be16 value
, ovs_be16 mask
)
1067 if (mask
!= htons(0)) {
1068 ds_put_format(s
, "%s%s=%s", colors
.param
, name
, colors
.end
);
1069 if (mask
== OVS_BE16_MAX
) {
1070 ds_put_format(s
, "%"PRIu16
, ntohs(value
));
1072 ds_put_format(s
, "0x%"PRIx16
"/0x%"PRIx16
,
1073 ntohs(value
), ntohs(mask
));
1075 ds_put_char(s
, ',');
1080 format_be32_masked(struct ds
*s
, const char *name
,
1081 ovs_be32 value
, ovs_be32 mask
)
1083 if (mask
!= htonl(0)) {
1084 ds_put_format(s
, "%s%s=%s", colors
.param
, name
, colors
.end
);
1085 if (mask
== OVS_BE32_MAX
) {
1086 ds_put_format(s
, "%"PRIu32
, ntohl(value
));
1088 ds_put_format(s
, "0x%"PRIx32
"/0x%"PRIx32
,
1089 ntohl(value
), ntohl(mask
));
1091 ds_put_char(s
, ',');
1096 format_uint32_masked(struct ds
*s
, const char *name
,
1097 uint32_t value
, uint32_t mask
)
1100 ds_put_format(s
, "%s%s=%s%#"PRIx32
,
1101 colors
.param
, name
, colors
.end
, value
);
1102 if (mask
!= UINT32_MAX
) {
1103 ds_put_format(s
, "/%#"PRIx32
, mask
);
1105 ds_put_char(s
, ',');
1110 format_be64_masked(struct ds
*s
, const char *name
,
1111 ovs_be64 value
, ovs_be64 mask
)
1113 if (mask
!= htonll(0)) {
1114 ds_put_format(s
, "%s%s=%s%#"PRIx64
,
1115 colors
.param
, name
, colors
.end
, ntohll(value
));
1116 if (mask
!= OVS_BE64_MAX
) {
1117 ds_put_format(s
, "/%#"PRIx64
, ntohll(mask
));
1119 ds_put_char(s
, ',');
1124 format_flow_tunnel(struct ds
*s
, const struct match
*match
)
1126 const struct flow_wildcards
*wc
= &match
->wc
;
1127 const struct flow_tnl
*tnl
= &match
->flow
.tunnel
;
1129 format_be64_masked(s
, "tun_id", tnl
->tun_id
, wc
->masks
.tunnel
.tun_id
);
1130 format_ip_netmask(s
, "tun_src", tnl
->ip_src
, wc
->masks
.tunnel
.ip_src
);
1131 format_ip_netmask(s
, "tun_dst", tnl
->ip_dst
, wc
->masks
.tunnel
.ip_dst
);
1132 format_ipv6_netmask(s
, "tun_ipv6_src", &tnl
->ipv6_src
,
1133 &wc
->masks
.tunnel
.ipv6_src
);
1134 format_ipv6_netmask(s
, "tun_ipv6_dst", &tnl
->ipv6_dst
,
1135 &wc
->masks
.tunnel
.ipv6_dst
);
1137 if (wc
->masks
.tunnel
.gbp_id
) {
1138 format_be16_masked(s
, "tun_gbp_id", tnl
->gbp_id
,
1139 wc
->masks
.tunnel
.gbp_id
);
1142 if (wc
->masks
.tunnel
.gbp_flags
) {
1143 ds_put_format(s
, "tun_gbp_flags=%#"PRIx8
",", tnl
->gbp_flags
);
1146 if (wc
->masks
.tunnel
.ip_tos
) {
1147 ds_put_format(s
, "tun_tos=%"PRIx8
",", tnl
->ip_tos
);
1149 if (wc
->masks
.tunnel
.ip_ttl
) {
1150 ds_put_format(s
, "tun_ttl=%"PRIu8
",", tnl
->ip_ttl
);
1152 if (wc
->masks
.tunnel
.flags
& FLOW_TNL_F_MASK
) {
1153 format_flags_masked(s
, "tun_flags", flow_tun_flag_to_string
,
1154 tnl
->flags
& FLOW_TNL_F_MASK
,
1155 wc
->masks
.tunnel
.flags
& FLOW_TNL_F_MASK
,
1157 ds_put_char(s
, ',');
1159 tun_metadata_match_format(s
, match
);
1163 format_ct_label_masked(struct ds
*s
, const ovs_u128
*key
, const ovs_u128
*mask
)
1165 if (!ovs_u128_is_zero(*mask
)) {
1166 ovs_be128 value
= hton128(*key
);
1167 ds_put_format(s
, "%sct_label=%s", colors
.param
, colors
.end
);
1168 ds_put_hex(s
, &value
, sizeof value
);
1169 if (!is_all_ones(mask
, sizeof(*mask
))) {
1170 value
= hton128(*mask
);
1171 ds_put_char(s
, '/');
1172 ds_put_hex(s
, &value
, sizeof value
);
1174 ds_put_char(s
, ',');
1178 /* Appends a string representation of 'match' to 's'. If 'priority' is
1179 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. If 'port_map' is
1180 * nonnull, uses it to translate port numbers to names in output. */
1182 match_format(const struct match
*match
,
1183 const struct ofputil_port_map
*port_map
,
1184 struct ds
*s
, int priority
)
1186 const struct flow_wildcards
*wc
= &match
->wc
;
1187 size_t start_len
= s
->length
;
1188 const struct flow
*f
= &match
->flow
;
1189 bool skip_type
= false;
1191 bool skip_proto
= false;
1195 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 39);
1197 if (priority
!= OFP_DEFAULT_PRIORITY
) {
1198 ds_put_format(s
, "%spriority=%s%d,",
1199 colors
.special
, colors
.end
, priority
);
1202 format_uint32_masked(s
, "pkt_mark", f
->pkt_mark
, wc
->masks
.pkt_mark
);
1204 if (wc
->masks
.recirc_id
) {
1205 format_uint32_masked(s
, "recirc_id", f
->recirc_id
,
1206 wc
->masks
.recirc_id
);
1209 if (wc
->masks
.dp_hash
) {
1210 format_uint32_masked(s
, "dp_hash", f
->dp_hash
,
1214 if (wc
->masks
.conj_id
) {
1215 ds_put_format(s
, "%sconj_id%s=%"PRIu32
",",
1216 colors
.param
, colors
.end
, f
->conj_id
);
1219 if (wc
->masks
.skb_priority
) {
1220 ds_put_format(s
, "%sskb_priority=%s%#"PRIx32
",",
1221 colors
.param
, colors
.end
, f
->skb_priority
);
1224 if (wc
->masks
.actset_output
) {
1225 ds_put_format(s
, "%sactset_output=%s", colors
.param
, colors
.end
);
1226 ofputil_format_port(f
->actset_output
, port_map
, s
);
1227 ds_put_char(s
, ',');
1230 if (wc
->masks
.ct_state
) {
1231 if (wc
->masks
.ct_state
== UINT8_MAX
) {
1232 ds_put_format(s
, "%sct_state=%s", colors
.param
, colors
.end
);
1234 format_flags(s
, ct_state_to_string
, f
->ct_state
, '|');
1236 ds_put_cstr(s
, "0"); /* No state. */
1239 format_flags_masked(s
, "ct_state", ct_state_to_string
,
1240 f
->ct_state
, wc
->masks
.ct_state
, UINT8_MAX
);
1242 ds_put_char(s
, ',');
1245 if (wc
->masks
.ct_zone
) {
1246 format_uint16_masked(s
, "ct_zone", f
->ct_zone
, wc
->masks
.ct_zone
);
1249 if (wc
->masks
.ct_mark
) {
1250 format_uint32_masked(s
, "ct_mark", f
->ct_mark
, wc
->masks
.ct_mark
);
1253 if (!ovs_u128_is_zero(wc
->masks
.ct_label
)) {
1254 format_ct_label_masked(s
, &f
->ct_label
, &wc
->masks
.ct_label
);
1257 format_ip_netmask(s
, "ct_nw_src", f
->ct_nw_src
,
1258 wc
->masks
.ct_nw_src
);
1259 format_ipv6_netmask(s
, "ct_ipv6_src", &f
->ct_ipv6_src
,
1260 &wc
->masks
.ct_ipv6_src
);
1261 format_ip_netmask(s
, "ct_nw_dst", f
->ct_nw_dst
,
1262 wc
->masks
.ct_nw_dst
);
1263 format_ipv6_netmask(s
, "ct_ipv6_dst", &f
->ct_ipv6_dst
,
1264 &wc
->masks
.ct_ipv6_dst
);
1265 if (wc
->masks
.ct_nw_proto
) {
1266 ds_put_format(s
, "%sct_nw_proto=%s%"PRIu8
",",
1267 colors
.param
, colors
.end
, f
->ct_nw_proto
);
1268 format_be16_masked(s
, "ct_tp_src", f
->ct_tp_src
, wc
->masks
.ct_tp_src
);
1269 format_be16_masked(s
, "ct_tp_dst", f
->ct_tp_dst
, wc
->masks
.ct_tp_dst
);
1272 if (wc
->masks
.packet_type
) {
1273 if (pt_ns_type_be(wc
->masks
.packet_type
) == 0) {
1274 ds_put_format(s
, "packet_type=(%u,*),",
1275 pt_ns(f
->packet_type
));
1276 } else if (pt_ns_type_be(wc
->masks
.packet_type
) == OVS_BE16_MAX
) {
1277 ds_put_format(s
, "packet_type=(%u,%#"PRIx16
"),",
1278 pt_ns(f
->packet_type
),
1279 pt_ns_type(f
->packet_type
));
1281 ds_put_format(s
, "packet_type=(%u,%#"PRIx16
"/%#"PRIx16
"),",
1282 pt_ns(f
->packet_type
),
1283 pt_ns_type(f
->packet_type
),
1284 pt_ns_type(wc
->masks
.packet_type
));
1288 if (wc
->masks
.dl_type
) {
1290 if (f
->dl_type
== htons(ETH_TYPE_IP
)) {
1291 if (wc
->masks
.nw_proto
) {
1293 if (f
->nw_proto
== IPPROTO_ICMP
) {
1294 ds_put_format(s
, "%sicmp%s,", colors
.value
, colors
.end
);
1295 } else if (f
->nw_proto
== IPPROTO_IGMP
) {
1296 ds_put_format(s
, "%sigmp%s,", colors
.value
, colors
.end
);
1297 } else if (f
->nw_proto
== IPPROTO_TCP
) {
1298 ds_put_format(s
, "%stcp%s,", colors
.value
, colors
.end
);
1299 } else if (f
->nw_proto
== IPPROTO_UDP
) {
1300 ds_put_format(s
, "%sudp%s,", colors
.value
, colors
.end
);
1301 } else if (f
->nw_proto
== IPPROTO_SCTP
) {
1302 ds_put_format(s
, "%ssctp%s,", colors
.value
, colors
.end
);
1304 ds_put_format(s
, "%sip%s,", colors
.value
, colors
.end
);
1308 ds_put_format(s
, "%sip%s,", colors
.value
, colors
.end
);
1310 } else if (f
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1311 if (wc
->masks
.nw_proto
) {
1313 if (f
->nw_proto
== IPPROTO_ICMPV6
) {
1314 ds_put_format(s
, "%sicmp6%s,", colors
.value
, colors
.end
);
1315 } else if (f
->nw_proto
== IPPROTO_TCP
) {
1316 ds_put_format(s
, "%stcp6%s,", colors
.value
, colors
.end
);
1317 } else if (f
->nw_proto
== IPPROTO_UDP
) {
1318 ds_put_format(s
, "%sudp6%s,", colors
.value
, colors
.end
);
1319 } else if (f
->nw_proto
== IPPROTO_SCTP
) {
1320 ds_put_format(s
, "%ssctp6%s,", colors
.value
, colors
.end
);
1322 ds_put_format(s
, "%sipv6%s,", colors
.value
, colors
.end
);
1326 ds_put_format(s
, "%sipv6%s,", colors
.value
, colors
.end
);
1328 } else if (f
->dl_type
== htons(ETH_TYPE_ARP
)) {
1329 ds_put_format(s
, "%sarp%s,", colors
.value
, colors
.end
);
1330 } else if (f
->dl_type
== htons(ETH_TYPE_RARP
)) {
1331 ds_put_format(s
, "%srarp%s,", colors
.value
, colors
.end
);
1332 } else if (f
->dl_type
== htons(ETH_TYPE_MPLS
)) {
1333 ds_put_format(s
, "%smpls%s,", colors
.value
, colors
.end
);
1334 } else if (f
->dl_type
== htons(ETH_TYPE_MPLS_MCAST
)) {
1335 ds_put_format(s
, "%smplsm%s,", colors
.value
, colors
.end
);
1340 for (i
= 0; i
< FLOW_N_REGS
; i
++) {
1341 #define REGNAME_LEN 20
1342 char regname
[REGNAME_LEN
];
1343 if (snprintf(regname
, REGNAME_LEN
, "reg%d", i
) >= REGNAME_LEN
) {
1344 strcpy(regname
, "reg?");
1346 format_uint32_masked(s
, regname
, f
->regs
[i
], wc
->masks
.regs
[i
]);
1349 format_flow_tunnel(s
, match
);
1351 format_be64_masked(s
, "metadata", f
->metadata
, wc
->masks
.metadata
);
1353 if (wc
->masks
.in_port
.ofp_port
) {
1354 ds_put_format(s
, "%sin_port=%s", colors
.param
, colors
.end
);
1355 ofputil_format_port(f
->in_port
.ofp_port
, port_map
, s
);
1356 ds_put_char(s
, ',');
1358 for (i
= 0; i
< FLOW_MAX_VLAN_HEADERS
; i
++) {
1361 if (!wc
->masks
.vlans
[i
].tci
) {
1365 /* Print VLAN tags as dl_vlan, dl_vlan1, dl_vlan2 ... */
1369 snprintf(str_i
, sizeof(str_i
), "%d", i
);
1371 ovs_be16 vid_mask
= wc
->masks
.vlans
[i
].tci
& htons(VLAN_VID_MASK
);
1372 ovs_be16 pcp_mask
= wc
->masks
.vlans
[i
].tci
& htons(VLAN_PCP_MASK
);
1373 ovs_be16 cfi
= wc
->masks
.vlans
[i
].tci
& htons(VLAN_CFI
);
1375 if (cfi
&& f
->vlans
[i
].tci
& htons(VLAN_CFI
)
1376 && (!vid_mask
|| vid_mask
== htons(VLAN_VID_MASK
))
1377 && (!pcp_mask
|| pcp_mask
== htons(VLAN_PCP_MASK
))
1378 && (vid_mask
|| pcp_mask
)) {
1380 ds_put_format(s
, "%sdl_vlan%s=%s%"PRIu16
",",
1381 colors
.param
, str_i
, colors
.end
,
1382 vlan_tci_to_vid(f
->vlans
[i
].tci
));
1385 ds_put_format(s
, "%sdl_vlan_pcp%s=%s%d,",
1386 colors
.param
, str_i
, colors
.end
,
1387 vlan_tci_to_pcp(f
->vlans
[i
].tci
));
1389 } else if (wc
->masks
.vlans
[i
].tci
== htons(0xffff)) {
1390 ds_put_format(s
, "%svlan_tci%s=%s0x%04"PRIx16
",",
1391 colors
.param
, str_i
, colors
.end
,
1392 ntohs(f
->vlans
[i
].tci
));
1394 ds_put_format(s
, "%svlan_tci%s=%s0x%04"PRIx16
"/0x%04"PRIx16
",",
1395 colors
.param
, str_i
, colors
.end
,
1396 ntohs(f
->vlans
[i
].tci
),
1397 ntohs(wc
->masks
.vlans
[i
].tci
));
1401 format_eth_masked(s
, "dl_src", f
->dl_src
, wc
->masks
.dl_src
);
1402 format_eth_masked(s
, "dl_dst", f
->dl_dst
, wc
->masks
.dl_dst
);
1404 if (!skip_type
&& wc
->masks
.dl_type
) {
1405 ds_put_format(s
, "%sdl_type=%s0x%04"PRIx16
",",
1406 colors
.param
, colors
.end
, ntohs(f
->dl_type
));
1408 if (f
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1409 format_ipv6_netmask(s
, "ipv6_src", &f
->ipv6_src
, &wc
->masks
.ipv6_src
);
1410 format_ipv6_netmask(s
, "ipv6_dst", &f
->ipv6_dst
, &wc
->masks
.ipv6_dst
);
1411 if (wc
->masks
.ipv6_label
) {
1412 if (wc
->masks
.ipv6_label
== OVS_BE32_MAX
) {
1413 ds_put_format(s
, "%sipv6_label=%s0x%05"PRIx32
",",
1414 colors
.param
, colors
.end
,
1415 ntohl(f
->ipv6_label
));
1417 ds_put_format(s
, "%sipv6_label=%s0x%05"PRIx32
"/0x%05"PRIx32
",",
1418 colors
.param
, colors
.end
, ntohl(f
->ipv6_label
),
1419 ntohl(wc
->masks
.ipv6_label
));
1422 } else if (f
->dl_type
== htons(ETH_TYPE_ARP
) ||
1423 f
->dl_type
== htons(ETH_TYPE_RARP
)) {
1424 format_ip_netmask(s
, "arp_spa", f
->nw_src
, wc
->masks
.nw_src
);
1425 format_ip_netmask(s
, "arp_tpa", f
->nw_dst
, wc
->masks
.nw_dst
);
1427 format_ip_netmask(s
, "nw_src", f
->nw_src
, wc
->masks
.nw_src
);
1428 format_ip_netmask(s
, "nw_dst", f
->nw_dst
, wc
->masks
.nw_dst
);
1430 if (!skip_proto
&& wc
->masks
.nw_proto
) {
1431 if (f
->dl_type
== htons(ETH_TYPE_ARP
) ||
1432 f
->dl_type
== htons(ETH_TYPE_RARP
)) {
1433 ds_put_format(s
, "%sarp_op=%s%"PRIu8
",",
1434 colors
.param
, colors
.end
, f
->nw_proto
);
1436 ds_put_format(s
, "%snw_proto=%s%"PRIu8
",",
1437 colors
.param
, colors
.end
, f
->nw_proto
);
1440 if (f
->dl_type
== htons(ETH_TYPE_ARP
) ||
1441 f
->dl_type
== htons(ETH_TYPE_RARP
)) {
1442 format_eth_masked(s
, "arp_sha", f
->arp_sha
, wc
->masks
.arp_sha
);
1443 format_eth_masked(s
, "arp_tha", f
->arp_tha
, wc
->masks
.arp_tha
);
1445 if (wc
->masks
.nw_tos
& IP_DSCP_MASK
) {
1446 ds_put_format(s
, "%snw_tos=%s%d,",
1447 colors
.param
, colors
.end
, f
->nw_tos
& IP_DSCP_MASK
);
1449 if (wc
->masks
.nw_tos
& IP_ECN_MASK
) {
1450 ds_put_format(s
, "%snw_ecn=%s%d,",
1451 colors
.param
, colors
.end
, f
->nw_tos
& IP_ECN_MASK
);
1453 if (wc
->masks
.nw_ttl
) {
1454 ds_put_format(s
, "%snw_ttl=%s%d,",
1455 colors
.param
, colors
.end
, f
->nw_ttl
);
1457 if (wc
->masks
.mpls_lse
[0] & htonl(MPLS_LABEL_MASK
)) {
1458 ds_put_format(s
, "%smpls_label=%s%"PRIu32
",", colors
.param
,
1459 colors
.end
, mpls_lse_to_label(f
->mpls_lse
[0]));
1461 if (wc
->masks
.mpls_lse
[0] & htonl(MPLS_TC_MASK
)) {
1462 ds_put_format(s
, "%smpls_tc=%s%"PRIu8
",", colors
.param
, colors
.end
,
1463 mpls_lse_to_tc(f
->mpls_lse
[0]));
1465 if (wc
->masks
.mpls_lse
[0] & htonl(MPLS_TTL_MASK
)) {
1466 ds_put_format(s
, "%smpls_ttl=%s%"PRIu8
",", colors
.param
, colors
.end
,
1467 mpls_lse_to_ttl(f
->mpls_lse
[0]));
1469 if (wc
->masks
.mpls_lse
[0] & htonl(MPLS_BOS_MASK
)) {
1470 ds_put_format(s
, "%smpls_bos=%s%"PRIu8
",", colors
.param
, colors
.end
,
1471 mpls_lse_to_bos(f
->mpls_lse
[0]));
1473 format_be32_masked(s
, "mpls_lse1", f
->mpls_lse
[1], wc
->masks
.mpls_lse
[1]);
1474 format_be32_masked(s
, "mpls_lse2", f
->mpls_lse
[2], wc
->masks
.mpls_lse
[2]);
1476 switch (wc
->masks
.nw_frag
) {
1477 case FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
:
1478 ds_put_format(s
, "%snw_frag=%s%s,", colors
.param
, colors
.end
,
1479 f
->nw_frag
& FLOW_NW_FRAG_ANY
1480 ? (f
->nw_frag
& FLOW_NW_FRAG_LATER
? "later" : "first")
1481 : (f
->nw_frag
& FLOW_NW_FRAG_LATER
? "<error>" : "no"));
1484 case FLOW_NW_FRAG_ANY
:
1485 ds_put_format(s
, "%snw_frag=%s%s,", colors
.param
, colors
.end
,
1486 f
->nw_frag
& FLOW_NW_FRAG_ANY
? "yes" : "no");
1489 case FLOW_NW_FRAG_LATER
:
1490 ds_put_format(s
, "%snw_frag=%s%s,", colors
.param
, colors
.end
,
1491 f
->nw_frag
& FLOW_NW_FRAG_LATER
? "later" : "not_later");
1494 if (f
->dl_type
== htons(ETH_TYPE_IP
) &&
1495 f
->nw_proto
== IPPROTO_ICMP
) {
1496 format_be16_masked(s
, "icmp_type", f
->tp_src
, wc
->masks
.tp_src
);
1497 format_be16_masked(s
, "icmp_code", f
->tp_dst
, wc
->masks
.tp_dst
);
1498 } else if (f
->dl_type
== htons(ETH_TYPE_IP
) &&
1499 f
->nw_proto
== IPPROTO_IGMP
) {
1500 format_be16_masked(s
, "igmp_type", f
->tp_src
, wc
->masks
.tp_src
);
1501 format_be16_masked(s
, "igmp_code", f
->tp_dst
, wc
->masks
.tp_dst
);
1502 } else if (f
->dl_type
== htons(ETH_TYPE_IPV6
) &&
1503 f
->nw_proto
== IPPROTO_ICMPV6
) {
1504 format_be16_masked(s
, "icmp_type", f
->tp_src
, wc
->masks
.tp_src
);
1505 format_be16_masked(s
, "icmp_code", f
->tp_dst
, wc
->masks
.tp_dst
);
1506 format_ipv6_netmask(s
, "nd_target", &f
->nd_target
,
1507 &wc
->masks
.nd_target
);
1508 format_eth_masked(s
, "nd_sll", f
->arp_sha
, wc
->masks
.arp_sha
);
1509 format_eth_masked(s
, "nd_tll", f
->arp_tha
, wc
->masks
.arp_tha
);
1511 format_be16_masked(s
, "tp_src", f
->tp_src
, wc
->masks
.tp_src
);
1512 format_be16_masked(s
, "tp_dst", f
->tp_dst
, wc
->masks
.tp_dst
);
1514 if (is_ip_any(f
) && f
->nw_proto
== IPPROTO_TCP
&& wc
->masks
.tcp_flags
) {
1515 format_flags_masked(s
, "tcp_flags", packet_tcp_flag_to_string
,
1516 ntohs(f
->tcp_flags
), TCP_FLAGS(wc
->masks
.tcp_flags
),
1517 TCP_FLAGS(OVS_BE16_MAX
));
1520 if (s
->length
> start_len
) {
1525 /* Converts 'match' to a string and returns the string. If 'priority' is
1526 * different from OFP_DEFAULT_PRIORITY, includes it in the string. If
1527 * 'port_map' is nonnull, uses it to translate port numbers to names in
1528 * output. The caller must free the string (with free()). */
1530 match_to_string(const struct match
*match
,
1531 const struct ofputil_port_map
*port_map
, int priority
)
1533 struct ds s
= DS_EMPTY_INITIALIZER
;
1534 match_format(match
, port_map
, &s
, priority
);
1535 return ds_steal_cstr(&s
);
1539 match_print(const struct match
*match
,
1540 const struct ofputil_port_map
*port_map
)
1542 char *s
= match_to_string(match
, port_map
, OFP_DEFAULT_PRIORITY
);
1547 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1548 * with minimatch_destroy(). */
1550 minimatch_init(struct minimatch
*dst
, const struct match
*src
)
1552 struct miniflow tmp
;
1554 miniflow_map_init(&tmp
, &src
->wc
.masks
);
1555 /* Allocate two consecutive miniflows. */
1556 miniflow_alloc(dst
->flows
, 2, &tmp
);
1557 miniflow_init(dst
->flow
, &src
->flow
);
1558 minimask_init(dst
->mask
, &src
->wc
);
1561 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1562 * with minimatch_destroy(). */
1564 minimatch_clone(struct minimatch
*dst
, const struct minimatch
*src
)
1566 /* Allocate two consecutive miniflows. */
1567 size_t data_size
= miniflow_alloc(dst
->flows
, 2, &src
->mask
->masks
);
1569 memcpy(miniflow_values(dst
->flow
),
1570 miniflow_get_values(src
->flow
), data_size
);
1571 memcpy(miniflow_values(&dst
->mask
->masks
),
1572 miniflow_get_values(&src
->mask
->masks
), data_size
);
1575 /* Initializes 'dst' with the data in 'src', destroying 'src'. The caller must
1576 * eventually free 'dst' with minimatch_destroy(). */
1578 minimatch_move(struct minimatch
*dst
, struct minimatch
*src
)
1580 dst
->flow
= src
->flow
;
1581 dst
->mask
= src
->mask
;
1584 /* Frees any memory owned by 'match'. Does not free the storage in which
1585 * 'match' itself resides; the caller is responsible for that. */
1587 minimatch_destroy(struct minimatch
*match
)
1592 /* Initializes 'dst' as a copy of 'src'. */
1594 minimatch_expand(const struct minimatch
*src
, struct match
*dst
)
1596 miniflow_expand(src
->flow
, &dst
->flow
);
1597 minimask_expand(src
->mask
, &dst
->wc
);
1598 memset(&dst
->tun_md
, 0, sizeof dst
->tun_md
);
1601 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
1603 minimatch_equal(const struct minimatch
*a
, const struct minimatch
*b
)
1605 return minimask_equal(a
->mask
, b
->mask
)
1606 && miniflow_equal(a
->flow
, b
->flow
);
1609 /* Returns true if 'target' satisifies 'match', that is, if each bit for which
1610 * 'match' specifies a particular value has the correct value in 'target'.
1612 * This function is equivalent to miniflow_equal_flow_in_minimask(&match->flow,
1613 * target, &match->mask) but it is faster because of the invariant that
1614 * match->flow.map and match->mask.map are the same. */
1616 minimatch_matches_flow(const struct minimatch
*match
,
1617 const struct flow
*target
)
1619 const uint64_t *flowp
= miniflow_get_values(match
->flow
);
1620 const uint64_t *maskp
= miniflow_get_values(&match
->mask
->masks
);
1623 FLOWMAP_FOR_EACH_INDEX(idx
, match
->flow
->map
) {
1624 if ((*flowp
++ ^ flow_u64_value(target
, idx
)) & *maskp
++) {
1632 /* Appends a string representation of 'match' to 's'. If 'priority' is
1633 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. If 'port_map' is
1634 * nonnull, uses it to translate port numbers to names in output. */
1636 minimatch_format(const struct minimatch
*match
,
1637 const struct tun_table
*tun_table
,
1638 const struct ofputil_port_map
*port_map
,
1639 struct ds
*s
, int priority
)
1641 struct match megamatch
;
1643 minimatch_expand(match
, &megamatch
);
1644 megamatch
.flow
.tunnel
.metadata
.tab
= tun_table
;
1646 match_format(&megamatch
, port_map
, s
, priority
);
1649 /* Converts 'match' to a string and returns the string. If 'priority' is
1650 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1651 * must free the string (with free()). If 'port_map' is nonnull, uses it to
1652 * translate port numbers to names in output. */
1654 minimatch_to_string(const struct minimatch
*match
,
1655 const struct ofputil_port_map
*port_map
, int priority
)
1657 struct match megamatch
;
1659 minimatch_expand(match
, &megamatch
);
1660 return match_to_string(&megamatch
, port_map
, priority
);