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