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