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(struct flow, MEMBER), \
29 sizeof ((struct flow *)0)->MEMBER, \
34 { sizeof(struct flow), 0, 0, "exact" },
37 static uint32_t hash_fields(const struct flow *, int table_idx);
38 static bool equal_fields(const struct flow *, const struct flow *,
41 static int table_idx_from_wildcards(uint32_t wildcards);
42 static struct cls_rule *table_insert(struct hmap *, struct cls_rule *);
43 static struct cls_rule *insert_exact_rule(struct classifier *,
45 static struct cls_bucket *find_bucket(struct hmap *, size_t hash,
46 const struct cls_rule *);
47 static struct cls_rule *search_table(const struct hmap *table, int field_idx,
48 const struct cls_rule *);
49 static struct cls_rule *search_exact_table(const struct classifier *,
50 size_t hash, const struct flow *);
51 static bool rules_match_1wild(const struct cls_rule *fixed,
52 const struct cls_rule *wild, int field_idx);
53 static bool rules_match_2wild(const struct cls_rule *wild1,
54 const struct cls_rule *wild2, int field_idx);
56 /* Converts the flow in 'flow' into a cls_rule in 'rule', with the given
57 * 'wildcards' and 'priority'.*/
59 cls_rule_from_flow(const struct flow *flow, uint32_t wildcards,
60 unsigned int priority, struct cls_rule *rule)
63 flow_wildcards_init(&rule->wc, wildcards);
64 rule->priority = priority;
65 rule->table_idx = table_idx_from_wildcards(rule->wc.wildcards);
68 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
69 * 'priority'. If 'tun_id_from_cookie' is set then the upper 32 bits of
70 * 'cookie' are stored in the rule as the tunnel ID. */
72 cls_rule_from_match(const struct ofp_match *match, unsigned int priority,
73 bool tun_id_from_cookie, uint64_t cookie,
74 struct cls_rule *rule)
77 flow_from_match(match, tun_id_from_cookie, cookie, &rule->flow, &wildcards);
78 flow_wildcards_init(&rule->wc, wildcards);
79 rule->priority = rule->wc.wildcards ? priority : UINT16_MAX;
80 rule->table_idx = table_idx_from_wildcards(rule->wc.wildcards);
83 /* Converts 'rule' to a string and returns the string. The caller must free
84 * the string (with free()). */
86 cls_rule_to_string(const struct cls_rule *rule)
88 struct ds s = DS_EMPTY_INITIALIZER;
89 ds_put_format(&s, "wildcards=%x priority=%u ",
90 rule->wc.wildcards, rule->priority);
91 flow_format(&s, &rule->flow);
95 /* Prints cls_rule 'rule', for debugging.
97 * (The output could be improved and expanded, but this was good enough to
98 * debug the classifier.) */
100 cls_rule_print(const struct cls_rule *rule)
102 printf("wildcards=%x priority=%u ", rule->wc.wildcards, rule->priority);
103 flow_print(stdout, &rule->flow);
107 /* Initializes 'cls' as a classifier that initially contains no classification
110 classifier_init(struct classifier *cls)
115 for (i = 0; i < ARRAY_SIZE(cls->tables); i++) {
116 hmap_init(&cls->tables[i]);
118 hmap_init(&cls->exact_table);
121 /* Destroys 'cls'. Rules within 'cls', if any, are not freed; this is the
122 * caller's responsibility. */
124 classifier_destroy(struct classifier *cls)
127 struct cls_bucket *bucket, *next_bucket;
130 for (tbl = &cls->tables[0]; tbl < &cls->tables[CLS_N_FIELDS]; tbl++) {
131 HMAP_FOR_EACH_SAFE (bucket, next_bucket, hmap_node, tbl) {
136 hmap_destroy(&cls->exact_table);
140 /* Returns true if 'cls' does not contain any classification rules, false
143 classifier_is_empty(const struct classifier *cls)
145 return cls->n_rules == 0;
148 /* Returns the number of rules in 'classifier'. */
150 classifier_count(const struct classifier *cls)
155 /* Returns the number of rules in 'classifier' that have no wildcards. */
157 classifier_count_exact(const struct classifier *cls)
159 return hmap_count(&cls->exact_table);
162 /* Inserts 'rule' into 'cls'. Transfers ownership of 'rule' to 'cls'.
164 * If 'cls' already contains an identical rule (including wildcards, values of
165 * fixed fields, and priority), replaces the old rule by 'rule' and returns the
166 * rule that was replaced. The caller takes ownership of the returned rule and
167 * is thus responsible for freeing it, etc., as necessary.
169 * Returns NULL if 'cls' does not contain a rule with an identical key, after
170 * inserting the new rule. In this case, no rules are displaced by the new
171 * rule, even rules that cannot have any effect because the new rule matches a
172 * superset of their flows and has higher priority. */
174 classifier_insert(struct classifier *cls, struct cls_rule *rule)
176 struct cls_rule *old;
177 assert((rule->wc.wildcards == 0) == (rule->table_idx == CLS_F_IDX_EXACT));
178 old = (rule->wc.wildcards
179 ? table_insert(&cls->tables[rule->table_idx], rule)
180 : insert_exact_rule(cls, rule));
187 /* Removes 'rule' from 'cls'. It is caller's responsibility to free 'rule', if
188 * this is desirable. */
190 classifier_remove(struct classifier *cls, struct cls_rule *rule)
192 if (rule->wc.wildcards) {
193 /* Remove 'rule' from bucket. If that empties the bucket, remove the
194 * bucket from its table. */
195 struct hmap *table = &cls->tables[rule->table_idx];
196 struct list *rules = list_remove(&rule->node.list);
197 if (list_is_empty(rules)) {
198 /* This code is a little tricky. list_remove() returns the list
199 * element just after the one removed. Since the list is now
200 * empty, this will be the address of the 'rules' member of the
201 * bucket that was just emptied, so pointer arithmetic (via
202 * CONTAINER_OF) can find that bucket. */
203 struct cls_bucket *bucket;
204 bucket = CONTAINER_OF(rules, struct cls_bucket, rules);
205 hmap_remove(table, &bucket->hmap_node);
209 /* Remove 'rule' from cls->exact_table. */
210 hmap_remove(&cls->exact_table, &rule->node.hmap);
215 /* Finds and returns the highest-priority rule in 'cls' that matches 'flow'.
216 * Returns a null pointer if no rules in 'cls' match 'flow'. If multiple rules
217 * of equal priority match 'flow', returns one arbitrarily.
219 * (When multiple rules of equal priority happen to fall into the same bucket,
220 * rules added more recently take priority over rules added less recently, but
221 * this is subject to change and should not be depended upon.) */
223 classifier_lookup(const struct classifier *cls, const struct flow *flow)
225 struct cls_rule *rule = classifier_lookup_exact(cls, flow);
227 rule = classifier_lookup_wild(cls, flow);
233 classifier_lookup_exact(const struct classifier *cls, const struct flow *flow)
235 return (!hmap_is_empty(&cls->exact_table)
236 ? search_exact_table(cls, flow_hash(flow, 0), flow)
241 classifier_lookup_wild(const struct classifier *cls, const struct flow *flow)
243 struct cls_rule *best = NULL;
244 if (cls->n_rules > hmap_count(&cls->exact_table)) {
245 struct cls_rule target;
248 cls_rule_from_flow(flow, 0, 0, &target);
249 for (i = 0; i < CLS_N_FIELDS; i++) {
250 struct cls_rule *rule = search_table(&cls->tables[i], i, &target);
251 if (rule && (!best || rule->priority > best->priority)) {
260 classifier_find_rule_exactly(const struct classifier *cls,
261 const struct flow *target, uint32_t wildcards,
262 unsigned int priority)
264 struct cls_bucket *bucket;
269 /* Ignores 'priority'. */
270 return search_exact_table(cls, flow_hash(target, 0), target);
273 assert(wildcards == (wildcards & OVSFW_ALL));
274 table_idx = table_idx_from_wildcards(wildcards);
275 hash = hash_fields(target, table_idx);
276 HMAP_FOR_EACH_WITH_HASH (bucket, hmap_node, hash,
277 &cls->tables[table_idx]) {
278 if (equal_fields(&bucket->fixed, target, table_idx)) {
279 struct cls_rule *pos;
280 LIST_FOR_EACH (pos, node.list, &bucket->rules) {
281 if (pos->priority < priority) {
283 } else if (pos->priority == priority &&
284 pos->wc.wildcards == wildcards &&
285 flow_equal(target, &pos->flow)) {
294 /* Checks if the flow defined by 'target' with 'wildcards' at 'priority'
295 * overlaps with any other rule at the same priority in the classifier.
296 * Two rules are considered overlapping if a packet could match both. */
298 classifier_rule_overlaps(const struct classifier *cls,
299 const struct flow *target, uint32_t wildcards,
300 unsigned int priority)
302 struct cls_rule target_rule;
303 const struct hmap *tbl;
306 return search_exact_table(cls, flow_hash(target, 0), target) ?
310 cls_rule_from_flow(target, wildcards, priority, &target_rule);
312 for (tbl = &cls->tables[0]; tbl < &cls->tables[CLS_N_FIELDS]; tbl++) {
313 struct cls_bucket *bucket;
315 HMAP_FOR_EACH (bucket, hmap_node, tbl) {
316 struct cls_rule *rule;
318 LIST_FOR_EACH (rule, node.list, &bucket->rules) {
319 if (rule->priority == priority
320 && rules_match_2wild(rule, &target_rule, 0)) {
330 /* Ignores target->priority.
332 * 'callback' is allowed to delete the rule that is passed as its argument, but
333 * it must not delete (or move) any other rules in 'cls' that are in the same
334 * table as the argument rule. Two rules are in the same table if their
335 * cls_rule structs have the same table_idx; as a special case, a rule with
336 * wildcards and an exact-match rule will never be in the same table. */
338 classifier_for_each_match(const struct classifier *cls,
339 const struct cls_rule *target,
340 int include, cls_cb_func *callback, void *aux)
342 if (include & CLS_INC_WILD) {
343 const struct hmap *table;
345 for (table = &cls->tables[0]; table < &cls->tables[CLS_N_FIELDS];
347 struct cls_bucket *bucket, *next_bucket;
349 HMAP_FOR_EACH_SAFE (bucket, next_bucket, hmap_node, table) {
350 /* XXX there is a bit of room for optimization here based on
351 * rejecting entire buckets on their fixed fields, but it will
352 * only be worthwhile for big buckets (which we hope we won't
353 * get anyway, but...) */
354 struct cls_rule *prev_rule, *rule;
356 /* We can't just use LIST_FOR_EACH_SAFE here because, if the
357 * callback deletes the last rule in the bucket, then the
358 * bucket itself will be destroyed. The bucket contains the
359 * list head so that's a use-after-free error. */
361 LIST_FOR_EACH (rule, node.list, &bucket->rules) {
362 if (rules_match_1wild(rule, target, 0)) {
364 callback(prev_rule, aux);
370 callback(prev_rule, aux);
376 if (include & CLS_INC_EXACT) {
377 if (target->wc.wildcards) {
378 struct cls_rule *rule, *next_rule;
380 HMAP_FOR_EACH_SAFE (rule, next_rule, node.hmap,
382 if (rules_match_1wild(rule, target, 0)) {
387 /* Optimization: there can be at most one match in the exact
389 size_t hash = flow_hash(&target->flow, 0);
390 struct cls_rule *rule = search_exact_table(cls, hash,
399 /* 'callback' is allowed to delete the rule that is passed as its argument, but
400 * it must not delete (or move) any other rules in 'cls' that are in the same
401 * table as the argument rule. Two rules are in the same table if their
402 * cls_rule structs have the same table_idx; as a special case, a rule with
403 * wildcards and an exact-match rule will never be in the same table.
405 * If 'include' is CLS_INC_EXACT then CLASSIFIER_FOR_EACH_EXACT_RULE(_SAFE) is
406 * probably easier to use. */
408 classifier_for_each(const struct classifier *cls, int include,
409 void (*callback)(struct cls_rule *, void *aux),
412 if (include & CLS_INC_WILD) {
413 const struct hmap *tbl;
415 for (tbl = &cls->tables[0]; tbl < &cls->tables[CLS_N_FIELDS]; tbl++) {
416 struct cls_bucket *bucket, *next_bucket;
418 HMAP_FOR_EACH_SAFE (bucket, next_bucket, hmap_node, tbl) {
419 struct cls_rule *prev_rule, *rule;
421 /* We can't just use LIST_FOR_EACH_SAFE here because, if the
422 * callback deletes the last rule in the bucket, then the
423 * bucket itself will be destroyed. The bucket contains the
424 * list head so that's a use-after-free error. */
426 LIST_FOR_EACH (rule, node.list, &bucket->rules) {
428 callback(prev_rule, aux);
433 callback(prev_rule, aux);
439 if (include & CLS_INC_EXACT) {
440 struct cls_rule *rule, *next_rule;
442 HMAP_FOR_EACH_SAFE (rule, next_rule, node.hmap, &cls->exact_table) {
448 static struct cls_bucket *create_bucket(struct hmap *, size_t hash,
449 const struct flow *fixed);
450 static struct cls_rule *bucket_insert(struct cls_bucket *, struct cls_rule *);
452 static inline bool equal_bytes(const void *, const void *, size_t n);
454 /* Returns a hash computed across the fields in 'flow' whose field indexes
455 * (CLS_F_IDX_*) are less than 'table_idx'. (If 'table_idx' is
456 * CLS_F_IDX_EXACT, hashes all the fields in 'flow'). */
458 hash_fields(const struct flow *flow, int table_idx)
460 /* I just know I'm going to hell for writing code this way.
462 * GCC generates pretty good code here, with only a single taken
463 * conditional jump per execution. Now the question is, would we be better
464 * off marking this function ALWAYS_INLINE and writing a wrapper that
465 * switches on the value of 'table_idx' to get rid of all the conditional
466 * jumps entirely (except for one in the wrapper)? Honestly I really,
467 * really hope that it doesn't matter in practice.
469 * We could do better by calculating hashes incrementally, instead of
470 * starting over from the top each time. But that would be even uglier. */
475 a = b = c = 0xdeadbeef + table_idx;
478 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
479 if (table_idx == CLS_F_IDX_##NAME) { \
481 memset((uint8_t *) tmp + n, 0, sizeof tmp - n); \
484 const size_t size = sizeof flow->MEMBER; \
485 const uint8_t *p1 = (const uint8_t *) &flow->MEMBER; \
486 const size_t p1_size = MIN(sizeof tmp - n, size); \
487 const uint8_t *p2 = p1 + p1_size; \
488 const size_t p2_size = size - p1_size; \
490 /* Append to 'tmp' as much data as will fit. */ \
491 memcpy((uint8_t *) tmp + n, p1, p1_size); \
494 /* If 'tmp' is full, mix. */ \
495 if (n == sizeof tmp) { \
503 /* Append to 'tmp' any data that didn't fit. */ \
504 memcpy(tmp, p2, p2_size); \
518 /* Compares the fields in 'a' and 'b' whose field indexes (CLS_F_IDX_*) are
519 * less than 'table_idx'. (If 'table_idx' is CLS_F_IDX_EXACT, compares all the
520 * fields in 'a' and 'b').
522 * Returns true if all the compared fields are equal, false otherwise. */
524 equal_fields(const struct flow *a, const struct flow *b, int table_idx)
526 /* XXX The generated code could be better here. */
527 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
528 if (table_idx == CLS_F_IDX_##NAME) { \
530 } else if (!equal_bytes(&a->MEMBER, &b->MEMBER, sizeof a->MEMBER)) { \
540 table_idx_from_wildcards(uint32_t wildcards)
543 return CLS_F_IDX_EXACT;
545 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
546 if (wildcards & WILDCARDS) { \
547 return CLS_F_IDX_##NAME; \
554 /* Inserts 'rule' into 'table'. Returns the rule, if any, that was displaced
555 * in favor of 'rule'. */
556 static struct cls_rule *
557 table_insert(struct hmap *table, struct cls_rule *rule)
559 struct cls_bucket *bucket;
562 hash = hash_fields(&rule->flow, rule->table_idx);
563 bucket = find_bucket(table, hash, rule);
565 bucket = create_bucket(table, hash, &rule->flow);
568 return bucket_insert(bucket, rule);
571 /* Inserts 'rule' into 'bucket', given that 'field' is the first wildcarded
574 * Returns the rule, if any, that was displaced in favor of 'rule'. */
575 static struct cls_rule *
576 bucket_insert(struct cls_bucket *bucket, struct cls_rule *rule)
578 struct cls_rule *pos;
579 LIST_FOR_EACH (pos, node.list, &bucket->rules) {
580 if (pos->priority == rule->priority) {
581 if (pos->wc.wildcards == rule->wc.wildcards
582 && rules_match_1wild(pos, rule, rule->table_idx))
584 list_replace(&rule->node.list, &pos->node.list);
587 } else if (pos->priority < rule->priority) {
591 list_insert(&pos->node.list, &rule->node.list);
595 static struct cls_rule *
596 insert_exact_rule(struct classifier *cls, struct cls_rule *rule)
598 struct cls_rule *old_rule;
601 hash = flow_hash(&rule->flow, 0);
602 old_rule = search_exact_table(cls, hash, &rule->flow);
604 hmap_remove(&cls->exact_table, &old_rule->node.hmap);
606 hmap_insert(&cls->exact_table, &rule->node.hmap, hash);
610 /* Returns the bucket in 'table' that has the given 'hash' and the same fields
611 * as 'rule->flow' (up to 'rule->table_idx'), or a null pointer if no bucket
613 static struct cls_bucket *
614 find_bucket(struct hmap *table, size_t hash, const struct cls_rule *rule)
616 struct cls_bucket *bucket;
617 HMAP_FOR_EACH_WITH_HASH (bucket, hmap_node, hash, table) {
618 if (equal_fields(&bucket->fixed, &rule->flow, rule->table_idx)) {
625 /* Creates a bucket and inserts it in 'table' with the given 'hash' and 'fixed'
626 * values. Returns the new bucket. */
627 static struct cls_bucket *
628 create_bucket(struct hmap *table, size_t hash, const struct flow *fixed)
630 struct cls_bucket *bucket = xmalloc(sizeof *bucket);
631 list_init(&bucket->rules);
632 bucket->fixed = *fixed;
633 hmap_insert(table, &bucket->hmap_node, hash);
637 /* Returns true if the 'n' bytes in 'a' and 'b' are equal, false otherwise. */
638 static inline bool ALWAYS_INLINE
639 equal_bytes(const void *a, const void *b, size_t n)
642 /* For some reason GCC generates stupid code for memcmp() of small
643 * constant integer lengths. Help it out.
645 * This function is always inlined, and it is always called with 'n' as a
646 * compile-time constant, so the switch statement gets optimized out and
647 * this whole function just expands to an instruction or two. */
650 return *(uint8_t *) a == *(uint8_t *) b;
653 return *(uint16_t *) a == *(uint16_t *) b;
656 return *(uint32_t *) a == *(uint32_t *) b;
659 return (*(uint32_t *) a == *(uint32_t *) b
660 && ((uint16_t *) a)[2] == ((uint16_t *) b)[2]);
666 /* I hope GCC is smarter on your platform. */
667 return !memcmp(a, b, n);
671 /* Returns the 32-bit unsigned integer at 'p'. */
672 static inline uint32_t
673 read_uint32(const void *p)
675 /* GCC optimizes this into a single machine instruction on x86. */
677 memcpy(&x, p, sizeof x);
681 /* Compares the specified field in 'a' and 'b'. Returns true if the fields are
682 * equal, or if the ofp_match wildcard bits in 'wildcards' are set such that
683 * non-equal values may be ignored. 'nw_src_mask' and 'nw_dst_mask' must be
684 * those that would be set for 'wildcards' by cls_rule_set_masks().
686 * The compared field is the one with wildcard bit or bits 'field_wc', offset
687 * 'rule_ofs' within cls_rule's "fields" member, and length 'len', in bytes. */
688 static inline bool ALWAYS_INLINE
689 field_matches(const struct flow *a_, const struct flow *b_,
690 uint32_t wildcards, uint32_t nw_src_mask, uint32_t nw_dst_mask,
691 uint32_t field_wc, int ofs, int len)
693 /* This function is always inlined, and it is always called with 'field_wc'
694 * as a compile-time constant, so the "if" conditionals here generate no
696 const void *a = (const uint8_t *) a_ + ofs;
697 const void *b = (const uint8_t *) b_ + ofs;
698 if (!(field_wc & (field_wc - 1))) {
699 /* Handle all the single-bit wildcard cases. */
700 return wildcards & field_wc || equal_bytes(a, b, len);
701 } else if (field_wc == OFPFW_NW_SRC_MASK ||
702 field_wc == OFPFW_NW_DST_MASK) {
703 uint32_t a_ip = read_uint32(a);
704 uint32_t b_ip = read_uint32(b);
705 uint32_t mask = (field_wc == OFPFW_NW_SRC_MASK
706 ? nw_src_mask : nw_dst_mask);
707 return ((a_ip ^ b_ip) & mask) == 0;
713 /* Returns true if 'a' and 'b' match, ignoring fields for which the wildcards
714 * in 'wildcards' are set. 'nw_src_mask' and 'nw_dst_mask' must be those that
715 * would be set for 'wildcards' by cls_rule_set_masks(). 'field_idx' is the
716 * index of the first field to be compared; fields before 'field_idx' are
717 * assumed to match. (Always returns true if 'field_idx' is CLS_N_FIELDS.) */
719 rules_match(const struct cls_rule *a, const struct cls_rule *b,
720 uint32_t wildcards, uint32_t nw_src_mask, uint32_t nw_dst_mask,
723 /* This is related to Duff's device (see
724 * http://en.wikipedia.org/wiki/Duff's_device). */
726 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
727 case CLS_F_IDX_##NAME: \
728 if (!field_matches(&a->flow, &b->flow, \
729 wildcards, nw_src_mask, nw_dst_mask, \
730 WILDCARDS, offsetof(struct flow, MEMBER), \
731 sizeof a->flow.MEMBER)) { \
741 /* Returns true if 'fixed' and 'wild' match. All fields in 'fixed' must have
742 * fixed values; 'wild' may contain wildcards.
744 * 'field_idx' is the index of the first field to be compared; fields before
745 * 'field_idx' are assumed to match. Always returns true if 'field_idx' is
748 rules_match_1wild(const struct cls_rule *fixed, const struct cls_rule *wild,
751 return rules_match(fixed, wild, wild->wc.wildcards, wild->wc.nw_src_mask,
752 wild->wc.nw_dst_mask, field_idx);
755 /* Returns true if 'wild1' and 'wild2' match, that is, if their fields
756 * are equal modulo wildcards in 'wild1' or 'wild2'.
758 * 'field_idx' is the index of the first field to be compared; fields before
759 * 'field_idx' are assumed to match. Always returns true if 'field_idx' is
762 rules_match_2wild(const struct cls_rule *wild1, const struct cls_rule *wild2,
765 return rules_match(wild1, wild2,
766 wild1->wc.wildcards | wild2->wc.wildcards,
767 wild1->wc.nw_src_mask & wild2->wc.nw_src_mask,
768 wild1->wc.nw_dst_mask & wild2->wc.nw_dst_mask,
772 /* Searches 'bucket' for a rule that matches 'target'. Returns the
773 * highest-priority match, if one is found, or a null pointer if there is no
776 * 'field_idx' must be the index of the first wildcarded field in 'bucket'. */
777 static struct cls_rule *
778 search_bucket(struct cls_bucket *bucket, int field_idx,
779 const struct cls_rule *target)
781 struct cls_rule *pos;
783 if (!equal_fields(&bucket->fixed, &target->flow, field_idx)) {
787 LIST_FOR_EACH (pos, node.list, &bucket->rules) {
788 if (rules_match_1wild(target, pos, field_idx)) {
795 /* Searches 'table' for a rule that matches 'target'. Returns the
796 * highest-priority match, if one is found, or a null pointer if there is no
799 * 'field_idx' must be the index of the first wildcarded field in 'table'. */
800 static struct cls_rule *
801 search_table(const struct hmap *table, int field_idx,
802 const struct cls_rule *target)
804 struct cls_bucket *bucket;
806 switch (hmap_count(table)) {
807 /* In these special cases there's no need to hash. */
811 bucket = CONTAINER_OF(hmap_first(table), struct cls_bucket, hmap_node);
812 return search_bucket(bucket, field_idx, target);
815 HMAP_FOR_EACH_WITH_HASH (bucket, hmap_node,
816 hash_fields(&target->flow, field_idx), table) {
817 struct cls_rule *rule = search_bucket(bucket, field_idx, target);
825 static struct cls_rule *
826 search_exact_table(const struct classifier *cls, size_t hash,
827 const struct flow *target)
829 struct cls_rule *rule;
831 HMAP_FOR_EACH_WITH_HASH (rule, node.hmap, hash, &cls->exact_table) {
832 if (flow_equal(&rule->flow, target)) {