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