Skip to content

Commit

Permalink
expr: Rename "macros" to "addr_sets".
Browse files Browse the repository at this point in the history
Macro is a very generic term, but the arguments are only ever address
sets, so rename for clarity.

Signed-off-by: Justin Pettit <[email protected]>
Acked-by: Ben Pfaff <[email protected]>
  • Loading branch information
justinpettit committed Jan 5, 2017
1 parent 1ab3905 commit b34e898
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 72 deletions.
18 changes: 9 additions & 9 deletions include/ovn/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,9 @@ expr_from_node(const struct ovs_list *node)
void expr_format(const struct expr *, struct ds *);
void expr_print(const struct expr *);
struct expr *expr_parse(struct lexer *, const struct shash *symtab,
const struct shash *macros);
const struct shash *addr_sets);
struct expr *expr_parse_string(const char *, const struct shash *symtab,
const struct shash *macros,
const struct shash *addr_sets,
char **errorp);

struct expr *expr_clone(struct expr *);
Expand All @@ -383,7 +383,7 @@ bool expr_is_simplified(const struct expr *);
bool expr_is_normalized(const struct expr *);

char *expr_parse_microflow(const char *, const struct shash *symtab,
const struct shash *macros,
const struct shash *addr_sets,
bool (*lookup_port)(const void *aux,
const char *port_name,
unsigned int *portp),
Expand Down Expand Up @@ -466,19 +466,19 @@ void expr_constant_set_format(const struct expr_constant_set *, struct ds *);
void expr_constant_set_destroy(struct expr_constant_set *cs);


/* Address sets, aka "macros".
/* Address sets.
*
* Instead of referring to a set of value as:
* {addr1, addr2, ..., addrN}
* You can register a set of values and refer to them as:
* $name
* The macros should all have integer/masked-integer values.
* The address set entries should all have integer/masked-integer values.
* The values that don't qualify are ignored.
*/

void expr_macros_add(struct shash *macros, const char *name,
const char * const *values, size_t n_values);
void expr_macros_remove(struct shash *macros, const char *name);
void expr_macros_destroy(struct shash *macros);
void expr_addr_sets_add(struct shash *addr_sets, const char *name,
const char * const *values, size_t n_values);
void expr_addr_sets_remove(struct shash *addr_sets, const char *name);
void expr_addr_sets_destroy(struct shash *addr_sets);

#endif /* ovn/expr.h */
7 changes: 4 additions & 3 deletions ovn/controller/lflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ update_address_sets(struct controller_ctx *ctx,
{
const struct sbrec_address_set *as;
SBREC_ADDRESS_SET_FOR_EACH (as, ctx->ovnsb_idl) {
expr_macros_add(expr_address_sets_p, as->name,
(const char *const *) as->addresses, as->n_addresses);
expr_addr_sets_add(expr_address_sets_p, as->name,
(const char *const *) as->addresses,
as->n_addresses);
}
}

Expand Down Expand Up @@ -385,7 +386,7 @@ lflow_run(struct controller_ctx *ctx, const struct lport_index *lports,
group_table, ct_zones, flow_table, &expr_address_sets);
add_neighbor_flows(ctx, lports, flow_table);

expr_macros_destroy(&expr_address_sets);
expr_addr_sets_destroy(&expr_address_sets);
shash_destroy(&expr_address_sets);
}

Expand Down
93 changes: 48 additions & 45 deletions ovn/lib/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,10 @@ expr_print(const struct expr *e)

/* Context maintained during expr_parse(). */
struct expr_context {
struct lexer *lexer; /* Lexer for pulling more tokens. */
const struct shash *symtab; /* Symbol table. */
const struct shash *macros; /* Table of macros. */
bool not; /* True inside odd number of NOT operators. */
struct lexer *lexer; /* Lexer for pulling more tokens. */
const struct shash *symtab; /* Symbol table. */
const struct shash *addr_sets; /* Address set table. */
bool not; /* True inside odd number of NOT operators. */
};

struct expr *expr_parse__(struct expr_context *);
Expand Down Expand Up @@ -692,14 +692,14 @@ assign_constant_set_type(struct expr_context *ctx,
}

static bool
parse_macros(struct expr_context *ctx, struct expr_constant_set *cs,
size_t *allocated_values)
parse_addr_sets(struct expr_context *ctx, struct expr_constant_set *cs,
size_t *allocated_values)
{
struct expr_constant_set *addr_set
= (ctx->macros
? shash_find_data(ctx->macros, ctx->lexer->token.s)
struct expr_constant_set *addr_sets
= (ctx->addr_sets
? shash_find_data(ctx->addr_sets, ctx->lexer->token.s)
: NULL);
if (!addr_set) {
if (!addr_sets) {
lexer_syntax_error(ctx->lexer, "expecting address set name");
return false;
}
Expand All @@ -708,13 +708,13 @@ parse_macros(struct expr_context *ctx, struct expr_constant_set *cs,
return false;
}

size_t n_values = cs->n_values + addr_set->n_values;
size_t n_values = cs->n_values + addr_sets->n_values;
if (n_values >= *allocated_values) {
cs->values = xrealloc(cs->values, n_values * sizeof *cs->values);
*allocated_values = n_values;
}
for (size_t i = 0; i < addr_set->n_values; i++) {
cs->values[cs->n_values++] = addr_set->values[i];
for (size_t i = 0; i < addr_sets->n_values; i++) {
cs->values[cs->n_values++] = addr_sets->values[i];
}

return true;
Expand Down Expand Up @@ -752,7 +752,7 @@ parse_constant(struct expr_context *ctx, struct expr_constant_set *cs,
lexer_get(ctx->lexer);
return true;
} else if (ctx->lexer->token.type == LEX_T_MACRO) {
if (!parse_macros(ctx, cs, allocated_values)) {
if (!parse_addr_sets(ctx, cs, allocated_values)) {
return false;
}
lexer_get(ctx->lexer);
Expand Down Expand Up @@ -908,22 +908,22 @@ expr_constant_set_destroy(struct expr_constant_set *cs)
}
}

/* Adds a macro named 'name' to 'macros', replacing any existing macro with the
* given name. */
/* Adds an address set named 'name' to 'addr_sets', replacing any existing
* address set entry with the given name. */
void
expr_macros_add(struct shash *macros, const char *name,
const char *const *values, size_t n_values)
expr_addr_sets_add(struct shash *addr_sets, const char *name,
const char *const *values, size_t n_values)
{
/* Replace any existing entry for this name. */
expr_macros_remove(macros, name);
expr_addr_sets_remove(addr_sets, name);

struct expr_constant_set *cs = xzalloc(sizeof *cs);
cs->type = EXPR_C_INTEGER;
cs->in_curlies = true;
cs->n_values = 0;
cs->values = xmalloc(n_values * sizeof *cs->values);
for (size_t i = 0; i < n_values; i++) {
/* Use the lexer to convert each macro into the proper
/* Use the lexer to convert each address set into the proper
* integer format. */
struct lexer lex;
lexer_init(&lex, values[i]);
Expand All @@ -944,29 +944,29 @@ expr_macros_add(struct shash *macros, const char *name,
lexer_destroy(&lex);
}

shash_add(macros, name, cs);
shash_add(addr_sets, name, cs);
}

void
expr_macros_remove(struct shash *macros, const char *name)
expr_addr_sets_remove(struct shash *addr_sets, const char *name)
{
struct expr_constant_set *cs = shash_find_and_delete(macros, name);
struct expr_constant_set *cs = shash_find_and_delete(addr_sets, name);
if (cs) {
expr_constant_set_destroy(cs);
free(cs);
}
}

/* Destroy all contents of 'macros'. */
/* Destroy all contents of 'addr_sets'. */
void
expr_macros_destroy(struct shash *macros)
expr_addr_sets_destroy(struct shash *addr_sets)
{
struct shash_node *node, *next;

SHASH_FOR_EACH_SAFE (node, next, macros) {
SHASH_FOR_EACH_SAFE (node, next, addr_sets) {
struct expr_constant_set *cs = node->data;

shash_delete(macros, node);
shash_delete(addr_sets, node);
expr_constant_set_destroy(cs);
free(cs);
}
Expand Down Expand Up @@ -1144,33 +1144,35 @@ expr_parse__(struct expr_context *ctx)
return e;
}

/* Parses an expression from 'lexer' using the symbols in 'symtab' and macros
* in 'macros'. If successful, returns the new expression; on failure, returns
* NULL. Returns nonnull if and only if lexer->error is NULL. */
/* Parses an expression from 'lexer' using the symbols in 'symtab' and
* address set table in 'addr_sets'. If successful, returns the new
* expression; on failure, returns NULL. Returns nonnull if and only if
* lexer->error is NULL. */
struct expr *
expr_parse(struct lexer *lexer, const struct shash *symtab,
const struct shash *macros)
const struct shash *addr_sets)
{
struct expr_context ctx = { .lexer = lexer,
.symtab = symtab,
.macros = macros };
.addr_sets = addr_sets };
return lexer->error ? NULL : expr_parse__(&ctx);
}

/* Parses the expression in 's' using the symbols in 'symtab' and macros in
* 'macros'. If successful, returns the new expression and sets '*errorp' to
* NULL. On failure, returns NULL and sets '*errorp' to an explanatory error
* message. The caller must eventually free the returned expression (with
* expr_destroy()) or error (with free()). */
/* Parses the expression in 's' using the symbols in 'symtab' and
* address set table in 'addr_sets'. If successful, returns the new
* expression and sets '*errorp' to NULL. On failure, returns NULL and
* sets '*errorp' to an explanatory error message. The caller must
* eventually free the returned expression (with expr_destroy()) or
* error (with free()). */
struct expr *
expr_parse_string(const char *s, const struct shash *symtab,
const struct shash *macros, char **errorp)
const struct shash *addr_sets, char **errorp)
{
struct lexer lexer;

lexer_init(&lexer, s);
lexer_get(&lexer);
struct expr *expr = expr_parse(&lexer, symtab, macros);
struct expr *expr = expr_parse(&lexer, symtab, addr_sets);
lexer_force_end(&lexer);
*errorp = lexer_steal_error(&lexer);
if (*errorp) {
Expand Down Expand Up @@ -3074,10 +3076,11 @@ expr_parse_microflow__(struct lexer *lexer,
return e;
}

/* Parses 's' as a microflow, using symbols from 'symtab', macros from
* 'macros', and looking up port numbers using 'lookup_port' and 'aux'. On
* success, stores the result in 'uflow' and returns NULL, otherwise zeros
* 'uflow' and returns an error message that the caller must free().
/* Parses 's' as a microflow, using symbols from 'symtab', address set
* table from 'addr_sets', and looking up port numbers using 'lookup_port'
* and 'aux'. On success, stores the result in 'uflow' and returns
* NULL, otherwise zeros 'uflow' and returns an error message that the
* caller must free().
*
* A "microflow" is a description of a single stream of packets, such as half a
* TCP connection. 's' uses the syntax of an OVN logical expression to express
Expand All @@ -3103,7 +3106,7 @@ expr_parse_microflow__(struct lexer *lexer,
* the last two as ambiguous. Just don't be too clever. */
char * OVS_WARN_UNUSED_RESULT
expr_parse_microflow(const char *s, const struct shash *symtab,
const struct shash *macros,
const struct shash *addr_sets,
bool (*lookup_port)(const void *aux,
const char *port_name,
unsigned int *portp),
Expand All @@ -3113,7 +3116,7 @@ expr_parse_microflow(const char *s, const struct shash *symtab,
lexer_init(&lexer, s);
lexer_get(&lexer);

struct expr *e = expr_parse(&lexer, symtab, macros);
struct expr *e = expr_parse(&lexer, symtab, addr_sets);
lexer_force_end(&lexer);

if (e) {
Expand Down
4 changes: 2 additions & 2 deletions ovn/lib/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ lex_parse_id(const char *p, enum lex_type type, struct lex_token *token)
}

static const char *
lex_parse_macro(const char *p, struct lex_token *token)
lex_parse_addr_set(const char *p, struct lex_token *token)
{
p++;
if (!lex_is_id1(*p)) {
Expand Down Expand Up @@ -744,7 +744,7 @@ lex_token_parse(struct lex_token *token, const char *p, const char **startp)
break;

case '$':
p = lex_parse_macro(p, token);
p = lex_parse_addr_set(p, token);
break;

case ':':
Expand Down
6 changes: 3 additions & 3 deletions ovn/utilities/ovn-trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,9 +579,9 @@ read_address_sets(void)

const struct sbrec_address_set *sbas;
SBREC_ADDRESS_SET_FOR_EACH (sbas, ovnsb_idl) {
expr_macros_add(&address_sets, sbas->name,
(const char *const *) sbas->addresses,
sbas->n_addresses);
expr_addr_sets_add(&address_sets, sbas->name,
(const char *const *) sbas->addresses,
sbas->n_addresses);
}
}

Expand Down
20 changes: 10 additions & 10 deletions tests/test-ovn.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ create_dhcp_opts(struct hmap *dhcp_opts, struct hmap *dhcpv6_opts)
}

static void
create_macros(struct shash *macros)
create_addr_sets(struct shash *addr_sets)
{
shash_init(macros);
shash_init(addr_sets);

static const char *const addrs1[] = {
"10.0.0.1", "10.0.0.2", "10.0.0.3",
Expand All @@ -202,9 +202,9 @@ create_macros(struct shash *macros)
"00:00:00:00:00:01", "00:00:00:00:00:02", "00:00:00:00:00:03",
};

expr_macros_add(macros, "set1", addrs1, 3);
expr_macros_add(macros, "set2", addrs2, 3);
expr_macros_add(macros, "set3", addrs3, 3);
expr_addr_sets_add(addr_sets, "set1", addrs1, 3);
expr_addr_sets_add(addr_sets, "set2", addrs2, 3);
expr_addr_sets_add(addr_sets, "set3", addrs3, 3);
}

static bool
Expand All @@ -223,12 +223,12 @@ static void
test_parse_expr__(int steps)
{
struct shash symtab;
struct shash macros;
struct shash addr_sets;
struct simap ports;
struct ds input;

create_symtab(&symtab);
create_macros(&macros);
create_addr_sets(&addr_sets);

simap_init(&ports);
simap_put(&ports, "eth0", 5);
Expand All @@ -240,7 +240,7 @@ test_parse_expr__(int steps)
struct expr *expr;
char *error;

expr = expr_parse_string(ds_cstr(&input), &symtab, &macros, &error);
expr = expr_parse_string(ds_cstr(&input), &symtab, &addr_sets, &error);
if (!error && steps > 0) {
expr = expr_annotate(expr, &symtab, &error);
}
Expand Down Expand Up @@ -277,8 +277,8 @@ test_parse_expr__(int steps)
simap_destroy(&ports);
expr_symtab_destroy(&symtab);
shash_destroy(&symtab);
expr_macros_destroy(&macros);
shash_destroy(&macros);
expr_addr_sets_destroy(&addr_sets);
shash_destroy(&addr_sets);
}

static void
Expand Down

0 comments on commit b34e898

Please sign in to comment.