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