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