]> git.proxmox.com Git - mirror_ovs.git/blame - ovn/lib/expr.c
json: New function json_string_escape().
[mirror_ovs.git] / ovn / lib / expr.c
CommitLineData
e0840f11
BP
1/*
2 * Copyright (c) 2015 Nicira, Inc.
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>
18#include "expr.h"
19#include "dynamic-string.h"
20#include "json.h"
21#include "lex.h"
22#include "match.h"
23#include "shash.h"
f386a8a7 24#include "simap.h"
e0840f11
BP
25#include "openvswitch/vlog.h"
26
27VLOG_DEFINE_THIS_MODULE(expr);
28\f
29/* Returns the name of measurement level 'level'. */
30const char *
31expr_level_to_string(enum expr_level level)
32{
33 switch (level) {
34 case EXPR_L_NOMINAL: return "nominal";
35 case EXPR_L_BOOLEAN: return "Boolean";
36 case EXPR_L_ORDINAL: return "ordinal";
37 default: OVS_NOT_REACHED();
38 }
39}
40\f
41/* Relational operators. */
42
43/* Returns a string form of relational operator 'relop'. */
44const char *
45expr_relop_to_string(enum expr_relop relop)
46{
47 switch (relop) {
48 case EXPR_R_EQ: return "==";
49 case EXPR_R_NE: return "!=";
50 case EXPR_R_LT: return "<";
51 case EXPR_R_LE: return "<=";
52 case EXPR_R_GT: return ">";
53 case EXPR_R_GE: return ">=";
54 default: OVS_NOT_REACHED();
55 }
56}
57
58bool
59expr_relop_from_token(enum lex_type type, enum expr_relop *relop)
60{
61 enum expr_relop r;
62
63 switch ((int) type) {
64 case LEX_T_EQ: r = EXPR_R_EQ; break;
65 case LEX_T_NE: r = EXPR_R_NE; break;
66 case LEX_T_LT: r = EXPR_R_LT; break;
67 case LEX_T_LE: r = EXPR_R_LE; break;
68 case LEX_T_GT: r = EXPR_R_GT; break;
69 case LEX_T_GE: r = EXPR_R_GE; break;
70 default: return false;
71 }
72
73 if (relop) {
74 *relop = r;
75 }
76 return true;
77}
78
79/* Returns the relational operator that 'relop' becomes if you turn the
80 * relation's operands around, e.g. EXPR_R_EQ does not change because "a == b"
81 * and "b == a" are equivalent, but EXPR_R_LE becomes EXPR_R_GE because "a <=
82 * b" is equivalent to "b >= a". */
83static enum expr_relop
84expr_relop_turn(enum expr_relop relop)
85{
86 switch (relop) {
87 case EXPR_R_EQ: return EXPR_R_EQ;
88 case EXPR_R_NE: return EXPR_R_NE;
89 case EXPR_R_LT: return EXPR_R_GT;
90 case EXPR_R_LE: return EXPR_R_GE;
91 case EXPR_R_GT: return EXPR_R_LT;
92 case EXPR_R_GE: return EXPR_R_LE;
93 default: OVS_NOT_REACHED();
94 }
95}
96
97/* Returns the relational operator that is the opposite of 'relop'. */
98static enum expr_relop
99expr_relop_invert(enum expr_relop relop)
100{
101 switch (relop) {
102 case EXPR_R_EQ: return EXPR_R_NE;
103 case EXPR_R_NE: return EXPR_R_EQ;
104 case EXPR_R_LT: return EXPR_R_GE;
105 case EXPR_R_LE: return EXPR_R_GT;
106 case EXPR_R_GT: return EXPR_R_LE;
107 case EXPR_R_GE: return EXPR_R_LT;
108 default: OVS_NOT_REACHED();
109 }
110}
111\f
112/* Constructing and manipulating expressions. */
113
114/* Creates and returns a logical AND or OR expression (according to 'type',
115 * which must be EXPR_T_AND or EXPR_T_OR) that initially has no
116 * sub-expressions. (To satisfy the invariants for expressions, the caller
117 * must add at least two sub-expressions whose types are different from
118 * 'type'.) */
119struct expr *
120expr_create_andor(enum expr_type type)
121{
122 struct expr *e = xmalloc(sizeof *e);
123 e->type = type;
124 list_init(&e->andor);
125 return e;
126}
127
128/* Returns a logical AND or OR expression (according to 'type', which must be
129 * EXPR_T_AND or EXPR_T_OR) whose sub-expressions are 'a' and 'b', with some
130 * flexibility:
131 *
132 * - If 'a' or 'b' is NULL, just returns the other one (which means that if
133 * that other one is not of the given 'type', then the returned
134 * expression is not either).
135 *
136 * - If 'a' or 'b', or both, have type 'type', then they are combined into
137 * a single node that satisfies the invariants for expressions. */
138struct expr *
139expr_combine(enum expr_type type, struct expr *a, struct expr *b)
140{
141 if (!a) {
142 return b;
143 } else if (!b) {
144 return a;
145 } else if (a->type == type) {
146 if (b->type == type) {
147 list_splice(&a->andor, b->andor.next, &b->andor);
148 free(b);
149 } else {
150 list_push_back(&a->andor, &b->node);
151 }
152 return a;
153 } else if (b->type == type) {
154 list_push_front(&b->andor, &a->node);
155 return b;
156 } else {
157 struct expr *e = expr_create_andor(type);
158 list_push_back(&e->andor, &a->node);
159 list_push_back(&e->andor, &b->node);
160 return e;
161 }
162}
163
164static void
165expr_insert_andor(struct expr *andor, struct expr *before, struct expr *new)
166{
167 if (new->type == andor->type) {
168 if (andor->type == EXPR_T_AND) {
169 /* Conjunction junction, what's your function? */
170 }
171 list_splice(&before->node, new->andor.next, &new->andor);
172 free(new);
173 } else {
174 list_insert(&before->node, &new->node);
175 }
176}
177
178/* Returns an EXPR_T_BOOLEAN expression with value 'b'. */
179struct expr *
180expr_create_boolean(bool b)
181{
182 struct expr *e = xmalloc(sizeof *e);
183 e->type = EXPR_T_BOOLEAN;
184 e->boolean = b;
185 return e;
186}
187
188static void
189expr_not(struct expr *expr)
190{
191 struct expr *sub;
192
193 switch (expr->type) {
194 case EXPR_T_CMP:
195 expr->cmp.relop = expr_relop_invert(expr->cmp.relop);
196 break;
197
198 case EXPR_T_AND:
199 case EXPR_T_OR:
200 LIST_FOR_EACH (sub, node, &expr->andor) {
201 expr_not(sub);
202 }
203 expr->type = expr->type == EXPR_T_AND ? EXPR_T_OR : EXPR_T_AND;
204 break;
205
206 case EXPR_T_BOOLEAN:
207 expr->boolean = !expr->boolean;
208 break;
209 default:
210 OVS_NOT_REACHED();
211 }
212}
213
214static struct expr *
215expr_fix_andor(struct expr *expr, bool short_circuit)
216{
217 struct expr *sub, *next;
218
219 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
220 if (sub->type == EXPR_T_BOOLEAN) {
221 if (sub->boolean == short_circuit) {
222 expr_destroy(expr);
223 return expr_create_boolean(short_circuit);
224 } else {
225 list_remove(&sub->node);
226 expr_destroy(sub);
227 }
228 }
229 }
230
231 if (list_is_short(&expr->andor)) {
232 if (list_is_empty(&expr->andor)) {
233 free(expr);
234 return expr_create_boolean(!short_circuit);
235 } else {
236 sub = expr_from_node(list_front(&expr->andor));
237 free(expr);
238 return sub;
239 }
240 } else {
241 return expr;
242 }
243}
244
245static struct expr *
246expr_fix(struct expr *expr)
247{
248 switch (expr->type) {
249 case EXPR_T_CMP:
250 return expr;
251
252 case EXPR_T_AND:
253 return expr_fix_andor(expr, false);
254
255 case EXPR_T_OR:
256 return expr_fix_andor(expr, true);
257
258 case EXPR_T_BOOLEAN:
259 return expr;
260
261 default:
262 OVS_NOT_REACHED();
263 }
264}
265\f
266/* Formatting. */
267
268static void
269find_bitwise_range(const union mf_subvalue *sv, int width,
270 int *startp, int *n_bitsp)
271{
272 unsigned int start = bitwise_scan(sv, sizeof *sv, true, 0, width);
273 if (start < width) {
274 unsigned int end = bitwise_scan(sv, sizeof *sv, false, start, width);
275 if (end >= width
276 || bitwise_scan(sv, sizeof *sv, true, end, width) >= width) {
277 *startp = start;
278 *n_bitsp = end - start;
279 return;
280 }
281 }
282 *startp = *n_bitsp = 0;
283}
284
e0840f11
BP
285static void
286expr_format_cmp(const struct expr *e, struct ds *s)
287{
288 /* The common case is numerical comparisons.
289 * Handle string comparisons as a special case. */
290 if (!e->cmp.symbol->width) {
291 ds_put_format(s, "%s %s ", e->cmp.symbol->name,
292 expr_relop_to_string(e->cmp.relop));
3b626771 293 json_string_escape(e->cmp.string, s);
e0840f11
BP
294 return;
295 }
296
297 int ofs, n;
298 find_bitwise_range(&e->cmp.mask, e->cmp.symbol->width, &ofs, &n);
299 if (n == 1 && (e->cmp.relop == EXPR_R_EQ || e->cmp.relop == EXPR_R_NE)) {
300 bool positive;
301
302 positive = bitwise_get_bit(&e->cmp.value, sizeof e->cmp.value, ofs);
303 positive ^= e->cmp.relop == EXPR_R_NE;
304 if (!positive) {
305 ds_put_char(s, '!');
306 }
307 ds_put_cstr(s, e->cmp.symbol->name);
308 if (e->cmp.symbol->width > 1) {
309 ds_put_format(s, "[%d]", ofs);
310 }
311 return;
312 }
313
314 ds_put_cstr(s, e->cmp.symbol->name);
315 if (n > 0 && n < e->cmp.symbol->width) {
316 if (n > 1) {
317 ds_put_format(s, "[%d..%d]", ofs, ofs + n - 1);
318 } else {
319 ds_put_format(s, "[%d]", ofs);
320 }
321 }
322
323 ds_put_format(s, " %s ", expr_relop_to_string(e->cmp.relop));
324
325 if (n) {
326 union mf_subvalue value;
327
328 memset(&value, 0, sizeof value);
329 bitwise_copy(&e->cmp.value, sizeof e->cmp.value, ofs,
330 &value, sizeof value, 0,
331 n);
332 mf_format_subvalue(&value, s);
333 } else {
334 mf_format_subvalue(&e->cmp.value, s);
335 ds_put_char(s, '/');
336 mf_format_subvalue(&e->cmp.mask, s);
337 }
338}
339
340static void
341expr_format_andor(const struct expr *e, const char *op, struct ds *s)
342{
343 struct expr *sub;
344 int i = 0;
345
346 LIST_FOR_EACH (sub, node, &e->andor) {
347 if (i++) {
348 ds_put_format(s, " %s ", op);
349 }
350
351 if (sub->type == EXPR_T_AND || sub->type == EXPR_T_OR) {
352 ds_put_char(s, '(');
353 expr_format(sub, s);
354 ds_put_char(s, ')');
355 } else {
356 expr_format(sub, s);
357 }
358 }
359}
360
361/* Appends a string form of 'e' to 's'. The string form is acceptable for
362 * parsing back into an equivalent expression. */
363void
364expr_format(const struct expr *e, struct ds *s)
365{
366 switch (e->type) {
367 case EXPR_T_CMP:
368 expr_format_cmp(e, s);
369 break;
370
371 case EXPR_T_AND:
372 expr_format_andor(e, "&&", s);
373 break;
374
375 case EXPR_T_OR:
376 expr_format_andor(e, "||", s);
377 break;
378
379 case EXPR_T_BOOLEAN:
380 ds_put_char(s, e->boolean ? '1' : '0');
381 break;
382 }
383}
384
385/* Prints a string form of 'e' on stdout, followed by a new-line. */
386void
387expr_print(const struct expr *e)
388{
389 struct ds output;
390
391 ds_init(&output);
392 expr_format(e, &output);
393 puts(ds_cstr(&output));
394 ds_destroy(&output);
395}
396\f
397/* Parsing. */
398
399/* Type of a "union expr_constant" or "struct expr_constant_set". */
400enum expr_constant_type {
401 EXPR_C_INTEGER,
402 EXPR_C_STRING
403};
404
405/* A string or integer constant (one must know which from context). */
406union expr_constant {
407 /* Integer constant.
408 *
409 * The width of a constant isn't always clear, e.g. if you write "1",
410 * there's no way to tell whether you mean for that to be a 1-bit constant
411 * or a 128-bit constant or somewhere in between. */
412 struct {
413 union mf_subvalue value;
414 union mf_subvalue mask; /* Only initialized if 'masked'. */
415 bool masked;
416
417 enum lex_format format; /* From the constant's lex_token. */
418 };
419
420 /* Null-terminated string constant. */
421 char *string;
422};
423
424/* A collection of "union expr_constant"s of the same type. */
425struct expr_constant_set {
426 union expr_constant *values; /* Constants. */
427 size_t n_values; /* Number of constants. */
428 enum expr_constant_type type; /* Type of the constants. */
429 bool in_curlies; /* Whether the constants were in {}. */
430};
431
432/* A reference to a symbol or a subfield of a symbol.
433 *
434 * For string fields, ofs and n_bits are 0. */
435struct expr_field {
436 const struct expr_symbol *symbol; /* The symbol. */
437 int ofs; /* Starting bit offset. */
438 int n_bits; /* Number of bits. */
439};
440
441/* Context maintained during expr_parse(). */
442struct expr_context {
443 struct lexer *lexer; /* Lexer for pulling more tokens. */
444 const struct shash *symtab; /* Symbol table. */
445 char *error; /* Error, if any, otherwise NULL. */
446 bool not; /* True inside odd number of NOT operators. */
447};
448
449struct expr *expr_parse__(struct expr_context *);
450static void expr_not(struct expr *);
451static void expr_constant_set_destroy(struct expr_constant_set *);
452static bool parse_field(struct expr_context *, struct expr_field *);
453
454static bool
455expr_error_handle_common(struct expr_context *ctx)
456{
457 if (ctx->error) {
458 /* Already have an error, suppress this one since the cascade seems
459 * unlikely to be useful. */
460 return true;
461 } else if (ctx->lexer->token.type == LEX_T_ERROR) {
462 /* The lexer signaled an error. Nothing at the expression level
463 * accepts an error token, so we'll inevitably end up here with some
464 * meaningless parse error. Report the lexical error instead. */
465 ctx->error = xstrdup(ctx->lexer->token.s);
466 return true;
467 } else {
468 return false;
469 }
470}
471
472static void OVS_PRINTF_FORMAT(2, 3)
473expr_error(struct expr_context *ctx, const char *message, ...)
474{
475 if (expr_error_handle_common(ctx)) {
476 return;
477 }
478
479 va_list args;
480 va_start(args, message);
481 ctx->error = xvasprintf(message, args);
482 va_end(args);
483}
484
485static void OVS_PRINTF_FORMAT(2, 3)
486expr_syntax_error(struct expr_context *ctx, const char *message, ...)
487{
488 if (expr_error_handle_common(ctx)) {
489 return;
490 }
491
492 struct ds s;
493
494 ds_init(&s);
495 ds_put_cstr(&s, "Syntax error ");
496 if (ctx->lexer->token.type == LEX_T_END) {
497 ds_put_cstr(&s, "at end of input ");
498 } else if (ctx->lexer->start) {
499 ds_put_format(&s, "at `%.*s' ",
500 (int) (ctx->lexer->input - ctx->lexer->start),
501 ctx->lexer->start);
502 }
503
504 va_list args;
505 va_start(args, message);
506 ds_put_format_valist(&s, message, args);
507 va_end(args);
508
509 ctx->error = ds_steal_cstr(&s);
510}
511
512static struct expr *
513make_cmp__(const struct expr_field *f, enum expr_relop r,
514 const union expr_constant *c)
515{
516 struct expr *e = xzalloc(sizeof *e);
517 e->type = EXPR_T_CMP;
518 e->cmp.symbol = f->symbol;
519 e->cmp.relop = r;
520 if (f->symbol->width) {
521 bitwise_copy(&c->value, sizeof c->value, 0,
522 &e->cmp.value, sizeof e->cmp.value, f->ofs,
523 f->n_bits);
524 if (c->masked) {
525 bitwise_copy(&c->mask, sizeof c->mask, 0,
526 &e->cmp.mask, sizeof e->cmp.mask, f->ofs,
527 f->n_bits);
528 } else {
529 bitwise_one(&e->cmp.mask, sizeof e->cmp.mask, f->ofs,
530 f->n_bits);
531 }
532 } else {
533 e->cmp.string = xstrdup(c->string);
534 }
535 return e;
536}
537
538/* Returns the minimum reasonable width for integer constant 'c'. */
539static int
540expr_constant_width(const union expr_constant *c)
541{
542 if (c->masked) {
543 return mf_subvalue_width(&c->mask);
544 }
545
546 switch (c->format) {
547 case LEX_F_DECIMAL:
548 case LEX_F_HEXADECIMAL:
549 return mf_subvalue_width(&c->value);
550
551 case LEX_F_IPV4:
552 return 32;
553
554 case LEX_F_IPV6:
555 return 128;
556
557 case LEX_F_ETHERNET:
558 return 48;
559
560 default:
561 OVS_NOT_REACHED();
562 }
563}
564
565static struct expr *
566make_cmp(struct expr_context *ctx,
567 const struct expr_field *f, enum expr_relop r,
568 struct expr_constant_set *cs)
569{
570 struct expr *e = NULL;
571
572 if (cs->type != (f->symbol->width ? EXPR_C_INTEGER : EXPR_C_STRING)) {
573 expr_error(ctx, "Can't compare %s field %s to %s constant.",
574 f->symbol->width ? "integer" : "string",
575 f->symbol->name,
576 cs->type == EXPR_C_INTEGER ? "integer" : "string");
577 goto exit;
578 }
579
580 if (r != EXPR_R_EQ && r != EXPR_R_NE) {
581 if (cs->in_curlies) {
582 expr_error(ctx, "Only == and != operators may be used "
583 "with value sets.");
584 goto exit;
585 }
586 if (f->symbol->level == EXPR_L_NOMINAL ||
587 f->symbol->level == EXPR_L_BOOLEAN) {
588 expr_error(ctx, "Only == and != operators may be used "
589 "with %s field %s.",
590 expr_level_to_string(f->symbol->level),
591 f->symbol->name);
592 goto exit;
593 }
594 if (cs->values[0].masked) {
595 expr_error(ctx, "Only == and != operators may be used with "
596 "masked constants. Consider using subfields instead "
597 "(e.g. eth.src[0..15] > 0x1111 in place of "
598 "eth.src > 00:00:00:00:11:11/00:00:00:00:ff:ff).");
599 goto exit;
600 }
601 }
602
603 if (f->symbol->level == EXPR_L_NOMINAL) {
604 if (f->symbol->expansion) {
605 for (size_t i = 0; i < cs->n_values; i++) {
606 const union mf_subvalue *value = &cs->values[i].value;
607 bool positive = (value->integer & htonll(1)) != 0;
608 positive ^= r == EXPR_R_NE;
609 positive ^= ctx->not;
610 if (!positive) {
611 const char *name = f->symbol->name;
612 expr_error(ctx, "Nominal predicate %s may only be tested "
613 "positively, e.g. `%s' or `%s == 1' but not "
614 "`!%s' or `%s == 0'.",
615 name, name, name, name, name);
616 goto exit;
617 }
618 }
619 } else if (r != (ctx->not ? EXPR_R_NE : EXPR_R_EQ)) {
620 expr_error(ctx, "Nominal field %s may only be tested for "
621 "equality (taking enclosing `!' operators into "
622 "account).", f->symbol->name);
623 goto exit;
624 }
625 }
626
627 if (f->symbol->width) {
628 for (size_t i = 0; i < cs->n_values; i++) {
629 int w = expr_constant_width(&cs->values[i]);
630 if (w > f->symbol->width) {
631 expr_error(ctx, "Cannot compare %d-bit constant against "
632 "%d-bit field %s.",
633 w, f->symbol->width, f->symbol->name);
634 goto exit;
635 }
636 }
637 }
638
639 e = make_cmp__(f, r, &cs->values[0]);
640 for (size_t i = 1; i < cs->n_values; i++) {
641 e = expr_combine(r == EXPR_R_EQ ? EXPR_T_OR : EXPR_T_AND,
642 e, make_cmp__(f, r, &cs->values[i]));
643 }
644exit:
645 expr_constant_set_destroy(cs);
646 return e;
647}
648
649static bool
650expr_get_int(struct expr_context *ctx, int *value)
651{
652 if (ctx->lexer->token.type == LEX_T_INTEGER
653 && ctx->lexer->token.format == LEX_F_DECIMAL
654 && ntohll(ctx->lexer->token.value.integer) <= INT_MAX) {
655 *value = ntohll(ctx->lexer->token.value.integer);
656 lexer_get(ctx->lexer);
657 return true;
658 } else {
659 expr_syntax_error(ctx, "expecting small integer.");
660 return false;
661 }
662}
663
664static bool
665parse_field(struct expr_context *ctx, struct expr_field *f)
666{
667 const struct expr_symbol *symbol;
668
669 if (ctx->lexer->token.type != LEX_T_ID) {
670 expr_syntax_error(ctx, "expecting field name.");
671 return false;
672 }
673
674 symbol = shash_find_data(ctx->symtab, ctx->lexer->token.s);
675 if (!symbol) {
676 expr_syntax_error(ctx, "expecting field name.");
677 return false;
678 }
679 lexer_get(ctx->lexer);
680
681 f->symbol = symbol;
682 if (lexer_match(ctx->lexer, LEX_T_LSQUARE)) {
683 int low, high;
684
685 if (!symbol->width) {
686 expr_error(ctx, "Cannot select subfield of string field %s.",
687 symbol->name);
688 return false;
689 }
690
691 if (!expr_get_int(ctx, &low)) {
692 return false;
693 }
694 if (lexer_match(ctx->lexer, LEX_T_ELLIPSIS)) {
695 if (!expr_get_int(ctx, &high)) {
696 return false;
697 }
698 } else {
699 high = low;
700 }
701
702 if (!lexer_match(ctx->lexer, LEX_T_RSQUARE)) {
703 expr_syntax_error(ctx, "expecting `]'.");
704 return false;
705 }
706
707 if (low > high) {
708 expr_error(ctx, "Invalid bit range %d to %d.", low, high);
709 return false;
710 } else if (high >= symbol->width) {
711 expr_error(ctx, "Cannot select bits %d to %d of %d-bit field %s.",
712 low, high, symbol->width, symbol->name);
713 return false;
714 } else if (symbol->level == EXPR_L_NOMINAL
715 && (low != 0 || high != symbol->width - 1)) {
716 expr_error(ctx, "Cannot select subfield of nominal field %s.",
717 symbol->name);
718 return false;
719 }
720
721 f->ofs = low;
722 f->n_bits = high - low + 1;
723 } else {
724 f->ofs = 0;
725 f->n_bits = symbol->width;
726 }
727
728 return true;
729}
730
731static bool
732parse_relop(struct expr_context *ctx, enum expr_relop *relop)
733{
734 if (expr_relop_from_token(ctx->lexer->token.type, relop)) {
735 lexer_get(ctx->lexer);
736 return true;
737 } else {
738 expr_syntax_error(ctx, "expecting relational operator.");
739 return false;
740 }
741}
742
743static bool
744assign_constant_set_type(struct expr_context *ctx,
745 struct expr_constant_set *cs,
746 enum expr_constant_type type)
747{
748 if (!cs->n_values || cs->type == type) {
749 cs->type = type;
750 return true;
751 } else {
752 expr_syntax_error(ctx, "expecting %s.",
753 cs->type == EXPR_C_INTEGER ? "integer" : "string");
754 return false;
755 }
756}
757
758static bool
759parse_constant(struct expr_context *ctx, struct expr_constant_set *cs,
760 size_t *allocated_values)
761{
762 if (cs->n_values >= *allocated_values) {
763 cs->values = x2nrealloc(cs->values, allocated_values,
764 sizeof *cs->values);
765 }
766
767 if (ctx->lexer->token.type == LEX_T_STRING) {
768 if (!assign_constant_set_type(ctx, cs, EXPR_C_STRING)) {
769 return false;
770 }
771 cs->values[cs->n_values++].string = xstrdup(ctx->lexer->token.s);
772 lexer_get(ctx->lexer);
773 return true;
774 } else if (ctx->lexer->token.type == LEX_T_INTEGER ||
775 ctx->lexer->token.type == LEX_T_MASKED_INTEGER) {
776 if (!assign_constant_set_type(ctx, cs, EXPR_C_INTEGER)) {
777 return false;
778 }
779
780 union expr_constant *c = &cs->values[cs->n_values++];
781 c->value = ctx->lexer->token.value;
782 c->format = ctx->lexer->token.format;
783 c->masked = ctx->lexer->token.type == LEX_T_MASKED_INTEGER;
784 if (c->masked) {
785 c->mask = ctx->lexer->token.mask;
786 }
787 lexer_get(ctx->lexer);
788 return true;
789 } else {
790 expr_syntax_error(ctx, "expecting constant.");
791 return false;
792 }
793}
794
795/* Parses a single or {}-enclosed set of integer or string constants into 'cs',
796 * which the caller need not have initialized. Returns true on success, in
797 * which case the caller owns 'cs', false on failure, in which case 'cs' is
798 * indeterminate. */
799static bool
800parse_constant_set(struct expr_context *ctx, struct expr_constant_set *cs)
801{
802 size_t allocated_values = 0;
803 bool ok;
804
805 memset(cs, 0, sizeof *cs);
806 if (lexer_match(ctx->lexer, LEX_T_LCURLY)) {
807 ok = true;
808 cs->in_curlies = true;
809 do {
810 if (!parse_constant(ctx, cs, &allocated_values)) {
811 ok = false;
812 break;
813 }
814 lexer_match(ctx->lexer, LEX_T_COMMA);
815 } while (!lexer_match(ctx->lexer, LEX_T_RCURLY));
816 } else {
817 ok = parse_constant(ctx, cs, &allocated_values);
818 }
819 if (!ok) {
820 expr_constant_set_destroy(cs);
821 }
822 return ok;
823}
824
825static void
826expr_constant_set_destroy(struct expr_constant_set *cs)
827{
828 if (cs) {
829 if (cs->type == EXPR_C_STRING) {
830 for (size_t i = 0; i < cs->n_values; i++) {
831 free(cs->values[i].string);
832 }
833 }
834 free(cs->values);
835 }
836}
837
838static struct expr *
839expr_parse_primary(struct expr_context *ctx, bool *atomic)
840{
841 *atomic = false;
842 if (lexer_match(ctx->lexer, LEX_T_LPAREN)) {
843 struct expr *e = expr_parse__(ctx);
844 if (!lexer_match(ctx->lexer, LEX_T_RPAREN)) {
845 expr_destroy(e);
846 expr_syntax_error(ctx, "expecting `)'.");
847 return NULL;
848 }
849 *atomic = true;
850 return e;
851 }
852
853 if (ctx->lexer->token.type == LEX_T_ID) {
854 struct expr_field f;
855 enum expr_relop r;
856 struct expr_constant_set c;
857
858 if (!parse_field(ctx, &f)) {
859 return NULL;
860 }
861
862 if (!expr_relop_from_token(ctx->lexer->token.type, &r)) {
863 if (f.n_bits > 1 && !ctx->not) {
864 expr_error(ctx, "Explicit `!= 0' is required for inequality "
865 "test of multibit field against 0.");
866 return NULL;
867 }
868
869 *atomic = true;
870
871 union expr_constant *cst = xzalloc(sizeof *cst);
872 cst->format = LEX_F_HEXADECIMAL;
873 cst->masked = false;
874
875 c.type = EXPR_C_INTEGER;
876 c.values = cst;
877 c.n_values = 1;
878 c.in_curlies = false;
879 return make_cmp(ctx, &f, EXPR_R_NE, &c);
880 } else if (parse_relop(ctx, &r) && parse_constant_set(ctx, &c)) {
881 return make_cmp(ctx, &f, r, &c);
882 } else {
883 return NULL;
884 }
885 } else {
886 struct expr_constant_set c1;
887 if (!parse_constant_set(ctx, &c1)) {
888 return NULL;
889 }
890
891 if (!expr_relop_from_token(ctx->lexer->token.type, NULL)
892 && c1.n_values == 1
893 && c1.type == EXPR_C_INTEGER
894 && c1.values[0].format == LEX_F_DECIMAL
895 && !c1.values[0].masked
896 && !c1.in_curlies) {
897 uint64_t x = ntohll(c1.values[0].value.integer);
898 if (x <= 1) {
899 *atomic = true;
900 expr_constant_set_destroy(&c1);
901 return expr_create_boolean(x);
902 }
903 }
904
905 enum expr_relop r1;
906 struct expr_field f;
907 if (!parse_relop(ctx, &r1) || !parse_field(ctx, &f)) {
908 expr_constant_set_destroy(&c1);
909 return NULL;
910 }
911
912 if (!expr_relop_from_token(ctx->lexer->token.type, NULL)) {
913 return make_cmp(ctx, &f, expr_relop_turn(r1), &c1);
914 }
915
916 enum expr_relop r2;
917 struct expr_constant_set c2;
918 if (!parse_relop(ctx, &r2) || !parse_constant_set(ctx, &c2)) {
919 expr_constant_set_destroy(&c1);
920 return NULL;
921 } else {
922 /* Reject "1 == field == 2", "1 < field > 2", and so on. */
923 if (!(((r1 == EXPR_R_LT || r1 == EXPR_R_LE) &&
924 (r2 == EXPR_R_LT || r2 == EXPR_R_LE)) ||
925 ((r1 == EXPR_R_GT || r1 == EXPR_R_GE) &&
926 (r2 == EXPR_R_GT || r2 == EXPR_R_GE)))) {
927 expr_error(ctx, "Range expressions must have the form "
928 "`x < field < y' or `x > field > y', with each "
929 "`<' optionally replaced by `<=' or `>' by `>=').");
930 expr_constant_set_destroy(&c1);
931 expr_constant_set_destroy(&c2);
932 return NULL;
933 }
934
935 struct expr *e1 = make_cmp(ctx, &f, expr_relop_turn(r1), &c1);
936 struct expr *e2 = make_cmp(ctx, &f, r2, &c2);
937 if (ctx->error) {
938 expr_destroy(e1);
939 expr_destroy(e2);
940 return NULL;
941 }
942 return expr_combine(EXPR_T_AND, e1, e2);
943 }
944 }
945}
946
947static struct expr *
948expr_parse_not(struct expr_context *ctx)
949{
950 bool atomic;
951
952 if (lexer_match(ctx->lexer, LEX_T_LOG_NOT)) {
953 ctx->not = !ctx->not;
954 struct expr *expr = expr_parse_primary(ctx, &atomic);
955 ctx->not = !ctx->not;
956
957 if (expr) {
958 if (!atomic) {
959 expr_error(ctx, "Missing parentheses around operand of !.");
960 expr_destroy(expr);
961 return NULL;
962 }
963 expr_not(expr);
964 }
965 return expr;
966 } else {
967 return expr_parse_primary(ctx, &atomic);
968 }
969}
970
971struct expr *
972expr_parse__(struct expr_context *ctx)
973{
974 struct expr *e = expr_parse_not(ctx);
975 if (!e) {
976 return NULL;
977 }
978
979 enum lex_type lex_type = ctx->lexer->token.type;
980 if (lex_type == LEX_T_LOG_AND || lex_type == LEX_T_LOG_OR) {
981 enum expr_type expr_type
982 = lex_type == LEX_T_LOG_AND ? EXPR_T_AND : EXPR_T_OR;
983
984 lexer_get(ctx->lexer);
985 do {
986 struct expr *e2 = expr_parse_not(ctx);
987 if (!e2) {
988 expr_destroy(e);
989 return NULL;
990 }
991 e = expr_combine(expr_type, e, e2);
992 } while (lexer_match(ctx->lexer, lex_type));
993 if (ctx->lexer->token.type == LEX_T_LOG_AND
994 || ctx->lexer->token.type == LEX_T_LOG_OR) {
995 expr_destroy(e);
996 expr_error(ctx,
997 "&& and || must be parenthesized when used together.");
998 return NULL;
999 }
1000 }
1001 return e;
1002}
1003
1004/* Parses an expression using the symbols in 'symtab' from 'lexer'. If
1005 * successful, returns the new expression and sets '*errorp' to NULL. On
1006 * failure, returns NULL and sets '*errorp' to an explanatory error message.
1007 * The caller must eventually free the returned expression (with
1008 * expr_destroy()) or error (with free()). */
1009struct expr *
1010expr_parse(struct lexer *lexer, const struct shash *symtab, char **errorp)
1011{
1012 struct expr_context ctx;
1013
1014 ctx.lexer = lexer;
1015 ctx.symtab = symtab;
1016 ctx.error = NULL;
1017 ctx.not = false;
1018
1019 struct expr *e = expr_parse__(&ctx);
1020 *errorp = ctx.error;
1021 ovs_assert((ctx.error != NULL) != (e != NULL));
1022 return e;
1023}
1024
1025/* Like expr_parse(), but the expression is taken from 's'. */
1026struct expr *
1027expr_parse_string(const char *s, const struct shash *symtab, char **errorp)
1028{
1029 struct lexer lexer;
1030 struct expr *expr;
1031
1032 lexer_init(&lexer, s);
1033 lexer_get(&lexer);
1034 expr = expr_parse(&lexer, symtab, errorp);
1035 if (!errorp && lexer.token.type != LEX_T_END) {
1036 *errorp = xstrdup("Extra tokens at end of input.");
1037 expr_destroy(expr);
1038 expr = NULL;
1039 }
1040 lexer_destroy(&lexer);
1041
1042 return expr;
1043}
1044\f
1045static struct expr_symbol *
1046add_symbol(struct shash *symtab, const char *name, int width,
1047 const char *prereqs, enum expr_level level,
1048 bool must_crossproduct)
1049{
1050 struct expr_symbol *symbol = xzalloc(sizeof *symbol);
1051 symbol->name = xstrdup(name);
1052 symbol->prereqs = prereqs && prereqs[0] ? xstrdup(prereqs) : NULL;
1053 symbol->width = width;
1054 symbol->level = level;
1055 symbol->must_crossproduct = must_crossproduct;
1056 shash_add_assert(symtab, symbol->name, symbol);
1057 return symbol;
1058}
1059
1060/* Adds field 'id' to symbol table 'symtab' under the given 'name'. Whenever
1061 * 'name' is referenced, expression annotation (see expr_annotate()) will
1062 * ensure that 'prereqs' are also true. If 'must_crossproduct' is true, then
1063 * conversion to flows will never attempt to use the field as a conjunctive
1064 * match dimension (see "Crossproducting" in the large comment on struct
1065 * expr_symbol in expr.h for an example).
1066 *
1067 * A given field 'id' must only be used for a single symbol in a symbol table.
1068 * Use subfields to duplicate or subset a field (you can even make a subfield
1069 * include all the bits of the "parent" field if you like). */
1070struct expr_symbol *
1071expr_symtab_add_field(struct shash *symtab, const char *name,
1072 enum mf_field_id id, const char *prereqs,
1073 bool must_crossproduct)
1074{
1075 const struct mf_field *field = mf_from_id(id);
1076 struct expr_symbol *symbol;
1077
1078 symbol = add_symbol(symtab, name, field->n_bits, prereqs,
1079 (field->maskable == MFM_FULLY
1080 ? EXPR_L_ORDINAL
1081 : EXPR_L_NOMINAL),
1082 must_crossproduct);
1083 symbol->field = field;
1084 return symbol;
1085}
1086
1087static bool
1088parse_field_from_string(const char *s, const struct shash *symtab,
1089 struct expr_field *field, char **errorp)
1090{
1091 struct lexer lexer;
1092 lexer_init(&lexer, s);
1093 lexer_get(&lexer);
1094
1095 struct expr_context ctx;
1096 ctx.lexer = &lexer;
1097 ctx.symtab = symtab;
1098 ctx.error = NULL;
1099 ctx.not = false;
1100
1101 bool ok = parse_field(&ctx, field);
1102 if (!ok) {
1103 *errorp = ctx.error;
1104 } else if (lexer.token.type != LEX_T_END) {
1105 *errorp = xstrdup("Extra tokens at end of input.");
1106 ok = false;
1107 }
1108
1109 lexer_destroy(&lexer);
1110
1111 return ok;
1112}
1113
1114/* Adds 'name' as a subfield of a larger field in 'symtab'. Whenever
1115 * 'name' is referenced, expression annotation (see expr_annotate()) will
1116 * ensure that 'prereqs' are also true.
1117 *
1118 * 'subfield' must describe the subfield as a string, e.g. "vlan.tci[0..11]"
1119 * for the low 12 bits of a larger field named "vlan.tci". */
1120struct expr_symbol *
1121expr_symtab_add_subfield(struct shash *symtab, const char *name,
1122 const char *prereqs, const char *subfield)
1123{
1124 struct expr_symbol *symbol;
1125 struct expr_field f;
1126 char *error;
1127
1128 if (!parse_field_from_string(subfield, symtab, &f, &error)) {
1129 VLOG_WARN("%s: error parsing %s subfield (%s)", subfield, name, error);
1130 free(error);
1131 return NULL;
1132 }
1133
1134 enum expr_level level = f.symbol->level;
1135 if (level != EXPR_L_ORDINAL) {
1136 VLOG_WARN("can't define %s as subfield of %s field %s",
1137 name, expr_level_to_string(level), f.symbol->name);
1138 }
1139
1140 symbol = add_symbol(symtab, name, f.n_bits, prereqs, level, false);
1141 symbol->expansion = xstrdup(subfield);
1142 return symbol;
1143}
1144
1145/* Adds a string-valued symbol named 'name' to 'symtab' with the specified
1146 * 'prereqs'. */
1147struct expr_symbol *
1148expr_symtab_add_string(struct shash *symtab, const char *name,
f386a8a7 1149 enum mf_field_id id, const char *prereqs)
e0840f11 1150{
f386a8a7
BP
1151 const struct mf_field *field = mf_from_id(id);
1152 struct expr_symbol *symbol;
1153
1154 symbol = add_symbol(symtab, name, 0, prereqs, EXPR_L_NOMINAL, false);
1155 symbol->field = field;
1156 return symbol;
e0840f11
BP
1157}
1158
1159static enum expr_level
1160expr_get_level(const struct expr *expr)
1161{
1162 const struct expr *sub;
1163 enum expr_level level = EXPR_L_ORDINAL;
1164
1165 switch (expr->type) {
1166 case EXPR_T_CMP:
1167 return (expr->cmp.symbol->level == EXPR_L_NOMINAL
1168 ? EXPR_L_NOMINAL
1169 : EXPR_L_BOOLEAN);
1170
1171 case EXPR_T_AND:
1172 case EXPR_T_OR:
1173 LIST_FOR_EACH (sub, node, &expr->andor) {
1174 enum expr_level sub_level = expr_get_level(sub);
1175 level = MIN(level, sub_level);
1176 }
1177 return level;
1178
1179 case EXPR_T_BOOLEAN:
1180 return EXPR_L_BOOLEAN;
1181
1182 default:
1183 OVS_NOT_REACHED();
1184 }
1185}
1186
1187static enum expr_level
1188expr_parse_level(const char *s, const struct shash *symtab, char **errorp)
1189{
1190 struct expr *expr = expr_parse_string(s, symtab, errorp);
1191 enum expr_level level = expr ? expr_get_level(expr) : EXPR_L_NOMINAL;
1192 expr_destroy(expr);
1193 return level;
1194}
1195
1196/* Adds a predicate symbol, whose value is the given Boolean 'expression',
1197 * named 'name' to 'symtab'. For example, "ip4 && ip4.proto == 1" might be an
1198 * appropriate predicate named "tcp4". */
1199struct expr_symbol *
1200expr_symtab_add_predicate(struct shash *symtab, const char *name,
1201 const char *expansion)
1202{
1203 struct expr_symbol *symbol;
1204 enum expr_level level;
1205 char *error;
1206
1207 level = expr_parse_level(expansion, symtab, &error);
1208 if (error) {
1209 VLOG_WARN("%s: error parsing %s expansion (%s)",
1210 expansion, name, error);
1211 free(error);
1212 return NULL;
1213 }
1214
1215 symbol = add_symbol(symtab, name, 1, NULL, level, false);
1216 symbol->expansion = xstrdup(expansion);
1217 return symbol;
1218}
1219
1220/* Destroys 'symtab' and all of its symbols. */
1221void
1222expr_symtab_destroy(struct shash *symtab)
1223{
1224 struct shash_node *node, *next;
1225
1226 SHASH_FOR_EACH_SAFE (node, next, symtab) {
1227 struct expr_symbol *symbol = node->data;
1228
1229 shash_delete(symtab, node);
1230 free(symbol->name);
1231 free(symbol->prereqs);
1232 free(symbol->expansion);
1233 free(symbol);
1234 }
1235}
1236\f
1237/* Cloning. */
1238
1239static struct expr *
1240expr_clone_cmp(struct expr *expr)
1241{
1242 struct expr *new = xmemdup(expr, sizeof *expr);
1243 if (!new->cmp.symbol->width) {
1244 new->cmp.string = xstrdup(new->cmp.string);
1245 }
1246 return new;
1247}
1248
1249static struct expr *
1250expr_clone_andor(struct expr *expr)
1251{
1252 struct expr *new = expr_create_andor(expr->type);
1253 struct expr *sub;
1254
1255 LIST_FOR_EACH (sub, node, &expr->andor) {
1256 struct expr *new_sub = expr_clone(sub);
1257 list_push_back(&new->andor, &new_sub->node);
1258 }
1259 return new;
1260}
1261
1262/* Returns a clone of 'expr'. This is a "deep copy": neither the returned
1263 * expression nor any of its substructure will be shared with 'expr'. */
1264struct expr *
1265expr_clone(struct expr *expr)
1266{
1267 switch (expr->type) {
1268 case EXPR_T_CMP:
1269 return expr_clone_cmp(expr);
1270
1271 case EXPR_T_AND:
1272 case EXPR_T_OR:
1273 return expr_clone_andor(expr);
1274
1275 case EXPR_T_BOOLEAN:
1276 return expr_create_boolean(expr->boolean);
1277 }
1278 OVS_NOT_REACHED();
1279}
1280\f
1281/* Destroys 'expr' and all of the sub-expressions it references. */
1282void
1283expr_destroy(struct expr *expr)
1284{
1285 if (!expr) {
1286 return;
1287 }
1288
1289 struct expr *sub, *next;
1290
1291 switch (expr->type) {
1292 case EXPR_T_CMP:
1293 if (!expr->cmp.symbol->width) {
1294 free(expr->cmp.string);
1295 }
1296 break;
1297
1298 case EXPR_T_AND:
1299 case EXPR_T_OR:
1300 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
1301 list_remove(&sub->node);
1302 expr_destroy(sub);
1303 }
1304 break;
1305
1306 case EXPR_T_BOOLEAN:
1307 break;
1308 }
1309 free(expr);
1310}
1311\f
1312/* Annotation. */
1313
1314/* An element in a linked list of symbols.
1315 *
1316 * Used to detect when a symbol is being expanded recursively, to allow
1317 * flagging an error. */
1318struct annotation_nesting {
1319 struct ovs_list node;
1320 const struct expr_symbol *symbol;
1321};
1322
1323struct expr *expr_annotate__(struct expr *, const struct shash *symtab,
1324 struct ovs_list *nesting, char **errorp);
1325
1326static struct expr *
1327parse_and_annotate(const char *s, const struct shash *symtab,
1328 struct ovs_list *nesting, char **errorp)
1329{
1330 char *error;
1331 struct expr *expr;
1332
1333 expr = expr_parse_string(s, symtab, &error);
1334 if (expr) {
1335 expr = expr_annotate__(expr, symtab, nesting, &error);
1336 }
1337 if (!expr) {
1338 *errorp = xasprintf("Error parsing expression `%s' encountered as "
1339 "prerequisite or predicate of initial expression: "
1340 "%s", s, error);
1341 free(error);
1342 }
1343 return expr;
1344}
1345
1346static struct expr *
1347expr_annotate_cmp(struct expr *expr, const struct shash *symtab,
1348 struct ovs_list *nesting, char **errorp)
1349{
1350 const struct expr_symbol *symbol = expr->cmp.symbol;
1351 const struct annotation_nesting *iter;
1352 LIST_FOR_EACH (iter, node, nesting) {
1353 if (iter->symbol == symbol) {
1354 *errorp = xasprintf("Recursive expansion of symbol `%s'.",
1355 symbol->name);
1356 expr_destroy(expr);
1357 return NULL;
1358 }
1359 }
1360
1361 struct annotation_nesting an;
1362 an.symbol = symbol;
1363 list_push_back(nesting, &an.node);
1364
1365 struct expr *prereqs = NULL;
1366 if (symbol->prereqs) {
1367 prereqs = parse_and_annotate(symbol->prereqs, symtab, nesting, errorp);
1368 if (!prereqs) {
1369 goto error;
1370 }
1371 }
1372
1373 if (symbol->expansion) {
1374 if (symbol->level == EXPR_L_ORDINAL) {
1375 struct expr_field field;
1376
1377 if (!parse_field_from_string(symbol->expansion, symtab,
1378 &field, errorp)) {
1379 goto error;
1380 }
1381
1382 expr->cmp.symbol = field.symbol;
1383 mf_subvalue_shift(&expr->cmp.value, field.ofs);
1384 mf_subvalue_shift(&expr->cmp.mask, field.ofs);
1385 } else {
1386 struct expr *expansion;
1387
1388 expansion = parse_and_annotate(symbol->expansion, symtab,
1389 nesting, errorp);
1390 if (!expansion) {
1391 goto error;
1392 }
1393
1394 bool positive = (expr->cmp.value.integer & htonll(1)) != 0;
1395 positive ^= expr->cmp.relop == EXPR_R_NE;
1396 if (!positive) {
1397 expr_not(expansion);
1398 }
1399
1400 expr_destroy(expr);
1401 expr = expansion;
1402 }
1403 }
1404
1405 list_remove(&an.node);
1406 return prereqs ? expr_combine(EXPR_T_AND, expr, prereqs) : expr;
1407
1408error:
1409 expr_destroy(expr);
1410 expr_destroy(prereqs);
1411 list_remove(&an.node);
1412 return NULL;
1413}
1414
1415struct expr *
1416expr_annotate__(struct expr *expr, const struct shash *symtab,
1417 struct ovs_list *nesting, char **errorp)
1418{
1419 switch (expr->type) {
1420 case EXPR_T_CMP:
1421 return expr_annotate_cmp(expr, symtab, nesting, errorp);
1422
1423 case EXPR_T_AND:
1424 case EXPR_T_OR: {
1425 struct expr *sub, *next;
1426
1427 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
1428 list_remove(&sub->node);
1429 struct expr *new_sub = expr_annotate__(sub, symtab,
1430 nesting, errorp);
1431 if (!new_sub) {
1432 expr_destroy(expr);
1433 return NULL;
1434 }
1435 expr_insert_andor(expr, next, new_sub);
1436 }
1437 *errorp = NULL;
1438 return expr;
1439 }
1440
1441 case EXPR_T_BOOLEAN:
1442 *errorp = NULL;
1443 return expr;
1444
1445 default:
1446 OVS_NOT_REACHED();
1447 }
1448}
1449
1450/* "Annotates" 'expr', which does the following:
1451 *
1452 * - Applies prerequisites, by locating each comparison operator whose
1453 * field has a prerequisite and adding a logical AND against those
1454 * prerequisites.
1455 *
1456 * - Expands references to subfield symbols, by replacing them by
1457 * references to their underlying field symbols (suitably shifted).
1458 *
1459 * - Expands references to predicate symbols, by replacing them by the
1460 * expressions that they expand to.
1461 *
1462 * In each case, annotation occurs recursively as necessary. */
1463struct expr *
1464expr_annotate(struct expr *expr, const struct shash *symtab, char **errorp)
1465{
1466 struct ovs_list nesting = OVS_LIST_INITIALIZER(&nesting);
1467 return expr_annotate__(expr, symtab, &nesting, errorp);
1468}
1469\f
1470static struct expr *
1471expr_simplify_ne(struct expr *expr)
1472{
1473 struct expr *new = NULL;
1474 const union mf_subvalue *value = &expr->cmp.value;
1475 const union mf_subvalue *mask = &expr->cmp.mask;
1476 int w = expr->cmp.symbol->width;
1477 int i;
1478
1479 for (i = 0; (i = bitwise_scan(mask, sizeof *mask, true, i, w)) < w; i++) {
1480 struct expr *e;
1481
1482 e = xzalloc(sizeof *e);
1483 e->type = EXPR_T_CMP;
1484 e->cmp.symbol = expr->cmp.symbol;
1485 e->cmp.relop = EXPR_R_EQ;
1486 bitwise_put_bit(&e->cmp.value, sizeof e->cmp.value, i,
1487 !bitwise_get_bit(value, sizeof *value, i));
1488 bitwise_put1(&e->cmp.mask, sizeof e->cmp.mask, i);
1489
1490 new = expr_combine(EXPR_T_OR, new, e);
1491 }
1492 ovs_assert(new);
1493
1494 expr_destroy(expr);
1495
1496 return new;
1497}
1498
1499static struct expr *
1500expr_simplify_relational(struct expr *expr)
1501{
1502 const union mf_subvalue *value = &expr->cmp.value;
1503 int start, n_bits, end;
1504
1505 find_bitwise_range(&expr->cmp.mask, expr->cmp.symbol->width,
1506 &start, &n_bits);
1507 ovs_assert(n_bits > 0);
1508 end = start + n_bits;
1509
1510 struct expr *new;
1511 if (expr->cmp.relop == EXPR_R_LE || expr->cmp.relop == EXPR_R_GE) {
1512 new = xmemdup(expr, sizeof *expr);
1513 new->cmp.relop = EXPR_R_EQ;
1514 } else {
1515 new = NULL;
1516 }
1517
1518 bool b = expr->cmp.relop == EXPR_R_LT || expr->cmp.relop == EXPR_R_LE;
1519 for (int z = bitwise_scan(value, sizeof *value, b, start, end);
1520 z < end;
1521 z = bitwise_scan(value, sizeof *value, b, z + 1, end)) {
1522 struct expr *e;
1523
1524 e = xmemdup(expr, sizeof *expr);
1525 e->cmp.relop = EXPR_R_EQ;
1526 bitwise_toggle_bit(&e->cmp.value, sizeof e->cmp.value, z);
1527 bitwise_zero(&e->cmp.value, sizeof e->cmp.value, start, z - start);
1528 bitwise_zero(&e->cmp.mask, sizeof e->cmp.mask, start, z - start);
1529 new = expr_combine(EXPR_T_OR, new, e);
1530 }
1531 expr_destroy(expr);
1532 return new ? new : expr_create_boolean(false);
1533}
1534
1535/* Takes ownership of 'expr' and returns an equivalent expression whose
1536 * EXPR_T_CMP nodes use only tests for equality (EXPR_R_EQ). */
1537struct expr *
1538expr_simplify(struct expr *expr)
1539{
1540 struct expr *sub, *next;
1541
1542 switch (expr->type) {
1543 case EXPR_T_CMP:
1544 return (expr->cmp.relop == EXPR_R_EQ || !expr->cmp.symbol->width ? expr
1545 : expr->cmp.relop == EXPR_R_NE ? expr_simplify_ne(expr)
1546 : expr_simplify_relational(expr));
1547
1548 case EXPR_T_AND:
1549 case EXPR_T_OR:
1550 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
1551 list_remove(&sub->node);
1552 expr_insert_andor(expr, next, expr_simplify(sub));
1553 }
1554 return expr_fix(expr);
1555
1556 case EXPR_T_BOOLEAN:
1557 return expr;
1558 }
1559 OVS_NOT_REACHED();
1560}
1561\f
1562static const struct expr_symbol *
1563expr_is_cmp(const struct expr *expr)
1564{
1565 switch (expr->type) {
1566 case EXPR_T_CMP:
1567 return expr->cmp.symbol;
1568
1569 case EXPR_T_AND:
1570 case EXPR_T_OR: {
1571 const struct expr_symbol *prev = NULL;
1572 struct expr *sub;
1573
1574 LIST_FOR_EACH (sub, node, &expr->andor) {
1575 const struct expr_symbol *symbol = expr_is_cmp(sub);
1576 if (!symbol || (prev && symbol != prev)) {
1577 return NULL;
1578 }
1579 prev = symbol;
1580 }
1581 return prev;
1582 }
1583
1584 case EXPR_T_BOOLEAN:
1585 return NULL;
1586
1587 default:
1588 OVS_NOT_REACHED();
1589 }
1590}
1591
1592struct expr_sort {
1593 struct expr *expr;
1594 const struct expr_symbol *relop;
1595 enum expr_type type;
1596};
1597
1598static int
1599compare_expr_sort(const void *a_, const void *b_)
1600{
1601 const struct expr_sort *a = a_;
1602 const struct expr_sort *b = b_;
1603
1604 if (a->type != b->type) {
1605 return a->type < b->type ? -1 : 1;
1606 } else if (a->relop) {
1607 int cmp = strcmp(a->relop->name, b->relop->name);
1608 if (cmp) {
1609 return cmp;
1610 }
1611
1612 enum expr_type a_type = a->expr->type;
1613 enum expr_type b_type = a->expr->type;
1614 return a_type < b_type ? -1 : a_type > b_type;
1615 } else if (a->type == EXPR_T_AND || a->type == EXPR_T_OR) {
1616 size_t a_len = list_size(&a->expr->andor);
1617 size_t b_len = list_size(&b->expr->andor);
1618 return a_len < b_len ? -1 : a_len > b_len;
1619 } else {
1620 return 0;
1621 }
1622}
1623
1624static struct expr *crush_cmps(struct expr *, const struct expr_symbol *);
1625
1626static struct expr *
1627crush_and(struct expr *expr, const struct expr_symbol *symbol)
1628{
1629 ovs_assert(!list_is_short(&expr->andor));
1630
1631 union mf_subvalue value, mask;
1632 memset(&value, 0, sizeof value);
1633 memset(&mask, 0, sizeof mask);
1634
1635 struct expr *sub, *next = NULL;
1636 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
1637 list_remove(&sub->node);
1638 struct expr *new = crush_cmps(sub, symbol);
1639 switch (new->type) {
1640 case EXPR_T_CMP:
1641 if (!mf_subvalue_intersect(&value, &mask,
1642 &new->cmp.value, &new->cmp.mask,
1643 &value, &mask)) {
1644 expr_destroy(new);
1645 expr_destroy(expr);
1646 return expr_create_boolean(false);
1647 }
1648 expr_destroy(new);
1649 break;
1650 case EXPR_T_AND:
1651 OVS_NOT_REACHED();
1652 case EXPR_T_OR:
1653 list_insert(&next->node, &new->node);
1654 break;
1655 case EXPR_T_BOOLEAN:
1656 if (!new->boolean) {
1657 expr_destroy(expr);
1658 return new;
1659 }
1660 free(new);
1661 break;
1662 }
1663 }
1664 if (list_is_empty(&expr->andor)) {
1665 if (is_all_zeros(&mask, sizeof mask)) {
1666 expr_destroy(expr);
1667 return expr_create_boolean(true);
1668 } else {
1669 struct expr *cmp;
1670 cmp = xmalloc(sizeof *cmp);
1671 cmp->type = EXPR_T_CMP;
1672 cmp->cmp.symbol = symbol;
1673 cmp->cmp.relop = EXPR_R_EQ;
1674 cmp->cmp.value = value;
1675 cmp->cmp.mask = mask;
1676 expr_destroy(expr);
1677 return cmp;
1678 }
1679 } else if (list_is_short(&expr->andor)) {
1680 /* Transform "a && (b || c || d)" into "ab || ac || ad" where "ab" is
1681 * computed as "a && b", etc. */
1682 struct expr *disjuncts = expr_from_node(list_pop_front(&expr->andor));
1683 struct expr *or;
1684
1685 or = xmalloc(sizeof *or);
1686 or->type = EXPR_T_OR;
1687 list_init(&or->andor);
1688
1689 ovs_assert(disjuncts->type == EXPR_T_OR);
1690 LIST_FOR_EACH_SAFE (sub, next, node, &disjuncts->andor) {
1691 ovs_assert(sub->type == EXPR_T_CMP);
1692 list_remove(&sub->node);
1693 if (mf_subvalue_intersect(&value, &mask,
1694 &sub->cmp.value, &sub->cmp.mask,
1695 &sub->cmp.value, &sub->cmp.mask)) {
1696 list_push_back(&or->andor, &sub->node);
1697 } else {
1698 free(sub);
1699 }
1700 }
1701 free(disjuncts);
1702 free(expr);
1703 if (list_is_empty(&or->andor)) {
1704 free(or);
1705 return expr_create_boolean(false);
1706 } else if (list_is_short(&or->andor)) {
1707 struct expr *cmp = expr_from_node(list_pop_front(&or->andor));
1708 free(or);
1709 return cmp;
1710 } else {
1711 return or;
1712 }
1713 } else {
1714 /* Transform "x && (a0 || a1) && (b0 || b1) && ..." into
1715 * "(xa0b0 || xa0b1 || xa1b0 || xa1b1) && ...". */
1716 struct expr *as = expr_from_node(list_pop_front(&expr->andor));
1717 struct expr *bs = expr_from_node(list_pop_front(&expr->andor));
1718 struct expr *new = NULL;
1719 struct expr *or;
1720
1721 or = xmalloc(sizeof *or);
1722 or->type = EXPR_T_OR;
1723 list_init(&or->andor);
1724
1725 struct expr *a;
1726 LIST_FOR_EACH (a, node, &as->andor) {
1727 union mf_subvalue a_value, a_mask;
1728
1729 ovs_assert(a->type == EXPR_T_CMP);
1730 if (!mf_subvalue_intersect(&value, &mask,
1731 &a->cmp.value, &a->cmp.mask,
1732 &a_value, &a_mask)) {
1733 continue;
1734 }
1735
1736 struct expr *b;
1737 LIST_FOR_EACH (b, node, &bs->andor) {
1738 ovs_assert(b->type == EXPR_T_CMP);
1739 if (!new) {
1740 new = xmalloc(sizeof *new);
1741 new->type = EXPR_T_CMP;
1742 new->cmp.symbol = symbol;
1743 new->cmp.relop = EXPR_R_EQ;
1744 }
1745 if (mf_subvalue_intersect(&a_value, &a_mask,
1746 &b->cmp.value, &b->cmp.mask,
1747 &new->cmp.value, &new->cmp.mask)) {
1748 list_push_back(&or->andor, &new->node);
1749 new = NULL;
1750 }
1751 }
1752 }
1753 expr_destroy(as);
1754 expr_destroy(bs);
1755 free(new);
1756
1757 if (list_is_empty(&or->andor)) {
1758 expr_destroy(expr);
1759 free(or);
1760 return expr_create_boolean(false);
1761 } else if (list_is_short(&or->andor)) {
1762 struct expr *cmp = expr_from_node(list_pop_front(&or->andor));
1763 free(or);
1764 if (list_is_empty(&expr->andor)) {
1765 expr_destroy(expr);
1766 return crush_cmps(cmp, symbol);
1767 } else {
1768 return crush_cmps(expr_combine(EXPR_T_AND, cmp, expr), symbol);
1769 }
1770 } else if (!list_is_empty(&expr->andor)) {
1771 struct expr *e = expr_combine(EXPR_T_AND, or, expr);
1772 ovs_assert(!list_is_short(&e->andor));
1773 return crush_cmps(e, symbol);
1774 } else {
1775 expr_destroy(expr);
1776 return crush_cmps(or, symbol);
1777 }
1778 }
1779}
1780
1781static int
1782compare_expr(const void *a_, const void *b_)
1783{
1784 const struct expr *const *ap = a_;
1785 const struct expr *const *bp = b_;
1786 const struct expr *a = *ap;
1787 const struct expr *b = *bp;
1788 int d = memcmp(&a->cmp.value, &b->cmp.value, sizeof a->cmp.value);
1789 if (!d) {
1790 d = memcmp(&a->cmp.mask, &b->cmp.mask, sizeof a->cmp.mask);
1791 }
1792 return d;
1793}
1794
1795static struct expr *
1796crush_or(struct expr *expr, const struct expr_symbol *symbol)
1797{
1798 struct expr *sub, *next = NULL;
1799
1800 /* First, crush all the subexpressions. That might eliminate the
1801 * OR-expression entirely; if so, return the result. */
1802 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
1803 list_remove(&sub->node);
1804 expr_insert_andor(expr, next, crush_cmps(sub, symbol));
1805 }
1806 expr = expr_fix(expr);
1807 if (expr->type != EXPR_T_OR) {
1808 return expr;
1809 }
1810
1811 /* Eliminate duplicates by sorting the subexpressions. */
1812 size_t n = list_size(&expr->andor);
1813 struct expr **subs = xmalloc(n * sizeof *subs);
1814
1815 size_t i = 0;
1816 LIST_FOR_EACH (sub, node, &expr->andor) {
1817 subs[i++] = sub;
1818 }
1819 ovs_assert(i == n);
1820
1821 qsort(subs, n, sizeof *subs, compare_expr);
1822
1823 list_init(&expr->andor);
1824 list_push_back(&expr->andor, &subs[0]->node);
1825 for (i = 1; i < n; i++) {
1826 struct expr *a = expr_from_node(list_back(&expr->andor));
1827 struct expr *b = subs[i];
1828 if (memcmp(&a->cmp.value, &b->cmp.value, sizeof a->cmp.value)
1829 || memcmp(&a->cmp.mask, &b->cmp.mask, sizeof a->cmp.mask)) {
1830 list_push_back(&expr->andor, &b->node);
1831 } else {
1832 free(b);
1833 }
1834 }
1835 free(subs);
1836 return expr_fix(expr);
1837}
1838
1839/* Converts 'expr', which must be a cmp in the sense determined by
1840 * expr_is_cmp(). Returns a cmp, a disjunction of cmps, or a boolean. */
1841static struct expr *
1842crush_cmps(struct expr *expr, const struct expr_symbol *symbol)
1843{
1844 switch (expr->type) {
1845 case EXPR_T_OR:
1846 return crush_or(expr, symbol);
1847
1848 case EXPR_T_AND:
1849 return crush_and(expr, symbol);
1850
1851 case EXPR_T_CMP:
1852 return expr;
1853
1854 case EXPR_T_BOOLEAN:
1855 return expr;
1856
1857 default:
1858 OVS_NOT_REACHED();
1859 }
1860}
1861
1862static struct expr *
1863expr_sort(struct expr *expr)
1864{
1865 size_t n = list_size(&expr->andor);
1866 struct expr_sort *subs = xmalloc(n * sizeof *subs);
1867 struct expr *sub;
1868 size_t i;
1869
1870 i = 0;
1871 LIST_FOR_EACH (sub, node, &expr->andor) {
1872 subs[i].expr = sub;
1873 subs[i].relop = expr_is_cmp(sub);
1874 subs[i].type = subs[i].relop ? EXPR_T_CMP : sub->type;
1875 i++;
1876 }
1877 ovs_assert(i == n);
1878
1879 qsort(subs, n, sizeof *subs, compare_expr_sort);
1880
1881 list_init(&expr->andor);
1882 for (int i = 0; i < n; ) {
1883 if (subs[i].relop) {
1884 int j;
1885 for (j = i + 1; j < n; j++) {
1886 if (subs[i].relop != subs[j].relop) {
1887 break;
1888 }
1889 }
1890
1891 struct expr *crushed;
1892 if (j == i + 1) {
1893 crushed = crush_cmps(subs[i].expr, subs[i].relop);
1894 } else {
1895 struct expr *combined = subs[i].expr;
1896 for (int k = i + 1; k < j; k++) {
1897 combined = expr_combine(EXPR_T_AND, combined,
1898 subs[k].expr);
1899 }
1900 ovs_assert(!list_is_short(&combined->andor));
1901 crushed = crush_cmps(combined, subs[i].relop);
1902 }
1903 if (crushed->type == EXPR_T_BOOLEAN) {
1904 if (!crushed->boolean) {
1905 for (int k = j; k < n; k++) {
1906 expr_destroy(subs[k].expr);
1907 }
1908 expr_destroy(expr);
1909 expr = crushed;
1910 break;
1911 } else {
1912 free(crushed);
1913 }
1914 } else {
1915 expr = expr_combine(EXPR_T_AND, expr, crushed);
1916 }
1917 i = j;
1918 } else {
1919 expr = expr_combine(EXPR_T_AND, expr, subs[i++].expr);
1920 }
1921 }
1922 free(subs);
1923
1924 return expr;
1925}
1926
1927static struct expr *expr_normalize_or(struct expr *expr);
1928
1929/* Returns 'expr', which is an AND, reduced to OR(AND(clause)) where
1930 * a clause is a cmp or a disjunction of cmps on a single field. */
1931static struct expr *
1932expr_normalize_and(struct expr *expr)
1933{
1934 ovs_assert(expr->type == EXPR_T_AND);
1935
1936 expr = expr_sort(expr);
1937 if (expr->type != EXPR_T_AND) {
1938 ovs_assert(expr->type == EXPR_T_BOOLEAN);
1939 return expr;
1940 }
1941
1942 struct expr *a, *b;
1943 LIST_FOR_EACH_SAFE (a, b, node, &expr->andor) {
1944 if (&b->node == &expr->andor
1945 || a->type != EXPR_T_CMP || b->type != EXPR_T_CMP) {
1946 } else if (a->cmp.symbol != b->cmp.symbol) {
1947 continue;
1948 } else if (mf_subvalue_intersect(&a->cmp.value, &a->cmp.mask,
1949 &b->cmp.value, &b->cmp.mask,
1950 &b->cmp.value, &b->cmp.mask)) {
1951 list_remove(&a->node);
1952 expr_destroy(a);
1953 } else {
1954 expr_destroy(expr);
1955 return expr_create_boolean(false);
1956 }
1957 }
1958 if (list_is_short(&expr->andor)) {
1959 struct expr *sub = expr_from_node(list_front(&expr->andor));
1960 free(expr);
1961 return sub;
1962 }
1963
1964 struct expr *sub;
1965 LIST_FOR_EACH (sub, node, &expr->andor) {
1966 if (sub->type == EXPR_T_CMP) {
1967 continue;
1968 }
1969
1970 ovs_assert(sub->type == EXPR_T_OR);
1971 const struct expr_symbol *symbol = expr_is_cmp(sub);
1972 if (!symbol || symbol->must_crossproduct) {
1973 struct expr *or = expr_create_andor(EXPR_T_OR);
1974 struct expr *k;
1975
1976 LIST_FOR_EACH (k, node, &sub->andor) {
1977 struct expr *and = expr_create_andor(EXPR_T_AND);
1978 struct expr *m;
1979
1980 LIST_FOR_EACH (m, node, &expr->andor) {
1981 struct expr *term = m == sub ? k : m;
1982 if (term->type == EXPR_T_AND) {
1983 struct expr *p;
1984
1985 LIST_FOR_EACH (p, node, &term->andor) {
1986 struct expr *new = expr_clone(p);
1987 list_push_back(&and->andor, &new->node);
1988 }
1989 } else {
1990 struct expr *new = expr_clone(term);
1991 list_push_back(&and->andor, &new->node);
1992 }
1993 }
1994 list_push_back(&or->andor, &and->node);
1995 }
1996 expr_destroy(expr);
1997 return expr_normalize_or(or);
1998 }
1999 }
2000 return expr;
2001}
2002
2003static struct expr *
2004expr_normalize_or(struct expr *expr)
2005{
2006 struct expr *sub, *next;
2007
2008 LIST_FOR_EACH_SAFE (sub, next, node, &expr->andor) {
2009 if (sub->type == EXPR_T_AND) {
2010 list_remove(&sub->node);
2011
2012 struct expr *new = expr_normalize_and(sub);
2013 if (new->type == EXPR_T_BOOLEAN) {
2014 if (new->boolean) {
2015 expr_destroy(expr);
2016 return new;
2017 }
2018 free(new);
2019 } else {
2020 expr_insert_andor(expr, next, new);
2021 }
2022 } else {
2023 ovs_assert(sub->type == EXPR_T_CMP);
2024 }
2025 }
2026 if (list_is_empty(&expr->andor)) {
2027 free(expr);
2028 return expr_create_boolean(false);
2029 }
2030 if (list_is_short(&expr->andor)) {
2031 struct expr *sub = expr_from_node(list_pop_front(&expr->andor));
2032 free(expr);
2033 return sub;
2034 }
2035
2036 return expr;
2037}
2038
2039/* Takes ownership of 'expr', which is either a constant "true" or "false" or
2040 * an expression in terms of only relationals, AND, and OR. Returns either a
2041 * constant "true" or "false" or 'expr' reduced to OR(AND(clause)) where a
2042 * clause is a cmp or a disjunction of cmps on a single field. This form is
2043 * significant because it is a form that can be directly converted to OpenFlow
2044 * flows with the Open vSwitch "conjunctive match" extension.
2045 *
2046 * 'expr' must already have been simplified, with expr_simplify(). */
2047struct expr *
2048expr_normalize(struct expr *expr)
2049{
2050 switch (expr->type) {
2051 case EXPR_T_CMP:
2052 return expr;
2053
2054 case EXPR_T_AND:
2055 return expr_normalize_and(expr);
2056
2057 case EXPR_T_OR:
2058 return expr_normalize_or(expr);
2059
2060 case EXPR_T_BOOLEAN:
2061 return expr;
2062 }
2063 OVS_NOT_REACHED();
2064}
2065\f
2066/* Creates, initializes, and returns a new 'struct expr_match'. If 'm' is
2067 * nonnull then it is copied into the new expr_match, otherwise the new
2068 * expr_match's 'match' member is initialized to a catch-all match for the
2069 * caller to refine in-place.
2070 *
2071 * If 'conj_id' is nonzero, adds one conjunction based on 'conj_id', 'clause',
2072 * and 'n_clauses' to the returned 'struct expr_match', otherwise the
2073 * expr_match will not have any conjunctions.
2074 *
2075 * The caller should use expr_match_add() to add the expr_match to a hash table
2076 * after it is finalized. */
2077static struct expr_match *
2078expr_match_new(const struct match *m, uint8_t clause, uint8_t n_clauses,
2079 uint32_t conj_id)
2080{
2081 struct expr_match *match = xmalloc(sizeof *match);
2082 if (m) {
2083 match->match = *m;
2084 } else {
2085 match_init_catchall(&match->match);
2086 }
2087 if (conj_id) {
2088 match->conjunctions = xmalloc(sizeof *match->conjunctions);
2089 match->conjunctions[0].id = conj_id;
2090 match->conjunctions[0].clause = clause;
2091 match->conjunctions[0].n_clauses = n_clauses;
2092 match->n = 1;
2093 match->allocated = 1;
2094 } else {
2095 match->conjunctions = NULL;
2096 match->n = 0;
2097 match->allocated = 0;
2098 }
2099 return match;
2100}
2101
2102/* Adds 'match' to hash table 'matches', which becomes the new owner of
2103 * 'match'.
2104 *
2105 * This might actually destroy 'match' because it gets merged together with
2106 * some existing conjunction.*/
2107static void
2108expr_match_add(struct hmap *matches, struct expr_match *match)
2109{
2110 uint32_t hash = match_hash(&match->match, 0);
2111 struct expr_match *m;
2112
2113 HMAP_FOR_EACH_WITH_HASH (m, hmap_node, hash, matches) {
2114 if (match_equal(&m->match, &match->match)) {
2115 if (!m->n || !match->n) {
2116 free(m->conjunctions);
2117 m->conjunctions = NULL;
2118 m->n = 0;
2119 m->allocated = 0;
2120 } else {
2121 ovs_assert(match->n == 1);
2122 if (m->n >= m->allocated) {
2123 m->conjunctions = x2nrealloc(m->conjunctions,
2124 &m->allocated,
2125 sizeof *m->conjunctions);
2126 }
2127 m->conjunctions[m->n++] = match->conjunctions[0];
2128 }
2129 free(match->conjunctions);
2130 free(match);
2131 return;
2132 }
2133 }
2134
2135 hmap_insert(matches, &match->hmap_node, hash);
2136}
2137
f386a8a7
BP
2138static bool
2139constrain_match(const struct expr *expr, const struct simap *ports,
2140 struct match *m)
e0840f11
BP
2141{
2142 ovs_assert(expr->type == EXPR_T_CMP);
f386a8a7
BP
2143 if (expr->cmp.symbol->width) {
2144 mf_mask_subfield(expr->cmp.symbol->field, &expr->cmp.value,
2145 &expr->cmp.mask, m);
2146 } else {
2147 const struct simap_node *node;
2148 node = ports ? simap_find(ports, expr->cmp.string) : NULL;
2149 if (!node) {
2150 return false;
2151 }
2152
2153 struct mf_subfield sf;
2154 sf.field = expr->cmp.symbol->field;
2155 sf.ofs = 0;
2156 sf.n_bits = expr->cmp.symbol->field->n_bits;
2157
2158 union mf_subvalue x;
2159 memset(&x, 0, sizeof x);
2160 x.integer = htonll(node->data);
2161
2162 mf_write_subfield(&sf, &x, m);
2163 }
2164 return true;
e0840f11
BP
2165}
2166
f386a8a7
BP
2167static bool
2168add_disjunction(const struct expr *or, const struct simap *ports,
2169 struct match *m, uint8_t clause, uint8_t n_clauses,
2170 uint32_t conj_id, struct hmap *matches)
e0840f11
BP
2171{
2172 struct expr *sub;
f386a8a7 2173 int n = 0;
e0840f11
BP
2174
2175 ovs_assert(or->type == EXPR_T_OR);
2176 LIST_FOR_EACH (sub, node, &or->andor) {
2177 struct expr_match *match = expr_match_new(m, clause, n_clauses,
2178 conj_id);
f386a8a7
BP
2179 if (constrain_match(sub, ports, &match->match)) {
2180 expr_match_add(matches, match);
2181 n++;
2182 } else {
2183 free(match->conjunctions);
2184 free(match);
2185 }
e0840f11 2186 }
f386a8a7
BP
2187
2188 /* If n == 1, then this didn't really need to be a disjunction. Oh well,
2189 * that shouldn't happen much. */
2190 return n > 0;
e0840f11
BP
2191}
2192
2193static void
f386a8a7
BP
2194add_conjunction(const struct expr *and, const struct simap *ports,
2195 uint32_t *n_conjsp, struct hmap *matches)
e0840f11
BP
2196{
2197 struct match match;
2198 int n_clauses = 0;
2199 struct expr *sub;
2200
2201 match_init_catchall(&match);
2202
2203 ovs_assert(and->type == EXPR_T_AND);
2204 LIST_FOR_EACH (sub, node, &and->andor) {
2205 switch (sub->type) {
2206 case EXPR_T_CMP:
f386a8a7
BP
2207 if (!constrain_match(sub, ports, &match)) {
2208 return;
2209 }
e0840f11
BP
2210 break;
2211 case EXPR_T_OR:
2212 n_clauses++;
2213 break;
2214 case EXPR_T_AND:
2215 case EXPR_T_BOOLEAN:
2216 OVS_NOT_REACHED();
2217 }
2218 }
2219
2220 if (!n_clauses) {
2221 expr_match_add(matches, expr_match_new(&match, 0, 0, 0));
2222 } else if (n_clauses == 1) {
2223 LIST_FOR_EACH (sub, node, &and->andor) {
2224 if (sub->type == EXPR_T_OR) {
f386a8a7 2225 add_disjunction(sub, ports, &match, 0, 0, 0, matches);
e0840f11
BP
2226 }
2227 }
2228 } else {
2229 int clause = 0;
2230 (*n_conjsp)++;
2231 LIST_FOR_EACH (sub, node, &and->andor) {
2232 if (sub->type == EXPR_T_OR) {
f386a8a7
BP
2233 if (!add_disjunction(sub, ports, &match, clause++,
2234 n_clauses, *n_conjsp, matches)) {
2235 /* This clause can't ever match, so we might as well skip
2236 * adding the other clauses--the overall disjunctive flow
2237 * can't ever match. Ideally we would also back out all of
2238 * the clauses we already added, but that seems like a lot
2239 * of trouble for a case that might never occur in
2240 * practice. */
2241 return;
2242 }
e0840f11
BP
2243 }
2244 }
2245 }
2246}
2247
2248static void
f386a8a7
BP
2249add_cmp_flow(const struct expr *cmp, const struct simap *ports,
2250 struct hmap *matches)
e0840f11
BP
2251{
2252 struct expr_match *m = expr_match_new(NULL, 0, 0, 0);
f386a8a7
BP
2253 if (constrain_match(cmp, ports, &m->match)) {
2254 expr_match_add(matches, m);
2255 } else {
2256 free(m);
2257 }
e0840f11
BP
2258}
2259
2260/* Converts 'expr', which must be in the form returned by expr_normalize(), to
2261 * a collection of Open vSwitch flows in 'matches', which this function
f386a8a7
BP
2262 * initializes to an hmap of "struct expr_match" structures. Returns the
2263 * number of conjunctive match IDs consumed by 'matches', which uses
2264 * conjunctive match IDs beginning with 0; the caller must offset or remap them
2265 * into the desired range as necessary.
2266 *
2267 * 'ports' must be a map from strings (presumably names of ports) to integers.
2268 * Any comparisons against string fields in 'expr' are translated into integers
2269 * through this map. A comparison against a string that is not in 'ports' acts
2270 * like a Boolean "false"; that is, it will always fail to match. For a simple
2271 * expression, this means that the overall expression always fails to match,
2272 * but an expression with a disjunction on the string field might still match
2273 * on other port names.
2274 *
2275 * (This treatment of string fields might be too simplistic in general, but it
2276 * seems reasonable for now when string fields are used only for ports.) */
e0840f11 2277uint32_t
f386a8a7
BP
2278expr_to_matches(const struct expr *expr, const struct simap *ports,
2279 struct hmap *matches)
e0840f11
BP
2280{
2281 uint32_t n_conjs = 0;
2282
2283 hmap_init(matches);
2284 switch (expr->type) {
2285 case EXPR_T_CMP:
f386a8a7 2286 add_cmp_flow(expr, ports, matches);
e0840f11
BP
2287 break;
2288
2289 case EXPR_T_AND:
f386a8a7 2290 add_conjunction(expr, ports, &n_conjs, matches);
e0840f11
BP
2291 break;
2292
2293 case EXPR_T_OR:
2294 if (expr_is_cmp(expr)) {
2295 struct expr *sub;
2296
2297 LIST_FOR_EACH (sub, node, &expr->andor) {
f386a8a7 2298 add_cmp_flow(sub, ports, matches);
e0840f11
BP
2299 }
2300 } else {
2301 struct expr *sub;
2302
2303 LIST_FOR_EACH (sub, node, &expr->andor) {
2304 if (sub->type == EXPR_T_AND) {
f386a8a7 2305 add_conjunction(sub, ports, &n_conjs, matches);
e0840f11 2306 } else {
f386a8a7 2307 add_cmp_flow(sub, ports, matches);
e0840f11
BP
2308 }
2309 }
2310 }
2311 break;
2312
2313 case EXPR_T_BOOLEAN:
2314 if (expr->boolean) {
2315 struct expr_match *m = expr_match_new(NULL, 0, 0, 0);
2316 expr_match_add(matches, m);
2317 } else {
2318 /* No match. */
2319 }
2320 break;
2321 }
2322 return n_conjs;
2323}
f386a8a7
BP
2324
2325/* Destroys all of the 'struct expr_match'es in 'matches', as well as the
2326 * 'matches' hmap itself. */
2327void
2328expr_matches_destroy(struct hmap *matches)
2329{
2330 struct expr_match *m, *n;
2331
2332 HMAP_FOR_EACH_SAFE (m, n, hmap_node, matches) {
2333 hmap_remove(matches, &m->hmap_node);
2334 free(m->conjunctions);
2335 free(m);
2336 }
2337 hmap_destroy(matches);
2338}
2339
2340/* Prints a representation of the 'struct expr_match'es in 'matches' to
2341 * 'stream'. */
2342void
2343expr_matches_print(const struct hmap *matches, FILE *stream)
2344{
2345 if (hmap_is_empty(matches)) {
2346 fputs("(no flows)\n", stream);
2347 return;
2348 }
2349
2350 const struct expr_match *m;
2351 HMAP_FOR_EACH (m, hmap_node, matches) {
2352 char *s = match_to_string(&m->match, OFP_DEFAULT_PRIORITY);
2353 fputs(s, stream);
2354 free(s);
2355
2356 if (m->n) {
2357 for (int i = 0; i < m->n; i++) {
2358 const struct cls_conjunction *c = &m->conjunctions[i];
2359 fprintf(stream, "%c conjunction(%"PRIu32", %d/%d)",
2360 i == 0 ? ':' : ',', c->id, c->clause, c->n_clauses);
2361 }
2362 }
2363 putc('\n', stream);
2364 }
2365}
e0840f11
BP
2366\f
2367/* Returns true if 'expr' honors the invariants for expressions (see the large
2368 * comment above "struct expr" in expr.h), false otherwise. */
2369bool
2370expr_honors_invariants(const struct expr *expr)
2371{
2372 const struct expr *sub;
2373
2374 switch (expr->type) {
2375 case EXPR_T_CMP:
2376 if (expr->cmp.symbol->width) {
2377 for (int i = 0; i < ARRAY_SIZE(expr->cmp.value.be64); i++) {
2378 if (expr->cmp.value.be64[i] & ~expr->cmp.mask.be64[i]) {
2379 return false;
2380 }
2381 }
2382 }
2383 return true;
2384
2385 case EXPR_T_AND:
2386 case EXPR_T_OR:
2387 if (list_is_short(&expr->andor)) {
2388 return false;
2389 }
2390 LIST_FOR_EACH (sub, node, &expr->andor) {
2391 if (sub->type == expr->type || !expr_honors_invariants(sub)) {
2392 return false;
2393 }
2394 }
2395 return true;
2396
2397 case EXPR_T_BOOLEAN:
2398 return true;
2399
2400 default:
2401 OVS_NOT_REACHED();
2402 }
2403}
2404
2405static bool
2406expr_is_normalized_and(const struct expr *expr)
2407{
2408 /* XXX should also check that no symbol is repeated. */
2409 const struct expr *sub;
2410
2411 LIST_FOR_EACH (sub, node, &expr->andor) {
2412 if (!expr_is_cmp(sub)) {
2413 return false;
2414 }
2415 }
2416 return true;
2417}
2418
2419/* Returns true if 'expr' is in the form returned by expr_normalize(), false
2420 * otherwise. */
2421bool
2422expr_is_normalized(const struct expr *expr)
2423{
2424 switch (expr->type) {
2425 case EXPR_T_CMP:
2426 return true;
2427
2428 case EXPR_T_AND:
2429 return expr_is_normalized_and(expr);
2430
2431 case EXPR_T_OR:
2432 if (!expr_is_cmp(expr)) {
2433 const struct expr *sub;
2434
2435 LIST_FOR_EACH (sub, node, &expr->andor) {
2436 if (!expr_is_cmp(sub) && !expr_is_normalized_and(sub)) {
2437 return false;
2438 }
2439 }
2440 }
2441 return true;
2442
2443 case EXPR_T_BOOLEAN:
2444 return true;
2445
2446 default:
2447 OVS_NOT_REACHED();
2448 }
2449}