]> git.proxmox.com Git - mirror_ovs.git/blame - lib/match.c
ovs-vswitchd: Better document that ovs-vswitchd manages its own datapaths.
[mirror_ovs.git] / lib / match.c
CommitLineData
81a76618 1/*
fd13c6b5 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
81a76618
BP
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <config.h>
e29747e4 18#include "openvswitch/match.h"
81a76618 19#include <stdlib.h>
e29747e4 20#include "flow.h"
81a76618 21#include "byte-order.h"
0deec6d2 22#include "colors.h"
3e8a2ad1 23#include "openvswitch/dynamic-string.h"
0d71302e
BP
24#include "openvswitch/meta-flow.h"
25#include "openvswitch/ofp-port.h"
81a76618 26#include "packets.h"
9558d2a5 27#include "tun-metadata.h"
3d2fbd70 28#include "openvswitch/nsh.h"
81a76618
BP
29
30/* Converts the flow in 'flow' into a match in 'match', with the given
31 * 'wildcards'. */
32void
33match_init(struct match *match,
34 const struct flow *flow, const struct flow_wildcards *wc)
35{
36 match->flow = *flow;
37 match->wc = *wc;
38 match_zero_wildcarded_fields(match);
9558d2a5 39 memset(&match->tun_md, 0, sizeof match->tun_md);
81a76618
BP
40}
41
aa6c9932
MM
42/* Converts a flow into a match. It sets the wildcard masks based on
43 * the packet contents. It will not set the mask for fields that do not
44 * make sense for the packet type. */
45void
46match_wc_init(struct match *match, const struct flow *flow)
47{
aa6c9932 48 match->flow = *flow;
aa6c9932 49
78c9486d
JR
50 flow_wildcards_init_for_packet(&match->wc, flow);
51 WC_MASK_FIELD(&match->wc, regs);
52 WC_MASK_FIELD(&match->wc, metadata);
9558d2a5
JG
53
54 memset(&match->tun_md, 0, sizeof match->tun_md);
aa6c9932
MM
55}
56
81a76618
BP
57/* Initializes 'match' as a "catch-all" match that matches every packet. */
58void
59match_init_catchall(struct match *match)
60{
61 memset(&match->flow, 0, sizeof match->flow);
62 flow_wildcards_init_catchall(&match->wc);
9558d2a5 63 memset(&match->tun_md, 0, sizeof match->tun_md);
81a76618
BP
64}
65
66/* For each bit or field wildcarded in 'match', sets the corresponding bit or
67 * field in 'flow' to all-0-bits. It is important to maintain this invariant
68 * in a match that might be inserted into a classifier.
69 *
70 * It is never necessary to call this function directly for a match that is
71 * initialized or modified only by match_*() functions. It is useful to
72 * restore the invariant in a match whose 'wc' member is modified by hand.
73 */
74void
75match_zero_wildcarded_fields(struct match *match)
76{
77 flow_zero_wildcards(&match->flow, &match->wc);
78}
79
a79f29f2
AZ
80void
81match_set_dp_hash(struct match *match, uint32_t value)
82{
83 match_set_dp_hash_masked(match, value, UINT32_MAX);
84}
85
86void
87match_set_dp_hash_masked(struct match *match, uint32_t value, uint32_t mask)
88{
89 match->wc.masks.dp_hash = mask;
90 match->flow.dp_hash = value & mask;
91}
92
93void
94match_set_recirc_id(struct match *match, uint32_t value)
95{
96 match->flow.recirc_id = value;
97 match->wc.masks.recirc_id = UINT32_MAX;
98}
99
18080541
BP
100void
101match_set_conj_id(struct match *match, uint32_t value)
102{
103 match->flow.conj_id = value;
104 match->wc.masks.conj_id = UINT32_MAX;
105}
106
81a76618
BP
107void
108match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
109{
110 match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
111}
112
113void
114match_set_reg_masked(struct match *match, unsigned int reg_idx,
115 uint32_t value, uint32_t mask)
116{
cb22974d 117 ovs_assert(reg_idx < FLOW_N_REGS);
81a76618
BP
118 flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
119 match->flow.regs[reg_idx] = value & mask;
120}
121
79fe0f46
BP
122void
123match_set_xreg(struct match *match, unsigned int xreg_idx, uint64_t value)
124{
125 match_set_xreg_masked(match, xreg_idx, value, UINT64_MAX);
126}
127
128void
129match_set_xreg_masked(struct match *match, unsigned int xreg_idx,
130 uint64_t value, uint64_t mask)
131{
132 ovs_assert(xreg_idx < FLOW_N_XREGS);
133 flow_wildcards_set_xreg_mask(&match->wc, xreg_idx, mask);
134 flow_set_xreg(&match->flow, xreg_idx, value & mask);
135}
136
b23ada8e
JP
137void
138match_set_xxreg(struct match *match, unsigned int xxreg_idx, ovs_u128 value)
139{
140 match_set_xxreg_masked(match, xxreg_idx, value, OVS_U128_MAX);
141}
142
143void
144match_set_xxreg_masked(struct match *match, unsigned int xxreg_idx,
145 ovs_u128 value, ovs_u128 mask)
146{
147 ovs_assert(xxreg_idx < FLOW_N_XXREGS);
148 flow_wildcards_set_xxreg_mask(&match->wc, xxreg_idx, mask);
149 flow_set_xxreg(&match->flow, xxreg_idx, ovs_u128_and(value, mask));
150}
151
c61f3870
BP
152void
153match_set_actset_output(struct match *match, ofp_port_t actset_output)
154{
155 match->wc.masks.actset_output = u16_to_ofp(UINT16_MAX);
156 match->flow.actset_output = actset_output;
157}
158
81a76618
BP
159void
160match_set_metadata(struct match *match, ovs_be64 metadata)
161{
b8266395 162 match_set_metadata_masked(match, metadata, OVS_BE64_MAX);
81a76618
BP
163}
164
165void
166match_set_metadata_masked(struct match *match,
167 ovs_be64 metadata, ovs_be64 mask)
168{
169 match->wc.masks.metadata = mask;
170 match->flow.metadata = metadata & mask;
171}
172
173void
174match_set_tun_id(struct match *match, ovs_be64 tun_id)
175{
b8266395 176 match_set_tun_id_masked(match, tun_id, OVS_BE64_MAX);
81a76618
BP
177}
178
179void
180match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
181{
296e07ac
JG
182 match->wc.masks.tunnel.tun_id = mask;
183 match->flow.tunnel.tun_id = tun_id & mask;
81a76618
BP
184}
185
4fe3445a
PS
186void
187match_set_tun_src(struct match *match, ovs_be32 src)
188{
b8266395 189 match_set_tun_src_masked(match, src, OVS_BE32_MAX);
4fe3445a
PS
190}
191
192void
193match_set_tun_src_masked(struct match *match, ovs_be32 src, ovs_be32 mask)
194{
195 match->wc.masks.tunnel.ip_src = mask;
196 match->flow.tunnel.ip_src = src & mask;
197}
198
199void
200match_set_tun_dst(struct match *match, ovs_be32 dst)
201{
b8266395 202 match_set_tun_dst_masked(match, dst, OVS_BE32_MAX);
4fe3445a
PS
203}
204
205void
206match_set_tun_dst_masked(struct match *match, ovs_be32 dst, ovs_be32 mask)
207{
208 match->wc.masks.tunnel.ip_dst = mask;
209 match->flow.tunnel.ip_dst = dst & mask;
210}
211
ffe4c74f
JB
212void
213match_set_tun_ipv6_src(struct match *match, const struct in6_addr *src)
214{
215 match->flow.tunnel.ipv6_src = *src;
216 match->wc.masks.tunnel.ipv6_src = in6addr_exact;
217}
218
219void
220match_set_tun_ipv6_src_masked(struct match *match, const struct in6_addr *src,
221 const struct in6_addr *mask)
222{
223 match->flow.tunnel.ipv6_src = ipv6_addr_bitand(src, mask);
224 match->wc.masks.tunnel.ipv6_src = *mask;
225}
226
227void
228match_set_tun_ipv6_dst(struct match *match, const struct in6_addr *dst)
229{
230 match->flow.tunnel.ipv6_dst = *dst;
231 match->wc.masks.tunnel.ipv6_dst = in6addr_exact;
232}
233
234void
235match_set_tun_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
236 const struct in6_addr *mask)
237{
238 match->flow.tunnel.ipv6_dst = ipv6_addr_bitand(dst, mask);
239 match->wc.masks.tunnel.ipv6_dst = *mask;
240}
241
4fe3445a
PS
242void
243match_set_tun_ttl(struct match *match, uint8_t ttl)
244{
245 match_set_tun_ttl_masked(match, ttl, UINT8_MAX);
246}
247
248void
249match_set_tun_ttl_masked(struct match *match, uint8_t ttl, uint8_t mask)
250{
251 match->wc.masks.tunnel.ip_ttl = mask;
252 match->flow.tunnel.ip_ttl = ttl & mask;
253}
254
255void
256match_set_tun_tos(struct match *match, uint8_t tos)
257{
258 match_set_tun_tos_masked(match, tos, UINT8_MAX);
259}
260
261void
262match_set_tun_tos_masked(struct match *match, uint8_t tos, uint8_t mask)
263{
264 match->wc.masks.tunnel.ip_tos = mask;
265 match->flow.tunnel.ip_tos = tos & mask;
266}
267
268void
269match_set_tun_flags(struct match *match, uint16_t flags)
270{
271 match_set_tun_flags_masked(match, flags, UINT16_MAX);
272}
273
274void
275match_set_tun_flags_masked(struct match *match, uint16_t flags, uint16_t mask)
276{
b666962b
JG
277 mask &= FLOW_TNL_PUB_F_MASK;
278
4fe3445a
PS
279 match->wc.masks.tunnel.flags = mask;
280 match->flow.tunnel.flags = flags & mask;
281}
282
3def36a8
RD
283void
284match_set_tun_tp_dst(struct match *match, ovs_be16 tp_dst)
285{
286 match_set_tun_tp_dst_masked(match, tp_dst, OVS_BE16_MAX);
287}
288
289void
290match_set_tun_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
291{
292 match->wc.masks.tunnel.tp_dst = mask;
293 match->flow.tunnel.tp_dst = port & mask;
294}
295
ac6073e3
MC
296void
297match_set_tun_gbp_id_masked(struct match *match, ovs_be16 gbp_id, ovs_be16 mask)
298{
299 match->wc.masks.tunnel.gbp_id = mask;
300 match->flow.tunnel.gbp_id = gbp_id & mask;
301}
302
303void
304match_set_tun_gbp_id(struct match *match, ovs_be16 gbp_id)
305{
306 match_set_tun_gbp_id_masked(match, gbp_id, OVS_BE16_MAX);
307}
308
309void
310match_set_tun_gbp_flags_masked(struct match *match, uint8_t flags, uint8_t mask)
311{
312 match->wc.masks.tunnel.gbp_flags = mask;
313 match->flow.tunnel.gbp_flags = flags & mask;
314}
315
316void
317match_set_tun_gbp_flags(struct match *match, uint8_t flags)
318{
319 match_set_tun_gbp_flags_masked(match, flags, UINT8_MAX);
320}
321
81a76618 322void
4e022ec0 323match_set_in_port(struct match *match, ofp_port_t ofp_port)
81a76618 324{
4e022ec0
AW
325 match->wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
326 match->flow.in_port.ofp_port = ofp_port;
81a76618
BP
327}
328
1b567fb9
AA
329void
330match_set_skb_priority(struct match *match, uint32_t skb_priority)
331{
332 match->wc.masks.skb_priority = UINT32_MAX;
333 match->flow.skb_priority = skb_priority;
334}
335
336void
1362e248 337match_set_pkt_mark(struct match *match, uint32_t pkt_mark)
1b567fb9 338{
ac923e91
JG
339 match_set_pkt_mark_masked(match, pkt_mark, UINT32_MAX);
340}
341
342void
343match_set_pkt_mark_masked(struct match *match, uint32_t pkt_mark, uint32_t mask)
344{
345 match->flow.pkt_mark = pkt_mark & mask;
346 match->wc.masks.pkt_mark = mask;
1b567fb9
AA
347}
348
07659514
JS
349void
350match_set_ct_state(struct match *match, uint32_t ct_state)
351{
352 match_set_ct_state_masked(match, ct_state, UINT32_MAX);
353}
354
355void
356match_set_ct_state_masked(struct match *match, uint32_t ct_state, uint32_t mask)
357{
2a28ccc8
JR
358 match->flow.ct_state = ct_state & mask & UINT8_MAX;
359 match->wc.masks.ct_state = mask & UINT8_MAX;
07659514
JS
360}
361
362void
363match_set_ct_zone(struct match *match, uint16_t ct_zone)
364{
365 match->flow.ct_zone = ct_zone;
366 match->wc.masks.ct_zone = UINT16_MAX;
367}
368
8e53fe8c
JS
369void
370match_set_ct_mark(struct match *match, uint32_t ct_mark)
371{
372 match_set_ct_mark_masked(match, ct_mark, UINT32_MAX);
373}
374
375void
376match_set_ct_mark_masked(struct match *match, uint32_t ct_mark,
377 uint32_t mask)
378{
379 match->flow.ct_mark = ct_mark & mask;
380 match->wc.masks.ct_mark = mask;
381}
382
9daf2348
JS
383void
384match_set_ct_label(struct match *match, ovs_u128 ct_label)
385{
386 ovs_u128 mask;
387
388 mask.u64.lo = UINT64_MAX;
389 mask.u64.hi = UINT64_MAX;
390 match_set_ct_label_masked(match, ct_label, mask);
391}
392
393void
394match_set_ct_label_masked(struct match *match, ovs_u128 value, ovs_u128 mask)
395{
396 match->flow.ct_label.u64.lo = value.u64.lo & mask.u64.lo;
397 match->flow.ct_label.u64.hi = value.u64.hi & mask.u64.hi;
398 match->wc.masks.ct_label = mask;
399}
400
daf4d3c1
JR
401void
402match_set_ct_nw_src(struct match *match, ovs_be32 ct_nw_src)
403{
404 match->flow.ct_nw_src = ct_nw_src;
405 match->wc.masks.ct_nw_src = OVS_BE32_MAX;
406}
407
408void
409match_set_ct_nw_src_masked(struct match *match, ovs_be32 ct_nw_src,
410 ovs_be32 mask)
411{
412 match->flow.ct_nw_src = ct_nw_src & mask;
413 match->wc.masks.ct_nw_src = mask;
414}
415
416void
417match_set_ct_nw_dst(struct match *match, ovs_be32 ct_nw_dst)
418{
419 match->flow.ct_nw_dst = ct_nw_dst;
420 match->wc.masks.ct_nw_dst = OVS_BE32_MAX;
421}
422
423void
424match_set_ct_nw_dst_masked(struct match *match, ovs_be32 ct_nw_dst,
425 ovs_be32 mask)
426{
427 match->flow.ct_nw_dst = ct_nw_dst & mask;
428 match->wc.masks.ct_nw_dst = mask;
429}
430
431void
432match_set_ct_nw_proto(struct match *match, uint8_t ct_nw_proto)
433{
434 match->flow.ct_nw_proto = ct_nw_proto;
435 match->wc.masks.ct_nw_proto = UINT8_MAX;
436}
437
438void
439match_set_ct_tp_src(struct match *match, ovs_be16 ct_tp_src)
440{
441 match_set_ct_tp_src_masked(match, ct_tp_src, OVS_BE16_MAX);
442}
443
444void
445match_set_ct_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
446{
447 match->flow.ct_tp_src = port & mask;
448 match->wc.masks.ct_tp_src = mask;
449}
450
451void
452match_set_ct_tp_dst(struct match *match, ovs_be16 ct_tp_dst)
453{
454 match_set_ct_tp_dst_masked(match, ct_tp_dst, OVS_BE16_MAX);
455}
456
457void
458match_set_ct_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
459{
460 match->flow.ct_tp_dst = port & mask;
461 match->wc.masks.ct_tp_dst = mask;
462}
463
464void
465match_set_ct_ipv6_src(struct match *match, const struct in6_addr *src)
466{
467 match->flow.ct_ipv6_src = *src;
468 match->wc.masks.ct_ipv6_src = in6addr_exact;
469}
470
471void
472match_set_ct_ipv6_src_masked(struct match *match, const struct in6_addr *src,
473 const struct in6_addr *mask)
474{
475 match->flow.ct_ipv6_src = ipv6_addr_bitand(src, mask);
476 match->wc.masks.ct_ipv6_src = *mask;
477}
478
479void
480match_set_ct_ipv6_dst(struct match *match, const struct in6_addr *dst)
481{
482 match->flow.ct_ipv6_dst = *dst;
483 match->wc.masks.ct_ipv6_dst = in6addr_exact;
484}
485
486void
487match_set_ct_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
488 const struct in6_addr *mask)
489{
490 match->flow.ct_ipv6_dst = ipv6_addr_bitand(dst, mask);
491 match->wc.masks.ct_ipv6_dst = *mask;
492}
493
beb75a40
JS
494void
495match_set_packet_type(struct match *match, ovs_be32 packet_type)
496{
497 match->flow.packet_type = packet_type;
498 match->wc.masks.packet_type = OVS_BE32_MAX;
499}
500
3d4b2e6e
JS
501/* If 'match' does not match on any packet type, make it match on Ethernet
502 * packets (the default packet type, as specified by OpenFlow). */
503void
504match_set_default_packet_type(struct match *match)
505{
506 if (!match->wc.masks.packet_type) {
507 match_set_packet_type(match, htonl(PT_ETH));
508 }
509}
510
511/* Returns true if 'match' matches only Ethernet packets (the default packet
512 * type, as specified by OpenFlow). */
513bool
514match_has_default_packet_type(const struct match *match)
515{
516 return (match->flow.packet_type == htonl(PT_ETH)
517 && match->wc.masks.packet_type == OVS_BE32_MAX);
518}
519
520/* A match on 'field' is being added to or has been added to 'match'. If
521 * 'field' is a data field, and 'match' does not already match on packet_type,
522 * this function make it match on the Ethernet packet_type.
523 *
524 * This function is useful because OpenFlow implicitly applies to Ethernet
525 * packets when there's no explicit packet_type, but matching on a metadata
526 * field doesn't imply anything about the packet_type and falsely inferring
527 * that it does can cause harm. A flow that matches only on metadata fields,
528 * for example, should be able to match more than just Ethernet flows. There
529 * are also important reasons that a catch-all match (one with no field matches
530 * at all) should not imply a packet_type(0,0) match. For example, a "flow
531 * dump" request that matches on no fields should return every flow in the
532 * switch, not just the flows that match on Ethernet. As a second example,
533 * OpenFlow 1.2+ special-cases "table miss" flows, that is catch-all flows with
534 * priority 0, and inferring a match on packet_type(0,0) causes such a flow not
535 * to be a table miss flow. */
536void
537match_add_ethernet_prereq(struct match *match, const struct mf_field *field)
538{
539 if (field->prereqs != MFP_NONE) {
540 match_set_default_packet_type(match);
541 }
542}
543
81a76618
BP
544void
545match_set_dl_type(struct match *match, ovs_be16 dl_type)
546{
b8266395 547 match->wc.masks.dl_type = OVS_BE16_MAX;
81a76618
BP
548 match->flow.dl_type = dl_type;
549}
550
551/* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
552 * exactly. 'mask_dst' is set to all 1s. */
553static void
74ff3298
JR
554set_eth(const struct eth_addr value_src,
555 struct eth_addr *value_dst,
556 struct eth_addr *mask_dst)
81a76618 557{
74ff3298
JR
558 *value_dst = value_src;
559 *mask_dst = eth_addr_exact;
81a76618
BP
560}
561
562/* Modifies 'value_src' so that the Ethernet address must match 'value_src'
563 * after each byte is ANDed with the appropriate byte in 'mask_src'.
564 * 'mask_dst' is set to 'mask_src' */
565static void
74ff3298
JR
566set_eth_masked(const struct eth_addr value_src,
567 const struct eth_addr mask_src,
568 struct eth_addr *value_dst, struct eth_addr *mask_dst)
81a76618
BP
569{
570 size_t i;
571
74ff3298
JR
572 for (i = 0; i < ARRAY_SIZE(value_dst->be16); i++) {
573 value_dst->be16[i] = value_src.be16[i] & mask_src.be16[i];
81a76618 574 }
74ff3298 575 *mask_dst = mask_src;
81a76618
BP
576}
577
578/* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
579 * exactly. */
580void
74ff3298 581match_set_dl_src(struct match *match, const struct eth_addr dl_src)
81a76618 582{
74ff3298 583 set_eth(dl_src, &match->flow.dl_src, &match->wc.masks.dl_src);
81a76618
BP
584}
585
586/* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
587 * after each byte is ANDed with the appropriate byte in 'mask'. */
588void
589match_set_dl_src_masked(struct match *match,
74ff3298
JR
590 const struct eth_addr dl_src,
591 const struct eth_addr mask)
81a76618 592{
74ff3298 593 set_eth_masked(dl_src, mask, &match->flow.dl_src, &match->wc.masks.dl_src);
81a76618
BP
594}
595
596/* Modifies 'match' so that the Ethernet address must match 'dl_dst'
597 * exactly. */
598void
74ff3298 599match_set_dl_dst(struct match *match, const struct eth_addr dl_dst)
81a76618 600{
74ff3298 601 set_eth(dl_dst, &match->flow.dl_dst, &match->wc.masks.dl_dst);
81a76618
BP
602}
603
604/* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
605 * byte is ANDed with the appropriate byte in 'mask'.
606 *
607 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
608 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
609void
610match_set_dl_dst_masked(struct match *match,
74ff3298
JR
611 const struct eth_addr dl_dst,
612 const struct eth_addr mask)
81a76618 613{
74ff3298 614 set_eth_masked(dl_dst, mask, &match->flow.dl_dst, &match->wc.masks.dl_dst);
81a76618
BP
615}
616
617void
618match_set_dl_tci(struct match *match, ovs_be16 tci)
619{
620 match_set_dl_tci_masked(match, tci, htons(0xffff));
621}
622
623void
624match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
625{
f0fb825a
EG
626 match->flow.vlans[0].tci = tci & mask;
627 match->wc.masks.vlans[0].tci = mask;
81a76618
BP
628}
629
630/* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
631 * wildcarded, then 'match' will match a packet regardless of whether it has an
632 * 802.1Q header or not. */
633void
634match_set_any_vid(struct match *match)
635{
f0fb825a
EG
636 if (match->wc.masks.vlans[0].tci & htons(VLAN_PCP_MASK)) {
637 match->wc.masks.vlans[0].tci &= ~htons(VLAN_VID_MASK);
638 match->flow.vlans[0].tci &= ~htons(VLAN_VID_MASK);
81a76618
BP
639 } else {
640 match_set_dl_tci_masked(match, htons(0), htons(0));
641 }
642}
643
644/* Modifies 'match' depending on 'dl_vlan':
645 *
646 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
647 * without an 802.1Q header.
648 *
649 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
650 * VID equals the low 12 bits of 'dl_vlan'.
651 */
652void
653match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
654{
655 flow_set_dl_vlan(&match->flow, dl_vlan);
656 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
f0fb825a 657 match->wc.masks.vlans[0].tci = OVS_BE16_MAX;
81a76618 658 } else {
f0fb825a 659 match->wc.masks.vlans[0].tci |= htons(VLAN_VID_MASK | VLAN_CFI);
81a76618
BP
660 }
661}
662
663/* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
664 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
665 * plus CFI). */
666void
667match_set_vlan_vid(struct match *match, ovs_be16 vid)
668{
669 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
670}
671
672
673/* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
674 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
675 * plus CFI), with the corresponding 'mask'. */
676void
677match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
678{
679 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
680 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
681
682 mask &= vid_mask;
683 flow_set_vlan_vid(&match->flow, vid & mask);
f0fb825a
EG
684 match->wc.masks.vlans[0].tci =
685 mask | (match->wc.masks.vlans[0].tci & pcp_mask);
81a76618
BP
686}
687
688/* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
689 * wildcarded, then 'match' will match a packet regardless of whether it has an
690 * 802.1Q header or not. */
691void
692match_set_any_pcp(struct match *match)
693{
f0fb825a
EG
694 if (match->wc.masks.vlans[0].tci & htons(VLAN_VID_MASK)) {
695 match->wc.masks.vlans[0].tci &= ~htons(VLAN_PCP_MASK);
696 match->flow.vlans[0].tci &= ~htons(VLAN_PCP_MASK);
81a76618
BP
697 } else {
698 match_set_dl_tci_masked(match, htons(0), htons(0));
699 }
700}
701
702/* Modifies 'match' so that it matches only packets with an 802.1Q header whose
703 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
704void
705match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
706{
707 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
f0fb825a 708 match->wc.masks.vlans[0].tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
81a76618
BP
709}
710
8bfd0fda
BP
711/* Modifies 'match' so that the MPLS label 'idx' matches 'lse' exactly. */
712void
713match_set_mpls_lse(struct match *match, int idx, ovs_be32 lse)
714{
715 match->wc.masks.mpls_lse[idx] = OVS_BE32_MAX;
716 match->flow.mpls_lse[idx] = lse;
717}
718
b02475c5
SH
719/* Modifies 'match' so that the MPLS label is wildcarded. */
720void
8bfd0fda 721match_set_any_mpls_label(struct match *match, int idx)
b02475c5 722{
8bfd0fda
BP
723 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_LABEL_MASK);
724 flow_set_mpls_label(&match->flow, idx, htonl(0));
b02475c5
SH
725}
726
727/* Modifies 'match' so that it matches only packets with an MPLS header whose
728 * label equals the low 20 bits of 'mpls_label'. */
729void
8bfd0fda 730match_set_mpls_label(struct match *match, int idx, ovs_be32 mpls_label)
b02475c5 731{
8bfd0fda
BP
732 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_LABEL_MASK);
733 flow_set_mpls_label(&match->flow, idx, mpls_label);
b02475c5
SH
734}
735
736/* Modifies 'match' so that the MPLS TC is wildcarded. */
737void
8bfd0fda 738match_set_any_mpls_tc(struct match *match, int idx)
b02475c5 739{
8bfd0fda
BP
740 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TC_MASK);
741 flow_set_mpls_tc(&match->flow, idx, 0);
b02475c5
SH
742}
743
744/* Modifies 'match' so that it matches only packets with an MPLS header whose
745 * Traffic Class equals the low 3 bits of 'mpls_tc'. */
746void
8bfd0fda 747match_set_mpls_tc(struct match *match, int idx, uint8_t mpls_tc)
b02475c5 748{
8bfd0fda
BP
749 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TC_MASK);
750 flow_set_mpls_tc(&match->flow, idx, mpls_tc);
b02475c5
SH
751}
752
753/* Modifies 'match' so that the MPLS stack flag is wildcarded. */
754void
8bfd0fda 755match_set_any_mpls_bos(struct match *match, int idx)
b02475c5 756{
8bfd0fda
BP
757 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_BOS_MASK);
758 flow_set_mpls_bos(&match->flow, idx, 0);
b02475c5
SH
759}
760
761/* Modifies 'match' so that it matches only packets with an MPLS header whose
762 * Stack Flag equals the lower bit of 'mpls_bos' */
763void
8bfd0fda
BP
764match_set_mpls_bos(struct match *match, int idx, uint8_t mpls_bos)
765{
766 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_BOS_MASK);
767 flow_set_mpls_bos(&match->flow, idx, mpls_bos);
768}
769
bef3f465
BP
770/* Modifies 'match' so that the TTL of MPLS label 'idx' is wildcarded. */
771void
772match_set_any_mpls_ttl(struct match *match, int idx)
773{
774 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TTL_MASK);
775 flow_set_mpls_ttl(&match->flow, idx, 0);
776}
777
778/* Modifies 'match' so that it matches only packets in which the TTL of MPLS
779 * label 'idx' equals 'mpls_ttl'. */
780void
781match_set_mpls_ttl(struct match *match, int idx, uint8_t mpls_ttl)
782{
783 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TTL_MASK);
784 flow_set_mpls_ttl(&match->flow, idx, mpls_ttl);
785}
786
8bfd0fda
BP
787/* Modifies 'match' so that the MPLS LSE is wildcarded. */
788void
789match_set_any_mpls_lse(struct match *match, int idx)
b02475c5 790{
8bfd0fda
BP
791 match->wc.masks.mpls_lse[idx] = htonl(0);
792 flow_set_mpls_lse(&match->flow, idx, htonl(0));
b02475c5
SH
793}
794
81a76618
BP
795void
796match_set_tp_src(struct match *match, ovs_be16 tp_src)
797{
b8266395 798 match_set_tp_src_masked(match, tp_src, OVS_BE16_MAX);
81a76618
BP
799}
800
801void
802match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
803{
804 match->flow.tp_src = port & mask;
805 match->wc.masks.tp_src = mask;
806}
807
808void
809match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
810{
b8266395 811 match_set_tp_dst_masked(match, tp_dst, OVS_BE16_MAX);
81a76618
BP
812}
813
814void
815match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
816{
817 match->flow.tp_dst = port & mask;
818 match->wc.masks.tp_dst = mask;
819}
820
dc235f7f
JR
821void
822match_set_tcp_flags(struct match *match, ovs_be16 flags)
823{
824 match_set_tcp_flags_masked(match, flags, OVS_BE16_MAX);
825}
826
827void
828match_set_tcp_flags_masked(struct match *match, ovs_be16 flags, ovs_be16 mask)
829{
830 match->flow.tcp_flags = flags & mask;
831 match->wc.masks.tcp_flags = mask;
832}
833
81a76618
BP
834void
835match_set_nw_proto(struct match *match, uint8_t nw_proto)
836{
837 match->flow.nw_proto = nw_proto;
838 match->wc.masks.nw_proto = UINT8_MAX;
839}
840
841void
842match_set_nw_src(struct match *match, ovs_be32 nw_src)
843{
844 match->flow.nw_src = nw_src;
b8266395 845 match->wc.masks.nw_src = OVS_BE32_MAX;
81a76618
BP
846}
847
848void
849match_set_nw_src_masked(struct match *match,
850 ovs_be32 nw_src, ovs_be32 mask)
851{
852 match->flow.nw_src = nw_src & mask;
853 match->wc.masks.nw_src = mask;
854}
855
856void
857match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
858{
859 match->flow.nw_dst = nw_dst;
b8266395 860 match->wc.masks.nw_dst = OVS_BE32_MAX;
81a76618
BP
861}
862
863void
864match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
865{
866 match->flow.nw_dst = ip & mask;
867 match->wc.masks.nw_dst = mask;
868}
869
870void
871match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
872{
873 match->wc.masks.nw_tos |= IP_DSCP_MASK;
874 match->flow.nw_tos &= ~IP_DSCP_MASK;
875 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
876}
877
878void
879match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
880{
881 match->wc.masks.nw_tos |= IP_ECN_MASK;
882 match->flow.nw_tos &= ~IP_ECN_MASK;
883 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
884}
885
886void
887match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
888{
889 match->wc.masks.nw_ttl = UINT8_MAX;
890 match->flow.nw_ttl = nw_ttl;
891}
892
c489ad67
PB
893void
894match_set_nw_ttl_masked(struct match *match, uint8_t nw_ttl, uint8_t mask)
895{
896 match->flow.nw_ttl = nw_ttl & mask;
897 match->wc.masks.nw_ttl = mask;
898}
899
81a76618
BP
900void
901match_set_nw_frag(struct match *match, uint8_t nw_frag)
902{
903 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
904 match->flow.nw_frag = nw_frag;
905}
906
907void
908match_set_nw_frag_masked(struct match *match,
909 uint8_t nw_frag, uint8_t mask)
910{
911 match->flow.nw_frag = nw_frag & mask;
912 match->wc.masks.nw_frag = mask;
913}
914
915void
916match_set_icmp_type(struct match *match, uint8_t icmp_type)
917{
918 match_set_tp_src(match, htons(icmp_type));
919}
920
921void
922match_set_icmp_code(struct match *match, uint8_t icmp_code)
923{
924 match_set_tp_dst(match, htons(icmp_code));
925}
926
927void
74ff3298 928match_set_arp_sha(struct match *match, const struct eth_addr sha)
81a76618 929{
74ff3298
JR
930 match->flow.arp_sha = sha;
931 match->wc.masks.arp_sha = eth_addr_exact;
81a76618
BP
932}
933
934void
935match_set_arp_sha_masked(struct match *match,
74ff3298
JR
936 const struct eth_addr arp_sha,
937 const struct eth_addr mask)
81a76618
BP
938{
939 set_eth_masked(arp_sha, mask,
74ff3298 940 &match->flow.arp_sha, &match->wc.masks.arp_sha);
81a76618
BP
941}
942
943void
74ff3298 944match_set_arp_tha(struct match *match, const struct eth_addr tha)
81a76618 945{
74ff3298
JR
946 match->flow.arp_tha = tha;
947 match->wc.masks.arp_tha = eth_addr_exact;
81a76618
BP
948}
949
950void
951match_set_arp_tha_masked(struct match *match,
74ff3298
JR
952 const struct eth_addr arp_tha,
953 const struct eth_addr mask)
81a76618
BP
954{
955 set_eth_masked(arp_tha, mask,
74ff3298 956 &match->flow.arp_tha, &match->wc.masks.arp_tha);
81a76618
BP
957}
958
959void
960match_set_ipv6_src(struct match *match, const struct in6_addr *src)
961{
962 match->flow.ipv6_src = *src;
963 match->wc.masks.ipv6_src = in6addr_exact;
964}
965
966void
967match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
968 const struct in6_addr *mask)
969{
970 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
971 match->wc.masks.ipv6_src = *mask;
972}
973
974void
975match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
976{
977 match->flow.ipv6_dst = *dst;
978 match->wc.masks.ipv6_dst = in6addr_exact;
979}
980
981void
982match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
983 const struct in6_addr *mask)
984{
985 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
986 match->wc.masks.ipv6_dst = *mask;
987}
988
989void
990match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
991{
b8266395 992 match->wc.masks.ipv6_label = OVS_BE32_MAX;
81a76618
BP
993 match->flow.ipv6_label = ipv6_label;
994}
995
996
997void
998match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
999 ovs_be32 mask)
1000{
1001 match->flow.ipv6_label = ipv6_label & mask;
1002 match->wc.masks.ipv6_label = mask;
1003}
1004
1005void
1006match_set_nd_target(struct match *match, const struct in6_addr *target)
1007{
1008 match->flow.nd_target = *target;
1009 match->wc.masks.nd_target = in6addr_exact;
1010}
1011
1012void
1013match_set_nd_target_masked(struct match *match,
1014 const struct in6_addr *target,
1015 const struct in6_addr *mask)
1016{
1017 match->flow.nd_target = ipv6_addr_bitand(target, mask);
1018 match->wc.masks.nd_target = *mask;
1019}
1020
1021/* Returns true if 'a' and 'b' wildcard the same fields and have the same
1022 * values for fixed fields, otherwise false. */
1023bool
1024match_equal(const struct match *a, const struct match *b)
1025{
1026 return (flow_wildcards_equal(&a->wc, &b->wc)
1027 && flow_equal(&a->flow, &b->flow));
1028}
1029
1030/* Returns a hash value for the flow and wildcards in 'match', starting from
1031 * 'basis'. */
1032uint32_t
1033match_hash(const struct match *match, uint32_t basis)
1034{
1035 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
1036}
1037
adcf00ba
AZ
1038static bool
1039match_has_default_recirc_id(const struct match *m)
1040{
1041 return m->flow.recirc_id == 0 && (m->wc.masks.recirc_id == UINT32_MAX ||
1042 m->wc.masks.recirc_id == 0);
1043}
1044
1045static bool
1046match_has_default_dp_hash(const struct match *m)
1047{
1048 return ((m->flow.dp_hash | m->wc.masks.dp_hash) == 0);
1049}
1050
1051/* Return true if the hidden fields of the match are set to the default values.
1052 * The default values equals to those set up by match_init_hidden_fields(). */
1053bool
1054match_has_default_hidden_fields(const struct match *m)
1055{
1056 return match_has_default_recirc_id(m) && match_has_default_dp_hash(m);
1057}
1058
1059void
1060match_init_hidden_fields(struct match *m)
1061{
1062 match_set_recirc_id(m, 0);
1063 match_set_dp_hash_masked(m, 0, 0);
1064}
1065
81a76618 1066static void
3bd0fd39 1067format_eth_masked(struct ds *s, const char *name,
74ff3298 1068 const struct eth_addr eth, const struct eth_addr mask)
81a76618
BP
1069{
1070 if (!eth_addr_is_zero(mask)) {
0deec6d2 1071 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
74ff3298 1072 eth_format_masked(eth, &mask, s);
81a76618
BP
1073 ds_put_char(s, ',');
1074 }
1075}
1076
1077static void
1078format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
1079 ovs_be32 netmask)
1080{
1081 if (netmask) {
0deec6d2 1082 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
81a76618
BP
1083 ip_format_masked(ip, netmask, s);
1084 ds_put_char(s, ',');
1085 }
1086}
1087
1088static void
1089format_ipv6_netmask(struct ds *s, const char *name,
1090 const struct in6_addr *addr,
1091 const struct in6_addr *netmask)
1092{
1093 if (!ipv6_mask_is_any(netmask)) {
0deec6d2 1094 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
ac6d120f 1095 ipv6_format_masked(addr, netmask, s);
81a76618
BP
1096 ds_put_char(s, ',');
1097 }
1098}
1099
3d2fbd70
JS
1100static void
1101format_uint8_masked(struct ds *s, const char *name,
1102 uint8_t value, uint8_t mask)
1103{
1104 if (mask != 0) {
1105 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
1106 if (mask == UINT8_MAX) {
1107 ds_put_format(s, "%"PRIu8, value);
1108 } else {
1109 ds_put_format(s, "0x%02"PRIx8"/0x%02"PRIx8, value, mask);
1110 }
1111 ds_put_char(s, ',');
1112 }
1113}
1114
07659514
JS
1115static void
1116format_uint16_masked(struct ds *s, const char *name,
1117 uint16_t value, uint16_t mask)
1118{
1119 if (mask != 0) {
0deec6d2 1120 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
07659514
JS
1121 if (mask == UINT16_MAX) {
1122 ds_put_format(s, "%"PRIu16, value);
1123 } else {
1124 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16, value, mask);
1125 }
1126 ds_put_char(s, ',');
1127 }
1128}
1129
81a76618
BP
1130static void
1131format_be16_masked(struct ds *s, const char *name,
1132 ovs_be16 value, ovs_be16 mask)
1133{
1134 if (mask != htons(0)) {
0deec6d2 1135 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
b8266395 1136 if (mask == OVS_BE16_MAX) {
81a76618
BP
1137 ds_put_format(s, "%"PRIu16, ntohs(value));
1138 } else {
1139 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
1140 ntohs(value), ntohs(mask));
1141 }
1142 ds_put_char(s, ',');
1143 }
1144}
1145
8bfd0fda
BP
1146static void
1147format_be32_masked(struct ds *s, const char *name,
1148 ovs_be32 value, ovs_be32 mask)
1149{
1150 if (mask != htonl(0)) {
0deec6d2 1151 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
8bfd0fda
BP
1152 if (mask == OVS_BE32_MAX) {
1153 ds_put_format(s, "%"PRIu32, ntohl(value));
3d2fbd70
JS
1154 } else {
1155 ds_put_format(s, "0x%08"PRIx32"/0x%08"PRIx32,
1156 ntohl(value), ntohl(mask));
1157 }
1158 ds_put_char(s, ',');
1159 }
1160}
1161
1162static void
1163format_be32_masked_hex(struct ds *s, const char *name,
1164 ovs_be32 value, ovs_be32 mask)
1165{
1166 if (mask != htonl(0)) {
1167 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
1168 if (mask == OVS_BE32_MAX) {
1169 ds_put_format(s, "0x%"PRIx32, ntohl(value));
8bfd0fda
BP
1170 } else {
1171 ds_put_format(s, "0x%"PRIx32"/0x%"PRIx32,
1172 ntohl(value), ntohl(mask));
1173 }
1174 ds_put_char(s, ',');
1175 }
1176}
1177
b60f804e
AZ
1178static void
1179format_uint32_masked(struct ds *s, const char *name,
1180 uint32_t value, uint32_t mask)
1181{
1182 if (mask) {
0deec6d2
QM
1183 ds_put_format(s, "%s%s=%s%#"PRIx32,
1184 colors.param, name, colors.end, value);
b60f804e
AZ
1185 if (mask != UINT32_MAX) {
1186 ds_put_format(s, "/%#"PRIx32, mask);
1187 }
1188 ds_put_char(s, ',');
1189 }
1190}
1191
1192static void
1193format_be64_masked(struct ds *s, const char *name,
1194 ovs_be64 value, ovs_be64 mask)
1195{
1196 if (mask != htonll(0)) {
0deec6d2
QM
1197 ds_put_format(s, "%s%s=%s%#"PRIx64,
1198 colors.param, name, colors.end, ntohll(value));
b60f804e
AZ
1199 if (mask != OVS_BE64_MAX) {
1200 ds_put_format(s, "/%#"PRIx64, ntohll(mask));
1201 }
1202 ds_put_char(s, ',');
1203 }
1204}
1205
4fe3445a
PS
1206static void
1207format_flow_tunnel(struct ds *s, const struct match *match)
1208{
1209 const struct flow_wildcards *wc = &match->wc;
1210 const struct flow_tnl *tnl = &match->flow.tunnel;
1211
b60f804e 1212 format_be64_masked(s, "tun_id", tnl->tun_id, wc->masks.tunnel.tun_id);
4fe3445a
PS
1213 format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
1214 format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
ffe4c74f
JB
1215 format_ipv6_netmask(s, "tun_ipv6_src", &tnl->ipv6_src,
1216 &wc->masks.tunnel.ipv6_src);
1217 format_ipv6_netmask(s, "tun_ipv6_dst", &tnl->ipv6_dst,
1218 &wc->masks.tunnel.ipv6_dst);
4fe3445a 1219
ac6073e3
MC
1220 if (wc->masks.tunnel.gbp_id) {
1221 format_be16_masked(s, "tun_gbp_id", tnl->gbp_id,
1222 wc->masks.tunnel.gbp_id);
1223 }
1224
1225 if (wc->masks.tunnel.gbp_flags) {
1226 ds_put_format(s, "tun_gbp_flags=%#"PRIx8",", tnl->gbp_flags);
1227 }
1228
4fe3445a
PS
1229 if (wc->masks.tunnel.ip_tos) {
1230 ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
1231 }
1232 if (wc->masks.tunnel.ip_ttl) {
1233 ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
1234 }
3721b406 1235 if (wc->masks.tunnel.flags & FLOW_TNL_F_MASK) {
b666962b 1236 format_flags_masked(s, "tun_flags", flow_tun_flag_to_string,
3721b406 1237 tnl->flags & FLOW_TNL_F_MASK,
b666962b
JG
1238 wc->masks.tunnel.flags & FLOW_TNL_F_MASK,
1239 FLOW_TNL_F_MASK);
4fe3445a
PS
1240 ds_put_char(s, ',');
1241 }
9558d2a5 1242 tun_metadata_match_format(s, match);
4fe3445a
PS
1243}
1244
9daf2348
JS
1245static void
1246format_ct_label_masked(struct ds *s, const ovs_u128 *key, const ovs_u128 *mask)
1247{
2ff8484b 1248 if (!ovs_u128_is_zero(*mask)) {
32ea15f6 1249 ovs_be128 value = hton128(*key);
0deec6d2 1250 ds_put_format(s, "%sct_label=%s", colors.param, colors.end);
9daf2348
JS
1251 ds_put_hex(s, &value, sizeof value);
1252 if (!is_all_ones(mask, sizeof(*mask))) {
32ea15f6 1253 value = hton128(*mask);
9daf2348
JS
1254 ds_put_char(s, '/');
1255 ds_put_hex(s, &value, sizeof value);
1256 }
1257 ds_put_char(s, ',');
1258 }
1259}
1260
3d2fbd70
JS
1261static void
1262format_nsh_masked(struct ds *s, const struct flow *f, const struct flow *m)
1263{
17553f27
YY
1264 ovs_be32 spi_mask = nsh_path_hdr_to_spi(m->nsh.path_hdr);
1265 if (spi_mask == htonl(NSH_SPI_MASK >> NSH_SPI_SHIFT)) {
1266 spi_mask = OVS_BE32_MAX;
1267 }
3d2fbd70 1268 format_uint8_masked(s, "nsh_flags", f->nsh.flags, m->nsh.flags);
17553f27 1269 format_uint8_masked(s, "nsh_ttl", f->nsh.ttl, m->nsh.ttl);
3d2fbd70
JS
1270 format_uint8_masked(s, "nsh_mdtype", f->nsh.mdtype, m->nsh.mdtype);
1271 format_uint8_masked(s, "nsh_np", f->nsh.np, m->nsh.np);
17553f27
YY
1272
1273 format_be32_masked_hex(s, "nsh_spi", nsh_path_hdr_to_spi(f->nsh.path_hdr),
1274 spi_mask);
1275 format_uint8_masked(s, "nsh_si", nsh_path_hdr_to_si(f->nsh.path_hdr),
1276 nsh_path_hdr_to_si(m->nsh.path_hdr));
3d2fbd70 1277 if (m->nsh.mdtype == UINT8_MAX && f->nsh.mdtype == NSH_M_TYPE1) {
f59cb331
YY
1278 format_be32_masked_hex(s, "nsh_c1", f->nsh.context[0],
1279 m->nsh.context[0]);
1280 format_be32_masked_hex(s, "nsh_c2", f->nsh.context[1],
1281 m->nsh.context[1]);
1282 format_be32_masked_hex(s, "nsh_c3", f->nsh.context[2],
1283 m->nsh.context[2]);
1284 format_be32_masked_hex(s, "nsh_c4", f->nsh.context[3],
1285 m->nsh.context[3]);
3d2fbd70
JS
1286 }
1287}
1288
81a76618 1289/* Appends a string representation of 'match' to 's'. If 'priority' is
50f96b10
BP
1290 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. If 'port_map' is
1291 * nonnull, uses it to translate port numbers to names in output. */
81a76618 1292void
50f96b10
BP
1293match_format(const struct match *match,
1294 const struct ofputil_port_map *port_map,
1295 struct ds *s, int priority)
81a76618
BP
1296{
1297 const struct flow_wildcards *wc = &match->wc;
1298 size_t start_len = s->length;
1299 const struct flow *f = &match->flow;
1300 bool skip_type = false;
1301 bool skip_proto = false;
3d4b2e6e 1302 ovs_be16 dl_type = f->dl_type;
e3981271 1303 bool is_megaflow = false;
81a76618
BP
1304 int i;
1305
3d2fbd70 1306 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 40);
81a76618
BP
1307
1308 if (priority != OFP_DEFAULT_PRIORITY) {
0deec6d2
QM
1309 ds_put_format(s, "%spriority=%s%d,",
1310 colors.special, colors.end, priority);
81a76618
BP
1311 }
1312
b60f804e 1313 format_uint32_masked(s, "pkt_mark", f->pkt_mark, wc->masks.pkt_mark);
1b567fb9 1314
a79f29f2
AZ
1315 if (wc->masks.recirc_id) {
1316 format_uint32_masked(s, "recirc_id", f->recirc_id,
1317 wc->masks.recirc_id);
e3981271 1318 is_megaflow = true;
a79f29f2
AZ
1319 }
1320
330de069 1321 if (wc->masks.dp_hash) {
a79f29f2
AZ
1322 format_uint32_masked(s, "dp_hash", f->dp_hash,
1323 wc->masks.dp_hash);
1324 }
1325
18080541 1326 if (wc->masks.conj_id) {
0deec6d2
QM
1327 ds_put_format(s, "%sconj_id%s=%"PRIu32",",
1328 colors.param, colors.end, f->conj_id);
18080541
BP
1329 }
1330
1b567fb9 1331 if (wc->masks.skb_priority) {
0deec6d2
QM
1332 ds_put_format(s, "%sskb_priority=%s%#"PRIx32",",
1333 colors.param, colors.end, f->skb_priority);
1b567fb9
AA
1334 }
1335
c61f3870 1336 if (wc->masks.actset_output) {
0deec6d2 1337 ds_put_format(s, "%sactset_output=%s", colors.param, colors.end);
50f96b10 1338 ofputil_format_port(f->actset_output, port_map, s);
c61f3870
BP
1339 ds_put_char(s, ',');
1340 }
1341
07659514 1342 if (wc->masks.ct_state) {
2a28ccc8 1343 if (wc->masks.ct_state == UINT8_MAX) {
0deec6d2 1344 ds_put_format(s, "%sct_state=%s", colors.param, colors.end);
07659514
JS
1345 if (f->ct_state) {
1346 format_flags(s, ct_state_to_string, f->ct_state, '|');
1347 } else {
1348 ds_put_cstr(s, "0"); /* No state. */
1349 }
1350 } else {
1351 format_flags_masked(s, "ct_state", ct_state_to_string,
2a28ccc8 1352 f->ct_state, wc->masks.ct_state, UINT8_MAX);
07659514
JS
1353 }
1354 ds_put_char(s, ',');
1355 }
1356
1357 if (wc->masks.ct_zone) {
1358 format_uint16_masked(s, "ct_zone", f->ct_zone, wc->masks.ct_zone);
1359 }
1360
8e53fe8c
JS
1361 if (wc->masks.ct_mark) {
1362 format_uint32_masked(s, "ct_mark", f->ct_mark, wc->masks.ct_mark);
1363 }
1364
2ff8484b 1365 if (!ovs_u128_is_zero(wc->masks.ct_label)) {
9daf2348
JS
1366 format_ct_label_masked(s, &f->ct_label, &wc->masks.ct_label);
1367 }
1368
daf4d3c1
JR
1369 format_ip_netmask(s, "ct_nw_src", f->ct_nw_src,
1370 wc->masks.ct_nw_src);
1371 format_ipv6_netmask(s, "ct_ipv6_src", &f->ct_ipv6_src,
1372 &wc->masks.ct_ipv6_src);
1373 format_ip_netmask(s, "ct_nw_dst", f->ct_nw_dst,
1374 wc->masks.ct_nw_dst);
1375 format_ipv6_netmask(s, "ct_ipv6_dst", &f->ct_ipv6_dst,
1376 &wc->masks.ct_ipv6_dst);
1377 if (wc->masks.ct_nw_proto) {
1378 ds_put_format(s, "%sct_nw_proto=%s%"PRIu8",",
1379 colors.param, colors.end, f->ct_nw_proto);
1380 format_be16_masked(s, "ct_tp_src", f->ct_tp_src, wc->masks.ct_tp_src);
1381 format_be16_masked(s, "ct_tp_dst", f->ct_tp_dst, wc->masks.ct_tp_dst);
1382 }
1383
e3981271
JS
1384 if (wc->masks.packet_type &&
1385 (!match_has_default_packet_type(match) || is_megaflow)) {
3d4b2e6e
JS
1386 format_packet_type_masked(s, f->packet_type, wc->masks.packet_type);
1387 ds_put_char(s, ',');
1388 if (pt_ns(f->packet_type) == OFPHTN_ETHERTYPE) {
1389 dl_type = pt_ns_type_be(f->packet_type);
beb75a40
JS
1390 }
1391 }
1392
81a76618
BP
1393 if (wc->masks.dl_type) {
1394 skip_type = true;
3d4b2e6e 1395 if (dl_type == htons(ETH_TYPE_IP)) {
81a76618
BP
1396 if (wc->masks.nw_proto) {
1397 skip_proto = true;
1398 if (f->nw_proto == IPPROTO_ICMP) {
0deec6d2 1399 ds_put_format(s, "%sicmp%s,", colors.value, colors.end);
0e612675 1400 } else if (f->nw_proto == IPPROTO_IGMP) {
0deec6d2 1401 ds_put_format(s, "%sigmp%s,", colors.value, colors.end);
81a76618 1402 } else if (f->nw_proto == IPPROTO_TCP) {
0deec6d2 1403 ds_put_format(s, "%stcp%s,", colors.value, colors.end);
81a76618 1404 } else if (f->nw_proto == IPPROTO_UDP) {
0deec6d2 1405 ds_put_format(s, "%sudp%s,", colors.value, colors.end);
0d56eaf2 1406 } else if (f->nw_proto == IPPROTO_SCTP) {
0deec6d2 1407 ds_put_format(s, "%ssctp%s,", colors.value, colors.end);
81a76618 1408 } else {
0deec6d2 1409 ds_put_format(s, "%sip%s,", colors.value, colors.end);
81a76618
BP
1410 skip_proto = false;
1411 }
1412 } else {
0deec6d2 1413 ds_put_format(s, "%sip%s,", colors.value, colors.end);
81a76618 1414 }
3d4b2e6e 1415 } else if (dl_type == htons(ETH_TYPE_IPV6)) {
81a76618
BP
1416 if (wc->masks.nw_proto) {
1417 skip_proto = true;
1418 if (f->nw_proto == IPPROTO_ICMPV6) {
0deec6d2 1419 ds_put_format(s, "%sicmp6%s,", colors.value, colors.end);
81a76618 1420 } else if (f->nw_proto == IPPROTO_TCP) {
0deec6d2 1421 ds_put_format(s, "%stcp6%s,", colors.value, colors.end);
81a76618 1422 } else if (f->nw_proto == IPPROTO_UDP) {
0deec6d2 1423 ds_put_format(s, "%sudp6%s,", colors.value, colors.end);
0d56eaf2 1424 } else if (f->nw_proto == IPPROTO_SCTP) {
0deec6d2 1425 ds_put_format(s, "%ssctp6%s,", colors.value, colors.end);
81a76618 1426 } else {
0deec6d2 1427 ds_put_format(s, "%sipv6%s,", colors.value, colors.end);
81a76618
BP
1428 skip_proto = false;
1429 }
1430 } else {
0deec6d2 1431 ds_put_format(s, "%sipv6%s,", colors.value, colors.end);
81a76618 1432 }
3d4b2e6e 1433 } else if (dl_type == htons(ETH_TYPE_ARP)) {
0deec6d2 1434 ds_put_format(s, "%sarp%s,", colors.value, colors.end);
3d4b2e6e 1435 } else if (dl_type == htons(ETH_TYPE_RARP)) {
0deec6d2 1436 ds_put_format(s, "%srarp%s,", colors.value, colors.end);
3d4b2e6e 1437 } else if (dl_type == htons(ETH_TYPE_MPLS)) {
0deec6d2 1438 ds_put_format(s, "%smpls%s,", colors.value, colors.end);
3d4b2e6e 1439 } else if (dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
0deec6d2 1440 ds_put_format(s, "%smplsm%s,", colors.value, colors.end);
81a76618
BP
1441 } else {
1442 skip_type = false;
1443 }
1444 }
1445 for (i = 0; i < FLOW_N_REGS; i++) {
b60f804e
AZ
1446 #define REGNAME_LEN 20
1447 char regname[REGNAME_LEN];
1448 if (snprintf(regname, REGNAME_LEN, "reg%d", i) >= REGNAME_LEN) {
1449 strcpy(regname, "reg?");
81a76618 1450 }
b60f804e 1451 format_uint32_masked(s, regname, f->regs[i], wc->masks.regs[i]);
81a76618 1452 }
4fe3445a
PS
1453
1454 format_flow_tunnel(s, match);
1455
b60f804e
AZ
1456 format_be64_masked(s, "metadata", f->metadata, wc->masks.metadata);
1457
4e022ec0 1458 if (wc->masks.in_port.ofp_port) {
0deec6d2 1459 ds_put_format(s, "%sin_port=%s", colors.param, colors.end);
50f96b10 1460 ofputil_format_port(f->in_port.ofp_port, port_map, s);
576ec803 1461 ds_put_char(s, ',');
81a76618 1462 }
f0fb825a
EG
1463 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
1464 char str_i[8];
81a76618 1465
f0fb825a
EG
1466 if (!wc->masks.vlans[i].tci) {
1467 break;
1468 }
1469
1470 /* Print VLAN tags as dl_vlan, dl_vlan1, dl_vlan2 ... */
1471 if (i == 0) {
1472 str_i[0] = '\0';
1473 } else {
1474 snprintf(str_i, sizeof(str_i), "%d", i);
1475 }
1476 ovs_be16 vid_mask = wc->masks.vlans[i].tci & htons(VLAN_VID_MASK);
1477 ovs_be16 pcp_mask = wc->masks.vlans[i].tci & htons(VLAN_PCP_MASK);
1478 ovs_be16 cfi = wc->masks.vlans[i].tci & htons(VLAN_CFI);
1479
1480 if (cfi && f->vlans[i].tci & htons(VLAN_CFI)
81a76618
BP
1481 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
1482 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
1483 && (vid_mask || pcp_mask)) {
1484 if (vid_mask) {
f0fb825a
EG
1485 ds_put_format(s, "%sdl_vlan%s=%s%"PRIu16",",
1486 colors.param, str_i, colors.end,
1487 vlan_tci_to_vid(f->vlans[i].tci));
81a76618
BP
1488 }
1489 if (pcp_mask) {
f0fb825a
EG
1490 ds_put_format(s, "%sdl_vlan_pcp%s=%s%d,",
1491 colors.param, str_i, colors.end,
1492 vlan_tci_to_pcp(f->vlans[i].tci));
81a76618 1493 }
f0fb825a
EG
1494 } else if (wc->masks.vlans[i].tci == htons(0xffff)) {
1495 ds_put_format(s, "%svlan_tci%s=%s0x%04"PRIx16",",
1496 colors.param, str_i, colors.end,
1497 ntohs(f->vlans[i].tci));
81a76618 1498 } else {
f0fb825a
EG
1499 ds_put_format(s, "%svlan_tci%s=%s0x%04"PRIx16"/0x%04"PRIx16",",
1500 colors.param, str_i, colors.end,
1501 ntohs(f->vlans[i].tci),
1502 ntohs(wc->masks.vlans[i].tci));
81a76618
BP
1503 }
1504 }
beb75a40 1505
81a76618
BP
1506 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
1507 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
beb75a40 1508
81a76618 1509 if (!skip_type && wc->masks.dl_type) {
0deec6d2 1510 ds_put_format(s, "%sdl_type=%s0x%04"PRIx16",",
3d4b2e6e 1511 colors.param, colors.end, ntohs(dl_type));
81a76618 1512 }
3d4b2e6e 1513 if (dl_type == htons(ETH_TYPE_IPV6)) {
81a76618
BP
1514 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
1515 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
1516 if (wc->masks.ipv6_label) {
b8266395 1517 if (wc->masks.ipv6_label == OVS_BE32_MAX) {
0deec6d2
QM
1518 ds_put_format(s, "%sipv6_label=%s0x%05"PRIx32",",
1519 colors.param, colors.end,
81a76618
BP
1520 ntohl(f->ipv6_label));
1521 } else {
0deec6d2
QM
1522 ds_put_format(s, "%sipv6_label=%s0x%05"PRIx32"/0x%05"PRIx32",",
1523 colors.param, colors.end, ntohl(f->ipv6_label),
81a76618
BP
1524 ntohl(wc->masks.ipv6_label));
1525 }
1526 }
3d4b2e6e
JS
1527 } else if (dl_type == htons(ETH_TYPE_ARP) ||
1528 dl_type == htons(ETH_TYPE_RARP)) {
666d0863
MM
1529 format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
1530 format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
3d2fbd70
JS
1531 } else if (dl_type == htons(ETH_TYPE_NSH)) {
1532 format_nsh_masked(s, f, &wc->masks);
81a76618
BP
1533 } else {
1534 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
1535 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
1536 }
1537 if (!skip_proto && wc->masks.nw_proto) {
3d4b2e6e
JS
1538 if (dl_type == htons(ETH_TYPE_ARP) ||
1539 dl_type == htons(ETH_TYPE_RARP)) {
0deec6d2
QM
1540 ds_put_format(s, "%sarp_op=%s%"PRIu8",",
1541 colors.param, colors.end, f->nw_proto);
81a76618 1542 } else {
0deec6d2
QM
1543 ds_put_format(s, "%snw_proto=%s%"PRIu8",",
1544 colors.param, colors.end, f->nw_proto);
81a76618
BP
1545 }
1546 }
3d4b2e6e
JS
1547 if (dl_type == htons(ETH_TYPE_ARP) ||
1548 dl_type == htons(ETH_TYPE_RARP)) {
81a76618
BP
1549 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
1550 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
1551 }
1552 if (wc->masks.nw_tos & IP_DSCP_MASK) {
fd13c6b5 1553 ds_put_format(s, "%snw_tos=%s%d,",
0deec6d2 1554 colors.param, colors.end, f->nw_tos & IP_DSCP_MASK);
81a76618
BP
1555 }
1556 if (wc->masks.nw_tos & IP_ECN_MASK) {
fd13c6b5 1557 ds_put_format(s, "%snw_ecn=%s%d,",
0deec6d2 1558 colors.param, colors.end, f->nw_tos & IP_ECN_MASK);
81a76618
BP
1559 }
1560 if (wc->masks.nw_ttl) {
fd13c6b5 1561 ds_put_format(s, "%snw_ttl=%s%d,",
0deec6d2 1562 colors.param, colors.end, f->nw_ttl);
81a76618 1563 }
8bfd0fda 1564 if (wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
0deec6d2
QM
1565 ds_put_format(s, "%smpls_label=%s%"PRIu32",", colors.param,
1566 colors.end, mpls_lse_to_label(f->mpls_lse[0]));
b02475c5 1567 }
8bfd0fda 1568 if (wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
0deec6d2 1569 ds_put_format(s, "%smpls_tc=%s%"PRIu8",", colors.param, colors.end,
78c9486d 1570 mpls_lse_to_tc(f->mpls_lse[0]));
b02475c5 1571 }
8bfd0fda 1572 if (wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK)) {
0deec6d2 1573 ds_put_format(s, "%smpls_ttl=%s%"PRIu8",", colors.param, colors.end,
78c9486d 1574 mpls_lse_to_ttl(f->mpls_lse[0]));
392c30ba 1575 }
8bfd0fda 1576 if (wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
0deec6d2 1577 ds_put_format(s, "%smpls_bos=%s%"PRIu8",", colors.param, colors.end,
78c9486d 1578 mpls_lse_to_bos(f->mpls_lse[0]));
b02475c5 1579 }
8bfd0fda
BP
1580 format_be32_masked(s, "mpls_lse1", f->mpls_lse[1], wc->masks.mpls_lse[1]);
1581 format_be32_masked(s, "mpls_lse2", f->mpls_lse[2], wc->masks.mpls_lse[2]);
1582
81a76618
BP
1583 switch (wc->masks.nw_frag) {
1584 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
0deec6d2 1585 ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
81a76618
BP
1586 f->nw_frag & FLOW_NW_FRAG_ANY
1587 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
1588 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
1589 break;
1590
1591 case FLOW_NW_FRAG_ANY:
0deec6d2 1592 ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
81a76618
BP
1593 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
1594 break;
1595
1596 case FLOW_NW_FRAG_LATER:
0deec6d2 1597 ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
81a76618
BP
1598 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
1599 break;
1600 }
3d4b2e6e 1601 if (dl_type == htons(ETH_TYPE_IP) &&
19564d20 1602 f->nw_proto == IPPROTO_ICMP) {
81a76618
BP
1603 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1604 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
3d4b2e6e 1605 } else if (dl_type == htons(ETH_TYPE_IP) &&
0e612675
FL
1606 f->nw_proto == IPPROTO_IGMP) {
1607 format_be16_masked(s, "igmp_type", f->tp_src, wc->masks.tp_src);
1608 format_be16_masked(s, "igmp_code", f->tp_dst, wc->masks.tp_dst);
3d4b2e6e 1609 } else if (dl_type == htons(ETH_TYPE_IPV6) &&
19564d20 1610 f->nw_proto == IPPROTO_ICMPV6) {
81a76618
BP
1611 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1612 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1613 format_ipv6_netmask(s, "nd_target", &f->nd_target,
1614 &wc->masks.nd_target);
1615 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
1616 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
1f6d8197 1617 } else {
81a76618
BP
1618 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
1619 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
1620 }
dc235f7f 1621 if (is_ip_any(f) && f->nw_proto == IPPROTO_TCP && wc->masks.tcp_flags) {
8e4c1621
JG
1622 format_flags_masked(s, "tcp_flags", packet_tcp_flag_to_string,
1623 ntohs(f->tcp_flags), TCP_FLAGS(wc->masks.tcp_flags),
1624 TCP_FLAGS(OVS_BE16_MAX));
dc235f7f 1625 }
81a76618 1626
41f562be
SH
1627 if (s->length > start_len) {
1628 ds_chomp(s, ',');
81a76618
BP
1629 }
1630}
1631
1632/* Converts 'match' to a string and returns the string. If 'priority' is
50f96b10
BP
1633 * different from OFP_DEFAULT_PRIORITY, includes it in the string. If
1634 * 'port_map' is nonnull, uses it to translate port numbers to names in
1635 * output. The caller must free the string (with free()). */
81a76618 1636char *
50f96b10
BP
1637match_to_string(const struct match *match,
1638 const struct ofputil_port_map *port_map, int priority)
81a76618
BP
1639{
1640 struct ds s = DS_EMPTY_INITIALIZER;
50f96b10 1641 match_format(match, port_map, &s, priority);
81a76618
BP
1642 return ds_steal_cstr(&s);
1643}
1644
1645void
50f96b10
BP
1646match_print(const struct match *match,
1647 const struct ofputil_port_map *port_map)
81a76618 1648{
50f96b10 1649 char *s = match_to_string(match, port_map, OFP_DEFAULT_PRIORITY);
81a76618
BP
1650 puts(s);
1651 free(s);
1652}
5cb7a798
BP
1653\f
1654/* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1655 * with minimatch_destroy(). */
1656void
1657minimatch_init(struct minimatch *dst, const struct match *src)
1658{
ceb3bd67
JR
1659 struct miniflow tmp;
1660
1661 miniflow_map_init(&tmp, &src->wc.masks);
1662 /* Allocate two consecutive miniflows. */
1663 miniflow_alloc(dst->flows, 2, &tmp);
1664 miniflow_init(dst->flow, &src->flow);
1665 minimask_init(dst->mask, &src->wc);
1f7270d7
BP
1666
1667 dst->tun_md = tun_metadata_allocation_clone(&src->tun_md);
5cb7a798
BP
1668}
1669
1dc1ec24
BP
1670/* Initializes 'match' as a "catch-all" match that matches every packet. */
1671void
1672minimatch_init_catchall(struct minimatch *match)
1673{
1674 match->flows[0] = xcalloc(2, sizeof *match->flow);
1675 match->flows[1] = match->flows[0] + 1;
1676 match->tun_md = NULL;
1677}
1678
5cb7a798
BP
1679/* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1680 * with minimatch_destroy(). */
1681void
1682minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1683{
ceb3bd67
JR
1684 /* Allocate two consecutive miniflows. */
1685 size_t data_size = miniflow_alloc(dst->flows, 2, &src->mask->masks);
1686
09b0fa9c
JR
1687 memcpy(miniflow_values(dst->flow),
1688 miniflow_get_values(src->flow), data_size);
1689 memcpy(miniflow_values(&dst->mask->masks),
1690 miniflow_get_values(&src->mask->masks), data_size);
1f7270d7 1691 dst->tun_md = tun_metadata_allocation_clone(src->tun_md);
5cb7a798
BP
1692}
1693
b2c1f00b
BP
1694/* Initializes 'dst' with the data in 'src', destroying 'src'. The caller must
1695 * eventually free 'dst' with minimatch_destroy(). */
1696void
1697minimatch_move(struct minimatch *dst, struct minimatch *src)
1698{
8fd47924
JR
1699 dst->flow = src->flow;
1700 dst->mask = src->mask;
1f7270d7 1701 dst->tun_md = src->tun_md;
b2c1f00b
BP
1702}
1703
5cb7a798
BP
1704/* Frees any memory owned by 'match'. Does not free the storage in which
1705 * 'match' itself resides; the caller is responsible for that. */
1706void
1707minimatch_destroy(struct minimatch *match)
1708{
8fd47924 1709 free(match->flow);
1f7270d7 1710 free(match->tun_md);
5cb7a798
BP
1711}
1712
1713/* Initializes 'dst' as a copy of 'src'. */
1714void
1715minimatch_expand(const struct minimatch *src, struct match *dst)
1716{
8fd47924
JR
1717 miniflow_expand(src->flow, &dst->flow);
1718 minimask_expand(src->mask, &dst->wc);
1f7270d7 1719 tun_metadata_allocation_copy(&dst->tun_md, src->tun_md);
5cb7a798
BP
1720}
1721
1722/* Returns true if 'a' and 'b' match the same packets, false otherwise. */
1723bool
1724minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1725{
8fd47924
JR
1726 return minimask_equal(a->mask, b->mask)
1727 && miniflow_equal(a->flow, b->flow);
5cb7a798
BP
1728}
1729
1dc1ec24
BP
1730/* Returns a hash value for the flow and wildcards in 'match', starting from
1731 * 'basis'. */
1732uint32_t
1733minimatch_hash(const struct minimatch *match, uint32_t basis)
1734{
1735 size_t n_values = miniflow_n_values(match->flow);
1736 size_t flow_size = sizeof *match->flow + MINIFLOW_VALUES_SIZE(n_values);
1737 return hash_bytes(match->flow, 2 * flow_size, basis);
1738}
1739
df40c152
BP
1740/* Returns true if 'target' satisifies 'match', that is, if each bit for which
1741 * 'match' specifies a particular value has the correct value in 'target'.
1742 *
1743 * This function is equivalent to miniflow_equal_flow_in_minimask(&match->flow,
1744 * target, &match->mask) but it is faster because of the invariant that
1745 * match->flow.map and match->mask.map are the same. */
1746bool
1747minimatch_matches_flow(const struct minimatch *match,
1748 const struct flow *target)
1749{
09b0fa9c
JR
1750 const uint64_t *flowp = miniflow_get_values(match->flow);
1751 const uint64_t *maskp = miniflow_get_values(&match->mask->masks);
361d808d 1752 size_t idx;
df40c152 1753
5fcff47b
JR
1754 FLOWMAP_FOR_EACH_INDEX(idx, match->flow->map) {
1755 if ((*flowp++ ^ flow_u64_value(target, idx)) & *maskp++) {
080e28d0 1756 return false;
df40c152 1757 }
df40c152
BP
1758 }
1759
1760 return true;
1761}
1762
5cb7a798 1763/* Appends a string representation of 'match' to 's'. If 'priority' is
50f96b10
BP
1764 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. If 'port_map' is
1765 * nonnull, uses it to translate port numbers to names in output. */
5cb7a798 1766void
8d8ab6c2 1767minimatch_format(const struct minimatch *match,
50f96b10
BP
1768 const struct tun_table *tun_table,
1769 const struct ofputil_port_map *port_map,
1770 struct ds *s, int priority)
5cb7a798
BP
1771{
1772 struct match megamatch;
1773
1774 minimatch_expand(match, &megamatch);
8d8ab6c2
JG
1775 megamatch.flow.tunnel.metadata.tab = tun_table;
1776
50f96b10 1777 match_format(&megamatch, port_map, s, priority);
5cb7a798
BP
1778}
1779
1780/* Converts 'match' to a string and returns the string. If 'priority' is
1781 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
50f96b10
BP
1782 * must free the string (with free()). If 'port_map' is nonnull, uses it to
1783 * translate port numbers to names in output. */
5cb7a798 1784char *
50f96b10
BP
1785minimatch_to_string(const struct minimatch *match,
1786 const struct ofputil_port_map *port_map, int priority)
5cb7a798
BP
1787{
1788 struct match megamatch;
1789
1790 minimatch_expand(match, &megamatch);
50f96b10 1791 return match_to_string(&megamatch, port_map, priority);
5cb7a798 1792}
1dc1ec24
BP
1793
1794static bool
1795minimatch_has_default_recirc_id(const struct minimatch *m)
1796{
1797 uint32_t flow_recirc_id = miniflow_get_recirc_id(m->flow);
1798 uint32_t mask_recirc_id = miniflow_get_recirc_id(&m->mask->masks);
1799 return flow_recirc_id == 0 && (mask_recirc_id == UINT32_MAX ||
1800 mask_recirc_id == 0);
1801}
1802
1803static bool
1804minimatch_has_default_dp_hash(const struct minimatch *m)
1805{
1806 return (!miniflow_get_dp_hash(m->flow)
1807 && !miniflow_get_dp_hash(&m->mask->masks));
1808}
1809
1810/* Return true if the hidden fields of the match are set to the default values.
1811 * The default values equals to those set up by match_init_hidden_fields(). */
1812bool
1813minimatch_has_default_hidden_fields(const struct minimatch *m)
1814{
1815 return (minimatch_has_default_recirc_id(m)
1816 && minimatch_has_default_dp_hash(m));
1817}