classifier: Add helpers for setting ethernet addresses
[sliver-openvswitch.git] / lib / classifier.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 "classifier.h"
19 #include <assert.h>
20 #include <errno.h>
21 #include <netinet/in.h>
22 #include "byte-order.h"
23 #include "dynamic-string.h"
24 #include "flow.h"
25 #include "hash.h"
26 #include "odp-util.h"
27 #include "ofp-util.h"
28 #include "packets.h"
29
30 static struct cls_table *find_table(const struct classifier *,
31                                     const struct flow_wildcards *);
32 static struct cls_table *insert_table(struct classifier *,
33                                       const struct flow_wildcards *);
34
35 static void destroy_table(struct classifier *, struct cls_table *);
36
37 static struct cls_rule *find_match(const struct cls_table *,
38                                    const struct flow *);
39 static struct cls_rule *find_equal(struct cls_table *, const struct flow *,
40                                    uint32_t hash);
41 static struct cls_rule *insert_rule(struct cls_table *, struct cls_rule *);
42
43 static bool flow_equal_except(const struct flow *, const struct flow *,
44                                 const struct flow_wildcards *);
45
46 /* Iterates RULE over HEAD and all of the cls_rules on HEAD->list. */
47 #define FOR_EACH_RULE_IN_LIST(RULE, HEAD)                               \
48     for ((RULE) = (HEAD); (RULE) != NULL; (RULE) = next_rule_in_list(RULE))
49 #define FOR_EACH_RULE_IN_LIST_SAFE(RULE, NEXT, HEAD)                    \
50     for ((RULE) = (HEAD);                                               \
51          (RULE) != NULL && ((NEXT) = next_rule_in_list(RULE), true);    \
52          (RULE) = (NEXT))
53
54 static struct cls_rule *next_rule_in_list__(struct cls_rule *);
55 static struct cls_rule *next_rule_in_list(struct cls_rule *);
56
57 /* Converts the flow in 'flow' into a cls_rule in 'rule', with the given
58  * 'wildcards' and 'priority'. */
59 void
60 cls_rule_init(const struct flow *flow, const struct flow_wildcards *wildcards,
61               unsigned int priority, struct cls_rule *rule)
62 {
63     rule->flow = *flow;
64     rule->wc = *wildcards;
65     rule->priority = priority;
66     cls_rule_zero_wildcarded_fields(rule);
67 }
68
69 /* Converts the flow in 'flow' into an exact-match cls_rule in 'rule', with the
70  * given 'priority'.  (For OpenFlow 1.0, exact-match rule are always highest
71  * priority, so 'priority' should be at least 65535.) */
72 void
73 cls_rule_init_exact(const struct flow *flow,
74                     unsigned int priority, struct cls_rule *rule)
75 {
76     rule->flow = *flow;
77     rule->flow.skb_priority = 0;
78     flow_wildcards_init_exact(&rule->wc);
79     rule->priority = priority;
80 }
81
82 /* Initializes 'rule' as a "catch-all" rule that matches every packet, with
83  * priority 'priority'. */
84 void
85 cls_rule_init_catchall(struct cls_rule *rule, unsigned int priority)
86 {
87     memset(&rule->flow, 0, sizeof rule->flow);
88     flow_wildcards_init_catchall(&rule->wc);
89     rule->priority = priority;
90 }
91
92 /* For each bit or field wildcarded in 'rule', sets the corresponding bit or
93  * field in 'flow' to all-0-bits.  It is important to maintain this invariant
94  * in a clr_rule that might be inserted into a classifier.
95  *
96  * It is never necessary to call this function directly for a cls_rule that is
97  * initialized or modified only by cls_rule_*() functions.  It is useful to
98  * restore the invariant in a cls_rule whose 'wc' member is modified by hand.
99  */
100 void
101 cls_rule_zero_wildcarded_fields(struct cls_rule *rule)
102 {
103     flow_zero_wildcards(&rule->flow, &rule->wc);
104 }
105
106 void
107 cls_rule_set_reg(struct cls_rule *rule, unsigned int reg_idx, uint32_t value)
108 {
109     cls_rule_set_reg_masked(rule, reg_idx, value, UINT32_MAX);
110 }
111
112 void
113 cls_rule_set_reg_masked(struct cls_rule *rule, unsigned int reg_idx,
114                         uint32_t value, uint32_t mask)
115 {
116     assert(reg_idx < FLOW_N_REGS);
117     flow_wildcards_set_reg_mask(&rule->wc, reg_idx, mask);
118     rule->flow.regs[reg_idx] = value & mask;
119 }
120
121 void
122 cls_rule_set_metadata(struct cls_rule *rule, ovs_be64 metadata)
123 {
124     cls_rule_set_metadata_masked(rule, metadata, htonll(UINT64_MAX));
125 }
126
127 void
128 cls_rule_set_metadata_masked(struct cls_rule *rule, ovs_be64 metadata,
129                              ovs_be64 mask)
130 {
131     rule->wc.metadata_mask = mask;
132     rule->flow.metadata = metadata & mask;
133 }
134
135 void
136 cls_rule_set_tun_id(struct cls_rule *rule, ovs_be64 tun_id)
137 {
138     cls_rule_set_tun_id_masked(rule, tun_id, htonll(UINT64_MAX));
139 }
140
141 void
142 cls_rule_set_tun_id_masked(struct cls_rule *rule,
143                            ovs_be64 tun_id, ovs_be64 mask)
144 {
145     rule->wc.tun_id_mask = mask;
146     rule->flow.tun_id = tun_id & mask;
147 }
148
149 void
150 cls_rule_set_in_port(struct cls_rule *rule, uint16_t ofp_port)
151 {
152     rule->wc.wildcards &= ~FWW_IN_PORT;
153     rule->flow.in_port = ofp_port;
154 }
155
156 void
157 cls_rule_set_dl_type(struct cls_rule *rule, ovs_be16 dl_type)
158 {
159     rule->wc.wildcards &= ~FWW_DL_TYPE;
160     rule->flow.dl_type = dl_type;
161 }
162
163 /* Modifies 'value_src' so that the Ethernet address must match
164  * 'value_dst' exactly. 'mask_dst' is set to all 1s */
165 static void
166 cls_rule_set_eth(const uint8_t value_src[ETH_ADDR_LEN],
167                  uint8_t value_dst[ETH_ADDR_LEN],
168                  uint8_t mask_dst[ETH_ADDR_LEN])
169 {
170     memcpy(value_dst, value_src, ETH_ADDR_LEN);
171     memset(mask_dst, 0xff, ETH_ADDR_LEN);
172 }
173
174 /* Modifies 'value_src' so that the Ethernet address must match
175  * 'value_src' after each byte is ANDed with the appropriate byte in
176  * 'mask_src'. 'mask_dst' is set to 'mask_src' */
177 static void
178 cls_rule_set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
179                         const uint8_t mask_src[ETH_ADDR_LEN],
180                         uint8_t value_dst[ETH_ADDR_LEN],
181                         uint8_t mask_dst[ETH_ADDR_LEN])
182 {
183     size_t i;
184
185     for (i = 0; i < ETH_ADDR_LEN; i++) {
186         value_dst[i] = value_src[i] & mask_src[i];
187         mask_dst[i] = mask_src[i];
188     }
189 }
190
191 /* Modifies 'rule' so that the source Ethernet address
192  * must match 'dl_src' exactly. */
193 void
194 cls_rule_set_dl_src(struct cls_rule *rule, const uint8_t dl_src[ETH_ADDR_LEN])
195 {
196     cls_rule_set_eth(dl_src, rule->flow.dl_src, rule->wc.dl_src_mask);
197 }
198
199 /* Modifies 'rule' so that the source Ethernet address
200  * must match 'dl_src' after each byte is ANDed with
201  * the appropriate byte in 'mask'. */
202 void
203 cls_rule_set_dl_src_masked(struct cls_rule *rule,
204                            const uint8_t dl_src[ETH_ADDR_LEN],
205                            const uint8_t mask[ETH_ADDR_LEN])
206 {
207     cls_rule_set_eth_masked(dl_src, mask,
208                             rule->flow.dl_src, rule->wc.dl_src_mask);
209 }
210
211 /* Modifies 'rule' so that the destination Ethernet address
212  * must match 'dl_dst' exactly. */
213 void
214 cls_rule_set_dl_dst(struct cls_rule *rule, const uint8_t dl_dst[ETH_ADDR_LEN])
215 {
216     cls_rule_set_eth(dl_dst, rule->flow.dl_dst, rule->wc.dl_dst_mask);
217 }
218
219 /* Modifies 'rule' so that the destination Ethernet address
220  * must match 'dl_src' after each byte is ANDed with
221  * the appropriate byte in 'mask'. */
222 void
223 cls_rule_set_dl_dst_masked(struct cls_rule *rule,
224                            const uint8_t dl_dst[ETH_ADDR_LEN],
225                            const uint8_t mask[ETH_ADDR_LEN])
226 {
227     cls_rule_set_eth_masked(dl_dst, mask,
228                             rule->flow.dl_dst, rule->wc.dl_dst_mask);
229 }
230
231 void
232 cls_rule_set_dl_tci(struct cls_rule *rule, ovs_be16 tci)
233 {
234     cls_rule_set_dl_tci_masked(rule, tci, htons(0xffff));
235 }
236
237 void
238 cls_rule_set_dl_tci_masked(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask)
239 {
240     rule->flow.vlan_tci = tci & mask;
241     rule->wc.vlan_tci_mask = mask;
242 }
243
244 /* Modifies 'rule' so that the VLAN VID is wildcarded.  If the PCP is already
245  * wildcarded, then 'rule' will match a packet regardless of whether it has an
246  * 802.1Q header or not. */
247 void
248 cls_rule_set_any_vid(struct cls_rule *rule)
249 {
250     if (rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK)) {
251         rule->wc.vlan_tci_mask &= ~htons(VLAN_VID_MASK);
252         rule->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
253     } else {
254         cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
255     }
256 }
257
258 /* Modifies 'rule' depending on 'dl_vlan':
259  *
260  *   - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'rule' match only packets
261  *     without an 802.1Q header.
262  *
263  *   - Otherwise, makes 'rule' match only packets with an 802.1Q header whose
264  *     VID equals the low 12 bits of 'dl_vlan'.
265  */
266 void
267 cls_rule_set_dl_vlan(struct cls_rule *rule, ovs_be16 dl_vlan)
268 {
269     flow_set_vlan_vid(&rule->flow, dl_vlan);
270     if (dl_vlan == htons(OFP10_VLAN_NONE)) {
271         rule->wc.vlan_tci_mask = htons(UINT16_MAX);
272     } else {
273         rule->wc.vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
274     }
275 }
276
277 /* Modifies 'rule' so that the VLAN PCP is wildcarded.  If the VID is already
278  * wildcarded, then 'rule' will match a packet regardless of whether it has an
279  * 802.1Q header or not. */
280 void
281 cls_rule_set_any_pcp(struct cls_rule *rule)
282 {
283     if (rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK)) {
284         rule->wc.vlan_tci_mask &= ~htons(VLAN_PCP_MASK);
285         rule->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
286     } else {
287         cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
288     }
289 }
290
291 /* Modifies 'rule' so that it matches only packets with an 802.1Q header whose
292  * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
293 void
294 cls_rule_set_dl_vlan_pcp(struct cls_rule *rule, uint8_t dl_vlan_pcp)
295 {
296     flow_set_vlan_pcp(&rule->flow, dl_vlan_pcp);
297     rule->wc.vlan_tci_mask |= htons(VLAN_CFI | VLAN_PCP_MASK);
298 }
299
300 void
301 cls_rule_set_tp_src(struct cls_rule *rule, ovs_be16 tp_src)
302 {
303     cls_rule_set_tp_src_masked(rule, tp_src, htons(UINT16_MAX));
304 }
305
306 void
307 cls_rule_set_tp_src_masked(struct cls_rule *rule, ovs_be16 port, ovs_be16 mask)
308 {
309     rule->flow.tp_src = port & mask;
310     rule->wc.tp_src_mask = mask;
311 }
312
313 void
314 cls_rule_set_tp_dst(struct cls_rule *rule, ovs_be16 tp_dst)
315 {
316     cls_rule_set_tp_dst_masked(rule, tp_dst, htons(UINT16_MAX));
317 }
318
319 void
320 cls_rule_set_tp_dst_masked(struct cls_rule *rule, ovs_be16 port, ovs_be16 mask)
321 {
322     rule->flow.tp_dst = port & mask;
323     rule->wc.tp_dst_mask = mask;
324 }
325
326 void
327 cls_rule_set_nw_proto(struct cls_rule *rule, uint8_t nw_proto)
328 {
329     rule->wc.wildcards &= ~FWW_NW_PROTO;
330     rule->flow.nw_proto = nw_proto;
331 }
332
333 void
334 cls_rule_set_nw_src(struct cls_rule *rule, ovs_be32 nw_src)
335 {
336     rule->flow.nw_src = nw_src;
337     rule->wc.nw_src_mask = htonl(UINT32_MAX);
338 }
339
340 void
341 cls_rule_set_nw_src_masked(struct cls_rule *rule,
342                            ovs_be32 nw_src, ovs_be32 mask)
343 {
344     rule->flow.nw_src = nw_src & mask;
345     rule->wc.nw_src_mask = mask;
346 }
347
348 void
349 cls_rule_set_nw_dst(struct cls_rule *rule, ovs_be32 nw_dst)
350 {
351     rule->flow.nw_dst = nw_dst;
352     rule->wc.nw_dst_mask = htonl(UINT32_MAX);
353 }
354
355 void
356 cls_rule_set_nw_dst_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
357 {
358     rule->flow.nw_dst = ip & mask;
359     rule->wc.nw_dst_mask = mask;
360 }
361
362 void
363 cls_rule_set_nw_dscp(struct cls_rule *rule, uint8_t nw_dscp)
364 {
365     rule->wc.wildcards &= ~FWW_NW_DSCP;
366     rule->flow.nw_tos &= ~IP_DSCP_MASK;
367     rule->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
368 }
369
370 void
371 cls_rule_set_nw_ecn(struct cls_rule *rule, uint8_t nw_ecn)
372 {
373     rule->wc.wildcards &= ~FWW_NW_ECN;
374     rule->flow.nw_tos &= ~IP_ECN_MASK;
375     rule->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
376 }
377
378 void
379 cls_rule_set_nw_ttl(struct cls_rule *rule, uint8_t nw_ttl)
380 {
381     rule->wc.wildcards &= ~FWW_NW_TTL;
382     rule->flow.nw_ttl = nw_ttl;
383 }
384
385 void
386 cls_rule_set_nw_frag(struct cls_rule *rule, uint8_t nw_frag)
387 {
388     rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
389     rule->flow.nw_frag = nw_frag;
390 }
391
392 void
393 cls_rule_set_nw_frag_masked(struct cls_rule *rule,
394                             uint8_t nw_frag, uint8_t mask)
395 {
396     rule->flow.nw_frag = nw_frag & mask;
397     rule->wc.nw_frag_mask = mask;
398 }
399
400 void
401 cls_rule_set_icmp_type(struct cls_rule *rule, uint8_t icmp_type)
402 {
403     cls_rule_set_tp_src(rule, htons(icmp_type));
404 }
405
406 void
407 cls_rule_set_icmp_code(struct cls_rule *rule, uint8_t icmp_code)
408 {
409     cls_rule_set_tp_dst(rule, htons(icmp_code));
410 }
411
412 void
413 cls_rule_set_arp_sha(struct cls_rule *rule, const uint8_t sha[ETH_ADDR_LEN])
414 {
415     rule->wc.wildcards &= ~FWW_ARP_SHA;
416     memcpy(rule->flow.arp_sha, sha, ETH_ADDR_LEN);
417 }
418
419 void
420 cls_rule_set_arp_tha(struct cls_rule *rule, const uint8_t tha[ETH_ADDR_LEN])
421 {
422     rule->wc.wildcards &= ~FWW_ARP_THA;
423     memcpy(rule->flow.arp_tha, tha, ETH_ADDR_LEN);
424 }
425
426 void
427 cls_rule_set_ipv6_src(struct cls_rule *rule, const struct in6_addr *src)
428 {
429     rule->flow.ipv6_src = *src;
430     rule->wc.ipv6_src_mask = in6addr_exact;
431 }
432
433 void
434 cls_rule_set_ipv6_src_masked(struct cls_rule *rule, const struct in6_addr *src,
435                              const struct in6_addr *mask)
436 {
437     rule->flow.ipv6_src = ipv6_addr_bitand(src, mask);
438     rule->wc.ipv6_src_mask = *mask;
439 }
440
441 void
442 cls_rule_set_ipv6_dst(struct cls_rule *rule, const struct in6_addr *dst)
443 {
444     rule->flow.ipv6_dst = *dst;
445     rule->wc.ipv6_dst_mask = in6addr_exact;
446 }
447
448 void
449 cls_rule_set_ipv6_dst_masked(struct cls_rule *rule, const struct in6_addr *dst,
450                              const struct in6_addr *mask)
451 {
452     rule->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
453     rule->wc.ipv6_dst_mask = *mask;
454 }
455
456 void
457 cls_rule_set_ipv6_label(struct cls_rule *rule, ovs_be32 ipv6_label)
458 {
459     cls_rule_set_ipv6_label_masked(rule, ipv6_label, htonl(UINT32_MAX));
460 }
461
462 void
463 cls_rule_set_ipv6_label_masked(struct cls_rule *rule, ovs_be32 ipv6_label,
464                                ovs_be32 mask)
465 {
466     rule->flow.ipv6_label = ipv6_label & mask;
467     rule->wc.ipv6_label_mask = mask;
468 }
469
470 void
471 cls_rule_set_nd_target(struct cls_rule *rule, const struct in6_addr *target)
472 {
473     rule->flow.nd_target = *target;
474     rule->wc.nd_target_mask = in6addr_exact;
475 }
476
477 void
478 cls_rule_set_nd_target_masked(struct cls_rule *rule,
479                               const struct in6_addr *target,
480                               const struct in6_addr *mask)
481 {
482     rule->flow.nd_target = ipv6_addr_bitand(target, mask);
483     rule->wc.nd_target_mask = *mask;
484 }
485
486 /* Returns true if 'a' and 'b' have the same priority, wildcard the same
487  * fields, and have the same values for fixed fields, otherwise false. */
488 bool
489 cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
490 {
491     return (a->priority == b->priority
492             && flow_wildcards_equal(&a->wc, &b->wc)
493             && flow_equal(&a->flow, &b->flow));
494 }
495
496 /* Returns a hash value for the flow, wildcards, and priority in 'rule',
497  * starting from 'basis'. */
498 uint32_t
499 cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
500 {
501     uint32_t h0 = flow_hash(&rule->flow, basis);
502     uint32_t h1 = flow_wildcards_hash(&rule->wc, h0);
503     return hash_int(rule->priority, h1);
504 }
505
506 static void
507 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
508                   const uint8_t mask[6])
509 {
510     if (!eth_addr_is_zero(mask)) {
511         ds_put_format(s, "%s=", name);
512         eth_format_masked(eth, mask, s);
513         ds_put_char(s, ',');
514     }
515 }
516
517 static void
518 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
519                   ovs_be32 netmask)
520 {
521     if (netmask) {
522         ds_put_format(s, "%s=", name);
523         ip_format_masked(ip, netmask, s);
524         ds_put_char(s, ',');
525     }
526 }
527
528 static void
529 format_ipv6_netmask(struct ds *s, const char *name,
530                     const struct in6_addr *addr,
531                     const struct in6_addr *netmask)
532 {
533     if (!ipv6_mask_is_any(netmask)) {
534         ds_put_format(s, "%s=", name);
535         print_ipv6_masked(s, addr, netmask);
536         ds_put_char(s, ',');
537     }
538 }
539
540
541 static void
542 format_be16_masked(struct ds *s, const char *name,
543                    ovs_be16 value, ovs_be16 mask)
544 {
545     if (mask != htons(0)) {
546         ds_put_format(s, "%s=", name);
547         if (mask == htons(UINT16_MAX)) {
548             ds_put_format(s, "%"PRIu16, ntohs(value));
549         } else {
550             ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
551                           ntohs(value), ntohs(mask));
552         }
553         ds_put_char(s, ',');
554     }
555 }
556
557 void
558 cls_rule_format(const struct cls_rule *rule, struct ds *s)
559 {
560     const struct flow_wildcards *wc = &rule->wc;
561     size_t start_len = s->length;
562     flow_wildcards_t w = wc->wildcards;
563     const struct flow *f = &rule->flow;
564     bool skip_type = false;
565     bool skip_proto = false;
566
567     int i;
568
569     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 13);
570
571     if (rule->priority != OFP_DEFAULT_PRIORITY) {
572         ds_put_format(s, "priority=%d,", rule->priority);
573     }
574
575     if (!(w & FWW_DL_TYPE)) {
576         skip_type = true;
577         if (f->dl_type == htons(ETH_TYPE_IP)) {
578             if (!(w & FWW_NW_PROTO)) {
579                 skip_proto = true;
580                 if (f->nw_proto == IPPROTO_ICMP) {
581                     ds_put_cstr(s, "icmp,");
582                 } else if (f->nw_proto == IPPROTO_TCP) {
583                     ds_put_cstr(s, "tcp,");
584                 } else if (f->nw_proto == IPPROTO_UDP) {
585                     ds_put_cstr(s, "udp,");
586                 } else {
587                     ds_put_cstr(s, "ip,");
588                     skip_proto = false;
589                 }
590             } else {
591                 ds_put_cstr(s, "ip,");
592             }
593         } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
594             if (!(w & FWW_NW_PROTO)) {
595                 skip_proto = true;
596                 if (f->nw_proto == IPPROTO_ICMPV6) {
597                     ds_put_cstr(s, "icmp6,");
598                 } else if (f->nw_proto == IPPROTO_TCP) {
599                     ds_put_cstr(s, "tcp6,");
600                 } else if (f->nw_proto == IPPROTO_UDP) {
601                     ds_put_cstr(s, "udp6,");
602                 } else {
603                     ds_put_cstr(s, "ipv6,");
604                     skip_proto = false;
605                 }
606             } else {
607                 ds_put_cstr(s, "ipv6,");
608             }
609         } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
610             ds_put_cstr(s, "arp,");
611         } else {
612             skip_type = false;
613         }
614     }
615     for (i = 0; i < FLOW_N_REGS; i++) {
616         switch (wc->reg_masks[i]) {
617         case 0:
618             break;
619         case UINT32_MAX:
620             ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
621             break;
622         default:
623             ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
624                           i, f->regs[i], wc->reg_masks[i]);
625             break;
626         }
627     }
628     switch (wc->tun_id_mask) {
629     case 0:
630         break;
631     case CONSTANT_HTONLL(UINT64_MAX):
632         ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(f->tun_id));
633         break;
634     default:
635         ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
636                       ntohll(f->tun_id), ntohll(wc->tun_id_mask));
637         break;
638     }
639     switch (wc->metadata_mask) {
640     case 0:
641         break;
642     case CONSTANT_HTONLL(UINT64_MAX):
643         ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata));
644         break;
645     default:
646         ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",",
647                       ntohll(f->metadata), ntohll(wc->metadata_mask));
648         break;
649     }
650     if (!(w & FWW_IN_PORT)) {
651         ds_put_format(s, "in_port=%"PRIu16",", f->in_port);
652     }
653     if (wc->vlan_tci_mask) {
654         ovs_be16 vid_mask = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
655         ovs_be16 pcp_mask = wc->vlan_tci_mask & htons(VLAN_PCP_MASK);
656         ovs_be16 cfi = wc->vlan_tci_mask & htons(VLAN_CFI);
657
658         if (cfi && f->vlan_tci & htons(VLAN_CFI)
659             && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
660             && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
661             && (vid_mask || pcp_mask)) {
662             if (vid_mask) {
663                 ds_put_format(s, "dl_vlan=%"PRIu16",",
664                               vlan_tci_to_vid(f->vlan_tci));
665             }
666             if (pcp_mask) {
667                 ds_put_format(s, "dl_vlan_pcp=%d,",
668                               vlan_tci_to_pcp(f->vlan_tci));
669             }
670         } else if (wc->vlan_tci_mask == htons(0xffff)) {
671             ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
672         } else {
673             ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
674                           ntohs(f->vlan_tci), ntohs(wc->vlan_tci_mask));
675         }
676     }
677     format_eth_masked(s, "dl_src", f->dl_src, wc->dl_src_mask);
678     format_eth_masked(s, "dl_dst", f->dl_dst, wc->dl_dst_mask);
679     if (!skip_type && !(w & FWW_DL_TYPE)) {
680         ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
681     }
682     if (f->dl_type == htons(ETH_TYPE_IPV6)) {
683         format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->ipv6_src_mask);
684         format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->ipv6_dst_mask);
685         if (wc->ipv6_label_mask) {
686             if (wc->ipv6_label_mask == htonl(UINT32_MAX)) {
687                 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
688                               ntohl(f->ipv6_label));
689             } else {
690                 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
691                               ntohl(f->ipv6_label),
692                               ntohl(wc->ipv6_label_mask));
693             }
694         }
695     } else {
696         format_ip_netmask(s, "nw_src", f->nw_src, wc->nw_src_mask);
697         format_ip_netmask(s, "nw_dst", f->nw_dst, wc->nw_dst_mask);
698     }
699     if (!skip_proto && !(w & FWW_NW_PROTO)) {
700         if (f->dl_type == htons(ETH_TYPE_ARP)) {
701             ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
702         } else {
703             ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
704         }
705     }
706     if (f->dl_type == htons(ETH_TYPE_ARP)) {
707         if (!(w & FWW_ARP_SHA)) {
708             ds_put_format(s, "arp_sha="ETH_ADDR_FMT",",
709                     ETH_ADDR_ARGS(f->arp_sha));
710         }
711         if (!(w & FWW_ARP_THA)) {
712             ds_put_format(s, "arp_tha="ETH_ADDR_FMT",",
713                     ETH_ADDR_ARGS(f->arp_tha));
714         }
715     }
716     if (!(w & FWW_NW_DSCP)) {
717         ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
718     }
719     if (!(w & FWW_NW_ECN)) {
720         ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
721     }
722     if (!(w & FWW_NW_TTL)) {
723         ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
724     }
725     switch (wc->nw_frag_mask) {
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->tp_src_mask);
745         format_be16_masked(s, "icmp_code", f->tp_dst, wc->tp_dst_mask);
746     } else if (f->nw_proto == IPPROTO_ICMPV6) {
747         format_be16_masked(s, "icmp_type", f->tp_src, wc->tp_src_mask);
748         format_be16_masked(s, "icmp_code", f->tp_dst, wc->tp_dst_mask);
749         format_ipv6_netmask(s, "nd_target", &f->nd_target,
750                             &wc->nd_target_mask);
751         if (!(w & FWW_ARP_SHA)) {
752             ds_put_format(s, "nd_sll="ETH_ADDR_FMT",",
753                     ETH_ADDR_ARGS(f->arp_sha));
754         }
755         if (!(w & FWW_ARP_THA)) {
756             ds_put_format(s, "nd_tll="ETH_ADDR_FMT",",
757                     ETH_ADDR_ARGS(f->arp_tha));
758         }
759    } else {
760         format_be16_masked(s, "tp_src", f->tp_src, wc->tp_src_mask);
761         format_be16_masked(s, "tp_dst", f->tp_dst, wc->tp_dst_mask);
762     }
763
764     if (s->length > start_len && ds_last(s) == ',') {
765         s->length--;
766     }
767 }
768
769 /* Converts 'rule' to a string and returns the string.  The caller must free
770  * the string (with free()). */
771 char *
772 cls_rule_to_string(const struct cls_rule *rule)
773 {
774     struct ds s = DS_EMPTY_INITIALIZER;
775     cls_rule_format(rule, &s);
776     return ds_steal_cstr(&s);
777 }
778
779 void
780 cls_rule_print(const struct cls_rule *rule)
781 {
782     char *s = cls_rule_to_string(rule);
783     puts(s);
784     free(s);
785 }
786 \f
787 /* Initializes 'cls' as a classifier that initially contains no classification
788  * rules. */
789 void
790 classifier_init(struct classifier *cls)
791 {
792     cls->n_rules = 0;
793     hmap_init(&cls->tables);
794 }
795
796 /* Destroys 'cls'.  Rules within 'cls', if any, are not freed; this is the
797  * caller's responsibility. */
798 void
799 classifier_destroy(struct classifier *cls)
800 {
801     if (cls) {
802         struct cls_table *table, *next_table;
803
804         HMAP_FOR_EACH_SAFE (table, next_table, hmap_node, &cls->tables) {
805             hmap_destroy(&table->rules);
806             hmap_remove(&cls->tables, &table->hmap_node);
807             free(table);
808         }
809         hmap_destroy(&cls->tables);
810     }
811 }
812
813 /* Returns true if 'cls' contains no classification rules, false otherwise. */
814 bool
815 classifier_is_empty(const struct classifier *cls)
816 {
817     return cls->n_rules == 0;
818 }
819
820 /* Returns the number of rules in 'classifier'. */
821 int
822 classifier_count(const struct classifier *cls)
823 {
824     return cls->n_rules;
825 }
826
827 /* Inserts 'rule' into 'cls'.  Until 'rule' is removed from 'cls', the caller
828  * must not modify or free it.
829  *
830  * If 'cls' already contains an identical rule (including wildcards, values of
831  * fixed fields, and priority), replaces the old rule by 'rule' and returns the
832  * rule that was replaced.  The caller takes ownership of the returned rule and
833  * is thus responsible for freeing it, etc., as necessary.
834  *
835  * Returns NULL if 'cls' does not contain a rule with an identical key, after
836  * inserting the new rule.  In this case, no rules are displaced by the new
837  * rule, even rules that cannot have any effect because the new rule matches a
838  * superset of their flows and has higher priority. */
839 struct cls_rule *
840 classifier_replace(struct classifier *cls, struct cls_rule *rule)
841 {
842     struct cls_rule *old_rule;
843     struct cls_table *table;
844
845     table = find_table(cls, &rule->wc);
846     if (!table) {
847         table = insert_table(cls, &rule->wc);
848     }
849
850     old_rule = insert_rule(table, rule);
851     if (!old_rule) {
852         table->n_table_rules++;
853         cls->n_rules++;
854     }
855     return old_rule;
856 }
857
858 /* Inserts 'rule' into 'cls'.  Until 'rule' is removed from 'cls', the caller
859  * must not modify or free it.
860  *
861  * 'cls' must not contain an identical rule (including wildcards, values of
862  * fixed fields, and priority).  Use classifier_find_rule_exactly() to find
863  * such a rule. */
864 void
865 classifier_insert(struct classifier *cls, struct cls_rule *rule)
866 {
867     struct cls_rule *displaced_rule = classifier_replace(cls, rule);
868     assert(!displaced_rule);
869 }
870
871 /* Removes 'rule' from 'cls'.  It is the caller's responsibility to free
872  * 'rule', if this is desirable. */
873 void
874 classifier_remove(struct classifier *cls, struct cls_rule *rule)
875 {
876     struct cls_rule *head;
877     struct cls_table *table;
878
879     table = find_table(cls, &rule->wc);
880     head = find_equal(table, &rule->flow, rule->hmap_node.hash);
881     if (head != rule) {
882         list_remove(&rule->list);
883     } else if (list_is_empty(&rule->list)) {
884         hmap_remove(&table->rules, &rule->hmap_node);
885     } else {
886         struct cls_rule *next = CONTAINER_OF(rule->list.next,
887                                              struct cls_rule, list);
888
889         list_remove(&rule->list);
890         hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
891     }
892
893     if (--table->n_table_rules == 0) {
894         destroy_table(cls, table);
895     }
896
897     cls->n_rules--;
898 }
899
900 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
901  * Returns a null pointer if no rules in 'cls' match 'flow'.  If multiple rules
902  * of equal priority match 'flow', returns one arbitrarily. */
903 struct cls_rule *
904 classifier_lookup(const struct classifier *cls, const struct flow *flow)
905 {
906     struct cls_table *table;
907     struct cls_rule *best;
908
909     best = NULL;
910     HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
911         struct cls_rule *rule = find_match(table, flow);
912         if (rule && (!best || rule->priority > best->priority)) {
913             best = rule;
914         }
915     }
916     return best;
917 }
918
919 /* Finds and returns a rule in 'cls' with exactly the same priority and
920  * matching criteria as 'target'.  Returns a null pointer if 'cls' doesn't
921  * contain an exact match. */
922 struct cls_rule *
923 classifier_find_rule_exactly(const struct classifier *cls,
924                              const struct cls_rule *target)
925 {
926     struct cls_rule *head, *rule;
927     struct cls_table *table;
928
929     table = find_table(cls, &target->wc);
930     if (!table) {
931         return NULL;
932     }
933
934     head = find_equal(table, &target->flow, flow_hash(&target->flow, 0));
935     FOR_EACH_RULE_IN_LIST (rule, head) {
936         if (target->priority >= rule->priority) {
937             return target->priority == rule->priority ? rule : NULL;
938         }
939     }
940     return NULL;
941 }
942
943 /* Checks if 'target' would overlap any other rule in 'cls'.  Two rules are
944  * considered to overlap if both rules have the same priority and a packet
945  * could match both. */
946 bool
947 classifier_rule_overlaps(const struct classifier *cls,
948                          const struct cls_rule *target)
949 {
950     struct cls_table *table;
951
952     HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
953         struct flow_wildcards wc;
954         struct cls_rule *head;
955
956         flow_wildcards_combine(&wc, &target->wc, &table->wc);
957         HMAP_FOR_EACH (head, hmap_node, &table->rules) {
958             struct cls_rule *rule;
959
960             FOR_EACH_RULE_IN_LIST (rule, head) {
961                 if (rule->priority == target->priority
962                     && flow_equal_except(&target->flow, &rule->flow, &wc)) {
963                     return true;
964                 }
965             }
966         }
967     }
968
969     return false;
970 }
971
972 /* Returns true if 'rule' exactly matches 'criteria' or if 'rule' is more
973  * specific than 'criteria'.  That is, 'rule' matches 'criteria' and this
974  * function returns true if, for every field:
975  *
976  *   - 'criteria' and 'rule' specify the same (non-wildcarded) value for the
977  *     field, or
978  *
979  *   - 'criteria' wildcards the field,
980  *
981  * Conversely, 'rule' does not match 'criteria' and this function returns false
982  * if, for at least one field:
983  *
984  *   - 'criteria' and 'rule' specify different values for the field, or
985  *
986  *   - 'criteria' specifies a value for the field but 'rule' wildcards it.
987  *
988  * Equivalently, the truth table for whether a field matches is:
989  *
990  *                                     rule
991  *
992  *                   c         wildcard    exact
993  *                   r        +---------+---------+
994  *                   i   wild |   yes   |   yes   |
995  *                   t   card |         |         |
996  *                   e        +---------+---------+
997  *                   r  exact |    no   |if values|
998  *                   i        |         |are equal|
999  *                   a        +---------+---------+
1000  *
1001  * This is the matching rule used by OpenFlow 1.0 non-strict OFPT_FLOW_MOD
1002  * commands and by OpenFlow 1.0 aggregate and flow stats.
1003  *
1004  * Ignores rule->priority and criteria->priority. */
1005 bool
1006 cls_rule_is_loose_match(const struct cls_rule *rule,
1007                         const struct cls_rule *criteria)
1008 {
1009     return (!flow_wildcards_has_extra(&rule->wc, &criteria->wc)
1010             && flow_equal_except(&rule->flow, &criteria->flow, &criteria->wc));
1011 }
1012 \f
1013 /* Iteration. */
1014
1015 static bool
1016 rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
1017 {
1018     return (!target
1019             || flow_equal_except(&rule->flow, &target->flow, &target->wc));
1020 }
1021
1022 static struct cls_rule *
1023 search_table(const struct cls_table *table, const struct cls_rule *target)
1024 {
1025     if (!target || !flow_wildcards_has_extra(&table->wc, &target->wc)) {
1026         struct cls_rule *rule;
1027
1028         HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
1029             if (rule_matches(rule, target)) {
1030                 return rule;
1031             }
1032         }
1033     }
1034     return NULL;
1035 }
1036
1037 /* Initializes 'cursor' for iterating through rules in 'cls':
1038  *
1039  *     - If 'target' is null, the cursor will visit every rule in 'cls'.
1040  *
1041  *     - If 'target' is nonnull, the cursor will visit each 'rule' in 'cls'
1042  *       such that cls_rule_is_loose_match(rule, target) returns true.
1043  *
1044  * Ignores target->priority. */
1045 void
1046 cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
1047                 const struct cls_rule *target)
1048 {
1049     cursor->cls = cls;
1050     cursor->target = target;
1051 }
1052
1053 /* Returns the first matching cls_rule in 'cursor''s iteration, or a null
1054  * pointer if there are no matches. */
1055 struct cls_rule *
1056 cls_cursor_first(struct cls_cursor *cursor)
1057 {
1058     struct cls_table *table;
1059
1060     HMAP_FOR_EACH (table, hmap_node, &cursor->cls->tables) {
1061         struct cls_rule *rule = search_table(table, cursor->target);
1062         if (rule) {
1063             cursor->table = table;
1064             return rule;
1065         }
1066     }
1067
1068     return NULL;
1069 }
1070
1071 /* Returns the next matching cls_rule in 'cursor''s iteration, or a null
1072  * pointer if there are no more matches. */
1073 struct cls_rule *
1074 cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
1075 {
1076     const struct cls_table *table;
1077     struct cls_rule *next;
1078
1079     next = next_rule_in_list__(rule);
1080     if (next->priority < rule->priority) {
1081         return next;
1082     }
1083
1084     /* 'next' is the head of the list, that is, the rule that is included in
1085      * the table's hmap.  (This is important when the classifier contains rules
1086      * that differ only in priority.) */
1087     rule = next;
1088     HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
1089         if (rule_matches(rule, cursor->target)) {
1090             return rule;
1091         }
1092     }
1093
1094     table = cursor->table;
1095     HMAP_FOR_EACH_CONTINUE (table, hmap_node, &cursor->cls->tables) {
1096         rule = search_table(table, cursor->target);
1097         if (rule) {
1098             cursor->table = table;
1099             return rule;
1100         }
1101     }
1102
1103     return NULL;
1104 }
1105 \f
1106 static struct cls_table *
1107 find_table(const struct classifier *cls, const struct flow_wildcards *wc)
1108 {
1109     struct cls_table *table;
1110
1111     HMAP_FOR_EACH_IN_BUCKET (table, hmap_node, flow_wildcards_hash(wc, 0),
1112                              &cls->tables) {
1113         if (flow_wildcards_equal(wc, &table->wc)) {
1114             return table;
1115         }
1116     }
1117     return NULL;
1118 }
1119
1120 static struct cls_table *
1121 insert_table(struct classifier *cls, const struct flow_wildcards *wc)
1122 {
1123     struct cls_table *table;
1124
1125     table = xzalloc(sizeof *table);
1126     hmap_init(&table->rules);
1127     table->wc = *wc;
1128     table->is_catchall = flow_wildcards_is_catchall(&table->wc);
1129     hmap_insert(&cls->tables, &table->hmap_node, flow_wildcards_hash(wc, 0));
1130
1131     return table;
1132 }
1133
1134 static void
1135 destroy_table(struct classifier *cls, struct cls_table *table)
1136 {
1137     hmap_remove(&cls->tables, &table->hmap_node);
1138     hmap_destroy(&table->rules);
1139     free(table);
1140 }
1141
1142 static struct cls_rule *
1143 find_match(const struct cls_table *table, const struct flow *flow)
1144 {
1145     struct cls_rule *rule;
1146
1147     if (table->is_catchall) {
1148         HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
1149             return rule;
1150         }
1151     } else {
1152         struct flow f;
1153
1154         f = *flow;
1155         flow_zero_wildcards(&f, &table->wc);
1156         HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, flow_hash(&f, 0),
1157                                  &table->rules) {
1158             if (flow_equal(&f, &rule->flow)) {
1159                 return rule;
1160             }
1161         }
1162     }
1163
1164     return NULL;
1165 }
1166
1167 static struct cls_rule *
1168 find_equal(struct cls_table *table, const struct flow *flow, uint32_t hash)
1169 {
1170     struct cls_rule *head;
1171
1172     HMAP_FOR_EACH_WITH_HASH (head, hmap_node, hash, &table->rules) {
1173         if (flow_equal(&head->flow, flow)) {
1174             return head;
1175         }
1176     }
1177     return NULL;
1178 }
1179
1180 static struct cls_rule *
1181 insert_rule(struct cls_table *table, struct cls_rule *new)
1182 {
1183     struct cls_rule *head;
1184
1185     new->hmap_node.hash = flow_hash(&new->flow, 0);
1186
1187     head = find_equal(table, &new->flow, new->hmap_node.hash);
1188     if (!head) {
1189         hmap_insert(&table->rules, &new->hmap_node, new->hmap_node.hash);
1190         list_init(&new->list);
1191         return NULL;
1192     } else {
1193         /* Scan the list for the insertion point that will keep the list in
1194          * order of decreasing priority. */
1195         struct cls_rule *rule;
1196         FOR_EACH_RULE_IN_LIST (rule, head) {
1197             if (new->priority >= rule->priority) {
1198                 if (rule == head) {
1199                     /* 'new' is the new highest-priority flow in the list. */
1200                     hmap_replace(&table->rules,
1201                                  &rule->hmap_node, &new->hmap_node);
1202                 }
1203
1204                 if (new->priority == rule->priority) {
1205                     list_replace(&new->list, &rule->list);
1206                     return rule;
1207                 } else {
1208                     list_insert(&rule->list, &new->list);
1209                     return NULL;
1210                 }
1211             }
1212         }
1213
1214         /* Insert 'new' at the end of the list. */
1215         list_push_back(&head->list, &new->list);
1216         return NULL;
1217     }
1218 }
1219
1220 static struct cls_rule *
1221 next_rule_in_list__(struct cls_rule *rule)
1222 {
1223     struct cls_rule *next = OBJECT_CONTAINING(rule->list.next, next, list);
1224     return next;
1225 }
1226
1227 static struct cls_rule *
1228 next_rule_in_list(struct cls_rule *rule)
1229 {
1230     struct cls_rule *next = next_rule_in_list__(rule);
1231     return next->priority < rule->priority ? next : NULL;
1232 }
1233
1234 static bool
1235 ipv6_equal_except(const struct in6_addr *a, const struct in6_addr *b,
1236                   const struct in6_addr *mask)
1237 {
1238     int i;
1239
1240 #ifdef s6_addr32
1241     for (i=0; i<4; i++) {
1242         if ((a->s6_addr32[i] ^ b->s6_addr32[i]) & mask->s6_addr32[i]) {
1243             return false;
1244         }
1245     }
1246 #else
1247     for (i=0; i<16; i++) {
1248         if ((a->s6_addr[i] ^ b->s6_addr[i]) & mask->s6_addr[i]) {
1249             return false;
1250         }
1251     }
1252 #endif
1253
1254     return true;
1255 }
1256
1257
1258 static bool
1259 flow_equal_except(const struct flow *a, const struct flow *b,
1260                   const struct flow_wildcards *wildcards)
1261 {
1262     const flow_wildcards_t wc = wildcards->wildcards;
1263     int i;
1264
1265     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 13);
1266
1267     for (i = 0; i < FLOW_N_REGS; i++) {
1268         if ((a->regs[i] ^ b->regs[i]) & wildcards->reg_masks[i]) {
1269             return false;
1270         }
1271     }
1272
1273     return (!((a->tun_id ^ b->tun_id) & wildcards->tun_id_mask)
1274             && !((a->metadata ^ b->metadata) & wildcards->metadata_mask)
1275             && !((a->nw_src ^ b->nw_src) & wildcards->nw_src_mask)
1276             && !((a->nw_dst ^ b->nw_dst) & wildcards->nw_dst_mask)
1277             && (wc & FWW_IN_PORT || a->in_port == b->in_port)
1278             && !((a->vlan_tci ^ b->vlan_tci) & wildcards->vlan_tci_mask)
1279             && (wc & FWW_DL_TYPE || a->dl_type == b->dl_type)
1280             && !((a->tp_src ^ b->tp_src) & wildcards->tp_src_mask)
1281             && !((a->tp_dst ^ b->tp_dst) & wildcards->tp_dst_mask)
1282             && eth_addr_equal_except(a->dl_src, b->dl_src,
1283                                      wildcards->dl_src_mask)
1284             && eth_addr_equal_except(a->dl_dst, b->dl_dst,
1285                                      wildcards->dl_dst_mask)
1286             && (wc & FWW_NW_PROTO || a->nw_proto == b->nw_proto)
1287             && (wc & FWW_NW_TTL || a->nw_ttl == b->nw_ttl)
1288             && (wc & FWW_NW_DSCP || !((a->nw_tos ^ b->nw_tos) & IP_DSCP_MASK))
1289             && (wc & FWW_NW_ECN || !((a->nw_tos ^ b->nw_tos) & IP_ECN_MASK))
1290             && !((a->nw_frag ^ b->nw_frag) & wildcards->nw_frag_mask)
1291             && (wc & FWW_ARP_SHA || eth_addr_equals(a->arp_sha, b->arp_sha))
1292             && (wc & FWW_ARP_THA || eth_addr_equals(a->arp_tha, b->arp_tha))
1293             && !((a->ipv6_label ^ b->ipv6_label) & wildcards->ipv6_label_mask)
1294             && ipv6_equal_except(&a->ipv6_src, &b->ipv6_src,
1295                     &wildcards->ipv6_src_mask)
1296             && ipv6_equal_except(&a->ipv6_dst, &b->ipv6_dst,
1297                     &wildcards->ipv6_dst_mask)
1298             && ipv6_equal_except(&a->nd_target, &b->nd_target,
1299                                  &wildcards->nd_target_mask));
1300 }