Skip to content

Commit

Permalink
lib: Separate versioning to its own module.
Browse files Browse the repository at this point in the history
Separate rule versioning to lib/versions.h to make it easier to use
versioning for other data types.

Signed-off-by: Jarno Rajahalme <[email protected]>
Acked-by: Ben Pfaff <[email protected]>
  • Loading branch information
Jarno Rajahalme committed Jul 29, 2016
1 parent c5f346a commit 44e0c35
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 133 deletions.
1 change: 1 addition & 0 deletions lib/automake.mk
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ lib_libopenvswitch_la_SOURCES = \
lib/vconn-provider.h \
lib/vconn-stream.c \
lib/vconn.c \
lib/versions.h \
lib/vlan-bitmap.c \
lib/vlan-bitmap.h \
lib/vlog.c \
Expand Down
31 changes: 7 additions & 24 deletions lib/classifier-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,8 @@ struct cls_match {
/* Accessed by all readers. */
struct cmap_node cmap_node; /* Within struct cls_subtable 'rules'. */

/* Rule versioning.
*
* CLS_NOT_REMOVED_VERSION has a special meaning for 'remove_version',
* meaning that the rule has been added but not yet removed.
*/
const cls_version_t add_version; /* Version rule was added in. */
ATOMIC(cls_version_t) remove_version; /* Version rule is removed in. */
/* Rule versioning. */
struct versions versions;

const struct cls_rule *cls_rule;
const struct miniflow flow; /* Matching rule. Mask is in the subtable. */
Expand All @@ -102,34 +97,22 @@ get_cls_match(const struct cls_rule *rule)
void cls_match_free_cb(struct cls_match *);

static inline void
cls_match_set_remove_version(struct cls_match *rule, cls_version_t version)
cls_match_set_remove_version(struct cls_match *rule, ovs_version_t version)
{
atomic_store_relaxed(&rule->remove_version, version);
versions_set_remove_version(&rule->versions, version);
}

static inline bool
cls_match_visible_in_version(const struct cls_match *rule,
cls_version_t version)
ovs_version_t version)
{
cls_version_t remove_version;

/* C11 does not want to access an atomic via a const object pointer. */
atomic_read_relaxed(&CONST_CAST(struct cls_match *, rule)->remove_version,
&remove_version);

return rule->add_version <= version && version < remove_version;
return versions_visible_in_version(&rule->versions, version);
}

static inline bool
cls_match_is_eventually_invisible(const struct cls_match *rule)
{
cls_version_t remove_version;

/* C11 does not want to access an atomic via a const object pointer. */
atomic_read_relaxed(&CONST_CAST(struct cls_match *, rule)->remove_version,
&remove_version);

return remove_version <= CLS_MAX_VERSION;
return versions_is_eventually_invisible(&rule->versions);
}


Expand Down
45 changes: 22 additions & 23 deletions lib/classifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ cls_conjunction_set_alloc(struct cls_match *match,
}

static struct cls_match *
cls_match_alloc(const struct cls_rule *rule, cls_version_t version,
cls_match_alloc(const struct cls_rule *rule, ovs_version_t version,
const struct cls_conjunction conj[], size_t n)
{
size_t count = miniflow_n_values(rule->match.flow);
Expand All @@ -95,9 +95,8 @@ cls_match_alloc(const struct cls_rule *rule, cls_version_t version,
ovsrcu_init(&cls_match->next, NULL);
*CONST_CAST(const struct cls_rule **, &cls_match->cls_rule) = rule;
*CONST_CAST(int *, &cls_match->priority) = rule->priority;
*CONST_CAST(cls_version_t *, &cls_match->add_version) = version;
atomic_init(&cls_match->remove_version, version); /* Initially
* invisible. */
/* Make rule initially invisible. */
cls_match->versions = VERSIONS_INITIALIZER(version, version);
miniflow_clone(CONST_CAST(struct miniflow *, &cls_match->flow),
rule->match.flow, count);
ovsrcu_set_hidden(&cls_match->conj_set,
Expand All @@ -113,7 +112,7 @@ static struct cls_subtable *insert_subtable(struct classifier *cls,
static void destroy_subtable(struct classifier *cls, struct cls_subtable *);

static const struct cls_match *find_match_wc(const struct cls_subtable *,
cls_version_t version,
ovs_version_t version,
const struct flow *,
struct trie_ctx *,
unsigned int n_tries,
Expand All @@ -126,7 +125,7 @@ static struct cls_match *find_equal(const struct cls_subtable *,
* versioning is used at most one of them is ever visible for lookups on any
* given 'version'. */
static inline const struct cls_match *
next_visible_rule_in_list(const struct cls_match *rule, cls_version_t version)
next_visible_rule_in_list(const struct cls_match *rule, ovs_version_t version)
{
do {
rule = cls_match_next(rule);
Expand Down Expand Up @@ -288,11 +287,11 @@ cls_rule_is_catchall(const struct cls_rule *rule)
* This may only be called by the exclusive writer. */
void
cls_rule_make_invisible_in_version(const struct cls_rule *rule,
cls_version_t remove_version)
ovs_version_t remove_version)
{
struct cls_match *cls_match = get_cls_match_protected(rule);

ovs_assert(remove_version >= cls_match->add_version);
ovs_assert(remove_version >= cls_match->versions.add_version);

cls_match_set_remove_version(cls_match, remove_version);
}
Expand All @@ -305,14 +304,14 @@ void
cls_rule_restore_visibility(const struct cls_rule *rule)
{
cls_match_set_remove_version(get_cls_match_protected(rule),
CLS_NOT_REMOVED_VERSION);
OVS_VERSION_NOT_REMOVED);
}

/* Return true if 'rule' is visible in 'version'.
*
* 'rule' must be in a classifier. */
bool
cls_rule_visible_in_version(const struct cls_rule *rule, cls_version_t version)
cls_rule_visible_in_version(const struct cls_rule *rule, ovs_version_t version)
{
struct cls_match *cls_match = get_cls_match(rule);

Expand Down Expand Up @@ -516,7 +515,7 @@ static inline ovs_be32 minimatch_get_ports(const struct minimatch *match)
*/
const struct cls_rule *
classifier_replace(struct classifier *cls, const struct cls_rule *rule,
cls_version_t version,
ovs_version_t version,
const struct cls_conjunction *conjs, size_t n_conjs)
{
struct cls_match *new;
Expand Down Expand Up @@ -627,7 +626,7 @@ classifier_replace(struct classifier *cls, const struct cls_rule *rule,
/* No change in subtable's max priority or max count. */

/* Make 'new' visible to lookups in the appropriate version. */
cls_match_set_remove_version(new, CLS_NOT_REMOVED_VERSION);
cls_match_set_remove_version(new, OVS_VERSION_NOT_REMOVED);

/* Make rule visible to iterators (immediately). */
rculist_replace(CONST_CAST(struct rculist *, &rule->node),
Expand All @@ -644,7 +643,7 @@ classifier_replace(struct classifier *cls, const struct cls_rule *rule,
}

/* Make 'new' visible to lookups in the appropriate version. */
cls_match_set_remove_version(new, CLS_NOT_REMOVED_VERSION);
cls_match_set_remove_version(new, OVS_VERSION_NOT_REMOVED);

/* Make rule visible to iterators (immediately). */
rculist_push_back(&subtable->rules_list,
Expand Down Expand Up @@ -686,7 +685,7 @@ classifier_replace(struct classifier *cls, const struct cls_rule *rule,
* such a rule. */
void
classifier_insert(struct classifier *cls, const struct cls_rule *rule,
cls_version_t version, const struct cls_conjunction conj[],
ovs_version_t version, const struct cls_conjunction conj[],
size_t n_conj)
{
const struct cls_rule *displaced_rule
Expand Down Expand Up @@ -929,7 +928,7 @@ free_conjunctive_matches(struct hmap *matches,
* 'flow' is non-const to allow for temporary modifications during the lookup.
* Any changes are restored before returning. */
static const struct cls_rule *
classifier_lookup__(const struct classifier *cls, cls_version_t version,
classifier_lookup__(const struct classifier *cls, ovs_version_t version,
struct flow *flow, struct flow_wildcards *wc,
bool allow_conjunctive_matches)
{
Expand Down Expand Up @@ -1157,7 +1156,7 @@ classifier_lookup__(const struct classifier *cls, cls_version_t version,
* 'flow' is non-const to allow for temporary modifications during the lookup.
* Any changes are restored before returning. */
const struct cls_rule *
classifier_lookup(const struct classifier *cls, cls_version_t version,
classifier_lookup(const struct classifier *cls, ovs_version_t version,
struct flow *flow, struct flow_wildcards *wc)
{
return classifier_lookup__(cls, version, flow, wc, true);
Expand All @@ -1170,7 +1169,7 @@ classifier_lookup(const struct classifier *cls, cls_version_t version,
const struct cls_rule *
classifier_find_rule_exactly(const struct classifier *cls,
const struct cls_rule *target,
cls_version_t version)
ovs_version_t version)
{
const struct cls_match *head, *rule;
const struct cls_subtable *subtable;
Expand Down Expand Up @@ -1205,7 +1204,7 @@ classifier_find_rule_exactly(const struct classifier *cls,
const struct cls_rule *
classifier_find_match_exactly(const struct classifier *cls,
const struct match *target, int priority,
cls_version_t version)
ovs_version_t version)
{
const struct cls_rule *retval;
struct cls_rule cr;
Expand All @@ -1227,7 +1226,7 @@ classifier_find_match_exactly(const struct classifier *cls,
* dl_type could match both, if the rules also have the same priority. */
bool
classifier_rule_overlaps(const struct classifier *cls,
const struct cls_rule *target, cls_version_t version)
const struct cls_rule *target, ovs_version_t version)
{
struct cls_subtable *subtable;

Expand Down Expand Up @@ -1301,7 +1300,7 @@ cls_rule_is_loose_match(const struct cls_rule *rule,

static bool
rule_matches(const struct cls_rule *rule, const struct cls_rule *target,
cls_version_t version)
ovs_version_t version)
{
/* Rule may only match a target if it is visible in target's version. */
return cls_rule_visible_in_version(rule, version)
Expand Down Expand Up @@ -1340,7 +1339,7 @@ search_subtable(const struct cls_subtable *subtable,
* Ignores target->priority. */
struct cls_cursor
cls_cursor_start(const struct classifier *cls, const struct cls_rule *target,
cls_version_t version)
ovs_version_t version)
{
struct cls_cursor cursor;
struct cls_subtable *subtable;
Expand Down Expand Up @@ -1617,7 +1616,7 @@ miniflow_and_mask_matches_flow(const struct miniflow *flow,
}

static inline const struct cls_match *
find_match(const struct cls_subtable *subtable, cls_version_t version,
find_match(const struct cls_subtable *subtable, ovs_version_t version,
const struct flow *flow, uint32_t hash)
{
const struct cls_match *head, *rule;
Expand All @@ -1639,7 +1638,7 @@ find_match(const struct cls_subtable *subtable, cls_version_t version,
}

static const struct cls_match *
find_match_wc(const struct cls_subtable *subtable, cls_version_t version,
find_match_wc(const struct cls_subtable *subtable, ovs_version_t version,
const struct flow *flow, struct trie_ctx trie_ctx[CLS_MAX_TRIES],
unsigned int n_tries, struct flow_wildcards *wc)
{
Expand Down
29 changes: 12 additions & 17 deletions lib/classifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@
#include "pvector.h"
#include "rculist.h"
#include "openvswitch/type-props.h"
#include "versions.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -322,12 +323,6 @@ struct cls_trie {
rcu_trie_ptr root; /* NULL if none. */
};

typedef uint64_t cls_version_t;

#define CLS_MIN_VERSION 0 /* Default version number to use. */
#define CLS_MAX_VERSION (TYPE_MAXIMUM(cls_version_t) - 1)
#define CLS_NOT_REMOVED_VERSION TYPE_MAXIMUM(cls_version_t)

enum {
CLS_MAX_INDICES = 3, /* Maximum number of lookup indices per subtable. */
CLS_MAX_TRIES = 3 /* Maximum number of prefix trees per classifier. */
Expand Down Expand Up @@ -381,15 +376,15 @@ void cls_rule_destroy(struct cls_rule *);
void cls_rule_set_conjunctions(struct cls_rule *,
const struct cls_conjunction *, size_t n);
void cls_rule_make_invisible_in_version(const struct cls_rule *,
cls_version_t);
ovs_version_t);
void cls_rule_restore_visibility(const struct cls_rule *);

void classifier_insert(struct classifier *, const struct cls_rule *,
cls_version_t, const struct cls_conjunction *,
ovs_version_t, const struct cls_conjunction *,
size_t n_conjunctions);
const struct cls_rule *classifier_replace(struct classifier *,
const struct cls_rule *,
cls_version_t,
ovs_version_t,
const struct cls_conjunction *,
size_t n_conjunctions);
const struct cls_rule *classifier_remove(struct classifier *,
Expand All @@ -400,17 +395,17 @@ static inline void classifier_publish(struct classifier *);
/* Lookups. These are RCU protected and may run concurrently with modifiers
* and each other. */
const struct cls_rule *classifier_lookup(const struct classifier *,
cls_version_t, struct flow *,
ovs_version_t, struct flow *,
struct flow_wildcards *);
bool classifier_rule_overlaps(const struct classifier *,
const struct cls_rule *, cls_version_t);
const struct cls_rule *, ovs_version_t);
const struct cls_rule *classifier_find_rule_exactly(const struct classifier *,
const struct cls_rule *,
cls_version_t);
ovs_version_t);
const struct cls_rule *classifier_find_match_exactly(const struct classifier *,
const struct match *,
int priority,
cls_version_t);
ovs_version_t);
bool classifier_is_empty(const struct classifier *);
int classifier_count(const struct classifier *);

Expand All @@ -421,7 +416,7 @@ void cls_rule_format(const struct cls_rule *, struct ds *);
bool cls_rule_is_catchall(const struct cls_rule *);
bool cls_rule_is_loose_match(const struct cls_rule *rule,
const struct minimatch *criteria);
bool cls_rule_visible_in_version(const struct cls_rule *, cls_version_t);
bool cls_rule_visible_in_version(const struct cls_rule *, ovs_version_t);

/* Iteration.
*
Expand All @@ -439,18 +434,18 @@ struct cls_cursor {
const struct classifier *cls;
const struct cls_subtable *subtable;
const struct cls_rule *target;
cls_version_t version; /* Version to iterate. */
ovs_version_t version; /* Version to iterate. */
struct pvector_cursor subtables;
const struct cls_rule *rule;
};

struct cls_cursor cls_cursor_start(const struct classifier *,
const struct cls_rule *target,
cls_version_t);
ovs_version_t);
void cls_cursor_advance(struct cls_cursor *);

#define CLS_FOR_EACH(RULE, MEMBER, CLS) \
CLS_FOR_EACH_TARGET(RULE, MEMBER, CLS, NULL, CLS_MAX_VERSION)
CLS_FOR_EACH_TARGET(RULE, MEMBER, CLS, NULL, OVS_VERSION_MAX)
#define CLS_FOR_EACH_TARGET(RULE, MEMBER, CLS, TARGET, VERSION) \
for (struct cls_cursor cursor__ = cls_cursor_start(CLS, TARGET, VERSION); \
(cursor__.rule \
Expand Down
6 changes: 3 additions & 3 deletions lib/ovs-router.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ ovs_router_lookup(const struct in6_addr *ip6_dst, char output_bridge[],
const struct cls_rule *cr;
struct flow flow = {.ipv6_dst = *ip6_dst};

cr = classifier_lookup(&cls, CLS_MAX_VERSION, &flow, NULL);
cr = classifier_lookup(&cls, OVS_VERSION_MAX, &flow, NULL);
if (cr) {
struct ovs_router_entry *p = ovs_router_entry_cast(cr);

Expand Down Expand Up @@ -206,7 +206,7 @@ ovs_router_insert__(uint8_t priority, const struct in6_addr *ip6_dst,
cls_rule_init(&p->cr, &match, priority);

ovs_mutex_lock(&mutex);
cr = classifier_replace(&cls, &p->cr, CLS_MIN_VERSION, NULL, 0);
cr = classifier_replace(&cls, &p->cr, OVS_VERSION_MIN, NULL, 0);
ovs_mutex_unlock(&mutex);

if (cr) {
Expand Down Expand Up @@ -254,7 +254,7 @@ rt_entry_delete(uint8_t priority, const struct in6_addr *ip6_dst, uint8_t plen)
cls_rule_init(&rule, &match, priority);

/* Find the exact rule. */
cr = classifier_find_rule_exactly(&cls, &rule, CLS_MAX_VERSION);
cr = classifier_find_rule_exactly(&cls, &rule, OVS_VERSION_MAX);
if (cr) {
ovs_mutex_lock(&mutex);
res = __rt_entry_delete(cr);
Expand Down
Loading

0 comments on commit 44e0c35

Please sign in to comment.