/* Initializes 'rule' to match packets specified by 'match' at the given
* 'priority'.
*
+ * The caller must eventually destroy 'rule' with cls_rule_destroy().
+ *
* 'match' must satisfy the invariant described in the comment at the
* definition of struct match.
*
rule->priority = priority;
}
+/* Initializes 'dst' as a copy of 'src'.
+ *
+ * The caller must eventually destroy 'rule' with cls_rule_destroy(). */
+void
+cls_rule_clone(struct cls_rule *dst, const struct cls_rule *src)
+{
+ *dst = *src;
+}
+
+/* Frees memory referenced by 'rule'. Doesn't free 'rule' itself (it's
+ * normally embedded into a larger structure).
+ *
+ * ('rule' must not currently be in a classifier.) */
+void
+cls_rule_destroy(struct cls_rule *rule OVS_UNUSED)
+{
+ /* Nothing to do yet. */
+}
+
/* Returns true if 'a' and 'b' match the same packets at the same priority,
* false if they differ in some way. */
bool
* If 'cls' already contains an identical rule (including wildcards, values of
* fixed fields, and priority), replaces the old rule by 'rule' and returns the
* rule that was replaced. The caller takes ownership of the returned rule and
- * is thus responsible for freeing it, etc., as necessary.
+ * is thus responsible for destroying it with cls_rule_destroy(), freeing the
+ * memory block in which it resides, etc., as necessary.
*
* Returns NULL if 'cls' does not contain a rule with an identical key, after
* inserting the new rule. In this case, no rules are displaced by the new
assert(!displaced_rule);
}
-/* Removes 'rule' from 'cls'. It is the caller's responsibility to free
- * 'rule', if this is desirable. */
+/* Removes 'rule' from 'cls'. It is the caller's responsibility to destroy
+ * 'rule' with cls_rule_destroy(), freeing the memory block in which 'rule'
+ * resides, etc., as necessary. */
void
classifier_remove(struct classifier *cls, struct cls_rule *rule)
{
cls_rule_init(&cr, target, priority);
retval = classifier_find_rule_exactly(cls, &cr);
+ cls_rule_destroy(&cr);
return retval;
}
void cls_rule_init(struct cls_rule *,
const struct match *, unsigned int priority);
+void cls_rule_clone(struct cls_rule *, const struct cls_rule *);
+void cls_rule_destroy(struct cls_rule *);
bool cls_rule_equal(const struct cls_rule *, const struct cls_rule *);
uint32_t cls_rule_hash(const struct cls_rule *, uint32_t basis);
ofproto_rule_destroy__(struct rule *rule)
{
if (rule) {
+ cls_rule_destroy(&rule->cr);
free(rule->ofpacts);
rule->ofproto->ofproto_class->rule_dealloc(rule);
}
cls_cursor_init(&cursor, &table->cls, &cr);
CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
if (rule->pending) {
- return OFPROTO_POSTPONE;
+ error = OFPROTO_POSTPONE;
+ goto exit;
}
if (!ofproto_rule_is_hidden(rule)
&& ofproto_rule_has_out_port(rule, out_port)
}
}
}
- return 0;
+
+exit:
+ cls_rule_destroy(&cr);
+ return error;
}
/* Searches 'ofproto' for rules in table 'table_id' (or in all tables, if
&cr));
if (rule) {
if (rule->pending) {
- return OFPROTO_POSTPONE;
+ error = OFPROTO_POSTPONE;
+ goto exit;
}
if (!ofproto_rule_is_hidden(rule)
&& ofproto_rule_has_out_port(rule, out_port)
}
}
}
+
+exit:
+ cls_rule_destroy(&cr);
return 0;
}
/* Serialize against pending deletion. */
if (is_flow_deletion_pending(ofproto, &cr, table - ofproto->tables)) {
+ cls_rule_destroy(&rule->cr);
ofproto->ofproto_class->rule_dealloc(rule);
return OFPROTO_POSTPONE;
}
/* Check for overlap, if requested. */
if (fm->flags & OFPFF_CHECK_OVERLAP
&& classifier_rule_overlaps(&table->cls, &rule->cr)) {
+ cls_rule_destroy(&rule->cr);
ofproto->ofproto_class->rule_dealloc(rule);
return OFPERR_OFPFMFC_OVERLAP;
}
ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
}
}
+ cls_rule_destroy(&target);
}
static void
return rule ? CONTAINER_OF(rule, struct test_rule, cls_rule) : NULL;
}
+static struct test_rule *make_rule(int wc_fields, unsigned int priority,
+ int value_pat);
+static void free_rule(struct test_rule *);
+static struct test_rule *clone_rule(const struct test_rule *);
+
/* Trivial (linear) classifier. */
struct tcls {
size_t n_rules;
const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
if (cls_rule_equal(pos, &rule->cls_rule)) {
/* Exact match. */
- free(tcls->rules[i]);
- tcls->rules[i] = xmemdup(rule, sizeof *rule);
+ free_rule(tcls->rules[i]);
+ tcls->rules[i] = clone_rule(rule);
return tcls->rules[i];
} else if (pos->priority < rule->cls_rule.priority) {
break;
memmove(&tcls->rules[i + 1], &tcls->rules[i],
sizeof *tcls->rules * (tcls->n_rules - i));
}
- tcls->rules[i] = xmemdup(rule, sizeof *rule);
+ tcls->rules[i] = clone_rule(rule);
tcls->n_rules++;
return tcls->rules[i];
}
cls_cursor_init(&cursor, cls, NULL);
CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
classifier_remove(cls, &rule->cls_rule);
- free(rule);
+ free_rule(rule);
}
classifier_destroy(cls);
}
return rule;
}
+static struct test_rule *
+clone_rule(const struct test_rule *src)
+{
+ struct test_rule *dst;
+
+ dst = xmalloc(sizeof *dst);
+ dst->aux = src->aux;
+ cls_rule_clone(&dst->cls_rule, &src->cls_rule);
+ return dst;
+}
+
+static void
+free_rule(struct test_rule *rule)
+{
+ cls_rule_destroy(&rule->cls_rule);
+ free(rule);
+}
+
static void
shuffle(unsigned int *p, size_t n)
{
assert(tcls_is_empty(&tcls));
compare_classifiers(&cls, &tcls);
- free(rule);
+ free_rule(rule);
classifier_destroy(&cls);
tcls_destroy(&tcls);
}
tcls_insert(&tcls, rule2);
assert(test_rule_from_cls_rule(
classifier_replace(&cls, &rule2->cls_rule)) == rule1);
- free(rule1);
+ free_rule(rule1);
check_tables(&cls, 1, 1, 0);
compare_classifiers(&cls, &tcls);
tcls_destroy(&tcls);
tcls_destroy(&tcls);
for (i = 0; i < N_RULES; i++) {
- free(rules[i]);
+ free_rule(rules[i]);
}
} while (next_permutation(ops, ARRAY_SIZE(ops)));
assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
for (i = 0; i < N_RULES; i++) {
tcls_remove(&tcls, tcls_rules[i]);
classifier_remove(&cls, &rules[i]->cls_rule);
- free(rules[i]);
+ free_rule(rules[i]);
check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
compare_classifiers(&cls, &tcls);
struct test_rule *target;
struct cls_cursor cursor;
- target = xmemdup(tcls.rules[rand() % tcls.n_rules],
- sizeof(struct test_rule));
+ target = clone_rule(tcls.rules[rand() % tcls.n_rules]);
cls_cursor_init(&cursor, &cls, &target->cls_rule);
CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
classifier_remove(&cls, &rule->cls_rule);
- free(rule);
+ free_rule(rule);
}
tcls_delete_matches(&tcls, &target->cls_rule);
compare_classifiers(&cls, &tcls);
check_tables(&cls, -1, -1, -1);
- free(target);
+ free_rule(target);
}
destroy_classifier(&cls);
if (fte) {
fte_version_free(fte->versions[0]);
fte_version_free(fte->versions[1]);
+ cls_rule_destroy(&fte->rule);
free(fte);
}
}
if (old) {
fte_version_free(old->versions[index]);
fte->versions[!index] = old->versions[!index];
+ cls_rule_destroy(&old->rule);
free(old);
}
}