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