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