]> git.proxmox.com Git - mirror_ovs.git/blame - lib/match.c
netdev-dpdk: add hotplug support
[mirror_ovs.git] / lib / match.c
CommitLineData
81a76618 1/*
bef3f465 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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{
343 match->flow.ct_state = ct_state & mask & UINT16_MAX;
344 match->wc.masks.ct_state = mask & UINT16_MAX;
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
81a76618
BP
386void
387match_set_dl_type(struct match *match, ovs_be16 dl_type)
388{
b8266395 389 match->wc.masks.dl_type = OVS_BE16_MAX;
81a76618
BP
390 match->flow.dl_type = dl_type;
391}
392
393/* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
394 * exactly. 'mask_dst' is set to all 1s. */
395static void
74ff3298
JR
396set_eth(const struct eth_addr value_src,
397 struct eth_addr *value_dst,
398 struct eth_addr *mask_dst)
81a76618 399{
74ff3298
JR
400 *value_dst = value_src;
401 *mask_dst = eth_addr_exact;
81a76618
BP
402}
403
404/* Modifies 'value_src' so that the Ethernet address must match 'value_src'
405 * after each byte is ANDed with the appropriate byte in 'mask_src'.
406 * 'mask_dst' is set to 'mask_src' */
407static void
74ff3298
JR
408set_eth_masked(const struct eth_addr value_src,
409 const struct eth_addr mask_src,
410 struct eth_addr *value_dst, struct eth_addr *mask_dst)
81a76618
BP
411{
412 size_t i;
413
74ff3298
JR
414 for (i = 0; i < ARRAY_SIZE(value_dst->be16); i++) {
415 value_dst->be16[i] = value_src.be16[i] & mask_src.be16[i];
81a76618 416 }
74ff3298 417 *mask_dst = mask_src;
81a76618
BP
418}
419
420/* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
421 * exactly. */
422void
74ff3298 423match_set_dl_src(struct match *match, const struct eth_addr dl_src)
81a76618 424{
74ff3298 425 set_eth(dl_src, &match->flow.dl_src, &match->wc.masks.dl_src);
81a76618
BP
426}
427
428/* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
429 * after each byte is ANDed with the appropriate byte in 'mask'. */
430void
431match_set_dl_src_masked(struct match *match,
74ff3298
JR
432 const struct eth_addr dl_src,
433 const struct eth_addr mask)
81a76618 434{
74ff3298 435 set_eth_masked(dl_src, mask, &match->flow.dl_src, &match->wc.masks.dl_src);
81a76618
BP
436}
437
438/* Modifies 'match' so that the Ethernet address must match 'dl_dst'
439 * exactly. */
440void
74ff3298 441match_set_dl_dst(struct match *match, const struct eth_addr dl_dst)
81a76618 442{
74ff3298 443 set_eth(dl_dst, &match->flow.dl_dst, &match->wc.masks.dl_dst);
81a76618
BP
444}
445
446/* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
447 * byte is ANDed with the appropriate byte in 'mask'.
448 *
449 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
450 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
451void
452match_set_dl_dst_masked(struct match *match,
74ff3298
JR
453 const struct eth_addr dl_dst,
454 const struct eth_addr mask)
81a76618 455{
74ff3298 456 set_eth_masked(dl_dst, mask, &match->flow.dl_dst, &match->wc.masks.dl_dst);
81a76618
BP
457}
458
459void
460match_set_dl_tci(struct match *match, ovs_be16 tci)
461{
462 match_set_dl_tci_masked(match, tci, htons(0xffff));
463}
464
465void
466match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
467{
468 match->flow.vlan_tci = tci & mask;
469 match->wc.masks.vlan_tci = mask;
470}
471
472/* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
473 * wildcarded, then 'match' will match a packet regardless of whether it has an
474 * 802.1Q header or not. */
475void
476match_set_any_vid(struct match *match)
477{
478 if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
479 match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
480 match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
481 } else {
482 match_set_dl_tci_masked(match, htons(0), htons(0));
483 }
484}
485
486/* Modifies 'match' depending on 'dl_vlan':
487 *
488 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
489 * without an 802.1Q header.
490 *
491 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
492 * VID equals the low 12 bits of 'dl_vlan'.
493 */
494void
495match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
496{
497 flow_set_dl_vlan(&match->flow, dl_vlan);
498 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
b8266395 499 match->wc.masks.vlan_tci = OVS_BE16_MAX;
81a76618
BP
500 } else {
501 match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
502 }
503}
504
505/* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
506 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
507 * plus CFI). */
508void
509match_set_vlan_vid(struct match *match, ovs_be16 vid)
510{
511 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
512}
513
514
515/* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
516 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
517 * plus CFI), with the corresponding 'mask'. */
518void
519match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
520{
521 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
522 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
523
524 mask &= vid_mask;
525 flow_set_vlan_vid(&match->flow, vid & mask);
526 match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
527}
528
529/* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
530 * wildcarded, then 'match' will match a packet regardless of whether it has an
531 * 802.1Q header or not. */
532void
533match_set_any_pcp(struct match *match)
534{
535 if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
536 match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
537 match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
538 } else {
539 match_set_dl_tci_masked(match, htons(0), htons(0));
540 }
541}
542
543/* Modifies 'match' so that it matches only packets with an 802.1Q header whose
544 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
545void
546match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
547{
548 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
549 match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
550}
551
8bfd0fda
BP
552/* Modifies 'match' so that the MPLS label 'idx' matches 'lse' exactly. */
553void
554match_set_mpls_lse(struct match *match, int idx, ovs_be32 lse)
555{
556 match->wc.masks.mpls_lse[idx] = OVS_BE32_MAX;
557 match->flow.mpls_lse[idx] = lse;
558}
559
b02475c5
SH
560/* Modifies 'match' so that the MPLS label is wildcarded. */
561void
8bfd0fda 562match_set_any_mpls_label(struct match *match, int idx)
b02475c5 563{
8bfd0fda
BP
564 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_LABEL_MASK);
565 flow_set_mpls_label(&match->flow, idx, htonl(0));
b02475c5
SH
566}
567
568/* Modifies 'match' so that it matches only packets with an MPLS header whose
569 * label equals the low 20 bits of 'mpls_label'. */
570void
8bfd0fda 571match_set_mpls_label(struct match *match, int idx, ovs_be32 mpls_label)
b02475c5 572{
8bfd0fda
BP
573 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_LABEL_MASK);
574 flow_set_mpls_label(&match->flow, idx, mpls_label);
b02475c5
SH
575}
576
577/* Modifies 'match' so that the MPLS TC is wildcarded. */
578void
8bfd0fda 579match_set_any_mpls_tc(struct match *match, int idx)
b02475c5 580{
8bfd0fda
BP
581 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TC_MASK);
582 flow_set_mpls_tc(&match->flow, idx, 0);
b02475c5
SH
583}
584
585/* Modifies 'match' so that it matches only packets with an MPLS header whose
586 * Traffic Class equals the low 3 bits of 'mpls_tc'. */
587void
8bfd0fda 588match_set_mpls_tc(struct match *match, int idx, uint8_t mpls_tc)
b02475c5 589{
8bfd0fda
BP
590 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TC_MASK);
591 flow_set_mpls_tc(&match->flow, idx, mpls_tc);
b02475c5
SH
592}
593
594/* Modifies 'match' so that the MPLS stack flag is wildcarded. */
595void
8bfd0fda 596match_set_any_mpls_bos(struct match *match, int idx)
b02475c5 597{
8bfd0fda
BP
598 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_BOS_MASK);
599 flow_set_mpls_bos(&match->flow, idx, 0);
b02475c5
SH
600}
601
602/* Modifies 'match' so that it matches only packets with an MPLS header whose
603 * Stack Flag equals the lower bit of 'mpls_bos' */
604void
8bfd0fda
BP
605match_set_mpls_bos(struct match *match, int idx, uint8_t mpls_bos)
606{
607 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_BOS_MASK);
608 flow_set_mpls_bos(&match->flow, idx, mpls_bos);
609}
610
bef3f465
BP
611/* Modifies 'match' so that the TTL of MPLS label 'idx' is wildcarded. */
612void
613match_set_any_mpls_ttl(struct match *match, int idx)
614{
615 match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TTL_MASK);
616 flow_set_mpls_ttl(&match->flow, idx, 0);
617}
618
619/* Modifies 'match' so that it matches only packets in which the TTL of MPLS
620 * label 'idx' equals 'mpls_ttl'. */
621void
622match_set_mpls_ttl(struct match *match, int idx, uint8_t mpls_ttl)
623{
624 match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TTL_MASK);
625 flow_set_mpls_ttl(&match->flow, idx, mpls_ttl);
626}
627
8bfd0fda
BP
628/* Modifies 'match' so that the MPLS LSE is wildcarded. */
629void
630match_set_any_mpls_lse(struct match *match, int idx)
b02475c5 631{
8bfd0fda
BP
632 match->wc.masks.mpls_lse[idx] = htonl(0);
633 flow_set_mpls_lse(&match->flow, idx, htonl(0));
b02475c5
SH
634}
635
81a76618
BP
636void
637match_set_tp_src(struct match *match, ovs_be16 tp_src)
638{
b8266395 639 match_set_tp_src_masked(match, tp_src, OVS_BE16_MAX);
81a76618
BP
640}
641
642void
643match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
644{
645 match->flow.tp_src = port & mask;
646 match->wc.masks.tp_src = mask;
647}
648
649void
650match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
651{
b8266395 652 match_set_tp_dst_masked(match, tp_dst, OVS_BE16_MAX);
81a76618
BP
653}
654
655void
656match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
657{
658 match->flow.tp_dst = port & mask;
659 match->wc.masks.tp_dst = mask;
660}
661
dc235f7f
JR
662void
663match_set_tcp_flags(struct match *match, ovs_be16 flags)
664{
665 match_set_tcp_flags_masked(match, flags, OVS_BE16_MAX);
666}
667
668void
669match_set_tcp_flags_masked(struct match *match, ovs_be16 flags, ovs_be16 mask)
670{
671 match->flow.tcp_flags = flags & mask;
672 match->wc.masks.tcp_flags = mask;
673}
674
81a76618
BP
675void
676match_set_nw_proto(struct match *match, uint8_t nw_proto)
677{
678 match->flow.nw_proto = nw_proto;
679 match->wc.masks.nw_proto = UINT8_MAX;
680}
681
682void
683match_set_nw_src(struct match *match, ovs_be32 nw_src)
684{
685 match->flow.nw_src = nw_src;
b8266395 686 match->wc.masks.nw_src = OVS_BE32_MAX;
81a76618
BP
687}
688
689void
690match_set_nw_src_masked(struct match *match,
691 ovs_be32 nw_src, ovs_be32 mask)
692{
693 match->flow.nw_src = nw_src & mask;
694 match->wc.masks.nw_src = mask;
695}
696
697void
698match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
699{
700 match->flow.nw_dst = nw_dst;
b8266395 701 match->wc.masks.nw_dst = OVS_BE32_MAX;
81a76618
BP
702}
703
704void
705match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
706{
707 match->flow.nw_dst = ip & mask;
708 match->wc.masks.nw_dst = mask;
709}
710
711void
712match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
713{
714 match->wc.masks.nw_tos |= IP_DSCP_MASK;
715 match->flow.nw_tos &= ~IP_DSCP_MASK;
716 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
717}
718
719void
720match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
721{
722 match->wc.masks.nw_tos |= IP_ECN_MASK;
723 match->flow.nw_tos &= ~IP_ECN_MASK;
724 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
725}
726
727void
728match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
729{
730 match->wc.masks.nw_ttl = UINT8_MAX;
731 match->flow.nw_ttl = nw_ttl;
732}
733
734void
735match_set_nw_frag(struct match *match, uint8_t nw_frag)
736{
737 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
738 match->flow.nw_frag = nw_frag;
739}
740
741void
742match_set_nw_frag_masked(struct match *match,
743 uint8_t nw_frag, uint8_t mask)
744{
745 match->flow.nw_frag = nw_frag & mask;
746 match->wc.masks.nw_frag = mask;
747}
748
749void
750match_set_icmp_type(struct match *match, uint8_t icmp_type)
751{
752 match_set_tp_src(match, htons(icmp_type));
753}
754
755void
756match_set_icmp_code(struct match *match, uint8_t icmp_code)
757{
758 match_set_tp_dst(match, htons(icmp_code));
759}
760
761void
74ff3298 762match_set_arp_sha(struct match *match, const struct eth_addr sha)
81a76618 763{
74ff3298
JR
764 match->flow.arp_sha = sha;
765 match->wc.masks.arp_sha = eth_addr_exact;
81a76618
BP
766}
767
768void
769match_set_arp_sha_masked(struct match *match,
74ff3298
JR
770 const struct eth_addr arp_sha,
771 const struct eth_addr mask)
81a76618
BP
772{
773 set_eth_masked(arp_sha, mask,
74ff3298 774 &match->flow.arp_sha, &match->wc.masks.arp_sha);
81a76618
BP
775}
776
777void
74ff3298 778match_set_arp_tha(struct match *match, const struct eth_addr tha)
81a76618 779{
74ff3298
JR
780 match->flow.arp_tha = tha;
781 match->wc.masks.arp_tha = eth_addr_exact;
81a76618
BP
782}
783
784void
785match_set_arp_tha_masked(struct match *match,
74ff3298
JR
786 const struct eth_addr arp_tha,
787 const struct eth_addr mask)
81a76618
BP
788{
789 set_eth_masked(arp_tha, mask,
74ff3298 790 &match->flow.arp_tha, &match->wc.masks.arp_tha);
81a76618
BP
791}
792
793void
794match_set_ipv6_src(struct match *match, const struct in6_addr *src)
795{
796 match->flow.ipv6_src = *src;
797 match->wc.masks.ipv6_src = in6addr_exact;
798}
799
800void
801match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
802 const struct in6_addr *mask)
803{
804 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
805 match->wc.masks.ipv6_src = *mask;
806}
807
808void
809match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
810{
811 match->flow.ipv6_dst = *dst;
812 match->wc.masks.ipv6_dst = in6addr_exact;
813}
814
815void
816match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
817 const struct in6_addr *mask)
818{
819 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
820 match->wc.masks.ipv6_dst = *mask;
821}
822
823void
824match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
825{
b8266395 826 match->wc.masks.ipv6_label = OVS_BE32_MAX;
81a76618
BP
827 match->flow.ipv6_label = ipv6_label;
828}
829
830
831void
832match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
833 ovs_be32 mask)
834{
835 match->flow.ipv6_label = ipv6_label & mask;
836 match->wc.masks.ipv6_label = mask;
837}
838
839void
840match_set_nd_target(struct match *match, const struct in6_addr *target)
841{
842 match->flow.nd_target = *target;
843 match->wc.masks.nd_target = in6addr_exact;
844}
845
846void
847match_set_nd_target_masked(struct match *match,
848 const struct in6_addr *target,
849 const struct in6_addr *mask)
850{
851 match->flow.nd_target = ipv6_addr_bitand(target, mask);
852 match->wc.masks.nd_target = *mask;
853}
854
855/* Returns true if 'a' and 'b' wildcard the same fields and have the same
856 * values for fixed fields, otherwise false. */
857bool
858match_equal(const struct match *a, const struct match *b)
859{
860 return (flow_wildcards_equal(&a->wc, &b->wc)
861 && flow_equal(&a->flow, &b->flow));
862}
863
864/* Returns a hash value for the flow and wildcards in 'match', starting from
865 * 'basis'. */
866uint32_t
867match_hash(const struct match *match, uint32_t basis)
868{
869 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
870}
871
adcf00ba
AZ
872static bool
873match_has_default_recirc_id(const struct match *m)
874{
875 return m->flow.recirc_id == 0 && (m->wc.masks.recirc_id == UINT32_MAX ||
876 m->wc.masks.recirc_id == 0);
877}
878
879static bool
880match_has_default_dp_hash(const struct match *m)
881{
882 return ((m->flow.dp_hash | m->wc.masks.dp_hash) == 0);
883}
884
885/* Return true if the hidden fields of the match are set to the default values.
886 * The default values equals to those set up by match_init_hidden_fields(). */
887bool
888match_has_default_hidden_fields(const struct match *m)
889{
890 return match_has_default_recirc_id(m) && match_has_default_dp_hash(m);
891}
892
893void
894match_init_hidden_fields(struct match *m)
895{
896 match_set_recirc_id(m, 0);
897 match_set_dp_hash_masked(m, 0, 0);
898}
899
81a76618 900static void
3bd0fd39 901format_eth_masked(struct ds *s, const char *name,
74ff3298 902 const struct eth_addr eth, const struct eth_addr mask)
81a76618
BP
903{
904 if (!eth_addr_is_zero(mask)) {
0deec6d2 905 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
74ff3298 906 eth_format_masked(eth, &mask, s);
81a76618
BP
907 ds_put_char(s, ',');
908 }
909}
910
911static void
912format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
913 ovs_be32 netmask)
914{
915 if (netmask) {
0deec6d2 916 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
81a76618
BP
917 ip_format_masked(ip, netmask, s);
918 ds_put_char(s, ',');
919 }
920}
921
922static void
923format_ipv6_netmask(struct ds *s, const char *name,
924 const struct in6_addr *addr,
925 const struct in6_addr *netmask)
926{
927 if (!ipv6_mask_is_any(netmask)) {
0deec6d2 928 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
ac6d120f 929 ipv6_format_masked(addr, netmask, s);
81a76618
BP
930 ds_put_char(s, ',');
931 }
932}
933
07659514
JS
934static void
935format_uint16_masked(struct ds *s, const char *name,
936 uint16_t value, uint16_t mask)
937{
938 if (mask != 0) {
0deec6d2 939 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
07659514
JS
940 if (mask == UINT16_MAX) {
941 ds_put_format(s, "%"PRIu16, value);
942 } else {
943 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16, value, mask);
944 }
945 ds_put_char(s, ',');
946 }
947}
948
81a76618
BP
949static void
950format_be16_masked(struct ds *s, const char *name,
951 ovs_be16 value, ovs_be16 mask)
952{
953 if (mask != htons(0)) {
0deec6d2 954 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
b8266395 955 if (mask == OVS_BE16_MAX) {
81a76618
BP
956 ds_put_format(s, "%"PRIu16, ntohs(value));
957 } else {
958 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
959 ntohs(value), ntohs(mask));
960 }
961 ds_put_char(s, ',');
962 }
963}
964
8bfd0fda
BP
965static void
966format_be32_masked(struct ds *s, const char *name,
967 ovs_be32 value, ovs_be32 mask)
968{
969 if (mask != htonl(0)) {
0deec6d2 970 ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
8bfd0fda
BP
971 if (mask == OVS_BE32_MAX) {
972 ds_put_format(s, "%"PRIu32, ntohl(value));
973 } else {
974 ds_put_format(s, "0x%"PRIx32"/0x%"PRIx32,
975 ntohl(value), ntohl(mask));
976 }
977 ds_put_char(s, ',');
978 }
979}
980
b60f804e
AZ
981static void
982format_uint32_masked(struct ds *s, const char *name,
983 uint32_t value, uint32_t mask)
984{
985 if (mask) {
0deec6d2
QM
986 ds_put_format(s, "%s%s=%s%#"PRIx32,
987 colors.param, name, colors.end, value);
b60f804e
AZ
988 if (mask != UINT32_MAX) {
989 ds_put_format(s, "/%#"PRIx32, mask);
990 }
991 ds_put_char(s, ',');
992 }
993}
994
995static void
996format_be64_masked(struct ds *s, const char *name,
997 ovs_be64 value, ovs_be64 mask)
998{
999 if (mask != htonll(0)) {
0deec6d2
QM
1000 ds_put_format(s, "%s%s=%s%#"PRIx64,
1001 colors.param, name, colors.end, ntohll(value));
b60f804e
AZ
1002 if (mask != OVS_BE64_MAX) {
1003 ds_put_format(s, "/%#"PRIx64, ntohll(mask));
1004 }
1005 ds_put_char(s, ',');
1006 }
1007}
1008
4fe3445a
PS
1009static void
1010format_flow_tunnel(struct ds *s, const struct match *match)
1011{
1012 const struct flow_wildcards *wc = &match->wc;
1013 const struct flow_tnl *tnl = &match->flow.tunnel;
1014
b60f804e 1015 format_be64_masked(s, "tun_id", tnl->tun_id, wc->masks.tunnel.tun_id);
4fe3445a
PS
1016 format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
1017 format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
ffe4c74f
JB
1018 format_ipv6_netmask(s, "tun_ipv6_src", &tnl->ipv6_src,
1019 &wc->masks.tunnel.ipv6_src);
1020 format_ipv6_netmask(s, "tun_ipv6_dst", &tnl->ipv6_dst,
1021 &wc->masks.tunnel.ipv6_dst);
4fe3445a 1022
ac6073e3
MC
1023 if (wc->masks.tunnel.gbp_id) {
1024 format_be16_masked(s, "tun_gbp_id", tnl->gbp_id,
1025 wc->masks.tunnel.gbp_id);
1026 }
1027
1028 if (wc->masks.tunnel.gbp_flags) {
1029 ds_put_format(s, "tun_gbp_flags=%#"PRIx8",", tnl->gbp_flags);
1030 }
1031
4fe3445a
PS
1032 if (wc->masks.tunnel.ip_tos) {
1033 ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
1034 }
1035 if (wc->masks.tunnel.ip_ttl) {
1036 ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
1037 }
3721b406 1038 if (wc->masks.tunnel.flags & FLOW_TNL_F_MASK) {
b666962b 1039 format_flags_masked(s, "tun_flags", flow_tun_flag_to_string,
3721b406 1040 tnl->flags & FLOW_TNL_F_MASK,
b666962b
JG
1041 wc->masks.tunnel.flags & FLOW_TNL_F_MASK,
1042 FLOW_TNL_F_MASK);
4fe3445a
PS
1043 ds_put_char(s, ',');
1044 }
9558d2a5 1045 tun_metadata_match_format(s, match);
4fe3445a
PS
1046}
1047
9daf2348
JS
1048static void
1049format_ct_label_masked(struct ds *s, const ovs_u128 *key, const ovs_u128 *mask)
1050{
2ff8484b 1051 if (!ovs_u128_is_zero(*mask)) {
32ea15f6 1052 ovs_be128 value = hton128(*key);
0deec6d2 1053 ds_put_format(s, "%sct_label=%s", colors.param, colors.end);
9daf2348
JS
1054 ds_put_hex(s, &value, sizeof value);
1055 if (!is_all_ones(mask, sizeof(*mask))) {
32ea15f6 1056 value = hton128(*mask);
9daf2348
JS
1057 ds_put_char(s, '/');
1058 ds_put_hex(s, &value, sizeof value);
1059 }
1060 ds_put_char(s, ',');
1061 }
1062}
1063
81a76618
BP
1064/* Appends a string representation of 'match' to 's'. If 'priority' is
1065 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1066void
eb391b76 1067match_format(const struct match *match, struct ds *s, int priority)
81a76618
BP
1068{
1069 const struct flow_wildcards *wc = &match->wc;
1070 size_t start_len = s->length;
1071 const struct flow *f = &match->flow;
1072 bool skip_type = false;
0deec6d2 1073
81a76618
BP
1074 bool skip_proto = false;
1075
1076 int i;
1077
847b8b02 1078 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 36);
81a76618
BP
1079
1080 if (priority != OFP_DEFAULT_PRIORITY) {
0deec6d2
QM
1081 ds_put_format(s, "%spriority=%s%d,",
1082 colors.special, colors.end, priority);
81a76618
BP
1083 }
1084
b60f804e 1085 format_uint32_masked(s, "pkt_mark", f->pkt_mark, wc->masks.pkt_mark);
1b567fb9 1086
a79f29f2
AZ
1087 if (wc->masks.recirc_id) {
1088 format_uint32_masked(s, "recirc_id", f->recirc_id,
1089 wc->masks.recirc_id);
1090 }
1091
330de069 1092 if (wc->masks.dp_hash) {
a79f29f2
AZ
1093 format_uint32_masked(s, "dp_hash", f->dp_hash,
1094 wc->masks.dp_hash);
1095 }
1096
18080541 1097 if (wc->masks.conj_id) {
0deec6d2
QM
1098 ds_put_format(s, "%sconj_id%s=%"PRIu32",",
1099 colors.param, colors.end, f->conj_id);
18080541
BP
1100 }
1101
1b567fb9 1102 if (wc->masks.skb_priority) {
0deec6d2
QM
1103 ds_put_format(s, "%sskb_priority=%s%#"PRIx32",",
1104 colors.param, colors.end, f->skb_priority);
1b567fb9
AA
1105 }
1106
c61f3870 1107 if (wc->masks.actset_output) {
0deec6d2 1108 ds_put_format(s, "%sactset_output=%s", colors.param, colors.end);
c61f3870
BP
1109 ofputil_format_port(f->actset_output, s);
1110 ds_put_char(s, ',');
1111 }
1112
07659514
JS
1113 if (wc->masks.ct_state) {
1114 if (wc->masks.ct_state == UINT16_MAX) {
0deec6d2 1115 ds_put_format(s, "%sct_state=%s", colors.param, colors.end);
07659514
JS
1116 if (f->ct_state) {
1117 format_flags(s, ct_state_to_string, f->ct_state, '|');
1118 } else {
1119 ds_put_cstr(s, "0"); /* No state. */
1120 }
1121 } else {
1122 format_flags_masked(s, "ct_state", ct_state_to_string,
1123 f->ct_state, wc->masks.ct_state, UINT16_MAX);
1124 }
1125 ds_put_char(s, ',');
1126 }
1127
1128 if (wc->masks.ct_zone) {
1129 format_uint16_masked(s, "ct_zone", f->ct_zone, wc->masks.ct_zone);
1130 }
1131
8e53fe8c
JS
1132 if (wc->masks.ct_mark) {
1133 format_uint32_masked(s, "ct_mark", f->ct_mark, wc->masks.ct_mark);
1134 }
1135
2ff8484b 1136 if (!ovs_u128_is_zero(wc->masks.ct_label)) {
9daf2348
JS
1137 format_ct_label_masked(s, &f->ct_label, &wc->masks.ct_label);
1138 }
1139
81a76618
BP
1140 if (wc->masks.dl_type) {
1141 skip_type = true;
1142 if (f->dl_type == htons(ETH_TYPE_IP)) {
1143 if (wc->masks.nw_proto) {
1144 skip_proto = true;
1145 if (f->nw_proto == IPPROTO_ICMP) {
0deec6d2 1146 ds_put_format(s, "%sicmp%s,", colors.value, colors.end);
0e612675 1147 } else if (f->nw_proto == IPPROTO_IGMP) {
0deec6d2 1148 ds_put_format(s, "%sigmp%s,", colors.value, colors.end);
81a76618 1149 } else if (f->nw_proto == IPPROTO_TCP) {
0deec6d2 1150 ds_put_format(s, "%stcp%s,", colors.value, colors.end);
81a76618 1151 } else if (f->nw_proto == IPPROTO_UDP) {
0deec6d2 1152 ds_put_format(s, "%sudp%s,", colors.value, colors.end);
0d56eaf2 1153 } else if (f->nw_proto == IPPROTO_SCTP) {
0deec6d2 1154 ds_put_format(s, "%ssctp%s,", colors.value, colors.end);
81a76618 1155 } else {
0deec6d2 1156 ds_put_format(s, "%sip%s,", colors.value, colors.end);
81a76618
BP
1157 skip_proto = false;
1158 }
1159 } else {
0deec6d2 1160 ds_put_format(s, "%sip%s,", colors.value, colors.end);
81a76618
BP
1161 }
1162 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
1163 if (wc->masks.nw_proto) {
1164 skip_proto = true;
1165 if (f->nw_proto == IPPROTO_ICMPV6) {
0deec6d2 1166 ds_put_format(s, "%sicmp6%s,", colors.value, colors.end);
81a76618 1167 } else if (f->nw_proto == IPPROTO_TCP) {
0deec6d2 1168 ds_put_format(s, "%stcp6%s,", colors.value, colors.end);
81a76618 1169 } else if (f->nw_proto == IPPROTO_UDP) {
0deec6d2 1170 ds_put_format(s, "%sudp6%s,", colors.value, colors.end);
0d56eaf2 1171 } else if (f->nw_proto == IPPROTO_SCTP) {
0deec6d2 1172 ds_put_format(s, "%ssctp6%s,", colors.value, colors.end);
81a76618 1173 } else {
0deec6d2 1174 ds_put_format(s, "%sipv6%s,", colors.value, colors.end);
81a76618
BP
1175 skip_proto = false;
1176 }
1177 } else {
0deec6d2 1178 ds_put_format(s, "%sipv6%s,", colors.value, colors.end);
81a76618
BP
1179 }
1180 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
0deec6d2 1181 ds_put_format(s, "%sarp%s,", colors.value, colors.end);
8087f5ff 1182 } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
0deec6d2 1183 ds_put_format(s, "%srarp%s,", colors.value, colors.end);
392c30ba 1184 } else if (f->dl_type == htons(ETH_TYPE_MPLS)) {
0deec6d2 1185 ds_put_format(s, "%smpls%s,", colors.value, colors.end);
392c30ba 1186 } else if (f->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
0deec6d2 1187 ds_put_format(s, "%smplsm%s,", colors.value, colors.end);
81a76618
BP
1188 } else {
1189 skip_type = false;
1190 }
1191 }
1192 for (i = 0; i < FLOW_N_REGS; i++) {
b60f804e
AZ
1193 #define REGNAME_LEN 20
1194 char regname[REGNAME_LEN];
1195 if (snprintf(regname, REGNAME_LEN, "reg%d", i) >= REGNAME_LEN) {
1196 strcpy(regname, "reg?");
81a76618 1197 }
b60f804e 1198 format_uint32_masked(s, regname, f->regs[i], wc->masks.regs[i]);
81a76618 1199 }
4fe3445a
PS
1200
1201 format_flow_tunnel(s, match);
1202
b60f804e
AZ
1203 format_be64_masked(s, "metadata", f->metadata, wc->masks.metadata);
1204
4e022ec0 1205 if (wc->masks.in_port.ofp_port) {
0deec6d2 1206 ds_put_format(s, "%sin_port=%s", colors.param, colors.end);
4e022ec0 1207 ofputil_format_port(f->in_port.ofp_port, s);
576ec803 1208 ds_put_char(s, ',');
81a76618
BP
1209 }
1210 if (wc->masks.vlan_tci) {
1211 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
1212 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
1213 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
1214
1215 if (cfi && f->vlan_tci & htons(VLAN_CFI)
1216 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
1217 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
1218 && (vid_mask || pcp_mask)) {
1219 if (vid_mask) {
0deec6d2
QM
1220 ds_put_format(s, "%sdl_vlan=%s%"PRIu16",", colors.param,
1221 colors.end, vlan_tci_to_vid(f->vlan_tci));
81a76618
BP
1222 }
1223 if (pcp_mask) {
0deec6d2
QM
1224 ds_put_format(s, "%sdl_vlan_pcp=%s%d,", colors.param,
1225 colors.end, vlan_tci_to_pcp(f->vlan_tci));
81a76618
BP
1226 }
1227 } else if (wc->masks.vlan_tci == htons(0xffff)) {
0deec6d2
QM
1228 ds_put_format(s, "%svlan_tci=%s0x%04"PRIx16",", colors.param,
1229 colors.end, ntohs(f->vlan_tci));
81a76618 1230 } else {
0deec6d2
QM
1231 ds_put_format(s, "%svlan_tci=%s0x%04"PRIx16"/0x%04"PRIx16",",
1232 colors.param, colors.end,
81a76618
BP
1233 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
1234 }
1235 }
1236 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
1237 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
1238 if (!skip_type && wc->masks.dl_type) {
0deec6d2
QM
1239 ds_put_format(s, "%sdl_type=%s0x%04"PRIx16",",
1240 colors.param, colors.end, ntohs(f->dl_type));
81a76618
BP
1241 }
1242 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
1243 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
1244 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
1245 if (wc->masks.ipv6_label) {
b8266395 1246 if (wc->masks.ipv6_label == OVS_BE32_MAX) {
0deec6d2
QM
1247 ds_put_format(s, "%sipv6_label=%s0x%05"PRIx32",",
1248 colors.param, colors.end,
81a76618
BP
1249 ntohl(f->ipv6_label));
1250 } else {
0deec6d2
QM
1251 ds_put_format(s, "%sipv6_label=%s0x%05"PRIx32"/0x%05"PRIx32",",
1252 colors.param, colors.end, ntohl(f->ipv6_label),
81a76618
BP
1253 ntohl(wc->masks.ipv6_label));
1254 }
1255 }
8087f5ff
MM
1256 } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
1257 f->dl_type == htons(ETH_TYPE_RARP)) {
666d0863
MM
1258 format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
1259 format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
81a76618
BP
1260 } else {
1261 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
1262 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
1263 }
1264 if (!skip_proto && wc->masks.nw_proto) {
8087f5ff
MM
1265 if (f->dl_type == htons(ETH_TYPE_ARP) ||
1266 f->dl_type == htons(ETH_TYPE_RARP)) {
0deec6d2
QM
1267 ds_put_format(s, "%sarp_op=%s%"PRIu8",",
1268 colors.param, colors.end, f->nw_proto);
81a76618 1269 } else {
0deec6d2
QM
1270 ds_put_format(s, "%snw_proto=%s%"PRIu8",",
1271 colors.param, colors.end, f->nw_proto);
81a76618
BP
1272 }
1273 }
8087f5ff
MM
1274 if (f->dl_type == htons(ETH_TYPE_ARP) ||
1275 f->dl_type == htons(ETH_TYPE_RARP)) {
81a76618
BP
1276 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
1277 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
1278 }
1279 if (wc->masks.nw_tos & IP_DSCP_MASK) {
0deec6d2
QM
1280 ds_put_format(s, "%snw_tos=%s%"PRIu8",",
1281 colors.param, colors.end, f->nw_tos & IP_DSCP_MASK);
81a76618
BP
1282 }
1283 if (wc->masks.nw_tos & IP_ECN_MASK) {
0deec6d2
QM
1284 ds_put_format(s, "%snw_ecn=%s%"PRIu8",",
1285 colors.param, colors.end, f->nw_tos & IP_ECN_MASK);
81a76618
BP
1286 }
1287 if (wc->masks.nw_ttl) {
0deec6d2
QM
1288 ds_put_format(s, "%snw_ttl=%s%"PRIu8",",
1289 colors.param, colors.end, f->nw_ttl);
81a76618 1290 }
8bfd0fda 1291 if (wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
0deec6d2
QM
1292 ds_put_format(s, "%smpls_label=%s%"PRIu32",", colors.param,
1293 colors.end, mpls_lse_to_label(f->mpls_lse[0]));
b02475c5 1294 }
8bfd0fda 1295 if (wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
0deec6d2 1296 ds_put_format(s, "%smpls_tc=%s%"PRIu8",", colors.param, colors.end,
78c9486d 1297 mpls_lse_to_tc(f->mpls_lse[0]));
b02475c5 1298 }
8bfd0fda 1299 if (wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK)) {
0deec6d2 1300 ds_put_format(s, "%smpls_ttl=%s%"PRIu8",", colors.param, colors.end,
78c9486d 1301 mpls_lse_to_ttl(f->mpls_lse[0]));
392c30ba 1302 }
8bfd0fda 1303 if (wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
0deec6d2 1304 ds_put_format(s, "%smpls_bos=%s%"PRIu8",", colors.param, colors.end,
78c9486d 1305 mpls_lse_to_bos(f->mpls_lse[0]));
b02475c5 1306 }
8bfd0fda
BP
1307 format_be32_masked(s, "mpls_lse1", f->mpls_lse[1], wc->masks.mpls_lse[1]);
1308 format_be32_masked(s, "mpls_lse2", f->mpls_lse[2], wc->masks.mpls_lse[2]);
1309
81a76618
BP
1310 switch (wc->masks.nw_frag) {
1311 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
0deec6d2 1312 ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
81a76618
BP
1313 f->nw_frag & FLOW_NW_FRAG_ANY
1314 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
1315 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
1316 break;
1317
1318 case FLOW_NW_FRAG_ANY:
0deec6d2 1319 ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
81a76618
BP
1320 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
1321 break;
1322
1323 case FLOW_NW_FRAG_LATER:
0deec6d2 1324 ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
81a76618
BP
1325 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
1326 break;
1327 }
19564d20
MM
1328 if (f->dl_type == htons(ETH_TYPE_IP) &&
1329 f->nw_proto == IPPROTO_ICMP) {
81a76618
BP
1330 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1331 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
0e612675
FL
1332 } else if (f->dl_type == htons(ETH_TYPE_IP) &&
1333 f->nw_proto == IPPROTO_IGMP) {
1334 format_be16_masked(s, "igmp_type", f->tp_src, wc->masks.tp_src);
1335 format_be16_masked(s, "igmp_code", f->tp_dst, wc->masks.tp_dst);
19564d20
MM
1336 } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
1337 f->nw_proto == IPPROTO_ICMPV6) {
81a76618
BP
1338 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1339 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1340 format_ipv6_netmask(s, "nd_target", &f->nd_target,
1341 &wc->masks.nd_target);
1342 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
1343 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
1f6d8197 1344 } else {
81a76618
BP
1345 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
1346 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
1347 }
dc235f7f 1348 if (is_ip_any(f) && f->nw_proto == IPPROTO_TCP && wc->masks.tcp_flags) {
8e4c1621
JG
1349 format_flags_masked(s, "tcp_flags", packet_tcp_flag_to_string,
1350 ntohs(f->tcp_flags), TCP_FLAGS(wc->masks.tcp_flags),
1351 TCP_FLAGS(OVS_BE16_MAX));
dc235f7f 1352 }
81a76618 1353
41f562be
SH
1354 if (s->length > start_len) {
1355 ds_chomp(s, ',');
81a76618
BP
1356 }
1357}
1358
1359/* Converts 'match' to a string and returns the string. If 'priority' is
1360 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1361 * must free the string (with free()). */
1362char *
eb391b76 1363match_to_string(const struct match *match, int priority)
81a76618
BP
1364{
1365 struct ds s = DS_EMPTY_INITIALIZER;
1366 match_format(match, &s, priority);
1367 return ds_steal_cstr(&s);
1368}
1369
1370void
1371match_print(const struct match *match)
1372{
1373 char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
1374 puts(s);
1375 free(s);
1376}
5cb7a798
BP
1377\f
1378/* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1379 * with minimatch_destroy(). */
1380void
1381minimatch_init(struct minimatch *dst, const struct match *src)
1382{
ceb3bd67
JR
1383 struct miniflow tmp;
1384
1385 miniflow_map_init(&tmp, &src->wc.masks);
1386 /* Allocate two consecutive miniflows. */
1387 miniflow_alloc(dst->flows, 2, &tmp);
1388 miniflow_init(dst->flow, &src->flow);
1389 minimask_init(dst->mask, &src->wc);
5cb7a798
BP
1390}
1391
1392/* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1393 * with minimatch_destroy(). */
1394void
1395minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1396{
ceb3bd67
JR
1397 /* Allocate two consecutive miniflows. */
1398 size_t data_size = miniflow_alloc(dst->flows, 2, &src->mask->masks);
1399
09b0fa9c
JR
1400 memcpy(miniflow_values(dst->flow),
1401 miniflow_get_values(src->flow), data_size);
1402 memcpy(miniflow_values(&dst->mask->masks),
1403 miniflow_get_values(&src->mask->masks), data_size);
5cb7a798
BP
1404}
1405
b2c1f00b
BP
1406/* Initializes 'dst' with the data in 'src', destroying 'src'. The caller must
1407 * eventually free 'dst' with minimatch_destroy(). */
1408void
1409minimatch_move(struct minimatch *dst, struct minimatch *src)
1410{
8fd47924
JR
1411 dst->flow = src->flow;
1412 dst->mask = src->mask;
b2c1f00b
BP
1413}
1414
5cb7a798
BP
1415/* Frees any memory owned by 'match'. Does not free the storage in which
1416 * 'match' itself resides; the caller is responsible for that. */
1417void
1418minimatch_destroy(struct minimatch *match)
1419{
8fd47924 1420 free(match->flow);
5cb7a798
BP
1421}
1422
1423/* Initializes 'dst' as a copy of 'src'. */
1424void
1425minimatch_expand(const struct minimatch *src, struct match *dst)
1426{
8fd47924
JR
1427 miniflow_expand(src->flow, &dst->flow);
1428 minimask_expand(src->mask, &dst->wc);
9558d2a5 1429 memset(&dst->tun_md, 0, sizeof dst->tun_md);
5cb7a798
BP
1430}
1431
1432/* Returns true if 'a' and 'b' match the same packets, false otherwise. */
1433bool
1434minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1435{
8fd47924
JR
1436 return minimask_equal(a->mask, b->mask)
1437 && miniflow_equal(a->flow, b->flow);
5cb7a798
BP
1438}
1439
df40c152
BP
1440/* Returns true if 'target' satisifies 'match', that is, if each bit for which
1441 * 'match' specifies a particular value has the correct value in 'target'.
1442 *
1443 * This function is equivalent to miniflow_equal_flow_in_minimask(&match->flow,
1444 * target, &match->mask) but it is faster because of the invariant that
1445 * match->flow.map and match->mask.map are the same. */
1446bool
1447minimatch_matches_flow(const struct minimatch *match,
1448 const struct flow *target)
1449{
09b0fa9c
JR
1450 const uint64_t *flowp = miniflow_get_values(match->flow);
1451 const uint64_t *maskp = miniflow_get_values(&match->mask->masks);
361d808d 1452 size_t idx;
df40c152 1453
5fcff47b
JR
1454 FLOWMAP_FOR_EACH_INDEX(idx, match->flow->map) {
1455 if ((*flowp++ ^ flow_u64_value(target, idx)) & *maskp++) {
080e28d0 1456 return false;
df40c152 1457 }
df40c152
BP
1458 }
1459
1460 return true;
1461}
1462
5cb7a798
BP
1463/* Appends a string representation of 'match' to 's'. If 'priority' is
1464 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1465void
8d8ab6c2
JG
1466minimatch_format(const struct minimatch *match,
1467 const struct tun_table *tun_table,struct ds *s, int priority)
5cb7a798
BP
1468{
1469 struct match megamatch;
1470
1471 minimatch_expand(match, &megamatch);
8d8ab6c2
JG
1472 megamatch.flow.tunnel.metadata.tab = tun_table;
1473
5cb7a798
BP
1474 match_format(&megamatch, s, priority);
1475}
1476
1477/* Converts 'match' to a string and returns the string. If 'priority' is
1478 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1479 * must free the string (with free()). */
1480char *
eb391b76 1481minimatch_to_string(const struct minimatch *match, int priority)
5cb7a798
BP
1482{
1483 struct match megamatch;
1484
1485 minimatch_expand(match, &megamatch);
1486 return match_to_string(&megamatch, priority);
1487}