Merge "master" into "ovn".
[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->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     ofpact_pad(ofpacts);
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     const char *arrow = strstr(s, "->");
193     struct mf_subfield dst;
194     union mf_subvalue imm;
195     char *error;
196
197     memset(&imm, 0, sizeof imm);
198     if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X') && arrow) {
199         const char *in = arrow - 1;
200         uint8_t *out = imm.u8 + sizeof imm.u8 - 1;
201         int n = arrow - (s + 2);
202         int i;
203
204         for (i = 0; i < n; i++) {
205             int hexit = hexit_value(in[-i]);
206             if (hexit < 0) {
207                 return xasprintf("%s: bad hex digit in value", full_s);
208             }
209             out[-(i / 2)] |= i % 2 ? hexit << 4 : hexit;
210         }
211         s = arrow;
212     } else {
213         imm.integer = htonll(strtoull(s, (char **) &s, 0));
214     }
215
216     if (strncmp(s, "->", 2)) {
217         return xasprintf("%s: missing `->' following value", full_s);
218     }
219     s += 2;
220
221     error = mf_parse_subfield(&dst, s);
222     if (error) {
223         return error;
224     }
225     if (!mf_nxm_header(dst.field->id)) {
226         return xasprintf("%s: experimenter OXM field '%s' not supported",
227                          full_s, s);
228     }
229
230     if (!bitwise_is_all_zeros(&imm, sizeof imm, dst.n_bits,
231                               (8 * sizeof imm) - dst.n_bits)) {
232         return xasprintf("%s: value does not fit into %u bits",
233                          full_s, dst.n_bits);
234     }
235
236     spec->n_bits = dst.n_bits;
237     spec->src_type = NX_LEARN_SRC_IMMEDIATE;
238     spec->src_imm = imm;
239     spec->dst_type = NX_LEARN_DST_LOAD;
240     spec->dst = dst;
241     return NULL;
242 }
243
244 /* Returns NULL if successful, otherwise a malloc()'d string describing the
245  * error.  The caller is responsible for freeing the returned string. */
246 static char * OVS_WARN_UNUSED_RESULT
247 learn_parse_spec(const char *orig, char *name, char *value,
248                  struct ofpact_learn_spec *spec)
249 {
250     if (mf_from_name(name)) {
251         const struct mf_field *dst = mf_from_name(name);
252         union mf_value imm;
253         char *error;
254
255         error = mf_parse_value(dst, value, &imm);
256         if (error) {
257             return error;
258         }
259
260         spec->n_bits = dst->n_bits;
261         spec->src_type = NX_LEARN_SRC_IMMEDIATE;
262         memset(&spec->src_imm, 0, sizeof spec->src_imm);
263         memcpy(&spec->src_imm.u8[sizeof spec->src_imm - dst->n_bytes],
264                &imm, dst->n_bytes);
265         spec->dst_type = NX_LEARN_DST_MATCH;
266         spec->dst.field = dst;
267         spec->dst.ofs = 0;
268         spec->dst.n_bits = dst->n_bits;
269     } else if (strchr(name, '[')) {
270         /* Parse destination and check prerequisites. */
271         char *error;
272
273         error = mf_parse_subfield(&spec->dst, name);
274         if (error) {
275             return error;
276         }
277         if (!mf_nxm_header(spec->dst.field->id)) {
278             return xasprintf("%s: experimenter OXM field '%s' not supported",
279                              orig, name);
280         }
281
282         /* Parse source and check prerequisites. */
283         if (value[0] != '\0') {
284             error = mf_parse_subfield(&spec->src, value);
285             if (error) {
286                 return error;
287             }
288             if (spec->src.n_bits != spec->dst.n_bits) {
289                 return xasprintf("%s: bit widths of %s (%u) and %s (%u) "
290                                  "differ", orig, name, spec->src.n_bits, value,
291                                  spec->dst.n_bits);
292             }
293         } else {
294             spec->src = spec->dst;
295         }
296
297         spec->n_bits = spec->src.n_bits;
298         spec->src_type = NX_LEARN_SRC_FIELD;
299         spec->dst_type = NX_LEARN_DST_MATCH;
300     } else if (!strcmp(name, "load")) {
301         if (value[strcspn(value, "[-")] == '-') {
302             char *error = learn_parse_load_immediate(value, spec);
303             if (error) {
304                 return error;
305             }
306         } else {
307             struct ofpact_reg_move move;
308             char *error;
309
310             error = nxm_parse_reg_move(&move, value);
311             if (error) {
312                 return error;
313             }
314
315             spec->n_bits = move.src.n_bits;
316             spec->src_type = NX_LEARN_SRC_FIELD;
317             spec->src = move.src;
318             spec->dst_type = NX_LEARN_DST_LOAD;
319             spec->dst = move.dst;
320         }
321     } else if (!strcmp(name, "output")) {
322         char *error = mf_parse_subfield(&spec->src, value);
323         if (error) {
324             return error;
325         }
326
327         spec->n_bits = spec->src.n_bits;
328         spec->src_type = NX_LEARN_SRC_FIELD;
329         spec->dst_type = NX_LEARN_DST_OUTPUT;
330     } else {
331         return xasprintf("%s: unknown keyword %s", orig, name);
332     }
333
334     return NULL;
335 }
336
337 /* Returns NULL if successful, otherwise a malloc()'d string describing the
338  * error.  The caller is responsible for freeing the returned string. */
339 static char * OVS_WARN_UNUSED_RESULT
340 learn_parse__(char *orig, char *arg, struct ofpbuf *ofpacts)
341 {
342     struct ofpact_learn *learn;
343     struct match match;
344     char *name, *value;
345
346     learn = ofpact_put_LEARN(ofpacts);
347     learn->idle_timeout = OFP_FLOW_PERMANENT;
348     learn->hard_timeout = OFP_FLOW_PERMANENT;
349     learn->priority = OFP_DEFAULT_PRIORITY;
350     learn->table_id = 1;
351
352     match_init_catchall(&match);
353     while (ofputil_parse_key_value(&arg, &name, &value)) {
354         if (!strcmp(name, "table")) {
355             learn->table_id = atoi(value);
356             if (learn->table_id == 255) {
357                 return xasprintf("%s: table id 255 not valid for `learn' "
358                                  "action", orig);
359             }
360         } else if (!strcmp(name, "priority")) {
361             learn->priority = atoi(value);
362         } else if (!strcmp(name, "idle_timeout")) {
363             learn->idle_timeout = atoi(value);
364         } else if (!strcmp(name, "hard_timeout")) {
365             learn->hard_timeout = atoi(value);
366         } else if (!strcmp(name, "fin_idle_timeout")) {
367             learn->fin_idle_timeout = atoi(value);
368         } else if (!strcmp(name, "fin_hard_timeout")) {
369             learn->fin_hard_timeout = atoi(value);
370         } else if (!strcmp(name, "cookie")) {
371             learn->cookie = htonll(strtoull(value, NULL, 0));
372         } else if (!strcmp(name, "send_flow_rem")) {
373             learn->flags |= NX_LEARN_F_SEND_FLOW_REM;
374         } else if (!strcmp(name, "delete_learned")) {
375             learn->flags |= NX_LEARN_F_DELETE_LEARNED;
376         } else {
377             struct ofpact_learn_spec *spec;
378             char *error;
379
380             spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
381             learn = ofpacts->header;
382             learn->n_specs++;
383
384             error = learn_parse_spec(orig, name, value, spec);
385             if (error) {
386                 return error;
387             }
388
389             /* Update 'match' to allow for satisfying destination
390              * prerequisites. */
391             if (spec->src_type == NX_LEARN_SRC_IMMEDIATE
392                 && spec->dst_type == NX_LEARN_DST_MATCH) {
393                 mf_write_subfield(&spec->dst, &spec->src_imm, &match);
394             }
395         }
396     }
397     ofpact_update_len(ofpacts, &learn->ofpact);
398
399     return NULL;
400 }
401
402 /* Parses 'arg' as a set of arguments to the "learn" action and appends a
403  * matching OFPACT_LEARN action to 'ofpacts'.  ovs-ofctl(8) describes the
404  * format parsed.
405  *
406  * Returns NULL if successful, otherwise a malloc()'d string describing the
407  * error.  The caller is responsible for freeing the returned string.
408  *
409  * If 'flow' is nonnull, then it should be the flow from a struct match that is
410  * the matching rule for the learning action.  This helps to better validate
411  * the action's arguments.
412  *
413  * Modifies 'arg'. */
414 char * OVS_WARN_UNUSED_RESULT
415 learn_parse(char *arg, struct ofpbuf *ofpacts)
416 {
417     char *orig = xstrdup(arg);
418     char *error = learn_parse__(orig, arg, ofpacts);
419     free(orig);
420     return error;
421 }
422
423 /* Appends a description of 'learn' to 's', in the format that ovs-ofctl(8)
424  * describes. */
425 void
426 learn_format(const struct ofpact_learn *learn, struct ds *s)
427 {
428     const struct ofpact_learn_spec *spec;
429     struct match match;
430
431     match_init_catchall(&match);
432
433     ds_put_format(s, "learn(table=%"PRIu8, learn->table_id);
434     if (learn->idle_timeout != OFP_FLOW_PERMANENT) {
435         ds_put_format(s, ",idle_timeout=%"PRIu16, learn->idle_timeout);
436     }
437     if (learn->hard_timeout != OFP_FLOW_PERMANENT) {
438         ds_put_format(s, ",hard_timeout=%"PRIu16, learn->hard_timeout);
439     }
440     if (learn->fin_idle_timeout) {
441         ds_put_format(s, ",fin_idle_timeout=%"PRIu16, learn->fin_idle_timeout);
442     }
443     if (learn->fin_hard_timeout) {
444         ds_put_format(s, ",fin_hard_timeout=%"PRIu16, learn->fin_hard_timeout);
445     }
446     if (learn->priority != OFP_DEFAULT_PRIORITY) {
447         ds_put_format(s, ",priority=%"PRIu16, learn->priority);
448     }
449     if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
450         ds_put_cstr(s, ",send_flow_rem");
451     }
452     if (learn->flags & NX_LEARN_F_DELETE_LEARNED) {
453         ds_put_cstr(s, ",delete_learned");
454     }
455     if (learn->cookie != 0) {
456         ds_put_format(s, ",cookie=%#"PRIx64, ntohll(learn->cookie));
457     }
458
459     for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
460         ds_put_char(s, ',');
461
462         switch (spec->src_type | spec->dst_type) {
463         case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH:
464             if (spec->dst.ofs == 0
465                 && spec->dst.n_bits == spec->dst.field->n_bits) {
466                 union mf_value value;
467
468                 memset(&value, 0, sizeof value);
469                 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
470                              &value, spec->dst.field->n_bytes, 0,
471                              spec->dst.field->n_bits);
472                 ds_put_format(s, "%s=", spec->dst.field->name);
473                 mf_format(spec->dst.field, &value, NULL, s);
474             } else {
475                 mf_format_subfield(&spec->dst, s);
476                 ds_put_char(s, '=');
477                 mf_format_subvalue(&spec->src_imm, s);
478             }
479             break;
480
481         case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
482             mf_format_subfield(&spec->dst, s);
483             if (spec->src.field != spec->dst.field ||
484                 spec->src.ofs != spec->dst.ofs) {
485                 ds_put_char(s, '=');
486                 mf_format_subfield(&spec->src, s);
487             }
488             break;
489
490         case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
491             ds_put_format(s, "load:");
492             mf_format_subvalue(&spec->src_imm, s);
493             ds_put_cstr(s, "->");
494             mf_format_subfield(&spec->dst, s);
495             break;
496
497         case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
498             ds_put_cstr(s, "load:");
499             mf_format_subfield(&spec->src, s);
500             ds_put_cstr(s, "->");
501             mf_format_subfield(&spec->dst, s);
502             break;
503
504         case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
505             ds_put_cstr(s, "output:");
506             mf_format_subfield(&spec->src, s);
507             break;
508         }
509     }
510     ds_put_char(s, ')');
511 }