]> git.proxmox.com Git - ovs.git/blame - ovn/lib/expr.c
expr: Fine-tune parser error message for common typo.
[ovs.git] / ovn / lib / expr.c
CommitLineData
e0840f11 1/*
f1c16a85 2 * Copyright (c) 2015, 2016 Nicira, Inc.
e0840f11
BP
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <config.h>
f4248336 18#include "byte-order.h"
ee89ea7b 19#include "openvswitch/json.h"
b4970837 20#include "logical-fields.h"
b598f214 21#include "openvswitch/dynamic-string.h"
e29747e4 22#include "openvswitch/match.h"
b598f214
BW
23#include "openvswitch/ofp-actions.h"
24#include "openvswitch/vlog.h"
ee89ea7b 25#include "openvswitch/shash.h"
8b2ed684
AR
26#include "ovn/expr.h"
27#include "ovn/lex.h"
f386a8a7 28#include "simap.h"
9d4aecca 29#include "sset.h"
ee89ea7b 30#include "util.h"
e0840f11
BP
31
32VLOG_DEFINE_THIS_MODULE(expr);
33\f
34/* Returns the name of measurement level 'level'. */
35const char *
36expr_level_to_string(enum expr_level level)
37{
38 switch (level) {
39 case EXPR_L_NOMINAL: return "nominal";
40 case EXPR_L_BOOLEAN: return "Boolean";
41 case EXPR_L_ORDINAL: return "ordinal";
42 default: OVS_NOT_REACHED();
43 }
44}
45\f
46/* Relational operators. */
47
48/* Returns a string form of relational operator 'relop'. */
49const char *
50expr_relop_to_string(enum expr_relop relop)
51{
52 switch (relop) {
53 case EXPR_R_EQ: return "==";
54 case EXPR_R_NE: return "!=";
55 case EXPR_R_LT: return "<";
56 case EXPR_R_LE: return "<=";
57 case EXPR_R_GT: return ">";
58 case EXPR_R_GE: return ">=";
59 default: OVS_NOT_REACHED();
60 }
61}
62
63bool
64expr_relop_from_token(enum lex_type type, enum expr_relop *relop)
65{
66 enum expr_relop r;
67
68 switch ((int) type) {
69 case LEX_T_EQ: r = EXPR_R_EQ; break;
70 case LEX_T_NE: r = EXPR_R_NE; break;
71 case LEX_T_LT: r = EXPR_R_LT; break;
72 case LEX_T_LE: r = EXPR_R_LE; break;
73 case LEX_T_GT: r = EXPR_R_GT; break;
74 case LEX_T_GE: r = EXPR_R_GE; break;
75 default: return false;
76 }
77
78 if (relop) {
79 *relop = r;
80 }
81 return true;
82}
83
84/* Returns the relational operator that 'relop' becomes if you turn the
85 * relation's operands around, e.g. EXPR_R_EQ does not change because "a == b"
86 * and "b == a" are equivalent, but EXPR_R_LE becomes EXPR_R_GE because "a <=
87 * b" is equivalent to "b >= a". */
88static enum expr_relop
89expr_relop_turn(enum expr_relop relop)
90{
91 switch (relop) {
92 case EXPR_R_EQ: return EXPR_R_EQ;
93 case EXPR_R_NE: return EXPR_R_NE;
94 case EXPR_R_LT: return EXPR_R_GT;
95 case EXPR_R_LE: return EXPR_R_GE;
96 case EXPR_R_GT: return EXPR_R_LT;
97 case EXPR_R_GE: return EXPR_R_LE;
98 default: OVS_NOT_REACHED();
99 }
100}
101
102/* Returns the relational operator that is the opposite of 'relop'. */
103static enum expr_relop
104expr_relop_invert(enum expr_relop relop)
105{
106 switch (relop) {
107 case EXPR_R_EQ: return EXPR_R_NE;
108 case EXPR_R_NE: return EXPR_R_EQ;
109 case EXPR_R_LT: return EXPR_R_GE;
110 case EXPR_R_LE: return EXPR_R_GT;
111 case EXPR_R_GT: return EXPR_R_LE;
112 case EXPR_R_GE: return EXPR_R_LT;
113 default: OVS_NOT_REACHED();
114 }
115}
116\f
117/* Constructing and manipulating expressions. */
118
119/* Creates and returns a logical AND or OR expression (according to 'type',
120 * which must be EXPR_T_AND or EXPR_T_OR) that initially has no
121 * sub-expressions. (To satisfy the invariants for expressions, the caller
122 * must add at least two sub-expressions whose types are different from
123 * 'type'.) */
124struct expr *
125expr_create_andor(enum expr_type type)
126{
127 struct expr *e = xmalloc(sizeof *e);
128 e->type = type;
417e7e66 129 ovs_list_init(&e->andor);
e0840f11
BP
130 return e;
131}
132
133/* Returns a logical AND or OR expression (according to 'type', which must be
134 * EXPR_T_AND or EXPR_T_OR) whose sub-expressions are 'a' and 'b', with some
135 * flexibility:
136 *
137 * - If 'a' or 'b' is NULL, just returns the other one (which means that if
138 * that other one is not of the given 'type', then the returned
139 * expression is not either).
140 *
141 * - If 'a' or 'b', or both, have type 'type', then they are combined into
142 * a single node that satisfies the invariants for expressions. */
143struct expr *
144expr_combine(enum expr_type type, struct expr *a, struct expr *b)
145{
146 if (!a) {
147 return b;
148 } else if (!b) {
149 return a;
150 } else if (a->type == type) {
151 if (b->type == type) {
417e7e66 152 ovs_list_splice(&a->andor, b->andor.next, &b->andor);
e0840f11
BP
153 free(b);
154 } else {
417e7e66 155 ovs_list_push_back(&a->andor, &b->node);
e0840f11
BP
156 }
157 return a;
158 } else if (b->type == type) {
417e7e66 159 ovs_list_push_front(&b->andor, &a->node);
e0840f11
BP
160 return b;
161 } else {
162 struct expr *e = expr_create_andor(type);
417e7e66
BW
163 ovs_list_push_back(&e->andor, &a->node);
164 ovs_list_push_back(&e->andor, &b->node);
e0840f11
BP
165 return e;
166 }
167}
168
169static void
170expr_insert_andor(struct expr *andor, struct expr *before, struct expr *new)
171{
172 if (new->type == andor->type) {
173 if (andor->type == EXPR_T_AND) {
174 /* Conjunction junction, what's your function? */
175 }
417e7e66 176 ovs_list_splice(&before->node, new->andor.next, &new->andor);
e0840f11
BP
177 free(new);
178 } else {
417e7e66 179 ovs_list_insert(&before->node, &new->node);
e0840f11
BP
180 }
181}
182
183/* Returns an EXPR_T_BOOLEAN expression with value 'b'. */
184struct expr *
185expr_create_boolean(bool b)
186{
187 struct expr *e = xmalloc(sizeof *e);
188 e->type = EXPR_T_BOOLEAN;
189 e->boolean = b;
190 return e;
191}
192
193static void
194expr_not(struct expr *expr)
195{
196 struct expr *sub;
197
198 switch (expr->type) {
199 case EXPR_T_CMP:
200 expr->cmp.relop = expr_relop_invert(expr->cmp.relop);
201 break;
202
203 case EXPR_T_AND:
204 case EXPR_T_OR:
205 LIST_FOR_EACH (sub, node, &expr->andor) {
206 expr_not(sub);
207 }
208 expr->type = expr->type == EXPR_T_AND ? EXPR_T_OR : EXPR_T_AND;
209 break;
210
211 case EXPR_T_BOOLEAN:
212 expr->boolean = !expr->boolean;
213 break;
214 default:
215 OVS_NOT_REACHED();
216 }
217}
218
219static struct expr *
220expr_fix_andor(struct expr *expr, bool short_circuit)
221{
222 struct expr *sub, *next;
223
224 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
225 if (sub->type == EXPR_T_BOOLEAN) {
226 if (sub->boolean == short_circuit) {
227 expr_destroy(expr);
228 return expr_create_boolean(short_circuit);
229 } else {
417e7e66 230 ovs_list_remove(&sub->node);
e0840f11
BP
231 expr_destroy(sub);
232 }
233 }
234 }
235
417e7e66
BW
236 if (ovs_list_is_short(&expr->andor)) {
237 if (ovs_list_is_empty(&expr->andor)) {
e0840f11
BP
238 free(expr);
239 return expr_create_boolean(!short_circuit);
240 } else {
417e7e66 241 sub = expr_from_node(ovs_list_front(&expr->andor));
e0840f11
BP
242 free(expr);
243 return sub;
244 }
245 } else {
246 return expr;
247 }
248}
249
fd477c6e
BP
250/* Returns 'expr' modified so that top-level oddities are fixed up:
251 *
252 * - Eliminates any EXPR_T_BOOLEAN operands at the top level.
253 *
254 * - Replaces one-operand EXPR_T_AND or EXPR_T_OR by its subexpression. */
e0840f11
BP
255static struct expr *
256expr_fix(struct expr *expr)
257{
258 switch (expr->type) {
259 case EXPR_T_CMP:
260 return expr;
261
262 case EXPR_T_AND:
263 return expr_fix_andor(expr, false);
264
265 case EXPR_T_OR:
266 return expr_fix_andor(expr, true);
267
268 case EXPR_T_BOOLEAN:
269 return expr;
270
271 default:
272 OVS_NOT_REACHED();
273 }
274}
275\f
276/* Formatting. */
277
278static void
279find_bitwise_range(const union mf_subvalue *sv, int width,
280 int *startp, int *n_bitsp)
281{
282 unsigned int start = bitwise_scan(sv, sizeof *sv, true, 0, width);
283 if (start < width) {
284 unsigned int end = bitwise_scan(sv, sizeof *sv, false, start, width);
285 if (end >= width
286 || bitwise_scan(sv, sizeof *sv, true, end, width) >= width) {
287 *startp = start;
288 *n_bitsp = end - start;
289 return;
290 }
291 }
292 *startp = *n_bitsp = 0;
293}
294
e0840f11
BP
295static void
296expr_format_cmp(const struct expr *e, struct ds *s)
297{
298 /* The common case is numerical comparisons.
299 * Handle string comparisons as a special case. */
300 if (!e->cmp.symbol->width) {
301 ds_put_format(s, "%s %s ", e->cmp.symbol->name,
302 expr_relop_to_string(e->cmp.relop));
3b626771 303 json_string_escape(e->cmp.string, s);
e0840f11
BP
304 return;
305 }
306
307 int ofs, n;
308 find_bitwise_range(&e->cmp.mask, e->cmp.symbol->width, &ofs, &n);
309 if (n == 1 && (e->cmp.relop == EXPR_R_EQ || e->cmp.relop == EXPR_R_NE)) {
310 bool positive;
311
312 positive = bitwise_get_bit(&e->cmp.value, sizeof e->cmp.value, ofs);
313 positive ^= e->cmp.relop == EXPR_R_NE;
314 if (!positive) {
315 ds_put_char(s, '!');
316 }
317 ds_put_cstr(s, e->cmp.symbol->name);
318 if (e->cmp.symbol->width > 1) {
319 ds_put_format(s, "[%d]", ofs);
320 }
321 return;
322 }
323
324 ds_put_cstr(s, e->cmp.symbol->name);
325 if (n > 0 && n < e->cmp.symbol->width) {
326 if (n > 1) {
327 ds_put_format(s, "[%d..%d]", ofs, ofs + n - 1);
328 } else {
329 ds_put_format(s, "[%d]", ofs);
330 }
331 }
332
333 ds_put_format(s, " %s ", expr_relop_to_string(e->cmp.relop));
334
335 if (n) {
336 union mf_subvalue value;
337
338 memset(&value, 0, sizeof value);
339 bitwise_copy(&e->cmp.value, sizeof e->cmp.value, ofs,
340 &value, sizeof value, 0,
341 n);
342 mf_format_subvalue(&value, s);
343 } else {
344 mf_format_subvalue(&e->cmp.value, s);
345 ds_put_char(s, '/');
346 mf_format_subvalue(&e->cmp.mask, s);
347 }
348}
349
350static void
351expr_format_andor(const struct expr *e, const char *op, struct ds *s)
352{
353 struct expr *sub;
354 int i = 0;
355
356 LIST_FOR_EACH (sub, node, &e->andor) {
357 if (i++) {
358 ds_put_format(s, " %s ", op);
359 }
360
361 if (sub->type == EXPR_T_AND || sub->type == EXPR_T_OR) {
362 ds_put_char(s, '(');
363 expr_format(sub, s);
364 ds_put_char(s, ')');
365 } else {
366 expr_format(sub, s);
367 }
368 }
369}
370
371/* Appends a string form of 'e' to 's'. The string form is acceptable for
372 * parsing back into an equivalent expression. */
373void
374expr_format(const struct expr *e, struct ds *s)
375{
376 switch (e->type) {
377 case EXPR_T_CMP:
378 expr_format_cmp(e, s);
379 break;
380
381 case EXPR_T_AND:
382 expr_format_andor(e, "&&", s);
383 break;
384
385 case EXPR_T_OR:
386 expr_format_andor(e, "||", s);
387 break;
388
389 case EXPR_T_BOOLEAN:
390 ds_put_char(s, e->boolean ? '1' : '0');
391 break;
392 }
393}
394
395/* Prints a string form of 'e' on stdout, followed by a new-line. */
396void
397expr_print(const struct expr *e)
398{
399 struct ds output;
400
401 ds_init(&output);
402 expr_format(e, &output);
403 puts(ds_cstr(&output));
404 ds_destroy(&output);
405}
406\f
407/* Parsing. */
408
e0840f11
BP
409/* Context maintained during expr_parse(). */
410struct expr_context {
411 struct lexer *lexer; /* Lexer for pulling more tokens. */
412 const struct shash *symtab; /* Symbol table. */
2c5cbb15 413 const struct shash *macros; /* Table of macros. */
e0840f11
BP
414 char *error; /* Error, if any, otherwise NULL. */
415 bool not; /* True inside odd number of NOT operators. */
416};
417
418struct expr *expr_parse__(struct expr_context *);
419static void expr_not(struct expr *);
e0840f11
BP
420static bool parse_field(struct expr_context *, struct expr_field *);
421
422static bool
423expr_error_handle_common(struct expr_context *ctx)
424{
425 if (ctx->error) {
426 /* Already have an error, suppress this one since the cascade seems
427 * unlikely to be useful. */
428 return true;
429 } else if (ctx->lexer->token.type == LEX_T_ERROR) {
430 /* The lexer signaled an error. Nothing at the expression level
431 * accepts an error token, so we'll inevitably end up here with some
432 * meaningless parse error. Report the lexical error instead. */
433 ctx->error = xstrdup(ctx->lexer->token.s);
434 return true;
435 } else {
436 return false;
437 }
438}
439
440static void OVS_PRINTF_FORMAT(2, 3)
441expr_error(struct expr_context *ctx, const char *message, ...)
442{
443 if (expr_error_handle_common(ctx)) {
444 return;
445 }
446
447 va_list args;
448 va_start(args, message);
449 ctx->error = xvasprintf(message, args);
450 va_end(args);
451}
452
453static void OVS_PRINTF_FORMAT(2, 3)
454expr_syntax_error(struct expr_context *ctx, const char *message, ...)
455{
456 if (expr_error_handle_common(ctx)) {
457 return;
458 }
459
460 struct ds s;
461
462 ds_init(&s);
463 ds_put_cstr(&s, "Syntax error ");
464 if (ctx->lexer->token.type == LEX_T_END) {
465 ds_put_cstr(&s, "at end of input ");
466 } else if (ctx->lexer->start) {
467 ds_put_format(&s, "at `%.*s' ",
468 (int) (ctx->lexer->input - ctx->lexer->start),
469 ctx->lexer->start);
470 }
471
472 va_list args;
473 va_start(args, message);
474 ds_put_format_valist(&s, message, args);
475 va_end(args);
476
477 ctx->error = ds_steal_cstr(&s);
478}
479
480static struct expr *
481make_cmp__(const struct expr_field *f, enum expr_relop r,
482 const union expr_constant *c)
483{
484 struct expr *e = xzalloc(sizeof *e);
485 e->type = EXPR_T_CMP;
486 e->cmp.symbol = f->symbol;
487 e->cmp.relop = r;
488 if (f->symbol->width) {
489 bitwise_copy(&c->value, sizeof c->value, 0,
490 &e->cmp.value, sizeof e->cmp.value, f->ofs,
491 f->n_bits);
492 if (c->masked) {
493 bitwise_copy(&c->mask, sizeof c->mask, 0,
494 &e->cmp.mask, sizeof e->cmp.mask, f->ofs,
495 f->n_bits);
496 } else {
497 bitwise_one(&e->cmp.mask, sizeof e->cmp.mask, f->ofs,
498 f->n_bits);
499 }
500 } else {
501 e->cmp.string = xstrdup(c->string);
502 }
503 return e;
504}
505
506/* Returns the minimum reasonable width for integer constant 'c'. */
507static int
508expr_constant_width(const union expr_constant *c)
509{
510 if (c->masked) {
511 return mf_subvalue_width(&c->mask);
512 }
513
514 switch (c->format) {
515 case LEX_F_DECIMAL:
516 case LEX_F_HEXADECIMAL:
517 return mf_subvalue_width(&c->value);
518
519 case LEX_F_IPV4:
520 return 32;
521
522 case LEX_F_IPV6:
523 return 128;
524
525 case LEX_F_ETHERNET:
526 return 48;
527
528 default:
529 OVS_NOT_REACHED();
530 }
531}
532
3b7cb7e1
BP
533static bool
534type_check(struct expr_context *ctx, const struct expr_field *f,
535 struct expr_constant_set *cs)
536{
537 if (cs->type != (f->symbol->width ? EXPR_C_INTEGER : EXPR_C_STRING)) {
538 expr_error(ctx, "%s field %s is not compatible with %s constant.",
539 f->symbol->width ? "Integer" : "String",
540 f->symbol->name,
541 cs->type == EXPR_C_INTEGER ? "integer" : "string");
542 return false;
543 }
544
545 if (f->symbol->width) {
546 for (size_t i = 0; i < cs->n_values; i++) {
547 int w = expr_constant_width(&cs->values[i]);
548 if (w > f->symbol->width) {
549 expr_error(ctx, "%d-bit constant is not compatible with "
550 "%d-bit field %s.",
551 w, f->symbol->width, f->symbol->name);
552 return false;
553 }
554 }
555 }
556
557 return true;
558}
559
e0840f11
BP
560static struct expr *
561make_cmp(struct expr_context *ctx,
562 const struct expr_field *f, enum expr_relop r,
563 struct expr_constant_set *cs)
564{
565 struct expr *e = NULL;
566
3b7cb7e1 567 if (!type_check(ctx, f, cs)) {
e0840f11
BP
568 goto exit;
569 }
570
571 if (r != EXPR_R_EQ && r != EXPR_R_NE) {
572 if (cs->in_curlies) {
573 expr_error(ctx, "Only == and != operators may be used "
574 "with value sets.");
575 goto exit;
576 }
577 if (f->symbol->level == EXPR_L_NOMINAL ||
578 f->symbol->level == EXPR_L_BOOLEAN) {
579 expr_error(ctx, "Only == and != operators may be used "
580 "with %s field %s.",
581 expr_level_to_string(f->symbol->level),
582 f->symbol->name);
583 goto exit;
584 }
585 if (cs->values[0].masked) {
586 expr_error(ctx, "Only == and != operators may be used with "
587 "masked constants. Consider using subfields instead "
588 "(e.g. eth.src[0..15] > 0x1111 in place of "
589 "eth.src > 00:00:00:00:11:11/00:00:00:00:ff:ff).");
590 goto exit;
591 }
592 }
593
594 if (f->symbol->level == EXPR_L_NOMINAL) {
595 if (f->symbol->expansion) {
fd477c6e 596 ovs_assert(f->symbol->width > 0);
e0840f11
BP
597 for (size_t i = 0; i < cs->n_values; i++) {
598 const union mf_subvalue *value = &cs->values[i].value;
599 bool positive = (value->integer & htonll(1)) != 0;
600 positive ^= r == EXPR_R_NE;
601 positive ^= ctx->not;
602 if (!positive) {
603 const char *name = f->symbol->name;
604 expr_error(ctx, "Nominal predicate %s may only be tested "
605 "positively, e.g. `%s' or `%s == 1' but not "
606 "`!%s' or `%s == 0'.",
607 name, name, name, name, name);
608 goto exit;
609 }
610 }
611 } else if (r != (ctx->not ? EXPR_R_NE : EXPR_R_EQ)) {
612 expr_error(ctx, "Nominal field %s may only be tested for "
613 "equality (taking enclosing `!' operators into "
614 "account).", f->symbol->name);
615 goto exit;
616 }
617 }
618
e0840f11
BP
619 e = make_cmp__(f, r, &cs->values[0]);
620 for (size_t i = 1; i < cs->n_values; i++) {
621 e = expr_combine(r == EXPR_R_EQ ? EXPR_T_OR : EXPR_T_AND,
622 e, make_cmp__(f, r, &cs->values[i]));
623 }
624exit:
625 expr_constant_set_destroy(cs);
626 return e;
627}
628
629static bool
630expr_get_int(struct expr_context *ctx, int *value)
631{
558ec83d
BP
632 bool ok = lexer_get_int(ctx->lexer, value);
633 if (!ok) {
e0840f11 634 expr_syntax_error(ctx, "expecting small integer.");
e0840f11 635 }
558ec83d 636 return ok;
e0840f11
BP
637}
638
639static bool
640parse_field(struct expr_context *ctx, struct expr_field *f)
641{
642 const struct expr_symbol *symbol;
643
644 if (ctx->lexer->token.type != LEX_T_ID) {
645 expr_syntax_error(ctx, "expecting field name.");
646 return false;
647 }
648
649 symbol = shash_find_data(ctx->symtab, ctx->lexer->token.s);
650 if (!symbol) {
651 expr_syntax_error(ctx, "expecting field name.");
652 return false;
653 }
654 lexer_get(ctx->lexer);
655
656 f->symbol = symbol;
657 if (lexer_match(ctx->lexer, LEX_T_LSQUARE)) {
658 int low, high;
659
660 if (!symbol->width) {
661 expr_error(ctx, "Cannot select subfield of string field %s.",
662 symbol->name);
663 return false;
664 }
665
666 if (!expr_get_int(ctx, &low)) {
667 return false;
668 }
669 if (lexer_match(ctx->lexer, LEX_T_ELLIPSIS)) {
670 if (!expr_get_int(ctx, &high)) {
671 return false;
672 }
673 } else {
674 high = low;
675 }
676
677 if (!lexer_match(ctx->lexer, LEX_T_RSQUARE)) {
678 expr_syntax_error(ctx, "expecting `]'.");
679 return false;
680 }
681
682 if (low > high) {
683 expr_error(ctx, "Invalid bit range %d to %d.", low, high);
684 return false;
685 } else if (high >= symbol->width) {
686 expr_error(ctx, "Cannot select bits %d to %d of %d-bit field %s.",
687 low, high, symbol->width, symbol->name);
688 return false;
689 } else if (symbol->level == EXPR_L_NOMINAL
690 && (low != 0 || high != symbol->width - 1)) {
691 expr_error(ctx, "Cannot select subfield of nominal field %s.",
692 symbol->name);
693 return false;
694 }
695
696 f->ofs = low;
697 f->n_bits = high - low + 1;
698 } else {
699 f->ofs = 0;
700 f->n_bits = symbol->width;
701 }
702
703 return true;
704}
705
706static bool
707parse_relop(struct expr_context *ctx, enum expr_relop *relop)
708{
709 if (expr_relop_from_token(ctx->lexer->token.type, relop)) {
710 lexer_get(ctx->lexer);
711 return true;
712 } else {
713 expr_syntax_error(ctx, "expecting relational operator.");
714 return false;
715 }
716}
717
718static bool
719assign_constant_set_type(struct expr_context *ctx,
720 struct expr_constant_set *cs,
721 enum expr_constant_type type)
722{
723 if (!cs->n_values || cs->type == type) {
724 cs->type = type;
725 return true;
726 } else {
727 expr_syntax_error(ctx, "expecting %s.",
728 cs->type == EXPR_C_INTEGER ? "integer" : "string");
729 return false;
730 }
731}
732
2c5cbb15
RB
733static bool
734parse_macros(struct expr_context *ctx, struct expr_constant_set *cs,
735 size_t *allocated_values)
736{
737 struct expr_constant_set *addr_set
738 = shash_find_data(ctx->macros, ctx->lexer->token.s);
739 if (!addr_set) {
740 expr_syntax_error(ctx, "expecting address set name.");
741 return false;
742 }
743
744 if (!assign_constant_set_type(ctx, cs, EXPR_C_INTEGER)) {
745 return false;
746 }
747
748 size_t n_values = cs->n_values + addr_set->n_values;
749 if (n_values >= *allocated_values) {
750 cs->values = xrealloc(cs->values, n_values * sizeof *cs->values);
751 *allocated_values = n_values;
752 }
753 for (size_t i = 0; i < addr_set->n_values; i++) {
754 cs->values[cs->n_values++] = addr_set->values[i];
755 }
756
757 return true;
758}
759
e0840f11
BP
760static bool
761parse_constant(struct expr_context *ctx, struct expr_constant_set *cs,
762 size_t *allocated_values)
763{
764 if (cs->n_values >= *allocated_values) {
765 cs->values = x2nrealloc(cs->values, allocated_values,
766 sizeof *cs->values);
767 }
768
769 if (ctx->lexer->token.type == LEX_T_STRING) {
770 if (!assign_constant_set_type(ctx, cs, EXPR_C_STRING)) {
771 return false;
772 }
773 cs->values[cs->n_values++].string = xstrdup(ctx->lexer->token.s);
774 lexer_get(ctx->lexer);
775 return true;
776 } else if (ctx->lexer->token.type == LEX_T_INTEGER ||
777 ctx->lexer->token.type == LEX_T_MASKED_INTEGER) {
778 if (!assign_constant_set_type(ctx, cs, EXPR_C_INTEGER)) {
779 return false;
780 }
781
782 union expr_constant *c = &cs->values[cs->n_values++];
783 c->value = ctx->lexer->token.value;
784 c->format = ctx->lexer->token.format;
785 c->masked = ctx->lexer->token.type == LEX_T_MASKED_INTEGER;
786 if (c->masked) {
787 c->mask = ctx->lexer->token.mask;
788 }
789 lexer_get(ctx->lexer);
790 return true;
2c5cbb15
RB
791 } else if (ctx->lexer->token.type == LEX_T_MACRO) {
792 if (!parse_macros(ctx, cs, allocated_values)) {
793 return false;
794 }
795 lexer_get(ctx->lexer);
796 return true;
e0840f11
BP
797 } else {
798 expr_syntax_error(ctx, "expecting constant.");
799 return false;
800 }
801}
802
803/* Parses a single or {}-enclosed set of integer or string constants into 'cs',
804 * which the caller need not have initialized. Returns true on success, in
805 * which case the caller owns 'cs', false on failure, in which case 'cs' is
806 * indeterminate. */
807static bool
808parse_constant_set(struct expr_context *ctx, struct expr_constant_set *cs)
809{
810 size_t allocated_values = 0;
811 bool ok;
812
813 memset(cs, 0, sizeof *cs);
814 if (lexer_match(ctx->lexer, LEX_T_LCURLY)) {
815 ok = true;
816 cs->in_curlies = true;
817 do {
818 if (!parse_constant(ctx, cs, &allocated_values)) {
819 ok = false;
820 break;
821 }
822 lexer_match(ctx->lexer, LEX_T_COMMA);
823 } while (!lexer_match(ctx->lexer, LEX_T_RCURLY));
824 } else {
825 ok = parse_constant(ctx, cs, &allocated_values);
826 }
827 if (!ok) {
828 expr_constant_set_destroy(cs);
829 }
830 return ok;
831}
832
42814145 833void
e0840f11
BP
834expr_constant_set_destroy(struct expr_constant_set *cs)
835{
836 if (cs) {
837 if (cs->type == EXPR_C_STRING) {
838 for (size_t i = 0; i < cs->n_values; i++) {
839 free(cs->values[i].string);
840 }
841 }
842 free(cs->values);
843 }
844}
845
2c5cbb15
RB
846/* Adds a macro named 'name' to 'macros', replacing any existing macro with the
847 * given name. */
848void
849expr_macros_add(struct shash *macros, const char *name,
850 const char *const *values, size_t n_values)
851{
852 /* Replace any existing entry for this name. */
853 expr_macros_remove(macros, name);
854
855 struct expr_constant_set *cs = xzalloc(sizeof *cs);
856 cs->type = EXPR_C_INTEGER;
857 cs->in_curlies = true;
858 cs->n_values = 0;
859 cs->values = xmalloc(n_values * sizeof *cs->values);
860 for (size_t i = 0; i < n_values; i++) {
861 /* Use the lexer to convert each macro into the proper
862 * integer format. */
863 struct lexer lex;
864 lexer_init(&lex, values[i]);
865 lexer_get(&lex);
866 if (lex.token.type != LEX_T_INTEGER
867 && lex.token.type != LEX_T_MASKED_INTEGER) {
868 VLOG_WARN("Invalid address set entry: '%s', token type: %d",
869 values[i], lex.token.type);
870 } else {
871 union expr_constant *c = &cs->values[cs->n_values++];
872 c->value = lex.token.value;
873 c->format = lex.token.format;
874 c->masked = lex.token.type == LEX_T_MASKED_INTEGER;
875 if (c->masked) {
876 c->mask = lex.token.mask;
877 }
878 }
879 lexer_destroy(&lex);
880 }
881
882 shash_add(macros, name, cs);
883}
884
885void
886expr_macros_remove(struct shash *macros, const char *name)
887{
888 struct expr_constant_set *cs = shash_find_and_delete(macros, name);
889 if (cs) {
890 expr_constant_set_destroy(cs);
891 free(cs);
892 }
893}
894
895/* Destroy all contents of 'macros'. */
896void
897expr_macros_destroy(struct shash *macros)
898{
899 struct shash_node *node, *next;
900
901 SHASH_FOR_EACH_SAFE (node, next, macros) {
902 struct expr_constant_set *cs = node->data;
903
904 shash_delete(macros, node);
905 expr_constant_set_destroy(cs);
906 }
907}
908
e0840f11
BP
909static struct expr *
910expr_parse_primary(struct expr_context *ctx, bool *atomic)
911{
912 *atomic = false;
913 if (lexer_match(ctx->lexer, LEX_T_LPAREN)) {
914 struct expr *e = expr_parse__(ctx);
915 if (!lexer_match(ctx->lexer, LEX_T_RPAREN)) {
916 expr_destroy(e);
917 expr_syntax_error(ctx, "expecting `)'.");
918 return NULL;
919 }
920 *atomic = true;
921 return e;
922 }
923
924 if (ctx->lexer->token.type == LEX_T_ID) {
925 struct expr_field f;
926 enum expr_relop r;
927 struct expr_constant_set c;
928
929 if (!parse_field(ctx, &f)) {
930 return NULL;
931 }
932
933 if (!expr_relop_from_token(ctx->lexer->token.type, &r)) {
76da94b5
BP
934 if (!f.n_bits || ctx->lexer->token.type == LEX_T_EQUALS) {
935 expr_syntax_error(ctx, "expecting relational operator.");
936 return NULL;
937 } else if (f.n_bits > 1 && !ctx->not) {
e0840f11 938 expr_error(ctx, "Explicit `!= 0' is required for inequality "
76da94b5 939 "test of multibit field against 0.");
e0840f11
BP
940 return NULL;
941 }
942
943 *atomic = true;
944
945 union expr_constant *cst = xzalloc(sizeof *cst);
946 cst->format = LEX_F_HEXADECIMAL;
947 cst->masked = false;
948
949 c.type = EXPR_C_INTEGER;
950 c.values = cst;
951 c.n_values = 1;
952 c.in_curlies = false;
953 return make_cmp(ctx, &f, EXPR_R_NE, &c);
954 } else if (parse_relop(ctx, &r) && parse_constant_set(ctx, &c)) {
955 return make_cmp(ctx, &f, r, &c);
956 } else {
957 return NULL;
958 }
959 } else {
960 struct expr_constant_set c1;
961 if (!parse_constant_set(ctx, &c1)) {
962 return NULL;
963 }
964
965 if (!expr_relop_from_token(ctx->lexer->token.type, NULL)
966 && c1.n_values == 1
967 && c1.type == EXPR_C_INTEGER
968 && c1.values[0].format == LEX_F_DECIMAL
969 && !c1.values[0].masked
970 && !c1.in_curlies) {
971 uint64_t x = ntohll(c1.values[0].value.integer);
972 if (x <= 1) {
973 *atomic = true;
974 expr_constant_set_destroy(&c1);
975 return expr_create_boolean(x);
976 }
977 }
978
979 enum expr_relop r1;
980 struct expr_field f;
981 if (!parse_relop(ctx, &r1) || !parse_field(ctx, &f)) {
982 expr_constant_set_destroy(&c1);
983 return NULL;
984 }
985
986 if (!expr_relop_from_token(ctx->lexer->token.type, NULL)) {
987 return make_cmp(ctx, &f, expr_relop_turn(r1), &c1);
988 }
989
990 enum expr_relop r2;
991 struct expr_constant_set c2;
992 if (!parse_relop(ctx, &r2) || !parse_constant_set(ctx, &c2)) {
993 expr_constant_set_destroy(&c1);
994 return NULL;
995 } else {
996 /* Reject "1 == field == 2", "1 < field > 2", and so on. */
997 if (!(((r1 == EXPR_R_LT || r1 == EXPR_R_LE) &&
998 (r2 == EXPR_R_LT || r2 == EXPR_R_LE)) ||
999 ((r1 == EXPR_R_GT || r1 == EXPR_R_GE) &&
1000 (r2 == EXPR_R_GT || r2 == EXPR_R_GE)))) {
1001 expr_error(ctx, "Range expressions must have the form "
1002 "`x < field < y' or `x > field > y', with each "
1003 "`<' optionally replaced by `<=' or `>' by `>=').");
1004 expr_constant_set_destroy(&c1);
1005 expr_constant_set_destroy(&c2);
1006 return NULL;
1007 }
1008
1009 struct expr *e1 = make_cmp(ctx, &f, expr_relop_turn(r1), &c1);
1010 struct expr *e2 = make_cmp(ctx, &f, r2, &c2);
1011 if (ctx->error) {
1012 expr_destroy(e1);
1013 expr_destroy(e2);
1014 return NULL;
1015 }
1016 return expr_combine(EXPR_T_AND, e1, e2);
1017 }
1018 }
1019}
1020
1021static struct expr *
1022expr_parse_not(struct expr_context *ctx)
1023{
1024 bool atomic;
1025
1026 if (lexer_match(ctx->lexer, LEX_T_LOG_NOT)) {
1027 ctx->not = !ctx->not;
1028 struct expr *expr = expr_parse_primary(ctx, &atomic);
1029 ctx->not = !ctx->not;
1030
1031 if (expr) {
1032 if (!atomic) {
1033 expr_error(ctx, "Missing parentheses around operand of !.");
1034 expr_destroy(expr);
1035 return NULL;
1036 }
1037 expr_not(expr);
1038 }
1039 return expr;
1040 } else {
1041 return expr_parse_primary(ctx, &atomic);
1042 }
1043}
1044
1045struct expr *
1046expr_parse__(struct expr_context *ctx)
1047{
1048 struct expr *e = expr_parse_not(ctx);
1049 if (!e) {
1050 return NULL;
1051 }
1052
1053 enum lex_type lex_type = ctx->lexer->token.type;
1054 if (lex_type == LEX_T_LOG_AND || lex_type == LEX_T_LOG_OR) {
1055 enum expr_type expr_type
1056 = lex_type == LEX_T_LOG_AND ? EXPR_T_AND : EXPR_T_OR;
1057
1058 lexer_get(ctx->lexer);
1059 do {
1060 struct expr *e2 = expr_parse_not(ctx);
1061 if (!e2) {
1062 expr_destroy(e);
1063 return NULL;
1064 }
1065 e = expr_combine(expr_type, e, e2);
1066 } while (lexer_match(ctx->lexer, lex_type));
1067 if (ctx->lexer->token.type == LEX_T_LOG_AND
1068 || ctx->lexer->token.type == LEX_T_LOG_OR) {
1069 expr_destroy(e);
1070 expr_error(ctx,
1071 "&& and || must be parenthesized when used together.");
1072 return NULL;
1073 }
1074 }
1075 return e;
1076}
1077
1078/* Parses an expression using the symbols in 'symtab' from 'lexer'. If
1079 * successful, returns the new expression and sets '*errorp' to NULL. On
1080 * failure, returns NULL and sets '*errorp' to an explanatory error message.
1081 * The caller must eventually free the returned expression (with
1082 * expr_destroy()) or error (with free()). */
1083struct expr *
2c5cbb15
RB
1084expr_parse(struct lexer *lexer, const struct shash *symtab,
1085 const struct shash *macros, char **errorp)
e0840f11 1086{
2c5cbb15
RB
1087 struct expr_context ctx = { .lexer = lexer,
1088 .symtab = symtab,
1089 .macros = macros };
e0840f11
BP
1090 struct expr *e = expr_parse__(&ctx);
1091 *errorp = ctx.error;
1092 ovs_assert((ctx.error != NULL) != (e != NULL));
1093 return e;
1094}
1095
1096/* Like expr_parse(), but the expression is taken from 's'. */
1097struct expr *
2c5cbb15
RB
1098expr_parse_string(const char *s, const struct shash *symtab,
1099 const struct shash *macros, char **errorp)
e0840f11
BP
1100{
1101 struct lexer lexer;
1102 struct expr *expr;
1103
1104 lexer_init(&lexer, s);
1105 lexer_get(&lexer);
2c5cbb15 1106 expr = expr_parse(&lexer, symtab, macros, errorp);
8b34ccda 1107 if (!*errorp && lexer.token.type != LEX_T_END) {
e0840f11
BP
1108 *errorp = xstrdup("Extra tokens at end of input.");
1109 expr_destroy(expr);
1110 expr = NULL;
1111 }
1112 lexer_destroy(&lexer);
1113
1114 return expr;
1115}
1116\f
1117static struct expr_symbol *
1118add_symbol(struct shash *symtab, const char *name, int width,
1119 const char *prereqs, enum expr_level level,
1120 bool must_crossproduct)
1121{
1122 struct expr_symbol *symbol = xzalloc(sizeof *symbol);
1123 symbol->name = xstrdup(name);
1124 symbol->prereqs = prereqs && prereqs[0] ? xstrdup(prereqs) : NULL;
1125 symbol->width = width;
1126 symbol->level = level;
1127 symbol->must_crossproduct = must_crossproduct;
1128 shash_add_assert(symtab, symbol->name, symbol);
1129 return symbol;
1130}
1131
1132/* Adds field 'id' to symbol table 'symtab' under the given 'name'. Whenever
1133 * 'name' is referenced, expression annotation (see expr_annotate()) will
1134 * ensure that 'prereqs' are also true. If 'must_crossproduct' is true, then
1135 * conversion to flows will never attempt to use the field as a conjunctive
1136 * match dimension (see "Crossproducting" in the large comment on struct
1137 * expr_symbol in expr.h for an example).
1138 *
1139 * A given field 'id' must only be used for a single symbol in a symbol table.
1140 * Use subfields to duplicate or subset a field (you can even make a subfield
1141 * include all the bits of the "parent" field if you like). */
1142struct expr_symbol *
1143expr_symtab_add_field(struct shash *symtab, const char *name,
1144 enum mf_field_id id, const char *prereqs,
1145 bool must_crossproduct)
1146{
1147 const struct mf_field *field = mf_from_id(id);
1148 struct expr_symbol *symbol;
1149
1150 symbol = add_symbol(symtab, name, field->n_bits, prereqs,
1151 (field->maskable == MFM_FULLY
1152 ? EXPR_L_ORDINAL
1153 : EXPR_L_NOMINAL),
1154 must_crossproduct);
1155 symbol->field = field;
1156 return symbol;
1157}
1158
1159static bool
1160parse_field_from_string(const char *s, const struct shash *symtab,
1161 struct expr_field *field, char **errorp)
1162{
1163 struct lexer lexer;
1164 lexer_init(&lexer, s);
1165 lexer_get(&lexer);
1166
1998e474 1167 struct expr_context ctx = { .lexer = &lexer, .symtab = symtab };
e0840f11
BP
1168 bool ok = parse_field(&ctx, field);
1169 if (!ok) {
1170 *errorp = ctx.error;
1171 } else if (lexer.token.type != LEX_T_END) {
1172 *errorp = xstrdup("Extra tokens at end of input.");
1173 ok = false;
1174 }
1175
1176 lexer_destroy(&lexer);
1177
1178 return ok;
1179}
1180
1181/* Adds 'name' as a subfield of a larger field in 'symtab'. Whenever
1182 * 'name' is referenced, expression annotation (see expr_annotate()) will
1183 * ensure that 'prereqs' are also true.
1184 *
1185 * 'subfield' must describe the subfield as a string, e.g. "vlan.tci[0..11]"
1186 * for the low 12 bits of a larger field named "vlan.tci". */
1187struct expr_symbol *
1188expr_symtab_add_subfield(struct shash *symtab, const char *name,
1189 const char *prereqs, const char *subfield)
1190{
1191 struct expr_symbol *symbol;
1192 struct expr_field f;
1193 char *error;
1194
1195 if (!parse_field_from_string(subfield, symtab, &f, &error)) {
1196 VLOG_WARN("%s: error parsing %s subfield (%s)", subfield, name, error);
1197 free(error);
1198 return NULL;
1199 }
1200
1201 enum expr_level level = f.symbol->level;
1202 if (level != EXPR_L_ORDINAL) {
1203 VLOG_WARN("can't define %s as subfield of %s field %s",
1204 name, expr_level_to_string(level), f.symbol->name);
1205 }
1206
1207 symbol = add_symbol(symtab, name, f.n_bits, prereqs, level, false);
1208 symbol->expansion = xstrdup(subfield);
1209 return symbol;
1210}
1211
1212/* Adds a string-valued symbol named 'name' to 'symtab' with the specified
1213 * 'prereqs'. */
1214struct expr_symbol *
1215expr_symtab_add_string(struct shash *symtab, const char *name,
f386a8a7 1216 enum mf_field_id id, const char *prereqs)
e0840f11 1217{
f386a8a7
BP
1218 const struct mf_field *field = mf_from_id(id);
1219 struct expr_symbol *symbol;
1220
1221 symbol = add_symbol(symtab, name, 0, prereqs, EXPR_L_NOMINAL, false);
1222 symbol->field = field;
1223 return symbol;
e0840f11
BP
1224}
1225
1226static enum expr_level
1227expr_get_level(const struct expr *expr)
1228{
1229 const struct expr *sub;
1230 enum expr_level level = EXPR_L_ORDINAL;
1231
1232 switch (expr->type) {
1233 case EXPR_T_CMP:
1234 return (expr->cmp.symbol->level == EXPR_L_NOMINAL
1235 ? EXPR_L_NOMINAL
1236 : EXPR_L_BOOLEAN);
1237
1238 case EXPR_T_AND:
1239 case EXPR_T_OR:
1240 LIST_FOR_EACH (sub, node, &expr->andor) {
1241 enum expr_level sub_level = expr_get_level(sub);
1242 level = MIN(level, sub_level);
1243 }
1244 return level;
1245
1246 case EXPR_T_BOOLEAN:
1247 return EXPR_L_BOOLEAN;
1248
1249 default:
1250 OVS_NOT_REACHED();
1251 }
1252}
1253
1254static enum expr_level
1255expr_parse_level(const char *s, const struct shash *symtab, char **errorp)
1256{
2c5cbb15 1257 struct expr *expr = expr_parse_string(s, symtab, NULL, errorp);
e0840f11
BP
1258 enum expr_level level = expr ? expr_get_level(expr) : EXPR_L_NOMINAL;
1259 expr_destroy(expr);
1260 return level;
1261}
1262
1263/* Adds a predicate symbol, whose value is the given Boolean 'expression',
44283953 1264 * named 'name' to 'symtab'. For example, "ip4 && ip4.proto == 6" might be an
e0840f11
BP
1265 * appropriate predicate named "tcp4". */
1266struct expr_symbol *
1267expr_symtab_add_predicate(struct shash *symtab, const char *name,
1268 const char *expansion)
1269{
1270 struct expr_symbol *symbol;
1271 enum expr_level level;
1272 char *error;
1273
1274 level = expr_parse_level(expansion, symtab, &error);
1275 if (error) {
1276 VLOG_WARN("%s: error parsing %s expansion (%s)",
1277 expansion, name, error);
1278 free(error);
1279 return NULL;
1280 }
1281
1282 symbol = add_symbol(symtab, name, 1, NULL, level, false);
1283 symbol->expansion = xstrdup(expansion);
1284 return symbol;
1285}
1286
1287/* Destroys 'symtab' and all of its symbols. */
1288void
1289expr_symtab_destroy(struct shash *symtab)
1290{
1291 struct shash_node *node, *next;
1292
1293 SHASH_FOR_EACH_SAFE (node, next, symtab) {
1294 struct expr_symbol *symbol = node->data;
1295
1296 shash_delete(symtab, node);
1297 free(symbol->name);
1298 free(symbol->prereqs);
1299 free(symbol->expansion);
1300 free(symbol);
1301 }
1302}
1303\f
1304/* Cloning. */
1305
1306static struct expr *
1307expr_clone_cmp(struct expr *expr)
1308{
1309 struct expr *new = xmemdup(expr, sizeof *expr);
1310 if (!new->cmp.symbol->width) {
1311 new->cmp.string = xstrdup(new->cmp.string);
1312 }
1313 return new;
1314}
1315
1316static struct expr *
1317expr_clone_andor(struct expr *expr)
1318{
1319 struct expr *new = expr_create_andor(expr->type);
1320 struct expr *sub;
1321
1322 LIST_FOR_EACH (sub, node, &expr->andor) {
1323 struct expr *new_sub = expr_clone(sub);
417e7e66 1324 ovs_list_push_back(&new->andor, &new_sub->node);
e0840f11
BP
1325 }
1326 return new;
1327}
1328
1329/* Returns a clone of 'expr'. This is a "deep copy": neither the returned
1330 * expression nor any of its substructure will be shared with 'expr'. */
1331struct expr *
1332expr_clone(struct expr *expr)
1333{
1334 switch (expr->type) {
1335 case EXPR_T_CMP:
1336 return expr_clone_cmp(expr);
1337
1338 case EXPR_T_AND:
1339 case EXPR_T_OR:
1340 return expr_clone_andor(expr);
1341
1342 case EXPR_T_BOOLEAN:
1343 return expr_create_boolean(expr->boolean);
1344 }
1345 OVS_NOT_REACHED();
1346}
1347\f
1348/* Destroys 'expr' and all of the sub-expressions it references. */
1349void
1350expr_destroy(struct expr *expr)
1351{
1352 if (!expr) {
1353 return;
1354 }
1355
1356 struct expr *sub, *next;
1357
1358 switch (expr->type) {
1359 case EXPR_T_CMP:
1360 if (!expr->cmp.symbol->width) {
1361 free(expr->cmp.string);
1362 }
1363 break;
1364
1365 case EXPR_T_AND:
1366 case EXPR_T_OR:
1367 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
417e7e66 1368 ovs_list_remove(&sub->node);
e0840f11
BP
1369 expr_destroy(sub);
1370 }
1371 break;
1372
1373 case EXPR_T_BOOLEAN:
1374 break;
1375 }
1376 free(expr);
1377}
1378\f
1379/* Annotation. */
1380
1381/* An element in a linked list of symbols.
1382 *
1383 * Used to detect when a symbol is being expanded recursively, to allow
1384 * flagging an error. */
1385struct annotation_nesting {
1386 struct ovs_list node;
1387 const struct expr_symbol *symbol;
1388};
1389
1390struct expr *expr_annotate__(struct expr *, const struct shash *symtab,
1391 struct ovs_list *nesting, char **errorp);
1392
1393static struct expr *
1394parse_and_annotate(const char *s, const struct shash *symtab,
1395 struct ovs_list *nesting, char **errorp)
1396{
1397 char *error;
1398 struct expr *expr;
1399
2c5cbb15 1400 expr = expr_parse_string(s, symtab, NULL, &error);
e0840f11
BP
1401 if (expr) {
1402 expr = expr_annotate__(expr, symtab, nesting, &error);
1403 }
3b7cb7e1
BP
1404 if (expr) {
1405 *errorp = NULL;
1406 } else {
e0840f11
BP
1407 *errorp = xasprintf("Error parsing expression `%s' encountered as "
1408 "prerequisite or predicate of initial expression: "
1409 "%s", s, error);
1410 free(error);
1411 }
1412 return expr;
1413}
1414
1415static struct expr *
1416expr_annotate_cmp(struct expr *expr, const struct shash *symtab,
1417 struct ovs_list *nesting, char **errorp)
1418{
1419 const struct expr_symbol *symbol = expr->cmp.symbol;
1420 const struct annotation_nesting *iter;
1421 LIST_FOR_EACH (iter, node, nesting) {
1422 if (iter->symbol == symbol) {
1423 *errorp = xasprintf("Recursive expansion of symbol `%s'.",
1424 symbol->name);
1425 expr_destroy(expr);
1426 return NULL;
1427 }
1428 }
1429
1430 struct annotation_nesting an;
1431 an.symbol = symbol;
417e7e66 1432 ovs_list_push_back(nesting, &an.node);
e0840f11
BP
1433
1434 struct expr *prereqs = NULL;
1435 if (symbol->prereqs) {
1436 prereqs = parse_and_annotate(symbol->prereqs, symtab, nesting, errorp);
1437 if (!prereqs) {
1438 goto error;
1439 }
1440 }
1441
1442 if (symbol->expansion) {
1443 if (symbol->level == EXPR_L_ORDINAL) {
1444 struct expr_field field;
1445
1446 if (!parse_field_from_string(symbol->expansion, symtab,
1447 &field, errorp)) {
1448 goto error;
1449 }
1450
1451 expr->cmp.symbol = field.symbol;
1452 mf_subvalue_shift(&expr->cmp.value, field.ofs);
1453 mf_subvalue_shift(&expr->cmp.mask, field.ofs);
1454 } else {
1455 struct expr *expansion;
1456
1457 expansion = parse_and_annotate(symbol->expansion, symtab,
1458 nesting, errorp);
1459 if (!expansion) {
1460 goto error;
1461 }
1462
1463 bool positive = (expr->cmp.value.integer & htonll(1)) != 0;
1464 positive ^= expr->cmp.relop == EXPR_R_NE;
1465 if (!positive) {
1466 expr_not(expansion);
1467 }
1468
1469 expr_destroy(expr);
1470 expr = expansion;
1471 }
1472 }
1473
417e7e66 1474 ovs_list_remove(&an.node);
e0840f11
BP
1475 return prereqs ? expr_combine(EXPR_T_AND, expr, prereqs) : expr;
1476
1477error:
1478 expr_destroy(expr);
1479 expr_destroy(prereqs);
417e7e66 1480 ovs_list_remove(&an.node);
e0840f11
BP
1481 return NULL;
1482}
1483
1484struct expr *
1485expr_annotate__(struct expr *expr, const struct shash *symtab,
1486 struct ovs_list *nesting, char **errorp)
1487{
1488 switch (expr->type) {
1489 case EXPR_T_CMP:
1490 return expr_annotate_cmp(expr, symtab, nesting, errorp);
1491
1492 case EXPR_T_AND:
1493 case EXPR_T_OR: {
1494 struct expr *sub, *next;
1495
1496 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
417e7e66 1497 ovs_list_remove(&sub->node);
e0840f11
BP
1498 struct expr *new_sub = expr_annotate__(sub, symtab,
1499 nesting, errorp);
1500 if (!new_sub) {
1501 expr_destroy(expr);
1502 return NULL;
1503 }
1504 expr_insert_andor(expr, next, new_sub);
1505 }
1506 *errorp = NULL;
1507 return expr;
1508 }
1509
1510 case EXPR_T_BOOLEAN:
1511 *errorp = NULL;
1512 return expr;
1513
1514 default:
1515 OVS_NOT_REACHED();
1516 }
1517}
1518
1519/* "Annotates" 'expr', which does the following:
1520 *
1521 * - Applies prerequisites, by locating each comparison operator whose
1522 * field has a prerequisite and adding a logical AND against those
1523 * prerequisites.
1524 *
1525 * - Expands references to subfield symbols, by replacing them by
1526 * references to their underlying field symbols (suitably shifted).
1527 *
1528 * - Expands references to predicate symbols, by replacing them by the
1529 * expressions that they expand to.
1530 *
1385d7b7
JP
1531 * In each case, annotation occurs recursively as necessary.
1532 *
1533 * On failure, returns NULL and sets '*errorp' to an explanatory error
1534 * message, which the caller must free. */
e0840f11
BP
1535struct expr *
1536expr_annotate(struct expr *expr, const struct shash *symtab, char **errorp)
1537{
1538 struct ovs_list nesting = OVS_LIST_INITIALIZER(&nesting);
1539 return expr_annotate__(expr, symtab, &nesting, errorp);
1540}
1541\f
1542static struct expr *
1543expr_simplify_ne(struct expr *expr)
1544{
1545 struct expr *new = NULL;
1546 const union mf_subvalue *value = &expr->cmp.value;
1547 const union mf_subvalue *mask = &expr->cmp.mask;
1548 int w = expr->cmp.symbol->width;
1549 int i;
1550
1551 for (i = 0; (i = bitwise_scan(mask, sizeof *mask, true, i, w)) < w; i++) {
1552 struct expr *e;
1553
1554 e = xzalloc(sizeof *e);
1555 e->type = EXPR_T_CMP;
1556 e->cmp.symbol = expr->cmp.symbol;
1557 e->cmp.relop = EXPR_R_EQ;
1558 bitwise_put_bit(&e->cmp.value, sizeof e->cmp.value, i,
1559 !bitwise_get_bit(value, sizeof *value, i));
1560 bitwise_put1(&e->cmp.mask, sizeof e->cmp.mask, i);
1561
1562 new = expr_combine(EXPR_T_OR, new, e);
1563 }
1564 ovs_assert(new);
1565
1566 expr_destroy(expr);
1567
1568 return new;
1569}
1570
1571static struct expr *
1572expr_simplify_relational(struct expr *expr)
1573{
1574 const union mf_subvalue *value = &expr->cmp.value;
1575 int start, n_bits, end;
1576
1577 find_bitwise_range(&expr->cmp.mask, expr->cmp.symbol->width,
1578 &start, &n_bits);
1579 ovs_assert(n_bits > 0);
1580 end = start + n_bits;
1581
1582 struct expr *new;
1583 if (expr->cmp.relop == EXPR_R_LE || expr->cmp.relop == EXPR_R_GE) {
1584 new = xmemdup(expr, sizeof *expr);
1585 new->cmp.relop = EXPR_R_EQ;
1586 } else {
1587 new = NULL;
1588 }
1589
1590 bool b = expr->cmp.relop == EXPR_R_LT || expr->cmp.relop == EXPR_R_LE;
1591 for (int z = bitwise_scan(value, sizeof *value, b, start, end);
1592 z < end;
1593 z = bitwise_scan(value, sizeof *value, b, z + 1, end)) {
1594 struct expr *e;
1595
1596 e = xmemdup(expr, sizeof *expr);
1597 e->cmp.relop = EXPR_R_EQ;
1598 bitwise_toggle_bit(&e->cmp.value, sizeof e->cmp.value, z);
1599 bitwise_zero(&e->cmp.value, sizeof e->cmp.value, start, z - start);
1600 bitwise_zero(&e->cmp.mask, sizeof e->cmp.mask, start, z - start);
1601 new = expr_combine(EXPR_T_OR, new, e);
1602 }
1603 expr_destroy(expr);
1604 return new ? new : expr_create_boolean(false);
1605}
1606
1607/* Takes ownership of 'expr' and returns an equivalent expression whose
1608 * EXPR_T_CMP nodes use only tests for equality (EXPR_R_EQ). */
1609struct expr *
1610expr_simplify(struct expr *expr)
1611{
1612 struct expr *sub, *next;
1613
1614 switch (expr->type) {
1615 case EXPR_T_CMP:
1616 return (expr->cmp.relop == EXPR_R_EQ || !expr->cmp.symbol->width ? expr
1617 : expr->cmp.relop == EXPR_R_NE ? expr_simplify_ne(expr)
1618 : expr_simplify_relational(expr));
1619
1620 case EXPR_T_AND:
1621 case EXPR_T_OR:
1622 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
417e7e66 1623 ovs_list_remove(&sub->node);
e0840f11
BP
1624 expr_insert_andor(expr, next, expr_simplify(sub));
1625 }
1626 return expr_fix(expr);
1627
1628 case EXPR_T_BOOLEAN:
1629 return expr;
1630 }
1631 OVS_NOT_REACHED();
1632}
1633\f
1634static const struct expr_symbol *
1635expr_is_cmp(const struct expr *expr)
1636{
1637 switch (expr->type) {
1638 case EXPR_T_CMP:
1639 return expr->cmp.symbol;
1640
1641 case EXPR_T_AND:
1642 case EXPR_T_OR: {
1643 const struct expr_symbol *prev = NULL;
1644 struct expr *sub;
1645
1646 LIST_FOR_EACH (sub, node, &expr->andor) {
1647 const struct expr_symbol *symbol = expr_is_cmp(sub);
1648 if (!symbol || (prev && symbol != prev)) {
1649 return NULL;
1650 }
1651 prev = symbol;
1652 }
1653 return prev;
1654 }
1655
1656 case EXPR_T_BOOLEAN:
1657 return NULL;
1658
1659 default:
1660 OVS_NOT_REACHED();
1661 }
1662}
1663
1664struct expr_sort {
1665 struct expr *expr;
1666 const struct expr_symbol *relop;
1667 enum expr_type type;
1668};
1669
1670static int
1671compare_expr_sort(const void *a_, const void *b_)
1672{
1673 const struct expr_sort *a = a_;
1674 const struct expr_sort *b = b_;
1675
1676 if (a->type != b->type) {
1677 return a->type < b->type ? -1 : 1;
1678 } else if (a->relop) {
1679 int cmp = strcmp(a->relop->name, b->relop->name);
1680 if (cmp) {
1681 return cmp;
1682 }
1683
1684 enum expr_type a_type = a->expr->type;
1685 enum expr_type b_type = a->expr->type;
1686 return a_type < b_type ? -1 : a_type > b_type;
1687 } else if (a->type == EXPR_T_AND || a->type == EXPR_T_OR) {
417e7e66
BW
1688 size_t a_len = ovs_list_size(&a->expr->andor);
1689 size_t b_len = ovs_list_size(&b->expr->andor);
e0840f11
BP
1690 return a_len < b_len ? -1 : a_len > b_len;
1691 } else {
1692 return 0;
1693 }
1694}
1695
1696static struct expr *crush_cmps(struct expr *, const struct expr_symbol *);
1697
9d4aecca
BP
1698static bool
1699disjunction_matches_string(const struct expr *or, const char *s)
1700{
1701 const struct expr *sub;
1702
1703 LIST_FOR_EACH (sub, node, &or->andor) {
1704 if (!strcmp(sub->cmp.string, s)) {
1705 return true;
1706 }
1707 }
1708
1709 return false;
1710}
1711
1712/* Implementation of crush_cmps() for expr->type == EXPR_T_AND and a
1713 * string-typed 'symbol'. */
e0840f11 1714static struct expr *
9d4aecca
BP
1715crush_and_string(struct expr *expr, const struct expr_symbol *symbol)
1716{
417e7e66 1717 ovs_assert(!ovs_list_is_short(&expr->andor));
9d4aecca
BP
1718
1719 struct expr *singleton = NULL;
1720
1721 /* First crush each subexpression into either a single EXPR_T_CMP or an
1722 * EXPR_T_OR with EXPR_T_CMP subexpressions. */
1723 struct expr *sub, *next = NULL;
1724 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
417e7e66 1725 ovs_list_remove(&sub->node);
9d4aecca
BP
1726 struct expr *new = crush_cmps(sub, symbol);
1727 switch (new->type) {
1728 case EXPR_T_CMP:
1729 if (!singleton) {
417e7e66 1730 ovs_list_insert(&next->node, &new->node);
9d4aecca
BP
1731 singleton = new;
1732 } else {
1733 bool match = !strcmp(new->cmp.string, singleton->cmp.string);
1734 expr_destroy(new);
1735 if (!match) {
1736 expr_destroy(expr);
1737 return expr_create_boolean(false);
1738 }
1739 }
1740 break;
1741 case EXPR_T_AND:
1742 OVS_NOT_REACHED();
1743 case EXPR_T_OR:
417e7e66 1744 ovs_list_insert(&next->node, &new->node);
9d4aecca
BP
1745 break;
1746 case EXPR_T_BOOLEAN:
1747 if (!new->boolean) {
1748 expr_destroy(expr);
1749 return new;
1750 }
1751 free(new);
1752 break;
1753 }
1754 }
1755
1756 /* If we have a singleton, then the result is either the singleton itself
1757 * (if the ORs allow the singleton) or false. */
1758 if (singleton) {
1759 LIST_FOR_EACH (sub, node, &expr->andor) {
1760 if (sub->type == EXPR_T_OR
1761 && !disjunction_matches_string(sub, singleton->cmp.string)) {
1762 expr_destroy(expr);
1763 return expr_create_boolean(false);
1764 }
1765 }
417e7e66 1766 ovs_list_remove(&singleton->node);
9d4aecca
BP
1767 expr_destroy(expr);
1768 return singleton;
1769 }
1770
1771 /* Otherwise the result is the intersection of all of the ORs. */
1772 struct sset result = SSET_INITIALIZER(&result);
1773 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
1774 struct sset strings = SSET_INITIALIZER(&strings);
1775 const struct expr *s;
1776 LIST_FOR_EACH (s, node, &sub->andor) {
1777 sset_add(&strings, s->cmp.string);
1778 }
1779 if (sset_is_empty(&result)) {
1780 sset_swap(&result, &strings);
1781 } else {
1782 sset_intersect(&result, &strings);
1783 }
1784 sset_destroy(&strings);
1785
1786 if (sset_is_empty(&result)) {
1787 expr_destroy(expr);
1788 sset_destroy(&result);
1789 return expr_create_boolean(false);
1790 }
1791 }
1792
1793 expr_destroy(expr);
1794 expr = expr_create_andor(EXPR_T_OR);
1795
1796 const char *string;
1797 SSET_FOR_EACH (string, &result) {
1798 sub = xmalloc(sizeof *sub);
1799 sub->type = EXPR_T_CMP;
1800 sub->cmp.symbol = symbol;
1801 sub->cmp.string = xstrdup(string);
417e7e66 1802 ovs_list_push_back(&expr->andor, &sub->node);
9d4aecca 1803 }
1cad4a75 1804 sset_destroy(&result);
9d4aecca
BP
1805 return expr_fix(expr);
1806}
1807
1808/* Implementation of crush_cmps() for expr->type == EXPR_T_AND and a
1809 * numeric-typed 'symbol'. */
1810static struct expr *
1811crush_and_numeric(struct expr *expr, const struct expr_symbol *symbol)
e0840f11 1812{
417e7e66 1813 ovs_assert(!ovs_list_is_short(&expr->andor));
e0840f11
BP
1814
1815 union mf_subvalue value, mask;
1816 memset(&value, 0, sizeof value);
1817 memset(&mask, 0, sizeof mask);
1818
1819 struct expr *sub, *next = NULL;
1820 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
417e7e66 1821 ovs_list_remove(&sub->node);
e0840f11
BP
1822 struct expr *new = crush_cmps(sub, symbol);
1823 switch (new->type) {
1824 case EXPR_T_CMP:
1825 if (!mf_subvalue_intersect(&value, &mask,
1826 &new->cmp.value, &new->cmp.mask,
1827 &value, &mask)) {
1828 expr_destroy(new);
1829 expr_destroy(expr);
1830 return expr_create_boolean(false);
1831 }
1832 expr_destroy(new);
1833 break;
1834 case EXPR_T_AND:
1835 OVS_NOT_REACHED();
1836 case EXPR_T_OR:
417e7e66 1837 ovs_list_insert(&next->node, &new->node);
e0840f11
BP
1838 break;
1839 case EXPR_T_BOOLEAN:
1840 if (!new->boolean) {
1841 expr_destroy(expr);
1842 return new;
1843 }
45c4387b 1844 expr_destroy(new);
e0840f11
BP
1845 break;
1846 }
1847 }
417e7e66 1848 if (ovs_list_is_empty(&expr->andor)) {
e0840f11
BP
1849 if (is_all_zeros(&mask, sizeof mask)) {
1850 expr_destroy(expr);
1851 return expr_create_boolean(true);
1852 } else {
1853 struct expr *cmp;
1854 cmp = xmalloc(sizeof *cmp);
1855 cmp->type = EXPR_T_CMP;
1856 cmp->cmp.symbol = symbol;
1857 cmp->cmp.relop = EXPR_R_EQ;
1858 cmp->cmp.value = value;
1859 cmp->cmp.mask = mask;
1860 expr_destroy(expr);
1861 return cmp;
1862 }
417e7e66 1863 } else if (ovs_list_is_short(&expr->andor)) {
e0840f11
BP
1864 /* Transform "a && (b || c || d)" into "ab || ac || ad" where "ab" is
1865 * computed as "a && b", etc. */
417e7e66 1866 struct expr *disjuncts = expr_from_node(ovs_list_pop_front(&expr->andor));
e0840f11
BP
1867 struct expr *or;
1868
1869 or = xmalloc(sizeof *or);
1870 or->type = EXPR_T_OR;
417e7e66 1871 ovs_list_init(&or->andor);
e0840f11
BP
1872
1873 ovs_assert(disjuncts->type == EXPR_T_OR);
1874 LIST_FOR_EACH_SAFE (sub, next, node, &disjuncts->andor) {
1875 ovs_assert(sub->type == EXPR_T_CMP);
417e7e66 1876 ovs_list_remove(&sub->node);
e0840f11
BP
1877 if (mf_subvalue_intersect(&value, &mask,
1878 &sub->cmp.value, &sub->cmp.mask,
1879 &sub->cmp.value, &sub->cmp.mask)) {
417e7e66 1880 ovs_list_push_back(&or->andor, &sub->node);
e0840f11 1881 } else {
45c4387b 1882 expr_destroy(sub);
e0840f11
BP
1883 }
1884 }
1885 free(disjuncts);
1886 free(expr);
417e7e66 1887 if (ovs_list_is_empty(&or->andor)) {
e0840f11
BP
1888 free(or);
1889 return expr_create_boolean(false);
417e7e66
BW
1890 } else if (ovs_list_is_short(&or->andor)) {
1891 struct expr *cmp = expr_from_node(ovs_list_pop_front(&or->andor));
e0840f11
BP
1892 free(or);
1893 return cmp;
1894 } else {
1895 return or;
1896 }
1897 } else {
1898 /* Transform "x && (a0 || a1) && (b0 || b1) && ..." into
1899 * "(xa0b0 || xa0b1 || xa1b0 || xa1b1) && ...". */
417e7e66
BW
1900 struct expr *as = expr_from_node(ovs_list_pop_front(&expr->andor));
1901 struct expr *bs = expr_from_node(ovs_list_pop_front(&expr->andor));
e0840f11
BP
1902 struct expr *new = NULL;
1903 struct expr *or;
1904
1905 or = xmalloc(sizeof *or);
1906 or->type = EXPR_T_OR;
417e7e66 1907 ovs_list_init(&or->andor);
e0840f11
BP
1908
1909 struct expr *a;
1910 LIST_FOR_EACH (a, node, &as->andor) {
1911 union mf_subvalue a_value, a_mask;
1912
1913 ovs_assert(a->type == EXPR_T_CMP);
1914 if (!mf_subvalue_intersect(&value, &mask,
1915 &a->cmp.value, &a->cmp.mask,
1916 &a_value, &a_mask)) {
1917 continue;
1918 }
1919
1920 struct expr *b;
1921 LIST_FOR_EACH (b, node, &bs->andor) {
1922 ovs_assert(b->type == EXPR_T_CMP);
1923 if (!new) {
1924 new = xmalloc(sizeof *new);
1925 new->type = EXPR_T_CMP;
1926 new->cmp.symbol = symbol;
1927 new->cmp.relop = EXPR_R_EQ;
1928 }
1929 if (mf_subvalue_intersect(&a_value, &a_mask,
1930 &b->cmp.value, &b->cmp.mask,
1931 &new->cmp.value, &new->cmp.mask)) {
417e7e66 1932 ovs_list_push_back(&or->andor, &new->node);
e0840f11
BP
1933 new = NULL;
1934 }
1935 }
1936 }
1937 expr_destroy(as);
1938 expr_destroy(bs);
1939 free(new);
1940
417e7e66 1941 if (ovs_list_is_empty(&or->andor)) {
e0840f11
BP
1942 expr_destroy(expr);
1943 free(or);
1944 return expr_create_boolean(false);
417e7e66
BW
1945 } else if (ovs_list_is_short(&or->andor)) {
1946 struct expr *cmp = expr_from_node(ovs_list_pop_front(&or->andor));
e0840f11 1947 free(or);
417e7e66 1948 if (ovs_list_is_empty(&expr->andor)) {
e0840f11
BP
1949 expr_destroy(expr);
1950 return crush_cmps(cmp, symbol);
1951 } else {
1952 return crush_cmps(expr_combine(EXPR_T_AND, cmp, expr), symbol);
1953 }
417e7e66 1954 } else if (!ovs_list_is_empty(&expr->andor)) {
e0840f11 1955 struct expr *e = expr_combine(EXPR_T_AND, or, expr);
417e7e66 1956 ovs_assert(!ovs_list_is_short(&e->andor));
e0840f11
BP
1957 return crush_cmps(e, symbol);
1958 } else {
1959 expr_destroy(expr);
1960 return crush_cmps(or, symbol);
1961 }
1962 }
1963}
1964
1965static int
9d4aecca
BP
1966compare_cmps_3way(const struct expr *a, const struct expr *b)
1967{
1968 ovs_assert(a->cmp.symbol == b->cmp.symbol);
1969 if (!a->cmp.symbol->width) {
1970 return strcmp(a->cmp.string, b->cmp.string);
1971 } else {
1972 int d = memcmp(&a->cmp.value, &b->cmp.value, sizeof a->cmp.value);
1973 if (!d) {
1974 d = memcmp(&a->cmp.mask, &b->cmp.mask, sizeof a->cmp.mask);
1975 }
1976 return d;
1977 }
1978}
1979
1980static int
1981compare_cmps_cb(const void *a_, const void *b_)
e0840f11
BP
1982{
1983 const struct expr *const *ap = a_;
1984 const struct expr *const *bp = b_;
1985 const struct expr *a = *ap;
1986 const struct expr *b = *bp;
9d4aecca 1987 return compare_cmps_3way(a, b);
e0840f11
BP
1988}
1989
fd477c6e 1990/* Implementation of crush_cmps() for expr->type == EXPR_T_OR. */
e0840f11
BP
1991static struct expr *
1992crush_or(struct expr *expr, const struct expr_symbol *symbol)
1993{
1994 struct expr *sub, *next = NULL;
1995
1996 /* First, crush all the subexpressions. That might eliminate the
fd477c6e
BP
1997 * OR-expression entirely; if so, return the result. Otherwise, 'expr'
1998 * is now a disjunction of cmps over the same symbol. */
e0840f11 1999 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
417e7e66 2000 ovs_list_remove(&sub->node);
e0840f11
BP
2001 expr_insert_andor(expr, next, crush_cmps(sub, symbol));
2002 }
2003 expr = expr_fix(expr);
2004 if (expr->type != EXPR_T_OR) {
2005 return expr;
2006 }
2007
fd477c6e 2008 /* Sort subexpressions by value and mask, to bring together duplicates. */
417e7e66 2009 size_t n = ovs_list_size(&expr->andor);
e0840f11
BP
2010 struct expr **subs = xmalloc(n * sizeof *subs);
2011
2012 size_t i = 0;
2013 LIST_FOR_EACH (sub, node, &expr->andor) {
2014 subs[i++] = sub;
2015 }
2016 ovs_assert(i == n);
2017
9d4aecca 2018 qsort(subs, n, sizeof *subs, compare_cmps_cb);
e0840f11 2019
9d4aecca 2020 /* Eliminate duplicates. */
417e7e66
BW
2021 ovs_list_init(&expr->andor);
2022 ovs_list_push_back(&expr->andor, &subs[0]->node);
e0840f11 2023 for (i = 1; i < n; i++) {
417e7e66 2024 struct expr *a = expr_from_node(ovs_list_back(&expr->andor));
e0840f11 2025 struct expr *b = subs[i];
9d4aecca 2026 if (compare_cmps_3way(a, b)) {
417e7e66 2027 ovs_list_push_back(&expr->andor, &b->node);
e0840f11 2028 } else {
45c4387b 2029 expr_destroy(b);
e0840f11
BP
2030 }
2031 }
2032 free(subs);
2033 return expr_fix(expr);
2034}
2035
fd477c6e
BP
2036/* Takes ownership of 'expr', which must be a cmp in the sense determined by
2037 * 'expr_is_cmp(expr)', where 'symbol' is the symbol returned by that function.
2038 * Returns an equivalent expression owned by the caller that is a single
2039 * EXPR_T_CMP or a disjunction of them or a EXPR_T_BOOLEAN. */
e0840f11
BP
2040static struct expr *
2041crush_cmps(struct expr *expr, const struct expr_symbol *symbol)
2042{
2043 switch (expr->type) {
2044 case EXPR_T_OR:
2045 return crush_or(expr, symbol);
2046
2047 case EXPR_T_AND:
9d4aecca
BP
2048 return (symbol->width
2049 ? crush_and_numeric(expr, symbol)
2050 : crush_and_string(expr, symbol));
e0840f11
BP
2051
2052 case EXPR_T_CMP:
2053 return expr;
2054
2055 case EXPR_T_BOOLEAN:
2056 return expr;
2057
2058 default:
2059 OVS_NOT_REACHED();
2060 }
2061}
2062
2063static struct expr *
2064expr_sort(struct expr *expr)
2065{
417e7e66 2066 size_t n = ovs_list_size(&expr->andor);
e0840f11
BP
2067 struct expr_sort *subs = xmalloc(n * sizeof *subs);
2068 struct expr *sub;
2069 size_t i;
2070
2071 i = 0;
2072 LIST_FOR_EACH (sub, node, &expr->andor) {
2073 subs[i].expr = sub;
2074 subs[i].relop = expr_is_cmp(sub);
2075 subs[i].type = subs[i].relop ? EXPR_T_CMP : sub->type;
2076 i++;
2077 }
2078 ovs_assert(i == n);
2079
2080 qsort(subs, n, sizeof *subs, compare_expr_sort);
2081
417e7e66 2082 ovs_list_init(&expr->andor);
e0840f11
BP
2083 for (int i = 0; i < n; ) {
2084 if (subs[i].relop) {
2085 int j;
2086 for (j = i + 1; j < n; j++) {
2087 if (subs[i].relop != subs[j].relop) {
2088 break;
2089 }
2090 }
2091
2092 struct expr *crushed;
2093 if (j == i + 1) {
2094 crushed = crush_cmps(subs[i].expr, subs[i].relop);
2095 } else {
2096 struct expr *combined = subs[i].expr;
2097 for (int k = i + 1; k < j; k++) {
2098 combined = expr_combine(EXPR_T_AND, combined,
2099 subs[k].expr);
2100 }
417e7e66 2101 ovs_assert(!ovs_list_is_short(&combined->andor));
e0840f11
BP
2102 crushed = crush_cmps(combined, subs[i].relop);
2103 }
2104 if (crushed->type == EXPR_T_BOOLEAN) {
2105 if (!crushed->boolean) {
2106 for (int k = j; k < n; k++) {
2107 expr_destroy(subs[k].expr);
2108 }
2109 expr_destroy(expr);
2110 expr = crushed;
2111 break;
2112 } else {
2113 free(crushed);
2114 }
2115 } else {
2116 expr = expr_combine(EXPR_T_AND, expr, crushed);
2117 }
2118 i = j;
2119 } else {
2120 expr = expr_combine(EXPR_T_AND, expr, subs[i++].expr);
2121 }
2122 }
2123 free(subs);
2124
2125 return expr;
2126}
2127
2128static struct expr *expr_normalize_or(struct expr *expr);
2129
2130/* Returns 'expr', which is an AND, reduced to OR(AND(clause)) where
2131 * a clause is a cmp or a disjunction of cmps on a single field. */
2132static struct expr *
2133expr_normalize_and(struct expr *expr)
2134{
2135 ovs_assert(expr->type == EXPR_T_AND);
2136
2137 expr = expr_sort(expr);
2138 if (expr->type != EXPR_T_AND) {
2139 ovs_assert(expr->type == EXPR_T_BOOLEAN);
2140 return expr;
2141 }
2142
2143 struct expr *a, *b;
2144 LIST_FOR_EACH_SAFE (a, b, node, &expr->andor) {
2145 if (&b->node == &expr->andor
9d4aecca
BP
2146 || a->type != EXPR_T_CMP || b->type != EXPR_T_CMP
2147 || a->cmp.symbol != b->cmp.symbol) {
e0840f11 2148 continue;
9d4aecca
BP
2149 } else if (a->cmp.symbol->width
2150 ? mf_subvalue_intersect(&a->cmp.value, &a->cmp.mask,
2151 &b->cmp.value, &b->cmp.mask,
2152 &b->cmp.value, &b->cmp.mask)
2153 : !strcmp(a->cmp.string, b->cmp.string)) {
417e7e66 2154 ovs_list_remove(&a->node);
e0840f11
BP
2155 expr_destroy(a);
2156 } else {
2157 expr_destroy(expr);
2158 return expr_create_boolean(false);
2159 }
2160 }
417e7e66
BW
2161 if (ovs_list_is_short(&expr->andor)) {
2162 struct expr *sub = expr_from_node(ovs_list_front(&expr->andor));
e0840f11
BP
2163 free(expr);
2164 return sub;
2165 }
2166
2167 struct expr *sub;
2168 LIST_FOR_EACH (sub, node, &expr->andor) {
2169 if (sub->type == EXPR_T_CMP) {
2170 continue;
2171 }
2172
2173 ovs_assert(sub->type == EXPR_T_OR);
2174 const struct expr_symbol *symbol = expr_is_cmp(sub);
2175 if (!symbol || symbol->must_crossproduct) {
2176 struct expr *or = expr_create_andor(EXPR_T_OR);
2177 struct expr *k;
2178
2179 LIST_FOR_EACH (k, node, &sub->andor) {
2180 struct expr *and = expr_create_andor(EXPR_T_AND);
2181 struct expr *m;
2182
2183 LIST_FOR_EACH (m, node, &expr->andor) {
2184 struct expr *term = m == sub ? k : m;
2185 if (term->type == EXPR_T_AND) {
2186 struct expr *p;
2187
2188 LIST_FOR_EACH (p, node, &term->andor) {
2189 struct expr *new = expr_clone(p);
417e7e66 2190 ovs_list_push_back(&and->andor, &new->node);
e0840f11
BP
2191 }
2192 } else {
2193 struct expr *new = expr_clone(term);
417e7e66 2194 ovs_list_push_back(&and->andor, &new->node);
e0840f11
BP
2195 }
2196 }
417e7e66 2197 ovs_list_push_back(&or->andor, &and->node);
e0840f11
BP
2198 }
2199 expr_destroy(expr);
2200 return expr_normalize_or(or);
2201 }
2202 }
2203 return expr;
2204}
2205
2206static struct expr *
2207expr_normalize_or(struct expr *expr)
2208{
2209 struct expr *sub, *next;
2210
2211 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
2212 if (sub->type == EXPR_T_AND) {
417e7e66 2213 ovs_list_remove(&sub->node);
e0840f11
BP
2214
2215 struct expr *new = expr_normalize_and(sub);
2216 if (new->type == EXPR_T_BOOLEAN) {
2217 if (new->boolean) {
2218 expr_destroy(expr);
2219 return new;
2220 }
2221 free(new);
2222 } else {
2223 expr_insert_andor(expr, next, new);
2224 }
2225 } else {
2226 ovs_assert(sub->type == EXPR_T_CMP);
2227 }
2228 }
417e7e66 2229 if (ovs_list_is_empty(&expr->andor)) {
e0840f11
BP
2230 free(expr);
2231 return expr_create_boolean(false);
2232 }
417e7e66
BW
2233 if (ovs_list_is_short(&expr->andor)) {
2234 struct expr *sub = expr_from_node(ovs_list_pop_front(&expr->andor));
e0840f11
BP
2235 free(expr);
2236 return sub;
2237 }
2238
2239 return expr;
2240}
2241
2242/* Takes ownership of 'expr', which is either a constant "true" or "false" or
2243 * an expression in terms of only relationals, AND, and OR. Returns either a
2244 * constant "true" or "false" or 'expr' reduced to OR(AND(clause)) where a
2245 * clause is a cmp or a disjunction of cmps on a single field. This form is
2246 * significant because it is a form that can be directly converted to OpenFlow
2247 * flows with the Open vSwitch "conjunctive match" extension.
2248 *
2249 * 'expr' must already have been simplified, with expr_simplify(). */
2250struct expr *
2251expr_normalize(struct expr *expr)
2252{
2253 switch (expr->type) {
2254 case EXPR_T_CMP:
2255 return expr;
2256
2257 case EXPR_T_AND:
2258 return expr_normalize_and(expr);
2259
2260 case EXPR_T_OR:
2261 return expr_normalize_or(expr);
2262
2263 case EXPR_T_BOOLEAN:
2264 return expr;
2265 }
2266 OVS_NOT_REACHED();
2267}
2268\f
2269/* Creates, initializes, and returns a new 'struct expr_match'. If 'm' is
2270 * nonnull then it is copied into the new expr_match, otherwise the new
2271 * expr_match's 'match' member is initialized to a catch-all match for the
2272 * caller to refine in-place.
2273 *
2274 * If 'conj_id' is nonzero, adds one conjunction based on 'conj_id', 'clause',
2275 * and 'n_clauses' to the returned 'struct expr_match', otherwise the
2276 * expr_match will not have any conjunctions.
2277 *
2278 * The caller should use expr_match_add() to add the expr_match to a hash table
2279 * after it is finalized. */
2280static struct expr_match *
2281expr_match_new(const struct match *m, uint8_t clause, uint8_t n_clauses,
2282 uint32_t conj_id)
2283{
2284 struct expr_match *match = xmalloc(sizeof *match);
2285 if (m) {
2286 match->match = *m;
2287 } else {
2288 match_init_catchall(&match->match);
2289 }
2290 if (conj_id) {
2291 match->conjunctions = xmalloc(sizeof *match->conjunctions);
2292 match->conjunctions[0].id = conj_id;
2293 match->conjunctions[0].clause = clause;
2294 match->conjunctions[0].n_clauses = n_clauses;
2295 match->n = 1;
2296 match->allocated = 1;
2297 } else {
2298 match->conjunctions = NULL;
2299 match->n = 0;
2300 match->allocated = 0;
2301 }
2302 return match;
2303}
2304
2305/* Adds 'match' to hash table 'matches', which becomes the new owner of
2306 * 'match'.
2307 *
2308 * This might actually destroy 'match' because it gets merged together with
2309 * some existing conjunction.*/
2310static void
2311expr_match_add(struct hmap *matches, struct expr_match *match)
2312{
2313 uint32_t hash = match_hash(&match->match, 0);
2314 struct expr_match *m;
2315
2316 HMAP_FOR_EACH_WITH_HASH (m, hmap_node, hash, matches) {
2317 if (match_equal(&m->match, &match->match)) {
2318 if (!m->n || !match->n) {
2319 free(m->conjunctions);
2320 m->conjunctions = NULL;
2321 m->n = 0;
2322 m->allocated = 0;
2323 } else {
2324 ovs_assert(match->n == 1);
2325 if (m->n >= m->allocated) {
2326 m->conjunctions = x2nrealloc(m->conjunctions,
2327 &m->allocated,
2328 sizeof *m->conjunctions);
2329 }
2330 m->conjunctions[m->n++] = match->conjunctions[0];
2331 }
2332 free(match->conjunctions);
2333 free(match);
2334 return;
2335 }
2336 }
2337
2338 hmap_insert(matches, &match->hmap_node, hash);
2339}
2340
f386a8a7 2341static bool
f1c16a85
BP
2342constrain_match(const struct expr *expr,
2343 bool (*lookup_port)(const void *aux, const char *port_name,
2344 unsigned int *portp),
2345 const void *aux, struct match *m)
e0840f11
BP
2346{
2347 ovs_assert(expr->type == EXPR_T_CMP);
f386a8a7
BP
2348 if (expr->cmp.symbol->width) {
2349 mf_mask_subfield(expr->cmp.symbol->field, &expr->cmp.value,
2350 &expr->cmp.mask, m);
2351 } else {
f1c16a85
BP
2352 unsigned int port;
2353 if (!lookup_port(aux, expr->cmp.string, &port)) {
f386a8a7
BP
2354 return false;
2355 }
2356
2357 struct mf_subfield sf;
2358 sf.field = expr->cmp.symbol->field;
2359 sf.ofs = 0;
2360 sf.n_bits = expr->cmp.symbol->field->n_bits;
2361
2362 union mf_subvalue x;
2363 memset(&x, 0, sizeof x);
f1c16a85 2364 x.integer = htonll(port);
f386a8a7
BP
2365
2366 mf_write_subfield(&sf, &x, m);
2367 }
2368 return true;
e0840f11
BP
2369}
2370
f386a8a7 2371static bool
f1c16a85
BP
2372add_disjunction(const struct expr *or,
2373 bool (*lookup_port)(const void *aux, const char *port_name,
2374 unsigned int *portp),
2375 const void *aux,
f386a8a7
BP
2376 struct match *m, uint8_t clause, uint8_t n_clauses,
2377 uint32_t conj_id, struct hmap *matches)
e0840f11
BP
2378{
2379 struct expr *sub;
f386a8a7 2380 int n = 0;
e0840f11
BP
2381
2382 ovs_assert(or->type == EXPR_T_OR);
2383 LIST_FOR_EACH (sub, node, &or->andor) {
2384 struct expr_match *match = expr_match_new(m, clause, n_clauses,
2385 conj_id);
f1c16a85 2386 if (constrain_match(sub, lookup_port, aux, &match->match)) {
f386a8a7
BP
2387 expr_match_add(matches, match);
2388 n++;
2389 } else {
2390 free(match->conjunctions);
2391 free(match);
2392 }
e0840f11 2393 }
f386a8a7
BP
2394
2395 /* If n == 1, then this didn't really need to be a disjunction. Oh well,
2396 * that shouldn't happen much. */
2397 return n > 0;
e0840f11
BP
2398}
2399
2400static void
f1c16a85
BP
2401add_conjunction(const struct expr *and,
2402 bool (*lookup_port)(const void *aux, const char *port_name,
2403 unsigned int *portp),
2404 const void *aux, uint32_t *n_conjsp, struct hmap *matches)
e0840f11
BP
2405{
2406 struct match match;
2407 int n_clauses = 0;
2408 struct expr *sub;
2409
2410 match_init_catchall(&match);
2411
2412 ovs_assert(and->type == EXPR_T_AND);
2413 LIST_FOR_EACH (sub, node, &and->andor) {
2414 switch (sub->type) {
2415 case EXPR_T_CMP:
f1c16a85 2416 if (!constrain_match(sub, lookup_port, aux, &match)) {
f386a8a7
BP
2417 return;
2418 }
e0840f11
BP
2419 break;
2420 case EXPR_T_OR:
2421 n_clauses++;
2422 break;
2423 case EXPR_T_AND:
2424 case EXPR_T_BOOLEAN:
2425 OVS_NOT_REACHED();
2426 }
2427 }
2428
2429 if (!n_clauses) {
2430 expr_match_add(matches, expr_match_new(&match, 0, 0, 0));
2431 } else if (n_clauses == 1) {
2432 LIST_FOR_EACH (sub, node, &and->andor) {
2433 if (sub->type == EXPR_T_OR) {
f1c16a85
BP
2434 add_disjunction(sub, lookup_port, aux, &match, 0, 0, 0,
2435 matches);
e0840f11
BP
2436 }
2437 }
2438 } else {
2439 int clause = 0;
2440 (*n_conjsp)++;
2441 LIST_FOR_EACH (sub, node, &and->andor) {
2442 if (sub->type == EXPR_T_OR) {
f1c16a85 2443 if (!add_disjunction(sub, lookup_port, aux, &match, clause++,
f386a8a7
BP
2444 n_clauses, *n_conjsp, matches)) {
2445 /* This clause can't ever match, so we might as well skip
2446 * adding the other clauses--the overall disjunctive flow
2447 * can't ever match. Ideally we would also back out all of
2448 * the clauses we already added, but that seems like a lot
2449 * of trouble for a case that might never occur in
2450 * practice. */
2451 return;
2452 }
e0840f11
BP
2453 }
2454 }
40e07b2a
BP
2455
2456 /* Add the flow that matches on conj_id. */
2457 match_set_conj_id(&match, *n_conjsp);
2458 expr_match_add(matches, expr_match_new(&match, 0, 0, 0));
e0840f11
BP
2459 }
2460}
2461
2462static void
f1c16a85
BP
2463add_cmp_flow(const struct expr *cmp,
2464 bool (*lookup_port)(const void *aux, const char *port_name,
2465 unsigned int *portp),
2466 const void *aux, struct hmap *matches)
e0840f11
BP
2467{
2468 struct expr_match *m = expr_match_new(NULL, 0, 0, 0);
f1c16a85 2469 if (constrain_match(cmp, lookup_port, aux, &m->match)) {
f386a8a7
BP
2470 expr_match_add(matches, m);
2471 } else {
2472 free(m);
2473 }
e0840f11
BP
2474}
2475
2476/* Converts 'expr', which must be in the form returned by expr_normalize(), to
2477 * a collection of Open vSwitch flows in 'matches', which this function
f386a8a7
BP
2478 * initializes to an hmap of "struct expr_match" structures. Returns the
2479 * number of conjunctive match IDs consumed by 'matches', which uses
2480 * conjunctive match IDs beginning with 0; the caller must offset or remap them
2481 * into the desired range as necessary.
2482 *
40e07b2a
BP
2483 * The matches inserted into 'matches' will be of three distinct kinds:
2484 *
2485 * - Ordinary flows. The caller should add these OpenFlow flows with
2486 * its desired actions.
2487 *
2488 * - Conjunctive flows, distinguished by 'n > 0' in the expr_match
2489 * structure. The caller should add these OpenFlow flows with the
2490 * conjunction(id, k/n) actions as specified in the 'conjunctions' array,
2491 * remapping the ids.
2492 *
2493 * - conj_id flows, distinguished by matching on the "conj_id" field. The
2494 * caller should remap the conj_id and add the OpenFlow flow with its
2495 * desired actions.
2496 *
f1c16a85
BP
2497 * 'lookup_port' must be a function to map from a port name to a port number.
2498 * When successful, 'lookup_port' stores the port number into '*portp' and
2499 * returns true; when there is no port by the given name, it returns false.
2500 * 'aux' is passed to 'lookup_port' as auxiliary data. Any comparisons against
2501 * string fields in 'expr' are translated into integers through this function.
2502 * A comparison against a string that is not in 'ports' acts like a Boolean
2503 * "false"; that is, it will always fail to match. For a simple expression,
2504 * this means that the overall expression always fails to match, but an
2505 * expression with a disjunction on the string field might still match on other
2506 * port names.
f386a8a7
BP
2507 *
2508 * (This treatment of string fields might be too simplistic in general, but it
2509 * seems reasonable for now when string fields are used only for ports.) */
e0840f11 2510uint32_t
f1c16a85
BP
2511expr_to_matches(const struct expr *expr,
2512 bool (*lookup_port)(const void *aux, const char *port_name,
2513 unsigned int *portp),
2514 const void *aux, struct hmap *matches)
e0840f11
BP
2515{
2516 uint32_t n_conjs = 0;
2517
2518 hmap_init(matches);
2519 switch (expr->type) {
2520 case EXPR_T_CMP:
f1c16a85 2521 add_cmp_flow(expr, lookup_port, aux, matches);
e0840f11
BP
2522 break;
2523
2524 case EXPR_T_AND:
f1c16a85 2525 add_conjunction(expr, lookup_port, aux, &n_conjs, matches);
e0840f11
BP
2526 break;
2527
2528 case EXPR_T_OR:
2529 if (expr_is_cmp(expr)) {
2530 struct expr *sub;
2531
2532 LIST_FOR_EACH (sub, node, &expr->andor) {
f1c16a85 2533 add_cmp_flow(sub, lookup_port, aux, matches);
e0840f11
BP
2534 }
2535 } else {
2536 struct expr *sub;
2537
2538 LIST_FOR_EACH (sub, node, &expr->andor) {
2539 if (sub->type == EXPR_T_AND) {
f1c16a85 2540 add_conjunction(sub, lookup_port, aux, &n_conjs, matches);
e0840f11 2541 } else {
f1c16a85 2542 add_cmp_flow(sub, lookup_port, aux, matches);
e0840f11
BP
2543 }
2544 }
2545 }
2546 break;
2547
2548 case EXPR_T_BOOLEAN:
2549 if (expr->boolean) {
2550 struct expr_match *m = expr_match_new(NULL, 0, 0, 0);
2551 expr_match_add(matches, m);
2552 } else {
2553 /* No match. */
2554 }
2555 break;
2556 }
2557 return n_conjs;
2558}
f386a8a7
BP
2559
2560/* Destroys all of the 'struct expr_match'es in 'matches', as well as the
2561 * 'matches' hmap itself. */
2562void
2563expr_matches_destroy(struct hmap *matches)
2564{
4ec3d7c7 2565 struct expr_match *m;
f386a8a7 2566
4ec3d7c7 2567 HMAP_FOR_EACH_POP (m, hmap_node, matches) {
f386a8a7
BP
2568 free(m->conjunctions);
2569 free(m);
2570 }
2571 hmap_destroy(matches);
2572}
2573
2574/* Prints a representation of the 'struct expr_match'es in 'matches' to
2575 * 'stream'. */
2576void
2577expr_matches_print(const struct hmap *matches, FILE *stream)
2578{
2579 if (hmap_is_empty(matches)) {
2580 fputs("(no flows)\n", stream);
2581 return;
2582 }
2583
2584 const struct expr_match *m;
2585 HMAP_FOR_EACH (m, hmap_node, matches) {
2586 char *s = match_to_string(&m->match, OFP_DEFAULT_PRIORITY);
2587 fputs(s, stream);
2588 free(s);
2589
2590 if (m->n) {
2591 for (int i = 0; i < m->n; i++) {
2592 const struct cls_conjunction *c = &m->conjunctions[i];
2593 fprintf(stream, "%c conjunction(%"PRIu32", %d/%d)",
2594 i == 0 ? ':' : ',', c->id, c->clause, c->n_clauses);
2595 }
2596 }
2597 putc('\n', stream);
2598 }
2599}
e0840f11
BP
2600\f
2601/* Returns true if 'expr' honors the invariants for expressions (see the large
2602 * comment above "struct expr" in expr.h), false otherwise. */
2603bool
2604expr_honors_invariants(const struct expr *expr)
2605{
2606 const struct expr *sub;
2607
2608 switch (expr->type) {
2609 case EXPR_T_CMP:
2610 if (expr->cmp.symbol->width) {
2611 for (int i = 0; i < ARRAY_SIZE(expr->cmp.value.be64); i++) {
2612 if (expr->cmp.value.be64[i] & ~expr->cmp.mask.be64[i]) {
2613 return false;
2614 }
2615 }
2616 }
2617 return true;
2618
2619 case EXPR_T_AND:
2620 case EXPR_T_OR:
417e7e66 2621 if (ovs_list_is_short(&expr->andor)) {
e0840f11
BP
2622 return false;
2623 }
2624 LIST_FOR_EACH (sub, node, &expr->andor) {
2625 if (sub->type == expr->type || !expr_honors_invariants(sub)) {
2626 return false;
2627 }
2628 }
2629 return true;
2630
2631 case EXPR_T_BOOLEAN:
2632 return true;
2633
2634 default:
2635 OVS_NOT_REACHED();
2636 }
2637}
2638
2639static bool
2640expr_is_normalized_and(const struct expr *expr)
2641{
2642 /* XXX should also check that no symbol is repeated. */
2643 const struct expr *sub;
2644
2645 LIST_FOR_EACH (sub, node, &expr->andor) {
2646 if (!expr_is_cmp(sub)) {
2647 return false;
2648 }
2649 }
2650 return true;
2651}
2652
2653/* Returns true if 'expr' is in the form returned by expr_normalize(), false
2654 * otherwise. */
2655bool
2656expr_is_normalized(const struct expr *expr)
2657{
2658 switch (expr->type) {
2659 case EXPR_T_CMP:
2660 return true;
2661
2662 case EXPR_T_AND:
2663 return expr_is_normalized_and(expr);
2664
2665 case EXPR_T_OR:
2666 if (!expr_is_cmp(expr)) {
2667 const struct expr *sub;
2668
2669 LIST_FOR_EACH (sub, node, &expr->andor) {
2670 if (!expr_is_cmp(sub) && !expr_is_normalized_and(sub)) {
2671 return false;
2672 }
2673 }
2674 }
2675 return true;
2676
2677 case EXPR_T_BOOLEAN:
2678 return true;
2679
2680 default:
2681 OVS_NOT_REACHED();
2682 }
2683}
3b7cb7e1
BP
2684\f
2685/* Action parsing helper. */
2686
a20c96c6
BP
2687/* Expands 'f' repeatedly as long as it has an expansion, that is, as long as
2688 * it is a subfield or a predicate. Adds any prerequisites for 'f' to
2689 * '*prereqs'.
2690 *
2691 * If 'rw', verifies that 'f' is a read/write field.
2692 *
a20c96c6
BP
2693 * Returns true if successful, false if an error was encountered (in which case
2694 * 'ctx->error' reports the particular error). */
5ee054fb 2695static bool
ce57ea75 2696expand_symbol(struct expr_context *ctx, bool rw,
a20c96c6 2697 struct expr_field *f, struct expr **prereqsp)
3b7cb7e1 2698{
a20c96c6
BP
2699 const struct expr_symbol *orig_symbol = f->symbol;
2700
5ee054fb 2701 if (f->symbol->expansion && f->symbol->level != EXPR_L_ORDINAL) {
ce57ea75
BP
2702 expr_error(ctx, "Predicate symbol %s used where lvalue required.",
2703 f->symbol->name);
5ee054fb 2704 return false;
3b7cb7e1
BP
2705 }
2706
3b7cb7e1
BP
2707 for (;;) {
2708 /* Accumulate prerequisites. */
5ee054fb 2709 if (f->symbol->prereqs) {
3b7cb7e1
BP
2710 struct ovs_list nesting = OVS_LIST_INITIALIZER(&nesting);
2711 char *error;
2712 struct expr *e;
5ee054fb 2713 e = parse_and_annotate(f->symbol->prereqs, ctx->symtab, &nesting,
3b7cb7e1
BP
2714 &error);
2715 if (error) {
2716 expr_error(ctx, "%s", error);
2717 free(error);
5ee054fb 2718 return false;
3b7cb7e1 2719 }
5ee054fb 2720 *prereqsp = expr_combine(EXPR_T_AND, *prereqsp, e);
3b7cb7e1
BP
2721 }
2722
2723 /* If there's no expansion, we're done. */
5ee054fb 2724 if (!f->symbol->expansion) {
3b7cb7e1
BP
2725 break;
2726 }
2727
2728 /* Expand. */
2729 struct expr_field expansion;
2730 char *error;
5ee054fb 2731 if (!parse_field_from_string(f->symbol->expansion, ctx->symtab,
3b7cb7e1
BP
2732 &expansion, &error)) {
2733 expr_error(ctx, "%s", error);
2734 free(error);
5ee054fb 2735 return false;
3b7cb7e1 2736 }
5ee054fb
BP
2737 f->symbol = expansion.symbol;
2738 f->ofs += expansion.ofs;
3b7cb7e1
BP
2739 }
2740
a20c96c6
BP
2741 if (rw && !f->symbol->field->writable) {
2742 expr_error(ctx, "Field %s is not modifiable.", orig_symbol->name);
2743 return false;
2744 }
2745
5ee054fb
BP
2746 return true;
2747}
2748
a20c96c6
BP
2749static void
2750mf_subfield_from_expr_field(const struct expr_field *f, struct mf_subfield *sf)
2751{
2752 sf->field = f->symbol->field;
2753 sf->ofs = f->ofs;
2754 sf->n_bits = f->n_bits ? f->n_bits : f->symbol->field->n_bits;
2755}
2756
2757static void
2758init_stack_action(const struct expr_field *f, struct ofpact_stack *stack)
2759{
2760 mf_subfield_from_expr_field(f, &stack->subfield);
2761}
2762
8fb72d29
BP
2763static char * OVS_WARN_UNUSED_RESULT
2764parse_assignment(struct lexer *lexer, struct expr_field *dst,
2765 const struct shash *symtab, bool exchange,
f1c16a85
BP
2766 bool (*lookup_port)(const void *aux, const char *port_name,
2767 unsigned int *portp),
8fb72d29
BP
2768 const void *aux, struct ofpbuf *ofpacts,
2769 struct expr **prereqsp)
5ee054fb 2770{
8fb72d29 2771 struct expr_context ctx = { .lexer = lexer, .symtab = symtab };
5ee054fb
BP
2772 struct expr *prereqs = NULL;
2773
2774 /* Parse destination and do basic checking. */
8fb72d29
BP
2775 const struct expr_symbol *orig_dst = dst->symbol;
2776 if (!expand_symbol(&ctx, true, dst, &prereqs)) {
5ee054fb 2777 goto exit;
3b7cb7e1
BP
2778 }
2779
8fb72d29 2780 if (exchange || ctx.lexer->token.type == LEX_T_ID) {
5ee054fb 2781 struct expr_field src;
8fb72d29 2782 if (!parse_field(&ctx, &src)) {
5ee054fb
BP
2783 goto exit;
2784 }
2785 const struct expr_symbol *orig_src = src.symbol;
8fb72d29 2786 if (!expand_symbol(&ctx, exchange, &src, &prereqs)) {
5ee054fb
BP
2787 goto exit;
2788 }
2789
8fb72d29 2790 if ((dst->symbol->width != 0) != (src.symbol->width != 0)) {
a20c96c6 2791 if (exchange) {
8fb72d29 2792 expr_error(&ctx,
a20c96c6
BP
2793 "Can't exchange %s field (%s) with %s field (%s).",
2794 orig_dst->width ? "integer" : "string",
2795 orig_dst->name,
2796 orig_src->width ? "integer" : "string",
2797 orig_src->name);
2798 } else {
8fb72d29
BP
2799 expr_error(&ctx,
2800 "Can't assign %s field (%s) to %s field (%s).",
a20c96c6
BP
2801 orig_src->width ? "integer" : "string",
2802 orig_src->name,
2803 orig_dst->width ? "integer" : "string",
2804 orig_dst->name);
2805 }
5ee054fb
BP
2806 goto exit;
2807 }
2808
8fb72d29 2809 if (dst->n_bits != src.n_bits) {
a20c96c6 2810 if (exchange) {
8fb72d29 2811 expr_error(&ctx,
a20c96c6 2812 "Can't exchange %d-bit field with %d-bit field.",
8fb72d29 2813 dst->n_bits, src.n_bits);
a20c96c6 2814 } else {
8fb72d29 2815 expr_error(&ctx,
a20c96c6 2816 "Can't assign %d-bit value to %d-bit destination.",
8fb72d29 2817 src.n_bits, dst->n_bits);
a20c96c6 2818 }
5ee054fb 2819 goto exit;
8fb72d29
BP
2820 } else if (!dst->n_bits &&
2821 dst->symbol->field->n_bits != src.symbol->field->n_bits) {
2822 expr_error(&ctx, "String fields %s and %s are incompatible for "
a20c96c6
BP
2823 "%s.", orig_dst->name, orig_src->name,
2824 exchange ? "exchange" : "assignment");
5ee054fb 2825 goto exit;
3b7cb7e1
BP
2826 }
2827
a20c96c6
BP
2828 if (exchange) {
2829 init_stack_action(&src, ofpact_put_STACK_PUSH(ofpacts));
8fb72d29 2830 init_stack_action(dst, ofpact_put_STACK_PUSH(ofpacts));
a20c96c6 2831 init_stack_action(&src, ofpact_put_STACK_POP(ofpacts));
8fb72d29 2832 init_stack_action(dst, ofpact_put_STACK_POP(ofpacts));
a20c96c6
BP
2833 } else {
2834 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2835 mf_subfield_from_expr_field(&src, &move->src);
8fb72d29 2836 mf_subfield_from_expr_field(dst, &move->dst);
a20c96c6 2837 }
3b7cb7e1 2838 } else {
5ee054fb 2839 struct expr_constant_set cs;
8fb72d29 2840 if (!parse_constant_set(&ctx, &cs)) {
5ee054fb
BP
2841 goto exit;
2842 }
2843
8fb72d29 2844 if (!type_check(&ctx, dst, &cs)) {
5ee054fb
BP
2845 goto exit_destroy_cs;
2846 }
2847 if (cs.in_curlies) {
8fb72d29 2848 expr_error(&ctx, "Assignments require a single value.");
5ee054fb
BP
2849 goto exit_destroy_cs;
2850 }
2851
2852 union expr_constant *c = cs.values;
2853 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
8fb72d29
BP
2854 sf->field = dst->symbol->field;
2855 if (dst->symbol->width) {
2856 mf_subvalue_shift(&c->value, dst->ofs);
5ee054fb
BP
2857 if (!c->masked) {
2858 memset(&c->mask, 0, sizeof c->mask);
8fb72d29 2859 bitwise_one(&c->mask, sizeof c->mask, dst->ofs, dst->n_bits);
5ee054fb 2860 } else {
8fb72d29 2861 mf_subvalue_shift(&c->mask, dst->ofs);
5ee054fb
BP
2862 }
2863
2864 memcpy(&sf->value,
2865 &c->value.u8[sizeof c->value - sf->field->n_bytes],
2866 sf->field->n_bytes);
2867 memcpy(&sf->mask,
2868 &c->mask.u8[sizeof c->mask - sf->field->n_bytes],
2869 sf->field->n_bytes);
2870 } else {
f1c16a85
BP
2871 uint32_t port;
2872 if (!lookup_port(aux, c->string, &port)) {
2873 port = 0;
2874 }
5ee054fb
BP
2875 bitwise_put(port, &sf->value,
2876 sf->field->n_bytes, 0, sf->field->n_bits);
b4970837 2877 bitwise_one(&sf->mask, sf->field->n_bytes, 0, sf->field->n_bits);
5ee054fb
BP
2878 }
2879
2880 exit_destroy_cs:
2881 expr_constant_set_destroy(&cs);
3b7cb7e1
BP
2882 }
2883
3b7cb7e1 2884exit:
8fb72d29
BP
2885 if (ctx.error) {
2886 expr_destroy(prereqs);
2887 prereqs = NULL;
2888 }
2889 *prereqsp = prereqs;
2890 return ctx.error;
3b7cb7e1
BP
2891}
2892
2893/* A helper for actions_parse(), to parse an OVN assignment action in the form
8fb72d29
BP
2894 * "field = value" or "field = field2" into 'ofpacts'. The caller must have
2895 * already parsed and skipped the left-hand side "field =" and pass in the
2896 * field as 'dst'. Other parameters and return value match those for
2897 * actions_parse(). */
2898char * OVS_WARN_UNUSED_RESULT
2899expr_parse_assignment(struct lexer *lexer, struct expr_field *dst,
2900 const struct shash *symtab,
f1c16a85
BP
2901 bool (*lookup_port)(const void *aux,
2902 const char *port_name,
2903 unsigned int *portp),
2904 const void *aux,
3b7cb7e1 2905 struct ofpbuf *ofpacts, struct expr **prereqsp)
8fb72d29
BP
2906{
2907 return parse_assignment(lexer, dst, symtab, false, lookup_port, aux,
2908 ofpacts, prereqsp);
2909}
2910
2911/* A helper for actions_parse(), to parse an OVN exchange action in the form
2912 * "field1 <-> field2" into 'ofpacts'. The caller must have already parsed and
2913 * skipped the left-hand side "field1 <->" and pass in 'field1'. Other
2914 * parameters and return value match those for actions_parse(). */
2915char * OVS_WARN_UNUSED_RESULT
2916expr_parse_exchange(struct lexer *lexer, struct expr_field *field,
2917 const struct shash *symtab,
2918 bool (*lookup_port)(const void *aux,
2919 const char *port_name,
2920 unsigned int *portp),
2921 const void *aux,
2922 struct ofpbuf *ofpacts, struct expr **prereqsp)
2923{
2924 return parse_assignment(lexer, field, symtab, true, lookup_port, aux,
2925 ofpacts, prereqsp);
2926}
2927
2928/* Parses a field or subfield from 'lexer' into 'field', obtaining field names
2929 * from 'symtab'. Returns NULL if successful, otherwise an error message owned
2930 * by the caller. */
2931char * OVS_WARN_UNUSED_RESULT
2932expr_parse_field(struct lexer *lexer, const struct shash *symtab,
2933 struct expr_field *field)
3b7cb7e1 2934{
1998e474 2935 struct expr_context ctx = { .lexer = lexer, .symtab = symtab };
8fb72d29
BP
2936 if (!parse_field(&ctx, field)) {
2937 memset(field, 0, sizeof *field);
3b7cb7e1 2938 }
3b7cb7e1
BP
2939 return ctx.error;
2940}
0bac7164 2941
8fb72d29
BP
2942/* Takes 'field', which was presumably parsed by expr_parse_field(), and
2943 * converts it into mf_subfield 'sf' and a set of prerequisites in '*prereqsp'.
2944 *
2945 * 'n_bits' specifies the number of bits that the field must have, and 0
2946 * indicates a string field; reports an error if 'field' has a different type
2947 * or width. If 'rw' is true, it is an error if 'field' is read-only. Uses
2948 * 'symtab 'for expanding references and 'lexer' for error reporting.
2949 *
2950 * Returns NULL if successful, otherwise an error message owned by the
2951 * caller. */
2952char * OVS_WARN_UNUSED_RESULT
2953expr_expand_field(struct lexer *lexer, const struct shash *symtab,
2954 const struct expr_field *orig_field, int n_bits, bool rw,
2955 struct mf_subfield *sf, struct expr **prereqsp)
0bac7164 2956{
1998e474 2957 struct expr_context ctx = { .lexer = lexer, .symtab = symtab };
0bac7164 2958 struct expr *prereqs = NULL;
0bac7164 2959
8fb72d29 2960 struct expr_field field = *orig_field;
0bac7164
BP
2961 if (!expand_symbol(&ctx, rw, &field, &prereqs)) {
2962 goto exit;
2963 }
8fb72d29 2964 ovs_assert(field.n_bits == orig_field->n_bits);
0bac7164
BP
2965
2966 if (n_bits != field.n_bits) {
2967 if (n_bits && field.n_bits) {
2968 expr_error(&ctx, "Cannot use %d-bit field %s[%d..%d] "
2969 "where %d-bit field is required.",
8fb72d29
BP
2970 orig_field->n_bits, orig_field->symbol->name,
2971 orig_field->ofs,
2972 orig_field->ofs + orig_field->n_bits - 1, n_bits);
0bac7164
BP
2973 } else if (n_bits) {
2974 expr_error(&ctx, "Cannot use string field %s where numeric "
2975 "field is required.",
8fb72d29 2976 orig_field->symbol->name);
0bac7164
BP
2977 } else {
2978 expr_error(&ctx, "Cannot use numeric field %s where string "
2979 "field is required.",
8fb72d29 2980 orig_field->symbol->name);
0bac7164
BP
2981 }
2982 }
2983
2984exit:
2985 if (!ctx.error) {
2986 mf_subfield_from_expr_field(&field, sf);
2987 *prereqsp = prereqs;
2988 } else {
2989 memset(sf, 0, sizeof *sf);
2990 expr_destroy(prereqs);
2991 *prereqsp = NULL;
2992 }
2993 return ctx.error;
2994}
42814145
NS
2995
2996char * OVS_WARN_UNUSED_RESULT
2997expr_parse_constant_set(struct lexer *lexer, const struct shash *symtab,
2998 struct expr_constant_set *cs)
2999{
3000 struct expr_context ctx = { .lexer = lexer, .symtab = symtab };
3001 parse_constant_set(&ctx, cs);
3002 return ctx.error;
3003}