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