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