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