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