packets: Create global helper is_ip_any().
[sliver-openvswitch.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 #include "vlog.h"
25
26 VLOG_DEFINE_THIS_MODULE(match);
27
28
29 /* Converts the flow in 'flow' into a match in 'match', with the given
30  * 'wildcards'. */
31 void
32 match_init(struct match *match,
33            const struct flow *flow, const struct flow_wildcards *wc)
34 {
35     match->flow = *flow;
36     match->wc = *wc;
37     match_zero_wildcarded_fields(match);
38 }
39
40 /* Converts a flow into a match.  It sets the wildcard masks based on
41  * the packet contents.  It will not set the mask for fields that do not
42  * make sense for the packet type. */
43 void
44 match_wc_init(struct match *match, const struct flow *flow)
45 {
46     struct flow_wildcards *wc;
47     int i;
48
49     match->flow = *flow;
50     wc = &match->wc;
51     memset(&wc->masks, 0x0, sizeof wc->masks);
52
53     memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
54
55     if (flow->nw_proto) {
56         memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
57     }
58
59     if (flow->skb_priority) {
60         memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
61     }
62
63     if (flow->skb_mark) {
64         memset(&wc->masks.skb_mark, 0xff, sizeof wc->masks.skb_mark);
65     }
66
67     for (i = 0; i < FLOW_N_REGS; i++) {
68         if (flow->regs[i]) {
69             memset(&wc->masks.regs[i], 0xff, sizeof wc->masks.regs[i]);
70         }
71     }
72
73     if (flow->tunnel.ip_dst) {
74         if (flow->tunnel.flags & FLOW_TNL_F_KEY) {
75             memset(&wc->masks.tunnel.tun_id, 0xff, sizeof wc->masks.tunnel.tun_id);
76         }
77         memset(&wc->masks.tunnel.ip_src, 0xff, sizeof wc->masks.tunnel.ip_src);
78         memset(&wc->masks.tunnel.ip_dst, 0xff, sizeof wc->masks.tunnel.ip_dst);
79         memset(&wc->masks.tunnel.flags, 0xff, sizeof wc->masks.tunnel.flags);
80         memset(&wc->masks.tunnel.ip_tos, 0xff, sizeof wc->masks.tunnel.ip_tos);
81         memset(&wc->masks.tunnel.ip_ttl, 0xff, sizeof wc->masks.tunnel.ip_ttl);
82     } else if (flow->tunnel.tun_id) {
83         memset(&wc->masks.tunnel.tun_id, 0xff, sizeof wc->masks.tunnel.tun_id);
84     }
85
86     memset(&wc->masks.metadata, 0xff, sizeof wc->masks.metadata);
87     memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
88     memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
89     memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
90     memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
91
92     if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
93         memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
94         memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
95         memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
96     } else if (flow->dl_type == htons(ETH_TYPE_IP) ||
97                (flow->dl_type == htons(ETH_TYPE_ARP)) ||
98                (flow->dl_type == htons(ETH_TYPE_RARP))) {
99         memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
100         memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
101     }
102
103     if (flow->dl_type == htons(ETH_TYPE_ARP) ||
104         flow->dl_type == htons(ETH_TYPE_RARP)) {
105         memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
106         memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
107     }
108
109     if (is_ip_any(flow)) {
110         memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
111         memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
112     }
113
114     if (flow->nw_frag) {
115         memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
116     }
117
118     if (flow->nw_proto == IPPROTO_ICMP || flow->nw_proto == IPPROTO_ICMPV6 ||
119         (flow->tp_src || flow->tp_dst)) {
120         memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
121         memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
122     }
123
124     if (flow->nw_proto == IPPROTO_ICMPV6) {
125         memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
126         memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
127     }
128
129     return;
130 }
131
132 /* Converts the flow in 'flow' into an exact-match match in 'match'. */
133 void
134 match_init_exact(struct match *match, const struct flow *flow)
135 {
136     ovs_be64 tun_id = flow->tunnel.tun_id;
137
138     match->flow = *flow;
139     match->flow.skb_priority = 0;
140     match->flow.skb_mark = 0;
141     memset(&match->flow.tunnel, 0, sizeof match->flow.tunnel);
142     match->flow.tunnel.tun_id = tun_id;
143     flow_wildcards_init_exact(&match->wc);
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_reg(struct match *match, unsigned int reg_idx, uint32_t value)
170 {
171     match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
172 }
173
174 void
175 match_set_reg_masked(struct match *match, unsigned int reg_idx,
176                      uint32_t value, uint32_t mask)
177 {
178     ovs_assert(reg_idx < FLOW_N_REGS);
179     flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
180     match->flow.regs[reg_idx] = value & mask;
181 }
182
183 void
184 match_set_metadata(struct match *match, ovs_be64 metadata)
185 {
186     match_set_metadata_masked(match, metadata, htonll(UINT64_MAX));
187 }
188
189 void
190 match_set_metadata_masked(struct match *match,
191                           ovs_be64 metadata, ovs_be64 mask)
192 {
193     match->wc.masks.metadata = mask;
194     match->flow.metadata = metadata & mask;
195 }
196
197 void
198 match_set_tun_id(struct match *match, ovs_be64 tun_id)
199 {
200     match_set_tun_id_masked(match, tun_id, htonll(UINT64_MAX));
201 }
202
203 void
204 match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
205 {
206     match->wc.masks.tunnel.tun_id = mask;
207     match->flow.tunnel.tun_id = tun_id & mask;
208 }
209
210 void
211 match_set_tun_src(struct match *match, ovs_be32 src)
212 {
213     match_set_tun_src_masked(match, src, htonl(UINT32_MAX));
214 }
215
216 void
217 match_set_tun_src_masked(struct match *match, ovs_be32 src, ovs_be32 mask)
218 {
219     match->wc.masks.tunnel.ip_src = mask;
220     match->flow.tunnel.ip_src = src & mask;
221 }
222
223 void
224 match_set_tun_dst(struct match *match, ovs_be32 dst)
225 {
226     match_set_tun_dst_masked(match, dst, htonl(UINT32_MAX));
227 }
228
229 void
230 match_set_tun_dst_masked(struct match *match, ovs_be32 dst, ovs_be32 mask)
231 {
232     match->wc.masks.tunnel.ip_dst = mask;
233     match->flow.tunnel.ip_dst = dst & mask;
234 }
235
236 void
237 match_set_tun_ttl(struct match *match, uint8_t ttl)
238 {
239     match_set_tun_ttl_masked(match, ttl, UINT8_MAX);
240 }
241
242 void
243 match_set_tun_ttl_masked(struct match *match, uint8_t ttl, uint8_t mask)
244 {
245     match->wc.masks.tunnel.ip_ttl = mask;
246     match->flow.tunnel.ip_ttl = ttl & mask;
247 }
248
249 void
250 match_set_tun_tos(struct match *match, uint8_t tos)
251 {
252     match_set_tun_tos_masked(match, tos, UINT8_MAX);
253 }
254
255 void
256 match_set_tun_tos_masked(struct match *match, uint8_t tos, uint8_t mask)
257 {
258     match->wc.masks.tunnel.ip_tos = mask;
259     match->flow.tunnel.ip_tos = tos & mask;
260 }
261
262 void
263 match_set_tun_flags(struct match *match, uint16_t flags)
264 {
265     match_set_tun_flags_masked(match, flags, UINT16_MAX);
266 }
267
268 void
269 match_set_tun_flags_masked(struct match *match, uint16_t flags, uint16_t mask)
270 {
271     match->wc.masks.tunnel.flags = mask;
272     match->flow.tunnel.flags = flags & mask;
273 }
274
275 void
276 match_set_in_port(struct match *match, uint16_t ofp_port)
277 {
278     match->wc.masks.in_port = UINT16_MAX;
279     match->flow.in_port = ofp_port;
280 }
281
282 void
283 match_set_skb_priority(struct match *match, uint32_t skb_priority)
284 {
285     match->wc.masks.skb_priority = UINT32_MAX;
286     match->flow.skb_priority = skb_priority;
287 }
288
289 void
290 match_set_skb_mark(struct match *match, uint32_t skb_mark)
291 {
292     match->wc.masks.skb_mark = UINT32_MAX;
293     match->flow.skb_mark = skb_mark;
294 }
295
296 void
297 match_set_dl_type(struct match *match, ovs_be16 dl_type)
298 {
299     match->wc.masks.dl_type = htons(UINT16_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 = htons(UINT16_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 void
464 match_set_tp_src(struct match *match, ovs_be16 tp_src)
465 {
466     match_set_tp_src_masked(match, tp_src, htons(UINT16_MAX));
467 }
468
469 void
470 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
471 {
472     match->flow.tp_src = port & mask;
473     match->wc.masks.tp_src = mask;
474 }
475
476 void
477 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
478 {
479     match_set_tp_dst_masked(match, tp_dst, htons(UINT16_MAX));
480 }
481
482 void
483 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
484 {
485     match->flow.tp_dst = port & mask;
486     match->wc.masks.tp_dst = mask;
487 }
488
489 void
490 match_set_nw_proto(struct match *match, uint8_t nw_proto)
491 {
492     match->flow.nw_proto = nw_proto;
493     match->wc.masks.nw_proto = UINT8_MAX;
494 }
495
496 void
497 match_set_nw_src(struct match *match, ovs_be32 nw_src)
498 {
499     match->flow.nw_src = nw_src;
500     match->wc.masks.nw_src = htonl(UINT32_MAX);
501 }
502
503 void
504 match_set_nw_src_masked(struct match *match,
505                         ovs_be32 nw_src, ovs_be32 mask)
506 {
507     match->flow.nw_src = nw_src & mask;
508     match->wc.masks.nw_src = mask;
509 }
510
511 void
512 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
513 {
514     match->flow.nw_dst = nw_dst;
515     match->wc.masks.nw_dst = htonl(UINT32_MAX);
516 }
517
518 void
519 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
520 {
521     match->flow.nw_dst = ip & mask;
522     match->wc.masks.nw_dst = mask;
523 }
524
525 void
526 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
527 {
528     match->wc.masks.nw_tos |= IP_DSCP_MASK;
529     match->flow.nw_tos &= ~IP_DSCP_MASK;
530     match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
531 }
532
533 void
534 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
535 {
536     match->wc.masks.nw_tos |= IP_ECN_MASK;
537     match->flow.nw_tos &= ~IP_ECN_MASK;
538     match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
539 }
540
541 void
542 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
543 {
544     match->wc.masks.nw_ttl = UINT8_MAX;
545     match->flow.nw_ttl = nw_ttl;
546 }
547
548 void
549 match_set_nw_frag(struct match *match, uint8_t nw_frag)
550 {
551     match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
552     match->flow.nw_frag = nw_frag;
553 }
554
555 void
556 match_set_nw_frag_masked(struct match *match,
557                          uint8_t nw_frag, uint8_t mask)
558 {
559     match->flow.nw_frag = nw_frag & mask;
560     match->wc.masks.nw_frag = mask;
561 }
562
563 void
564 match_set_icmp_type(struct match *match, uint8_t icmp_type)
565 {
566     match_set_tp_src(match, htons(icmp_type));
567 }
568
569 void
570 match_set_icmp_code(struct match *match, uint8_t icmp_code)
571 {
572     match_set_tp_dst(match, htons(icmp_code));
573 }
574
575 void
576 match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN])
577 {
578     memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN);
579     memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN);
580 }
581
582 void
583 match_set_arp_sha_masked(struct match *match,
584                          const uint8_t arp_sha[ETH_ADDR_LEN],
585                          const uint8_t mask[ETH_ADDR_LEN])
586 {
587     set_eth_masked(arp_sha, mask,
588                    match->flow.arp_sha, match->wc.masks.arp_sha);
589 }
590
591 void
592 match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN])
593 {
594     memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN);
595     memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN);
596 }
597
598 void
599 match_set_arp_tha_masked(struct match *match,
600                          const uint8_t arp_tha[ETH_ADDR_LEN],
601                          const uint8_t mask[ETH_ADDR_LEN])
602 {
603     set_eth_masked(arp_tha, mask,
604                    match->flow.arp_tha, match->wc.masks.arp_tha);
605 }
606
607 void
608 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
609 {
610     match->flow.ipv6_src = *src;
611     match->wc.masks.ipv6_src = in6addr_exact;
612 }
613
614 void
615 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
616                           const struct in6_addr *mask)
617 {
618     match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
619     match->wc.masks.ipv6_src = *mask;
620 }
621
622 void
623 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
624 {
625     match->flow.ipv6_dst = *dst;
626     match->wc.masks.ipv6_dst = in6addr_exact;
627 }
628
629 void
630 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
631                           const struct in6_addr *mask)
632 {
633     match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
634     match->wc.masks.ipv6_dst = *mask;
635 }
636
637 void
638 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
639 {
640     match->wc.masks.ipv6_label = htonl(UINT32_MAX);
641     match->flow.ipv6_label = ipv6_label;
642 }
643
644
645 void
646 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
647                             ovs_be32 mask)
648 {
649     match->flow.ipv6_label = ipv6_label & mask;
650     match->wc.masks.ipv6_label = mask;
651 }
652
653 void
654 match_set_nd_target(struct match *match, const struct in6_addr *target)
655 {
656     match->flow.nd_target = *target;
657     match->wc.masks.nd_target = in6addr_exact;
658 }
659
660 void
661 match_set_nd_target_masked(struct match *match,
662                            const struct in6_addr *target,
663                            const struct in6_addr *mask)
664 {
665     match->flow.nd_target = ipv6_addr_bitand(target, mask);
666     match->wc.masks.nd_target = *mask;
667 }
668
669 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
670  * values for fixed fields, otherwise false. */
671 bool
672 match_equal(const struct match *a, const struct match *b)
673 {
674     return (flow_wildcards_equal(&a->wc, &b->wc)
675             && flow_equal(&a->flow, &b->flow));
676 }
677
678 /* Returns a hash value for the flow and wildcards in 'match', starting from
679  * 'basis'. */
680 uint32_t
681 match_hash(const struct match *match, uint32_t basis)
682 {
683     return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
684 }
685
686 static void
687 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
688                   const uint8_t mask[6])
689 {
690     if (!eth_addr_is_zero(mask)) {
691         ds_put_format(s, "%s=", name);
692         eth_format_masked(eth, mask, s);
693         ds_put_char(s, ',');
694     }
695 }
696
697 static void
698 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
699                   ovs_be32 netmask)
700 {
701     if (netmask) {
702         ds_put_format(s, "%s=", name);
703         ip_format_masked(ip, netmask, s);
704         ds_put_char(s, ',');
705     }
706 }
707
708 static void
709 format_ipv6_netmask(struct ds *s, const char *name,
710                     const struct in6_addr *addr,
711                     const struct in6_addr *netmask)
712 {
713     if (!ipv6_mask_is_any(netmask)) {
714         ds_put_format(s, "%s=", name);
715         print_ipv6_masked(s, addr, netmask);
716         ds_put_char(s, ',');
717     }
718 }
719
720
721 static void
722 format_be16_masked(struct ds *s, const char *name,
723                    ovs_be16 value, ovs_be16 mask)
724 {
725     if (mask != htons(0)) {
726         ds_put_format(s, "%s=", name);
727         if (mask == htons(UINT16_MAX)) {
728             ds_put_format(s, "%"PRIu16, ntohs(value));
729         } else {
730             ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
731                           ntohs(value), ntohs(mask));
732         }
733         ds_put_char(s, ',');
734     }
735 }
736
737 static void
738 format_flow_tunnel(struct ds *s, const struct match *match)
739 {
740     const struct flow_wildcards *wc = &match->wc;
741     const struct flow_tnl *tnl = &match->flow.tunnel;
742
743     switch (wc->masks.tunnel.tun_id) {
744     case 0:
745         break;
746     case CONSTANT_HTONLL(UINT64_MAX):
747         ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(tnl->tun_id));
748         break;
749     default:
750         ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
751                       ntohll(tnl->tun_id),
752                       ntohll(wc->masks.tunnel.tun_id));
753         break;
754     }
755     format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
756     format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
757
758     if (wc->masks.tunnel.ip_tos) {
759         ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
760     }
761     if (wc->masks.tunnel.ip_ttl) {
762         ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
763     }
764     if (wc->masks.tunnel.flags) {
765         format_flags(s, flow_tun_flag_to_string, tnl->flags, '|');
766         ds_put_char(s, ',');
767     }
768 }
769
770 /* Appends a string representation of 'match' to 's'.  If 'priority' is
771  * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
772 void
773 match_format(const struct match *match, struct ds *s, unsigned int priority)
774 {
775     const struct flow_wildcards *wc = &match->wc;
776     size_t start_len = s->length;
777     const struct flow *f = &match->flow;
778     bool skip_type = false;
779     bool skip_proto = false;
780
781     int i;
782
783     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 18);
784
785     if (priority != OFP_DEFAULT_PRIORITY) {
786         ds_put_format(s, "priority=%u,", priority);
787     }
788
789     if (wc->masks.skb_mark) {
790         ds_put_format(s, "skb_mark=%#"PRIx32",", f->skb_mark);
791     }
792
793     if (wc->masks.skb_priority) {
794         ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority);
795     }
796
797     if (wc->masks.dl_type) {
798         skip_type = true;
799         if (f->dl_type == htons(ETH_TYPE_IP)) {
800             if (wc->masks.nw_proto) {
801                 skip_proto = true;
802                 if (f->nw_proto == IPPROTO_ICMP) {
803                     ds_put_cstr(s, "icmp,");
804                 } else if (f->nw_proto == IPPROTO_TCP) {
805                     ds_put_cstr(s, "tcp,");
806                 } else if (f->nw_proto == IPPROTO_UDP) {
807                     ds_put_cstr(s, "udp,");
808                 } else {
809                     ds_put_cstr(s, "ip,");
810                     skip_proto = false;
811                 }
812             } else {
813                 ds_put_cstr(s, "ip,");
814             }
815         } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
816             if (wc->masks.nw_proto) {
817                 skip_proto = true;
818                 if (f->nw_proto == IPPROTO_ICMPV6) {
819                     ds_put_cstr(s, "icmp6,");
820                 } else if (f->nw_proto == IPPROTO_TCP) {
821                     ds_put_cstr(s, "tcp6,");
822                 } else if (f->nw_proto == IPPROTO_UDP) {
823                     ds_put_cstr(s, "udp6,");
824                 } else {
825                     ds_put_cstr(s, "ipv6,");
826                     skip_proto = false;
827                 }
828             } else {
829                 ds_put_cstr(s, "ipv6,");
830             }
831         } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
832             ds_put_cstr(s, "arp,");
833         } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
834             ds_put_cstr(s, "rarp,");
835         } else {
836             skip_type = false;
837         }
838     }
839     for (i = 0; i < FLOW_N_REGS; i++) {
840         switch (wc->masks.regs[i]) {
841         case 0:
842             break;
843         case UINT32_MAX:
844             ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
845             break;
846         default:
847             ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
848                           i, f->regs[i], wc->masks.regs[i]);
849             break;
850         }
851     }
852
853     format_flow_tunnel(s, match);
854
855     switch (wc->masks.metadata) {
856     case 0:
857         break;
858     case CONSTANT_HTONLL(UINT64_MAX):
859         ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata));
860         break;
861     default:
862         ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",",
863                       ntohll(f->metadata), ntohll(wc->masks.metadata));
864         break;
865     }
866     if (wc->masks.in_port) {
867         ds_put_cstr(s, "in_port=");
868         ofputil_format_port(f->in_port, s);
869         ds_put_char(s, ',');
870     }
871     if (wc->masks.vlan_tci) {
872         ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
873         ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
874         ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
875
876         if (cfi && f->vlan_tci & htons(VLAN_CFI)
877             && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
878             && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
879             && (vid_mask || pcp_mask)) {
880             if (vid_mask) {
881                 ds_put_format(s, "dl_vlan=%"PRIu16",",
882                               vlan_tci_to_vid(f->vlan_tci));
883             }
884             if (pcp_mask) {
885                 ds_put_format(s, "dl_vlan_pcp=%d,",
886                               vlan_tci_to_pcp(f->vlan_tci));
887             }
888         } else if (wc->masks.vlan_tci == htons(0xffff)) {
889             ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
890         } else {
891             ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
892                           ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
893         }
894     }
895     format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
896     format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
897     if (!skip_type && wc->masks.dl_type) {
898         ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
899     }
900     if (f->dl_type == htons(ETH_TYPE_IPV6)) {
901         format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
902         format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
903         if (wc->masks.ipv6_label) {
904             if (wc->masks.ipv6_label == htonl(UINT32_MAX)) {
905                 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
906                               ntohl(f->ipv6_label));
907             } else {
908                 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
909                               ntohl(f->ipv6_label),
910                               ntohl(wc->masks.ipv6_label));
911             }
912         }
913     } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
914                f->dl_type == htons(ETH_TYPE_RARP)) {
915         format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
916         format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
917     } else {
918         format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
919         format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
920     }
921     if (!skip_proto && wc->masks.nw_proto) {
922         if (f->dl_type == htons(ETH_TYPE_ARP) ||
923             f->dl_type == htons(ETH_TYPE_RARP)) {
924             ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
925         } else {
926             ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
927         }
928     }
929     if (f->dl_type == htons(ETH_TYPE_ARP) ||
930         f->dl_type == htons(ETH_TYPE_RARP)) {
931         format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
932         format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
933     }
934     if (wc->masks.nw_tos & IP_DSCP_MASK) {
935         ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
936     }
937     if (wc->masks.nw_tos & IP_ECN_MASK) {
938         ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
939     }
940     if (wc->masks.nw_ttl) {
941         ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
942     }
943     switch (wc->masks.nw_frag) {
944     case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
945         ds_put_format(s, "nw_frag=%s,",
946                       f->nw_frag & FLOW_NW_FRAG_ANY
947                       ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
948                       : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
949         break;
950
951     case FLOW_NW_FRAG_ANY:
952         ds_put_format(s, "nw_frag=%s,",
953                       f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
954         break;
955
956     case FLOW_NW_FRAG_LATER:
957         ds_put_format(s, "nw_frag=%s,",
958                       f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
959         break;
960     }
961     if (f->dl_type == htons(ETH_TYPE_IP) &&
962         f->nw_proto == IPPROTO_ICMP) {
963         format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
964         format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
965     } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
966                f->nw_proto == IPPROTO_ICMPV6) {
967         format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
968         format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
969         format_ipv6_netmask(s, "nd_target", &f->nd_target,
970                             &wc->masks.nd_target);
971         format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
972         format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
973     } else {
974         format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
975         format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
976     }
977
978     if (s->length > start_len && ds_last(s) == ',') {
979         s->length--;
980     }
981 }
982
983 /* Converts 'match' to a string and returns the string.  If 'priority' is
984  * different from OFP_DEFAULT_PRIORITY, includes it in the string.  The caller
985  * must free the string (with free()). */
986 char *
987 match_to_string(const struct match *match, unsigned int priority)
988 {
989     struct ds s = DS_EMPTY_INITIALIZER;
990     match_format(match, &s, priority);
991     return ds_steal_cstr(&s);
992 }
993
994 void
995 match_print(const struct match *match)
996 {
997     char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
998     puts(s);
999     free(s);
1000 }
1001 \f
1002 /* Initializes 'dst' as a copy of 'src'.  The caller must eventually free 'dst'
1003  * with minimatch_destroy(). */
1004 void
1005 minimatch_init(struct minimatch *dst, const struct match *src)
1006 {
1007     miniflow_init(&dst->flow, &src->flow);
1008     minimask_init(&dst->mask, &src->wc);
1009 }
1010
1011 /* Initializes 'dst' as a copy of 'src'.  The caller must eventually free 'dst'
1012  * with minimatch_destroy(). */
1013 void
1014 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1015 {
1016     miniflow_clone(&dst->flow, &src->flow);
1017     minimask_clone(&dst->mask, &src->mask);
1018 }
1019
1020 /* Frees any memory owned by 'match'.  Does not free the storage in which
1021  * 'match' itself resides; the caller is responsible for that. */
1022 void
1023 minimatch_destroy(struct minimatch *match)
1024 {
1025     miniflow_destroy(&match->flow);
1026     minimask_destroy(&match->mask);
1027 }
1028
1029 /* Initializes 'dst' as a copy of 'src'. */
1030 void
1031 minimatch_expand(const struct minimatch *src, struct match *dst)
1032 {
1033     miniflow_expand(&src->flow, &dst->flow);
1034     minimask_expand(&src->mask, &dst->wc);
1035 }
1036
1037 /* Returns true if 'a' and 'b' match the same packets, false otherwise.  */
1038 bool
1039 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1040 {
1041     return (miniflow_equal(&a->flow, &b->flow)
1042             && minimask_equal(&a->mask, &b->mask));
1043 }
1044
1045 /* Returns a hash value for 'match', given 'basis'. */
1046 uint32_t
1047 minimatch_hash(const struct minimatch *match, uint32_t basis)
1048 {
1049     return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
1050 }
1051
1052 /* Appends a string representation of 'match' to 's'.  If 'priority' is
1053  * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1054 void
1055 minimatch_format(const struct minimatch *match, struct ds *s,
1056                  unsigned int priority)
1057 {
1058     struct match megamatch;
1059
1060     minimatch_expand(match, &megamatch);
1061     match_format(&megamatch, s, priority);
1062 }
1063
1064 /* Converts 'match' to a string and returns the string.  If 'priority' is
1065  * different from OFP_DEFAULT_PRIORITY, includes it in the string.  The caller
1066  * must free the string (with free()). */
1067 char *
1068 minimatch_to_string(const struct minimatch *match, unsigned int priority)
1069 {
1070     struct match megamatch;
1071
1072     minimatch_expand(match, &megamatch);
1073     return match_to_string(&megamatch, priority);
1074 }