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