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