return node ? CONTAINER_OF(node, struct cls_table, hmap_node) : NULL;
}
-static struct cls_rule *
-cls_rule_from_hmap_node(const struct hmap_node *node)
-{
- return node ? CONTAINER_OF(node, struct cls_rule, hmap_node) : NULL;
-}
-
-/* Returns the cls_table within 'cls' that has no wildcards, or NULL if there
- * is none. */
-struct cls_table *
-classifier_exact_table(const struct classifier *cls)
-{
- struct flow_wildcards exact_wc;
- flow_wildcards_init_exact(&exact_wc);
- return find_table(cls, &exact_wc);
-}
-
-/* Returns the first rule in 'table', or a null pointer if 'table' is NULL. */
-struct cls_rule *
-cls_table_first_rule(const struct cls_table *table)
-{
- return table ? cls_rule_from_hmap_node(hmap_first(&table->rules)) : NULL;
-}
-
-/* Returns the next rule in 'table' following 'rule', or a null pointer if
- * 'rule' is the last rule in 'table'. */
-struct cls_rule *
-cls_table_next_rule(const struct cls_table *table, const struct cls_rule *rule)
-{
- struct cls_rule *next
- = CONTAINER_OF(rule->list.next, struct cls_rule, list);
-
- return (next->priority < rule->priority
- ? next
- : cls_rule_from_hmap_node(hmap_next(&table->rules,
- &next->hmap_node)));
-}
-
/* Converts the flow in 'flow' into a cls_rule in 'rule', with the given
* 'wildcards' and 'priority'. */
void
return cls->n_rules;
}
-/* Returns the number of rules in 'classifier' that have no wildcards. */
-int
-classifier_count_exact(const struct classifier *cls)
-{
- struct cls_table *exact_table = classifier_exact_table(cls);
- return exact_table ? exact_table->n_table_rules : 0;
-}
-
/* Inserts 'rule' into 'cls'. Until 'rule' is removed from 'cls', the caller
* must not modify or free it.
*
hmap_replace(&table->rules, &rule->hmap_node, &next->hmap_node);
}
- if (--table->n_table_rules == 0 && !table->n_refs) {
+ if (--table->n_table_rules == 0) {
destroy_table(cls, table);
}
return false;
}
+\f
+/* Iteration. */
+
+static bool
+rule_matches(const struct cls_rule *rule, const struct cls_rule *target)
+{
+ return (!target
+ || flow_equal_except(&rule->flow, &target->flow, &target->wc));
+}
+
+static struct cls_rule *
+search_table(const struct cls_table *table, const struct cls_rule *target)
+{
+ if (!target || !flow_wildcards_has_extra(&table->wc, &target->wc)) {
+ struct cls_rule *rule;
+
+ HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
+ if (rule_matches(rule, target)) {
+ return rule;
+ }
+ }
+ }
+ return NULL;
+}
-/* Searches 'cls' for rules that exactly match 'target' or are more specific
- * than 'target'. That is, a given 'rule' matches 'target' if, for every
- * field:
+/* Initializes 'cursor' for iterating through 'cls' rules that exactly match
+ * 'target' or are more specific than 'target'. That is, a given 'rule'
+ * matches 'target' if, for every field:
*
* - 'target' and 'rule' specify the same (non-wildcarded) value for the
* field, or
*
* Ignores target->priority.
*
- * 'callback' is allowed to delete the rule that is passed as its argument, but
- * it must not delete (or move) any other rules in 'cls' that have the same
- * wildcards as the argument rule. */
+ * 'target' may be NULL to iterate over every rule in 'cls'. */
void
-classifier_for_each_match(const struct classifier *cls_,
- const struct cls_rule *target,
- cls_cb_func *callback, void *aux)
-{
- struct classifier *cls = (struct classifier *) cls_;
- struct cls_table *table, *next_table;
-
- for (table = classifier_first_table(cls); table; table = next_table) {
- if (!flow_wildcards_has_extra(&table->wc, &target->wc)) {
- /* We have eliminated the "no" case in the truth table above. Two
- * of the three remaining cases are trivial. We only need to check
- * the fourth case, where both 'rule' and 'target' require an exact
- * match. */
- struct cls_rule *head, *next_head;
-
- table->n_refs++;
- HMAP_FOR_EACH_SAFE (head, next_head, hmap_node, &table->rules) {
- if (flow_equal_except(&head->flow, &target->flow,
- &target->wc)) {
- struct cls_rule *rule, *next_rule;
-
- FOR_EACH_RULE_IN_LIST_SAFE (rule, next_rule, head) {
- callback(rule, aux);
- }
- }
- }
- next_table = classifier_next_table(cls, table);
- if (!--table->n_refs && !table->n_table_rules) {
- destroy_table(cls, table);
- }
- } else {
- next_table = classifier_next_table(cls, table);
+cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
+ const struct cls_rule *target)
+{
+ cursor->cls = cls;
+ cursor->target = target;
+}
+
+/* Returns the first matching cls_rule in 'cursor''s iteration, or a null
+ * pointer if there are no matches. */
+struct cls_rule *
+cls_cursor_first(struct cls_cursor *cursor)
+{
+ struct cls_table *table;
+
+ for (table = classifier_first_table(cursor->cls); table;
+ table = classifier_next_table(cursor->cls, table)) {
+ struct cls_rule *rule = search_table(table, cursor->target);
+ if (rule) {
+ cursor->table = table;
+ return rule;
}
}
+
+ return NULL;
}
-/* 'callback' is allowed to delete the rule that is passed as its argument, but
- * it must not delete (or move) any other rules in 'cls' that have the same
- * wildcards as the argument rule. */
-void
-classifier_for_each(const struct classifier *cls_,
- cls_cb_func *callback, void *aux)
+/* Returns the next matching cls_rule in 'cursor''s iteration, or a null
+ * pointer if there are no more matches. */
+struct cls_rule *
+cls_cursor_next(struct cls_cursor *cursor, struct cls_rule *rule)
{
- struct classifier *cls = (struct classifier *) cls_;
- struct cls_table *table, *next_table;
+ const struct cls_table *table;
+ struct cls_rule *next;
- for (table = classifier_first_table(cls); table; table = next_table) {
- struct cls_rule *head, *next_head;
-
- table->n_refs++;
- HMAP_FOR_EACH_SAFE (head, next_head, hmap_node, &table->rules) {
- struct cls_rule *rule, *next_rule;
+ next = next_rule_in_list(rule);
+ if (next) {
+ return next;
+ }
- FOR_EACH_RULE_IN_LIST_SAFE (rule, next_rule, head) {
- callback(rule, aux);
- }
+ HMAP_FOR_EACH_CONTINUE (rule, hmap_node, &cursor->table->rules) {
+ if (rule_matches(rule, cursor->target)) {
+ return rule;
}
- next_table = classifier_next_table(cls, table);
- if (!--table->n_refs && !table->n_table_rules) {
- destroy_table(cls, table);
+ }
+
+ for (table = classifier_next_table(cursor->cls, cursor->table); table;
+ table = classifier_next_table(cursor->cls, table)) {
+ rule = search_table(table, cursor->target);
+ if (rule) {
+ cursor->table = table;
+ return rule;
}
}
+
+ return NULL;
}
\f
static struct cls_table *