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