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