ofp-actions: Centralize all OpenFlow action code for maintainability.
[cascardo/ovs.git] / lib / learn.c
1 /*
2  * Copyright (c) 2011, 2012, 2013, 2014 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->buffer_id = UINT32_MAX;
105     fm->out_port = OFPP_NONE;
106     fm->flags = 0;
107     if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
108         fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
109     }
110     fm->ofpacts = NULL;
111     fm->ofpacts_len = 0;
112     fm->delete_reason = OFPRR_DELETE;
113
114     if (learn->fin_idle_timeout || learn->fin_hard_timeout) {
115         struct ofpact_fin_timeout *oft;
116
117         oft = ofpact_put_FIN_TIMEOUT(ofpacts);
118         oft->fin_idle_timeout = learn->fin_idle_timeout;
119         oft->fin_hard_timeout = learn->fin_hard_timeout;
120     }
121
122     for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
123         union mf_subvalue value;
124         int chunk, ofs;
125
126         if (spec->src_type == NX_LEARN_SRC_FIELD) {
127             mf_read_subfield(&spec->src, flow, &value);
128         } else {
129             value = spec->src_imm;
130         }
131
132         switch (spec->dst_type) {
133         case NX_LEARN_DST_MATCH:
134             mf_write_subfield(&spec->dst, &value, &fm->match);
135             break;
136
137         case NX_LEARN_DST_LOAD:
138             for (ofs = 0; ofs < spec->n_bits; ofs += chunk) {
139                 struct ofpact_reg_load *load;
140
141                 chunk = MIN(spec->n_bits - ofs, 64);
142
143                 load = ofpact_put_REG_LOAD(ofpacts);
144                 load->dst.field = spec->dst.field;
145                 load->dst.ofs = spec->dst.ofs + ofs;
146                 load->dst.n_bits = chunk;
147                 bitwise_copy(&value, sizeof value, ofs,
148                              &load->subvalue, sizeof load->subvalue, 0,
149                              chunk);
150             }
151             break;
152
153         case NX_LEARN_DST_OUTPUT:
154             if (spec->n_bits <= 16
155                 || is_all_zeros(value.u8, sizeof value - 2)) {
156                 ofp_port_t port = u16_to_ofp(ntohs(value.be16[7]));
157
158                 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)
159                     || port == OFPP_IN_PORT
160                     || port == OFPP_FLOOD
161                     || port == OFPP_LOCAL
162                     || port == OFPP_ALL) {
163                     ofpact_put_OUTPUT(ofpacts)->port = port;
164                 }
165             }
166             break;
167         }
168     }
169     ofpact_pad(ofpacts);
170
171     fm->ofpacts = ofpbuf_data(ofpacts);
172     fm->ofpacts_len = ofpbuf_size(ofpacts);
173 }
174
175 /* Perform a bitwise-OR on 'wc''s fields that are relevant as sources in
176  * the learn action 'learn'. */
177 void
178 learn_mask(const struct ofpact_learn *learn, struct flow_wildcards *wc)
179 {
180     const struct ofpact_learn_spec *spec;
181     union mf_subvalue value;
182
183     memset(&value, 0xff, sizeof value);
184     for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
185         if (spec->src_type == NX_LEARN_SRC_FIELD) {
186             mf_write_subfield_flow(&spec->src, &value, &wc->masks);
187         }
188     }
189 }
190
191 /* Returns NULL if successful, otherwise a malloc()'d string describing the
192  * error.  The caller is responsible for freeing the returned string. */
193 static char * WARN_UNUSED_RESULT
194 learn_parse_load_immediate(const char *s, struct ofpact_learn_spec *spec)
195 {
196     const char *full_s = s;
197     const char *arrow = strstr(s, "->");
198     struct mf_subfield dst;
199     union mf_subvalue imm;
200     char *error;
201
202     memset(&imm, 0, sizeof imm);
203     if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X') && arrow) {
204         const char *in = arrow - 1;
205         uint8_t *out = imm.u8 + sizeof imm.u8 - 1;
206         int n = arrow - (s + 2);
207         int i;
208
209         for (i = 0; i < n; i++) {
210             int hexit = hexit_value(in[-i]);
211             if (hexit < 0) {
212                 return xasprintf("%s: bad hex digit in value", full_s);
213             }
214             out[-(i / 2)] |= i % 2 ? hexit << 4 : hexit;
215         }
216         s = arrow;
217     } else {
218         imm.be64[1] = htonll(strtoull(s, (char **) &s, 0));
219     }
220
221     if (strncmp(s, "->", 2)) {
222         return xasprintf("%s: missing `->' following value", full_s);
223     }
224     s += 2;
225
226     error = mf_parse_subfield(&dst, s);
227     if (error) {
228         return error;
229     }
230
231     if (!bitwise_is_all_zeros(&imm, sizeof imm, dst.n_bits,
232                               (8 * sizeof imm) - dst.n_bits)) {
233         return xasprintf("%s: value does not fit into %u bits",
234                          full_s, dst.n_bits);
235     }
236
237     spec->n_bits = dst.n_bits;
238     spec->src_type = NX_LEARN_SRC_IMMEDIATE;
239     spec->src_imm = imm;
240     spec->dst_type = NX_LEARN_DST_LOAD;
241     spec->dst = dst;
242     return NULL;
243 }
244
245 /* Returns NULL if successful, otherwise a malloc()'d string describing the
246  * error.  The caller is responsible for freeing the returned string. */
247 static char * WARN_UNUSED_RESULT
248 learn_parse_spec(const char *orig, char *name, char *value,
249                  struct ofpact_learn_spec *spec)
250 {
251     if (mf_from_name(name)) {
252         const struct mf_field *dst = mf_from_name(name);
253         union mf_value imm;
254         char *error;
255
256         error = mf_parse_value(dst, value, &imm);
257         if (error) {
258             return error;
259         }
260
261         spec->n_bits = dst->n_bits;
262         spec->src_type = NX_LEARN_SRC_IMMEDIATE;
263         memset(&spec->src_imm, 0, sizeof spec->src_imm);
264         memcpy(&spec->src_imm.u8[sizeof spec->src_imm - dst->n_bytes],
265                &imm, dst->n_bytes);
266         spec->dst_type = NX_LEARN_DST_MATCH;
267         spec->dst.field = dst;
268         spec->dst.ofs = 0;
269         spec->dst.n_bits = dst->n_bits;
270     } else if (strchr(name, '[')) {
271         /* Parse destination and check prerequisites. */
272         char *error;
273
274         error = mf_parse_subfield(&spec->dst, name);
275         if (error) {
276             return error;
277         }
278
279         /* Parse source and check prerequisites. */
280         if (value[0] != '\0') {
281             error = mf_parse_subfield(&spec->src, value);
282             if (error) {
283                 return error;
284             }
285             if (spec->src.n_bits != spec->dst.n_bits) {
286                 return xasprintf("%s: bit widths of %s (%u) and %s (%u) "
287                                  "differ", orig, name, spec->src.n_bits, value,
288                                  spec->dst.n_bits);
289             }
290         } else {
291             spec->src = spec->dst;
292         }
293
294         spec->n_bits = spec->src.n_bits;
295         spec->src_type = NX_LEARN_SRC_FIELD;
296         spec->dst_type = NX_LEARN_DST_MATCH;
297     } else if (!strcmp(name, "load")) {
298         if (value[strcspn(value, "[-")] == '-') {
299             char *error = learn_parse_load_immediate(value, spec);
300             if (error) {
301                 return error;
302             }
303         } else {
304             struct ofpact_reg_move move;
305             char *error;
306
307             error = nxm_parse_reg_move(&move, value);
308             if (error) {
309                 return error;
310             }
311
312             spec->n_bits = move.src.n_bits;
313             spec->src_type = NX_LEARN_SRC_FIELD;
314             spec->src = move.src;
315             spec->dst_type = NX_LEARN_DST_LOAD;
316             spec->dst = move.dst;
317         }
318     } else if (!strcmp(name, "output")) {
319         char *error = mf_parse_subfield(&spec->src, value);
320         if (error) {
321             return error;
322         }
323
324         spec->n_bits = spec->src.n_bits;
325         spec->src_type = NX_LEARN_SRC_FIELD;
326         spec->dst_type = NX_LEARN_DST_OUTPUT;
327     } else {
328         return xasprintf("%s: unknown keyword %s", orig, name);
329     }
330
331     return NULL;
332 }
333
334 /* Returns NULL if successful, otherwise a malloc()'d string describing the
335  * error.  The caller is responsible for freeing the returned string. */
336 static char * WARN_UNUSED_RESULT
337 learn_parse__(char *orig, char *arg, struct ofpbuf *ofpacts)
338 {
339     struct ofpact_learn *learn;
340     struct match match;
341     char *name, *value;
342
343     learn = ofpact_put_LEARN(ofpacts);
344     learn->idle_timeout = OFP_FLOW_PERMANENT;
345     learn->hard_timeout = OFP_FLOW_PERMANENT;
346     learn->priority = OFP_DEFAULT_PRIORITY;
347     learn->table_id = 1;
348
349     match_init_catchall(&match);
350     while (ofputil_parse_key_value(&arg, &name, &value)) {
351         if (!strcmp(name, "table")) {
352             learn->table_id = atoi(value);
353             if (learn->table_id == 255) {
354                 return xasprintf("%s: table id 255 not valid for `learn' "
355                                  "action", orig);
356             }
357         } else if (!strcmp(name, "priority")) {
358             learn->priority = atoi(value);
359         } else if (!strcmp(name, "idle_timeout")) {
360             learn->idle_timeout = atoi(value);
361         } else if (!strcmp(name, "hard_timeout")) {
362             learn->hard_timeout = atoi(value);
363         } else if (!strcmp(name, "fin_idle_timeout")) {
364             learn->fin_idle_timeout = atoi(value);
365         } else if (!strcmp(name, "fin_hard_timeout")) {
366             learn->fin_hard_timeout = atoi(value);
367         } else if (!strcmp(name, "cookie")) {
368             learn->cookie = htonll(strtoull(value, NULL, 0));
369         } else if (!strcmp(name, "send_flow_rem")) {
370             learn->flags |= NX_LEARN_F_SEND_FLOW_REM;
371         } else if (!strcmp(name, "delete_learned")) {
372             learn->flags |= NX_LEARN_F_DELETE_LEARNED;
373         } else {
374             struct ofpact_learn_spec *spec;
375             char *error;
376
377             spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
378             learn = ofpacts->frame;
379             learn->n_specs++;
380
381             error = learn_parse_spec(orig, name, value, spec);
382             if (error) {
383                 return error;
384             }
385
386             /* Update 'match' to allow for satisfying destination
387              * prerequisites. */
388             if (spec->src_type == NX_LEARN_SRC_IMMEDIATE
389                 && spec->dst_type == NX_LEARN_DST_MATCH) {
390                 mf_write_subfield(&spec->dst, &spec->src_imm, &match);
391             }
392         }
393     }
394     ofpact_update_len(ofpacts, &learn->ofpact);
395
396     return NULL;
397 }
398
399 /* Parses 'arg' as a set of arguments to the "learn" action and appends a
400  * matching OFPACT_LEARN action to 'ofpacts'.  ovs-ofctl(8) describes the
401  * format parsed.
402  *
403  * Returns NULL if successful, otherwise a malloc()'d string describing the
404  * error.  The caller is responsible for freeing the returned string.
405  *
406  * If 'flow' is nonnull, then it should be the flow from a struct match that is
407  * the matching rule for the learning action.  This helps to better validate
408  * the action's arguments.
409  *
410  * Modifies 'arg'. */
411 char * WARN_UNUSED_RESULT
412 learn_parse(char *arg, struct ofpbuf *ofpacts)
413 {
414     char *orig = xstrdup(arg);
415     char *error = learn_parse__(orig, arg, ofpacts);
416     free(orig);
417     return error;
418 }
419
420 /* Appends a description of 'learn' to 's', in the format that ovs-ofctl(8)
421  * describes. */
422 void
423 learn_format(const struct ofpact_learn *learn, struct ds *s)
424 {
425     const struct ofpact_learn_spec *spec;
426     struct match match;
427
428     match_init_catchall(&match);
429
430     ds_put_format(s, "learn(table=%"PRIu8, learn->table_id);
431     if (learn->idle_timeout != OFP_FLOW_PERMANENT) {
432         ds_put_format(s, ",idle_timeout=%"PRIu16, learn->idle_timeout);
433     }
434     if (learn->hard_timeout != OFP_FLOW_PERMANENT) {
435         ds_put_format(s, ",hard_timeout=%"PRIu16, learn->hard_timeout);
436     }
437     if (learn->fin_idle_timeout) {
438         ds_put_format(s, ",fin_idle_timeout=%"PRIu16, learn->fin_idle_timeout);
439     }
440     if (learn->fin_hard_timeout) {
441         ds_put_format(s, ",fin_hard_timeout=%"PRIu16, learn->fin_hard_timeout);
442     }
443     if (learn->priority != OFP_DEFAULT_PRIORITY) {
444         ds_put_format(s, ",priority=%"PRIu16, learn->priority);
445     }
446     if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
447         ds_put_cstr(s, ",send_flow_rem");
448     }
449     if (learn->flags & NX_LEARN_F_DELETE_LEARNED) {
450         ds_put_cstr(s, ",delete_learned");
451     }
452     if (learn->cookie != 0) {
453         ds_put_format(s, ",cookie=%#"PRIx64, ntohll(learn->cookie));
454     }
455
456     for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
457         ds_put_char(s, ',');
458
459         switch (spec->src_type | spec->dst_type) {
460         case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH:
461             if (spec->dst.ofs == 0
462                 && spec->dst.n_bits == spec->dst.field->n_bits) {
463                 union mf_value value;
464
465                 memset(&value, 0, sizeof value);
466                 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
467                              &value, spec->dst.field->n_bytes, 0,
468                              spec->dst.field->n_bits);
469                 ds_put_format(s, "%s=", spec->dst.field->name);
470                 mf_format(spec->dst.field, &value, NULL, s);
471             } else {
472                 mf_format_subfield(&spec->dst, s);
473                 ds_put_char(s, '=');
474                 mf_format_subvalue(&spec->src_imm, s);
475             }
476             break;
477
478         case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
479             mf_format_subfield(&spec->dst, s);
480             if (spec->src.field != spec->dst.field ||
481                 spec->src.ofs != spec->dst.ofs) {
482                 ds_put_char(s, '=');
483                 mf_format_subfield(&spec->src, s);
484             }
485             break;
486
487         case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
488             ds_put_format(s, "load:");
489             mf_format_subvalue(&spec->src_imm, s);
490             ds_put_cstr(s, "->");
491             mf_format_subfield(&spec->dst, s);
492             break;
493
494         case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
495             ds_put_cstr(s, "load:");
496             mf_format_subfield(&spec->src, s);
497             ds_put_cstr(s, "->");
498             mf_format_subfield(&spec->dst, s);
499             break;
500
501         case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
502             ds_put_cstr(s, "output:");
503             mf_format_subfield(&spec->src, s);
504             break;
505         }
506     }
507     ds_put_char(s, ')');
508 }