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