actions: Factor out new helper function add_prerequisite().
[cascardo/ovs.git] / ovn / lib / actions.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 "actions.h"
19 #include <stdarg.h>
20 #include <stdbool.h>
21 #include "compiler.h"
22 #include "dynamic-string.h"
23 #include "expr.h"
24 #include "lex.h"
25 #include "logical-fields.h"
26 #include "ofp-actions.h"
27 #include "ofpbuf.h"
28 #include "simap.h"
29
30 /* Context maintained during actions_parse(). */
31 struct action_context {
32 /* Input. */
33
34     struct lexer *lexer;        /* Lexer for pulling more tokens. */
35     const struct simap *ports;  /* Map from port name to number. */
36     const struct shash *symtab; /* Symbol table. */
37
38     /* OVN maps each logical flow table (ltable), one-to-one, onto a physical
39      * OpenFlow flow table (ptable).  These members describe the mapping and
40      * data related to flow tables. */
41     uint8_t n_tables;           /* Number of flow tables. */
42     uint8_t first_ptable;       /* First OpenFlow table. */
43     uint8_t cur_ltable;         /* 0 <= cur_ltable < n_tables. */
44     uint8_t output_ptable;      /* OpenFlow table for 'output' to resubmit. */
45     const struct simap *ct_zones; /* Map from port name to conntrack zone. */
46
47 /* State. */
48     char *error;                /* Error, if any, otherwise NULL. */
49
50 /* Output. */
51     struct ofpbuf *ofpacts;     /* Actions. */
52     struct expr *prereqs;       /* Prerequisites to apply to match. */
53 };
54
55 static bool
56 action_error_handle_common(struct action_context *ctx)
57 {
58     if (ctx->error) {
59         /* Already have an error, suppress this one since the cascade seems
60          * unlikely to be useful. */
61         return true;
62     } else if (ctx->lexer->token.type == LEX_T_ERROR) {
63         /* The lexer signaled an error.  Nothing at the action level
64          * accepts an error token, so we'll inevitably end up here with some
65          * meaningless parse error.  Report the lexical error instead. */
66         ctx->error = xstrdup(ctx->lexer->token.s);
67         return true;
68     } else {
69         return false;
70     }
71 }
72
73 static void OVS_PRINTF_FORMAT(2, 3)
74 action_error(struct action_context *ctx, const char *message, ...)
75 {
76     if (action_error_handle_common(ctx)) {
77         return;
78     }
79
80     va_list args;
81     va_start(args, message);
82     ctx->error = xvasprintf(message, args);
83     va_end(args);
84 }
85
86 static void OVS_PRINTF_FORMAT(2, 3)
87 action_syntax_error(struct action_context *ctx, const char *message, ...)
88 {
89     if (action_error_handle_common(ctx)) {
90         return;
91     }
92
93     struct ds s;
94
95     ds_init(&s);
96     ds_put_cstr(&s, "Syntax error");
97     if (ctx->lexer->token.type == LEX_T_END) {
98         ds_put_cstr(&s, " at end of input");
99     } else if (ctx->lexer->start) {
100         ds_put_format(&s, " at `%.*s'",
101                       (int) (ctx->lexer->input - ctx->lexer->start),
102                       ctx->lexer->start);
103     }
104
105     if (message) {
106         ds_put_char(&s, ' ');
107
108         va_list args;
109         va_start(args, message);
110         ds_put_format_valist(&s, message, args);
111         va_end(args);
112     }
113     ds_put_char(&s, '.');
114
115     ctx->error = ds_steal_cstr(&s);
116 }
117
118 /* Parses an assignment or exchange action. */
119 static void
120 parse_set_action(struct action_context *ctx)
121 {
122     struct expr *prereqs;
123     char *error;
124
125     error = expr_parse_assignment(ctx->lexer, ctx->symtab, ctx->ports,
126                                   ctx->ofpacts, &prereqs);
127     if (error) {
128         action_error(ctx, "%s", error);
129         free(error);
130         return;
131     }
132
133     ctx->prereqs = expr_combine(EXPR_T_AND, ctx->prereqs, prereqs);
134 }
135
136 static void
137 emit_resubmit(struct action_context *ctx, uint8_t table_id)
138 {
139     struct ofpact_resubmit *resubmit = ofpact_put_RESUBMIT(ctx->ofpacts);
140     resubmit->in_port = OFPP_IN_PORT;
141     resubmit->table_id = table_id;
142 }
143
144 static bool
145 action_get_int(struct action_context *ctx, int *value)
146 {
147     bool ok = lexer_get_int(ctx->lexer, value);
148     if (!ok) {
149         action_syntax_error(ctx, "expecting small integer");
150     }
151     return ok;
152 }
153
154 static void
155 parse_next_action(struct action_context *ctx)
156 {
157     if (!ctx->n_tables) {
158         action_error(ctx, "\"next\" action not allowed here.");
159     } else if (lexer_match(ctx->lexer, LEX_T_LPAREN)) {
160         int ltable;
161
162         if (!action_get_int(ctx, &ltable)) {
163             return;
164         }
165         if (!lexer_match(ctx->lexer, LEX_T_RPAREN)) {
166             action_syntax_error(ctx, "expecting `)'");
167             return;
168         }
169
170         if (ltable >= ctx->n_tables) {
171             action_error(ctx, "\"next\" argument must be in range 0 to %d.",
172                          ctx->n_tables - 1);
173             return;
174         }
175
176         emit_resubmit(ctx, ctx->first_ptable + ltable);
177     } else {
178         if (ctx->cur_ltable < ctx->n_tables) {
179             emit_resubmit(ctx, ctx->first_ptable + ctx->cur_ltable + 1);
180         } else {
181             action_error(ctx, "\"next\" action not allowed in last table.");
182         }
183     }
184 }
185
186 /* Parses 'prerequisite' as an expression in the context of 'ctx', then adds it
187  * as a conjunction with the existing 'ctx->prereqs'. */
188 static void
189 add_prerequisite(struct action_context *ctx, const char *prerequisite)
190 {
191     struct expr *expr;
192     char *error;
193
194     expr = expr_parse_string(prerequisite, ctx->symtab, &error);
195     ovs_assert(!error);
196     ctx->prereqs = expr_combine(EXPR_T_AND, ctx->prereqs, expr);
197 }
198
199 static void
200 emit_ct(struct action_context *ctx, bool recirc_next, bool commit)
201 {
202     struct ofpact_conntrack *ct = ofpact_put_CT(ctx->ofpacts);
203     ct->flags |= commit ? NX_CT_F_COMMIT : 0;
204
205     /* If "recirc" is set, we automatically go to the next table. */
206     if (recirc_next) {
207         if (ctx->cur_ltable < ctx->n_tables) {
208             ct->recirc_table = ctx->first_ptable + ctx->cur_ltable + 1;
209         } else {
210             action_error(ctx, "\"ct_next\" action not allowed in last table.");
211             return;
212         }
213     } else {
214         ct->recirc_table = NX_CT_RECIRC_NONE;
215     }
216
217     ct->zone_src.field = mf_from_id(MFF_LOG_CT_ZONE);
218     ct->zone_src.ofs = 0;
219     ct->zone_src.n_bits = 16;
220
221     /* We do not support ALGs yet. */
222     ct->alg = 0;
223
224     /* CT only works with IP, so set up a prerequisite. */
225     add_prerequisite(ctx, "ip");
226 }
227
228 static void
229 parse_actions(struct action_context *ctx)
230 {
231     /* "drop;" by itself is a valid (empty) set of actions, but it can't be
232      * combined with other actions because that doesn't make sense. */
233     if (ctx->lexer->token.type == LEX_T_ID
234         && !strcmp(ctx->lexer->token.s, "drop")
235         && lexer_lookahead(ctx->lexer) == LEX_T_SEMICOLON) {
236         lexer_get(ctx->lexer);  /* Skip "drop". */
237         lexer_get(ctx->lexer);  /* Skip ";". */
238         if (ctx->lexer->token.type != LEX_T_END) {
239             action_syntax_error(ctx, "expecting end of input");
240         }
241         return;
242     }
243
244     while (ctx->lexer->token.type != LEX_T_END) {
245         if (ctx->lexer->token.type != LEX_T_ID) {
246             action_syntax_error(ctx, NULL);
247             break;
248         }
249
250         enum lex_type lookahead = lexer_lookahead(ctx->lexer);
251         if (lookahead == LEX_T_EQUALS || lookahead == LEX_T_EXCHANGE
252             || lookahead == LEX_T_LSQUARE) {
253             parse_set_action(ctx);
254         } else if (lexer_match_id(ctx->lexer, "next")) {
255             parse_next_action(ctx);
256         } else if (lexer_match_id(ctx->lexer, "output")) {
257             emit_resubmit(ctx, ctx->output_ptable);
258         } else if (lexer_match_id(ctx->lexer, "ip.ttl")) {
259             if (lexer_match(ctx->lexer, LEX_T_DECREMENT)) {
260                 add_prerequisite(ctx, "ip");
261                 ofpact_put_DEC_TTL(ctx->ofpacts);
262             } else {
263                 action_syntax_error(ctx, "expecting `--'");
264             }
265         } else if (lexer_match_id(ctx->lexer, "ct_next")) {
266             emit_ct(ctx, true, false);
267         } else if (lexer_match_id(ctx->lexer, "ct_commit")) {
268             emit_ct(ctx, false, true);
269         } else {
270             action_syntax_error(ctx, "expecting action");
271         }
272         if (!lexer_match(ctx->lexer, LEX_T_SEMICOLON)) {
273             action_syntax_error(ctx, "expecting ';'");
274         }
275         if (ctx->error) {
276             return;
277         }
278     }
279 }
280
281 /* Parses OVN actions, in the format described for the "actions" column in the
282  * Logical_Flow table in ovn-sb(5), and appends the parsed versions of the
283  * actions to 'ofpacts' as "struct ofpact"s.
284  *
285  * 'symtab' provides a table of "struct expr_symbol"s to support (as one would
286  * provide to expr_parse()).
287  *
288  * 'ports' must be a map from strings (presumably names of ports) to integers
289  * (as one would provide to expr_to_matches()).  Strings used in the actions
290  * that are not in 'ports' are translated to zero.
291  *
292  * 'ct_zones' provides a map from a port name to its connection tracking zone.
293  *
294  * OVN maps each logical flow table (ltable), one-to-one, onto a physical
295  * OpenFlow flow table (ptable).  A number of parameters describe this mapping
296  * and data related to flow tables:
297  *
298  *     - 'first_ptable' and 'n_tables' define the range of OpenFlow tables to
299  *       which the logical "next" action should be able to jump.  Logical table
300  *       0 maps to OpenFlow table 'first_ptable', logical table 1 to
301  *       'first_ptable + 1', and so on.  If 'n_tables' is 0 then "next" is
302  *       disallowed entirely.
303  *
304  *     - 'cur_ltable' is an offset from 'first_ptable' (e.g. 0 <= cur_ltable <
305  *       n_ptables) of the logical flow that contains the actions.  If
306  *       cur_ltable + 1 < n_tables, then this defines the default table that
307  *       "next" will jump to.
308  *
309  * 'next_table_id' should be the OpenFlow table to which the "next" action will
310  * resubmit, or 0 to disable "next".
311  *
312  *     - 'output_ptable' should be the OpenFlow table to which the logical
313  *       "output" action will resubmit
314  *
315  * Some actions add extra requirements (prerequisites) to the flow's match.  If
316  * so, this function sets '*prereqsp' to the actions' prerequisites; otherwise,
317  * it sets '*prereqsp' to NULL.  The caller owns '*prereqsp' and must
318  * eventually free it.
319  *
320  * Returns NULL on success, otherwise a malloc()'d error message that the
321  * caller must free.  On failure, 'ofpacts' has the same contents and
322  * '*prereqsp' is set to NULL, but some tokens may have been consumed from
323  * 'lexer'.
324   */
325 char * OVS_WARN_UNUSED_RESULT
326 actions_parse(struct lexer *lexer, const struct shash *symtab,
327               const struct simap *ports, const struct simap *ct_zones,
328               uint8_t first_ptable, uint8_t n_tables, uint8_t cur_ltable,
329               uint8_t output_ptable, struct ofpbuf *ofpacts,
330               struct expr **prereqsp)
331 {
332     size_t ofpacts_start = ofpacts->size;
333
334     struct action_context ctx;
335     ctx.lexer = lexer;
336     ctx.symtab = symtab;
337     ctx.ports = ports;
338     ctx.ct_zones = ct_zones;
339     ctx.first_ptable = first_ptable;
340     ctx.n_tables = n_tables;
341     ctx.cur_ltable = cur_ltable;
342     ctx.output_ptable = output_ptable;
343     ctx.error = NULL;
344     ctx.ofpacts = ofpacts;
345     ctx.prereqs = NULL;
346
347     parse_actions(&ctx);
348
349     if (!ctx.error) {
350         *prereqsp = ctx.prereqs;
351         return NULL;
352     } else {
353         ofpacts->size = ofpacts_start;
354         expr_destroy(ctx.prereqs);
355         *prereqsp = NULL;
356         return ctx.error;
357     }
358 }
359
360 /* Like actions_parse(), but the actions are taken from 's'. */
361 char * OVS_WARN_UNUSED_RESULT
362 actions_parse_string(const char *s, const struct shash *symtab,
363                      const struct simap *ports, const struct simap *ct_zones,
364                      uint8_t first_table, uint8_t n_tables, uint8_t cur_table,
365                      uint8_t output_table, struct ofpbuf *ofpacts,
366                      struct expr **prereqsp)
367 {
368     struct lexer lexer;
369     char *error;
370
371     lexer_init(&lexer, s);
372     lexer_get(&lexer);
373     error = actions_parse(&lexer, symtab, ports, ct_zones, first_table,
374                           n_tables, cur_table, output_table, ofpacts,
375                           prereqsp);
376     lexer_destroy(&lexer);
377
378     return error;
379 }