Add more files to the openvswitch library on MSVC
[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 static ovs_be16
34 get_be16(const void **pp)
35 {
36     const ovs_be16 *p = *pp;
37     ovs_be16 value = *p;
38     *pp = p + 1;
39     return value;
40 }
41
42 static ovs_be32
43 get_be32(const void **pp)
44 {
45     const ovs_be32 *p = *pp;
46     ovs_be32 value = get_unaligned_be32(p);
47     *pp = p + 1;
48     return value;
49 }
50
51 static void
52 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
53 {
54     sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
55     sf->ofs = ntohs(get_be16(p));
56     sf->n_bits = n_bits;
57 }
58
59 static unsigned int
60 learn_min_len(uint16_t header)
61 {
62     int n_bits = header & NX_LEARN_N_BITS_MASK;
63     int src_type = header & NX_LEARN_SRC_MASK;
64     int dst_type = header & NX_LEARN_DST_MASK;
65     unsigned int min_len;
66
67     min_len = 0;
68     if (src_type == NX_LEARN_SRC_FIELD) {
69         min_len += sizeof(ovs_be32); /* src_field */
70         min_len += sizeof(ovs_be16); /* src_ofs */
71     } else {
72         min_len += DIV_ROUND_UP(n_bits, 16);
73     }
74     if (dst_type == NX_LEARN_DST_MATCH ||
75         dst_type == NX_LEARN_DST_LOAD) {
76         min_len += sizeof(ovs_be32); /* dst_field */
77         min_len += sizeof(ovs_be16); /* dst_ofs */
78     }
79     return min_len;
80 }
81
82 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
83  * 'ofpacts'.  Returns 0 if successful, otherwise an OFPERR_*. */
84 enum ofperr
85 learn_from_openflow(const struct nx_action_learn *nal, struct ofpbuf *ofpacts)
86 {
87     struct ofpact_learn *learn;
88     const void *p, *end;
89
90     if (nal->pad) {
91         return OFPERR_OFPBAC_BAD_ARGUMENT;
92     }
93
94     learn = ofpact_put_LEARN(ofpacts);
95
96     learn->idle_timeout = ntohs(nal->idle_timeout);
97     learn->hard_timeout = ntohs(nal->hard_timeout);
98     learn->priority = ntohs(nal->priority);
99     learn->cookie = nal->cookie;
100     learn->table_id = nal->table_id;
101     learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
102     learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
103
104     learn->flags = ntohs(nal->flags);
105     if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
106                          NX_LEARN_F_DELETE_LEARNED)) {
107         return OFPERR_OFPBAC_BAD_ARGUMENT;
108     }
109
110     if (learn->table_id == 0xff) {
111         return OFPERR_OFPBAC_BAD_ARGUMENT;
112     }
113
114     end = (char *) nal + ntohs(nal->len);
115     for (p = nal + 1; p != end; ) {
116         struct ofpact_learn_spec *spec;
117         uint16_t header = ntohs(get_be16(&p));
118
119         if (!header) {
120             break;
121         }
122
123         spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
124         learn = ofpacts->frame;
125         learn->n_specs++;
126
127         spec->src_type = header & NX_LEARN_SRC_MASK;
128         spec->dst_type = header & NX_LEARN_DST_MASK;
129         spec->n_bits = header & NX_LEARN_N_BITS_MASK;
130
131         /* Check for valid src and dst type combination. */
132         if (spec->dst_type == NX_LEARN_DST_MATCH ||
133             spec->dst_type == NX_LEARN_DST_LOAD ||
134             (spec->dst_type == NX_LEARN_DST_OUTPUT &&
135              spec->src_type == NX_LEARN_SRC_FIELD)) {
136             /* OK. */
137         } else {
138             return OFPERR_OFPBAC_BAD_ARGUMENT;
139         }
140
141         /* Check that the arguments don't overrun the end of the action. */
142         if ((char *) end - (char *) p < learn_min_len(header)) {
143             return OFPERR_OFPBAC_BAD_LEN;
144         }
145
146         /* Get the source. */
147         if (spec->src_type == NX_LEARN_SRC_FIELD) {
148             get_subfield(spec->n_bits, &p, &spec->src);
149         } else {
150             int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
151
152             bitwise_copy(p, p_bytes, 0,
153                          &spec->src_imm, sizeof spec->src_imm, 0,
154                          spec->n_bits);
155             p = (const uint8_t *) p + p_bytes;
156         }
157
158         /* Get the destination. */
159         if (spec->dst_type == NX_LEARN_DST_MATCH ||
160             spec->dst_type == NX_LEARN_DST_LOAD) {
161             get_subfield(spec->n_bits, &p, &spec->dst);
162         }
163     }
164     ofpact_update_len(ofpacts, &learn->ofpact);
165
166     if (!is_all_zeros(p, (char *) end - (char *) p)) {
167         return OFPERR_OFPBAC_BAD_ARGUMENT;
168     }
169
170     return 0;
171 }
172
173 /* Checks that 'learn' is a valid action on 'flow'.  Returns 0 if it is valid,
174  * otherwise an OFPERR_*. */
175 enum ofperr
176 learn_check(const struct ofpact_learn *learn, const struct flow *flow)
177 {
178     const struct ofpact_learn_spec *spec;
179     struct match match;
180
181     match_init_catchall(&match);
182     for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
183         enum ofperr error;
184
185         /* Check the source. */
186         if (spec->src_type == NX_LEARN_SRC_FIELD) {
187             error = mf_check_src(&spec->src, flow);
188             if (error) {
189                 return error;
190             }
191         }
192
193         /* Check the destination. */
194         switch (spec->dst_type) {
195         case NX_LEARN_DST_MATCH:
196             error = mf_check_src(&spec->dst, &match.flow);
197             if (error) {
198                 return error;
199             }
200
201             mf_write_subfield(&spec->dst, &spec->src_imm, &match);
202             break;
203
204         case NX_LEARN_DST_LOAD:
205             error = mf_check_dst(&spec->dst, &match.flow);
206             if (error) {
207                 return error;
208             }
209             break;
210
211         case NX_LEARN_DST_OUTPUT:
212             /* Nothing to do. */
213             break;
214         }
215     }
216     return 0;
217 }
218
219 static void
220 put_be16(struct ofpbuf *b, ovs_be16 x)
221 {
222     ofpbuf_put(b, &x, sizeof x);
223 }
224
225 static void
226 put_be32(struct ofpbuf *b, ovs_be32 x)
227 {
228     ofpbuf_put(b, &x, sizeof x);
229 }
230
231 static void
232 put_u16(struct ofpbuf *b, uint16_t x)
233 {
234     put_be16(b, htons(x));
235 }
236
237 static void
238 put_u32(struct ofpbuf *b, uint32_t x)
239 {
240     put_be32(b, htonl(x));
241 }
242
243 /* Converts 'learn' into a "struct nx_action_learn" and appends that action to
244  * 'ofpacts'. */
245 void
246 learn_to_nxast(const struct ofpact_learn *learn, struct ofpbuf *openflow)
247 {
248     const struct ofpact_learn_spec *spec;
249     struct nx_action_learn *nal;
250     size_t start_ofs;
251
252     start_ofs = ofpbuf_size(openflow);
253     nal = ofputil_put_NXAST_LEARN(openflow);
254     nal->idle_timeout = htons(learn->idle_timeout);
255     nal->hard_timeout = htons(learn->hard_timeout);
256     nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
257     nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
258     nal->priority = htons(learn->priority);
259     nal->cookie = learn->cookie;
260     nal->flags = htons(learn->flags);
261     nal->table_id = learn->table_id;
262
263     for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
264         put_u16(openflow, spec->n_bits | spec->dst_type | spec->src_type);
265
266         if (spec->src_type == NX_LEARN_SRC_FIELD) {
267             put_u32(openflow, spec->src.field->nxm_header);
268             put_u16(openflow, spec->src.ofs);
269         } else {
270             size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
271             uint8_t *bits = ofpbuf_put_zeros(openflow, n_dst_bytes);
272             bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
273                          bits, n_dst_bytes, 0,
274                          spec->n_bits);
275         }
276
277         if (spec->dst_type == NX_LEARN_DST_MATCH ||
278             spec->dst_type == NX_LEARN_DST_LOAD) {
279             put_u32(openflow, spec->dst.field->nxm_header);
280             put_u16(openflow, spec->dst.ofs);
281         }
282     }
283
284     if ((ofpbuf_size(openflow) - start_ofs) % 8) {
285         ofpbuf_put_zeros(openflow, 8 - (ofpbuf_size(openflow) - start_ofs) % 8);
286     }
287
288     nal = ofpbuf_at_assert(openflow, start_ofs, sizeof *nal);
289     nal->len = htons(ofpbuf_size(openflow) - start_ofs);
290 }
291
292 /* Composes 'fm' so that executing it will implement 'learn' given that the
293  * packet being processed has 'flow' as its flow.
294  *
295  * Uses 'ofpacts' to store the flow mod's actions.  The caller must initialize
296  * 'ofpacts' and retains ownership of it.  'fm->ofpacts' will point into the
297  * 'ofpacts' buffer.
298  *
299  * The caller has to actually execute 'fm'. */
300 void
301 learn_execute(const struct ofpact_learn *learn, const struct flow *flow,
302               struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
303 {
304     const struct ofpact_learn_spec *spec;
305
306     match_init_catchall(&fm->match);
307     fm->priority = learn->priority;
308     fm->cookie = htonll(0);
309     fm->cookie_mask = htonll(0);
310     fm->new_cookie = learn->cookie;
311     fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
312     fm->table_id = learn->table_id;
313     fm->command = OFPFC_MODIFY_STRICT;
314     fm->idle_timeout = learn->idle_timeout;
315     fm->hard_timeout = learn->hard_timeout;
316     fm->buffer_id = UINT32_MAX;
317     fm->out_port = OFPP_NONE;
318     fm->flags = 0;
319     if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
320         fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
321     }
322     fm->ofpacts = NULL;
323     fm->ofpacts_len = 0;
324     fm->delete_reason = OFPRR_DELETE;
325
326     if (learn->fin_idle_timeout || learn->fin_hard_timeout) {
327         struct ofpact_fin_timeout *oft;
328
329         oft = ofpact_put_FIN_TIMEOUT(ofpacts);
330         oft->fin_idle_timeout = learn->fin_idle_timeout;
331         oft->fin_hard_timeout = learn->fin_hard_timeout;
332     }
333
334     for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
335         union mf_subvalue value;
336         int chunk, ofs;
337
338         if (spec->src_type == NX_LEARN_SRC_FIELD) {
339             mf_read_subfield(&spec->src, flow, &value);
340         } else {
341             value = spec->src_imm;
342         }
343
344         switch (spec->dst_type) {
345         case NX_LEARN_DST_MATCH:
346             mf_write_subfield(&spec->dst, &value, &fm->match);
347             break;
348
349         case NX_LEARN_DST_LOAD:
350             for (ofs = 0; ofs < spec->n_bits; ofs += chunk) {
351                 struct ofpact_reg_load *load;
352
353                 chunk = MIN(spec->n_bits - ofs, 64);
354
355                 load = ofpact_put_REG_LOAD(ofpacts);
356                 load->dst.field = spec->dst.field;
357                 load->dst.ofs = spec->dst.ofs + ofs;
358                 load->dst.n_bits = chunk;
359                 bitwise_copy(&value, sizeof value, ofs,
360                              &load->subvalue, sizeof load->subvalue, 0,
361                              chunk);
362             }
363             break;
364
365         case NX_LEARN_DST_OUTPUT:
366             if (spec->n_bits <= 16
367                 || is_all_zeros(value.u8, sizeof value - 2)) {
368                 ofp_port_t port = u16_to_ofp(ntohs(value.be16[7]));
369
370                 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)
371                     || port == OFPP_IN_PORT
372                     || port == OFPP_FLOOD
373                     || port == OFPP_LOCAL
374                     || port == OFPP_ALL) {
375                     ofpact_put_OUTPUT(ofpacts)->port = port;
376                 }
377             }
378             break;
379         }
380     }
381     ofpact_pad(ofpacts);
382
383     fm->ofpacts = ofpbuf_data(ofpacts);
384     fm->ofpacts_len = ofpbuf_size(ofpacts);
385 }
386
387 /* Perform a bitwise-OR on 'wc''s fields that are relevant as sources in
388  * the learn action 'learn'. */
389 void
390 learn_mask(const struct ofpact_learn *learn, struct flow_wildcards *wc)
391 {
392     const struct ofpact_learn_spec *spec;
393     union mf_subvalue value;
394
395     memset(&value, 0xff, sizeof value);
396     for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
397         if (spec->src_type == NX_LEARN_SRC_FIELD) {
398             mf_write_subfield_flow(&spec->src, &value, &wc->masks);
399         }
400     }
401 }
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 static char * WARN_UNUSED_RESULT
406 learn_parse_load_immediate(const char *s, struct ofpact_learn_spec *spec)
407 {
408     const char *full_s = s;
409     const char *arrow = strstr(s, "->");
410     struct mf_subfield dst;
411     union mf_subvalue imm;
412     char *error;
413
414     memset(&imm, 0, sizeof imm);
415     if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X') && arrow) {
416         const char *in = arrow - 1;
417         uint8_t *out = imm.u8 + sizeof imm.u8 - 1;
418         int n = arrow - (s + 2);
419         int i;
420
421         for (i = 0; i < n; i++) {
422             int hexit = hexit_value(in[-i]);
423             if (hexit < 0) {
424                 return xasprintf("%s: bad hex digit in value", full_s);
425             }
426             out[-(i / 2)] |= i % 2 ? hexit << 4 : hexit;
427         }
428         s = arrow;
429     } else {
430         imm.be64[1] = htonll(strtoull(s, (char **) &s, 0));
431     }
432
433     if (strncmp(s, "->", 2)) {
434         return xasprintf("%s: missing `->' following value", full_s);
435     }
436     s += 2;
437
438     error = mf_parse_subfield(&dst, s);
439     if (error) {
440         return error;
441     }
442
443     if (!bitwise_is_all_zeros(&imm, sizeof imm, dst.n_bits,
444                               (8 * sizeof imm) - dst.n_bits)) {
445         return xasprintf("%s: value does not fit into %u bits",
446                          full_s, dst.n_bits);
447     }
448
449     spec->n_bits = dst.n_bits;
450     spec->src_type = NX_LEARN_SRC_IMMEDIATE;
451     spec->src_imm = imm;
452     spec->dst_type = NX_LEARN_DST_LOAD;
453     spec->dst = dst;
454     return NULL;
455 }
456
457 /* Returns NULL if successful, otherwise a malloc()'d string describing the
458  * error.  The caller is responsible for freeing the returned string. */
459 static char * WARN_UNUSED_RESULT
460 learn_parse_spec(const char *orig, char *name, char *value,
461                  struct ofpact_learn_spec *spec)
462 {
463     if (mf_from_name(name)) {
464         const struct mf_field *dst = mf_from_name(name);
465         union mf_value imm;
466         char *error;
467
468         error = mf_parse_value(dst, value, &imm);
469         if (error) {
470             return error;
471         }
472
473         spec->n_bits = dst->n_bits;
474         spec->src_type = NX_LEARN_SRC_IMMEDIATE;
475         memset(&spec->src_imm, 0, sizeof spec->src_imm);
476         memcpy(&spec->src_imm.u8[sizeof spec->src_imm - dst->n_bytes],
477                &imm, dst->n_bytes);
478         spec->dst_type = NX_LEARN_DST_MATCH;
479         spec->dst.field = dst;
480         spec->dst.ofs = 0;
481         spec->dst.n_bits = dst->n_bits;
482     } else if (strchr(name, '[')) {
483         /* Parse destination and check prerequisites. */
484         char *error;
485
486         error = mf_parse_subfield(&spec->dst, name);
487         if (error) {
488             return error;
489         }
490
491         /* Parse source and check prerequisites. */
492         if (value[0] != '\0') {
493             error = mf_parse_subfield(&spec->src, value);
494             if (error) {
495                 return error;
496             }
497             if (spec->src.n_bits != spec->dst.n_bits) {
498                 return xasprintf("%s: bit widths of %s (%u) and %s (%u) "
499                                  "differ", orig, name, spec->src.n_bits, value,
500                                  spec->dst.n_bits);
501             }
502         } else {
503             spec->src = spec->dst;
504         }
505
506         spec->n_bits = spec->src.n_bits;
507         spec->src_type = NX_LEARN_SRC_FIELD;
508         spec->dst_type = NX_LEARN_DST_MATCH;
509     } else if (!strcmp(name, "load")) {
510         if (value[strcspn(value, "[-")] == '-') {
511             char *error = learn_parse_load_immediate(value, spec);
512             if (error) {
513                 return error;
514             }
515         } else {
516             struct ofpact_reg_move move;
517             char *error;
518
519             error = nxm_parse_reg_move(&move, value);
520             if (error) {
521                 return error;
522             }
523
524             spec->n_bits = move.src.n_bits;
525             spec->src_type = NX_LEARN_SRC_FIELD;
526             spec->src = move.src;
527             spec->dst_type = NX_LEARN_DST_LOAD;
528             spec->dst = move.dst;
529         }
530     } else if (!strcmp(name, "output")) {
531         char *error = mf_parse_subfield(&spec->src, value);
532         if (error) {
533             return error;
534         }
535
536         spec->n_bits = spec->src.n_bits;
537         spec->src_type = NX_LEARN_SRC_FIELD;
538         spec->dst_type = NX_LEARN_DST_OUTPUT;
539     } else {
540         return xasprintf("%s: unknown keyword %s", orig, name);
541     }
542
543     return NULL;
544 }
545
546 /* Returns NULL if successful, otherwise a malloc()'d string describing the
547  * error.  The caller is responsible for freeing the returned string. */
548 static char * WARN_UNUSED_RESULT
549 learn_parse__(char *orig, char *arg, struct ofpbuf *ofpacts)
550 {
551     struct ofpact_learn *learn;
552     struct match match;
553     char *name, *value;
554
555     learn = ofpact_put_LEARN(ofpacts);
556     learn->idle_timeout = OFP_FLOW_PERMANENT;
557     learn->hard_timeout = OFP_FLOW_PERMANENT;
558     learn->priority = OFP_DEFAULT_PRIORITY;
559     learn->table_id = 1;
560
561     match_init_catchall(&match);
562     while (ofputil_parse_key_value(&arg, &name, &value)) {
563         if (!strcmp(name, "table")) {
564             learn->table_id = atoi(value);
565             if (learn->table_id == 255) {
566                 return xasprintf("%s: table id 255 not valid for `learn' "
567                                  "action", orig);
568             }
569         } else if (!strcmp(name, "priority")) {
570             learn->priority = atoi(value);
571         } else if (!strcmp(name, "idle_timeout")) {
572             learn->idle_timeout = atoi(value);
573         } else if (!strcmp(name, "hard_timeout")) {
574             learn->hard_timeout = atoi(value);
575         } else if (!strcmp(name, "fin_idle_timeout")) {
576             learn->fin_idle_timeout = atoi(value);
577         } else if (!strcmp(name, "fin_hard_timeout")) {
578             learn->fin_hard_timeout = atoi(value);
579         } else if (!strcmp(name, "cookie")) {
580             learn->cookie = htonll(strtoull(value, NULL, 0));
581         } else if (!strcmp(name, "send_flow_rem")) {
582             learn->flags |= NX_LEARN_F_SEND_FLOW_REM;
583         } else if (!strcmp(name, "delete_learned")) {
584             learn->flags |= NX_LEARN_F_DELETE_LEARNED;
585         } else {
586             struct ofpact_learn_spec *spec;
587             char *error;
588
589             spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
590             learn = ofpacts->frame;
591             learn->n_specs++;
592
593             error = learn_parse_spec(orig, name, value, spec);
594             if (error) {
595                 return error;
596             }
597
598             /* Update 'match' to allow for satisfying destination
599              * prerequisites. */
600             if (spec->src_type == NX_LEARN_SRC_IMMEDIATE
601                 && spec->dst_type == NX_LEARN_DST_MATCH) {
602                 mf_write_subfield(&spec->dst, &spec->src_imm, &match);
603             }
604         }
605     }
606     ofpact_update_len(ofpacts, &learn->ofpact);
607
608     return NULL;
609 }
610
611 /* Parses 'arg' as a set of arguments to the "learn" action and appends a
612  * matching OFPACT_LEARN action to 'ofpacts'.  ovs-ofctl(8) describes the
613  * format parsed.
614  *
615  * Returns NULL if successful, otherwise a malloc()'d string describing the
616  * error.  The caller is responsible for freeing the returned string.
617  *
618  * If 'flow' is nonnull, then it should be the flow from a struct match that is
619  * the matching rule for the learning action.  This helps to better validate
620  * the action's arguments.
621  *
622  * Modifies 'arg'. */
623 char * WARN_UNUSED_RESULT
624 learn_parse(char *arg, struct ofpbuf *ofpacts)
625 {
626     char *orig = xstrdup(arg);
627     char *error = learn_parse__(orig, arg, ofpacts);
628     free(orig);
629     return error;
630 }
631
632 /* Appends a description of 'learn' to 's', in the format that ovs-ofctl(8)
633  * describes. */
634 void
635 learn_format(const struct ofpact_learn *learn, struct ds *s)
636 {
637     const struct ofpact_learn_spec *spec;
638     struct match match;
639
640     match_init_catchall(&match);
641
642     ds_put_format(s, "learn(table=%"PRIu8, learn->table_id);
643     if (learn->idle_timeout != OFP_FLOW_PERMANENT) {
644         ds_put_format(s, ",idle_timeout=%"PRIu16, learn->idle_timeout);
645     }
646     if (learn->hard_timeout != OFP_FLOW_PERMANENT) {
647         ds_put_format(s, ",hard_timeout=%"PRIu16, learn->hard_timeout);
648     }
649     if (learn->fin_idle_timeout) {
650         ds_put_format(s, ",fin_idle_timeout=%"PRIu16, learn->fin_idle_timeout);
651     }
652     if (learn->fin_hard_timeout) {
653         ds_put_format(s, ",fin_hard_timeout=%"PRIu16, learn->fin_hard_timeout);
654     }
655     if (learn->priority != OFP_DEFAULT_PRIORITY) {
656         ds_put_format(s, ",priority=%"PRIu16, learn->priority);
657     }
658     if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
659         ds_put_cstr(s, ",send_flow_rem");
660     }
661     if (learn->flags & NX_LEARN_F_DELETE_LEARNED) {
662         ds_put_cstr(s, ",delete_learned");
663     }
664     if (learn->cookie != 0) {
665         ds_put_format(s, ",cookie=%#"PRIx64, ntohll(learn->cookie));
666     }
667
668     for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
669         ds_put_char(s, ',');
670
671         switch (spec->src_type | spec->dst_type) {
672         case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH:
673             if (spec->dst.ofs == 0
674                 && spec->dst.n_bits == spec->dst.field->n_bits) {
675                 union mf_value value;
676
677                 memset(&value, 0, sizeof value);
678                 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
679                              &value, spec->dst.field->n_bytes, 0,
680                              spec->dst.field->n_bits);
681                 ds_put_format(s, "%s=", spec->dst.field->name);
682                 mf_format(spec->dst.field, &value, NULL, s);
683             } else {
684                 mf_format_subfield(&spec->dst, s);
685                 ds_put_char(s, '=');
686                 mf_format_subvalue(&spec->src_imm, s);
687             }
688             break;
689
690         case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
691             mf_format_subfield(&spec->dst, s);
692             if (spec->src.field != spec->dst.field ||
693                 spec->src.ofs != spec->dst.ofs) {
694                 ds_put_char(s, '=');
695                 mf_format_subfield(&spec->src, s);
696             }
697             break;
698
699         case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
700             ds_put_format(s, "load:");
701             mf_format_subvalue(&spec->src_imm, s);
702             ds_put_cstr(s, "->");
703             mf_format_subfield(&spec->dst, s);
704             break;
705
706         case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
707             ds_put_cstr(s, "load:");
708             mf_format_subfield(&spec->src, s);
709             ds_put_cstr(s, "->");
710             mf_format_subfield(&spec->dst, s);
711             break;
712
713         case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
714             ds_put_cstr(s, "output:");
715             mf_format_subfield(&spec->src, s);
716             break;
717         }
718     }
719     ds_put_char(s, ')');
720 }