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