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