2 * Copyright (c) 2009 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>
25 const struct cls_field cls_fields[CLS_N_FIELDS + 1] = {
26 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
27 { offsetof(flow_t, MEMBER), \
28 sizeof ((flow_t *)0)->MEMBER, \
33 { sizeof(flow_t), 0, 0, "exact" },
36 static uint32_t hash_fields(const flow_t *, int table_idx);
37 static bool equal_fields(const flow_t *, const flow_t *, int table_idx);
39 static int table_idx_from_wildcards(uint32_t wildcards);
40 static struct cls_rule *table_insert(struct hmap *, struct cls_rule *);
41 static struct cls_rule *insert_exact_rule(struct classifier *,
43 static struct cls_bucket *find_bucket(struct hmap *, size_t hash,
44 const struct cls_rule *);
45 static struct cls_rule *search_table(const struct hmap *table, int field_idx,
46 const struct cls_rule *);
47 static struct cls_rule *search_exact_table(const struct classifier *,
48 size_t hash, const flow_t *);
49 static bool rules_match_1wild(const struct cls_rule *fixed,
50 const struct cls_rule *wild, int field_idx);
52 /* Converts the flow in 'flow' into a cls_rule in 'rule', with the given
53 * 'wildcards' and 'priority'.*/
55 cls_rule_from_flow(struct cls_rule *rule, const flow_t *flow,
56 uint32_t wildcards, unsigned int priority)
59 flow_wildcards_init(&rule->wc, wildcards);
60 rule->priority = priority;
61 rule->table_idx = table_idx_from_wildcards(rule->wc.wildcards);
64 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
67 cls_rule_from_match(struct cls_rule *rule, const struct ofp_match *match,
68 unsigned int priority)
71 flow_from_match(&rule->flow, &wildcards, match);
72 flow_wildcards_init(&rule->wc, wildcards);
73 rule->priority = rule->wc.wildcards ? priority : UINT16_MAX;
74 rule->table_idx = table_idx_from_wildcards(rule->wc.wildcards);
77 /* Prints cls_rule 'rule', for debugging.
79 * (The output could be improved and expanded, but this was good enough to
80 * debug the classifier.) */
82 cls_rule_print(const struct cls_rule *rule)
84 printf("wildcards=%x priority=%u ", rule->wc.wildcards, rule->priority);
85 flow_print(stdout, &rule->flow);
89 /* Adjusts pointers around 'old', which must be in classifier 'cls', to
90 * compensate for it having been moved in memory to 'new' (e.g. due to
93 * This function cannot be realized in all possible flow classifier
94 * implementations, so we will probably have to change the interface if we
95 * change the implementation. Shouldn't be a big deal though. */
97 cls_rule_moved(struct classifier *cls, struct cls_rule *old,
101 if (new->wc.wildcards) {
102 list_moved(&new->node.list);
104 hmap_moved(&cls->exact_table, &old->node.hmap, &new->node.hmap);
109 /* Replaces 'old', which must be in classifier 'cls', by 'new' (e.g. due to
110 * realloc()); that is, after calling this function 'new' will be in 'cls' in
113 * 'new' and 'old' must be exactly the same: wildcard the same fields, have the
114 * same fixed values for non-wildcarded fields, and have the same priority.
116 * The caller takes ownership of 'old' and is thus responsible for freeing it,
117 * etc., as necessary.
119 * This function cannot be realized in all possible flow classifier
120 * implementations, so we will probably have to change the interface if we
121 * change the implementation. Shouldn't be a big deal though. */
123 cls_rule_replace(struct classifier *cls, const struct cls_rule *old,
124 struct cls_rule *new)
127 assert(old->wc.wildcards == new->wc.wildcards);
128 assert(old->priority == new->priority);
130 if (new->wc.wildcards) {
131 list_replace(&new->node.list, &old->node.list);
133 hmap_replace(&cls->exact_table, &old->node.hmap, &new->node.hmap);
137 /* Initializes 'cls' as a classifier that initially contains no classification
140 classifier_init(struct classifier *cls)
145 for (i = 0; i < ARRAY_SIZE(cls->tables); i++) {
146 hmap_init(&cls->tables[i]);
148 hmap_init(&cls->exact_table);
151 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
152 * caller's responsibility. */
154 classifier_destroy(struct classifier *cls)
157 struct cls_bucket *bucket, *next_bucket;
160 for (tbl = &cls->tables[0]; tbl < &cls->tables[CLS_N_FIELDS]; tbl++) {
161 HMAP_FOR_EACH_SAFE (bucket, next_bucket,
162 struct cls_bucket, hmap_node, tbl) {
167 hmap_destroy(&cls->exact_table);
171 /* Returns true if 'cls' does not contain any classification rules, false
174 classifier_is_empty(const struct classifier *cls)
176 return cls->n_rules == 0;
179 /* Returns the number of rules in 'classifier'. */
181 classifier_count(const struct classifier *cls)
186 /* Returns the number of rules in 'classifier' that have no wildcards. */
188 classifier_count_exact(const struct classifier *cls)
190 return hmap_count(&cls->exact_table);
193 /* Inserts 'rule' into 'cls'. Transfers ownership of 'rule' to 'cls'.
195 * If 'cls' already contains an identical rule (including wildcards, values of
196 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
197 * rule that was replaced. The caller takes ownership of the returned rule and
198 * is thus responsible for freeing it, etc., as necessary.
200 * Returns NULL if 'cls' does not contain a rule with an identical key, after
201 * inserting the new rule. In this case, no rules are displaced by the new
202 * rule, even rules that cannot have any effect because the new rule matches a
203 * superset of their flows and has higher priority. */
205 classifier_insert(struct classifier *cls, struct cls_rule *rule)
207 struct cls_rule *old;
208 assert((rule->wc.wildcards == 0) == (rule->table_idx == CLS_F_IDX_EXACT));
209 old = (rule->wc.wildcards
210 ? table_insert(&cls->tables[rule->table_idx], rule)
211 : insert_exact_rule(cls, rule));
218 /* Inserts 'rule' into 'cls'. Transfers ownership of 'rule' to 'cls'.
220 * 'rule' must be an exact-match rule (rule->wc.wildcards must be 0) and 'cls'
221 * must not contain any rule with an identical key. */
223 classifier_insert_exact(struct classifier *cls, struct cls_rule *rule)
225 hmap_insert(&cls->exact_table, &rule->node.hmap,
226 flow_hash(&rule->flow, 0));
230 /* Removes 'rule' from 'cls'. It is caller's responsibility to free 'rule', if
231 * this is desirable. */
233 classifier_remove(struct classifier *cls, struct cls_rule *rule)
235 if (rule->wc.wildcards) {
236 /* Remove 'rule' from bucket. If that empties the bucket, remove the
237 * bucket from its table. */
238 struct hmap *table = &cls->tables[rule->table_idx];
239 struct list *rules = list_remove(&rule->node.list);
240 if (list_is_empty(rules)) {
241 /* This code is a little tricky. list_remove() returns the list
242 * element just after the one removed. Since the list is now
243 * empty, this will be the address of the 'rules' member of the
244 * bucket that was just emptied, so pointer arithmetic (via
245 * CONTAINER_OF) can find that bucket. */
246 struct cls_bucket *bucket;
247 bucket = CONTAINER_OF(rules, struct cls_bucket, rules);
248 hmap_remove(table, &bucket->hmap_node);
252 /* Remove 'rule' from cls->exact_table. */
253 hmap_remove(&cls->exact_table, &rule->node.hmap);
258 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
259 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
260 * of equal priority match 'flow', returns one arbitrarily.
262 * (When multiple rules of equal priority happen to fall into the same bucket,
263 * rules added more recently take priority over rules added less recently, but
264 * this is subject to change and should not be depended upon.) */
266 classifier_lookup(const struct classifier *cls, const flow_t *flow)
268 struct cls_rule *rule = classifier_lookup_exact(cls, flow);
270 rule = classifier_lookup_wild(cls, flow);
276 classifier_lookup_exact(const struct classifier *cls, const flow_t *flow)
278 return (!hmap_is_empty(&cls->exact_table)
279 ? search_exact_table(cls, flow_hash(flow, 0), flow)
284 classifier_lookup_wild(const struct classifier *cls, const flow_t *flow)
286 struct cls_rule *best = NULL;
287 if (cls->n_rules > hmap_count(&cls->exact_table)) {
288 struct cls_rule target;
291 cls_rule_from_flow(&target, flow, 0, 0);
292 for (i = 0; i < CLS_N_FIELDS; i++) {
293 struct cls_rule *rule = search_table(&cls->tables[i], i, &target);
294 if (rule && (!best || rule->priority > best->priority)) {
303 classifier_find_rule_exactly(const struct classifier *cls,
304 const flow_t *target, uint32_t wildcards,
305 unsigned int priority)
307 struct cls_bucket *bucket;
312 /* Ignores 'priority'. */
313 return search_exact_table(cls, flow_hash(target, 0), target);
316 assert(wildcards == (wildcards & OFPFW_ALL));
317 table_idx = table_idx_from_wildcards(wildcards);
318 hash = hash_fields(target, table_idx);
319 HMAP_FOR_EACH_WITH_HASH (bucket, struct cls_bucket, hmap_node, hash,
320 &cls->tables[table_idx]) {
321 if (equal_fields(&bucket->fixed, target, table_idx)) {
322 struct cls_rule *pos;
323 LIST_FOR_EACH (pos, struct cls_rule, node.list, &bucket->rules) {
324 if (pos->priority < priority) {
326 } else if (pos->priority == priority &&
327 pos->wc.wildcards == wildcards &&
328 flow_equal(target, &pos->flow)) {
337 /* Ignores target->priority.
339 * 'callback' is allowed to delete the rule that is passed as its argument, but
340 * it must not delete (or move) any other rules in 'cls' that are in the same
341 * table as the argument rule. Two rules are in the same table if their
342 * cls_rule structs have the same table_idx; as a special case, a rule with
343 * wildcards and an exact-match rule will never be in the same table. */
345 classifier_for_each_match(const struct classifier *cls,
346 const struct cls_rule *target,
347 int include, cls_cb_func *callback, void *aux)
349 if (include & CLS_INC_WILD) {
350 const struct hmap *table;
352 for (table = &cls->tables[0]; table < &cls->tables[CLS_N_FIELDS];
354 struct cls_bucket *bucket, *next_bucket;
356 HMAP_FOR_EACH_SAFE (bucket, next_bucket,
357 struct cls_bucket, hmap_node, table) {
358 /* XXX there is a bit of room for optimization here based on
359 * rejecting entire buckets on their fixed fields, but it will
360 * only be worthwhile for big buckets (which we hope we won't
361 * get anyway, but...) */
362 struct cls_rule *prev_rule, *rule;
364 /* We can't just use LIST_FOR_EACH_SAFE here because, if the
365 * callback deletes the last rule in the bucket, then the
366 * bucket itself will be destroyed. The bucket contains the
367 * list head so that's a use-after-free error. */
369 LIST_FOR_EACH (rule, struct cls_rule, node.list,
371 if (rules_match_1wild(rule, target, 0)) {
373 callback(prev_rule, aux);
379 callback(prev_rule, aux);
385 if (include & CLS_INC_EXACT) {
386 if (target->wc.wildcards) {
387 struct cls_rule *rule, *next_rule;
389 HMAP_FOR_EACH_SAFE (rule, next_rule, struct cls_rule, node.hmap,
391 if (rules_match_1wild(rule, target, 0)) {
396 /* Optimization: there can be at most one match in the exact
398 size_t hash = flow_hash(&target->flow, 0);
399 struct cls_rule *rule = search_exact_table(cls, hash,
408 /* 'callback' is allowed to delete the rule that is passed as its argument, but
409 * it must not delete (or move) any other rules in 'cls' that are in the same
410 * table as the argument rule. Two rules are in the same table if their
411 * cls_rule structs have the same table_idx; as a special case, a rule with
412 * wildcards and an exact-match rule will never be in the same table. */
414 classifier_for_each(const struct classifier *cls, int include,
415 void (*callback)(struct cls_rule *, void *aux),
418 if (include & CLS_INC_WILD) {
419 const struct hmap *tbl;
421 for (tbl = &cls->tables[0]; tbl < &cls->tables[CLS_N_FIELDS]; tbl++) {
422 struct cls_bucket *bucket, *next_bucket;
424 HMAP_FOR_EACH_SAFE (bucket, next_bucket,
425 struct cls_bucket, hmap_node, tbl) {
426 struct cls_rule *prev_rule, *rule;
428 /* We can't just use LIST_FOR_EACH_SAFE here because, if the
429 * callback deletes the last rule in the bucket, then the
430 * bucket itself will be destroyed. The bucket contains the
431 * list head so that's a use-after-free error. */
433 LIST_FOR_EACH (rule, struct cls_rule, node.list,
436 callback(prev_rule, aux);
441 callback(prev_rule, aux);
447 if (include & CLS_INC_EXACT) {
448 struct cls_rule *rule, *next_rule;
450 HMAP_FOR_EACH_SAFE (rule, next_rule,
451 struct cls_rule, node.hmap, &cls->exact_table) {
457 static struct cls_bucket *create_bucket(struct hmap *, size_t hash,
458 const flow_t *fixed);
459 static struct cls_rule *bucket_insert(struct cls_bucket *, struct cls_rule *);
461 static inline bool equal_bytes(const void *, const void *, size_t n);
463 /* Returns a hash computed across the fields in 'flow' whose field indexes
464 * (CLS_F_IDX_*) are less than 'table_idx'. (If 'table_idx' is
465 * CLS_F_IDX_EXACT, hashes all the fields in 'flow'). */
467 hash_fields(const flow_t *flow, int table_idx)
469 /* I just know I'm going to hell for writing code this way.
471 * GCC generates pretty good code here, with only a single taken
472 * conditional jump per execution. Now the question is, would we be better
473 * off marking this function ALWAYS_INLINE and writing a wrapper that
474 * switches on the value of 'table_idx' to get rid of all the conditional
475 * jumps entirely (except for one in the wrapper)? Honestly I really,
476 * really hope that it doesn't matter in practice.
478 * We could do better by calculating hashes incrementally, instead of
479 * starting over from the top each time. But that would be even uglier. */
484 a = b = c = 0xdeadbeef + table_idx;
487 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
488 if (table_idx == CLS_F_IDX_##NAME) { \
490 memset((uint8_t *) tmp + n, 0, sizeof tmp - n); \
493 const size_t size = sizeof flow->MEMBER; \
494 const uint8_t *p1 = (const uint8_t *) &flow->MEMBER; \
495 const size_t p1_size = MIN(sizeof tmp - n, size); \
496 const uint8_t *p2 = p1 + p1_size; \
497 const size_t p2_size = size - p1_size; \
499 /* Append to 'tmp' as much data as will fit. */ \
500 memcpy((uint8_t *) tmp + n, p1, p1_size); \
503 /* If 'tmp' is full, mix. */ \
504 if (n == sizeof tmp) { \
512 /* Append to 'tmp' any data that didn't fit. */ \
513 memcpy(tmp, p2, p2_size); \
527 /* Compares the fields in 'a' and 'b' whose field indexes (CLS_F_IDX_*) are
528 * less than 'table_idx'. (If 'table_idx' is CLS_F_IDX_EXACT, compares all the
529 * fields in 'a' and 'b').
531 * Returns true if all the compared fields are equal, false otherwise. */
533 equal_fields(const flow_t *a, const flow_t *b, int table_idx)
535 /* XXX The generated code could be better here. */
536 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
537 if (table_idx == CLS_F_IDX_##NAME) { \
539 } else if (!equal_bytes(&a->MEMBER, &b->MEMBER, sizeof a->MEMBER)) { \
549 table_idx_from_wildcards(uint32_t wildcards)
552 return CLS_F_IDX_EXACT;
554 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
555 if (wildcards & WILDCARDS) { \
556 return CLS_F_IDX_##NAME; \
563 /* Inserts 'rule' into 'table'. Returns the rule, if any, that was displaced
564 * in favor of 'rule'. */
565 static struct cls_rule *
566 table_insert(struct hmap *table, struct cls_rule *rule)
568 struct cls_bucket *bucket;
571 hash = hash_fields(&rule->flow, rule->table_idx);
572 bucket = find_bucket(table, hash, rule);
574 bucket = create_bucket(table, hash, &rule->flow);
577 return bucket_insert(bucket, rule);
580 /* Inserts 'rule' into 'bucket', given that 'field' is the first wildcarded
583 * Returns the rule, if any, that was displaced in favor of 'rule'. */
584 static struct cls_rule *
585 bucket_insert(struct cls_bucket *bucket, struct cls_rule *rule)
587 struct cls_rule *pos;
588 LIST_FOR_EACH (pos, struct cls_rule, node.list, &bucket->rules) {
589 if (pos->priority <= rule->priority) {
590 if (pos->priority == rule->priority
591 && pos->wc.wildcards == rule->wc.wildcards
592 && rules_match_1wild(pos, rule, rule->table_idx))
594 list_replace(&rule->node.list, &pos->node.list);
600 list_insert(&pos->node.list, &rule->node.list);
604 static struct cls_rule *
605 insert_exact_rule(struct classifier *cls, struct cls_rule *rule)
607 struct cls_rule *old_rule;
610 hash = flow_hash(&rule->flow, 0);
611 old_rule = search_exact_table(cls, hash, &rule->flow);
613 hmap_remove(&cls->exact_table, &old_rule->node.hmap);
615 hmap_insert(&cls->exact_table, &rule->node.hmap, hash);
619 /* Returns the bucket in 'table' that has the given 'hash' and the same fields
620 * as 'rule->flow' (up to 'rule->table_idx'), or a null pointer if no bucket
622 static struct cls_bucket *
623 find_bucket(struct hmap *table, size_t hash, const struct cls_rule *rule)
625 struct cls_bucket *bucket;
626 HMAP_FOR_EACH_WITH_HASH (bucket, struct cls_bucket, hmap_node, hash,
628 if (equal_fields(&bucket->fixed, &rule->flow, rule->table_idx)) {
635 /* Creates a bucket and inserts it in 'table' with the given 'hash' and 'fixed'
636 * values. Returns the new bucket. */
637 static struct cls_bucket *
638 create_bucket(struct hmap *table, size_t hash, const flow_t *fixed)
640 struct cls_bucket *bucket = xmalloc(sizeof *bucket);
641 list_init(&bucket->rules);
642 bucket->fixed = *fixed;
643 hmap_insert(table, &bucket->hmap_node, hash);
647 /* Returns true if the 'n' bytes in 'a' and 'b' are equal, false otherwise. */
648 static inline bool ALWAYS_INLINE
649 equal_bytes(const void *a, const void *b, size_t n)
652 /* For some reason GCC generates stupid code for memcmp() of small
653 * constant integer lengths. Help it out.
655 * This function is always inlined, and it is always called with 'n' as a
656 * compile-time constant, so the switch statement gets optimized out and
657 * this whole function just expands to an instruction or two. */
660 return *(uint8_t *) a == *(uint8_t *) b;
663 return *(uint16_t *) a == *(uint16_t *) b;
666 return *(uint32_t *) a == *(uint32_t *) b;
669 return (*(uint32_t *) a == *(uint32_t *) b
670 && ((uint16_t *) a)[2] == ((uint16_t *) b)[2]);
676 /* I hope GCC is smarter on your platform. */
677 return !memcmp(a, b, n);
681 /* Returns the 32-bit unsigned integer at 'p'. */
682 static inline uint32_t
683 read_uint32(const void *p)
685 /* GCC optimizes this into a single machine instruction on x86. */
687 memcpy(&x, p, sizeof x);
691 /* Compares the specified field in 'a' and 'b'. Returns true if the fields are
692 * equal, or if the ofp_match wildcard bits in 'wildcards' are set such that
693 * non-equal values may be ignored. 'nw_src_mask' and 'nw_dst_mask' must be
694 * those that would be set for 'wildcards' by cls_rule_set_masks().
696 * The compared field is the one with wildcard bit or bits 'field_wc', offset
697 * 'rule_ofs' within cls_rule's "fields" member, and length 'len', in bytes. */
698 static inline bool ALWAYS_INLINE
699 field_matches(const flow_t *a_, const flow_t *b_,
700 uint32_t wildcards, uint32_t nw_src_mask, uint32_t nw_dst_mask,
701 uint32_t field_wc, int ofs, int len)
703 /* This function is always inlined, and it is always called with 'field_wc'
704 * as a compile-time constant, so the "if" conditionals here generate no
706 const void *a = (const uint8_t *) a_ + ofs;
707 const void *b = (const uint8_t *) b_ + ofs;
708 if (!(field_wc & (field_wc - 1))) {
709 /* Handle all the single-bit wildcard cases. */
710 return wildcards & field_wc || equal_bytes(a, b, len);
711 } else if (field_wc == OFPFW_NW_SRC_MASK ||
712 field_wc == OFPFW_NW_DST_MASK) {
713 uint32_t a_ip = read_uint32(a);
714 uint32_t b_ip = read_uint32(b);
715 uint32_t mask = (field_wc == OFPFW_NW_SRC_MASK
716 ? nw_src_mask : nw_dst_mask);
717 return ((a_ip ^ b_ip) & mask) == 0;
723 /* Returns true if 'a' and 'b' match, ignoring fields for which the wildcards
724 * in 'wildcards' are set. 'nw_src_mask' and 'nw_dst_mask' must be those that
725 * would be set for 'wildcards' by cls_rule_set_masks(). 'field_idx' is the
726 * index of the first field to be compared; fields before 'field_idx' are
727 * assumed to match. (Always returns true if 'field_idx' is CLS_N_FIELDS.) */
729 rules_match(const struct cls_rule *a, const struct cls_rule *b,
730 uint32_t wildcards, uint32_t nw_src_mask, uint32_t nw_dst_mask,
733 /* This is related to Duff's device (see
734 * http://en.wikipedia.org/wiki/Duff's_device). */
736 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
737 case CLS_F_IDX_##NAME: \
738 if (!field_matches(&a->flow, &b->flow, \
739 wildcards, nw_src_mask, nw_dst_mask, \
740 WILDCARDS, offsetof(flow_t, MEMBER), \
741 sizeof a->flow.MEMBER)) { \
751 /* Returns true if 'fixed' and 'wild' match. All fields in 'fixed' must have
752 * fixed values; 'wild' may contain wildcards.
754 * 'field_idx' is the index of the first field to be compared; fields before
755 * 'field_idx' are assumed to match. Always returns true if 'field_idx' is
758 rules_match_1wild(const struct cls_rule *fixed, const struct cls_rule *wild,
761 return rules_match(fixed, wild, wild->wc.wildcards, wild->wc.nw_src_mask,
762 wild->wc.nw_dst_mask, field_idx);
765 /* Searches 'bucket' for a rule that matches 'target'. Returns the
766 * highest-priority match, if one is found, or a null pointer if there is no
769 * 'field_idx' must be the index of the first wildcarded field in 'bucket'. */
770 static struct cls_rule *
771 search_bucket(struct cls_bucket *bucket, int field_idx,
772 const struct cls_rule *target)
774 struct cls_rule *pos;
776 if (!equal_fields(&bucket->fixed, &target->flow, field_idx)) {
780 LIST_FOR_EACH (pos, struct cls_rule, node.list, &bucket->rules) {
781 if (rules_match_1wild(target, pos, field_idx)) {
788 /* Searches 'table' for a rule that matches 'target'. Returns the
789 * highest-priority match, if one is found, or a null pointer if there is no
792 * 'field_idx' must be the index of the first wildcarded field in 'table'. */
793 static struct cls_rule *
794 search_table(const struct hmap *table, int field_idx,
795 const struct cls_rule *target)
797 struct cls_bucket *bucket;
799 switch (hmap_count(table)) {
800 /* In these special cases there's no need to hash. */
804 bucket = CONTAINER_OF(hmap_first(table), struct cls_bucket, hmap_node);
805 return search_bucket(bucket, field_idx, target);
808 HMAP_FOR_EACH_WITH_HASH (bucket, struct cls_bucket, hmap_node,
809 hash_fields(&target->flow, field_idx), table) {
810 struct cls_rule *rule = search_bucket(bucket, field_idx, target);
818 static struct cls_rule *
819 search_exact_table(const struct classifier *cls, size_t hash,
820 const flow_t *target)
822 struct cls_rule *rule;
824 HMAP_FOR_EACH_WITH_HASH (rule, struct cls_rule, node.hmap,
825 hash, &cls->exact_table) {
826 if (flow_equal(&rule->flow, target)) {