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