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