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 {
32 const struct action_params *ap; /* Parameters. */
33 struct lexer *lexer; /* Lexer for pulling more tokens. */
34 char *error; /* Error, if any, otherwise NULL. */
35 struct ofpbuf *ofpacts; /* Actions. */
36 struct expr *prereqs; /* Prerequisites to apply to match. */
39 static bool parse_action(struct action_context *);
42 action_error_handle_common(struct action_context *ctx)
45 /* Already have an error, suppress this one since the cascade seems
46 * unlikely to be useful. */
48 } else if (ctx->lexer->token.type == LEX_T_ERROR) {
49 /* The lexer signaled an error. Nothing at the action level
50 * accepts an error token, so we'll inevitably end up here with some
51 * meaningless parse error. Report the lexical error instead. */
52 ctx->error = xstrdup(ctx->lexer->token.s);
59 static void OVS_PRINTF_FORMAT(2, 3)
60 action_error(struct action_context *ctx, const char *message, ...)
62 if (action_error_handle_common(ctx)) {
67 va_start(args, message);
68 ctx->error = xvasprintf(message, args);
72 static void OVS_PRINTF_FORMAT(2, 3)
73 action_syntax_error(struct action_context *ctx, const char *message, ...)
75 if (action_error_handle_common(ctx)) {
82 ds_put_cstr(&s, "Syntax error");
83 if (ctx->lexer->token.type == LEX_T_END) {
84 ds_put_cstr(&s, " at end of input");
85 } else if (ctx->lexer->start) {
86 ds_put_format(&s, " at `%.*s'",
87 (int) (ctx->lexer->input - ctx->lexer->start),
95 va_start(args, message);
96 ds_put_format_valist(&s, message, args);
101 ctx->error = ds_steal_cstr(&s);
104 /* Parses an assignment or exchange action. */
106 parse_set_action(struct action_context *ctx)
108 struct expr *prereqs;
111 error = expr_parse_assignment(ctx->lexer, ctx->ap->symtab, ctx->ap->ports,
112 ctx->ofpacts, &prereqs);
114 action_error(ctx, "%s", error);
119 ctx->prereqs = expr_combine(EXPR_T_AND, ctx->prereqs, prereqs);
123 emit_resubmit(struct action_context *ctx, uint8_t table_id)
125 struct ofpact_resubmit *resubmit = ofpact_put_RESUBMIT(ctx->ofpacts);
126 resubmit->in_port = OFPP_IN_PORT;
127 resubmit->table_id = table_id;
131 action_get_int(struct action_context *ctx, int *value)
133 bool ok = lexer_get_int(ctx->lexer, value);
135 action_syntax_error(ctx, "expecting small integer");
141 parse_next_action(struct action_context *ctx)
143 if (!ctx->ap->n_tables) {
144 action_error(ctx, "\"next\" action not allowed here.");
145 } else if (lexer_match(ctx->lexer, LEX_T_LPAREN)) {
148 if (!action_get_int(ctx, <able)) {
151 if (!lexer_match(ctx->lexer, LEX_T_RPAREN)) {
152 action_syntax_error(ctx, "expecting `)'");
156 if (ltable >= ctx->ap->n_tables) {
157 action_error(ctx, "\"next\" argument must be in range 0 to %d.",
158 ctx->ap->n_tables - 1);
162 emit_resubmit(ctx, ctx->ap->first_ptable + ltable);
164 if (ctx->ap->cur_ltable < ctx->ap->n_tables) {
166 ctx->ap->first_ptable + ctx->ap->cur_ltable + 1);
168 action_error(ctx, "\"next\" action not allowed in last table.");
173 /* Parses 'prerequisite' as an expression in the context of 'ctx', then adds it
174 * as a conjunction with the existing 'ctx->prereqs'. */
176 add_prerequisite(struct action_context *ctx, const char *prerequisite)
181 expr = expr_parse_string(prerequisite, ctx->ap->symtab, &error);
183 ctx->prereqs = expr_combine(EXPR_T_AND, ctx->prereqs, expr);
187 emit_ct(struct action_context *ctx, bool recirc_next, bool commit)
189 struct ofpact_conntrack *ct = ofpact_put_CT(ctx->ofpacts);
190 ct->flags |= commit ? NX_CT_F_COMMIT : 0;
192 /* If "recirc" is set, we automatically go to the next table. */
194 if (ctx->ap->cur_ltable < ctx->ap->n_tables) {
195 ct->recirc_table = ctx->ap->first_ptable + ctx->ap->cur_ltable + 1;
197 action_error(ctx, "\"ct_next\" action not allowed in last table.");
201 ct->recirc_table = NX_CT_RECIRC_NONE;
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;
208 /* We do not support ALGs yet. */
211 /* CT only works with IP, so set up a prerequisite. */
212 add_prerequisite(ctx, "ip");
216 parse_action(struct action_context *ctx)
218 if (ctx->lexer->token.type != LEX_T_ID) {
219 action_syntax_error(ctx, NULL);
223 enum lex_type lookahead = lexer_lookahead(ctx->lexer);
224 if (lookahead == LEX_T_EQUALS || lookahead == LEX_T_EXCHANGE
225 || lookahead == LEX_T_LSQUARE) {
226 parse_set_action(ctx);
227 } else if (lexer_match_id(ctx->lexer, "next")) {
228 parse_next_action(ctx);
229 } else if (lexer_match_id(ctx->lexer, "output")) {
230 emit_resubmit(ctx, ctx->ap->output_ptable);
231 } else if (lexer_match_id(ctx->lexer, "ip.ttl")) {
232 if (lexer_match(ctx->lexer, LEX_T_DECREMENT)) {
233 add_prerequisite(ctx, "ip");
234 ofpact_put_DEC_TTL(ctx->ofpacts);
236 action_syntax_error(ctx, "expecting `--'");
238 } else if (lexer_match_id(ctx->lexer, "ct_next")) {
239 emit_ct(ctx, true, false);
240 } else if (lexer_match_id(ctx->lexer, "ct_commit")) {
241 emit_ct(ctx, false, true);
243 action_syntax_error(ctx, "expecting action");
245 if (!lexer_match(ctx->lexer, LEX_T_SEMICOLON)) {
246 action_syntax_error(ctx, "expecting ';'");
252 parse_actions(struct action_context *ctx)
254 /* "drop;" by itself is a valid (empty) set of actions, but it can't be
255 * combined with other actions because that doesn't make sense. */
256 if (ctx->lexer->token.type == LEX_T_ID
257 && !strcmp(ctx->lexer->token.s, "drop")
258 && lexer_lookahead(ctx->lexer) == LEX_T_SEMICOLON) {
259 lexer_get(ctx->lexer); /* Skip "drop". */
260 lexer_get(ctx->lexer); /* Skip ";". */
261 if (ctx->lexer->token.type != LEX_T_END) {
262 action_syntax_error(ctx, "expecting end of input");
267 while (ctx->lexer->token.type != LEX_T_END) {
268 if (!parse_action(ctx)) {
274 /* Parses OVN actions, in the format described for the "actions" column in the
275 * Logical_Flow table in ovn-sb(5), and appends the parsed versions of the
276 * actions to 'ofpacts' as "struct ofpact"s.
278 * 'ap' provides most of the parameters for translation.
280 * Some actions add extra requirements (prerequisites) to the flow's match. If
281 * so, this function sets '*prereqsp' to the actions' prerequisites; otherwise,
282 * it sets '*prereqsp' to NULL. The caller owns '*prereqsp' and must
283 * eventually free it.
285 * Returns NULL on success, otherwise a malloc()'d error message that the
286 * caller must free. On failure, 'ofpacts' has the same contents and
287 * '*prereqsp' is set to NULL, but some tokens may have been consumed from
290 char * OVS_WARN_UNUSED_RESULT
291 actions_parse(struct lexer *lexer, const struct action_params *ap,
292 struct ofpbuf *ofpacts, struct expr **prereqsp)
294 size_t ofpacts_start = ofpacts->size;
296 struct action_context ctx = {
306 *prereqsp = ctx.prereqs;
309 ofpacts->size = ofpacts_start;
310 expr_destroy(ctx.prereqs);
316 /* Like actions_parse(), but the actions are taken from 's'. */
317 char * OVS_WARN_UNUSED_RESULT
318 actions_parse_string(const char *s, const struct action_params *ap,
319 struct ofpbuf *ofpacts, struct expr **prereqsp)
324 lexer_init(&lexer, s);
326 error = actions_parse(&lexer, ap, ofpacts, prereqsp);
327 lexer_destroy(&lexer);