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