ccf97f04723b213452ec379ef7dc6a4bd4d1b7b1
[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 static void
187 emit_ct(struct action_context *ctx, bool recirc_next, bool commit)
188 {
189     struct ofpact_conntrack *ct = ofpact_put_CT(ctx->ofpacts);
190     ct->flags |= commit ? NX_CT_F_COMMIT : 0;
191
192     /* If "recirc" is set, we automatically go to the next table. */
193     if (recirc_next) {
194         if (ctx->cur_ltable < ctx->n_tables) {
195             ct->recirc_table = ctx->first_ptable + ctx->cur_ltable + 1;
196         } else {
197             action_error(ctx, "\"ct_next\" action not allowed in last table.");
198             return;
199         }
200     } else {
201         ct->recirc_table = NX_CT_RECIRC_NONE;
202     }
203
204     ct->zone_src.field = mf_from_id(MFF_LOG_CT_ZONE);
205     ct->zone_src.ofs = 0;
206     ct->zone_src.n_bits = 16;
207
208     /* We do not support ALGs yet. */
209     ct->alg = 0;
210
211     /* CT only works with IP, so set up a prerequisite. */
212     struct expr *expr;
213     char *error;
214
215     expr = expr_parse_string("ip", ctx->symtab, &error);
216     ovs_assert(!error);
217     ctx->prereqs = expr_combine(EXPR_T_AND, ctx->prereqs, expr);
218 }
219
220 static void
221 parse_actions(struct action_context *ctx)
222 {
223     /* "drop;" by itself is a valid (empty) set of actions, but it can't be
224      * combined with other actions because that doesn't make sense. */
225     if (ctx->lexer->token.type == LEX_T_ID
226         && !strcmp(ctx->lexer->token.s, "drop")
227         && lexer_lookahead(ctx->lexer) == LEX_T_SEMICOLON) {
228         lexer_get(ctx->lexer);  /* Skip "drop". */
229         lexer_get(ctx->lexer);  /* Skip ";". */
230         if (ctx->lexer->token.type != LEX_T_END) {
231             action_syntax_error(ctx, "expecting end of input");
232         }
233         return;
234     }
235
236     while (ctx->lexer->token.type != LEX_T_END) {
237         if (ctx->lexer->token.type != LEX_T_ID) {
238             action_syntax_error(ctx, NULL);
239             break;
240         }
241
242         enum lex_type lookahead = lexer_lookahead(ctx->lexer);
243         if (lookahead == LEX_T_EQUALS || lookahead == LEX_T_EXCHANGE
244             || lookahead == LEX_T_LSQUARE) {
245             parse_set_action(ctx);
246         } else if (lexer_match_id(ctx->lexer, "next")) {
247             parse_next_action(ctx);
248         } else if (lexer_match_id(ctx->lexer, "output")) {
249             emit_resubmit(ctx, ctx->output_ptable);
250         } else if (lexer_match_id(ctx->lexer, "ip4.ttl")) {
251             if (lexer_match(ctx->lexer, LEX_T_DECREMENT)) {
252                 struct expr *e = expr_parse_string("ip4", ctx->symtab,
253                                                    &ctx->error);
254                 ctx->prereqs = expr_combine(EXPR_T_AND, ctx->prereqs, e);
255                 ofpact_put_DEC_TTL(ctx->ofpacts);
256             } else {
257                 action_syntax_error(ctx, "expecting `--'");
258             }
259         } else if (lexer_match_id(ctx->lexer, "ct_next")) {
260             emit_ct(ctx, true, false);
261         } else if (lexer_match_id(ctx->lexer, "ct_commit")) {
262             emit_ct(ctx, false, true);
263         } else {
264             action_syntax_error(ctx, "expecting action");
265         }
266         if (!lexer_match(ctx->lexer, LEX_T_SEMICOLON)) {
267             action_syntax_error(ctx, "expecting ';'");
268         }
269         if (ctx->error) {
270             return;
271         }
272     }
273 }
274
275 /* Parses OVN actions, in the format described for the "actions" column in the
276  * Logical_Flow table in ovn-sb(5), and appends the parsed versions of the
277  * actions to 'ofpacts' as "struct ofpact"s.
278  *
279  * 'symtab' provides a table of "struct expr_symbol"s to support (as one would
280  * provide to expr_parse()).
281  *
282  * 'ports' must be a map from strings (presumably names of ports) to integers
283  * (as one would provide to expr_to_matches()).  Strings used in the actions
284  * that are not in 'ports' are translated to zero.
285  *
286  * 'ct_zones' provides a map from a port name to its connection tracking zone.
287  *
288  * OVN maps each logical flow table (ltable), one-to-one, onto a physical
289  * OpenFlow flow table (ptable).  A number of parameters describe this mapping
290  * and data related to flow tables:
291  *
292  *     - 'first_ptable' and 'n_tables' define the range of OpenFlow tables to
293  *       which the logical "next" action should be able to jump.  Logical table
294  *       0 maps to OpenFlow table 'first_ptable', logical table 1 to
295  *       'first_ptable + 1', and so on.  If 'n_tables' is 0 then "next" is
296  *       disallowed entirely.
297  *
298  *     - 'cur_ltable' is an offset from 'first_ptable' (e.g. 0 <= cur_ltable <
299  *       n_ptables) of the logical flow that contains the actions.  If
300  *       cur_ltable + 1 < n_tables, then this defines the default table that
301  *       "next" will jump to.
302  *
303  * 'next_table_id' should be the OpenFlow table to which the "next" action will
304  * resubmit, or 0 to disable "next".
305  *
306  *     - 'output_ptable' should be the OpenFlow table to which the logical
307  *       "output" action will resubmit
308  *
309  * Some actions add extra requirements (prerequisites) to the flow's match.  If
310  * so, this function sets '*prereqsp' to the actions' prerequisites; otherwise,
311  * it sets '*prereqsp' to NULL.  The caller owns '*prereqsp' and must
312  * eventually free it.
313  *
314  * Returns NULL on success, otherwise a malloc()'d error message that the
315  * caller must free.  On failure, 'ofpacts' has the same contents and
316  * '*prereqsp' is set to NULL, but some tokens may have been consumed from
317  * 'lexer'.
318   */
319 char * OVS_WARN_UNUSED_RESULT
320 actions_parse(struct lexer *lexer, const struct shash *symtab,
321               const struct simap *ports, const struct simap *ct_zones,
322               uint8_t first_ptable, uint8_t n_tables, uint8_t cur_ltable,
323               uint8_t output_ptable, struct ofpbuf *ofpacts,
324               struct expr **prereqsp)
325 {
326     size_t ofpacts_start = ofpacts->size;
327
328     struct action_context ctx;
329     ctx.lexer = lexer;
330     ctx.symtab = symtab;
331     ctx.ports = ports;
332     ctx.ct_zones = ct_zones;
333     ctx.first_ptable = first_ptable;
334     ctx.n_tables = n_tables;
335     ctx.cur_ltable = cur_ltable;
336     ctx.output_ptable = output_ptable;
337     ctx.error = NULL;
338     ctx.ofpacts = ofpacts;
339     ctx.prereqs = NULL;
340
341     parse_actions(&ctx);
342
343     if (!ctx.error) {
344         *prereqsp = ctx.prereqs;
345         return NULL;
346     } else {
347         ofpacts->size = ofpacts_start;
348         expr_destroy(ctx.prereqs);
349         *prereqsp = NULL;
350         return ctx.error;
351     }
352 }
353
354 /* Like actions_parse(), but the actions are taken from 's'. */
355 char * OVS_WARN_UNUSED_RESULT
356 actions_parse_string(const char *s, const struct shash *symtab,
357                      const struct simap *ports, const struct simap *ct_zones,
358                      uint8_t first_table, uint8_t n_tables, uint8_t cur_table,
359                      uint8_t output_table, struct ofpbuf *ofpacts,
360                      struct expr **prereqsp)
361 {
362     struct lexer lexer;
363     char *error;
364
365     lexer_init(&lexer, s);
366     lexer_get(&lexer);
367     error = actions_parse(&lexer, symtab, ports, ct_zones, first_table,
368                           n_tables, cur_table, output_table, ofpacts,
369                           prereqsp);
370     lexer_destroy(&lexer);
371
372     return error;
373 }