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