2 * Copyright (c) 2015, 2016 Nicira, Inc.
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 "byte-order.h"
22 #include "logical-fields.h"
23 #include "openvswitch/dynamic-string.h"
24 #include "openvswitch/match.h"
25 #include "openvswitch/ofp-actions.h"
26 #include "openvswitch/vlog.h"
31 VLOG_DEFINE_THIS_MODULE(expr);
33 /* Returns the name of measurement level 'level'. */
35 expr_level_to_string(enum expr_level level)
38 case EXPR_L_NOMINAL: return "nominal";
39 case EXPR_L_BOOLEAN: return "Boolean";
40 case EXPR_L_ORDINAL: return "ordinal";
41 default: OVS_NOT_REACHED();
45 /* Relational operators. */
47 /* Returns a string form of relational operator 'relop'. */
49 expr_relop_to_string(enum expr_relop relop)
52 case EXPR_R_EQ: return "==";
53 case EXPR_R_NE: return "!=";
54 case EXPR_R_LT: return "<";
55 case EXPR_R_LE: return "<=";
56 case EXPR_R_GT: return ">";
57 case EXPR_R_GE: return ">=";
58 default: OVS_NOT_REACHED();
63 expr_relop_from_token(enum lex_type type, enum expr_relop *relop)
68 case LEX_T_EQ: r = EXPR_R_EQ; break;
69 case LEX_T_NE: r = EXPR_R_NE; break;
70 case LEX_T_LT: r = EXPR_R_LT; break;
71 case LEX_T_LE: r = EXPR_R_LE; break;
72 case LEX_T_GT: r = EXPR_R_GT; break;
73 case LEX_T_GE: r = EXPR_R_GE; break;
74 default: return false;
83 /* Returns the relational operator that 'relop' becomes if you turn the
84 * relation's operands around, e.g. EXPR_R_EQ does not change because "a == b"
85 * and "b == a" are equivalent, but EXPR_R_LE becomes EXPR_R_GE because "a <=
86 * b" is equivalent to "b >= a". */
87 static enum expr_relop
88 expr_relop_turn(enum expr_relop relop)
91 case EXPR_R_EQ: return EXPR_R_EQ;
92 case EXPR_R_NE: return EXPR_R_NE;
93 case EXPR_R_LT: return EXPR_R_GT;
94 case EXPR_R_LE: return EXPR_R_GE;
95 case EXPR_R_GT: return EXPR_R_LT;
96 case EXPR_R_GE: return EXPR_R_LE;
97 default: OVS_NOT_REACHED();
101 /* Returns the relational operator that is the opposite of 'relop'. */
102 static enum expr_relop
103 expr_relop_invert(enum expr_relop relop)
106 case EXPR_R_EQ: return EXPR_R_NE;
107 case EXPR_R_NE: return EXPR_R_EQ;
108 case EXPR_R_LT: return EXPR_R_GE;
109 case EXPR_R_LE: return EXPR_R_GT;
110 case EXPR_R_GT: return EXPR_R_LE;
111 case EXPR_R_GE: return EXPR_R_LT;
112 default: OVS_NOT_REACHED();
116 /* Constructing and manipulating expressions. */
118 /* Creates and returns a logical AND or OR expression (according to 'type',
119 * which must be EXPR_T_AND or EXPR_T_OR) that initially has no
120 * sub-expressions. (To satisfy the invariants for expressions, the caller
121 * must add at least two sub-expressions whose types are different from
124 expr_create_andor(enum expr_type type)
126 struct expr *e = xmalloc(sizeof *e);
128 ovs_list_init(&e->andor);
132 /* Returns a logical AND or OR expression (according to 'type', which must be
133 * EXPR_T_AND or EXPR_T_OR) whose sub-expressions are 'a' and 'b', with some
136 * - If 'a' or 'b' is NULL, just returns the other one (which means that if
137 * that other one is not of the given 'type', then the returned
138 * expression is not either).
140 * - If 'a' or 'b', or both, have type 'type', then they are combined into
141 * a single node that satisfies the invariants for expressions. */
143 expr_combine(enum expr_type type, struct expr *a, struct expr *b)
149 } else if (a->type == type) {
150 if (b->type == type) {
151 ovs_list_splice(&a->andor, b->andor.next, &b->andor);
154 ovs_list_push_back(&a->andor, &b->node);
157 } else if (b->type == type) {
158 ovs_list_push_front(&b->andor, &a->node);
161 struct expr *e = expr_create_andor(type);
162 ovs_list_push_back(&e->andor, &a->node);
163 ovs_list_push_back(&e->andor, &b->node);
169 expr_insert_andor(struct expr *andor, struct expr *before, struct expr *new)
171 if (new->type == andor->type) {
172 if (andor->type == EXPR_T_AND) {
173 /* Conjunction junction, what's your function? */
175 ovs_list_splice(&before->node, new->andor.next, &new->andor);
178 ovs_list_insert(&before->node, &new->node);
182 /* Returns an EXPR_T_BOOLEAN expression with value 'b'. */
184 expr_create_boolean(bool b)
186 struct expr *e = xmalloc(sizeof *e);
187 e->type = EXPR_T_BOOLEAN;
193 expr_not(struct expr *expr)
197 switch (expr->type) {
199 expr->cmp.relop = expr_relop_invert(expr->cmp.relop);
204 LIST_FOR_EACH (sub, node, &expr->andor) {
207 expr->type = expr->type == EXPR_T_AND ? EXPR_T_OR : EXPR_T_AND;
211 expr->boolean = !expr->boolean;
219 expr_fix_andor(struct expr *expr, bool short_circuit)
221 struct expr *sub, *next;
223 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
224 if (sub->type == EXPR_T_BOOLEAN) {
225 if (sub->boolean == short_circuit) {
227 return expr_create_boolean(short_circuit);
229 ovs_list_remove(&sub->node);
235 if (ovs_list_is_short(&expr->andor)) {
236 if (ovs_list_is_empty(&expr->andor)) {
238 return expr_create_boolean(!short_circuit);
240 sub = expr_from_node(ovs_list_front(&expr->andor));
249 /* Returns 'expr' modified so that top-level oddities are fixed up:
251 * - Eliminates any EXPR_T_BOOLEAN operands at the top level.
253 * - Replaces one-operand EXPR_T_AND or EXPR_T_OR by its subexpression. */
255 expr_fix(struct expr *expr)
257 switch (expr->type) {
262 return expr_fix_andor(expr, false);
265 return expr_fix_andor(expr, true);
278 find_bitwise_range(const union mf_subvalue *sv, int width,
279 int *startp, int *n_bitsp)
281 unsigned int start = bitwise_scan(sv, sizeof *sv, true, 0, width);
283 unsigned int end = bitwise_scan(sv, sizeof *sv, false, start, width);
285 || bitwise_scan(sv, sizeof *sv, true, end, width) >= width) {
287 *n_bitsp = end - start;
291 *startp = *n_bitsp = 0;
295 expr_format_cmp(const struct expr *e, struct ds *s)
297 /* The common case is numerical comparisons.
298 * Handle string comparisons as a special case. */
299 if (!e->cmp.symbol->width) {
300 ds_put_format(s, "%s %s ", e->cmp.symbol->name,
301 expr_relop_to_string(e->cmp.relop));
302 json_string_escape(e->cmp.string, s);
307 find_bitwise_range(&e->cmp.mask, e->cmp.symbol->width, &ofs, &n);
308 if (n == 1 && (e->cmp.relop == EXPR_R_EQ || e->cmp.relop == EXPR_R_NE)) {
311 positive = bitwise_get_bit(&e->cmp.value, sizeof e->cmp.value, ofs);
312 positive ^= e->cmp.relop == EXPR_R_NE;
316 ds_put_cstr(s, e->cmp.symbol->name);
317 if (e->cmp.symbol->width > 1) {
318 ds_put_format(s, "[%d]", ofs);
323 ds_put_cstr(s, e->cmp.symbol->name);
324 if (n > 0 && n < e->cmp.symbol->width) {
326 ds_put_format(s, "[%d..%d]", ofs, ofs + n - 1);
328 ds_put_format(s, "[%d]", ofs);
332 ds_put_format(s, " %s ", expr_relop_to_string(e->cmp.relop));
335 union mf_subvalue value;
337 memset(&value, 0, sizeof value);
338 bitwise_copy(&e->cmp.value, sizeof e->cmp.value, ofs,
339 &value, sizeof value, 0,
341 mf_format_subvalue(&value, s);
343 mf_format_subvalue(&e->cmp.value, s);
345 mf_format_subvalue(&e->cmp.mask, s);
350 expr_format_andor(const struct expr *e, const char *op, struct ds *s)
355 LIST_FOR_EACH (sub, node, &e->andor) {
357 ds_put_format(s, " %s ", op);
360 if (sub->type == EXPR_T_AND || sub->type == EXPR_T_OR) {
370 /* Appends a string form of 'e' to 's'. The string form is acceptable for
371 * parsing back into an equivalent expression. */
373 expr_format(const struct expr *e, struct ds *s)
377 expr_format_cmp(e, s);
381 expr_format_andor(e, "&&", s);
385 expr_format_andor(e, "||", s);
389 ds_put_char(s, e->boolean ? '1' : '0');
394 /* Prints a string form of 'e' on stdout, followed by a new-line. */
396 expr_print(const struct expr *e)
401 expr_format(e, &output);
402 puts(ds_cstr(&output));
408 /* Context maintained during expr_parse(). */
409 struct expr_context {
410 struct lexer *lexer; /* Lexer for pulling more tokens. */
411 const struct shash *symtab; /* Symbol table. */
412 const struct shash *macros; /* Table of macros. */
413 char *error; /* Error, if any, otherwise NULL. */
414 bool not; /* True inside odd number of NOT operators. */
417 struct expr *expr_parse__(struct expr_context *);
418 static void expr_not(struct expr *);
419 static bool parse_field(struct expr_context *, struct expr_field *);
422 expr_error_handle_common(struct expr_context *ctx)
425 /* Already have an error, suppress this one since the cascade seems
426 * unlikely to be useful. */
428 } else if (ctx->lexer->token.type == LEX_T_ERROR) {
429 /* The lexer signaled an error. Nothing at the expression level
430 * accepts an error token, so we'll inevitably end up here with some
431 * meaningless parse error. Report the lexical error instead. */
432 ctx->error = xstrdup(ctx->lexer->token.s);
439 static void OVS_PRINTF_FORMAT(2, 3)
440 expr_error(struct expr_context *ctx, const char *message, ...)
442 if (expr_error_handle_common(ctx)) {
447 va_start(args, message);
448 ctx->error = xvasprintf(message, args);
452 static void OVS_PRINTF_FORMAT(2, 3)
453 expr_syntax_error(struct expr_context *ctx, const char *message, ...)
455 if (expr_error_handle_common(ctx)) {
462 ds_put_cstr(&s, "Syntax error ");
463 if (ctx->lexer->token.type == LEX_T_END) {
464 ds_put_cstr(&s, "at end of input ");
465 } else if (ctx->lexer->start) {
466 ds_put_format(&s, "at `%.*s' ",
467 (int) (ctx->lexer->input - ctx->lexer->start),
472 va_start(args, message);
473 ds_put_format_valist(&s, message, args);
476 ctx->error = ds_steal_cstr(&s);
480 make_cmp__(const struct expr_field *f, enum expr_relop r,
481 const union expr_constant *c)
483 struct expr *e = xzalloc(sizeof *e);
484 e->type = EXPR_T_CMP;
485 e->cmp.symbol = f->symbol;
487 if (f->symbol->width) {
488 bitwise_copy(&c->value, sizeof c->value, 0,
489 &e->cmp.value, sizeof e->cmp.value, f->ofs,
492 bitwise_copy(&c->mask, sizeof c->mask, 0,
493 &e->cmp.mask, sizeof e->cmp.mask, f->ofs,
496 bitwise_one(&e->cmp.mask, sizeof e->cmp.mask, f->ofs,
500 e->cmp.string = xstrdup(c->string);
505 /* Returns the minimum reasonable width for integer constant 'c'. */
507 expr_constant_width(const union expr_constant *c)
510 return mf_subvalue_width(&c->mask);
515 case LEX_F_HEXADECIMAL:
516 return mf_subvalue_width(&c->value);
533 type_check(struct expr_context *ctx, const struct expr_field *f,
534 struct expr_constant_set *cs)
536 if (cs->type != (f->symbol->width ? EXPR_C_INTEGER : EXPR_C_STRING)) {
537 expr_error(ctx, "%s field %s is not compatible with %s constant.",
538 f->symbol->width ? "Integer" : "String",
540 cs->type == EXPR_C_INTEGER ? "integer" : "string");
544 if (f->symbol->width) {
545 for (size_t i = 0; i < cs->n_values; i++) {
546 int w = expr_constant_width(&cs->values[i]);
547 if (w > f->symbol->width) {
548 expr_error(ctx, "%d-bit constant is not compatible with "
550 w, f->symbol->width, f->symbol->name);
560 make_cmp(struct expr_context *ctx,
561 const struct expr_field *f, enum expr_relop r,
562 struct expr_constant_set *cs)
564 struct expr *e = NULL;
566 if (!type_check(ctx, f, cs)) {
570 if (r != EXPR_R_EQ && r != EXPR_R_NE) {
571 if (cs->in_curlies) {
572 expr_error(ctx, "Only == and != operators may be used "
576 if (f->symbol->level == EXPR_L_NOMINAL ||
577 f->symbol->level == EXPR_L_BOOLEAN) {
578 expr_error(ctx, "Only == and != operators may be used "
580 expr_level_to_string(f->symbol->level),
584 if (cs->values[0].masked) {
585 expr_error(ctx, "Only == and != operators may be used with "
586 "masked constants. Consider using subfields instead "
587 "(e.g. eth.src[0..15] > 0x1111 in place of "
588 "eth.src > 00:00:00:00:11:11/00:00:00:00:ff:ff).");
593 if (f->symbol->level == EXPR_L_NOMINAL) {
594 if (f->symbol->expansion) {
595 ovs_assert(f->symbol->width > 0);
596 for (size_t i = 0; i < cs->n_values; i++) {
597 const union mf_subvalue *value = &cs->values[i].value;
598 bool positive = (value->integer & htonll(1)) != 0;
599 positive ^= r == EXPR_R_NE;
600 positive ^= ctx->not;
602 const char *name = f->symbol->name;
603 expr_error(ctx, "Nominal predicate %s may only be tested "
604 "positively, e.g. `%s' or `%s == 1' but not "
605 "`!%s' or `%s == 0'.",
606 name, name, name, name, name);
610 } else if (r != (ctx->not ? EXPR_R_NE : EXPR_R_EQ)) {
611 expr_error(ctx, "Nominal field %s may only be tested for "
612 "equality (taking enclosing `!' operators into "
613 "account).", f->symbol->name);
618 e = make_cmp__(f, r, &cs->values[0]);
619 for (size_t i = 1; i < cs->n_values; i++) {
620 e = expr_combine(r == EXPR_R_EQ ? EXPR_T_OR : EXPR_T_AND,
621 e, make_cmp__(f, r, &cs->values[i]));
624 expr_constant_set_destroy(cs);
629 expr_get_int(struct expr_context *ctx, int *value)
631 bool ok = lexer_get_int(ctx->lexer, value);
633 expr_syntax_error(ctx, "expecting small integer.");
639 parse_field(struct expr_context *ctx, struct expr_field *f)
641 const struct expr_symbol *symbol;
643 if (ctx->lexer->token.type != LEX_T_ID) {
644 expr_syntax_error(ctx, "expecting field name.");
648 symbol = shash_find_data(ctx->symtab, ctx->lexer->token.s);
650 expr_syntax_error(ctx, "expecting field name.");
653 lexer_get(ctx->lexer);
656 if (lexer_match(ctx->lexer, LEX_T_LSQUARE)) {
659 if (!symbol->width) {
660 expr_error(ctx, "Cannot select subfield of string field %s.",
665 if (!expr_get_int(ctx, &low)) {
668 if (lexer_match(ctx->lexer, LEX_T_ELLIPSIS)) {
669 if (!expr_get_int(ctx, &high)) {
676 if (!lexer_match(ctx->lexer, LEX_T_RSQUARE)) {
677 expr_syntax_error(ctx, "expecting `]'.");
682 expr_error(ctx, "Invalid bit range %d to %d.", low, high);
684 } else if (high >= symbol->width) {
685 expr_error(ctx, "Cannot select bits %d to %d of %d-bit field %s.",
686 low, high, symbol->width, symbol->name);
688 } else if (symbol->level == EXPR_L_NOMINAL
689 && (low != 0 || high != symbol->width - 1)) {
690 expr_error(ctx, "Cannot select subfield of nominal field %s.",
696 f->n_bits = high - low + 1;
699 f->n_bits = symbol->width;
706 parse_relop(struct expr_context *ctx, enum expr_relop *relop)
708 if (expr_relop_from_token(ctx->lexer->token.type, relop)) {
709 lexer_get(ctx->lexer);
712 expr_syntax_error(ctx, "expecting relational operator.");
718 assign_constant_set_type(struct expr_context *ctx,
719 struct expr_constant_set *cs,
720 enum expr_constant_type type)
722 if (!cs->n_values || cs->type == type) {
726 expr_syntax_error(ctx, "expecting %s.",
727 cs->type == EXPR_C_INTEGER ? "integer" : "string");
733 parse_macros(struct expr_context *ctx, struct expr_constant_set *cs,
734 size_t *allocated_values)
736 struct expr_constant_set *addr_set
737 = shash_find_data(ctx->macros, ctx->lexer->token.s);
739 expr_syntax_error(ctx, "expecting address set name.");
743 if (!assign_constant_set_type(ctx, cs, EXPR_C_INTEGER)) {
747 size_t n_values = cs->n_values + addr_set->n_values;
748 if (n_values >= *allocated_values) {
749 cs->values = xrealloc(cs->values, n_values * sizeof *cs->values);
750 *allocated_values = n_values;
752 for (size_t i = 0; i < addr_set->n_values; i++) {
753 cs->values[cs->n_values++] = addr_set->values[i];
760 parse_constant(struct expr_context *ctx, struct expr_constant_set *cs,
761 size_t *allocated_values)
763 if (cs->n_values >= *allocated_values) {
764 cs->values = x2nrealloc(cs->values, allocated_values,
768 if (ctx->lexer->token.type == LEX_T_STRING) {
769 if (!assign_constant_set_type(ctx, cs, EXPR_C_STRING)) {
772 cs->values[cs->n_values++].string = xstrdup(ctx->lexer->token.s);
773 lexer_get(ctx->lexer);
775 } else if (ctx->lexer->token.type == LEX_T_INTEGER ||
776 ctx->lexer->token.type == LEX_T_MASKED_INTEGER) {
777 if (!assign_constant_set_type(ctx, cs, EXPR_C_INTEGER)) {
781 union expr_constant *c = &cs->values[cs->n_values++];
782 c->value = ctx->lexer->token.value;
783 c->format = ctx->lexer->token.format;
784 c->masked = ctx->lexer->token.type == LEX_T_MASKED_INTEGER;
786 c->mask = ctx->lexer->token.mask;
788 lexer_get(ctx->lexer);
790 } else if (ctx->lexer->token.type == LEX_T_MACRO) {
791 if (!parse_macros(ctx, cs, allocated_values)) {
794 lexer_get(ctx->lexer);
797 expr_syntax_error(ctx, "expecting constant.");
802 /* Parses a single or {}-enclosed set of integer or string constants into 'cs',
803 * which the caller need not have initialized. Returns true on success, in
804 * which case the caller owns 'cs', false on failure, in which case 'cs' is
807 parse_constant_set(struct expr_context *ctx, struct expr_constant_set *cs)
809 size_t allocated_values = 0;
812 memset(cs, 0, sizeof *cs);
813 if (lexer_match(ctx->lexer, LEX_T_LCURLY)) {
815 cs->in_curlies = true;
817 if (!parse_constant(ctx, cs, &allocated_values)) {
821 lexer_match(ctx->lexer, LEX_T_COMMA);
822 } while (!lexer_match(ctx->lexer, LEX_T_RCURLY));
824 ok = parse_constant(ctx, cs, &allocated_values);
827 expr_constant_set_destroy(cs);
833 expr_constant_set_destroy(struct expr_constant_set *cs)
836 if (cs->type == EXPR_C_STRING) {
837 for (size_t i = 0; i < cs->n_values; i++) {
838 free(cs->values[i].string);
845 /* Adds a macro named 'name' to 'macros', replacing any existing macro with the
848 expr_macros_add(struct shash *macros, const char *name,
849 const char *const *values, size_t n_values)
851 /* Replace any existing entry for this name. */
852 expr_macros_remove(macros, name);
854 struct expr_constant_set *cs = xzalloc(sizeof *cs);
855 cs->type = EXPR_C_INTEGER;
856 cs->in_curlies = true;
858 cs->values = xmalloc(n_values * sizeof *cs->values);
859 for (size_t i = 0; i < n_values; i++) {
860 /* Use the lexer to convert each macro into the proper
863 lexer_init(&lex, values[i]);
865 if (lex.token.type != LEX_T_INTEGER
866 && lex.token.type != LEX_T_MASKED_INTEGER) {
867 VLOG_WARN("Invalid address set entry: '%s', token type: %d",
868 values[i], lex.token.type);
870 union expr_constant *c = &cs->values[cs->n_values++];
871 c->value = lex.token.value;
872 c->format = lex.token.format;
873 c->masked = lex.token.type == LEX_T_MASKED_INTEGER;
875 c->mask = lex.token.mask;
881 shash_add(macros, name, cs);
885 expr_macros_remove(struct shash *macros, const char *name)
887 struct expr_constant_set *cs = shash_find_and_delete(macros, name);
889 expr_constant_set_destroy(cs);
894 /* Destroy all contents of 'macros'. */
896 expr_macros_destroy(struct shash *macros)
898 struct shash_node *node, *next;
900 SHASH_FOR_EACH_SAFE (node, next, macros) {
901 struct expr_constant_set *cs = node->data;
903 shash_delete(macros, node);
904 expr_constant_set_destroy(cs);
909 expr_parse_primary(struct expr_context *ctx, bool *atomic)
912 if (lexer_match(ctx->lexer, LEX_T_LPAREN)) {
913 struct expr *e = expr_parse__(ctx);
914 if (!lexer_match(ctx->lexer, LEX_T_RPAREN)) {
916 expr_syntax_error(ctx, "expecting `)'.");
923 if (ctx->lexer->token.type == LEX_T_ID) {
926 struct expr_constant_set c;
928 if (!parse_field(ctx, &f)) {
932 if (!expr_relop_from_token(ctx->lexer->token.type, &r)) {
933 if (f.n_bits > 1 && !ctx->not) {
934 expr_error(ctx, "Explicit `!= 0' is required for inequality "
935 "test of multibit field against 0.");
941 union expr_constant *cst = xzalloc(sizeof *cst);
942 cst->format = LEX_F_HEXADECIMAL;
945 c.type = EXPR_C_INTEGER;
948 c.in_curlies = false;
949 return make_cmp(ctx, &f, EXPR_R_NE, &c);
950 } else if (parse_relop(ctx, &r) && parse_constant_set(ctx, &c)) {
951 return make_cmp(ctx, &f, r, &c);
956 struct expr_constant_set c1;
957 if (!parse_constant_set(ctx, &c1)) {
961 if (!expr_relop_from_token(ctx->lexer->token.type, NULL)
963 && c1.type == EXPR_C_INTEGER
964 && c1.values[0].format == LEX_F_DECIMAL
965 && !c1.values[0].masked
967 uint64_t x = ntohll(c1.values[0].value.integer);
970 expr_constant_set_destroy(&c1);
971 return expr_create_boolean(x);
977 if (!parse_relop(ctx, &r1) || !parse_field(ctx, &f)) {
978 expr_constant_set_destroy(&c1);
982 if (!expr_relop_from_token(ctx->lexer->token.type, NULL)) {
983 return make_cmp(ctx, &f, expr_relop_turn(r1), &c1);
987 struct expr_constant_set c2;
988 if (!parse_relop(ctx, &r2) || !parse_constant_set(ctx, &c2)) {
989 expr_constant_set_destroy(&c1);
992 /* Reject "1 == field == 2", "1 < field > 2", and so on. */
993 if (!(((r1 == EXPR_R_LT || r1 == EXPR_R_LE) &&
994 (r2 == EXPR_R_LT || r2 == EXPR_R_LE)) ||
995 ((r1 == EXPR_R_GT || r1 == EXPR_R_GE) &&
996 (r2 == EXPR_R_GT || r2 == EXPR_R_GE)))) {
997 expr_error(ctx, "Range expressions must have the form "
998 "`x < field < y' or `x > field > y', with each "
999 "`<' optionally replaced by `<=' or `>' by `>=').");
1000 expr_constant_set_destroy(&c1);
1001 expr_constant_set_destroy(&c2);
1005 struct expr *e1 = make_cmp(ctx, &f, expr_relop_turn(r1), &c1);
1006 struct expr *e2 = make_cmp(ctx, &f, r2, &c2);
1012 return expr_combine(EXPR_T_AND, e1, e2);
1017 static struct expr *
1018 expr_parse_not(struct expr_context *ctx)
1022 if (lexer_match(ctx->lexer, LEX_T_LOG_NOT)) {
1023 ctx->not = !ctx->not;
1024 struct expr *expr = expr_parse_primary(ctx, &atomic);
1025 ctx->not = !ctx->not;
1029 expr_error(ctx, "Missing parentheses around operand of !.");
1037 return expr_parse_primary(ctx, &atomic);
1042 expr_parse__(struct expr_context *ctx)
1044 struct expr *e = expr_parse_not(ctx);
1049 enum lex_type lex_type = ctx->lexer->token.type;
1050 if (lex_type == LEX_T_LOG_AND || lex_type == LEX_T_LOG_OR) {
1051 enum expr_type expr_type
1052 = lex_type == LEX_T_LOG_AND ? EXPR_T_AND : EXPR_T_OR;
1054 lexer_get(ctx->lexer);
1056 struct expr *e2 = expr_parse_not(ctx);
1061 e = expr_combine(expr_type, e, e2);
1062 } while (lexer_match(ctx->lexer, lex_type));
1063 if (ctx->lexer->token.type == LEX_T_LOG_AND
1064 || ctx->lexer->token.type == LEX_T_LOG_OR) {
1067 "&& and || must be parenthesized when used together.");
1074 /* Parses an expression using the symbols in 'symtab' from 'lexer'. If
1075 * successful, returns the new expression and sets '*errorp' to NULL. On
1076 * failure, returns NULL and sets '*errorp' to an explanatory error message.
1077 * The caller must eventually free the returned expression (with
1078 * expr_destroy()) or error (with free()). */
1080 expr_parse(struct lexer *lexer, const struct shash *symtab,
1081 const struct shash *macros, char **errorp)
1083 struct expr_context ctx = { .lexer = lexer,
1086 struct expr *e = expr_parse__(&ctx);
1087 *errorp = ctx.error;
1088 ovs_assert((ctx.error != NULL) != (e != NULL));
1092 /* Like expr_parse(), but the expression is taken from 's'. */
1094 expr_parse_string(const char *s, const struct shash *symtab,
1095 const struct shash *macros, char **errorp)
1100 lexer_init(&lexer, s);
1102 expr = expr_parse(&lexer, symtab, macros, errorp);
1103 if (!*errorp && lexer.token.type != LEX_T_END) {
1104 *errorp = xstrdup("Extra tokens at end of input.");
1108 lexer_destroy(&lexer);
1113 static struct expr_symbol *
1114 add_symbol(struct shash *symtab, const char *name, int width,
1115 const char *prereqs, enum expr_level level,
1116 bool must_crossproduct)
1118 struct expr_symbol *symbol = xzalloc(sizeof *symbol);
1119 symbol->name = xstrdup(name);
1120 symbol->prereqs = prereqs && prereqs[0] ? xstrdup(prereqs) : NULL;
1121 symbol->width = width;
1122 symbol->level = level;
1123 symbol->must_crossproduct = must_crossproduct;
1124 shash_add_assert(symtab, symbol->name, symbol);
1128 /* Adds field 'id' to symbol table 'symtab' under the given 'name'. Whenever
1129 * 'name' is referenced, expression annotation (see expr_annotate()) will
1130 * ensure that 'prereqs' are also true. If 'must_crossproduct' is true, then
1131 * conversion to flows will never attempt to use the field as a conjunctive
1132 * match dimension (see "Crossproducting" in the large comment on struct
1133 * expr_symbol in expr.h for an example).
1135 * A given field 'id' must only be used for a single symbol in a symbol table.
1136 * Use subfields to duplicate or subset a field (you can even make a subfield
1137 * include all the bits of the "parent" field if you like). */
1138 struct expr_symbol *
1139 expr_symtab_add_field(struct shash *symtab, const char *name,
1140 enum mf_field_id id, const char *prereqs,
1141 bool must_crossproduct)
1143 const struct mf_field *field = mf_from_id(id);
1144 struct expr_symbol *symbol;
1146 symbol = add_symbol(symtab, name, field->n_bits, prereqs,
1147 (field->maskable == MFM_FULLY
1151 symbol->field = field;
1156 parse_field_from_string(const char *s, const struct shash *symtab,
1157 struct expr_field *field, char **errorp)
1160 lexer_init(&lexer, s);
1163 struct expr_context ctx = { .lexer = &lexer, .symtab = symtab };
1164 bool ok = parse_field(&ctx, field);
1166 *errorp = ctx.error;
1167 } else if (lexer.token.type != LEX_T_END) {
1168 *errorp = xstrdup("Extra tokens at end of input.");
1172 lexer_destroy(&lexer);
1177 /* Adds 'name' as a subfield of a larger field in 'symtab'. Whenever
1178 * 'name' is referenced, expression annotation (see expr_annotate()) will
1179 * ensure that 'prereqs' are also true.
1181 * 'subfield' must describe the subfield as a string, e.g. "vlan.tci[0..11]"
1182 * for the low 12 bits of a larger field named "vlan.tci". */
1183 struct expr_symbol *
1184 expr_symtab_add_subfield(struct shash *symtab, const char *name,
1185 const char *prereqs, const char *subfield)
1187 struct expr_symbol *symbol;
1188 struct expr_field f;
1191 if (!parse_field_from_string(subfield, symtab, &f, &error)) {
1192 VLOG_WARN("%s: error parsing %s subfield (%s)", subfield, name, error);
1197 enum expr_level level = f.symbol->level;
1198 if (level != EXPR_L_ORDINAL) {
1199 VLOG_WARN("can't define %s as subfield of %s field %s",
1200 name, expr_level_to_string(level), f.symbol->name);
1203 symbol = add_symbol(symtab, name, f.n_bits, prereqs, level, false);
1204 symbol->expansion = xstrdup(subfield);
1208 /* Adds a string-valued symbol named 'name' to 'symtab' with the specified
1210 struct expr_symbol *
1211 expr_symtab_add_string(struct shash *symtab, const char *name,
1212 enum mf_field_id id, const char *prereqs)
1214 const struct mf_field *field = mf_from_id(id);
1215 struct expr_symbol *symbol;
1217 symbol = add_symbol(symtab, name, 0, prereqs, EXPR_L_NOMINAL, false);
1218 symbol->field = field;
1222 static enum expr_level
1223 expr_get_level(const struct expr *expr)
1225 const struct expr *sub;
1226 enum expr_level level = EXPR_L_ORDINAL;
1228 switch (expr->type) {
1230 return (expr->cmp.symbol->level == EXPR_L_NOMINAL
1236 LIST_FOR_EACH (sub, node, &expr->andor) {
1237 enum expr_level sub_level = expr_get_level(sub);
1238 level = MIN(level, sub_level);
1242 case EXPR_T_BOOLEAN:
1243 return EXPR_L_BOOLEAN;
1250 static enum expr_level
1251 expr_parse_level(const char *s, const struct shash *symtab, char **errorp)
1253 struct expr *expr = expr_parse_string(s, symtab, NULL, errorp);
1254 enum expr_level level = expr ? expr_get_level(expr) : EXPR_L_NOMINAL;
1259 /* Adds a predicate symbol, whose value is the given Boolean 'expression',
1260 * named 'name' to 'symtab'. For example, "ip4 && ip4.proto == 6" might be an
1261 * appropriate predicate named "tcp4". */
1262 struct expr_symbol *
1263 expr_symtab_add_predicate(struct shash *symtab, const char *name,
1264 const char *expansion)
1266 struct expr_symbol *symbol;
1267 enum expr_level level;
1270 level = expr_parse_level(expansion, symtab, &error);
1272 VLOG_WARN("%s: error parsing %s expansion (%s)",
1273 expansion, name, error);
1278 symbol = add_symbol(symtab, name, 1, NULL, level, false);
1279 symbol->expansion = xstrdup(expansion);
1283 /* Destroys 'symtab' and all of its symbols. */
1285 expr_symtab_destroy(struct shash *symtab)
1287 struct shash_node *node, *next;
1289 SHASH_FOR_EACH_SAFE (node, next, symtab) {
1290 struct expr_symbol *symbol = node->data;
1292 shash_delete(symtab, node);
1294 free(symbol->prereqs);
1295 free(symbol->expansion);
1302 static struct expr *
1303 expr_clone_cmp(struct expr *expr)
1305 struct expr *new = xmemdup(expr, sizeof *expr);
1306 if (!new->cmp.symbol->width) {
1307 new->cmp.string = xstrdup(new->cmp.string);
1312 static struct expr *
1313 expr_clone_andor(struct expr *expr)
1315 struct expr *new = expr_create_andor(expr->type);
1318 LIST_FOR_EACH (sub, node, &expr->andor) {
1319 struct expr *new_sub = expr_clone(sub);
1320 ovs_list_push_back(&new->andor, &new_sub->node);
1325 /* Returns a clone of 'expr'. This is a "deep copy": neither the returned
1326 * expression nor any of its substructure will be shared with 'expr'. */
1328 expr_clone(struct expr *expr)
1330 switch (expr->type) {
1332 return expr_clone_cmp(expr);
1336 return expr_clone_andor(expr);
1338 case EXPR_T_BOOLEAN:
1339 return expr_create_boolean(expr->boolean);
1344 /* Destroys 'expr' and all of the sub-expressions it references. */
1346 expr_destroy(struct expr *expr)
1352 struct expr *sub, *next;
1354 switch (expr->type) {
1356 if (!expr->cmp.symbol->width) {
1357 free(expr->cmp.string);
1363 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
1364 ovs_list_remove(&sub->node);
1369 case EXPR_T_BOOLEAN:
1377 /* An element in a linked list of symbols.
1379 * Used to detect when a symbol is being expanded recursively, to allow
1380 * flagging an error. */
1381 struct annotation_nesting {
1382 struct ovs_list node;
1383 const struct expr_symbol *symbol;
1386 struct expr *expr_annotate__(struct expr *, const struct shash *symtab,
1387 struct ovs_list *nesting, char **errorp);
1389 static struct expr *
1390 parse_and_annotate(const char *s, const struct shash *symtab,
1391 struct ovs_list *nesting, char **errorp)
1396 expr = expr_parse_string(s, symtab, NULL, &error);
1398 expr = expr_annotate__(expr, symtab, nesting, &error);
1403 *errorp = xasprintf("Error parsing expression `%s' encountered as "
1404 "prerequisite or predicate of initial expression: "
1411 static struct expr *
1412 expr_annotate_cmp(struct expr *expr, const struct shash *symtab,
1413 struct ovs_list *nesting, char **errorp)
1415 const struct expr_symbol *symbol = expr->cmp.symbol;
1416 const struct annotation_nesting *iter;
1417 LIST_FOR_EACH (iter, node, nesting) {
1418 if (iter->symbol == symbol) {
1419 *errorp = xasprintf("Recursive expansion of symbol `%s'.",
1426 struct annotation_nesting an;
1428 ovs_list_push_back(nesting, &an.node);
1430 struct expr *prereqs = NULL;
1431 if (symbol->prereqs) {
1432 prereqs = parse_and_annotate(symbol->prereqs, symtab, nesting, errorp);
1438 if (symbol->expansion) {
1439 if (symbol->level == EXPR_L_ORDINAL) {
1440 struct expr_field field;
1442 if (!parse_field_from_string(symbol->expansion, symtab,
1447 expr->cmp.symbol = field.symbol;
1448 mf_subvalue_shift(&expr->cmp.value, field.ofs);
1449 mf_subvalue_shift(&expr->cmp.mask, field.ofs);
1451 struct expr *expansion;
1453 expansion = parse_and_annotate(symbol->expansion, symtab,
1459 bool positive = (expr->cmp.value.integer & htonll(1)) != 0;
1460 positive ^= expr->cmp.relop == EXPR_R_NE;
1462 expr_not(expansion);
1470 ovs_list_remove(&an.node);
1471 return prereqs ? expr_combine(EXPR_T_AND, expr, prereqs) : expr;
1475 expr_destroy(prereqs);
1476 ovs_list_remove(&an.node);
1481 expr_annotate__(struct expr *expr, const struct shash *symtab,
1482 struct ovs_list *nesting, char **errorp)
1484 switch (expr->type) {
1486 return expr_annotate_cmp(expr, symtab, nesting, errorp);
1490 struct expr *sub, *next;
1492 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
1493 ovs_list_remove(&sub->node);
1494 struct expr *new_sub = expr_annotate__(sub, symtab,
1500 expr_insert_andor(expr, next, new_sub);
1506 case EXPR_T_BOOLEAN:
1515 /* "Annotates" 'expr', which does the following:
1517 * - Applies prerequisites, by locating each comparison operator whose
1518 * field has a prerequisite and adding a logical AND against those
1521 * - Expands references to subfield symbols, by replacing them by
1522 * references to their underlying field symbols (suitably shifted).
1524 * - Expands references to predicate symbols, by replacing them by the
1525 * expressions that they expand to.
1527 * In each case, annotation occurs recursively as necessary.
1529 * On failure, returns NULL and sets '*errorp' to an explanatory error
1530 * message, which the caller must free. */
1532 expr_annotate(struct expr *expr, const struct shash *symtab, char **errorp)
1534 struct ovs_list nesting = OVS_LIST_INITIALIZER(&nesting);
1535 return expr_annotate__(expr, symtab, &nesting, errorp);
1538 static struct expr *
1539 expr_simplify_ne(struct expr *expr)
1541 struct expr *new = NULL;
1542 const union mf_subvalue *value = &expr->cmp.value;
1543 const union mf_subvalue *mask = &expr->cmp.mask;
1544 int w = expr->cmp.symbol->width;
1547 for (i = 0; (i = bitwise_scan(mask, sizeof *mask, true, i, w)) < w; i++) {
1550 e = xzalloc(sizeof *e);
1551 e->type = EXPR_T_CMP;
1552 e->cmp.symbol = expr->cmp.symbol;
1553 e->cmp.relop = EXPR_R_EQ;
1554 bitwise_put_bit(&e->cmp.value, sizeof e->cmp.value, i,
1555 !bitwise_get_bit(value, sizeof *value, i));
1556 bitwise_put1(&e->cmp.mask, sizeof e->cmp.mask, i);
1558 new = expr_combine(EXPR_T_OR, new, e);
1567 static struct expr *
1568 expr_simplify_relational(struct expr *expr)
1570 const union mf_subvalue *value = &expr->cmp.value;
1571 int start, n_bits, end;
1573 find_bitwise_range(&expr->cmp.mask, expr->cmp.symbol->width,
1575 ovs_assert(n_bits > 0);
1576 end = start + n_bits;
1579 if (expr->cmp.relop == EXPR_R_LE || expr->cmp.relop == EXPR_R_GE) {
1580 new = xmemdup(expr, sizeof *expr);
1581 new->cmp.relop = EXPR_R_EQ;
1586 bool b = expr->cmp.relop == EXPR_R_LT || expr->cmp.relop == EXPR_R_LE;
1587 for (int z = bitwise_scan(value, sizeof *value, b, start, end);
1589 z = bitwise_scan(value, sizeof *value, b, z + 1, end)) {
1592 e = xmemdup(expr, sizeof *expr);
1593 e->cmp.relop = EXPR_R_EQ;
1594 bitwise_toggle_bit(&e->cmp.value, sizeof e->cmp.value, z);
1595 bitwise_zero(&e->cmp.value, sizeof e->cmp.value, start, z - start);
1596 bitwise_zero(&e->cmp.mask, sizeof e->cmp.mask, start, z - start);
1597 new = expr_combine(EXPR_T_OR, new, e);
1600 return new ? new : expr_create_boolean(false);
1603 /* Takes ownership of 'expr' and returns an equivalent expression whose
1604 * EXPR_T_CMP nodes use only tests for equality (EXPR_R_EQ). */
1606 expr_simplify(struct expr *expr)
1608 struct expr *sub, *next;
1610 switch (expr->type) {
1612 return (expr->cmp.relop == EXPR_R_EQ || !expr->cmp.symbol->width ? expr
1613 : expr->cmp.relop == EXPR_R_NE ? expr_simplify_ne(expr)
1614 : expr_simplify_relational(expr));
1618 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
1619 ovs_list_remove(&sub->node);
1620 expr_insert_andor(expr, next, expr_simplify(sub));
1622 return expr_fix(expr);
1624 case EXPR_T_BOOLEAN:
1630 static const struct expr_symbol *
1631 expr_is_cmp(const struct expr *expr)
1633 switch (expr->type) {
1635 return expr->cmp.symbol;
1639 const struct expr_symbol *prev = NULL;
1642 LIST_FOR_EACH (sub, node, &expr->andor) {
1643 const struct expr_symbol *symbol = expr_is_cmp(sub);
1644 if (!symbol || (prev && symbol != prev)) {
1652 case EXPR_T_BOOLEAN:
1662 const struct expr_symbol *relop;
1663 enum expr_type type;
1667 compare_expr_sort(const void *a_, const void *b_)
1669 const struct expr_sort *a = a_;
1670 const struct expr_sort *b = b_;
1672 if (a->type != b->type) {
1673 return a->type < b->type ? -1 : 1;
1674 } else if (a->relop) {
1675 int cmp = strcmp(a->relop->name, b->relop->name);
1680 enum expr_type a_type = a->expr->type;
1681 enum expr_type b_type = a->expr->type;
1682 return a_type < b_type ? -1 : a_type > b_type;
1683 } else if (a->type == EXPR_T_AND || a->type == EXPR_T_OR) {
1684 size_t a_len = ovs_list_size(&a->expr->andor);
1685 size_t b_len = ovs_list_size(&b->expr->andor);
1686 return a_len < b_len ? -1 : a_len > b_len;
1692 static struct expr *crush_cmps(struct expr *, const struct expr_symbol *);
1695 disjunction_matches_string(const struct expr *or, const char *s)
1697 const struct expr *sub;
1699 LIST_FOR_EACH (sub, node, &or->andor) {
1700 if (!strcmp(sub->cmp.string, s)) {
1708 /* Implementation of crush_cmps() for expr->type == EXPR_T_AND and a
1709 * string-typed 'symbol'. */
1710 static struct expr *
1711 crush_and_string(struct expr *expr, const struct expr_symbol *symbol)
1713 ovs_assert(!ovs_list_is_short(&expr->andor));
1715 struct expr *singleton = NULL;
1717 /* First crush each subexpression into either a single EXPR_T_CMP or an
1718 * EXPR_T_OR with EXPR_T_CMP subexpressions. */
1719 struct expr *sub, *next = NULL;
1720 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
1721 ovs_list_remove(&sub->node);
1722 struct expr *new = crush_cmps(sub, symbol);
1723 switch (new->type) {
1726 ovs_list_insert(&next->node, &new->node);
1729 bool match = !strcmp(new->cmp.string, singleton->cmp.string);
1733 return expr_create_boolean(false);
1740 ovs_list_insert(&next->node, &new->node);
1742 case EXPR_T_BOOLEAN:
1743 if (!new->boolean) {
1752 /* If we have a singleton, then the result is either the singleton itself
1753 * (if the ORs allow the singleton) or false. */
1755 LIST_FOR_EACH (sub, node, &expr->andor) {
1756 if (sub->type == EXPR_T_OR
1757 && !disjunction_matches_string(sub, singleton->cmp.string)) {
1759 return expr_create_boolean(false);
1762 ovs_list_remove(&singleton->node);
1767 /* Otherwise the result is the intersection of all of the ORs. */
1768 struct sset result = SSET_INITIALIZER(&result);
1769 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
1770 struct sset strings = SSET_INITIALIZER(&strings);
1771 const struct expr *s;
1772 LIST_FOR_EACH (s, node, &sub->andor) {
1773 sset_add(&strings, s->cmp.string);
1775 if (sset_is_empty(&result)) {
1776 sset_swap(&result, &strings);
1778 sset_intersect(&result, &strings);
1780 sset_destroy(&strings);
1782 if (sset_is_empty(&result)) {
1784 sset_destroy(&result);
1785 return expr_create_boolean(false);
1790 expr = expr_create_andor(EXPR_T_OR);
1793 SSET_FOR_EACH (string, &result) {
1794 sub = xmalloc(sizeof *sub);
1795 sub->type = EXPR_T_CMP;
1796 sub->cmp.symbol = symbol;
1797 sub->cmp.string = xstrdup(string);
1798 ovs_list_push_back(&expr->andor, &sub->node);
1800 sset_destroy(&result);
1801 return expr_fix(expr);
1804 /* Implementation of crush_cmps() for expr->type == EXPR_T_AND and a
1805 * numeric-typed 'symbol'. */
1806 static struct expr *
1807 crush_and_numeric(struct expr *expr, const struct expr_symbol *symbol)
1809 ovs_assert(!ovs_list_is_short(&expr->andor));
1811 union mf_subvalue value, mask;
1812 memset(&value, 0, sizeof value);
1813 memset(&mask, 0, sizeof mask);
1815 struct expr *sub, *next = NULL;
1816 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
1817 ovs_list_remove(&sub->node);
1818 struct expr *new = crush_cmps(sub, symbol);
1819 switch (new->type) {
1821 if (!mf_subvalue_intersect(&value, &mask,
1822 &new->cmp.value, &new->cmp.mask,
1826 return expr_create_boolean(false);
1833 ovs_list_insert(&next->node, &new->node);
1835 case EXPR_T_BOOLEAN:
1836 if (!new->boolean) {
1844 if (ovs_list_is_empty(&expr->andor)) {
1845 if (is_all_zeros(&mask, sizeof mask)) {
1847 return expr_create_boolean(true);
1850 cmp = xmalloc(sizeof *cmp);
1851 cmp->type = EXPR_T_CMP;
1852 cmp->cmp.symbol = symbol;
1853 cmp->cmp.relop = EXPR_R_EQ;
1854 cmp->cmp.value = value;
1855 cmp->cmp.mask = mask;
1859 } else if (ovs_list_is_short(&expr->andor)) {
1860 /* Transform "a && (b || c || d)" into "ab || ac || ad" where "ab" is
1861 * computed as "a && b", etc. */
1862 struct expr *disjuncts = expr_from_node(ovs_list_pop_front(&expr->andor));
1865 or = xmalloc(sizeof *or);
1866 or->type = EXPR_T_OR;
1867 ovs_list_init(&or->andor);
1869 ovs_assert(disjuncts->type == EXPR_T_OR);
1870 LIST_FOR_EACH_SAFE (sub, next, node, &disjuncts->andor) {
1871 ovs_assert(sub->type == EXPR_T_CMP);
1872 ovs_list_remove(&sub->node);
1873 if (mf_subvalue_intersect(&value, &mask,
1874 &sub->cmp.value, &sub->cmp.mask,
1875 &sub->cmp.value, &sub->cmp.mask)) {
1876 ovs_list_push_back(&or->andor, &sub->node);
1883 if (ovs_list_is_empty(&or->andor)) {
1885 return expr_create_boolean(false);
1886 } else if (ovs_list_is_short(&or->andor)) {
1887 struct expr *cmp = expr_from_node(ovs_list_pop_front(&or->andor));
1894 /* Transform "x && (a0 || a1) && (b0 || b1) && ..." into
1895 * "(xa0b0 || xa0b1 || xa1b0 || xa1b1) && ...". */
1896 struct expr *as = expr_from_node(ovs_list_pop_front(&expr->andor));
1897 struct expr *bs = expr_from_node(ovs_list_pop_front(&expr->andor));
1898 struct expr *new = NULL;
1901 or = xmalloc(sizeof *or);
1902 or->type = EXPR_T_OR;
1903 ovs_list_init(&or->andor);
1906 LIST_FOR_EACH (a, node, &as->andor) {
1907 union mf_subvalue a_value, a_mask;
1909 ovs_assert(a->type == EXPR_T_CMP);
1910 if (!mf_subvalue_intersect(&value, &mask,
1911 &a->cmp.value, &a->cmp.mask,
1912 &a_value, &a_mask)) {
1917 LIST_FOR_EACH (b, node, &bs->andor) {
1918 ovs_assert(b->type == EXPR_T_CMP);
1920 new = xmalloc(sizeof *new);
1921 new->type = EXPR_T_CMP;
1922 new->cmp.symbol = symbol;
1923 new->cmp.relop = EXPR_R_EQ;
1925 if (mf_subvalue_intersect(&a_value, &a_mask,
1926 &b->cmp.value, &b->cmp.mask,
1927 &new->cmp.value, &new->cmp.mask)) {
1928 ovs_list_push_back(&or->andor, &new->node);
1937 if (ovs_list_is_empty(&or->andor)) {
1940 return expr_create_boolean(false);
1941 } else if (ovs_list_is_short(&or->andor)) {
1942 struct expr *cmp = expr_from_node(ovs_list_pop_front(&or->andor));
1944 if (ovs_list_is_empty(&expr->andor)) {
1946 return crush_cmps(cmp, symbol);
1948 return crush_cmps(expr_combine(EXPR_T_AND, cmp, expr), symbol);
1950 } else if (!ovs_list_is_empty(&expr->andor)) {
1951 struct expr *e = expr_combine(EXPR_T_AND, or, expr);
1952 ovs_assert(!ovs_list_is_short(&e->andor));
1953 return crush_cmps(e, symbol);
1956 return crush_cmps(or, symbol);
1962 compare_cmps_3way(const struct expr *a, const struct expr *b)
1964 ovs_assert(a->cmp.symbol == b->cmp.symbol);
1965 if (!a->cmp.symbol->width) {
1966 return strcmp(a->cmp.string, b->cmp.string);
1968 int d = memcmp(&a->cmp.value, &b->cmp.value, sizeof a->cmp.value);
1970 d = memcmp(&a->cmp.mask, &b->cmp.mask, sizeof a->cmp.mask);
1977 compare_cmps_cb(const void *a_, const void *b_)
1979 const struct expr *const *ap = a_;
1980 const struct expr *const *bp = b_;
1981 const struct expr *a = *ap;
1982 const struct expr *b = *bp;
1983 return compare_cmps_3way(a, b);
1986 /* Implementation of crush_cmps() for expr->type == EXPR_T_OR. */
1987 static struct expr *
1988 crush_or(struct expr *expr, const struct expr_symbol *symbol)
1990 struct expr *sub, *next = NULL;
1992 /* First, crush all the subexpressions. That might eliminate the
1993 * OR-expression entirely; if so, return the result. Otherwise, 'expr'
1994 * is now a disjunction of cmps over the same symbol. */
1995 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
1996 ovs_list_remove(&sub->node);
1997 expr_insert_andor(expr, next, crush_cmps(sub, symbol));
1999 expr = expr_fix(expr);
2000 if (expr->type != EXPR_T_OR) {
2004 /* Sort subexpressions by value and mask, to bring together duplicates. */
2005 size_t n = ovs_list_size(&expr->andor);
2006 struct expr **subs = xmalloc(n * sizeof *subs);
2009 LIST_FOR_EACH (sub, node, &expr->andor) {
2014 qsort(subs, n, sizeof *subs, compare_cmps_cb);
2016 /* Eliminate duplicates. */
2017 ovs_list_init(&expr->andor);
2018 ovs_list_push_back(&expr->andor, &subs[0]->node);
2019 for (i = 1; i < n; i++) {
2020 struct expr *a = expr_from_node(ovs_list_back(&expr->andor));
2021 struct expr *b = subs[i];
2022 if (compare_cmps_3way(a, b)) {
2023 ovs_list_push_back(&expr->andor, &b->node);
2029 return expr_fix(expr);
2032 /* Takes ownership of 'expr', which must be a cmp in the sense determined by
2033 * 'expr_is_cmp(expr)', where 'symbol' is the symbol returned by that function.
2034 * Returns an equivalent expression owned by the caller that is a single
2035 * EXPR_T_CMP or a disjunction of them or a EXPR_T_BOOLEAN. */
2036 static struct expr *
2037 crush_cmps(struct expr *expr, const struct expr_symbol *symbol)
2039 switch (expr->type) {
2041 return crush_or(expr, symbol);
2044 return (symbol->width
2045 ? crush_and_numeric(expr, symbol)
2046 : crush_and_string(expr, symbol));
2051 case EXPR_T_BOOLEAN:
2059 static struct expr *
2060 expr_sort(struct expr *expr)
2062 size_t n = ovs_list_size(&expr->andor);
2063 struct expr_sort *subs = xmalloc(n * sizeof *subs);
2068 LIST_FOR_EACH (sub, node, &expr->andor) {
2070 subs[i].relop = expr_is_cmp(sub);
2071 subs[i].type = subs[i].relop ? EXPR_T_CMP : sub->type;
2076 qsort(subs, n, sizeof *subs, compare_expr_sort);
2078 ovs_list_init(&expr->andor);
2079 for (int i = 0; i < n; ) {
2080 if (subs[i].relop) {
2082 for (j = i + 1; j < n; j++) {
2083 if (subs[i].relop != subs[j].relop) {
2088 struct expr *crushed;
2090 crushed = crush_cmps(subs[i].expr, subs[i].relop);
2092 struct expr *combined = subs[i].expr;
2093 for (int k = i + 1; k < j; k++) {
2094 combined = expr_combine(EXPR_T_AND, combined,
2097 ovs_assert(!ovs_list_is_short(&combined->andor));
2098 crushed = crush_cmps(combined, subs[i].relop);
2100 if (crushed->type == EXPR_T_BOOLEAN) {
2101 if (!crushed->boolean) {
2102 for (int k = j; k < n; k++) {
2103 expr_destroy(subs[k].expr);
2112 expr = expr_combine(EXPR_T_AND, expr, crushed);
2116 expr = expr_combine(EXPR_T_AND, expr, subs[i++].expr);
2124 static struct expr *expr_normalize_or(struct expr *expr);
2126 /* Returns 'expr', which is an AND, reduced to OR(AND(clause)) where
2127 * a clause is a cmp or a disjunction of cmps on a single field. */
2128 static struct expr *
2129 expr_normalize_and(struct expr *expr)
2131 ovs_assert(expr->type == EXPR_T_AND);
2133 expr = expr_sort(expr);
2134 if (expr->type != EXPR_T_AND) {
2135 ovs_assert(expr->type == EXPR_T_BOOLEAN);
2140 LIST_FOR_EACH_SAFE (a, b, node, &expr->andor) {
2141 if (&b->node == &expr->andor
2142 || a->type != EXPR_T_CMP || b->type != EXPR_T_CMP
2143 || a->cmp.symbol != b->cmp.symbol) {
2145 } else if (a->cmp.symbol->width
2146 ? mf_subvalue_intersect(&a->cmp.value, &a->cmp.mask,
2147 &b->cmp.value, &b->cmp.mask,
2148 &b->cmp.value, &b->cmp.mask)
2149 : !strcmp(a->cmp.string, b->cmp.string)) {
2150 ovs_list_remove(&a->node);
2154 return expr_create_boolean(false);
2157 if (ovs_list_is_short(&expr->andor)) {
2158 struct expr *sub = expr_from_node(ovs_list_front(&expr->andor));
2164 LIST_FOR_EACH (sub, node, &expr->andor) {
2165 if (sub->type == EXPR_T_CMP) {
2169 ovs_assert(sub->type == EXPR_T_OR);
2170 const struct expr_symbol *symbol = expr_is_cmp(sub);
2171 if (!symbol || symbol->must_crossproduct) {
2172 struct expr *or = expr_create_andor(EXPR_T_OR);
2175 LIST_FOR_EACH (k, node, &sub->andor) {
2176 struct expr *and = expr_create_andor(EXPR_T_AND);
2179 LIST_FOR_EACH (m, node, &expr->andor) {
2180 struct expr *term = m == sub ? k : m;
2181 if (term->type == EXPR_T_AND) {
2184 LIST_FOR_EACH (p, node, &term->andor) {
2185 struct expr *new = expr_clone(p);
2186 ovs_list_push_back(&and->andor, &new->node);
2189 struct expr *new = expr_clone(term);
2190 ovs_list_push_back(&and->andor, &new->node);
2193 ovs_list_push_back(&or->andor, &and->node);
2196 return expr_normalize_or(or);
2202 static struct expr *
2203 expr_normalize_or(struct expr *expr)
2205 struct expr *sub, *next;
2207 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
2208 if (sub->type == EXPR_T_AND) {
2209 ovs_list_remove(&sub->node);
2211 struct expr *new = expr_normalize_and(sub);
2212 if (new->type == EXPR_T_BOOLEAN) {
2219 expr_insert_andor(expr, next, new);
2222 ovs_assert(sub->type == EXPR_T_CMP);
2225 if (ovs_list_is_empty(&expr->andor)) {
2227 return expr_create_boolean(false);
2229 if (ovs_list_is_short(&expr->andor)) {
2230 struct expr *sub = expr_from_node(ovs_list_pop_front(&expr->andor));
2238 /* Takes ownership of 'expr', which is either a constant "true" or "false" or
2239 * an expression in terms of only relationals, AND, and OR. Returns either a
2240 * constant "true" or "false" or 'expr' reduced to OR(AND(clause)) where a
2241 * clause is a cmp or a disjunction of cmps on a single field. This form is
2242 * significant because it is a form that can be directly converted to OpenFlow
2243 * flows with the Open vSwitch "conjunctive match" extension.
2245 * 'expr' must already have been simplified, with expr_simplify(). */
2247 expr_normalize(struct expr *expr)
2249 switch (expr->type) {
2254 return expr_normalize_and(expr);
2257 return expr_normalize_or(expr);
2259 case EXPR_T_BOOLEAN:
2265 /* Creates, initializes, and returns a new 'struct expr_match'. If 'm' is
2266 * nonnull then it is copied into the new expr_match, otherwise the new
2267 * expr_match's 'match' member is initialized to a catch-all match for the
2268 * caller to refine in-place.
2270 * If 'conj_id' is nonzero, adds one conjunction based on 'conj_id', 'clause',
2271 * and 'n_clauses' to the returned 'struct expr_match', otherwise the
2272 * expr_match will not have any conjunctions.
2274 * The caller should use expr_match_add() to add the expr_match to a hash table
2275 * after it is finalized. */
2276 static struct expr_match *
2277 expr_match_new(const struct match *m, uint8_t clause, uint8_t n_clauses,
2280 struct expr_match *match = xmalloc(sizeof *match);
2284 match_init_catchall(&match->match);
2287 match->conjunctions = xmalloc(sizeof *match->conjunctions);
2288 match->conjunctions[0].id = conj_id;
2289 match->conjunctions[0].clause = clause;
2290 match->conjunctions[0].n_clauses = n_clauses;
2292 match->allocated = 1;
2294 match->conjunctions = NULL;
2296 match->allocated = 0;
2301 /* Adds 'match' to hash table 'matches', which becomes the new owner of
2304 * This might actually destroy 'match' because it gets merged together with
2305 * some existing conjunction.*/
2307 expr_match_add(struct hmap *matches, struct expr_match *match)
2309 uint32_t hash = match_hash(&match->match, 0);
2310 struct expr_match *m;
2312 HMAP_FOR_EACH_WITH_HASH (m, hmap_node, hash, matches) {
2313 if (match_equal(&m->match, &match->match)) {
2314 if (!m->n || !match->n) {
2315 free(m->conjunctions);
2316 m->conjunctions = NULL;
2320 ovs_assert(match->n == 1);
2321 if (m->n >= m->allocated) {
2322 m->conjunctions = x2nrealloc(m->conjunctions,
2324 sizeof *m->conjunctions);
2326 m->conjunctions[m->n++] = match->conjunctions[0];
2328 free(match->conjunctions);
2334 hmap_insert(matches, &match->hmap_node, hash);
2338 constrain_match(const struct expr *expr,
2339 bool (*lookup_port)(const void *aux, const char *port_name,
2340 unsigned int *portp),
2341 const void *aux, struct match *m)
2343 ovs_assert(expr->type == EXPR_T_CMP);
2344 if (expr->cmp.symbol->width) {
2345 mf_mask_subfield(expr->cmp.symbol->field, &expr->cmp.value,
2346 &expr->cmp.mask, m);
2349 if (!lookup_port(aux, expr->cmp.string, &port)) {
2353 struct mf_subfield sf;
2354 sf.field = expr->cmp.symbol->field;
2356 sf.n_bits = expr->cmp.symbol->field->n_bits;
2358 union mf_subvalue x;
2359 memset(&x, 0, sizeof x);
2360 x.integer = htonll(port);
2362 mf_write_subfield(&sf, &x, m);
2368 add_disjunction(const struct expr *or,
2369 bool (*lookup_port)(const void *aux, const char *port_name,
2370 unsigned int *portp),
2372 struct match *m, uint8_t clause, uint8_t n_clauses,
2373 uint32_t conj_id, struct hmap *matches)
2378 ovs_assert(or->type == EXPR_T_OR);
2379 LIST_FOR_EACH (sub, node, &or->andor) {
2380 struct expr_match *match = expr_match_new(m, clause, n_clauses,
2382 if (constrain_match(sub, lookup_port, aux, &match->match)) {
2383 expr_match_add(matches, match);
2386 free(match->conjunctions);
2391 /* If n == 1, then this didn't really need to be a disjunction. Oh well,
2392 * that shouldn't happen much. */
2397 add_conjunction(const struct expr *and,
2398 bool (*lookup_port)(const void *aux, const char *port_name,
2399 unsigned int *portp),
2400 const void *aux, uint32_t *n_conjsp, struct hmap *matches)
2406 match_init_catchall(&match);
2408 ovs_assert(and->type == EXPR_T_AND);
2409 LIST_FOR_EACH (sub, node, &and->andor) {
2410 switch (sub->type) {
2412 if (!constrain_match(sub, lookup_port, aux, &match)) {
2420 case EXPR_T_BOOLEAN:
2426 expr_match_add(matches, expr_match_new(&match, 0, 0, 0));
2427 } else if (n_clauses == 1) {
2428 LIST_FOR_EACH (sub, node, &and->andor) {
2429 if (sub->type == EXPR_T_OR) {
2430 add_disjunction(sub, lookup_port, aux, &match, 0, 0, 0,
2437 LIST_FOR_EACH (sub, node, &and->andor) {
2438 if (sub->type == EXPR_T_OR) {
2439 if (!add_disjunction(sub, lookup_port, aux, &match, clause++,
2440 n_clauses, *n_conjsp, matches)) {
2441 /* This clause can't ever match, so we might as well skip
2442 * adding the other clauses--the overall disjunctive flow
2443 * can't ever match. Ideally we would also back out all of
2444 * the clauses we already added, but that seems like a lot
2445 * of trouble for a case that might never occur in
2452 /* Add the flow that matches on conj_id. */
2453 match_set_conj_id(&match, *n_conjsp);
2454 expr_match_add(matches, expr_match_new(&match, 0, 0, 0));
2459 add_cmp_flow(const struct expr *cmp,
2460 bool (*lookup_port)(const void *aux, const char *port_name,
2461 unsigned int *portp),
2462 const void *aux, struct hmap *matches)
2464 struct expr_match *m = expr_match_new(NULL, 0, 0, 0);
2465 if (constrain_match(cmp, lookup_port, aux, &m->match)) {
2466 expr_match_add(matches, m);
2472 /* Converts 'expr', which must be in the form returned by expr_normalize(), to
2473 * a collection of Open vSwitch flows in 'matches', which this function
2474 * initializes to an hmap of "struct expr_match" structures. Returns the
2475 * number of conjunctive match IDs consumed by 'matches', which uses
2476 * conjunctive match IDs beginning with 0; the caller must offset or remap them
2477 * into the desired range as necessary.
2479 * The matches inserted into 'matches' will be of three distinct kinds:
2481 * - Ordinary flows. The caller should add these OpenFlow flows with
2482 * its desired actions.
2484 * - Conjunctive flows, distinguished by 'n > 0' in the expr_match
2485 * structure. The caller should add these OpenFlow flows with the
2486 * conjunction(id, k/n) actions as specified in the 'conjunctions' array,
2487 * remapping the ids.
2489 * - conj_id flows, distinguished by matching on the "conj_id" field. The
2490 * caller should remap the conj_id and add the OpenFlow flow with its
2493 * 'lookup_port' must be a function to map from a port name to a port number.
2494 * When successful, 'lookup_port' stores the port number into '*portp' and
2495 * returns true; when there is no port by the given name, it returns false.
2496 * 'aux' is passed to 'lookup_port' as auxiliary data. Any comparisons against
2497 * string fields in 'expr' are translated into integers through this function.
2498 * A comparison against a string that is not in 'ports' acts like a Boolean
2499 * "false"; that is, it will always fail to match. For a simple expression,
2500 * this means that the overall expression always fails to match, but an
2501 * expression with a disjunction on the string field might still match on other
2504 * (This treatment of string fields might be too simplistic in general, but it
2505 * seems reasonable for now when string fields are used only for ports.) */
2507 expr_to_matches(const struct expr *expr,
2508 bool (*lookup_port)(const void *aux, const char *port_name,
2509 unsigned int *portp),
2510 const void *aux, struct hmap *matches)
2512 uint32_t n_conjs = 0;
2515 switch (expr->type) {
2517 add_cmp_flow(expr, lookup_port, aux, matches);
2521 add_conjunction(expr, lookup_port, aux, &n_conjs, matches);
2525 if (expr_is_cmp(expr)) {
2528 LIST_FOR_EACH (sub, node, &expr->andor) {
2529 add_cmp_flow(sub, lookup_port, aux, matches);
2534 LIST_FOR_EACH (sub, node, &expr->andor) {
2535 if (sub->type == EXPR_T_AND) {
2536 add_conjunction(sub, lookup_port, aux, &n_conjs, matches);
2538 add_cmp_flow(sub, lookup_port, aux, matches);
2544 case EXPR_T_BOOLEAN:
2545 if (expr->boolean) {
2546 struct expr_match *m = expr_match_new(NULL, 0, 0, 0);
2547 expr_match_add(matches, m);
2556 /* Destroys all of the 'struct expr_match'es in 'matches', as well as the
2557 * 'matches' hmap itself. */
2559 expr_matches_destroy(struct hmap *matches)
2561 struct expr_match *m;
2563 HMAP_FOR_EACH_POP (m, hmap_node, matches) {
2564 free(m->conjunctions);
2567 hmap_destroy(matches);
2570 /* Prints a representation of the 'struct expr_match'es in 'matches' to
2573 expr_matches_print(const struct hmap *matches, FILE *stream)
2575 if (hmap_is_empty(matches)) {
2576 fputs("(no flows)\n", stream);
2580 const struct expr_match *m;
2581 HMAP_FOR_EACH (m, hmap_node, matches) {
2582 char *s = match_to_string(&m->match, OFP_DEFAULT_PRIORITY);
2587 for (int i = 0; i < m->n; i++) {
2588 const struct cls_conjunction *c = &m->conjunctions[i];
2589 fprintf(stream, "%c conjunction(%"PRIu32", %d/%d)",
2590 i == 0 ? ':' : ',', c->id, c->clause, c->n_clauses);
2597 /* Returns true if 'expr' honors the invariants for expressions (see the large
2598 * comment above "struct expr" in expr.h), false otherwise. */
2600 expr_honors_invariants(const struct expr *expr)
2602 const struct expr *sub;
2604 switch (expr->type) {
2606 if (expr->cmp.symbol->width) {
2607 for (int i = 0; i < ARRAY_SIZE(expr->cmp.value.be64); i++) {
2608 if (expr->cmp.value.be64[i] & ~expr->cmp.mask.be64[i]) {
2617 if (ovs_list_is_short(&expr->andor)) {
2620 LIST_FOR_EACH (sub, node, &expr->andor) {
2621 if (sub->type == expr->type || !expr_honors_invariants(sub)) {
2627 case EXPR_T_BOOLEAN:
2636 expr_is_normalized_and(const struct expr *expr)
2638 /* XXX should also check that no symbol is repeated. */
2639 const struct expr *sub;
2641 LIST_FOR_EACH (sub, node, &expr->andor) {
2642 if (!expr_is_cmp(sub)) {
2649 /* Returns true if 'expr' is in the form returned by expr_normalize(), false
2652 expr_is_normalized(const struct expr *expr)
2654 switch (expr->type) {
2659 return expr_is_normalized_and(expr);
2662 if (!expr_is_cmp(expr)) {
2663 const struct expr *sub;
2665 LIST_FOR_EACH (sub, node, &expr->andor) {
2666 if (!expr_is_cmp(sub) && !expr_is_normalized_and(sub)) {
2673 case EXPR_T_BOOLEAN:
2681 /* Action parsing helper. */
2683 /* Expands 'f' repeatedly as long as it has an expansion, that is, as long as
2684 * it is a subfield or a predicate. Adds any prerequisites for 'f' to
2687 * If 'rw', verifies that 'f' is a read/write field.
2689 * Returns true if successful, false if an error was encountered (in which case
2690 * 'ctx->error' reports the particular error). */
2692 expand_symbol(struct expr_context *ctx, bool rw,
2693 struct expr_field *f, struct expr **prereqsp)
2695 const struct expr_symbol *orig_symbol = f->symbol;
2697 if (f->symbol->expansion && f->symbol->level != EXPR_L_ORDINAL) {
2698 expr_error(ctx, "Predicate symbol %s used where lvalue required.",
2704 /* Accumulate prerequisites. */
2705 if (f->symbol->prereqs) {
2706 struct ovs_list nesting = OVS_LIST_INITIALIZER(&nesting);
2709 e = parse_and_annotate(f->symbol->prereqs, ctx->symtab, &nesting,
2712 expr_error(ctx, "%s", error);
2716 *prereqsp = expr_combine(EXPR_T_AND, *prereqsp, e);
2719 /* If there's no expansion, we're done. */
2720 if (!f->symbol->expansion) {
2725 struct expr_field expansion;
2727 if (!parse_field_from_string(f->symbol->expansion, ctx->symtab,
2728 &expansion, &error)) {
2729 expr_error(ctx, "%s", error);
2733 f->symbol = expansion.symbol;
2734 f->ofs += expansion.ofs;
2737 if (rw && !f->symbol->field->writable) {
2738 expr_error(ctx, "Field %s is not modifiable.", orig_symbol->name);
2746 mf_subfield_from_expr_field(const struct expr_field *f, struct mf_subfield *sf)
2748 sf->field = f->symbol->field;
2750 sf->n_bits = f->n_bits ? f->n_bits : f->symbol->field->n_bits;
2754 init_stack_action(const struct expr_field *f, struct ofpact_stack *stack)
2756 mf_subfield_from_expr_field(f, &stack->subfield);
2759 static char * OVS_WARN_UNUSED_RESULT
2760 parse_assignment(struct lexer *lexer, struct expr_field *dst,
2761 const struct shash *symtab, bool exchange,
2762 bool (*lookup_port)(const void *aux, const char *port_name,
2763 unsigned int *portp),
2764 const void *aux, struct ofpbuf *ofpacts,
2765 struct expr **prereqsp)
2767 struct expr_context ctx = { .lexer = lexer, .symtab = symtab };
2768 struct expr *prereqs = NULL;
2770 /* Parse destination and do basic checking. */
2771 const struct expr_symbol *orig_dst = dst->symbol;
2772 if (!expand_symbol(&ctx, true, dst, &prereqs)) {
2776 if (exchange || ctx.lexer->token.type == LEX_T_ID) {
2777 struct expr_field src;
2778 if (!parse_field(&ctx, &src)) {
2781 const struct expr_symbol *orig_src = src.symbol;
2782 if (!expand_symbol(&ctx, exchange, &src, &prereqs)) {
2786 if ((dst->symbol->width != 0) != (src.symbol->width != 0)) {
2789 "Can't exchange %s field (%s) with %s field (%s).",
2790 orig_dst->width ? "integer" : "string",
2792 orig_src->width ? "integer" : "string",
2796 "Can't assign %s field (%s) to %s field (%s).",
2797 orig_src->width ? "integer" : "string",
2799 orig_dst->width ? "integer" : "string",
2805 if (dst->n_bits != src.n_bits) {
2808 "Can't exchange %d-bit field with %d-bit field.",
2809 dst->n_bits, src.n_bits);
2812 "Can't assign %d-bit value to %d-bit destination.",
2813 src.n_bits, dst->n_bits);
2816 } else if (!dst->n_bits &&
2817 dst->symbol->field->n_bits != src.symbol->field->n_bits) {
2818 expr_error(&ctx, "String fields %s and %s are incompatible for "
2819 "%s.", orig_dst->name, orig_src->name,
2820 exchange ? "exchange" : "assignment");
2825 init_stack_action(&src, ofpact_put_STACK_PUSH(ofpacts));
2826 init_stack_action(dst, ofpact_put_STACK_PUSH(ofpacts));
2827 init_stack_action(&src, ofpact_put_STACK_POP(ofpacts));
2828 init_stack_action(dst, ofpact_put_STACK_POP(ofpacts));
2830 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2831 mf_subfield_from_expr_field(&src, &move->src);
2832 mf_subfield_from_expr_field(dst, &move->dst);
2835 struct expr_constant_set cs;
2836 if (!parse_constant_set(&ctx, &cs)) {
2840 if (!type_check(&ctx, dst, &cs)) {
2841 goto exit_destroy_cs;
2843 if (cs.in_curlies) {
2844 expr_error(&ctx, "Assignments require a single value.");
2845 goto exit_destroy_cs;
2848 union expr_constant *c = cs.values;
2849 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2850 sf->field = dst->symbol->field;
2851 if (dst->symbol->width) {
2852 mf_subvalue_shift(&c->value, dst->ofs);
2854 memset(&c->mask, 0, sizeof c->mask);
2855 bitwise_one(&c->mask, sizeof c->mask, dst->ofs, dst->n_bits);
2857 mf_subvalue_shift(&c->mask, dst->ofs);
2861 &c->value.u8[sizeof c->value - sf->field->n_bytes],
2862 sf->field->n_bytes);
2864 &c->mask.u8[sizeof c->mask - sf->field->n_bytes],
2865 sf->field->n_bytes);
2868 if (!lookup_port(aux, c->string, &port)) {
2871 bitwise_put(port, &sf->value,
2872 sf->field->n_bytes, 0, sf->field->n_bits);
2873 bitwise_one(&sf->mask, sf->field->n_bytes, 0, sf->field->n_bits);
2875 /* If the logical input port is being zeroed, clear the OpenFlow
2876 * ingress port also, to allow a packet to be sent back to its
2878 if (!port && sf->field->id == MFF_LOG_INPORT) {
2879 sf = ofpact_put_SET_FIELD(ofpacts);
2880 sf->field = mf_from_id(MFF_IN_PORT);
2881 bitwise_one(&sf->mask,
2882 sf->field->n_bytes, 0, sf->field->n_bits);
2887 expr_constant_set_destroy(&cs);
2892 expr_destroy(prereqs);
2895 *prereqsp = prereqs;
2899 /* A helper for actions_parse(), to parse an OVN assignment action in the form
2900 * "field = value" or "field = field2" into 'ofpacts'. The caller must have
2901 * already parsed and skipped the left-hand side "field =" and pass in the
2902 * field as 'dst'. Other parameters and return value match those for
2903 * actions_parse(). */
2904 char * OVS_WARN_UNUSED_RESULT
2905 expr_parse_assignment(struct lexer *lexer, struct expr_field *dst,
2906 const struct shash *symtab,
2907 bool (*lookup_port)(const void *aux,
2908 const char *port_name,
2909 unsigned int *portp),
2911 struct ofpbuf *ofpacts, struct expr **prereqsp)
2913 return parse_assignment(lexer, dst, symtab, false, lookup_port, aux,
2917 /* A helper for actions_parse(), to parse an OVN exchange action in the form
2918 * "field1 <-> field2" into 'ofpacts'. The caller must have already parsed and
2919 * skipped the left-hand side "field1 <->" and pass in 'field1'. Other
2920 * parameters and return value match those for actions_parse(). */
2921 char * OVS_WARN_UNUSED_RESULT
2922 expr_parse_exchange(struct lexer *lexer, struct expr_field *field,
2923 const struct shash *symtab,
2924 bool (*lookup_port)(const void *aux,
2925 const char *port_name,
2926 unsigned int *portp),
2928 struct ofpbuf *ofpacts, struct expr **prereqsp)
2930 return parse_assignment(lexer, field, symtab, true, lookup_port, aux,
2934 /* Parses a field or subfield from 'lexer' into 'field', obtaining field names
2935 * from 'symtab'. Returns NULL if successful, otherwise an error message owned
2937 char * OVS_WARN_UNUSED_RESULT
2938 expr_parse_field(struct lexer *lexer, const struct shash *symtab,
2939 struct expr_field *field)
2941 struct expr_context ctx = { .lexer = lexer, .symtab = symtab };
2942 if (!parse_field(&ctx, field)) {
2943 memset(field, 0, sizeof *field);
2948 /* Takes 'field', which was presumably parsed by expr_parse_field(), and
2949 * converts it into mf_subfield 'sf' and a set of prerequisites in '*prereqsp'.
2951 * 'n_bits' specifies the number of bits that the field must have, and 0
2952 * indicates a string field; reports an error if 'field' has a different type
2953 * or width. If 'rw' is true, it is an error if 'field' is read-only. Uses
2954 * 'symtab 'for expanding references and 'lexer' for error reporting.
2956 * Returns NULL if successful, otherwise an error message owned by the
2958 char * OVS_WARN_UNUSED_RESULT
2959 expr_expand_field(struct lexer *lexer, const struct shash *symtab,
2960 const struct expr_field *orig_field, int n_bits, bool rw,
2961 struct mf_subfield *sf, struct expr **prereqsp)
2963 struct expr_context ctx = { .lexer = lexer, .symtab = symtab };
2964 struct expr *prereqs = NULL;
2966 struct expr_field field = *orig_field;
2967 if (!expand_symbol(&ctx, rw, &field, &prereqs)) {
2970 ovs_assert(field.n_bits == orig_field->n_bits);
2972 if (n_bits != field.n_bits) {
2973 if (n_bits && field.n_bits) {
2974 expr_error(&ctx, "Cannot use %d-bit field %s[%d..%d] "
2975 "where %d-bit field is required.",
2976 orig_field->n_bits, orig_field->symbol->name,
2978 orig_field->ofs + orig_field->n_bits - 1, n_bits);
2979 } else if (n_bits) {
2980 expr_error(&ctx, "Cannot use string field %s where numeric "
2981 "field is required.",
2982 orig_field->symbol->name);
2984 expr_error(&ctx, "Cannot use numeric field %s where string "
2985 "field is required.",
2986 orig_field->symbol->name);
2992 mf_subfield_from_expr_field(&field, sf);
2993 *prereqsp = prereqs;
2995 memset(sf, 0, sizeof *sf);
2996 expr_destroy(prereqs);
3002 char * OVS_WARN_UNUSED_RESULT
3003 expr_parse_constant_set(struct lexer *lexer, const struct shash *symtab,
3004 struct expr_constant_set *cs)
3006 struct expr_context ctx = { .lexer = lexer, .symtab = symtab };
3007 parse_constant_set(&ctx, cs);