]> git.proxmox.com Git - ovs.git/blob - lib/learn.c
lib: Check match and action prerequisities with 'match'.
[ovs.git] / lib / learn.c
1 /*
2 * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 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
19 #include "learn.h"
20
21 #include "byte-order.h"
22 #include "colors.h"
23 #include "nx-match.h"
24 #include "openflow/openflow.h"
25 #include "openvswitch/dynamic-string.h"
26 #include "openvswitch/match.h"
27 #include "openvswitch/meta-flow.h"
28 #include "openvswitch/ofp-actions.h"
29 #include "openvswitch/ofp-errors.h"
30 #include "openvswitch/ofp-util.h"
31 #include "openvswitch/ofpbuf.h"
32 #include "unaligned.h"
33
34
35 /* Checks that 'learn' is a valid action on 'flow'. Returns 0 if it is valid,
36 * otherwise an OFPERR_*. */
37 enum ofperr
38 learn_check(const struct ofpact_learn *learn, const struct match *src_match)
39 {
40 const struct ofpact_learn_spec *spec;
41 struct match dst_match;
42
43 match_init_catchall(&dst_match);
44 OFPACT_LEARN_SPEC_FOR_EACH (spec, learn) {
45 enum ofperr error;
46
47 /* Check the source. */
48 if (spec->src_type == NX_LEARN_SRC_FIELD) {
49 error = mf_check_src(&spec->src, src_match);
50 if (error) {
51 return error;
52 }
53 }
54
55 /* Check the destination. */
56 switch (spec->dst_type) {
57 case NX_LEARN_DST_MATCH:
58 error = mf_check_src(&spec->dst, &dst_match);
59 if (error) {
60 return error;
61 }
62 if (spec->src_type & NX_LEARN_SRC_IMMEDIATE) {
63 mf_write_subfield_value(&spec->dst,
64 ofpact_learn_spec_imm(spec),
65 &dst_match);
66 }
67 break;
68
69 case NX_LEARN_DST_LOAD:
70 error = mf_check_dst(&spec->dst, &dst_match);
71 if (error) {
72 return error;
73 }
74 break;
75
76 case NX_LEARN_DST_OUTPUT:
77 /* Nothing to do. */
78 break;
79 }
80 }
81 return 0;
82 }
83
84 /* Composes 'fm' so that executing it will implement 'learn' given that the
85 * packet being processed has 'flow' as its flow.
86 *
87 * Uses 'ofpacts' to store the flow mod's actions. The caller must initialize
88 * 'ofpacts' and retains ownership of it. 'fm->ofpacts' will point into the
89 * 'ofpacts' buffer.
90 *
91 * The caller has to actually execute 'fm'. */
92 void
93 learn_execute(const struct ofpact_learn *learn, const struct flow *flow,
94 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
95 {
96 const struct ofpact_learn_spec *spec;
97
98 match_init_catchall(&fm->match);
99 fm->priority = learn->priority;
100 fm->cookie = htonll(0);
101 fm->cookie_mask = htonll(0);
102 fm->new_cookie = learn->cookie;
103 fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
104 fm->table_id = learn->table_id;
105 fm->command = OFPFC_MODIFY_STRICT;
106 fm->idle_timeout = learn->idle_timeout;
107 fm->hard_timeout = learn->hard_timeout;
108 fm->importance = 0;
109 fm->buffer_id = UINT32_MAX;
110 fm->out_port = OFPP_NONE;
111 fm->flags = 0;
112 if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
113 fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
114 }
115 fm->ofpacts = NULL;
116 fm->ofpacts_len = 0;
117
118 if (learn->fin_idle_timeout || learn->fin_hard_timeout) {
119 struct ofpact_fin_timeout *oft;
120
121 oft = ofpact_put_FIN_TIMEOUT(ofpacts);
122 oft->fin_idle_timeout = learn->fin_idle_timeout;
123 oft->fin_hard_timeout = learn->fin_hard_timeout;
124 }
125
126 OFPACT_LEARN_SPEC_FOR_EACH (spec, learn) {
127 struct ofpact_set_field *sf;
128 union mf_subvalue value;
129
130 if (spec->src_type == NX_LEARN_SRC_FIELD) {
131 mf_read_subfield(&spec->src, flow, &value);
132 } else {
133 mf_subvalue_from_value(&spec->dst, &value,
134 ofpact_learn_spec_imm(spec));
135 }
136
137 switch (spec->dst_type) {
138 case NX_LEARN_DST_MATCH:
139 mf_write_subfield(&spec->dst, &value, &fm->match);
140 break;
141
142 case NX_LEARN_DST_LOAD:
143 sf = ofpact_put_reg_load(ofpacts, spec->dst.field, NULL, NULL);
144 bitwise_copy(&value, sizeof value, 0,
145 sf->value, spec->dst.field->n_bytes, spec->dst.ofs,
146 spec->n_bits);
147 bitwise_one(ofpact_set_field_mask(sf), spec->dst.field->n_bytes,
148 spec->dst.ofs, spec->n_bits);
149 break;
150
151 case NX_LEARN_DST_OUTPUT:
152 if (spec->n_bits <= 16
153 || is_all_zeros(value.u8, sizeof value - 2)) {
154 ofp_port_t port = u16_to_ofp(ntohll(value.integer));
155
156 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)
157 || port == OFPP_IN_PORT
158 || port == OFPP_FLOOD
159 || port == OFPP_LOCAL
160 || port == OFPP_ALL) {
161 ofpact_put_OUTPUT(ofpacts)->port = port;
162 }
163 }
164 break;
165 }
166 }
167
168 fm->ofpacts = ofpacts->data;
169 fm->ofpacts_len = ofpacts->size;
170 }
171
172 /* Perform a bitwise-OR on 'wc''s fields that are relevant as sources in
173 * the learn action 'learn'. */
174 void
175 learn_mask(const struct ofpact_learn *learn, struct flow_wildcards *wc)
176 {
177 const struct ofpact_learn_spec *spec;
178 union mf_subvalue value;
179
180 memset(&value, 0xff, sizeof value);
181 OFPACT_LEARN_SPEC_FOR_EACH (spec, learn) {
182 if (spec->src_type == NX_LEARN_SRC_FIELD) {
183 mf_write_subfield_flow(&spec->src, &value, &wc->masks);
184 }
185 }
186 }
187
188 /* Returns NULL if successful, otherwise a malloc()'d string describing the
189 * error. The caller is responsible for freeing the returned string. */
190 static char * OVS_WARN_UNUSED_RESULT
191 learn_parse_load_immediate(union mf_subvalue *imm, const char *s,
192 const char *full_s, struct ofpact_learn_spec *spec,
193 struct ofpbuf *ofpacts)
194 {
195 struct mf_subfield dst;
196 char *error;
197
198 error = mf_parse_subfield(&dst, s);
199 if (error) {
200 return error;
201 }
202 if (!mf_nxm_header(dst.field->id)) {
203 return xasprintf("%s: experimenter OXM field '%s' not supported",
204 full_s, s);
205 }
206
207 if (!bitwise_is_all_zeros(imm, sizeof *imm, dst.n_bits,
208 (8 * sizeof *imm) - dst.n_bits)) {
209 return xasprintf("%s: value does not fit into %u bits",
210 full_s, dst.n_bits);
211 }
212
213 spec->n_bits = dst.n_bits;
214 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
215 spec->dst_type = NX_LEARN_DST_LOAD;
216 spec->dst = dst;
217
218 /* Push value last, as this may reallocate 'spec'! */
219 unsigned int n_bytes = DIV_ROUND_UP(dst.n_bits, 8);
220 uint8_t *src_imm = ofpbuf_put_zeros(ofpacts, OFPACT_ALIGN(n_bytes));
221 memcpy(src_imm, &imm->u8[sizeof imm->u8 - n_bytes], n_bytes);
222
223 return NULL;
224 }
225
226 /* Returns NULL if successful, otherwise a malloc()'d string describing the
227 * error. The caller is responsible for freeing the returned string. */
228 static char * OVS_WARN_UNUSED_RESULT
229 learn_parse_spec(const char *orig, char *name, char *value,
230 struct ofpact_learn_spec *spec,
231 struct ofpbuf *ofpacts, struct match *match)
232 {
233 /* Parse destination and check prerequisites. */
234 struct mf_subfield dst;
235 char *error;
236
237 error = mf_parse_subfield(&dst, name);
238 if (!error) {
239 if (!mf_nxm_header(dst.field->id)) {
240 return xasprintf("%s: experimenter OXM field '%s' not supported",
241 orig, name);
242 }
243 spec->dst = dst;
244 spec->n_bits = dst.n_bits;
245 spec->dst_type = NX_LEARN_DST_MATCH;
246
247 /* Parse source and check prerequisites. */
248 if (value[0] != '\0') {
249 struct mf_subfield src;
250 error = mf_parse_subfield(&src, value);
251 if (error) {
252 union mf_value imm;
253 char *imm_error = NULL;
254
255 /* Try an immediate value. */
256 if (dst.ofs == 0 && dst.n_bits == dst.field->n_bits) {
257 /* Full field value. */
258 imm_error = mf_parse_value(dst.field, value, &imm);
259 } else {
260 char *tail;
261 /* Partial field value. */
262 if (parse_int_string(value, (uint8_t *)&imm,
263 dst.field->n_bytes, &tail)
264 || *tail != 0) {
265 imm_error = xasprintf("%s: cannot parse integer value", orig);
266 }
267
268 if (!imm_error &&
269 !bitwise_is_all_zeros(&imm, dst.field->n_bytes,
270 dst.n_bits,
271 dst.field->n_bytes * 8 - dst.n_bits)) {
272 struct ds ds;
273
274 ds_init(&ds);
275 mf_format(dst.field, &imm, NULL, &ds);
276 imm_error = xasprintf("%s: value %s does not fit into %d bits",
277 orig, ds_cstr(&ds), dst.n_bits);
278 ds_destroy(&ds);
279 }
280 }
281 if (imm_error) {
282 char *err = xasprintf("%s: %s value %s cannot be parsed as a subfield (%s) or an immediate value (%s)",
283 orig, name, value, error, imm_error);
284 free(error);
285 free(imm_error);
286 return err;
287 }
288
289 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
290
291 /* Update 'match' to allow for satisfying destination
292 * prerequisites. */
293 mf_write_subfield_value(&dst, &imm, match);
294
295 /* Push value last, as this may reallocate 'spec'! */
296 unsigned int imm_bytes = DIV_ROUND_UP(dst.n_bits, 8);
297 uint8_t *src_imm = ofpbuf_put_zeros(ofpacts,
298 OFPACT_ALIGN(imm_bytes));
299 memcpy(src_imm, &imm, imm_bytes);
300
301 free(error);
302 return NULL;
303 }
304 spec->src = src;
305 if (spec->src.n_bits != spec->dst.n_bits) {
306 return xasprintf("%s: bit widths of %s (%u) and %s (%u) "
307 "differ", orig, name, spec->src.n_bits, value,
308 spec->dst.n_bits);
309 }
310 } else {
311 spec->src = spec->dst;
312 }
313
314 spec->src_type = NX_LEARN_SRC_FIELD;
315 } else if (!strcmp(name, "load")) {
316 union mf_subvalue imm;
317 char *tail;
318 char *dst_value = strstr(value, "->");
319
320 if (dst_value == value) {
321 return xasprintf("%s: missing source before `->' in `%s'", name,
322 value);
323 }
324 if (!dst_value) {
325 return xasprintf("%s: missing `->' in `%s'", name, value);
326 }
327
328 if (!parse_int_string(value, imm.u8, sizeof imm.u8, (char **) &tail)
329 && tail != value) {
330 if (tail != dst_value) {
331 return xasprintf("%s: garbage before `->' in `%s'",
332 name, value);
333 }
334
335 char *error = learn_parse_load_immediate(&imm, dst_value + 2, value, spec,
336 ofpacts);
337 if (error) {
338 return error;
339 }
340 } else {
341 struct ofpact_reg_move move;
342 char *error;
343
344 error = nxm_parse_reg_move(&move, value);
345 if (error) {
346 return error;
347 }
348
349 spec->n_bits = move.src.n_bits;
350 spec->src_type = NX_LEARN_SRC_FIELD;
351 spec->src = move.src;
352 spec->dst_type = NX_LEARN_DST_LOAD;
353 spec->dst = move.dst;
354 }
355 } else if (!strcmp(name, "output")) {
356 char *error = mf_parse_subfield(&spec->src, value);
357 if (error) {
358 return error;
359 }
360
361 spec->n_bits = spec->src.n_bits;
362 spec->src_type = NX_LEARN_SRC_FIELD;
363 spec->dst_type = NX_LEARN_DST_OUTPUT;
364 } else {
365 return xasprintf("%s: unknown keyword %s", orig, name);
366 }
367
368 return NULL;
369 }
370
371 /* Returns NULL if successful, otherwise a malloc()'d string describing the
372 * error. The caller is responsible for freeing the returned string. */
373 static char * OVS_WARN_UNUSED_RESULT
374 learn_parse__(char *orig, char *arg, struct ofpbuf *ofpacts)
375 {
376 struct ofpact_learn *learn;
377 struct match match;
378 char *name, *value;
379
380 learn = ofpact_put_LEARN(ofpacts);
381 learn->idle_timeout = OFP_FLOW_PERMANENT;
382 learn->hard_timeout = OFP_FLOW_PERMANENT;
383 learn->priority = OFP_DEFAULT_PRIORITY;
384 learn->table_id = 1;
385
386 match_init_catchall(&match);
387 while (ofputil_parse_key_value(&arg, &name, &value)) {
388 if (!strcmp(name, "table")) {
389 learn->table_id = atoi(value);
390 if (learn->table_id == 255) {
391 return xasprintf("%s: table id 255 not valid for `learn' "
392 "action", orig);
393 }
394 } else if (!strcmp(name, "priority")) {
395 learn->priority = atoi(value);
396 } else if (!strcmp(name, "idle_timeout")) {
397 learn->idle_timeout = atoi(value);
398 } else if (!strcmp(name, "hard_timeout")) {
399 learn->hard_timeout = atoi(value);
400 } else if (!strcmp(name, "fin_idle_timeout")) {
401 learn->fin_idle_timeout = atoi(value);
402 } else if (!strcmp(name, "fin_hard_timeout")) {
403 learn->fin_hard_timeout = atoi(value);
404 } else if (!strcmp(name, "cookie")) {
405 learn->cookie = htonll(strtoull(value, NULL, 0));
406 } else if (!strcmp(name, "send_flow_rem")) {
407 learn->flags |= NX_LEARN_F_SEND_FLOW_REM;
408 } else if (!strcmp(name, "delete_learned")) {
409 learn->flags |= NX_LEARN_F_DELETE_LEARNED;
410 } else {
411 struct ofpact_learn_spec *spec;
412 char *error;
413
414 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
415 error = learn_parse_spec(orig, name, value, spec, ofpacts, &match);
416 if (error) {
417 return error;
418 }
419 learn = ofpacts->header;
420 }
421 }
422 ofpact_finish_LEARN(ofpacts, &learn);
423
424 return NULL;
425 }
426
427 /* Parses 'arg' as a set of arguments to the "learn" action and appends a
428 * matching OFPACT_LEARN action to 'ofpacts'. ovs-ofctl(8) describes the
429 * format parsed.
430 *
431 * Returns NULL if successful, otherwise a malloc()'d string describing the
432 * error. The caller is responsible for freeing the returned string.
433 *
434 * If 'flow' is nonnull, then it should be the flow from a struct match that is
435 * the matching rule for the learning action. This helps to better validate
436 * the action's arguments.
437 *
438 * Modifies 'arg'. */
439 char * OVS_WARN_UNUSED_RESULT
440 learn_parse(char *arg, struct ofpbuf *ofpacts)
441 {
442 char *orig = xstrdup(arg);
443 char *error = learn_parse__(orig, arg, ofpacts);
444 free(orig);
445 return error;
446 }
447
448 /* Appends a description of 'learn' to 's', in the format that ovs-ofctl(8)
449 * describes. */
450 void
451 learn_format(const struct ofpact_learn *learn, struct ds *s)
452 {
453 const struct ofpact_learn_spec *spec;
454 struct match match;
455
456 match_init_catchall(&match);
457
458 ds_put_format(s, "%slearn(%s%stable=%s%"PRIu8,
459 colors.learn, colors.end, colors.special, colors.end,
460 learn->table_id);
461 if (learn->idle_timeout != OFP_FLOW_PERMANENT) {
462 ds_put_format(s, ",%sidle_timeout=%s%"PRIu16,
463 colors.param, colors.end, learn->idle_timeout);
464 }
465 if (learn->hard_timeout != OFP_FLOW_PERMANENT) {
466 ds_put_format(s, ",%shard_timeout=%s%"PRIu16,
467 colors.param, colors.end, learn->hard_timeout);
468 }
469 if (learn->fin_idle_timeout) {
470 ds_put_format(s, ",%sfin_idle_timeout=%s%"PRIu16,
471 colors.param, colors.end, learn->fin_idle_timeout);
472 }
473 if (learn->fin_hard_timeout) {
474 ds_put_format(s, "%s,fin_hard_timeout=%s%"PRIu16,
475 colors.param, colors.end, learn->fin_hard_timeout);
476 }
477 if (learn->priority != OFP_DEFAULT_PRIORITY) {
478 ds_put_format(s, "%s,priority=%s%"PRIu16,
479 colors.special, colors.end, learn->priority);
480 }
481 if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
482 ds_put_format(s, ",%ssend_flow_rem%s", colors.value, colors.end);
483 }
484 if (learn->flags & NX_LEARN_F_DELETE_LEARNED) {
485 ds_put_format(s, ",%sdelete_learned%s", colors.value, colors.end);
486 }
487 if (learn->cookie != 0) {
488 ds_put_format(s, ",%scookie=%s%#"PRIx64,
489 colors.param, colors.end, ntohll(learn->cookie));
490 }
491
492 OFPACT_LEARN_SPEC_FOR_EACH (spec, learn) {
493 unsigned int n_bytes = DIV_ROUND_UP(spec->n_bits, 8);
494 ds_put_char(s, ',');
495
496 switch (spec->src_type | spec->dst_type) {
497 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH: {
498 if (spec->dst.ofs == 0
499 && spec->dst.n_bits == spec->dst.field->n_bits) {
500 union mf_value value;
501
502 memset(&value, 0, sizeof value);
503 memcpy(&value.b[spec->dst.field->n_bytes - n_bytes],
504 ofpact_learn_spec_imm(spec), n_bytes);
505 ds_put_format(s, "%s%s=%s", colors.param,
506 spec->dst.field->name, colors.end);
507 mf_format(spec->dst.field, &value, NULL, s);
508 } else {
509 ds_put_format(s, "%s", colors.param);
510 mf_format_subfield(&spec->dst, s);
511 ds_put_format(s, "=%s", colors.end);
512 ds_put_hex(s, ofpact_learn_spec_imm(spec), n_bytes);
513 }
514 break;
515 }
516 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
517 ds_put_format(s, "%s", colors.param);
518 mf_format_subfield(&spec->dst, s);
519 ds_put_format(s, "%s", colors.end);
520 if (spec->src.field != spec->dst.field ||
521 spec->src.ofs != spec->dst.ofs) {
522 ds_put_format(s, "%s=%s", colors.param, colors.end);
523 mf_format_subfield(&spec->src, s);
524 }
525 break;
526
527 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
528 ds_put_format(s, "%sload:%s", colors.special, colors.end);
529 ds_put_hex(s, ofpact_learn_spec_imm(spec), n_bytes);
530 ds_put_format(s, "%s->%s", colors.special, colors.end);
531 mf_format_subfield(&spec->dst, s);
532 break;
533
534 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
535 ds_put_format(s, "%sload:%s", colors.special, colors.end);
536 mf_format_subfield(&spec->src, s);
537 ds_put_format(s, "%s->%s", colors.special, colors.end);
538 mf_format_subfield(&spec->dst, s);
539 break;
540
541 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
542 ds_put_format(s, "%soutput:%s", colors.special, colors.end);
543 mf_format_subfield(&spec->src, s);
544 break;
545 }
546 }
547 ds_put_format(s, "%s)%s", colors.learn, colors.end);
548 }