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