2 * Copyright (c) 2009, 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "classifier.h"
21 #include <netinet/in.h>
22 #include "dynamic-string.h"
26 const struct cls_field cls_fields[CLS_N_FIELDS + 1] = {
27 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
28 { offsetof(flow_t, MEMBER), \
29 sizeof ((flow_t *)0)->MEMBER, \
34 { sizeof(flow_t), 0, 0, "exact" },
37 static uint32_t hash_fields(const flow_t *, int table_idx);
38 static bool equal_fields(const flow_t *, const flow_t *, int table_idx);
40 static int table_idx_from_wildcards(uint32_t wildcards);
41 static struct cls_rule *table_insert(struct hmap *, struct cls_rule *);
42 static struct cls_rule *insert_exact_rule(struct classifier *,
44 static struct cls_bucket *find_bucket(struct hmap *, size_t hash,
45 const struct cls_rule *);
46 static struct cls_rule *search_table(const struct hmap *table, int field_idx,
47 const struct cls_rule *);
48 static struct cls_rule *search_exact_table(const struct classifier *,
49 size_t hash, const flow_t *);
50 static bool rules_match_1wild(const struct cls_rule *fixed,
51 const struct cls_rule *wild, int field_idx);
52 static bool rules_match_2wild(const struct cls_rule *wild1,
53 const struct cls_rule *wild2, int field_idx);
55 /* Converts the flow in 'flow' into a cls_rule in 'rule', with the given
56 * 'wildcards' and 'priority'.*/
58 cls_rule_from_flow(struct cls_rule *rule, const flow_t *flow,
59 uint32_t wildcards, unsigned int priority)
62 flow_wildcards_init(&rule->wc, wildcards);
63 rule->priority = priority;
64 rule->table_idx = table_idx_from_wildcards(rule->wc.wildcards);
67 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
70 cls_rule_from_match(struct cls_rule *rule, const struct ofp_match *match,
71 unsigned int priority)
74 flow_from_match(&rule->flow, &wildcards, match);
75 flow_wildcards_init(&rule->wc, wildcards);
76 rule->priority = rule->wc.wildcards ? priority : UINT16_MAX;
77 rule->table_idx = table_idx_from_wildcards(rule->wc.wildcards);
80 /* Converts 'rule' to a string and returns the string. The caller must free
81 * the string (with free()). */
83 cls_rule_to_string(const struct cls_rule *rule)
85 struct ds s = DS_EMPTY_INITIALIZER;
86 ds_put_format(&s, "wildcards=%x priority=%u ",
87 rule->wc.wildcards, rule->priority);
88 flow_format(&s, &rule->flow);
92 /* Prints cls_rule 'rule', for debugging.
94 * (The output could be improved and expanded, but this was good enough to
95 * debug the classifier.) */
97 cls_rule_print(const struct cls_rule *rule)
99 printf("wildcards=%x priority=%u ", rule->wc.wildcards, rule->priority);
100 flow_print(stdout, &rule->flow);
104 /* Adjusts pointers around 'old', which must be in classifier 'cls', to
105 * compensate for it having been moved in memory to 'new' (e.g. due to
108 * This function cannot be realized in all possible flow classifier
109 * implementations, so we will probably have to change the interface if we
110 * change the implementation. Shouldn't be a big deal though. */
112 cls_rule_moved(struct classifier *cls, struct cls_rule *old,
113 struct cls_rule *new)
116 if (new->wc.wildcards) {
117 list_moved(&new->node.list);
119 hmap_node_moved(&cls->exact_table,
120 &old->node.hmap, &new->node.hmap);
125 /* Replaces 'old', which must be in classifier 'cls', by 'new' (e.g. due to
126 * realloc()); that is, after calling this function 'new' will be in 'cls' in
129 * 'new' and 'old' must be exactly the same: wildcard the same fields, have the
130 * same fixed values for non-wildcarded fields, and have the same priority.
132 * The caller takes ownership of 'old' and is thus responsible for freeing it,
133 * etc., as necessary.
135 * This function cannot be realized in all possible flow classifier
136 * implementations, so we will probably have to change the interface if we
137 * change the implementation. Shouldn't be a big deal though. */
139 cls_rule_replace(struct classifier *cls, const struct cls_rule *old,
140 struct cls_rule *new)
143 assert(old->wc.wildcards == new->wc.wildcards);
144 assert(old->priority == new->priority);
146 if (new->wc.wildcards) {
147 list_replace(&new->node.list, &old->node.list);
149 hmap_replace(&cls->exact_table, &old->node.hmap, &new->node.hmap);
153 /* Initializes 'cls' as a classifier that initially contains no classification
156 classifier_init(struct classifier *cls)
161 for (i = 0; i < ARRAY_SIZE(cls->tables); i++) {
162 hmap_init(&cls->tables[i]);
164 hmap_init(&cls->exact_table);
167 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
168 * caller's responsibility. */
170 classifier_destroy(struct classifier *cls)
173 struct cls_bucket *bucket, *next_bucket;
176 for (tbl = &cls->tables[0]; tbl < &cls->tables[CLS_N_FIELDS]; tbl++) {
177 HMAP_FOR_EACH_SAFE (bucket, next_bucket,
178 struct cls_bucket, hmap_node, tbl) {
183 hmap_destroy(&cls->exact_table);
187 /* Returns true if 'cls' does not contain any classification rules, false
190 classifier_is_empty(const struct classifier *cls)
192 return cls->n_rules == 0;
195 /* Returns the number of rules in 'classifier'. */
197 classifier_count(const struct classifier *cls)
202 /* Returns the number of rules in 'classifier' that have no wildcards. */
204 classifier_count_exact(const struct classifier *cls)
206 return hmap_count(&cls->exact_table);
209 /* Inserts 'rule' into 'cls'. Transfers ownership of 'rule' to 'cls'.
211 * If 'cls' already contains an identical rule (including wildcards, values of
212 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
213 * rule that was replaced. The caller takes ownership of the returned rule and
214 * is thus responsible for freeing it, etc., as necessary.
216 * Returns NULL if 'cls' does not contain a rule with an identical key, after
217 * inserting the new rule. In this case, no rules are displaced by the new
218 * rule, even rules that cannot have any effect because the new rule matches a
219 * superset of their flows and has higher priority. */
221 classifier_insert(struct classifier *cls, struct cls_rule *rule)
223 struct cls_rule *old;
224 assert((rule->wc.wildcards == 0) == (rule->table_idx == CLS_F_IDX_EXACT));
225 old = (rule->wc.wildcards
226 ? table_insert(&cls->tables[rule->table_idx], rule)
227 : insert_exact_rule(cls, rule));
234 /* Inserts 'rule' into 'cls'. Transfers ownership of 'rule' to 'cls'.
236 * 'rule' must be an exact-match rule (rule->wc.wildcards must be 0) and 'cls'
237 * must not contain any rule with an identical key. */
239 classifier_insert_exact(struct classifier *cls, struct cls_rule *rule)
241 hmap_insert(&cls->exact_table, &rule->node.hmap,
242 flow_hash(&rule->flow, 0));
246 /* Removes 'rule' from 'cls'. It is caller's responsibility to free 'rule', if
247 * this is desirable. */
249 classifier_remove(struct classifier *cls, struct cls_rule *rule)
251 if (rule->wc.wildcards) {
252 /* Remove 'rule' from bucket. If that empties the bucket, remove the
253 * bucket from its table. */
254 struct hmap *table = &cls->tables[rule->table_idx];
255 struct list *rules = list_remove(&rule->node.list);
256 if (list_is_empty(rules)) {
257 /* This code is a little tricky. list_remove() returns the list
258 * element just after the one removed. Since the list is now
259 * empty, this will be the address of the 'rules' member of the
260 * bucket that was just emptied, so pointer arithmetic (via
261 * CONTAINER_OF) can find that bucket. */
262 struct cls_bucket *bucket;
263 bucket = CONTAINER_OF(rules, struct cls_bucket, rules);
264 hmap_remove(table, &bucket->hmap_node);
268 /* Remove 'rule' from cls->exact_table. */
269 hmap_remove(&cls->exact_table, &rule->node.hmap);
274 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
275 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
276 * of equal priority match 'flow', returns one arbitrarily.
278 * (When multiple rules of equal priority happen to fall into the same bucket,
279 * rules added more recently take priority over rules added less recently, but
280 * this is subject to change and should not be depended upon.) */
282 classifier_lookup(const struct classifier *cls, const flow_t *flow)
284 struct cls_rule *rule = classifier_lookup_exact(cls, flow);
286 rule = classifier_lookup_wild(cls, flow);
292 classifier_lookup_exact(const struct classifier *cls, const flow_t *flow)
294 return (!hmap_is_empty(&cls->exact_table)
295 ? search_exact_table(cls, flow_hash(flow, 0), flow)
300 classifier_lookup_wild(const struct classifier *cls, const flow_t *flow)
302 struct cls_rule *best = NULL;
303 if (cls->n_rules > hmap_count(&cls->exact_table)) {
304 struct cls_rule target;
307 cls_rule_from_flow(&target, flow, 0, 0);
308 for (i = 0; i < CLS_N_FIELDS; i++) {
309 struct cls_rule *rule = search_table(&cls->tables[i], i, &target);
310 if (rule && (!best || rule->priority > best->priority)) {
319 classifier_find_rule_exactly(const struct classifier *cls,
320 const flow_t *target, uint32_t wildcards,
321 unsigned int priority)
323 struct cls_bucket *bucket;
328 /* Ignores 'priority'. */
329 return search_exact_table(cls, flow_hash(target, 0), target);
332 assert(wildcards == (wildcards & OFPFW_ALL));
333 table_idx = table_idx_from_wildcards(wildcards);
334 hash = hash_fields(target, table_idx);
335 HMAP_FOR_EACH_WITH_HASH (bucket, struct cls_bucket, hmap_node, hash,
336 &cls->tables[table_idx]) {
337 if (equal_fields(&bucket->fixed, target, table_idx)) {
338 struct cls_rule *pos;
339 LIST_FOR_EACH (pos, struct cls_rule, node.list, &bucket->rules) {
340 if (pos->priority < priority) {
342 } else if (pos->priority == priority &&
343 pos->wc.wildcards == wildcards &&
344 flow_equal(target, &pos->flow)) {
353 /* Checks if the flow defined by 'target' with 'wildcards' at 'priority'
354 * overlaps with any other rule at the same priority in the classifier.
355 * Two rules are considered overlapping if a packet could match both. */
357 classifier_rule_overlaps(const struct classifier *cls,
358 const flow_t *target, uint32_t wildcards,
359 unsigned int priority)
361 struct cls_rule target_rule;
362 const struct hmap *tbl;
365 return search_exact_table(cls, flow_hash(target, 0), target) ?
369 cls_rule_from_flow(&target_rule, target, wildcards, priority);
371 for (tbl = &cls->tables[0]; tbl < &cls->tables[CLS_N_FIELDS]; tbl++) {
372 struct cls_bucket *bucket;
374 HMAP_FOR_EACH (bucket, struct cls_bucket, hmap_node, tbl) {
375 struct cls_rule *rule;
377 LIST_FOR_EACH (rule, struct cls_rule, node.list,
379 if (rule->priority == priority
380 && rules_match_2wild(rule, &target_rule, 0)) {
390 /* Ignores target->priority.
392 * 'callback' is allowed to delete the rule that is passed as its argument, but
393 * it must not delete (or move) any other rules in 'cls' that are in the same
394 * table as the argument rule. Two rules are in the same table if their
395 * cls_rule structs have the same table_idx; as a special case, a rule with
396 * wildcards and an exact-match rule will never be in the same table. */
398 classifier_for_each_match(const struct classifier *cls,
399 const struct cls_rule *target,
400 int include, cls_cb_func *callback, void *aux)
402 if (include & CLS_INC_WILD) {
403 const struct hmap *table;
405 for (table = &cls->tables[0]; table < &cls->tables[CLS_N_FIELDS];
407 struct cls_bucket *bucket, *next_bucket;
409 HMAP_FOR_EACH_SAFE (bucket, next_bucket,
410 struct cls_bucket, hmap_node, table) {
411 /* XXX there is a bit of room for optimization here based on
412 * rejecting entire buckets on their fixed fields, but it will
413 * only be worthwhile for big buckets (which we hope we won't
414 * get anyway, but...) */
415 struct cls_rule *prev_rule, *rule;
417 /* We can't just use LIST_FOR_EACH_SAFE here because, if the
418 * callback deletes the last rule in the bucket, then the
419 * bucket itself will be destroyed. The bucket contains the
420 * list head so that's a use-after-free error. */
422 LIST_FOR_EACH (rule, struct cls_rule, node.list,
424 if (rules_match_1wild(rule, target, 0)) {
426 callback(prev_rule, aux);
432 callback(prev_rule, aux);
438 if (include & CLS_INC_EXACT) {
439 if (target->wc.wildcards) {
440 struct cls_rule *rule, *next_rule;
442 HMAP_FOR_EACH_SAFE (rule, next_rule, struct cls_rule, node.hmap,
444 if (rules_match_1wild(rule, target, 0)) {
449 /* Optimization: there can be at most one match in the exact
451 size_t hash = flow_hash(&target->flow, 0);
452 struct cls_rule *rule = search_exact_table(cls, hash,
461 /* 'callback' is allowed to delete the rule that is passed as its argument, but
462 * it must not delete (or move) any other rules in 'cls' that are in the same
463 * table as the argument rule. Two rules are in the same table if their
464 * cls_rule structs have the same table_idx; as a special case, a rule with
465 * wildcards and an exact-match rule will never be in the same table. */
467 classifier_for_each(const struct classifier *cls, int include,
468 void (*callback)(struct cls_rule *, void *aux),
471 if (include & CLS_INC_WILD) {
472 const struct hmap *tbl;
474 for (tbl = &cls->tables[0]; tbl < &cls->tables[CLS_N_FIELDS]; tbl++) {
475 struct cls_bucket *bucket, *next_bucket;
477 HMAP_FOR_EACH_SAFE (bucket, next_bucket,
478 struct cls_bucket, hmap_node, tbl) {
479 struct cls_rule *prev_rule, *rule;
481 /* We can't just use LIST_FOR_EACH_SAFE here because, if the
482 * callback deletes the last rule in the bucket, then the
483 * bucket itself will be destroyed. The bucket contains the
484 * list head so that's a use-after-free error. */
486 LIST_FOR_EACH (rule, struct cls_rule, node.list,
489 callback(prev_rule, aux);
494 callback(prev_rule, aux);
500 if (include & CLS_INC_EXACT) {
501 struct cls_rule *rule, *next_rule;
503 HMAP_FOR_EACH_SAFE (rule, next_rule,
504 struct cls_rule, node.hmap, &cls->exact_table) {
510 static struct cls_bucket *create_bucket(struct hmap *, size_t hash,
511 const flow_t *fixed);
512 static struct cls_rule *bucket_insert(struct cls_bucket *, struct cls_rule *);
514 static inline bool equal_bytes(const void *, const void *, size_t n);
516 /* Returns a hash computed across the fields in 'flow' whose field indexes
517 * (CLS_F_IDX_*) are less than 'table_idx'. (If 'table_idx' is
518 * CLS_F_IDX_EXACT, hashes all the fields in 'flow'). */
520 hash_fields(const flow_t *flow, int table_idx)
522 /* I just know I'm going to hell for writing code this way.
524 * GCC generates pretty good code here, with only a single taken
525 * conditional jump per execution. Now the question is, would we be better
526 * off marking this function ALWAYS_INLINE and writing a wrapper that
527 * switches on the value of 'table_idx' to get rid of all the conditional
528 * jumps entirely (except for one in the wrapper)? Honestly I really,
529 * really hope that it doesn't matter in practice.
531 * We could do better by calculating hashes incrementally, instead of
532 * starting over from the top each time. But that would be even uglier. */
537 a = b = c = 0xdeadbeef + table_idx;
540 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
541 if (table_idx == CLS_F_IDX_##NAME) { \
543 memset((uint8_t *) tmp + n, 0, sizeof tmp - n); \
546 const size_t size = sizeof flow->MEMBER; \
547 const uint8_t *p1 = (const uint8_t *) &flow->MEMBER; \
548 const size_t p1_size = MIN(sizeof tmp - n, size); \
549 const uint8_t *p2 = p1 + p1_size; \
550 const size_t p2_size = size - p1_size; \
552 /* Append to 'tmp' as much data as will fit. */ \
553 memcpy((uint8_t *) tmp + n, p1, p1_size); \
556 /* If 'tmp' is full, mix. */ \
557 if (n == sizeof tmp) { \
565 /* Append to 'tmp' any data that didn't fit. */ \
566 memcpy(tmp, p2, p2_size); \
580 /* Compares the fields in 'a' and 'b' whose field indexes (CLS_F_IDX_*) are
581 * less than 'table_idx'. (If 'table_idx' is CLS_F_IDX_EXACT, compares all the
582 * fields in 'a' and 'b').
584 * Returns true if all the compared fields are equal, false otherwise. */
586 equal_fields(const flow_t *a, const flow_t *b, int table_idx)
588 /* XXX The generated code could be better here. */
589 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
590 if (table_idx == CLS_F_IDX_##NAME) { \
592 } else if (!equal_bytes(&a->MEMBER, &b->MEMBER, sizeof a->MEMBER)) { \
602 table_idx_from_wildcards(uint32_t wildcards)
605 return CLS_F_IDX_EXACT;
607 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
608 if (wildcards & WILDCARDS) { \
609 return CLS_F_IDX_##NAME; \
616 /* Inserts 'rule' into 'table'. Returns the rule, if any, that was displaced
617 * in favor of 'rule'. */
618 static struct cls_rule *
619 table_insert(struct hmap *table, struct cls_rule *rule)
621 struct cls_bucket *bucket;
624 hash = hash_fields(&rule->flow, rule->table_idx);
625 bucket = find_bucket(table, hash, rule);
627 bucket = create_bucket(table, hash, &rule->flow);
630 return bucket_insert(bucket, rule);
633 /* Inserts 'rule' into 'bucket', given that 'field' is the first wildcarded
636 * Returns the rule, if any, that was displaced in favor of 'rule'. */
637 static struct cls_rule *
638 bucket_insert(struct cls_bucket *bucket, struct cls_rule *rule)
640 struct cls_rule *pos;
641 LIST_FOR_EACH (pos, struct cls_rule, node.list, &bucket->rules) {
642 if (pos->priority == rule->priority) {
643 if (pos->wc.wildcards == rule->wc.wildcards
644 && rules_match_1wild(pos, rule, rule->table_idx))
646 list_replace(&rule->node.list, &pos->node.list);
649 } else if (pos->priority < rule->priority) {
653 list_insert(&pos->node.list, &rule->node.list);
657 static struct cls_rule *
658 insert_exact_rule(struct classifier *cls, struct cls_rule *rule)
660 struct cls_rule *old_rule;
663 hash = flow_hash(&rule->flow, 0);
664 old_rule = search_exact_table(cls, hash, &rule->flow);
666 hmap_remove(&cls->exact_table, &old_rule->node.hmap);
668 hmap_insert(&cls->exact_table, &rule->node.hmap, hash);
672 /* Returns the bucket in 'table' that has the given 'hash' and the same fields
673 * as 'rule->flow' (up to 'rule->table_idx'), or a null pointer if no bucket
675 static struct cls_bucket *
676 find_bucket(struct hmap *table, size_t hash, const struct cls_rule *rule)
678 struct cls_bucket *bucket;
679 HMAP_FOR_EACH_WITH_HASH (bucket, struct cls_bucket, hmap_node, hash,
681 if (equal_fields(&bucket->fixed, &rule->flow, rule->table_idx)) {
688 /* Creates a bucket and inserts it in 'table' with the given 'hash' and 'fixed'
689 * values. Returns the new bucket. */
690 static struct cls_bucket *
691 create_bucket(struct hmap *table, size_t hash, const flow_t *fixed)
693 struct cls_bucket *bucket = xmalloc(sizeof *bucket);
694 list_init(&bucket->rules);
695 bucket->fixed = *fixed;
696 hmap_insert(table, &bucket->hmap_node, hash);
700 /* Returns true if the 'n' bytes in 'a' and 'b' are equal, false otherwise. */
701 static inline bool ALWAYS_INLINE
702 equal_bytes(const void *a, const void *b, size_t n)
705 /* For some reason GCC generates stupid code for memcmp() of small
706 * constant integer lengths. Help it out.
708 * This function is always inlined, and it is always called with 'n' as a
709 * compile-time constant, so the switch statement gets optimized out and
710 * this whole function just expands to an instruction or two. */
713 return *(uint8_t *) a == *(uint8_t *) b;
716 return *(uint16_t *) a == *(uint16_t *) b;
719 return *(uint32_t *) a == *(uint32_t *) b;
722 return (*(uint32_t *) a == *(uint32_t *) b
723 && ((uint16_t *) a)[2] == ((uint16_t *) b)[2]);
729 /* I hope GCC is smarter on your platform. */
730 return !memcmp(a, b, n);
734 /* Returns the 32-bit unsigned integer at 'p'. */
735 static inline uint32_t
736 read_uint32(const void *p)
738 /* GCC optimizes this into a single machine instruction on x86. */
740 memcpy(&x, p, sizeof x);
744 /* Compares the specified field in 'a' and 'b'. Returns true if the fields are
745 * equal, or if the ofp_match wildcard bits in 'wildcards' are set such that
746 * non-equal values may be ignored. 'nw_src_mask' and 'nw_dst_mask' must be
747 * those that would be set for 'wildcards' by cls_rule_set_masks().
749 * The compared field is the one with wildcard bit or bits 'field_wc', offset
750 * 'rule_ofs' within cls_rule's "fields" member, and length 'len', in bytes. */
751 static inline bool ALWAYS_INLINE
752 field_matches(const flow_t *a_, const flow_t *b_,
753 uint32_t wildcards, uint32_t nw_src_mask, uint32_t nw_dst_mask,
754 uint32_t field_wc, int ofs, int len)
756 /* This function is always inlined, and it is always called with 'field_wc'
757 * as a compile-time constant, so the "if" conditionals here generate no
759 const void *a = (const uint8_t *) a_ + ofs;
760 const void *b = (const uint8_t *) b_ + ofs;
761 if (!(field_wc & (field_wc - 1))) {
762 /* Handle all the single-bit wildcard cases. */
763 return wildcards & field_wc || equal_bytes(a, b, len);
764 } else if (field_wc == OFPFW_NW_SRC_MASK ||
765 field_wc == OFPFW_NW_DST_MASK) {
766 uint32_t a_ip = read_uint32(a);
767 uint32_t b_ip = read_uint32(b);
768 uint32_t mask = (field_wc == OFPFW_NW_SRC_MASK
769 ? nw_src_mask : nw_dst_mask);
770 return ((a_ip ^ b_ip) & mask) == 0;
776 /* Returns true if 'a' and 'b' match, ignoring fields for which the wildcards
777 * in 'wildcards' are set. 'nw_src_mask' and 'nw_dst_mask' must be those that
778 * would be set for 'wildcards' by cls_rule_set_masks(). 'field_idx' is the
779 * index of the first field to be compared; fields before 'field_idx' are
780 * assumed to match. (Always returns true if 'field_idx' is CLS_N_FIELDS.) */
782 rules_match(const struct cls_rule *a, const struct cls_rule *b,
783 uint32_t wildcards, uint32_t nw_src_mask, uint32_t nw_dst_mask,
786 /* This is related to Duff's device (see
787 * http://en.wikipedia.org/wiki/Duff's_device). */
789 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
790 case CLS_F_IDX_##NAME: \
791 if (!field_matches(&a->flow, &b->flow, \
792 wildcards, nw_src_mask, nw_dst_mask, \
793 WILDCARDS, offsetof(flow_t, MEMBER), \
794 sizeof a->flow.MEMBER)) { \
804 /* Returns true if 'fixed' and 'wild' match. All fields in 'fixed' must have
805 * fixed values; 'wild' may contain wildcards.
807 * 'field_idx' is the index of the first field to be compared; fields before
808 * 'field_idx' are assumed to match. Always returns true if 'field_idx' is
811 rules_match_1wild(const struct cls_rule *fixed, const struct cls_rule *wild,
814 return rules_match(fixed, wild, wild->wc.wildcards, wild->wc.nw_src_mask,
815 wild->wc.nw_dst_mask, field_idx);
818 /* Returns true if 'wild1' and 'wild2' match, that is, if their fields
819 * are equal modulo wildcards in 'wild1' or 'wild2'.
821 * 'field_idx' is the index of the first field to be compared; fields before
822 * 'field_idx' are assumed to match. Always returns true if 'field_idx' is
825 rules_match_2wild(const struct cls_rule *wild1, const struct cls_rule *wild2,
828 return rules_match(wild1, wild2,
829 wild1->wc.wildcards | wild2->wc.wildcards,
830 wild1->wc.nw_src_mask & wild2->wc.nw_src_mask,
831 wild1->wc.nw_dst_mask & wild2->wc.nw_dst_mask,
835 /* Searches 'bucket' for a rule that matches 'target'. Returns the
836 * highest-priority match, if one is found, or a null pointer if there is no
839 * 'field_idx' must be the index of the first wildcarded field in 'bucket'. */
840 static struct cls_rule *
841 search_bucket(struct cls_bucket *bucket, int field_idx,
842 const struct cls_rule *target)
844 struct cls_rule *pos;
846 if (!equal_fields(&bucket->fixed, &target->flow, field_idx)) {
850 LIST_FOR_EACH (pos, struct cls_rule, node.list, &bucket->rules) {
851 if (rules_match_1wild(target, pos, field_idx)) {
858 /* Searches 'table' for a rule that matches 'target'. Returns the
859 * highest-priority match, if one is found, or a null pointer if there is no
862 * 'field_idx' must be the index of the first wildcarded field in 'table'. */
863 static struct cls_rule *
864 search_table(const struct hmap *table, int field_idx,
865 const struct cls_rule *target)
867 struct cls_bucket *bucket;
869 switch (hmap_count(table)) {
870 /* In these special cases there's no need to hash. */
874 bucket = CONTAINER_OF(hmap_first(table), struct cls_bucket, hmap_node);
875 return search_bucket(bucket, field_idx, target);
878 HMAP_FOR_EACH_WITH_HASH (bucket, struct cls_bucket, hmap_node,
879 hash_fields(&target->flow, field_idx), table) {
880 struct cls_rule *rule = search_bucket(bucket, field_idx, target);
888 static struct cls_rule *
889 search_exact_table(const struct classifier *cls, size_t hash,
890 const flow_t *target)
892 struct cls_rule *rule;
894 HMAP_FOR_EACH_WITH_HASH (rule, struct cls_rule, node.hmap,
895 hash, &cls->exact_table) {
896 if (flow_equal(&rule->flow, target)) {