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