2 * Copyright (c) 2015 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "dynamic-string.h"
25 #include "ofp-actions.h"
29 /* Context maintained during actions_parse(). */
30 struct action_context {
33 struct lexer *lexer; /* Lexer for pulling more tokens. */
34 const struct simap *ports; /* Map from port name to number. */
35 const struct shash *symtab; /* Symbol table. */
37 /* OVN maps each logical flow table (ltable), one-to-one, onto a physical
38 * OpenFlow flow table (ptable). These members describe the mapping and
39 * data related to flow tables. */
40 uint8_t n_tables; /* Number of flow tables. */
41 uint8_t first_ptable; /* First OpenFlow table. */
42 uint8_t cur_ltable; /* 0 <= cur_ltable < n_tables. */
43 uint8_t output_ptable; /* OpenFlow table for 'output' to resubmit. */
44 const struct simap *ct_zones; /* Map from port name to conntrack zone. */
47 char *error; /* Error, if any, otherwise NULL. */
50 struct ofpbuf *ofpacts; /* Actions. */
51 struct expr *prereqs; /* Prerequisites to apply to match. */
55 action_error_handle_common(struct action_context *ctx)
58 /* Already have an error, suppress this one since the cascade seems
59 * unlikely to be useful. */
61 } else if (ctx->lexer->token.type == LEX_T_ERROR) {
62 /* The lexer signaled an error. Nothing at the action level
63 * accepts an error token, so we'll inevitably end up here with some
64 * meaningless parse error. Report the lexical error instead. */
65 ctx->error = xstrdup(ctx->lexer->token.s);
72 static void OVS_PRINTF_FORMAT(2, 3)
73 action_error(struct action_context *ctx, const char *message, ...)
75 if (action_error_handle_common(ctx)) {
80 va_start(args, message);
81 ctx->error = xvasprintf(message, args);
85 static void OVS_PRINTF_FORMAT(2, 3)
86 action_syntax_error(struct action_context *ctx, const char *message, ...)
88 if (action_error_handle_common(ctx)) {
95 ds_put_cstr(&s, "Syntax error");
96 if (ctx->lexer->token.type == LEX_T_END) {
97 ds_put_cstr(&s, " at end of input");
98 } else if (ctx->lexer->start) {
99 ds_put_format(&s, " at `%.*s'",
100 (int) (ctx->lexer->input - ctx->lexer->start),
105 ds_put_char(&s, ' ');
108 va_start(args, message);
109 ds_put_format_valist(&s, message, args);
112 ds_put_char(&s, '.');
114 ctx->error = ds_steal_cstr(&s);
117 /* Parses an assignment or exchange action. */
119 parse_set_action(struct action_context *ctx)
121 struct expr *prereqs;
124 error = expr_parse_assignment(ctx->lexer, ctx->symtab, ctx->ports,
125 ctx->ofpacts, &prereqs);
127 action_error(ctx, "%s", error);
132 ctx->prereqs = expr_combine(EXPR_T_AND, ctx->prereqs, prereqs);
136 emit_resubmit(struct action_context *ctx, uint8_t table_id)
138 struct ofpact_resubmit *resubmit = ofpact_put_RESUBMIT(ctx->ofpacts);
139 resubmit->in_port = OFPP_IN_PORT;
140 resubmit->table_id = table_id;
144 action_get_int(struct action_context *ctx, int *value)
146 bool ok = lexer_get_int(ctx->lexer, value);
148 action_syntax_error(ctx, "expecting small integer");
154 parse_next_action(struct action_context *ctx)
156 if (!ctx->n_tables) {
157 action_error(ctx, "\"next\" action not allowed here.");
158 } else if (lexer_match(ctx->lexer, LEX_T_LPAREN)) {
161 if (!action_get_int(ctx, <able)) {
164 if (!lexer_match(ctx->lexer, LEX_T_RPAREN)) {
165 action_syntax_error(ctx, "expecting `)'");
169 if (ltable >= ctx->n_tables) {
170 action_error(ctx, "\"next\" argument must be in range 0 to %d.",
175 emit_resubmit(ctx, ctx->first_ptable + ltable);
177 if (ctx->cur_ltable < ctx->n_tables) {
178 emit_resubmit(ctx, ctx->first_ptable + ctx->cur_ltable + 1);
180 action_error(ctx, "\"next\" action not allowed in last table.");
186 emit_ct(struct action_context *ctx, bool recirc_next, bool commit)
188 struct ofpact_conntrack *ct = ofpact_put_CT(ctx->ofpacts);
189 ct->flags |= commit ? NX_CT_F_COMMIT : 0;
191 /* If "recirc" is set, we automatically go to the next table. */
193 if (ctx->cur_ltable < ctx->n_tables) {
194 ct->recirc_table = ctx->first_ptable + ctx->cur_ltable + 1;
196 action_error(ctx, "\"ct_next\" action not allowed in last table.");
200 ct->recirc_table = NX_CT_RECIRC_NONE;
203 /* xxx Should remove hard-coding reg5 if we refactor library. */
204 ct->zone_src.field = mf_from_id(MFF_REG5);
205 ct->zone_src.ofs = 0;
206 ct->zone_src.n_bits = 16;
208 /* We do not support ALGs yet. */
211 /* CT only works with IP, so set up a prerequisite. */
215 expr = expr_parse_string("ip", ctx->symtab, &error);
217 ctx->prereqs = expr_combine(EXPR_T_AND, ctx->prereqs, expr);
221 parse_actions(struct action_context *ctx)
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");
236 while (ctx->lexer->token.type != LEX_T_END) {
237 if (ctx->lexer->token.type != LEX_T_ID) {
238 action_syntax_error(ctx, NULL);
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,
254 ctx->prereqs = expr_combine(EXPR_T_AND, ctx->prereqs, e);
255 ofpact_put_DEC_TTL(ctx->ofpacts);
257 action_syntax_error(ctx, "expecting `--'");
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);
264 action_syntax_error(ctx, "expecting action");
266 if (!lexer_match(ctx->lexer, LEX_T_SEMICOLON)) {
267 action_syntax_error(ctx, "expecting ';'");
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.
279 * 'symtab' provides a table of "struct expr_symbol"s to support (as one would
280 * provide to expr_parse()).
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.
286 * 'ct_zones' provides a map from a port name to its connection tracking zone.
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:
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.
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.
303 * 'next_table_id' should be the OpenFlow table to which the "next" action will
304 * resubmit, or 0 to disable "next".
306 * - 'output_ptable' should be the OpenFlow table to which the logical
307 * "output" action will resubmit
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.
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
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)
326 size_t ofpacts_start = ofpacts->size;
328 struct action_context ctx;
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;
338 ctx.ofpacts = ofpacts;
344 *prereqsp = ctx.prereqs;
347 ofpacts->size = ofpacts_start;
348 expr_destroy(ctx.prereqs);
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)
365 lexer_init(&lexer, s);
367 error = actions_parse(&lexer, symtab, ports, ct_zones, first_table,
368 n_tables, cur_table, output_table, ofpacts,
370 lexer_destroy(&lexer);