]> git.proxmox.com Git - mirror_ovs.git/blame - lib/match.c
netdev-offload-tc: Use single 'once' variable for probing tc features
[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
7dc18ae9
WT
322void
323match_set_tun_erspan_ver_masked(struct match *match, uint8_t ver, uint8_t mask)
324{
325 match->wc.masks.tunnel.erspan_ver = ver;
326 match->flow.tunnel.erspan_ver = ver & mask;
327}
328
329void
330match_set_tun_erspan_ver(struct match *match, uint8_t ver)
331{
332 match_set_tun_erspan_ver_masked(match, ver, UINT8_MAX);
333}
334
335void
336match_set_tun_erspan_idx_masked(struct match *match, uint32_t erspan_idx,
337 uint32_t mask)
338{
339 match->wc.masks.tunnel.erspan_idx = mask;
340 match->flow.tunnel.erspan_idx = erspan_idx & mask;
341}
342
343void
344match_set_tun_erspan_idx(struct match *match, uint32_t erspan_idx)
345{
346 match_set_tun_erspan_idx_masked(match, erspan_idx, UINT32_MAX);
347}
348
349void
350match_set_tun_erspan_dir_masked(struct match *match, uint8_t dir,
351 uint8_t mask)
352{
353 match->wc.masks.tunnel.erspan_dir = dir;
354 match->flow.tunnel.erspan_dir = dir & mask;
355}
356
357void
358match_set_tun_erspan_dir(struct match *match, uint8_t dir)
359{
360 match_set_tun_erspan_dir_masked(match, dir, UINT8_MAX);
361}
362
363void
364match_set_tun_erspan_hwid_masked(struct match *match, uint8_t hwid,
365 uint8_t mask)
366{
367 match->wc.masks.tunnel.erspan_hwid = hwid;
368 match->flow.tunnel.erspan_hwid = hwid & mask;
369}
370
371void
372match_set_tun_erspan_hwid(struct match *match, uint8_t hwid)
373{
374 match_set_tun_erspan_hwid_masked(match, hwid, UINT8_MAX);
375}
376
3c6d05a0
WT
377void
378match_set_tun_gtpu_flags_masked(struct match *match, uint8_t flags,
379 uint8_t mask)
380{
381 match->wc.masks.tunnel.gtpu_flags = flags;
382 match->flow.tunnel.gtpu_flags = flags & mask;
383}
384
385void
386match_set_tun_gtpu_flags(struct match *match, uint8_t flags)
387{
388 match_set_tun_gtpu_flags_masked(match, flags, UINT8_MAX);
389}
390
391void
392match_set_tun_gtpu_msgtype_masked(struct match *match, uint8_t msgtype,
393 uint8_t mask)
394{
395 match->wc.masks.tunnel.gtpu_msgtype = msgtype;
396 match->flow.tunnel.gtpu_msgtype = msgtype & mask;
397}
398
399void
400match_set_tun_gtpu_msgtype(struct match *match, uint8_t msgtype)
401{
402 match_set_tun_gtpu_msgtype_masked(match, msgtype, UINT8_MAX);
403}
404
81a76618 405void
4e022ec0 406match_set_in_port(struct match *match, ofp_port_t ofp_port)
81a76618 407{
4e022ec0
AW
408 match->wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
409 match->flow.in_port.ofp_port = ofp_port;
81a76618
BP
410}
411
1b567fb9
AA
412void
413match_set_skb_priority(struct match *match, uint32_t skb_priority)
414{
415 match->wc.masks.skb_priority = UINT32_MAX;
416 match->flow.skb_priority = skb_priority;
417}
418
419void
1362e248 420match_set_pkt_mark(struct match *match, uint32_t pkt_mark)
1b567fb9 421{
ac923e91
JG
422 match_set_pkt_mark_masked(match, pkt_mark, UINT32_MAX);
423}
424
425void
426match_set_pkt_mark_masked(struct match *match, uint32_t pkt_mark, uint32_t mask)
427{
428 match->flow.pkt_mark = pkt_mark & mask;
429 match->wc.masks.pkt_mark = mask;
1b567fb9
AA
430}
431
07659514
JS
432void
433match_set_ct_state(struct match *match, uint32_t ct_state)
434{
435 match_set_ct_state_masked(match, ct_state, UINT32_MAX);
436}
437
438void
439match_set_ct_state_masked(struct match *match, uint32_t ct_state, uint32_t mask)
440{
2a28ccc8
JR
441 match->flow.ct_state = ct_state & mask & UINT8_MAX;
442 match->wc.masks.ct_state = mask & UINT8_MAX;
07659514
JS
443}
444
445void
446match_set_ct_zone(struct match *match, uint16_t ct_zone)
447{
62df1f5b
PB
448 match_set_ct_zone_masked(match, ct_zone, UINT16_MAX);
449}
450
451void
452match_set_ct_zone_masked(struct match *match, uint16_t ct_zone, uint16_t mask)
453{
454 match->flow.ct_zone = ct_zone & mask;
455 match->wc.masks.ct_zone = mask;
07659514
JS
456}
457
8e53fe8c
JS
458void
459match_set_ct_mark(struct match *match, uint32_t ct_mark)
460{
461 match_set_ct_mark_masked(match, ct_mark, UINT32_MAX);
462}
463
464void
465match_set_ct_mark_masked(struct match *match, uint32_t ct_mark,
466 uint32_t mask)
467{
468 match->flow.ct_mark = ct_mark & mask;
469 match->wc.masks.ct_mark = mask;
470}
471
9daf2348
JS
472void
473match_set_ct_label(struct match *match, ovs_u128 ct_label)
474{
475 ovs_u128 mask;
476
477 mask.u64.lo = UINT64_MAX;
478 mask.u64.hi = UINT64_MAX;
479 match_set_ct_label_masked(match, ct_label, mask);
480}
481
482void
483match_set_ct_label_masked(struct match *match, ovs_u128 value, ovs_u128 mask)
484{
485 match->flow.ct_label.u64.lo = value.u64.lo & mask.u64.lo;
486 match->flow.ct_label.u64.hi = value.u64.hi & mask.u64.hi;
487 match->wc.masks.ct_label = mask;
488}
489
daf4d3c1
JR
490void
491match_set_ct_nw_src(struct match *match, ovs_be32 ct_nw_src)
492{
493 match->flow.ct_nw_src = ct_nw_src;
494 match->wc.masks.ct_nw_src = OVS_BE32_MAX;
495}
496
497void
498match_set_ct_nw_src_masked(struct match *match, ovs_be32 ct_nw_src,
499 ovs_be32 mask)
500{
501 match->flow.ct_nw_src = ct_nw_src & mask;
502 match->wc.masks.ct_nw_src = mask;
503}
504
505void
506match_set_ct_nw_dst(struct match *match, ovs_be32 ct_nw_dst)
507{
508 match->flow.ct_nw_dst = ct_nw_dst;
509 match->wc.masks.ct_nw_dst = OVS_BE32_MAX;
510}
511
512void
513match_set_ct_nw_dst_masked(struct match *match, ovs_be32 ct_nw_dst,
514 ovs_be32 mask)
515{
516 match->flow.ct_nw_dst = ct_nw_dst & mask;
517 match->wc.masks.ct_nw_dst = mask;
518}
519
520void
521match_set_ct_nw_proto(struct match *match, uint8_t ct_nw_proto)
522{
523 match->flow.ct_nw_proto = ct_nw_proto;
524 match->wc.masks.ct_nw_proto = UINT8_MAX;
525}
526
527void
528match_set_ct_tp_src(struct match *match, ovs_be16 ct_tp_src)
529{
530 match_set_ct_tp_src_masked(match, ct_tp_src, OVS_BE16_MAX);
531}
532
533void
534match_set_ct_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
535{
536 match->flow.ct_tp_src = port & mask;
537 match->wc.masks.ct_tp_src = mask;
538}
539
540void
541match_set_ct_tp_dst(struct match *match, ovs_be16 ct_tp_dst)
542{
543 match_set_ct_tp_dst_masked(match, ct_tp_dst, OVS_BE16_MAX);
544}
545
546void
547match_set_ct_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
548{
549 match->flow.ct_tp_dst = port & mask;
550 match->wc.masks.ct_tp_dst = mask;
551}
552
553void
554match_set_ct_ipv6_src(struct match *match, const struct in6_addr *src)
555{
556 match->flow.ct_ipv6_src = *src;
557 match->wc.masks.ct_ipv6_src = in6addr_exact;
558}
559
560void
561match_set_ct_ipv6_src_masked(struct match *match, const struct in6_addr *src,
562 const struct in6_addr *mask)
563{
564 match->flow.ct_ipv6_src = ipv6_addr_bitand(src, mask);
565 match->wc.masks.ct_ipv6_src = *mask;
566}
567
568void
569match_set_ct_ipv6_dst(struct match *match, const struct in6_addr *dst)
570{
571 match->flow.ct_ipv6_dst = *dst;
572 match->wc.masks.ct_ipv6_dst = in6addr_exact;
573}
574
575void
576match_set_ct_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
577 const struct in6_addr *mask)
578{
579 match->flow.ct_ipv6_dst = ipv6_addr_bitand(dst, mask);
580 match->wc.masks.ct_ipv6_dst = *mask;
581}
582
beb75a40
JS
583void
584match_set_packet_type(struct match *match, ovs_be32 packet_type)
585{
586 match->flow.packet_type = packet_type;
587 match->wc.masks.packet_type = OVS_BE32_MAX;
588}
589
3d4b2e6e
JS
590/* If 'match' does not match on any packet type, make it match on Ethernet
591 * packets (the default packet type, as specified by OpenFlow). */
592void
593match_set_default_packet_type(struct match *match)
594{
595 if (!match->wc.masks.packet_type) {
596 match_set_packet_type(match, htonl(PT_ETH));
597 }
598}
599
600/* Returns true if 'match' matches only Ethernet packets (the default packet
601 * type, as specified by OpenFlow). */
602bool
603match_has_default_packet_type(const struct match *match)
604{
605 return (match->flow.packet_type == htonl(PT_ETH)
606 && match->wc.masks.packet_type == OVS_BE32_MAX);
607}
608
609/* A match on 'field' is being added to or has been added to 'match'. If
610 * 'field' is a data field, and 'match' does not already match on packet_type,
611 * this function make it match on the Ethernet packet_type.
612 *
613 * This function is useful because OpenFlow implicitly applies to Ethernet
614 * packets when there's no explicit packet_type, but matching on a metadata
615 * field doesn't imply anything about the packet_type and falsely inferring
616 * that it does can cause harm. A flow that matches only on metadata fields,
617 * for example, should be able to match more than just Ethernet flows. There
618 * are also important reasons that a catch-all match (one with no field matches
619 * at all) should not imply a packet_type(0,0) match. For example, a "flow
620 * dump" request that matches on no fields should return every flow in the
621 * switch, not just the flows that match on Ethernet. As a second example,
622 * OpenFlow 1.2+ special-cases "table miss" flows, that is catch-all flows with
623 * priority 0, and inferring a match on packet_type(0,0) causes such a flow not
624 * to be a table miss flow. */
625void
626match_add_ethernet_prereq(struct match *match, const struct mf_field *field)
627{
628 if (field->prereqs != MFP_NONE) {
629 match_set_default_packet_type(match);
630 }
631}
632
81a76618
BP
633void
634match_set_dl_type(struct match *match, ovs_be16 dl_type)
635{
b8266395 636 match->wc.masks.dl_type = OVS_BE16_MAX;
81a76618
BP
637 match->flow.dl_type = dl_type;
638}
639
640/* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
641 * exactly. 'mask_dst' is set to all 1s. */
642static void
74ff3298
JR
643set_eth(const struct eth_addr value_src,
644 struct eth_addr *value_dst,
645 struct eth_addr *mask_dst)
81a76618 646{
74ff3298
JR
647 *value_dst = value_src;
648 *mask_dst = eth_addr_exact;
81a76618
BP
649}
650
651/* Modifies 'value_src' so that the Ethernet address must match 'value_src'
652 * after each byte is ANDed with the appropriate byte in 'mask_src'.
653 * 'mask_dst' is set to 'mask_src' */
654static void
74ff3298
JR
655set_eth_masked(const struct eth_addr value_src,
656 const struct eth_addr mask_src,
657 struct eth_addr *value_dst, struct eth_addr *mask_dst)
81a76618
BP
658{
659 size_t i;
660
74ff3298
JR
661 for (i = 0; i < ARRAY_SIZE(value_dst->be16); i++) {
662 value_dst->be16[i] = value_src.be16[i] & mask_src.be16[i];
81a76618 663 }
74ff3298 664 *mask_dst = mask_src;
81a76618
BP
665}
666
667/* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
668 * exactly. */
669void
74ff3298 670match_set_dl_src(struct match *match, const struct eth_addr dl_src)
81a76618 671{
74ff3298 672 set_eth(dl_src, &match->flow.dl_src, &match->wc.masks.dl_src);
81a76618
BP
673}
674
675/* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
676 * after each byte is ANDed with the appropriate byte in 'mask'. */
677void
678match_set_dl_src_masked(struct match *match,
74ff3298
JR
679 const struct eth_addr dl_src,
680 const struct eth_addr mask)
81a76618 681{
74ff3298 682 set_eth_masked(dl_src, mask, &match->flow.dl_src, &match->wc.masks.dl_src);
81a76618
BP
683}
684
685/* Modifies 'match' so that the Ethernet address must match 'dl_dst'
686 * exactly. */
687void
74ff3298 688match_set_dl_dst(struct match *match, const struct eth_addr dl_dst)
81a76618 689{
74ff3298 690 set_eth(dl_dst, &match->flow.dl_dst, &match->wc.masks.dl_dst);
81a76618
BP
691}
692
693/* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
694 * byte is ANDed with the appropriate byte in 'mask'.
695 *
696 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
697 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
698void
699match_set_dl_dst_masked(struct match *match,
74ff3298
JR
700 const struct eth_addr dl_dst,
701 const struct eth_addr mask)
81a76618 702{
74ff3298 703 set_eth_masked(dl_dst, mask, &match->flow.dl_dst, &match->wc.masks.dl_dst);
81a76618
BP
704}
705
706void
707match_set_dl_tci(struct match *match, ovs_be16 tci)
708{
709 match_set_dl_tci_masked(match, tci, htons(0xffff));
710}
711
712void
713match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
714{
f0fb825a
EG
715 match->flow.vlans[0].tci = tci & mask;
716 match->wc.masks.vlans[0].tci = mask;
81a76618
BP
717}
718
719/* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
720 * wildcarded, then 'match' will match a packet regardless of whether it has an
721 * 802.1Q header or not. */
722void
723match_set_any_vid(struct match *match)
724{
f0fb825a
EG
725 if (match->wc.masks.vlans[0].tci & htons(VLAN_PCP_MASK)) {
726 match->wc.masks.vlans[0].tci &= ~htons(VLAN_VID_MASK);
727 match->flow.vlans[0].tci &= ~htons(VLAN_VID_MASK);
81a76618
BP
728 } else {
729 match_set_dl_tci_masked(match, htons(0), htons(0));
730 }
731}
732
733/* Modifies 'match' depending on 'dl_vlan':
734 *
735 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
736 * without an 802.1Q header.
737 *
738 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
739 * VID equals the low 12 bits of 'dl_vlan'.
740 */
741void
2f9366be 742match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan, int id)
81a76618 743{
2f9366be 744 flow_set_dl_vlan(&match->flow, dl_vlan, id);
81a76618 745 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
2f9366be 746 match->wc.masks.vlans[id].tci = OVS_BE16_MAX;
81a76618 747 } else {
2f9366be 748 match->wc.masks.vlans[id].tci |= htons(VLAN_VID_MASK | VLAN_CFI);
81a76618
BP
749 }
750}
751
752/* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
753 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
754 * plus CFI). */
755void
756match_set_vlan_vid(struct match *match, ovs_be16 vid)
757{
758 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
759}
760
761
762/* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
763 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
764 * plus CFI), with the corresponding 'mask'. */
765void
766match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
767{
768 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
769 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
770
771 mask &= vid_mask;
772 flow_set_vlan_vid(&match->flow, vid & mask);
f0fb825a
EG
773 match->wc.masks.vlans[0].tci =
774 mask | (match->wc.masks.vlans[0].tci & pcp_mask);
81a76618
BP
775}
776
777/* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
778 * wildcarded, then 'match' will match a packet regardless of whether it has an
779 * 802.1Q header or not. */
780void
781match_set_any_pcp(struct match *match)
782{
f0fb825a
EG
783 if (match->wc.masks.vlans[0].tci & htons(VLAN_VID_MASK)) {
784 match->wc.masks.vlans[0].tci &= ~htons(VLAN_PCP_MASK);
785 match->flow.vlans[0].tci &= ~htons(VLAN_PCP_MASK);
81a76618
BP
786 } else {
787 match_set_dl_tci_masked(match, htons(0), htons(0));
788 }
789}
790
791/* Modifies 'match' so that it matches only packets with an 802.1Q header whose
792 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
793void
2f9366be 794match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp, int id)
81a76618 795{
2f9366be
JL
796 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp, id);
797 match->wc.masks.vlans[id].tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
81a76618
BP
798}
799
8bfd0fda
BP
800/* Modifies 'match' so that the MPLS label 'idx' matches 'lse' exactly. */
801void
802match_set_mpls_lse(struct match *match, int idx, ovs_be32 lse)
803{
804 match->wc.masks.mpls_lse[idx] = OVS_BE32_MAX;
805 match->flow.mpls_lse[idx] = lse;
806}
807
b02475c5
SH
808/* Modifies 'match' so that the MPLS label is wildcarded. */
809void
8bfd0fda 810match_set_any_mpls_label(struct match *match, int idx)
b02475c5 811{
8bfd0fda
BP
812 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_LABEL_MASK);
813 flow_set_mpls_label(&match->flow, idx, htonl(0));
b02475c5
SH
814}
815
816/* Modifies 'match' so that it matches only packets with an MPLS header whose
817 * label equals the low 20 bits of 'mpls_label'. */
818void
8bfd0fda 819match_set_mpls_label(struct match *match, int idx, ovs_be32 mpls_label)
b02475c5 820{
8bfd0fda
BP
821 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_LABEL_MASK);
822 flow_set_mpls_label(&match->flow, idx, mpls_label);
b02475c5
SH
823}
824
825/* Modifies 'match' so that the MPLS TC is wildcarded. */
826void
8bfd0fda 827match_set_any_mpls_tc(struct match *match, int idx)
b02475c5 828{
8bfd0fda
BP
829 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TC_MASK);
830 flow_set_mpls_tc(&match->flow, idx, 0);
b02475c5
SH
831}
832
833/* Modifies 'match' so that it matches only packets with an MPLS header whose
834 * Traffic Class equals the low 3 bits of 'mpls_tc'. */
835void
8bfd0fda 836match_set_mpls_tc(struct match *match, int idx, uint8_t mpls_tc)
b02475c5 837{
8bfd0fda
BP
838 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TC_MASK);
839 flow_set_mpls_tc(&match->flow, idx, mpls_tc);
b02475c5
SH
840}
841
842/* Modifies 'match' so that the MPLS stack flag is wildcarded. */
843void
8bfd0fda 844match_set_any_mpls_bos(struct match *match, int idx)
b02475c5 845{
8bfd0fda
BP
846 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_BOS_MASK);
847 flow_set_mpls_bos(&match->flow, idx, 0);
b02475c5
SH
848}
849
850/* Modifies 'match' so that it matches only packets with an MPLS header whose
851 * Stack Flag equals the lower bit of 'mpls_bos' */
852void
8bfd0fda
BP
853match_set_mpls_bos(struct match *match, int idx, uint8_t mpls_bos)
854{
855 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_BOS_MASK);
856 flow_set_mpls_bos(&match->flow, idx, mpls_bos);
857}
858
bef3f465
BP
859/* Modifies 'match' so that the TTL of MPLS label 'idx' is wildcarded. */
860void
861match_set_any_mpls_ttl(struct match *match, int idx)
862{
863 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TTL_MASK);
864 flow_set_mpls_ttl(&match->flow, idx, 0);
865}
866
867/* Modifies 'match' so that it matches only packets in which the TTL of MPLS
868 * label 'idx' equals 'mpls_ttl'. */
869void
870match_set_mpls_ttl(struct match *match, int idx, uint8_t mpls_ttl)
871{
872 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TTL_MASK);
873 flow_set_mpls_ttl(&match->flow, idx, mpls_ttl);
874}
875
8bfd0fda
BP
876/* Modifies 'match' so that the MPLS LSE is wildcarded. */
877void
878match_set_any_mpls_lse(struct match *match, int idx)
b02475c5 879{
8bfd0fda
BP
880 match->wc.masks.mpls_lse[idx] = htonl(0);
881 flow_set_mpls_lse(&match->flow, idx, htonl(0));
b02475c5
SH
882}
883
81a76618
BP
884void
885match_set_tp_src(struct match *match, ovs_be16 tp_src)
886{
b8266395 887 match_set_tp_src_masked(match, tp_src, OVS_BE16_MAX);
81a76618
BP
888}
889
890void
891match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
892{
893 match->flow.tp_src = port & mask;
894 match->wc.masks.tp_src = mask;
895}
896
897void
898match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
899{
b8266395 900 match_set_tp_dst_masked(match, tp_dst, OVS_BE16_MAX);
81a76618
BP
901}
902
903void
904match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
905{
906 match->flow.tp_dst = port & mask;
907 match->wc.masks.tp_dst = mask;
908}
909
dc235f7f
JR
910void
911match_set_tcp_flags(struct match *match, ovs_be16 flags)
912{
913 match_set_tcp_flags_masked(match, flags, OVS_BE16_MAX);
914}
915
916void
917match_set_tcp_flags_masked(struct match *match, ovs_be16 flags, ovs_be16 mask)
918{
919 match->flow.tcp_flags = flags & mask;
920 match->wc.masks.tcp_flags = mask;
921}
922
81a76618
BP
923void
924match_set_nw_proto(struct match *match, uint8_t nw_proto)
925{
926 match->flow.nw_proto = nw_proto;
927 match->wc.masks.nw_proto = UINT8_MAX;
928}
929
a3db6e47
TZ
930void
931match_set_nw_proto_masked(struct match *match,
932 const uint8_t nw_proto, const uint8_t mask)
933{
934 match->flow.nw_proto = nw_proto;
935 match->wc.masks.nw_proto = mask;
936}
937
81a76618
BP
938void
939match_set_nw_src(struct match *match, ovs_be32 nw_src)
940{
941 match->flow.nw_src = nw_src;
b8266395 942 match->wc.masks.nw_src = OVS_BE32_MAX;
81a76618
BP
943}
944
945void
946match_set_nw_src_masked(struct match *match,
947 ovs_be32 nw_src, ovs_be32 mask)
948{
949 match->flow.nw_src = nw_src & mask;
950 match->wc.masks.nw_src = mask;
951}
952
953void
954match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
955{
956 match->flow.nw_dst = nw_dst;
b8266395 957 match->wc.masks.nw_dst = OVS_BE32_MAX;
81a76618
BP
958}
959
960void
961match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
962{
963 match->flow.nw_dst = ip & mask;
964 match->wc.masks.nw_dst = mask;
965}
966
967void
968match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
969{
970 match->wc.masks.nw_tos |= IP_DSCP_MASK;
971 match->flow.nw_tos &= ~IP_DSCP_MASK;
972 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
973}
974
975void
976match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
977{
978 match->wc.masks.nw_tos |= IP_ECN_MASK;
979 match->flow.nw_tos &= ~IP_ECN_MASK;
980 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
981}
982
983void
984match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
985{
986 match->wc.masks.nw_ttl = UINT8_MAX;
987 match->flow.nw_ttl = nw_ttl;
988}
989
dfa2ccdb
OG
990void
991match_set_nw_tos_masked(struct match *match, uint8_t nw_tos, uint8_t mask)
992{
993 match->flow.nw_tos = nw_tos & mask;
994 match->wc.masks.nw_tos = mask;
995}
996
c489ad67
PB
997void
998match_set_nw_ttl_masked(struct match *match, uint8_t nw_ttl, uint8_t mask)
999{
1000 match->flow.nw_ttl = nw_ttl & mask;
1001 match->wc.masks.nw_ttl = mask;
1002}
1003
81a76618
BP
1004void
1005match_set_nw_frag(struct match *match, uint8_t nw_frag)
1006{
1007 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1008 match->flow.nw_frag = nw_frag;
1009}
1010
1011void
1012match_set_nw_frag_masked(struct match *match,
1013 uint8_t nw_frag, uint8_t mask)
1014{
1015 match->flow.nw_frag = nw_frag & mask;
1016 match->wc.masks.nw_frag = mask;
1017}
1018
1019void
1020match_set_icmp_type(struct match *match, uint8_t icmp_type)
1021{
1022 match_set_tp_src(match, htons(icmp_type));
1023}
1024
1025void
1026match_set_icmp_code(struct match *match, uint8_t icmp_code)
1027{
1028 match_set_tp_dst(match, htons(icmp_code));
1029}
1030
a3db6e47
TZ
1031void
1032match_set_arp_opcode_masked(struct match *match,
1033 const uint8_t opcode,
1034 const uint8_t mask)
1035{
1036 match_set_nw_proto_masked(match, opcode, mask);
1037}
1038
1039void
1040match_set_arp_spa_masked(struct match *match,
1041 const ovs_be32 arp_spa,
1042 const ovs_be32 mask)
1043{
1044 match_set_nw_src_masked(match, arp_spa, mask);
1045}
1046
1047void
1048match_set_arp_tpa_masked(struct match *match,
1049 const ovs_be32 arp_tpa,
1050 const ovs_be32 mask)
1051{
1052 match_set_nw_dst_masked(match, arp_tpa, mask);
1053}
1054
81a76618 1055void
74ff3298 1056match_set_arp_sha(struct match *match, const struct eth_addr sha)
81a76618 1057{
74ff3298
JR
1058 match->flow.arp_sha = sha;
1059 match->wc.masks.arp_sha = eth_addr_exact;
81a76618
BP
1060}
1061
1062void
1063match_set_arp_sha_masked(struct match *match,
74ff3298
JR
1064 const struct eth_addr arp_sha,
1065 const struct eth_addr mask)
81a76618
BP
1066{
1067 set_eth_masked(arp_sha, mask,
74ff3298 1068 &match->flow.arp_sha, &match->wc.masks.arp_sha);
81a76618
BP
1069}
1070
1071void
74ff3298 1072match_set_arp_tha(struct match *match, const struct eth_addr tha)
81a76618 1073{
74ff3298
JR
1074 match->flow.arp_tha = tha;
1075 match->wc.masks.arp_tha = eth_addr_exact;
81a76618
BP
1076}
1077
1078void
1079match_set_arp_tha_masked(struct match *match,
74ff3298
JR
1080 const struct eth_addr arp_tha,
1081 const struct eth_addr mask)
81a76618
BP
1082{
1083 set_eth_masked(arp_tha, mask,
74ff3298 1084 &match->flow.arp_tha, &match->wc.masks.arp_tha);
81a76618
BP
1085}
1086
1087void
1088match_set_ipv6_src(struct match *match, const struct in6_addr *src)
1089{
1090 match->flow.ipv6_src = *src;
1091 match->wc.masks.ipv6_src = in6addr_exact;
1092}
1093
1094void
1095match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
1096 const struct in6_addr *mask)
1097{
1098 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
1099 match->wc.masks.ipv6_src = *mask;
1100}
1101
1102void
1103match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
1104{
1105 match->flow.ipv6_dst = *dst;
1106 match->wc.masks.ipv6_dst = in6addr_exact;
1107}
1108
1109void
1110match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
1111 const struct in6_addr *mask)
1112{
1113 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
1114 match->wc.masks.ipv6_dst = *mask;
1115}
1116
1117void
1118match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
1119{
b8266395 1120 match->wc.masks.ipv6_label = OVS_BE32_MAX;
81a76618
BP
1121 match->flow.ipv6_label = ipv6_label;
1122}
1123
1124
1125void
1126match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
1127 ovs_be32 mask)
1128{
1129 match->flow.ipv6_label = ipv6_label & mask;
1130 match->wc.masks.ipv6_label = mask;
1131}
1132
1133void
1134match_set_nd_target(struct match *match, const struct in6_addr *target)
1135{
1136 match->flow.nd_target = *target;
1137 match->wc.masks.nd_target = in6addr_exact;
1138}
1139
1140void
1141match_set_nd_target_masked(struct match *match,
1142 const struct in6_addr *target,
1143 const struct in6_addr *mask)
1144{
1145 match->flow.nd_target = ipv6_addr_bitand(target, mask);
1146 match->wc.masks.nd_target = *mask;
1147}
1148
9b2b8497
VDA
1149void
1150match_set_nd_reserved (struct match *match, ovs_be32 value)
1151{
1152 match->flow.igmp_group_ip4 = value;
1153 match->wc.masks.igmp_group_ip4 = OVS_BE32_MAX;
1154}
1155
1156void
1157match_set_nd_options_type(struct match *match, uint8_t option)
1158{
1159 match_set_tcp_flags(match, htons(option));
1160}
1161
81a76618
BP
1162/* Returns true if 'a' and 'b' wildcard the same fields and have the same
1163 * values for fixed fields, otherwise false. */
1164bool
1165match_equal(const struct match *a, const struct match *b)
1166{
1167 return (flow_wildcards_equal(&a->wc, &b->wc)
1168 && flow_equal(&a->flow, &b->flow));
1169}
1170
1171/* Returns a hash value for the flow and wildcards in 'match', starting from
1172 * 'basis'. */
1173uint32_t
1174match_hash(const struct match *match, uint32_t basis)
1175{
1176 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
1177}
1178
adcf00ba
AZ
1179static bool
1180match_has_default_recirc_id(const struct match *m)
1181{
1182 return m->flow.recirc_id == 0 && (m->wc.masks.recirc_id == UINT32_MAX ||
1183 m->wc.masks.recirc_id == 0);
1184}
1185
1186static bool
1187match_has_default_dp_hash(const struct match *m)
1188{
1189 return ((m->flow.dp_hash | m->wc.masks.dp_hash) == 0);
1190}
1191
1192/* Return true if the hidden fields of the match are set to the default values.
1193 * The default values equals to those set up by match_init_hidden_fields(). */
1194bool
1195match_has_default_hidden_fields(const struct match *m)
1196{
1197 return match_has_default_recirc_id(m) && match_has_default_dp_hash(m);
1198}
1199
1200void
1201match_init_hidden_fields(struct match *m)
1202{
1203 match_set_recirc_id(m, 0);
1204 match_set_dp_hash_masked(m, 0, 0);
1205}
1206
81a76618 1207static void
3bd0fd39 1208format_eth_masked(struct ds *s, const char *name,
74ff3298 1209 const struct eth_addr eth, const struct eth_addr mask)
81a76618
BP
1210{
1211 if (!eth_addr_is_zero(mask)) {
0deec6d2 1212 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
74ff3298 1213 eth_format_masked(eth, &mask, s);
81a76618
BP
1214 ds_put_char(s, ',');
1215 }
1216}
1217
1218static void
1219format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
1220 ovs_be32 netmask)
1221{
1222 if (netmask) {
0deec6d2 1223 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
81a76618
BP
1224 ip_format_masked(ip, netmask, s);
1225 ds_put_char(s, ',');
1226 }
1227}
1228
1229static void
1230format_ipv6_netmask(struct ds *s, const char *name,
1231 const struct in6_addr *addr,
1232 const struct in6_addr *netmask)
1233{
1234 if (!ipv6_mask_is_any(netmask)) {
0deec6d2 1235 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
ac6d120f 1236 ipv6_format_masked(addr, netmask, s);
81a76618
BP
1237 ds_put_char(s, ',');
1238 }
1239}
1240
3d2fbd70
JS
1241static void
1242format_uint8_masked(struct ds *s, const char *name,
1243 uint8_t value, uint8_t mask)
1244{
1245 if (mask != 0) {
1246 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
1247 if (mask == UINT8_MAX) {
1248 ds_put_format(s, "%"PRIu8, value);
1249 } else {
1250 ds_put_format(s, "0x%02"PRIx8"/0x%02"PRIx8, value, mask);
1251 }
1252 ds_put_char(s, ',');
1253 }
1254}
1255
07659514
JS
1256static void
1257format_uint16_masked(struct ds *s, const char *name,
1258 uint16_t value, uint16_t mask)
1259{
1260 if (mask != 0) {
0deec6d2 1261 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
07659514
JS
1262 if (mask == UINT16_MAX) {
1263 ds_put_format(s, "%"PRIu16, value);
1264 } else {
1265 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16, value, mask);
1266 }
1267 ds_put_char(s, ',');
1268 }
1269}
1270
81a76618
BP
1271static void
1272format_be16_masked(struct ds *s, const char *name,
1273 ovs_be16 value, ovs_be16 mask)
1274{
1275 if (mask != htons(0)) {
0deec6d2 1276 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
b8266395 1277 if (mask == OVS_BE16_MAX) {
81a76618
BP
1278 ds_put_format(s, "%"PRIu16, ntohs(value));
1279 } else {
1280 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
1281 ntohs(value), ntohs(mask));
1282 }
1283 ds_put_char(s, ',');
1284 }
1285}
1286
8bfd0fda
BP
1287static void
1288format_be32_masked(struct ds *s, const char *name,
1289 ovs_be32 value, ovs_be32 mask)
1290{
1291 if (mask != htonl(0)) {
0deec6d2 1292 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
8bfd0fda
BP
1293 if (mask == OVS_BE32_MAX) {
1294 ds_put_format(s, "%"PRIu32, ntohl(value));
3d2fbd70
JS
1295 } else {
1296 ds_put_format(s, "0x%08"PRIx32"/0x%08"PRIx32,
1297 ntohl(value), ntohl(mask));
1298 }
1299 ds_put_char(s, ',');
1300 }
1301}
1302
1303static void
1304format_be32_masked_hex(struct ds *s, const char *name,
1305 ovs_be32 value, ovs_be32 mask)
1306{
1307 if (mask != htonl(0)) {
1308 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
1309 if (mask == OVS_BE32_MAX) {
1310 ds_put_format(s, "0x%"PRIx32, ntohl(value));
8bfd0fda
BP
1311 } else {
1312 ds_put_format(s, "0x%"PRIx32"/0x%"PRIx32,
1313 ntohl(value), ntohl(mask));
1314 }
1315 ds_put_char(s, ',');
1316 }
1317}
1318
b60f804e
AZ
1319static void
1320format_uint32_masked(struct ds *s, const char *name,
1321 uint32_t value, uint32_t mask)
1322{
1323 if (mask) {
0deec6d2
QM
1324 ds_put_format(s, "%s%s=%s%#"PRIx32,
1325 colors.param, name, colors.end, value);
b60f804e
AZ
1326 if (mask != UINT32_MAX) {
1327 ds_put_format(s, "/%#"PRIx32, mask);
1328 }
1329 ds_put_char(s, ',');
1330 }
1331}
1332
1333static void
1334format_be64_masked(struct ds *s, const char *name,
1335 ovs_be64 value, ovs_be64 mask)
1336{
1337 if (mask != htonll(0)) {
0deec6d2
QM
1338 ds_put_format(s, "%s%s=%s%#"PRIx64,
1339 colors.param, name, colors.end, ntohll(value));
b60f804e
AZ
1340 if (mask != OVS_BE64_MAX) {
1341 ds_put_format(s, "/%#"PRIx64, ntohll(mask));
1342 }
1343 ds_put_char(s, ',');
1344 }
1345}
1346
4fe3445a
PS
1347static void
1348format_flow_tunnel(struct ds *s, const struct match *match)
1349{
1350 const struct flow_wildcards *wc = &match->wc;
1351 const struct flow_tnl *tnl = &match->flow.tunnel;
1352
b60f804e 1353 format_be64_masked(s, "tun_id", tnl->tun_id, wc->masks.tunnel.tun_id);
4fe3445a
PS
1354 format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
1355 format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
ffe4c74f
JB
1356 format_ipv6_netmask(s, "tun_ipv6_src", &tnl->ipv6_src,
1357 &wc->masks.tunnel.ipv6_src);
1358 format_ipv6_netmask(s, "tun_ipv6_dst", &tnl->ipv6_dst,
1359 &wc->masks.tunnel.ipv6_dst);
4fe3445a 1360
ac6073e3
MC
1361 if (wc->masks.tunnel.gbp_id) {
1362 format_be16_masked(s, "tun_gbp_id", tnl->gbp_id,
1363 wc->masks.tunnel.gbp_id);
1364 }
1365
1366 if (wc->masks.tunnel.gbp_flags) {
1367 ds_put_format(s, "tun_gbp_flags=%#"PRIx8",", tnl->gbp_flags);
1368 }
1369
4fe3445a
PS
1370 if (wc->masks.tunnel.ip_tos) {
1371 ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
1372 }
1373 if (wc->masks.tunnel.ip_ttl) {
1374 ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
1375 }
7dc18ae9
WT
1376 if (wc->masks.tunnel.erspan_ver) {
1377 ds_put_format(s, "tun_erspan_ver=%"PRIu8",", tnl->erspan_ver);
1378 }
1379 if (wc->masks.tunnel.erspan_idx && tnl->erspan_ver == 1) {
9bf871c4 1380 ds_put_format(s, "tun_erspan_idx=%#"PRIx32",", tnl->erspan_idx);
7dc18ae9
WT
1381 }
1382 if (wc->masks.tunnel.erspan_dir && tnl->erspan_ver == 2) {
1383 ds_put_format(s, "tun_erspan_dir=%"PRIu8",", tnl->erspan_dir);
1384 }
1385 if (wc->masks.tunnel.erspan_hwid && tnl->erspan_ver == 2) {
1386 ds_put_format(s, "tun_erspan_hwid=%#"PRIx8",", tnl->erspan_hwid);
1387 }
3c6d05a0
WT
1388 if (wc->masks.tunnel.gtpu_flags) {
1389 ds_put_format(s, "gtpu_flags=%#"PRIx8",", tnl->gtpu_flags);
1390 }
1391 if (wc->masks.tunnel.gtpu_msgtype) {
1392 ds_put_format(s, "gtpu_msgtype=%"PRIu8",", tnl->gtpu_msgtype);
1393 }
3721b406 1394 if (wc->masks.tunnel.flags & FLOW_TNL_F_MASK) {
b666962b 1395 format_flags_masked(s, "tun_flags", flow_tun_flag_to_string,
3721b406 1396 tnl->flags & FLOW_TNL_F_MASK,
b666962b
JG
1397 wc->masks.tunnel.flags & FLOW_TNL_F_MASK,
1398 FLOW_TNL_F_MASK);
4fe3445a
PS
1399 ds_put_char(s, ',');
1400 }
9558d2a5 1401 tun_metadata_match_format(s, match);
4fe3445a
PS
1402}
1403
9daf2348
JS
1404static void
1405format_ct_label_masked(struct ds *s, const ovs_u128 *key, const ovs_u128 *mask)
1406{
2ff8484b 1407 if (!ovs_u128_is_zero(*mask)) {
32ea15f6 1408 ovs_be128 value = hton128(*key);
0deec6d2 1409 ds_put_format(s, "%sct_label=%s", colors.param, colors.end);
9daf2348
JS
1410 ds_put_hex(s, &value, sizeof value);
1411 if (!is_all_ones(mask, sizeof(*mask))) {
32ea15f6 1412 value = hton128(*mask);
9daf2348
JS
1413 ds_put_char(s, '/');
1414 ds_put_hex(s, &value, sizeof value);
1415 }
1416 ds_put_char(s, ',');
1417 }
1418}
1419
3d2fbd70
JS
1420static void
1421format_nsh_masked(struct ds *s, const struct flow *f, const struct flow *m)
1422{
17553f27
YY
1423 ovs_be32 spi_mask = nsh_path_hdr_to_spi(m->nsh.path_hdr);
1424 if (spi_mask == htonl(NSH_SPI_MASK >> NSH_SPI_SHIFT)) {
1425 spi_mask = OVS_BE32_MAX;
1426 }
3d2fbd70 1427 format_uint8_masked(s, "nsh_flags", f->nsh.flags, m->nsh.flags);
17553f27 1428 format_uint8_masked(s, "nsh_ttl", f->nsh.ttl, m->nsh.ttl);
3d2fbd70
JS
1429 format_uint8_masked(s, "nsh_mdtype", f->nsh.mdtype, m->nsh.mdtype);
1430 format_uint8_masked(s, "nsh_np", f->nsh.np, m->nsh.np);
17553f27
YY
1431
1432 format_be32_masked_hex(s, "nsh_spi", nsh_path_hdr_to_spi(f->nsh.path_hdr),
1433 spi_mask);
1434 format_uint8_masked(s, "nsh_si", nsh_path_hdr_to_si(f->nsh.path_hdr),
1435 nsh_path_hdr_to_si(m->nsh.path_hdr));
3d2fbd70 1436 if (m->nsh.mdtype == UINT8_MAX && f->nsh.mdtype == NSH_M_TYPE1) {
f59cb331
YY
1437 format_be32_masked_hex(s, "nsh_c1", f->nsh.context[0],
1438 m->nsh.context[0]);
1439 format_be32_masked_hex(s, "nsh_c2", f->nsh.context[1],
1440 m->nsh.context[1]);
1441 format_be32_masked_hex(s, "nsh_c3", f->nsh.context[2],
1442 m->nsh.context[2]);
1443 format_be32_masked_hex(s, "nsh_c4", f->nsh.context[3],
1444 m->nsh.context[3]);
3d2fbd70
JS
1445 }
1446}
1447
81a76618 1448/* Appends a string representation of 'match' to 's'. If 'priority' is
50f96b10
BP
1449 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. If 'port_map' is
1450 * nonnull, uses it to translate port numbers to names in output. */
81a76618 1451void
50f96b10
BP
1452match_format(const struct match *match,
1453 const struct ofputil_port_map *port_map,
1454 struct ds *s, int priority)
81a76618
BP
1455{
1456 const struct flow_wildcards *wc = &match->wc;
1457 size_t start_len = s->length;
1458 const struct flow *f = &match->flow;
1459 bool skip_type = false;
1460 bool skip_proto = false;
3d4b2e6e 1461 ovs_be16 dl_type = f->dl_type;
e3981271 1462 bool is_megaflow = false;
81a76618
BP
1463 int i;
1464
3c6d05a0 1465 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 42);
81a76618
BP
1466
1467 if (priority != OFP_DEFAULT_PRIORITY) {
0deec6d2
QM
1468 ds_put_format(s, "%spriority=%s%d,",
1469 colors.special, colors.end, priority);
81a76618
BP
1470 }
1471
b60f804e 1472 format_uint32_masked(s, "pkt_mark", f->pkt_mark, wc->masks.pkt_mark);
1b567fb9 1473
a79f29f2
AZ
1474 if (wc->masks.recirc_id) {
1475 format_uint32_masked(s, "recirc_id", f->recirc_id,
1476 wc->masks.recirc_id);
e3981271 1477 is_megaflow = true;
a79f29f2
AZ
1478 }
1479
330de069 1480 if (wc->masks.dp_hash) {
a79f29f2
AZ
1481 format_uint32_masked(s, "dp_hash", f->dp_hash,
1482 wc->masks.dp_hash);
1483 }
1484
18080541 1485 if (wc->masks.conj_id) {
0deec6d2
QM
1486 ds_put_format(s, "%sconj_id%s=%"PRIu32",",
1487 colors.param, colors.end, f->conj_id);
18080541
BP
1488 }
1489
1b567fb9 1490 if (wc->masks.skb_priority) {
0deec6d2
QM
1491 ds_put_format(s, "%sskb_priority=%s%#"PRIx32",",
1492 colors.param, colors.end, f->skb_priority);
1b567fb9
AA
1493 }
1494
c61f3870 1495 if (wc->masks.actset_output) {
0deec6d2 1496 ds_put_format(s, "%sactset_output=%s", colors.param, colors.end);
50f96b10 1497 ofputil_format_port(f->actset_output, port_map, s);
c61f3870
BP
1498 ds_put_char(s, ',');
1499 }
1500
07659514 1501 if (wc->masks.ct_state) {
2a28ccc8 1502 if (wc->masks.ct_state == UINT8_MAX) {
0deec6d2 1503 ds_put_format(s, "%sct_state=%s", colors.param, colors.end);
07659514
JS
1504 if (f->ct_state) {
1505 format_flags(s, ct_state_to_string, f->ct_state, '|');
1506 } else {
1507 ds_put_cstr(s, "0"); /* No state. */
1508 }
1509 } else {
1510 format_flags_masked(s, "ct_state", ct_state_to_string,
2a28ccc8 1511 f->ct_state, wc->masks.ct_state, UINT8_MAX);
07659514
JS
1512 }
1513 ds_put_char(s, ',');
1514 }
1515
1516 if (wc->masks.ct_zone) {
1517 format_uint16_masked(s, "ct_zone", f->ct_zone, wc->masks.ct_zone);
1518 }
1519
8e53fe8c
JS
1520 if (wc->masks.ct_mark) {
1521 format_uint32_masked(s, "ct_mark", f->ct_mark, wc->masks.ct_mark);
1522 }
1523
2ff8484b 1524 if (!ovs_u128_is_zero(wc->masks.ct_label)) {
9daf2348
JS
1525 format_ct_label_masked(s, &f->ct_label, &wc->masks.ct_label);
1526 }
1527
daf4d3c1
JR
1528 format_ip_netmask(s, "ct_nw_src", f->ct_nw_src,
1529 wc->masks.ct_nw_src);
1530 format_ipv6_netmask(s, "ct_ipv6_src", &f->ct_ipv6_src,
1531 &wc->masks.ct_ipv6_src);
1532 format_ip_netmask(s, "ct_nw_dst", f->ct_nw_dst,
1533 wc->masks.ct_nw_dst);
1534 format_ipv6_netmask(s, "ct_ipv6_dst", &f->ct_ipv6_dst,
1535 &wc->masks.ct_ipv6_dst);
1536 if (wc->masks.ct_nw_proto) {
1537 ds_put_format(s, "%sct_nw_proto=%s%"PRIu8",",
1538 colors.param, colors.end, f->ct_nw_proto);
1539 format_be16_masked(s, "ct_tp_src", f->ct_tp_src, wc->masks.ct_tp_src);
1540 format_be16_masked(s, "ct_tp_dst", f->ct_tp_dst, wc->masks.ct_tp_dst);
1541 }
1542
e3981271
JS
1543 if (wc->masks.packet_type &&
1544 (!match_has_default_packet_type(match) || is_megaflow)) {
3d4b2e6e
JS
1545 format_packet_type_masked(s, f->packet_type, wc->masks.packet_type);
1546 ds_put_char(s, ',');
1547 if (pt_ns(f->packet_type) == OFPHTN_ETHERTYPE) {
1548 dl_type = pt_ns_type_be(f->packet_type);
beb75a40
JS
1549 }
1550 }
1551
81a76618
BP
1552 if (wc->masks.dl_type) {
1553 skip_type = true;
3d4b2e6e 1554 if (dl_type == htons(ETH_TYPE_IP)) {
81a76618
BP
1555 if (wc->masks.nw_proto) {
1556 skip_proto = true;
1557 if (f->nw_proto == IPPROTO_ICMP) {
0deec6d2 1558 ds_put_format(s, "%sicmp%s,", colors.value, colors.end);
0e612675 1559 } else if (f->nw_proto == IPPROTO_IGMP) {
0deec6d2 1560 ds_put_format(s, "%sigmp%s,", colors.value, colors.end);
81a76618 1561 } else if (f->nw_proto == IPPROTO_TCP) {
0deec6d2 1562 ds_put_format(s, "%stcp%s,", colors.value, colors.end);
81a76618 1563 } else if (f->nw_proto == IPPROTO_UDP) {
0deec6d2 1564 ds_put_format(s, "%sudp%s,", colors.value, colors.end);
0d56eaf2 1565 } else if (f->nw_proto == IPPROTO_SCTP) {
0deec6d2 1566 ds_put_format(s, "%ssctp%s,", colors.value, colors.end);
81a76618 1567 } else {
0deec6d2 1568 ds_put_format(s, "%sip%s,", colors.value, colors.end);
81a76618
BP
1569 skip_proto = false;
1570 }
1571 } else {
0deec6d2 1572 ds_put_format(s, "%sip%s,", colors.value, colors.end);
81a76618 1573 }
3d4b2e6e 1574 } else if (dl_type == htons(ETH_TYPE_IPV6)) {
81a76618
BP
1575 if (wc->masks.nw_proto) {
1576 skip_proto = true;
1577 if (f->nw_proto == IPPROTO_ICMPV6) {
0deec6d2 1578 ds_put_format(s, "%sicmp6%s,", colors.value, colors.end);
81a76618 1579 } else if (f->nw_proto == IPPROTO_TCP) {
0deec6d2 1580 ds_put_format(s, "%stcp6%s,", colors.value, colors.end);
81a76618 1581 } else if (f->nw_proto == IPPROTO_UDP) {
0deec6d2 1582 ds_put_format(s, "%sudp6%s,", colors.value, colors.end);
0d56eaf2 1583 } else if (f->nw_proto == IPPROTO_SCTP) {
0deec6d2 1584 ds_put_format(s, "%ssctp6%s,", colors.value, colors.end);
81a76618 1585 } else {
0deec6d2 1586 ds_put_format(s, "%sipv6%s,", colors.value, colors.end);
81a76618
BP
1587 skip_proto = false;
1588 }
1589 } else {
0deec6d2 1590 ds_put_format(s, "%sipv6%s,", colors.value, colors.end);
81a76618 1591 }
3d4b2e6e 1592 } else if (dl_type == htons(ETH_TYPE_ARP)) {
0deec6d2 1593 ds_put_format(s, "%sarp%s,", colors.value, colors.end);
3d4b2e6e 1594 } else if (dl_type == htons(ETH_TYPE_RARP)) {
0deec6d2 1595 ds_put_format(s, "%srarp%s,", colors.value, colors.end);
3d4b2e6e 1596 } else if (dl_type == htons(ETH_TYPE_MPLS)) {
0deec6d2 1597 ds_put_format(s, "%smpls%s,", colors.value, colors.end);
3d4b2e6e 1598 } else if (dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
0deec6d2 1599 ds_put_format(s, "%smplsm%s,", colors.value, colors.end);
81a76618
BP
1600 } else {
1601 skip_type = false;
1602 }
1603 }
1604 for (i = 0; i < FLOW_N_REGS; i++) {
b60f804e
AZ
1605 #define REGNAME_LEN 20
1606 char regname[REGNAME_LEN];
1607 if (snprintf(regname, REGNAME_LEN, "reg%d", i) >= REGNAME_LEN) {
1608 strcpy(regname, "reg?");
81a76618 1609 }
b60f804e 1610 format_uint32_masked(s, regname, f->regs[i], wc->masks.regs[i]);
81a76618 1611 }
4fe3445a
PS
1612
1613 format_flow_tunnel(s, match);
1614
b60f804e
AZ
1615 format_be64_masked(s, "metadata", f->metadata, wc->masks.metadata);
1616
4e022ec0 1617 if (wc->masks.in_port.ofp_port) {
0deec6d2 1618 ds_put_format(s, "%sin_port=%s", colors.param, colors.end);
50f96b10 1619 ofputil_format_port(f->in_port.ofp_port, port_map, s);
576ec803 1620 ds_put_char(s, ',');
81a76618 1621 }
f0fb825a
EG
1622 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
1623 char str_i[8];
81a76618 1624
f0fb825a
EG
1625 if (!wc->masks.vlans[i].tci) {
1626 break;
1627 }
1628
1629 /* Print VLAN tags as dl_vlan, dl_vlan1, dl_vlan2 ... */
1630 if (i == 0) {
1631 str_i[0] = '\0';
1632 } else {
1633 snprintf(str_i, sizeof(str_i), "%d", i);
1634 }
1635 ovs_be16 vid_mask = wc->masks.vlans[i].tci & htons(VLAN_VID_MASK);
1636 ovs_be16 pcp_mask = wc->masks.vlans[i].tci & htons(VLAN_PCP_MASK);
1637 ovs_be16 cfi = wc->masks.vlans[i].tci & htons(VLAN_CFI);
1638
1639 if (cfi && f->vlans[i].tci & htons(VLAN_CFI)
81a76618
BP
1640 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
1641 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
1642 && (vid_mask || pcp_mask)) {
1643 if (vid_mask) {
f0fb825a
EG
1644 ds_put_format(s, "%sdl_vlan%s=%s%"PRIu16",",
1645 colors.param, str_i, colors.end,
1646 vlan_tci_to_vid(f->vlans[i].tci));
81a76618
BP
1647 }
1648 if (pcp_mask) {
f0fb825a
EG
1649 ds_put_format(s, "%sdl_vlan_pcp%s=%s%d,",
1650 colors.param, str_i, colors.end,
1651 vlan_tci_to_pcp(f->vlans[i].tci));
81a76618 1652 }
f0fb825a
EG
1653 } else if (wc->masks.vlans[i].tci == htons(0xffff)) {
1654 ds_put_format(s, "%svlan_tci%s=%s0x%04"PRIx16",",
1655 colors.param, str_i, colors.end,
1656 ntohs(f->vlans[i].tci));
81a76618 1657 } else {
f0fb825a
EG
1658 ds_put_format(s, "%svlan_tci%s=%s0x%04"PRIx16"/0x%04"PRIx16",",
1659 colors.param, str_i, colors.end,
1660 ntohs(f->vlans[i].tci),
1661 ntohs(wc->masks.vlans[i].tci));
81a76618
BP
1662 }
1663 }
beb75a40 1664
81a76618
BP
1665 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
1666 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
beb75a40 1667
81a76618 1668 if (!skip_type && wc->masks.dl_type) {
0deec6d2 1669 ds_put_format(s, "%sdl_type=%s0x%04"PRIx16",",
3d4b2e6e 1670 colors.param, colors.end, ntohs(dl_type));
81a76618 1671 }
3d4b2e6e 1672 if (dl_type == htons(ETH_TYPE_IPV6)) {
81a76618
BP
1673 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
1674 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
1675 if (wc->masks.ipv6_label) {
b8266395 1676 if (wc->masks.ipv6_label == OVS_BE32_MAX) {
0deec6d2
QM
1677 ds_put_format(s, "%sipv6_label=%s0x%05"PRIx32",",
1678 colors.param, colors.end,
81a76618
BP
1679 ntohl(f->ipv6_label));
1680 } else {
0deec6d2
QM
1681 ds_put_format(s, "%sipv6_label=%s0x%05"PRIx32"/0x%05"PRIx32",",
1682 colors.param, colors.end, ntohl(f->ipv6_label),
81a76618
BP
1683 ntohl(wc->masks.ipv6_label));
1684 }
1685 }
3d4b2e6e
JS
1686 } else if (dl_type == htons(ETH_TYPE_ARP) ||
1687 dl_type == htons(ETH_TYPE_RARP)) {
666d0863
MM
1688 format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
1689 format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
3d2fbd70
JS
1690 } else if (dl_type == htons(ETH_TYPE_NSH)) {
1691 format_nsh_masked(s, f, &wc->masks);
81a76618
BP
1692 } else {
1693 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
1694 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
1695 }
1696 if (!skip_proto && wc->masks.nw_proto) {
3d4b2e6e
JS
1697 if (dl_type == htons(ETH_TYPE_ARP) ||
1698 dl_type == htons(ETH_TYPE_RARP)) {
0deec6d2
QM
1699 ds_put_format(s, "%sarp_op=%s%"PRIu8",",
1700 colors.param, colors.end, f->nw_proto);
81a76618 1701 } else {
0deec6d2
QM
1702 ds_put_format(s, "%snw_proto=%s%"PRIu8",",
1703 colors.param, colors.end, f->nw_proto);
81a76618
BP
1704 }
1705 }
3d4b2e6e
JS
1706 if (dl_type == htons(ETH_TYPE_ARP) ||
1707 dl_type == htons(ETH_TYPE_RARP)) {
81a76618
BP
1708 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
1709 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
1710 }
1711 if (wc->masks.nw_tos & IP_DSCP_MASK) {
fd13c6b5 1712 ds_put_format(s, "%snw_tos=%s%d,",
0deec6d2 1713 colors.param, colors.end, f->nw_tos & IP_DSCP_MASK);
81a76618
BP
1714 }
1715 if (wc->masks.nw_tos & IP_ECN_MASK) {
fd13c6b5 1716 ds_put_format(s, "%snw_ecn=%s%d,",
0deec6d2 1717 colors.param, colors.end, f->nw_tos & IP_ECN_MASK);
81a76618
BP
1718 }
1719 if (wc->masks.nw_ttl) {
fd13c6b5 1720 ds_put_format(s, "%snw_ttl=%s%d,",
0deec6d2 1721 colors.param, colors.end, f->nw_ttl);
81a76618 1722 }
8bfd0fda 1723 if (wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
0deec6d2
QM
1724 ds_put_format(s, "%smpls_label=%s%"PRIu32",", colors.param,
1725 colors.end, mpls_lse_to_label(f->mpls_lse[0]));
b02475c5 1726 }
8bfd0fda 1727 if (wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
0deec6d2 1728 ds_put_format(s, "%smpls_tc=%s%"PRIu8",", colors.param, colors.end,
78c9486d 1729 mpls_lse_to_tc(f->mpls_lse[0]));
b02475c5 1730 }
8bfd0fda 1731 if (wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK)) {
0deec6d2 1732 ds_put_format(s, "%smpls_ttl=%s%"PRIu8",", colors.param, colors.end,
78c9486d 1733 mpls_lse_to_ttl(f->mpls_lse[0]));
392c30ba 1734 }
8bfd0fda 1735 if (wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
0deec6d2 1736 ds_put_format(s, "%smpls_bos=%s%"PRIu8",", colors.param, colors.end,
78c9486d 1737 mpls_lse_to_bos(f->mpls_lse[0]));
b02475c5 1738 }
8bfd0fda
BP
1739 format_be32_masked(s, "mpls_lse1", f->mpls_lse[1], wc->masks.mpls_lse[1]);
1740 format_be32_masked(s, "mpls_lse2", f->mpls_lse[2], wc->masks.mpls_lse[2]);
1741
81a76618
BP
1742 switch (wc->masks.nw_frag) {
1743 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
0deec6d2 1744 ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
81a76618
BP
1745 f->nw_frag & FLOW_NW_FRAG_ANY
1746 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
1747 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
1748 break;
1749
1750 case FLOW_NW_FRAG_ANY:
0deec6d2 1751 ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
81a76618
BP
1752 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
1753 break;
1754
1755 case FLOW_NW_FRAG_LATER:
0deec6d2 1756 ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
81a76618
BP
1757 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
1758 break;
1759 }
3d4b2e6e 1760 if (dl_type == htons(ETH_TYPE_IP) &&
19564d20 1761 f->nw_proto == IPPROTO_ICMP) {
81a76618
BP
1762 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1763 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
3d4b2e6e 1764 } else if (dl_type == htons(ETH_TYPE_IP) &&
0e612675
FL
1765 f->nw_proto == IPPROTO_IGMP) {
1766 format_be16_masked(s, "igmp_type", f->tp_src, wc->masks.tp_src);
1767 format_be16_masked(s, "igmp_code", f->tp_dst, wc->masks.tp_dst);
3d4b2e6e 1768 } else if (dl_type == htons(ETH_TYPE_IPV6) &&
19564d20 1769 f->nw_proto == IPPROTO_ICMPV6) {
81a76618
BP
1770 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1771 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1772 format_ipv6_netmask(s, "nd_target", &f->nd_target,
1773 &wc->masks.nd_target);
1774 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
1775 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
9b2b8497
VDA
1776 if (wc->masks.igmp_group_ip4) {
1777 format_be32_masked(s,"nd_reserved", f->igmp_group_ip4,
1778 wc->masks.igmp_group_ip4);
1779 }
1780 if (wc->masks.tcp_flags) {
1781 format_be16_masked(s,"nd_options_type", f->tcp_flags,
1782 wc->masks.tcp_flags);
1783 }
1f6d8197 1784 } else {
81a76618
BP
1785 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
1786 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
1787 }
dc235f7f 1788 if (is_ip_any(f) && f->nw_proto == IPPROTO_TCP && wc->masks.tcp_flags) {
8e4c1621
JG
1789 format_flags_masked(s, "tcp_flags", packet_tcp_flag_to_string,
1790 ntohs(f->tcp_flags), TCP_FLAGS(wc->masks.tcp_flags),
1791 TCP_FLAGS(OVS_BE16_MAX));
dc235f7f 1792 }
81a76618 1793
41f562be
SH
1794 if (s->length > start_len) {
1795 ds_chomp(s, ',');
81a76618
BP
1796 }
1797}
1798
1799/* Converts 'match' to a string and returns the string. If 'priority' is
50f96b10
BP
1800 * different from OFP_DEFAULT_PRIORITY, includes it in the string. If
1801 * 'port_map' is nonnull, uses it to translate port numbers to names in
1802 * output. The caller must free the string (with free()). */
81a76618 1803char *
50f96b10
BP
1804match_to_string(const struct match *match,
1805 const struct ofputil_port_map *port_map, int priority)
81a76618
BP
1806{
1807 struct ds s = DS_EMPTY_INITIALIZER;
50f96b10 1808 match_format(match, port_map, &s, priority);
81a76618
BP
1809 return ds_steal_cstr(&s);
1810}
1811
1812void
50f96b10
BP
1813match_print(const struct match *match,
1814 const struct ofputil_port_map *port_map)
81a76618 1815{
50f96b10 1816 char *s = match_to_string(match, port_map, OFP_DEFAULT_PRIORITY);
81a76618
BP
1817 puts(s);
1818 free(s);
1819}
5cb7a798
BP
1820\f
1821/* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1822 * with minimatch_destroy(). */
1823void
1824minimatch_init(struct minimatch *dst, const struct match *src)
1825{
ceb3bd67
JR
1826 struct miniflow tmp;
1827
1828 miniflow_map_init(&tmp, &src->wc.masks);
1829 /* Allocate two consecutive miniflows. */
1830 miniflow_alloc(dst->flows, 2, &tmp);
1831 miniflow_init(dst->flow, &src->flow);
1832 minimask_init(dst->mask, &src->wc);
1f7270d7
BP
1833
1834 dst->tun_md = tun_metadata_allocation_clone(&src->tun_md);
5cb7a798
BP
1835}
1836
1dc1ec24
BP
1837/* Initializes 'match' as a "catch-all" match that matches every packet. */
1838void
1839minimatch_init_catchall(struct minimatch *match)
1840{
1841 match->flows[0] = xcalloc(2, sizeof *match->flow);
1842 match->flows[1] = match->flows[0] + 1;
1843 match->tun_md = NULL;
1844}
1845
5cb7a798
BP
1846/* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1847 * with minimatch_destroy(). */
1848void
1849minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1850{
ceb3bd67
JR
1851 /* Allocate two consecutive miniflows. */
1852 size_t data_size = miniflow_alloc(dst->flows, 2, &src->mask->masks);
1853
09b0fa9c
JR
1854 memcpy(miniflow_values(dst->flow),
1855 miniflow_get_values(src->flow), data_size);
1856 memcpy(miniflow_values(&dst->mask->masks),
1857 miniflow_get_values(&src->mask->masks), data_size);
1f7270d7 1858 dst->tun_md = tun_metadata_allocation_clone(src->tun_md);
5cb7a798
BP
1859}
1860
b2c1f00b
BP
1861/* Initializes 'dst' with the data in 'src', destroying 'src'. The caller must
1862 * eventually free 'dst' with minimatch_destroy(). */
1863void
1864minimatch_move(struct minimatch *dst, struct minimatch *src)
1865{
8fd47924
JR
1866 dst->flow = src->flow;
1867 dst->mask = src->mask;
1f7270d7 1868 dst->tun_md = src->tun_md;
b2c1f00b
BP
1869}
1870
5cb7a798
BP
1871/* Frees any memory owned by 'match'. Does not free the storage in which
1872 * 'match' itself resides; the caller is responsible for that. */
1873void
1874minimatch_destroy(struct minimatch *match)
1875{
8fd47924 1876 free(match->flow);
1f7270d7 1877 free(match->tun_md);
5cb7a798
BP
1878}
1879
1880/* Initializes 'dst' as a copy of 'src'. */
1881void
1882minimatch_expand(const struct minimatch *src, struct match *dst)
1883{
8fd47924
JR
1884 miniflow_expand(src->flow, &dst->flow);
1885 minimask_expand(src->mask, &dst->wc);
1f7270d7 1886 tun_metadata_allocation_copy(&dst->tun_md, src->tun_md);
5cb7a798
BP
1887}
1888
1889/* Returns true if 'a' and 'b' match the same packets, false otherwise. */
1890bool
1891minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1892{
8fd47924
JR
1893 return minimask_equal(a->mask, b->mask)
1894 && miniflow_equal(a->flow, b->flow);
5cb7a798
BP
1895}
1896
1dc1ec24
BP
1897/* Returns a hash value for the flow and wildcards in 'match', starting from
1898 * 'basis'. */
1899uint32_t
1900minimatch_hash(const struct minimatch *match, uint32_t basis)
1901{
1902 size_t n_values = miniflow_n_values(match->flow);
1903 size_t flow_size = sizeof *match->flow + MINIFLOW_VALUES_SIZE(n_values);
1904 return hash_bytes(match->flow, 2 * flow_size, basis);
1905}
1906
df40c152
BP
1907/* Returns true if 'target' satisifies 'match', that is, if each bit for which
1908 * 'match' specifies a particular value has the correct value in 'target'.
1909 *
1910 * This function is equivalent to miniflow_equal_flow_in_minimask(&match->flow,
1911 * target, &match->mask) but it is faster because of the invariant that
1912 * match->flow.map and match->mask.map are the same. */
1913bool
1914minimatch_matches_flow(const struct minimatch *match,
1915 const struct flow *target)
1916{
09b0fa9c
JR
1917 const uint64_t *flowp = miniflow_get_values(match->flow);
1918 const uint64_t *maskp = miniflow_get_values(&match->mask->masks);
361d808d 1919 size_t idx;
df40c152 1920
5fcff47b
JR
1921 FLOWMAP_FOR_EACH_INDEX(idx, match->flow->map) {
1922 if ((*flowp++ ^ flow_u64_value(target, idx)) & *maskp++) {
080e28d0 1923 return false;
df40c152 1924 }
df40c152
BP
1925 }
1926
1927 return true;
1928}
1929
5cb7a798 1930/* Appends a string representation of 'match' to 's'. If 'priority' is
50f96b10
BP
1931 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. If 'port_map' is
1932 * nonnull, uses it to translate port numbers to names in output. */
5cb7a798 1933void
8d8ab6c2 1934minimatch_format(const struct minimatch *match,
50f96b10
BP
1935 const struct tun_table *tun_table,
1936 const struct ofputil_port_map *port_map,
1937 struct ds *s, int priority)
5cb7a798
BP
1938{
1939 struct match megamatch;
1940
1941 minimatch_expand(match, &megamatch);
8d8ab6c2
JG
1942 megamatch.flow.tunnel.metadata.tab = tun_table;
1943
50f96b10 1944 match_format(&megamatch, port_map, s, priority);
5cb7a798
BP
1945}
1946
1947/* Converts 'match' to a string and returns the string. If 'priority' is
1948 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
50f96b10
BP
1949 * must free the string (with free()). If 'port_map' is nonnull, uses it to
1950 * translate port numbers to names in output. */
5cb7a798 1951char *
50f96b10
BP
1952minimatch_to_string(const struct minimatch *match,
1953 const struct ofputil_port_map *port_map, int priority)
5cb7a798
BP
1954{
1955 struct match megamatch;
1956
1957 minimatch_expand(match, &megamatch);
50f96b10 1958 return match_to_string(&megamatch, port_map, priority);
5cb7a798 1959}
1dc1ec24
BP
1960
1961static bool
1962minimatch_has_default_recirc_id(const struct minimatch *m)
1963{
1964 uint32_t flow_recirc_id = miniflow_get_recirc_id(m->flow);
1965 uint32_t mask_recirc_id = miniflow_get_recirc_id(&m->mask->masks);
1966 return flow_recirc_id == 0 && (mask_recirc_id == UINT32_MAX ||
1967 mask_recirc_id == 0);
1968}
1969
1970static bool
1971minimatch_has_default_dp_hash(const struct minimatch *m)
1972{
1973 return (!miniflow_get_dp_hash(m->flow)
1974 && !miniflow_get_dp_hash(&m->mask->masks));
1975}
1976
1977/* Return true if the hidden fields of the match are set to the default values.
1978 * The default values equals to those set up by match_init_hidden_fields(). */
1979bool
1980minimatch_has_default_hidden_fields(const struct minimatch *m)
1981{
1982 return (minimatch_has_default_recirc_id(m)
1983 && minimatch_has_default_dp_hash(m));
1984}