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