]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/match.c
2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include "byte-order.h"
22 #include "dynamic-string.h"
26 VLOG_DEFINE_THIS_MODULE(match
);
29 /* Converts the flow in 'flow' into a match in 'match', with the given
32 match_init(struct match
*match
,
33 const struct flow
*flow
, const struct flow_wildcards
*wc
)
37 match_zero_wildcarded_fields(match
);
40 /* Converts a flow into a match. It sets the wildcard masks based on
41 * the packet contents. It will not set the mask for fields that do not
42 * make sense for the packet type. */
44 match_wc_init(struct match
*match
, const struct flow
*flow
)
46 struct flow_wildcards
*wc
;
51 memset(&wc
->masks
, 0x0, sizeof wc
->masks
);
53 memset(&wc
->masks
.dl_type
, 0xff, sizeof wc
->masks
.dl_type
);
56 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
59 for (i
= 0; i
< FLOW_N_REGS
; i
++) {
61 memset(&wc
->masks
.regs
[i
], 0xff, sizeof wc
->masks
.regs
[i
]);
65 if (flow
->tunnel
.ip_dst
|| flow
->tunnel
.tun_id
) {
66 memset(&wc
->masks
.tunnel
.tun_id
, 0xff, sizeof wc
->masks
.tunnel
.tun_id
);
67 memset(&wc
->masks
.tunnel
.ip_src
, 0xff, sizeof wc
->masks
.tunnel
.ip_src
);
68 memset(&wc
->masks
.tunnel
.ip_dst
, 0xff, sizeof wc
->masks
.tunnel
.ip_dst
);
69 memset(&wc
->masks
.tunnel
.flags
, 0xff, sizeof wc
->masks
.tunnel
.flags
);
70 memset(&wc
->masks
.tunnel
.ip_tos
, 0xff, sizeof wc
->masks
.tunnel
.ip_tos
);
71 memset(&wc
->masks
.tunnel
.ip_ttl
, 0xff, sizeof wc
->masks
.tunnel
.ip_ttl
);
73 memset(&wc
->masks
.metadata
, 0xff, sizeof wc
->masks
.metadata
);
74 memset(&wc
->masks
.in_port
, 0xff, sizeof wc
->masks
.in_port
);
75 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
76 memset(&wc
->masks
.dl_src
, 0xff, sizeof wc
->masks
.dl_src
);
77 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
79 if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
80 memset(&wc
->masks
.ipv6_src
, 0xff, sizeof wc
->masks
.ipv6_src
);
81 memset(&wc
->masks
.ipv6_dst
, 0xff, sizeof wc
->masks
.ipv6_dst
);
82 memset(&wc
->masks
.ipv6_label
, 0xff, sizeof wc
->masks
.ipv6_label
);
83 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
) ||
84 (flow
->dl_type
== htons(ETH_TYPE_ARP
)) ||
85 (flow
->dl_type
== htons(ETH_TYPE_RARP
))) {
86 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
87 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
90 if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
91 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
92 memset(&wc
->masks
.arp_sha
, 0xff, sizeof wc
->masks
.arp_sha
);
93 memset(&wc
->masks
.arp_tha
, 0xff, sizeof wc
->masks
.arp_tha
);
96 if (flow
->dl_type
== htons(ETH_TYPE_IPV6
) ||
97 flow
->dl_type
== htons(ETH_TYPE_IP
)) {
98 memset(&wc
->masks
.nw_tos
, 0xff, sizeof wc
->masks
.nw_tos
);
99 memset(&wc
->masks
.nw_ttl
, 0xff, sizeof wc
->masks
.nw_ttl
);
103 memset(&wc
->masks
.nw_frag
, 0xff, sizeof wc
->masks
.nw_frag
);
106 if (flow
->nw_proto
== IPPROTO_ICMP
|| flow
->nw_proto
== IPPROTO_ICMPV6
||
107 (flow
->tp_src
|| flow
->tp_dst
)) {
108 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
109 memset(&wc
->masks
.tp_dst
, 0xff, sizeof wc
->masks
.tp_dst
);
112 if (flow
->nw_proto
== IPPROTO_ICMPV6
) {
113 memset(&wc
->masks
.arp_sha
, 0xff, sizeof wc
->masks
.arp_sha
);
114 memset(&wc
->masks
.arp_tha
, 0xff, sizeof wc
->masks
.arp_tha
);
120 /* Converts the flow in 'flow' into an exact-match match in 'match'. */
122 match_init_exact(struct match
*match
, const struct flow
*flow
)
124 ovs_be64 tun_id
= flow
->tunnel
.tun_id
;
127 match
->flow
.skb_priority
= 0;
128 memset(&match
->flow
.tunnel
, 0, sizeof match
->flow
.tunnel
);
129 match
->flow
.tunnel
.tun_id
= tun_id
;
130 flow_wildcards_init_exact(&match
->wc
);
133 /* Initializes 'match' as a "catch-all" match that matches every packet. */
135 match_init_catchall(struct match
*match
)
137 memset(&match
->flow
, 0, sizeof match
->flow
);
138 flow_wildcards_init_catchall(&match
->wc
);
141 /* For each bit or field wildcarded in 'match', sets the corresponding bit or
142 * field in 'flow' to all-0-bits. It is important to maintain this invariant
143 * in a match that might be inserted into a classifier.
145 * It is never necessary to call this function directly for a match that is
146 * initialized or modified only by match_*() functions. It is useful to
147 * restore the invariant in a match whose 'wc' member is modified by hand.
150 match_zero_wildcarded_fields(struct match
*match
)
152 flow_zero_wildcards(&match
->flow
, &match
->wc
);
156 match_set_reg(struct match
*match
, unsigned int reg_idx
, uint32_t value
)
158 match_set_reg_masked(match
, reg_idx
, value
, UINT32_MAX
);
162 match_set_reg_masked(struct match
*match
, unsigned int reg_idx
,
163 uint32_t value
, uint32_t mask
)
165 assert(reg_idx
< FLOW_N_REGS
);
166 flow_wildcards_set_reg_mask(&match
->wc
, reg_idx
, mask
);
167 match
->flow
.regs
[reg_idx
] = value
& mask
;
171 match_set_metadata(struct match
*match
, ovs_be64 metadata
)
173 match_set_metadata_masked(match
, metadata
, htonll(UINT64_MAX
));
177 match_set_metadata_masked(struct match
*match
,
178 ovs_be64 metadata
, ovs_be64 mask
)
180 match
->wc
.masks
.metadata
= mask
;
181 match
->flow
.metadata
= metadata
& mask
;
185 match_set_tun_id(struct match
*match
, ovs_be64 tun_id
)
187 match_set_tun_id_masked(match
, tun_id
, htonll(UINT64_MAX
));
191 match_set_tun_id_masked(struct match
*match
, ovs_be64 tun_id
, ovs_be64 mask
)
193 match
->wc
.masks
.tunnel
.tun_id
= mask
;
194 match
->flow
.tunnel
.tun_id
= tun_id
& mask
;
198 match_set_in_port(struct match
*match
, uint16_t ofp_port
)
200 match
->wc
.masks
.in_port
= UINT16_MAX
;
201 match
->flow
.in_port
= ofp_port
;
205 match_set_dl_type(struct match
*match
, ovs_be16 dl_type
)
207 match
->wc
.masks
.dl_type
= htons(UINT16_MAX
);
208 match
->flow
.dl_type
= dl_type
;
211 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
212 * exactly. 'mask_dst' is set to all 1s. */
214 set_eth(const uint8_t value_src
[ETH_ADDR_LEN
],
215 uint8_t value_dst
[ETH_ADDR_LEN
],
216 uint8_t mask_dst
[ETH_ADDR_LEN
])
218 memcpy(value_dst
, value_src
, ETH_ADDR_LEN
);
219 memset(mask_dst
, 0xff, ETH_ADDR_LEN
);
222 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
223 * after each byte is ANDed with the appropriate byte in 'mask_src'.
224 * 'mask_dst' is set to 'mask_src' */
226 set_eth_masked(const uint8_t value_src
[ETH_ADDR_LEN
],
227 const uint8_t mask_src
[ETH_ADDR_LEN
],
228 uint8_t value_dst
[ETH_ADDR_LEN
],
229 uint8_t mask_dst
[ETH_ADDR_LEN
])
233 for (i
= 0; i
< ETH_ADDR_LEN
; i
++) {
234 value_dst
[i
] = value_src
[i
] & mask_src
[i
];
235 mask_dst
[i
] = mask_src
[i
];
239 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
242 match_set_dl_src(struct match
*match
, const uint8_t dl_src
[ETH_ADDR_LEN
])
244 set_eth(dl_src
, match
->flow
.dl_src
, match
->wc
.masks
.dl_src
);
247 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
248 * after each byte is ANDed with the appropriate byte in 'mask'. */
250 match_set_dl_src_masked(struct match
*match
,
251 const uint8_t dl_src
[ETH_ADDR_LEN
],
252 const uint8_t mask
[ETH_ADDR_LEN
])
254 set_eth_masked(dl_src
, mask
, match
->flow
.dl_src
, match
->wc
.masks
.dl_src
);
257 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
260 match_set_dl_dst(struct match
*match
, const uint8_t dl_dst
[ETH_ADDR_LEN
])
262 set_eth(dl_dst
, match
->flow
.dl_dst
, match
->wc
.masks
.dl_dst
);
265 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
266 * byte is ANDed with the appropriate byte in 'mask'.
268 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
269 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
271 match_set_dl_dst_masked(struct match
*match
,
272 const uint8_t dl_dst
[ETH_ADDR_LEN
],
273 const uint8_t mask
[ETH_ADDR_LEN
])
275 set_eth_masked(dl_dst
, mask
, match
->flow
.dl_dst
, match
->wc
.masks
.dl_dst
);
279 match_set_dl_tci(struct match
*match
, ovs_be16 tci
)
281 match_set_dl_tci_masked(match
, tci
, htons(0xffff));
285 match_set_dl_tci_masked(struct match
*match
, ovs_be16 tci
, ovs_be16 mask
)
287 match
->flow
.vlan_tci
= tci
& mask
;
288 match
->wc
.masks
.vlan_tci
= mask
;
291 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
292 * wildcarded, then 'match' will match a packet regardless of whether it has an
293 * 802.1Q header or not. */
295 match_set_any_vid(struct match
*match
)
297 if (match
->wc
.masks
.vlan_tci
& htons(VLAN_PCP_MASK
)) {
298 match
->wc
.masks
.vlan_tci
&= ~htons(VLAN_VID_MASK
);
299 match
->flow
.vlan_tci
&= ~htons(VLAN_VID_MASK
);
301 match_set_dl_tci_masked(match
, htons(0), htons(0));
305 /* Modifies 'match' depending on 'dl_vlan':
307 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
308 * without an 802.1Q header.
310 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
311 * VID equals the low 12 bits of 'dl_vlan'.
314 match_set_dl_vlan(struct match
*match
, ovs_be16 dl_vlan
)
316 flow_set_dl_vlan(&match
->flow
, dl_vlan
);
317 if (dl_vlan
== htons(OFP10_VLAN_NONE
)) {
318 match
->wc
.masks
.vlan_tci
= htons(UINT16_MAX
);
320 match
->wc
.masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
324 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
325 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
328 match_set_vlan_vid(struct match
*match
, ovs_be16 vid
)
330 match_set_vlan_vid_masked(match
, vid
, htons(VLAN_VID_MASK
| VLAN_CFI
));
334 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
335 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
336 * plus CFI), with the corresponding 'mask'. */
338 match_set_vlan_vid_masked(struct match
*match
, ovs_be16 vid
, ovs_be16 mask
)
340 ovs_be16 pcp_mask
= htons(VLAN_PCP_MASK
);
341 ovs_be16 vid_mask
= htons(VLAN_VID_MASK
| VLAN_CFI
);
344 flow_set_vlan_vid(&match
->flow
, vid
& mask
);
345 match
->wc
.masks
.vlan_tci
= mask
| (match
->wc
.masks
.vlan_tci
& pcp_mask
);
348 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
349 * wildcarded, then 'match' will match a packet regardless of whether it has an
350 * 802.1Q header or not. */
352 match_set_any_pcp(struct match
*match
)
354 if (match
->wc
.masks
.vlan_tci
& htons(VLAN_VID_MASK
)) {
355 match
->wc
.masks
.vlan_tci
&= ~htons(VLAN_PCP_MASK
);
356 match
->flow
.vlan_tci
&= ~htons(VLAN_PCP_MASK
);
358 match_set_dl_tci_masked(match
, htons(0), htons(0));
362 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
363 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
365 match_set_dl_vlan_pcp(struct match
*match
, uint8_t dl_vlan_pcp
)
367 flow_set_vlan_pcp(&match
->flow
, dl_vlan_pcp
);
368 match
->wc
.masks
.vlan_tci
|= htons(VLAN_CFI
| VLAN_PCP_MASK
);
372 match_set_tp_src(struct match
*match
, ovs_be16 tp_src
)
374 match_set_tp_src_masked(match
, tp_src
, htons(UINT16_MAX
));
378 match_set_tp_src_masked(struct match
*match
, ovs_be16 port
, ovs_be16 mask
)
380 match
->flow
.tp_src
= port
& mask
;
381 match
->wc
.masks
.tp_src
= mask
;
385 match_set_tp_dst(struct match
*match
, ovs_be16 tp_dst
)
387 match_set_tp_dst_masked(match
, tp_dst
, htons(UINT16_MAX
));
391 match_set_tp_dst_masked(struct match
*match
, ovs_be16 port
, ovs_be16 mask
)
393 match
->flow
.tp_dst
= port
& mask
;
394 match
->wc
.masks
.tp_dst
= mask
;
398 match_set_nw_proto(struct match
*match
, uint8_t nw_proto
)
400 match
->flow
.nw_proto
= nw_proto
;
401 match
->wc
.masks
.nw_proto
= UINT8_MAX
;
405 match_set_nw_src(struct match
*match
, ovs_be32 nw_src
)
407 match
->flow
.nw_src
= nw_src
;
408 match
->wc
.masks
.nw_src
= htonl(UINT32_MAX
);
412 match_set_nw_src_masked(struct match
*match
,
413 ovs_be32 nw_src
, ovs_be32 mask
)
415 match
->flow
.nw_src
= nw_src
& mask
;
416 match
->wc
.masks
.nw_src
= mask
;
420 match_set_nw_dst(struct match
*match
, ovs_be32 nw_dst
)
422 match
->flow
.nw_dst
= nw_dst
;
423 match
->wc
.masks
.nw_dst
= htonl(UINT32_MAX
);
427 match_set_nw_dst_masked(struct match
*match
, ovs_be32 ip
, ovs_be32 mask
)
429 match
->flow
.nw_dst
= ip
& mask
;
430 match
->wc
.masks
.nw_dst
= mask
;
434 match_set_nw_dscp(struct match
*match
, uint8_t nw_dscp
)
436 match
->wc
.masks
.nw_tos
|= IP_DSCP_MASK
;
437 match
->flow
.nw_tos
&= ~IP_DSCP_MASK
;
438 match
->flow
.nw_tos
|= nw_dscp
& IP_DSCP_MASK
;
442 match_set_nw_ecn(struct match
*match
, uint8_t nw_ecn
)
444 match
->wc
.masks
.nw_tos
|= IP_ECN_MASK
;
445 match
->flow
.nw_tos
&= ~IP_ECN_MASK
;
446 match
->flow
.nw_tos
|= nw_ecn
& IP_ECN_MASK
;
450 match_set_nw_ttl(struct match
*match
, uint8_t nw_ttl
)
452 match
->wc
.masks
.nw_ttl
= UINT8_MAX
;
453 match
->flow
.nw_ttl
= nw_ttl
;
457 match_set_nw_frag(struct match
*match
, uint8_t nw_frag
)
459 match
->wc
.masks
.nw_frag
|= FLOW_NW_FRAG_MASK
;
460 match
->flow
.nw_frag
= nw_frag
;
464 match_set_nw_frag_masked(struct match
*match
,
465 uint8_t nw_frag
, uint8_t mask
)
467 match
->flow
.nw_frag
= nw_frag
& mask
;
468 match
->wc
.masks
.nw_frag
= mask
;
472 match_set_icmp_type(struct match
*match
, uint8_t icmp_type
)
474 match_set_tp_src(match
, htons(icmp_type
));
478 match_set_icmp_code(struct match
*match
, uint8_t icmp_code
)
480 match_set_tp_dst(match
, htons(icmp_code
));
484 match_set_arp_sha(struct match
*match
, const uint8_t sha
[ETH_ADDR_LEN
])
486 memcpy(match
->flow
.arp_sha
, sha
, ETH_ADDR_LEN
);
487 memset(match
->wc
.masks
.arp_sha
, UINT8_MAX
, ETH_ADDR_LEN
);
491 match_set_arp_sha_masked(struct match
*match
,
492 const uint8_t arp_sha
[ETH_ADDR_LEN
],
493 const uint8_t mask
[ETH_ADDR_LEN
])
495 set_eth_masked(arp_sha
, mask
,
496 match
->flow
.arp_sha
, match
->wc
.masks
.arp_sha
);
500 match_set_arp_tha(struct match
*match
, const uint8_t tha
[ETH_ADDR_LEN
])
502 memcpy(match
->flow
.arp_tha
, tha
, ETH_ADDR_LEN
);
503 memset(match
->wc
.masks
.arp_tha
, UINT8_MAX
, ETH_ADDR_LEN
);
507 match_set_arp_tha_masked(struct match
*match
,
508 const uint8_t arp_tha
[ETH_ADDR_LEN
],
509 const uint8_t mask
[ETH_ADDR_LEN
])
511 set_eth_masked(arp_tha
, mask
,
512 match
->flow
.arp_tha
, match
->wc
.masks
.arp_tha
);
516 match_set_ipv6_src(struct match
*match
, const struct in6_addr
*src
)
518 match
->flow
.ipv6_src
= *src
;
519 match
->wc
.masks
.ipv6_src
= in6addr_exact
;
523 match_set_ipv6_src_masked(struct match
*match
, const struct in6_addr
*src
,
524 const struct in6_addr
*mask
)
526 match
->flow
.ipv6_src
= ipv6_addr_bitand(src
, mask
);
527 match
->wc
.masks
.ipv6_src
= *mask
;
531 match_set_ipv6_dst(struct match
*match
, const struct in6_addr
*dst
)
533 match
->flow
.ipv6_dst
= *dst
;
534 match
->wc
.masks
.ipv6_dst
= in6addr_exact
;
538 match_set_ipv6_dst_masked(struct match
*match
, const struct in6_addr
*dst
,
539 const struct in6_addr
*mask
)
541 match
->flow
.ipv6_dst
= ipv6_addr_bitand(dst
, mask
);
542 match
->wc
.masks
.ipv6_dst
= *mask
;
546 match_set_ipv6_label(struct match
*match
, ovs_be32 ipv6_label
)
548 match
->wc
.masks
.ipv6_label
= htonl(UINT32_MAX
);
549 match
->flow
.ipv6_label
= ipv6_label
;
554 match_set_ipv6_label_masked(struct match
*match
, ovs_be32 ipv6_label
,
557 match
->flow
.ipv6_label
= ipv6_label
& mask
;
558 match
->wc
.masks
.ipv6_label
= mask
;
562 match_set_nd_target(struct match
*match
, const struct in6_addr
*target
)
564 match
->flow
.nd_target
= *target
;
565 match
->wc
.masks
.nd_target
= in6addr_exact
;
569 match_set_nd_target_masked(struct match
*match
,
570 const struct in6_addr
*target
,
571 const struct in6_addr
*mask
)
573 match
->flow
.nd_target
= ipv6_addr_bitand(target
, mask
);
574 match
->wc
.masks
.nd_target
= *mask
;
577 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
578 * values for fixed fields, otherwise false. */
580 match_equal(const struct match
*a
, const struct match
*b
)
582 return (flow_wildcards_equal(&a
->wc
, &b
->wc
)
583 && flow_equal(&a
->flow
, &b
->flow
));
586 /* Returns a hash value for the flow and wildcards in 'match', starting from
589 match_hash(const struct match
*match
, uint32_t basis
)
591 return flow_wildcards_hash(&match
->wc
, flow_hash(&match
->flow
, basis
));
595 format_eth_masked(struct ds
*s
, const char *name
, const uint8_t eth
[6],
596 const uint8_t mask
[6])
598 if (!eth_addr_is_zero(mask
)) {
599 ds_put_format(s
, "%s=", name
);
600 eth_format_masked(eth
, mask
, s
);
606 format_ip_netmask(struct ds
*s
, const char *name
, ovs_be32 ip
,
610 ds_put_format(s
, "%s=", name
);
611 ip_format_masked(ip
, netmask
, s
);
617 format_ipv6_netmask(struct ds
*s
, const char *name
,
618 const struct in6_addr
*addr
,
619 const struct in6_addr
*netmask
)
621 if (!ipv6_mask_is_any(netmask
)) {
622 ds_put_format(s
, "%s=", name
);
623 print_ipv6_masked(s
, addr
, netmask
);
630 format_be16_masked(struct ds
*s
, const char *name
,
631 ovs_be16 value
, ovs_be16 mask
)
633 if (mask
!= htons(0)) {
634 ds_put_format(s
, "%s=", name
);
635 if (mask
== htons(UINT16_MAX
)) {
636 ds_put_format(s
, "%"PRIu16
, ntohs(value
));
638 ds_put_format(s
, "0x%"PRIx16
"/0x%"PRIx16
,
639 ntohs(value
), ntohs(mask
));
645 /* Appends a string representation of 'match' to 's'. If 'priority' is
646 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
648 match_format(const struct match
*match
, struct ds
*s
, unsigned int priority
)
650 const struct flow_wildcards
*wc
= &match
->wc
;
651 size_t start_len
= s
->length
;
652 const struct flow
*f
= &match
->flow
;
653 bool skip_type
= false;
654 bool skip_proto
= false;
658 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 17);
660 if (priority
!= OFP_DEFAULT_PRIORITY
) {
661 ds_put_format(s
, "priority=%u,", priority
);
664 if (wc
->masks
.dl_type
) {
666 if (f
->dl_type
== htons(ETH_TYPE_IP
)) {
667 if (wc
->masks
.nw_proto
) {
669 if (f
->nw_proto
== IPPROTO_ICMP
) {
670 ds_put_cstr(s
, "icmp,");
671 } else if (f
->nw_proto
== IPPROTO_TCP
) {
672 ds_put_cstr(s
, "tcp,");
673 } else if (f
->nw_proto
== IPPROTO_UDP
) {
674 ds_put_cstr(s
, "udp,");
676 ds_put_cstr(s
, "ip,");
680 ds_put_cstr(s
, "ip,");
682 } else if (f
->dl_type
== htons(ETH_TYPE_IPV6
)) {
683 if (wc
->masks
.nw_proto
) {
685 if (f
->nw_proto
== IPPROTO_ICMPV6
) {
686 ds_put_cstr(s
, "icmp6,");
687 } else if (f
->nw_proto
== IPPROTO_TCP
) {
688 ds_put_cstr(s
, "tcp6,");
689 } else if (f
->nw_proto
== IPPROTO_UDP
) {
690 ds_put_cstr(s
, "udp6,");
692 ds_put_cstr(s
, "ipv6,");
696 ds_put_cstr(s
, "ipv6,");
698 } else if (f
->dl_type
== htons(ETH_TYPE_ARP
)) {
699 ds_put_cstr(s
, "arp,");
700 } else if (f
->dl_type
== htons(ETH_TYPE_RARP
)) {
701 ds_put_cstr(s
, "rarp,");
706 for (i
= 0; i
< FLOW_N_REGS
; i
++) {
707 switch (wc
->masks
.regs
[i
]) {
711 ds_put_format(s
, "reg%d=0x%"PRIx32
",", i
, f
->regs
[i
]);
714 ds_put_format(s
, "reg%d=0x%"PRIx32
"/0x%"PRIx32
",",
715 i
, f
->regs
[i
], wc
->masks
.regs
[i
]);
719 switch (wc
->masks
.tunnel
.tun_id
) {
722 case CONSTANT_HTONLL(UINT64_MAX
):
723 ds_put_format(s
, "tun_id=%#"PRIx64
",", ntohll(f
->tunnel
.tun_id
));
726 ds_put_format(s
, "tun_id=%#"PRIx64
"/%#"PRIx64
",",
727 ntohll(f
->tunnel
.tun_id
),
728 ntohll(wc
->masks
.tunnel
.tun_id
));
731 switch (wc
->masks
.metadata
) {
734 case CONSTANT_HTONLL(UINT64_MAX
):
735 ds_put_format(s
, "metadata=%#"PRIx64
",", ntohll(f
->metadata
));
738 ds_put_format(s
, "metadata=%#"PRIx64
"/%#"PRIx64
",",
739 ntohll(f
->metadata
), ntohll(wc
->masks
.metadata
));
742 if (wc
->masks
.in_port
) {
743 ds_put_format(s
, "in_port=%"PRIu16
",", f
->in_port
);
745 if (wc
->masks
.vlan_tci
) {
746 ovs_be16 vid_mask
= wc
->masks
.vlan_tci
& htons(VLAN_VID_MASK
);
747 ovs_be16 pcp_mask
= wc
->masks
.vlan_tci
& htons(VLAN_PCP_MASK
);
748 ovs_be16 cfi
= wc
->masks
.vlan_tci
& htons(VLAN_CFI
);
750 if (cfi
&& f
->vlan_tci
& htons(VLAN_CFI
)
751 && (!vid_mask
|| vid_mask
== htons(VLAN_VID_MASK
))
752 && (!pcp_mask
|| pcp_mask
== htons(VLAN_PCP_MASK
))
753 && (vid_mask
|| pcp_mask
)) {
755 ds_put_format(s
, "dl_vlan=%"PRIu16
",",
756 vlan_tci_to_vid(f
->vlan_tci
));
759 ds_put_format(s
, "dl_vlan_pcp=%d,",
760 vlan_tci_to_pcp(f
->vlan_tci
));
762 } else if (wc
->masks
.vlan_tci
== htons(0xffff)) {
763 ds_put_format(s
, "vlan_tci=0x%04"PRIx16
",", ntohs(f
->vlan_tci
));
765 ds_put_format(s
, "vlan_tci=0x%04"PRIx16
"/0x%04"PRIx16
",",
766 ntohs(f
->vlan_tci
), ntohs(wc
->masks
.vlan_tci
));
769 format_eth_masked(s
, "dl_src", f
->dl_src
, wc
->masks
.dl_src
);
770 format_eth_masked(s
, "dl_dst", f
->dl_dst
, wc
->masks
.dl_dst
);
771 if (!skip_type
&& wc
->masks
.dl_type
) {
772 ds_put_format(s
, "dl_type=0x%04"PRIx16
",", ntohs(f
->dl_type
));
774 if (f
->dl_type
== htons(ETH_TYPE_IPV6
)) {
775 format_ipv6_netmask(s
, "ipv6_src", &f
->ipv6_src
, &wc
->masks
.ipv6_src
);
776 format_ipv6_netmask(s
, "ipv6_dst", &f
->ipv6_dst
, &wc
->masks
.ipv6_dst
);
777 if (wc
->masks
.ipv6_label
) {
778 if (wc
->masks
.ipv6_label
== htonl(UINT32_MAX
)) {
779 ds_put_format(s
, "ipv6_label=0x%05"PRIx32
",",
780 ntohl(f
->ipv6_label
));
782 ds_put_format(s
, "ipv6_label=0x%05"PRIx32
"/0x%05"PRIx32
",",
783 ntohl(f
->ipv6_label
),
784 ntohl(wc
->masks
.ipv6_label
));
787 } else if (f
->dl_type
== htons(ETH_TYPE_ARP
) ||
788 f
->dl_type
== htons(ETH_TYPE_RARP
)) {
789 format_ip_netmask(s
, "arp_spa", f
->nw_src
, wc
->masks
.nw_src
);
790 format_ip_netmask(s
, "arp_tpa", f
->nw_dst
, wc
->masks
.nw_dst
);
792 format_ip_netmask(s
, "nw_src", f
->nw_src
, wc
->masks
.nw_src
);
793 format_ip_netmask(s
, "nw_dst", f
->nw_dst
, wc
->masks
.nw_dst
);
795 if (!skip_proto
&& wc
->masks
.nw_proto
) {
796 if (f
->dl_type
== htons(ETH_TYPE_ARP
) ||
797 f
->dl_type
== htons(ETH_TYPE_RARP
)) {
798 ds_put_format(s
, "arp_op=%"PRIu8
",", f
->nw_proto
);
800 ds_put_format(s
, "nw_proto=%"PRIu8
",", f
->nw_proto
);
803 if (f
->dl_type
== htons(ETH_TYPE_ARP
) ||
804 f
->dl_type
== htons(ETH_TYPE_RARP
)) {
805 format_eth_masked(s
, "arp_sha", f
->arp_sha
, wc
->masks
.arp_sha
);
806 format_eth_masked(s
, "arp_tha", f
->arp_tha
, wc
->masks
.arp_tha
);
808 if (wc
->masks
.nw_tos
& IP_DSCP_MASK
) {
809 ds_put_format(s
, "nw_tos=%"PRIu8
",", f
->nw_tos
& IP_DSCP_MASK
);
811 if (wc
->masks
.nw_tos
& IP_ECN_MASK
) {
812 ds_put_format(s
, "nw_ecn=%"PRIu8
",", f
->nw_tos
& IP_ECN_MASK
);
814 if (wc
->masks
.nw_ttl
) {
815 ds_put_format(s
, "nw_ttl=%"PRIu8
",", f
->nw_ttl
);
817 switch (wc
->masks
.nw_frag
) {
818 case FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
:
819 ds_put_format(s
, "nw_frag=%s,",
820 f
->nw_frag
& FLOW_NW_FRAG_ANY
821 ? (f
->nw_frag
& FLOW_NW_FRAG_LATER
? "later" : "first")
822 : (f
->nw_frag
& FLOW_NW_FRAG_LATER
? "<error>" : "no"));
825 case FLOW_NW_FRAG_ANY
:
826 ds_put_format(s
, "nw_frag=%s,",
827 f
->nw_frag
& FLOW_NW_FRAG_ANY
? "yes" : "no");
830 case FLOW_NW_FRAG_LATER
:
831 ds_put_format(s
, "nw_frag=%s,",
832 f
->nw_frag
& FLOW_NW_FRAG_LATER
? "later" : "not_later");
835 if (f
->dl_type
== htons(ETH_TYPE_IP
) &&
836 f
->nw_proto
== IPPROTO_ICMP
) {
837 format_be16_masked(s
, "icmp_type", f
->tp_src
, wc
->masks
.tp_src
);
838 format_be16_masked(s
, "icmp_code", f
->tp_dst
, wc
->masks
.tp_dst
);
839 } else if (f
->dl_type
== htons(ETH_TYPE_IPV6
) &&
840 f
->nw_proto
== IPPROTO_ICMPV6
) {
841 format_be16_masked(s
, "icmp_type", f
->tp_src
, wc
->masks
.tp_src
);
842 format_be16_masked(s
, "icmp_code", f
->tp_dst
, wc
->masks
.tp_dst
);
843 format_ipv6_netmask(s
, "nd_target", &f
->nd_target
,
844 &wc
->masks
.nd_target
);
845 format_eth_masked(s
, "nd_sll", f
->arp_sha
, wc
->masks
.arp_sha
);
846 format_eth_masked(s
, "nd_tll", f
->arp_tha
, wc
->masks
.arp_tha
);
848 format_be16_masked(s
, "tp_src", f
->tp_src
, wc
->masks
.tp_src
);
849 format_be16_masked(s
, "tp_dst", f
->tp_dst
, wc
->masks
.tp_dst
);
852 if (s
->length
> start_len
&& ds_last(s
) == ',') {
857 /* Converts 'match' to a string and returns the string. If 'priority' is
858 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
859 * must free the string (with free()). */
861 match_to_string(const struct match
*match
, unsigned int priority
)
863 struct ds s
= DS_EMPTY_INITIALIZER
;
864 match_format(match
, &s
, priority
);
865 return ds_steal_cstr(&s
);
869 match_print(const struct match
*match
)
871 char *s
= match_to_string(match
, OFP_DEFAULT_PRIORITY
);
876 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
877 * with minimatch_destroy(). */
879 minimatch_init(struct minimatch
*dst
, const struct match
*src
)
881 miniflow_init(&dst
->flow
, &src
->flow
);
882 minimask_init(&dst
->mask
, &src
->wc
);
885 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
886 * with minimatch_destroy(). */
888 minimatch_clone(struct minimatch
*dst
, const struct minimatch
*src
)
890 miniflow_clone(&dst
->flow
, &src
->flow
);
891 minimask_clone(&dst
->mask
, &src
->mask
);
894 /* Frees any memory owned by 'match'. Does not free the storage in which
895 * 'match' itself resides; the caller is responsible for that. */
897 minimatch_destroy(struct minimatch
*match
)
899 miniflow_destroy(&match
->flow
);
900 minimask_destroy(&match
->mask
);
903 /* Initializes 'dst' as a copy of 'src'. */
905 minimatch_expand(const struct minimatch
*src
, struct match
*dst
)
907 miniflow_expand(&src
->flow
, &dst
->flow
);
908 minimask_expand(&src
->mask
, &dst
->wc
);
911 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
913 minimatch_equal(const struct minimatch
*a
, const struct minimatch
*b
)
915 return (miniflow_equal(&a
->flow
, &b
->flow
)
916 && minimask_equal(&a
->mask
, &b
->mask
));
919 /* Returns a hash value for 'match', given 'basis'. */
921 minimatch_hash(const struct minimatch
*match
, uint32_t basis
)
923 return miniflow_hash(&match
->flow
, minimask_hash(&match
->mask
, basis
));
926 /* Appends a string representation of 'match' to 's'. If 'priority' is
927 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
929 minimatch_format(const struct minimatch
*match
, struct ds
*s
,
930 unsigned int priority
)
932 struct match megamatch
;
934 minimatch_expand(match
, &megamatch
);
935 match_format(&megamatch
, s
, priority
);
938 /* Converts 'match' to a string and returns the string. If 'priority' is
939 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
940 * must free the string (with free()). */
942 minimatch_to_string(const struct minimatch
*match
, unsigned int priority
)
944 struct match megamatch
;
946 minimatch_expand(match
, &megamatch
);
947 return match_to_string(&megamatch
, priority
);