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