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