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 "logical-fields.h"
26 #include "ofp-actions.h"
30 /* Context maintained during actions_parse(). */
31 struct action_context {
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. */
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. */
48 char *error; /* Error, if any, otherwise NULL. */
51 struct ofpbuf *ofpacts; /* Actions. */
52 struct expr *prereqs; /* Prerequisites to apply to match. */
56 action_error_handle_common(struct action_context *ctx)
59 /* Already have an error, suppress this one since the cascade seems
60 * unlikely to be useful. */
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);
73 static void OVS_PRINTF_FORMAT(2, 3)
74 action_error(struct action_context *ctx, const char *message, ...)
76 if (action_error_handle_common(ctx)) {
81 va_start(args, message);
82 ctx->error = xvasprintf(message, args);
86 static void OVS_PRINTF_FORMAT(2, 3)
87 action_syntax_error(struct action_context *ctx, const char *message, ...)
89 if (action_error_handle_common(ctx)) {
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),
106 ds_put_char(&s, ' ');
109 va_start(args, message);
110 ds_put_format_valist(&s, message, args);
113 ds_put_char(&s, '.');
115 ctx->error = ds_steal_cstr(&s);
118 /* Parses an assignment or exchange action. */
120 parse_set_action(struct action_context *ctx)
122 struct expr *prereqs;
125 error = expr_parse_assignment(ctx->lexer, ctx->symtab, ctx->ports,
126 ctx->ofpacts, &prereqs);
128 action_error(ctx, "%s", error);
133 ctx->prereqs = expr_combine(EXPR_T_AND, ctx->prereqs, prereqs);
137 emit_resubmit(struct action_context *ctx, uint8_t table_id)
139 struct ofpact_resubmit *resubmit = ofpact_put_RESUBMIT(ctx->ofpacts);
140 resubmit->in_port = OFPP_IN_PORT;
141 resubmit->table_id = table_id;
145 action_get_int(struct action_context *ctx, int *value)
147 bool ok = lexer_get_int(ctx->lexer, value);
149 action_syntax_error(ctx, "expecting small integer");
155 parse_next_action(struct action_context *ctx)
157 if (!ctx->n_tables) {
158 action_error(ctx, "\"next\" action not allowed here.");
159 } else if (lexer_match(ctx->lexer, LEX_T_LPAREN)) {
162 if (!action_get_int(ctx, <able)) {
165 if (!lexer_match(ctx->lexer, LEX_T_RPAREN)) {
166 action_syntax_error(ctx, "expecting `)'");
170 if (ltable >= ctx->n_tables) {
171 action_error(ctx, "\"next\" argument must be in range 0 to %d.",
176 emit_resubmit(ctx, ctx->first_ptable + ltable);
178 if (ctx->cur_ltable < ctx->n_tables) {
179 emit_resubmit(ctx, ctx->first_ptable + ctx->cur_ltable + 1);
181 action_error(ctx, "\"next\" action not allowed in last table.");
186 /* Parses 'prerequisite' as an expression in the context of 'ctx', then adds it
187 * as a conjunction with the existing 'ctx->prereqs'. */
189 add_prerequisite(struct action_context *ctx, const char *prerequisite)
194 expr = expr_parse_string(prerequisite, ctx->symtab, &error);
196 ctx->prereqs = expr_combine(EXPR_T_AND, ctx->prereqs, expr);
200 emit_ct(struct action_context *ctx, bool recirc_next, bool commit)
202 struct ofpact_conntrack *ct = ofpact_put_CT(ctx->ofpacts);
203 ct->flags |= commit ? NX_CT_F_COMMIT : 0;
205 /* If "recirc" is set, we automatically go to the next table. */
207 if (ctx->cur_ltable < ctx->n_tables) {
208 ct->recirc_table = ctx->first_ptable + ctx->cur_ltable + 1;
210 action_error(ctx, "\"ct_next\" action not allowed in last table.");
214 ct->recirc_table = NX_CT_RECIRC_NONE;
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;
221 /* We do not support ALGs yet. */
224 /* CT only works with IP, so set up a prerequisite. */
225 add_prerequisite(ctx, "ip");
229 parse_actions(struct action_context *ctx)
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");
244 while (ctx->lexer->token.type != LEX_T_END) {
245 if (ctx->lexer->token.type != LEX_T_ID) {
246 action_syntax_error(ctx, NULL);
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);
263 action_syntax_error(ctx, "expecting `--'");
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);
270 action_syntax_error(ctx, "expecting action");
272 if (!lexer_match(ctx->lexer, LEX_T_SEMICOLON)) {
273 action_syntax_error(ctx, "expecting ';'");
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.
285 * 'symtab' provides a table of "struct expr_symbol"s to support (as one would
286 * provide to expr_parse()).
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.
292 * 'ct_zones' provides a map from a port name to its connection tracking zone.
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:
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.
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.
309 * 'next_table_id' should be the OpenFlow table to which the "next" action will
310 * resubmit, or 0 to disable "next".
312 * - 'output_ptable' should be the OpenFlow table to which the logical
313 * "output" action will resubmit
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.
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
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)
332 size_t ofpacts_start = ofpacts->size;
334 struct action_context ctx;
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;
344 ctx.ofpacts = ofpacts;
350 *prereqsp = ctx.prereqs;
353 ofpacts->size = ofpacts_start;
354 expr_destroy(ctx.prereqs);
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)
371 lexer_init(&lexer, s);
373 error = actions_parse(&lexer, symtab, ports, ct_zones, first_table,
374 n_tables, cur_table, output_table, ofpacts,
376 lexer_destroy(&lexer);