lib/match: Do not format undefined fields.
[cascardo/ovs.git] / lib / match.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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 "match.h"
19 #include <stdlib.h>
20 #include "byte-order.h"
21 #include "dynamic-string.h"
22 #include "ofp-util.h"
23 #include "packets.h"
24
25 /* Converts the flow in 'flow' into a match in 'match', with the given
26  * 'wildcards'. */
27 void
28 match_init(struct match *match,
29            const struct flow *flow, const struct flow_wildcards *wc)
30 {
31     match->flow = *flow;
32     match->wc = *wc;
33     match_zero_wildcarded_fields(match);
34 }
35
36 /* Converts a flow into a match.  It sets the wildcard masks based on
37  * the packet contents.  It will not set the mask for fields that do not
38  * make sense for the packet type. */
39 void
40 match_wc_init(struct match *match, const struct flow *flow)
41 {
42     match->flow = *flow;
43
44     flow_wildcards_init_for_packet(&match->wc, flow);
45     WC_MASK_FIELD(&match->wc, regs);
46     WC_MASK_FIELD(&match->wc, metadata);
47 }
48
49 /* Initializes 'match' as a "catch-all" match that matches every packet. */
50 void
51 match_init_catchall(struct match *match)
52 {
53     memset(&match->flow, 0, sizeof match->flow);
54     flow_wildcards_init_catchall(&match->wc);
55 }
56
57 /* For each bit or field wildcarded in 'match', sets the corresponding bit or
58  * field in 'flow' to all-0-bits.  It is important to maintain this invariant
59  * in a match that might be inserted into a classifier.
60  *
61  * It is never necessary to call this function directly for a match that is
62  * initialized or modified only by match_*() functions.  It is useful to
63  * restore the invariant in a match whose 'wc' member is modified by hand.
64  */
65 void
66 match_zero_wildcarded_fields(struct match *match)
67 {
68     flow_zero_wildcards(&match->flow, &match->wc);
69 }
70
71 void
72 match_set_dp_hash(struct match *match, uint32_t value)
73 {
74     match_set_dp_hash_masked(match, value, UINT32_MAX);
75 }
76
77 void
78 match_set_dp_hash_masked(struct match *match, uint32_t value, uint32_t mask)
79 {
80     match->wc.masks.dp_hash = mask;
81     match->flow.dp_hash = value & mask;
82 }
83
84 void
85 match_set_recirc_id(struct match *match, uint32_t value)
86 {
87     match->flow.recirc_id = value;
88     match->wc.masks.recirc_id = UINT32_MAX;
89 }
90
91 void
92 match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
93 {
94     match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
95 }
96
97 void
98 match_set_reg_masked(struct match *match, unsigned int reg_idx,
99                      uint32_t value, uint32_t mask)
100 {
101     ovs_assert(reg_idx < FLOW_N_REGS);
102     flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
103     match->flow.regs[reg_idx] = value & mask;
104 }
105
106 void
107 match_set_xreg(struct match *match, unsigned int xreg_idx, uint64_t value)
108 {
109     match_set_xreg_masked(match, xreg_idx, value, UINT64_MAX);
110 }
111
112 void
113 match_set_xreg_masked(struct match *match, unsigned int xreg_idx,
114                       uint64_t value, uint64_t mask)
115 {
116     ovs_assert(xreg_idx < FLOW_N_XREGS);
117     flow_wildcards_set_xreg_mask(&match->wc, xreg_idx, mask);
118     flow_set_xreg(&match->flow, xreg_idx, value & mask);
119 }
120
121 void
122 match_set_metadata(struct match *match, ovs_be64 metadata)
123 {
124     match_set_metadata_masked(match, metadata, OVS_BE64_MAX);
125 }
126
127 void
128 match_set_metadata_masked(struct match *match,
129                           ovs_be64 metadata, ovs_be64 mask)
130 {
131     match->wc.masks.metadata = mask;
132     match->flow.metadata = metadata & mask;
133 }
134
135 void
136 match_set_tun_id(struct match *match, ovs_be64 tun_id)
137 {
138     match_set_tun_id_masked(match, tun_id, OVS_BE64_MAX);
139 }
140
141 void
142 match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
143 {
144     match->wc.masks.tunnel.tun_id = mask;
145     match->flow.tunnel.tun_id = tun_id & mask;
146 }
147
148 void
149 match_set_tun_src(struct match *match, ovs_be32 src)
150 {
151     match_set_tun_src_masked(match, src, OVS_BE32_MAX);
152 }
153
154 void
155 match_set_tun_src_masked(struct match *match, ovs_be32 src, ovs_be32 mask)
156 {
157     match->wc.masks.tunnel.ip_src = mask;
158     match->flow.tunnel.ip_src = src & mask;
159 }
160
161 void
162 match_set_tun_dst(struct match *match, ovs_be32 dst)
163 {
164     match_set_tun_dst_masked(match, dst, OVS_BE32_MAX);
165 }
166
167 void
168 match_set_tun_dst_masked(struct match *match, ovs_be32 dst, ovs_be32 mask)
169 {
170     match->wc.masks.tunnel.ip_dst = mask;
171     match->flow.tunnel.ip_dst = dst & mask;
172 }
173
174 void
175 match_set_tun_ttl(struct match *match, uint8_t ttl)
176 {
177     match_set_tun_ttl_masked(match, ttl, UINT8_MAX);
178 }
179
180 void
181 match_set_tun_ttl_masked(struct match *match, uint8_t ttl, uint8_t mask)
182 {
183     match->wc.masks.tunnel.ip_ttl = mask;
184     match->flow.tunnel.ip_ttl = ttl & mask;
185 }
186
187 void
188 match_set_tun_tos(struct match *match, uint8_t tos)
189 {
190     match_set_tun_tos_masked(match, tos, UINT8_MAX);
191 }
192
193 void
194 match_set_tun_tos_masked(struct match *match, uint8_t tos, uint8_t mask)
195 {
196     match->wc.masks.tunnel.ip_tos = mask;
197     match->flow.tunnel.ip_tos = tos & mask;
198 }
199
200 void
201 match_set_tun_flags(struct match *match, uint16_t flags)
202 {
203     match_set_tun_flags_masked(match, flags, UINT16_MAX);
204 }
205
206 void
207 match_set_tun_flags_masked(struct match *match, uint16_t flags, uint16_t mask)
208 {
209     match->wc.masks.tunnel.flags = mask;
210     match->flow.tunnel.flags = flags & mask;
211 }
212
213 void
214 match_set_in_port(struct match *match, ofp_port_t ofp_port)
215 {
216     match->wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
217     match->flow.in_port.ofp_port = ofp_port;
218 }
219
220 void
221 match_set_skb_priority(struct match *match, uint32_t skb_priority)
222 {
223     match->wc.masks.skb_priority = UINT32_MAX;
224     match->flow.skb_priority = skb_priority;
225 }
226
227 void
228 match_set_pkt_mark(struct match *match, uint32_t pkt_mark)
229 {
230     match_set_pkt_mark_masked(match, pkt_mark, UINT32_MAX);
231 }
232
233 void
234 match_set_pkt_mark_masked(struct match *match, uint32_t pkt_mark, uint32_t mask)
235 {
236     match->flow.pkt_mark = pkt_mark & mask;
237     match->wc.masks.pkt_mark = mask;
238 }
239
240 void
241 match_set_dl_type(struct match *match, ovs_be16 dl_type)
242 {
243     match->wc.masks.dl_type = OVS_BE16_MAX;
244     match->flow.dl_type = dl_type;
245 }
246
247 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
248  * exactly.  'mask_dst' is set to all 1s. */
249 static void
250 set_eth(const uint8_t value_src[ETH_ADDR_LEN],
251         uint8_t value_dst[ETH_ADDR_LEN],
252         uint8_t mask_dst[ETH_ADDR_LEN])
253 {
254     memcpy(value_dst, value_src, ETH_ADDR_LEN);
255     memset(mask_dst, 0xff, ETH_ADDR_LEN);
256 }
257
258 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
259  * after each byte is ANDed with the appropriate byte in 'mask_src'.
260  * 'mask_dst' is set to 'mask_src' */
261 static void
262 set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
263                const uint8_t mask_src[ETH_ADDR_LEN],
264                uint8_t value_dst[ETH_ADDR_LEN],
265                uint8_t mask_dst[ETH_ADDR_LEN])
266 {
267     size_t i;
268
269     for (i = 0; i < ETH_ADDR_LEN; i++) {
270         value_dst[i] = value_src[i] & mask_src[i];
271         mask_dst[i] = mask_src[i];
272     }
273 }
274
275 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
276  * exactly. */
277 void
278 match_set_dl_src(struct match *match, const uint8_t dl_src[ETH_ADDR_LEN])
279 {
280     set_eth(dl_src, match->flow.dl_src, match->wc.masks.dl_src);
281 }
282
283 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
284  * after each byte is ANDed with the appropriate byte in 'mask'. */
285 void
286 match_set_dl_src_masked(struct match *match,
287                         const uint8_t dl_src[ETH_ADDR_LEN],
288                         const uint8_t mask[ETH_ADDR_LEN])
289 {
290     set_eth_masked(dl_src, mask, match->flow.dl_src, match->wc.masks.dl_src);
291 }
292
293 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
294  * exactly. */
295 void
296 match_set_dl_dst(struct match *match, const uint8_t dl_dst[ETH_ADDR_LEN])
297 {
298     set_eth(dl_dst, match->flow.dl_dst, match->wc.masks.dl_dst);
299 }
300
301 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
302  * byte is ANDed with the appropriate byte in 'mask'.
303  *
304  * This function will assert-fail if 'mask' is invalid.  Only 'mask' values
305  * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
306 void
307 match_set_dl_dst_masked(struct match *match,
308                         const uint8_t dl_dst[ETH_ADDR_LEN],
309                         const uint8_t mask[ETH_ADDR_LEN])
310 {
311     set_eth_masked(dl_dst, mask, match->flow.dl_dst, match->wc.masks.dl_dst);
312 }
313
314 void
315 match_set_dl_tci(struct match *match, ovs_be16 tci)
316 {
317     match_set_dl_tci_masked(match, tci, htons(0xffff));
318 }
319
320 void
321 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
322 {
323     match->flow.vlan_tci = tci & mask;
324     match->wc.masks.vlan_tci = mask;
325 }
326
327 /* Modifies 'match' so that the VLAN VID is wildcarded.  If the PCP is already
328  * wildcarded, then 'match' will match a packet regardless of whether it has an
329  * 802.1Q header or not. */
330 void
331 match_set_any_vid(struct match *match)
332 {
333     if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
334         match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
335         match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
336     } else {
337         match_set_dl_tci_masked(match, htons(0), htons(0));
338     }
339 }
340
341 /* Modifies 'match' depending on 'dl_vlan':
342  *
343  *   - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
344  *     without an 802.1Q header.
345  *
346  *   - Otherwise, makes 'match' match only packets with an 802.1Q header whose
347  *     VID equals the low 12 bits of 'dl_vlan'.
348  */
349 void
350 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
351 {
352     flow_set_dl_vlan(&match->flow, dl_vlan);
353     if (dl_vlan == htons(OFP10_VLAN_NONE)) {
354         match->wc.masks.vlan_tci = OVS_BE16_MAX;
355     } else {
356         match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
357     }
358 }
359
360 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
361  * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
362  * plus CFI). */
363 void
364 match_set_vlan_vid(struct match *match, ovs_be16 vid)
365 {
366     match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
367 }
368
369
370 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
371  * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
372  * plus CFI), with the corresponding 'mask'. */
373 void
374 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
375 {
376     ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
377     ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
378
379     mask &= vid_mask;
380     flow_set_vlan_vid(&match->flow, vid & mask);
381     match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
382 }
383
384 /* Modifies 'match' so that the VLAN PCP is wildcarded.  If the VID is already
385  * wildcarded, then 'match' will match a packet regardless of whether it has an
386  * 802.1Q header or not. */
387 void
388 match_set_any_pcp(struct match *match)
389 {
390     if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
391         match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
392         match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
393     } else {
394         match_set_dl_tci_masked(match, htons(0), htons(0));
395     }
396 }
397
398 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
399  * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
400 void
401 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
402 {
403     flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
404     match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
405 }
406
407 /* Modifies 'match' so that the MPLS label 'idx' matches 'lse' exactly. */
408 void
409 match_set_mpls_lse(struct match *match, int idx, ovs_be32 lse)
410 {
411     match->wc.masks.mpls_lse[idx] = OVS_BE32_MAX;
412     match->flow.mpls_lse[idx] = lse;
413 }
414
415 /* Modifies 'match' so that the MPLS label is wildcarded. */
416 void
417 match_set_any_mpls_label(struct match *match, int idx)
418 {
419     match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_LABEL_MASK);
420     flow_set_mpls_label(&match->flow, idx, htonl(0));
421 }
422
423 /* Modifies 'match' so that it matches only packets with an MPLS header whose
424  * label equals the low 20 bits of 'mpls_label'. */
425 void
426 match_set_mpls_label(struct match *match, int idx, ovs_be32 mpls_label)
427 {
428     match->wc.masks.mpls_lse[idx] |= htonl(MPLS_LABEL_MASK);
429     flow_set_mpls_label(&match->flow, idx, mpls_label);
430 }
431
432 /* Modifies 'match' so that the MPLS TC is wildcarded. */
433 void
434 match_set_any_mpls_tc(struct match *match, int idx)
435 {
436     match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TC_MASK);
437     flow_set_mpls_tc(&match->flow, idx, 0);
438 }
439
440 /* Modifies 'match' so that it matches only packets with an MPLS header whose
441  * Traffic Class equals the low 3 bits of 'mpls_tc'. */
442 void
443 match_set_mpls_tc(struct match *match, int idx, uint8_t mpls_tc)
444 {
445     match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TC_MASK);
446     flow_set_mpls_tc(&match->flow, idx, mpls_tc);
447 }
448
449 /* Modifies 'match' so that the MPLS stack flag is wildcarded. */
450 void
451 match_set_any_mpls_bos(struct match *match, int idx)
452 {
453     match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_BOS_MASK);
454     flow_set_mpls_bos(&match->flow, idx, 0);
455 }
456
457 /* Modifies 'match' so that it matches only packets with an MPLS header whose
458  * Stack Flag equals the lower bit of 'mpls_bos' */
459 void
460 match_set_mpls_bos(struct match *match, int idx, uint8_t mpls_bos)
461 {
462     match->wc.masks.mpls_lse[idx] |= htonl(MPLS_BOS_MASK);
463     flow_set_mpls_bos(&match->flow, idx, mpls_bos);
464 }
465
466 /* Modifies 'match' so that the MPLS LSE is wildcarded. */
467 void
468 match_set_any_mpls_lse(struct match *match, int idx)
469 {
470     match->wc.masks.mpls_lse[idx] = htonl(0);
471     flow_set_mpls_lse(&match->flow, idx, htonl(0));
472 }
473
474 void
475 match_set_tp_src(struct match *match, ovs_be16 tp_src)
476 {
477     match_set_tp_src_masked(match, tp_src, OVS_BE16_MAX);
478 }
479
480 void
481 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
482 {
483     match->flow.tp_src = port & mask;
484     match->wc.masks.tp_src = mask;
485 }
486
487 void
488 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
489 {
490     match_set_tp_dst_masked(match, tp_dst, OVS_BE16_MAX);
491 }
492
493 void
494 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
495 {
496     match->flow.tp_dst = port & mask;
497     match->wc.masks.tp_dst = mask;
498 }
499
500 void
501 match_set_tcp_flags(struct match *match, ovs_be16 flags)
502 {
503     match_set_tcp_flags_masked(match, flags, OVS_BE16_MAX);
504 }
505
506 void
507 match_set_tcp_flags_masked(struct match *match, ovs_be16 flags, ovs_be16 mask)
508 {
509     match->flow.tcp_flags = flags & mask;
510     match->wc.masks.tcp_flags = mask;
511 }
512
513 void
514 match_set_nw_proto(struct match *match, uint8_t nw_proto)
515 {
516     match->flow.nw_proto = nw_proto;
517     match->wc.masks.nw_proto = UINT8_MAX;
518 }
519
520 void
521 match_set_nw_src(struct match *match, ovs_be32 nw_src)
522 {
523     match->flow.nw_src = nw_src;
524     match->wc.masks.nw_src = OVS_BE32_MAX;
525 }
526
527 void
528 match_set_nw_src_masked(struct match *match,
529                         ovs_be32 nw_src, ovs_be32 mask)
530 {
531     match->flow.nw_src = nw_src & mask;
532     match->wc.masks.nw_src = mask;
533 }
534
535 void
536 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
537 {
538     match->flow.nw_dst = nw_dst;
539     match->wc.masks.nw_dst = OVS_BE32_MAX;
540 }
541
542 void
543 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
544 {
545     match->flow.nw_dst = ip & mask;
546     match->wc.masks.nw_dst = mask;
547 }
548
549 void
550 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
551 {
552     match->wc.masks.nw_tos |= IP_DSCP_MASK;
553     match->flow.nw_tos &= ~IP_DSCP_MASK;
554     match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
555 }
556
557 void
558 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
559 {
560     match->wc.masks.nw_tos |= IP_ECN_MASK;
561     match->flow.nw_tos &= ~IP_ECN_MASK;
562     match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
563 }
564
565 void
566 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
567 {
568     match->wc.masks.nw_ttl = UINT8_MAX;
569     match->flow.nw_ttl = nw_ttl;
570 }
571
572 void
573 match_set_nw_frag(struct match *match, uint8_t nw_frag)
574 {
575     match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
576     match->flow.nw_frag = nw_frag;
577 }
578
579 void
580 match_set_nw_frag_masked(struct match *match,
581                          uint8_t nw_frag, uint8_t mask)
582 {
583     match->flow.nw_frag = nw_frag & mask;
584     match->wc.masks.nw_frag = mask;
585 }
586
587 void
588 match_set_icmp_type(struct match *match, uint8_t icmp_type)
589 {
590     match_set_tp_src(match, htons(icmp_type));
591 }
592
593 void
594 match_set_icmp_code(struct match *match, uint8_t icmp_code)
595 {
596     match_set_tp_dst(match, htons(icmp_code));
597 }
598
599 void
600 match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN])
601 {
602     memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN);
603     memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN);
604 }
605
606 void
607 match_set_arp_sha_masked(struct match *match,
608                          const uint8_t arp_sha[ETH_ADDR_LEN],
609                          const uint8_t mask[ETH_ADDR_LEN])
610 {
611     set_eth_masked(arp_sha, mask,
612                    match->flow.arp_sha, match->wc.masks.arp_sha);
613 }
614
615 void
616 match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN])
617 {
618     memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN);
619     memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN);
620 }
621
622 void
623 match_set_arp_tha_masked(struct match *match,
624                          const uint8_t arp_tha[ETH_ADDR_LEN],
625                          const uint8_t mask[ETH_ADDR_LEN])
626 {
627     set_eth_masked(arp_tha, mask,
628                    match->flow.arp_tha, match->wc.masks.arp_tha);
629 }
630
631 void
632 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
633 {
634     match->flow.ipv6_src = *src;
635     match->wc.masks.ipv6_src = in6addr_exact;
636 }
637
638 void
639 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
640                           const struct in6_addr *mask)
641 {
642     match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
643     match->wc.masks.ipv6_src = *mask;
644 }
645
646 void
647 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
648 {
649     match->flow.ipv6_dst = *dst;
650     match->wc.masks.ipv6_dst = in6addr_exact;
651 }
652
653 void
654 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
655                           const struct in6_addr *mask)
656 {
657     match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
658     match->wc.masks.ipv6_dst = *mask;
659 }
660
661 void
662 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
663 {
664     match->wc.masks.ipv6_label = OVS_BE32_MAX;
665     match->flow.ipv6_label = ipv6_label;
666 }
667
668
669 void
670 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
671                             ovs_be32 mask)
672 {
673     match->flow.ipv6_label = ipv6_label & mask;
674     match->wc.masks.ipv6_label = mask;
675 }
676
677 void
678 match_set_nd_target(struct match *match, const struct in6_addr *target)
679 {
680     match->flow.nd_target = *target;
681     match->wc.masks.nd_target = in6addr_exact;
682 }
683
684 void
685 match_set_nd_target_masked(struct match *match,
686                            const struct in6_addr *target,
687                            const struct in6_addr *mask)
688 {
689     match->flow.nd_target = ipv6_addr_bitand(target, mask);
690     match->wc.masks.nd_target = *mask;
691 }
692
693 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
694  * values for fixed fields, otherwise false. */
695 bool
696 match_equal(const struct match *a, const struct match *b)
697 {
698     return (flow_wildcards_equal(&a->wc, &b->wc)
699             && flow_equal(&a->flow, &b->flow));
700 }
701
702 /* Returns a hash value for the flow and wildcards in 'match', starting from
703  * 'basis'. */
704 uint32_t
705 match_hash(const struct match *match, uint32_t basis)
706 {
707     return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
708 }
709
710 static bool
711 match_has_default_recirc_id(const struct match *m)
712 {
713     return m->flow.recirc_id == 0 && (m->wc.masks.recirc_id == UINT32_MAX ||
714                                       m->wc.masks.recirc_id == 0);
715 }
716
717 static bool
718 match_has_default_dp_hash(const struct match *m)
719 {
720     return ((m->flow.dp_hash | m->wc.masks.dp_hash) == 0);
721 }
722
723 /* Return true if the hidden fields of the match are set to the default values.
724  * The default values equals to those set up by match_init_hidden_fields(). */
725 bool
726 match_has_default_hidden_fields(const struct match *m)
727 {
728     return match_has_default_recirc_id(m) && match_has_default_dp_hash(m);
729 }
730
731 void
732 match_init_hidden_fields(struct match *m)
733 {
734     match_set_recirc_id(m, 0);
735     match_set_dp_hash_masked(m, 0, 0);
736 }
737
738 static void
739 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
740                   const uint8_t mask[6])
741 {
742     if (!eth_addr_is_zero(mask)) {
743         ds_put_format(s, "%s=", name);
744         eth_format_masked(eth, mask, s);
745         ds_put_char(s, ',');
746     }
747 }
748
749 static void
750 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
751                   ovs_be32 netmask)
752 {
753     if (netmask) {
754         ds_put_format(s, "%s=", name);
755         ip_format_masked(ip, netmask, s);
756         ds_put_char(s, ',');
757     }
758 }
759
760 static void
761 format_ipv6_netmask(struct ds *s, const char *name,
762                     const struct in6_addr *addr,
763                     const struct in6_addr *netmask)
764 {
765     if (!ipv6_mask_is_any(netmask)) {
766         ds_put_format(s, "%s=", name);
767         print_ipv6_masked(s, addr, netmask);
768         ds_put_char(s, ',');
769     }
770 }
771
772 static void
773 format_be16_masked(struct ds *s, const char *name,
774                    ovs_be16 value, ovs_be16 mask)
775 {
776     if (mask != htons(0)) {
777         ds_put_format(s, "%s=", name);
778         if (mask == OVS_BE16_MAX) {
779             ds_put_format(s, "%"PRIu16, ntohs(value));
780         } else {
781             ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
782                           ntohs(value), ntohs(mask));
783         }
784         ds_put_char(s, ',');
785     }
786 }
787
788 static void
789 format_be32_masked(struct ds *s, const char *name,
790                    ovs_be32 value, ovs_be32 mask)
791 {
792     if (mask != htonl(0)) {
793         ds_put_format(s, "%s=", name);
794         if (mask == OVS_BE32_MAX) {
795             ds_put_format(s, "%"PRIu32, ntohl(value));
796         } else {
797             ds_put_format(s, "0x%"PRIx32"/0x%"PRIx32,
798                           ntohl(value), ntohl(mask));
799         }
800         ds_put_char(s, ',');
801     }
802 }
803
804 static void
805 format_uint32_masked(struct ds *s, const char *name,
806                    uint32_t value, uint32_t mask)
807 {
808     if (mask) {
809         ds_put_format(s, "%s=%#"PRIx32, name, value);
810         if (mask != UINT32_MAX) {
811             ds_put_format(s, "/%#"PRIx32, mask);
812         }
813         ds_put_char(s, ',');
814     }
815 }
816
817 static void
818 format_be64_masked(struct ds *s, const char *name,
819                    ovs_be64 value, ovs_be64 mask)
820 {
821     if (mask != htonll(0)) {
822         ds_put_format(s, "%s=%#"PRIx64, name, ntohll(value));
823         if (mask != OVS_BE64_MAX) {
824             ds_put_format(s, "/%#"PRIx64, ntohll(mask));
825         }
826         ds_put_char(s, ',');
827     }
828 }
829
830 static void
831 format_flow_tunnel(struct ds *s, const struct match *match)
832 {
833     const struct flow_wildcards *wc = &match->wc;
834     const struct flow_tnl *tnl = &match->flow.tunnel;
835
836     format_be64_masked(s, "tun_id", tnl->tun_id, wc->masks.tunnel.tun_id);
837     format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
838     format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
839
840     if (wc->masks.tunnel.ip_tos) {
841         ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
842     }
843     if (wc->masks.tunnel.ip_ttl) {
844         ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
845     }
846     if (wc->masks.tunnel.flags) {
847         format_flags(s, flow_tun_flag_to_string, tnl->flags, '|');
848         ds_put_char(s, ',');
849     }
850 }
851
852 /* Appends a string representation of 'match' to 's'.  If 'priority' is
853  * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
854 void
855 match_format(const struct match *match, struct ds *s, unsigned int priority)
856 {
857     const struct flow_wildcards *wc = &match->wc;
858     size_t start_len = s->length;
859     const struct flow *f = &match->flow;
860     bool skip_type = false;
861     bool skip_proto = false;
862
863     int i;
864
865     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 27);
866
867     if (priority != OFP_DEFAULT_PRIORITY) {
868         ds_put_format(s, "priority=%u,", priority);
869     }
870
871     format_uint32_masked(s, "pkt_mark", f->pkt_mark, wc->masks.pkt_mark);
872
873     if (wc->masks.recirc_id) {
874         format_uint32_masked(s, "recirc_id", f->recirc_id,
875                              wc->masks.recirc_id);
876     }
877
878     if (f->dp_hash && wc->masks.dp_hash) {
879         format_uint32_masked(s, "dp_hash", f->dp_hash,
880                              wc->masks.dp_hash);
881     }
882
883     if (wc->masks.skb_priority) {
884         ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority);
885     }
886
887     if (wc->masks.dl_type) {
888         skip_type = true;
889         if (f->dl_type == htons(ETH_TYPE_IP)) {
890             if (wc->masks.nw_proto) {
891                 skip_proto = true;
892                 if (f->nw_proto == IPPROTO_ICMP) {
893                     ds_put_cstr(s, "icmp,");
894                 } else if (f->nw_proto == IPPROTO_IGMP) {
895                     ds_put_cstr(s, "igmp,");
896                 } else if (f->nw_proto == IPPROTO_TCP) {
897                     ds_put_cstr(s, "tcp,");
898                 } else if (f->nw_proto == IPPROTO_UDP) {
899                     ds_put_cstr(s, "udp,");
900                 } else if (f->nw_proto == IPPROTO_SCTP) {
901                     ds_put_cstr(s, "sctp,");
902                 } else {
903                     ds_put_cstr(s, "ip,");
904                     skip_proto = false;
905                 }
906             } else {
907                 ds_put_cstr(s, "ip,");
908             }
909         } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
910             if (wc->masks.nw_proto) {
911                 skip_proto = true;
912                 if (f->nw_proto == IPPROTO_ICMPV6) {
913                     ds_put_cstr(s, "icmp6,");
914                 } else if (f->nw_proto == IPPROTO_TCP) {
915                     ds_put_cstr(s, "tcp6,");
916                 } else if (f->nw_proto == IPPROTO_UDP) {
917                     ds_put_cstr(s, "udp6,");
918                 } else if (f->nw_proto == IPPROTO_SCTP) {
919                     ds_put_cstr(s, "sctp6,");
920                 } else {
921                     ds_put_cstr(s, "ipv6,");
922                     skip_proto = false;
923                 }
924             } else {
925                 ds_put_cstr(s, "ipv6,");
926             }
927         } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
928             ds_put_cstr(s, "arp,");
929         } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
930             ds_put_cstr(s, "rarp,");
931         } else if (f->dl_type == htons(ETH_TYPE_MPLS)) {
932             ds_put_cstr(s, "mpls,");
933         } else if (f->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
934             ds_put_cstr(s, "mplsm,");
935         } else {
936             skip_type = false;
937         }
938     }
939     for (i = 0; i < FLOW_N_REGS; i++) {
940         #define REGNAME_LEN 20
941         char regname[REGNAME_LEN];
942         if (snprintf(regname, REGNAME_LEN, "reg%d", i) >= REGNAME_LEN) {
943             strcpy(regname, "reg?");
944         }
945         format_uint32_masked(s, regname, f->regs[i], wc->masks.regs[i]);
946     }
947
948     format_flow_tunnel(s, match);
949
950     format_be64_masked(s, "metadata", f->metadata, wc->masks.metadata);
951
952     if (wc->masks.in_port.ofp_port) {
953         ds_put_cstr(s, "in_port=");
954         ofputil_format_port(f->in_port.ofp_port, s);
955         ds_put_char(s, ',');
956     }
957     if (wc->masks.vlan_tci) {
958         ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
959         ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
960         ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
961
962         if (cfi && f->vlan_tci & htons(VLAN_CFI)
963             && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
964             && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
965             && (vid_mask || pcp_mask)) {
966             if (vid_mask) {
967                 ds_put_format(s, "dl_vlan=%"PRIu16",",
968                               vlan_tci_to_vid(f->vlan_tci));
969             }
970             if (pcp_mask) {
971                 ds_put_format(s, "dl_vlan_pcp=%d,",
972                               vlan_tci_to_pcp(f->vlan_tci));
973             }
974         } else if (wc->masks.vlan_tci == htons(0xffff)) {
975             ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
976         } else {
977             ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
978                           ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
979         }
980     }
981     format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
982     format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
983     if (!skip_type && wc->masks.dl_type) {
984         ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
985     }
986     if (f->dl_type == htons(ETH_TYPE_IPV6)) {
987         format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
988         format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
989         if (wc->masks.ipv6_label) {
990             if (wc->masks.ipv6_label == OVS_BE32_MAX) {
991                 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
992                               ntohl(f->ipv6_label));
993             } else {
994                 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
995                               ntohl(f->ipv6_label),
996                               ntohl(wc->masks.ipv6_label));
997             }
998         }
999     } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
1000                f->dl_type == htons(ETH_TYPE_RARP)) {
1001         format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
1002         format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
1003     } else {
1004         format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
1005         format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
1006     }
1007     if (!skip_proto && wc->masks.nw_proto) {
1008         if (f->dl_type == htons(ETH_TYPE_ARP) ||
1009             f->dl_type == htons(ETH_TYPE_RARP)) {
1010             ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
1011         } else {
1012             ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
1013         }
1014     }
1015     if (f->dl_type == htons(ETH_TYPE_ARP) ||
1016         f->dl_type == htons(ETH_TYPE_RARP)) {
1017         format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
1018         format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
1019     }
1020     if (wc->masks.nw_tos & IP_DSCP_MASK) {
1021         ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
1022     }
1023     if (wc->masks.nw_tos & IP_ECN_MASK) {
1024         ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
1025     }
1026     if (wc->masks.nw_ttl) {
1027         ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
1028     }
1029     if (wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
1030         ds_put_format(s, "mpls_label=%"PRIu32",",
1031                       mpls_lse_to_label(f->mpls_lse[0]));
1032     }
1033     if (wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
1034         ds_put_format(s, "mpls_tc=%"PRIu8",",
1035                       mpls_lse_to_tc(f->mpls_lse[0]));
1036     }
1037     if (wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK)) {
1038         ds_put_format(s, "mpls_ttl=%"PRIu8",",
1039                       mpls_lse_to_ttl(f->mpls_lse[0]));
1040     }
1041     if (wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
1042         ds_put_format(s, "mpls_bos=%"PRIu8",",
1043                       mpls_lse_to_bos(f->mpls_lse[0]));
1044     }
1045     format_be32_masked(s, "mpls_lse1", f->mpls_lse[1], wc->masks.mpls_lse[1]);
1046     format_be32_masked(s, "mpls_lse2", f->mpls_lse[2], wc->masks.mpls_lse[2]);
1047
1048     switch (wc->masks.nw_frag) {
1049     case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
1050         ds_put_format(s, "nw_frag=%s,",
1051                       f->nw_frag & FLOW_NW_FRAG_ANY
1052                       ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
1053                       : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
1054         break;
1055
1056     case FLOW_NW_FRAG_ANY:
1057         ds_put_format(s, "nw_frag=%s,",
1058                       f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
1059         break;
1060
1061     case FLOW_NW_FRAG_LATER:
1062         ds_put_format(s, "nw_frag=%s,",
1063                       f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
1064         break;
1065     }
1066     if (f->dl_type == htons(ETH_TYPE_IP) &&
1067         f->nw_proto == IPPROTO_ICMP) {
1068         format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1069         format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1070     } else if (f->dl_type == htons(ETH_TYPE_IP) &&
1071                f->nw_proto == IPPROTO_IGMP) {
1072         format_be16_masked(s, "igmp_type", f->tp_src, wc->masks.tp_src);
1073         format_be16_masked(s, "igmp_code", f->tp_dst, wc->masks.tp_dst);
1074     } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
1075                f->nw_proto == IPPROTO_ICMPV6) {
1076         format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1077         format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1078         format_ipv6_netmask(s, "nd_target", &f->nd_target,
1079                             &wc->masks.nd_target);
1080         format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
1081         format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
1082     } else {
1083         format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
1084         format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
1085     }
1086     if (is_ip_any(f) && f->nw_proto == IPPROTO_TCP && wc->masks.tcp_flags) {
1087         uint16_t mask = TCP_FLAGS(wc->masks.tcp_flags);
1088
1089         if (mask == TCP_FLAGS(OVS_BE16_MAX)) {
1090             ds_put_cstr(s, "tcp_flags=");
1091             if (f->tcp_flags) {
1092                 format_flags(s, packet_tcp_flag_to_string, ntohs(f->tcp_flags),
1093                              '|');
1094             } else {
1095                 ds_put_cstr(s, "0"); /* Zero flags. */
1096             }
1097         } else if (mask) {
1098             format_flags_masked(s, "tcp_flags", packet_tcp_flag_to_string,
1099                                 ntohs(f->tcp_flags), mask);
1100         }
1101     }
1102
1103     if (s->length > start_len && ds_last(s) == ',') {
1104         s->length--;
1105     }
1106 }
1107
1108 /* Converts 'match' to a string and returns the string.  If 'priority' is
1109  * different from OFP_DEFAULT_PRIORITY, includes it in the string.  The caller
1110  * must free the string (with free()). */
1111 char *
1112 match_to_string(const struct match *match, unsigned int priority)
1113 {
1114     struct ds s = DS_EMPTY_INITIALIZER;
1115     match_format(match, &s, priority);
1116     return ds_steal_cstr(&s);
1117 }
1118
1119 void
1120 match_print(const struct match *match)
1121 {
1122     char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
1123     puts(s);
1124     free(s);
1125 }
1126 \f
1127 /* Initializes 'dst' as a copy of 'src'.  The caller must eventually free 'dst'
1128  * with minimatch_destroy(). */
1129 void
1130 minimatch_init(struct minimatch *dst, const struct match *src)
1131 {
1132     minimask_init(&dst->mask, &src->wc);
1133     miniflow_init_with_minimask(&dst->flow, &src->flow, &dst->mask);
1134 }
1135
1136 /* Initializes 'dst' as a copy of 'src'.  The caller must eventually free 'dst'
1137  * with minimatch_destroy(). */
1138 void
1139 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1140 {
1141     miniflow_clone(&dst->flow, &src->flow);
1142     minimask_clone(&dst->mask, &src->mask);
1143 }
1144
1145 /* Initializes 'dst' with the data in 'src', destroying 'src'.  The caller must
1146  * eventually free 'dst' with minimatch_destroy(). */
1147 void
1148 minimatch_move(struct minimatch *dst, struct minimatch *src)
1149 {
1150     miniflow_move(&dst->flow, &src->flow);
1151     minimask_move(&dst->mask, &src->mask);
1152 }
1153
1154 /* Frees any memory owned by 'match'.  Does not free the storage in which
1155  * 'match' itself resides; the caller is responsible for that. */
1156 void
1157 minimatch_destroy(struct minimatch *match)
1158 {
1159     miniflow_destroy(&match->flow);
1160     minimask_destroy(&match->mask);
1161 }
1162
1163 /* Initializes 'dst' as a copy of 'src'. */
1164 void
1165 minimatch_expand(const struct minimatch *src, struct match *dst)
1166 {
1167     miniflow_expand(&src->flow, &dst->flow);
1168     minimask_expand(&src->mask, &dst->wc);
1169 }
1170
1171 /* Returns true if 'a' and 'b' match the same packets, false otherwise.  */
1172 bool
1173 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1174 {
1175     return (miniflow_equal(&a->flow, &b->flow)
1176             && minimask_equal(&a->mask, &b->mask));
1177 }
1178
1179 /* Returns true if 'target' satisifies 'match', that is, if each bit for which
1180  * 'match' specifies a particular value has the correct value in 'target'.
1181  *
1182  * This function is equivalent to miniflow_equal_flow_in_minimask(&match->flow,
1183  * target, &match->mask) but it is faster because of the invariant that
1184  * match->flow.map and match->mask.map are the same. */
1185 bool
1186 minimatch_matches_flow(const struct minimatch *match,
1187                        const struct flow *target)
1188 {
1189     const uint32_t *target_u32 = (const uint32_t *) target;
1190     const uint32_t *flowp = miniflow_get_u32_values(&match->flow);
1191     const uint32_t *maskp = miniflow_get_u32_values(&match->mask.masks);
1192     uint64_t map;
1193
1194     for (map = match->flow.map; map; map = zero_rightmost_1bit(map)) {
1195         if ((*flowp++ ^ target_u32[raw_ctz(map)]) & *maskp++) {
1196             return false;
1197         }
1198     }
1199
1200     return true;
1201 }
1202
1203 /* Appends a string representation of 'match' to 's'.  If 'priority' is
1204  * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1205 void
1206 minimatch_format(const struct minimatch *match, struct ds *s,
1207                  unsigned int priority)
1208 {
1209     struct match megamatch;
1210
1211     minimatch_expand(match, &megamatch);
1212     match_format(&megamatch, s, priority);
1213 }
1214
1215 /* Converts 'match' to a string and returns the string.  If 'priority' is
1216  * different from OFP_DEFAULT_PRIORITY, includes it in the string.  The caller
1217  * must free the string (with free()). */
1218 char *
1219 minimatch_to_string(const struct minimatch *match, unsigned int priority)
1220 {
1221     struct match megamatch;
1222
1223     minimatch_expand(match, &megamatch);
1224     return match_to_string(&megamatch, priority);
1225 }