learn: Initialize cookie_mask in constructed flow_mod.
[sliver-openvswitch.git] / lib / learn.c
1 /*
2  * Copyright (c) 2011, 2012 Nicira Networks.
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                 mf_set_subfield(&dst, value, &rule);
181             }
182         }
183     }
184     if (!is_all_zeros(p, (char *) end - (char *) p)) {
185         return OFPERR_OFPBAC_BAD_ARGUMENT;
186     }
187
188     return 0;
189 }
190
191 void
192 learn_execute(const struct nx_action_learn *learn, const struct flow *flow,
193               struct ofputil_flow_mod *fm)
194 {
195     const void *p, *end;
196     struct ofpbuf actions;
197
198     cls_rule_init_catchall(&fm->cr, ntohs(learn->priority));
199     fm->cookie = learn->cookie;
200     fm->cookie_mask = htonll(UINT64_MAX);
201     fm->table_id = learn->table_id;
202     fm->command = OFPFC_MODIFY_STRICT;
203     fm->idle_timeout = ntohs(learn->idle_timeout);
204     fm->hard_timeout = ntohs(learn->hard_timeout);
205     fm->buffer_id = UINT32_MAX;
206     fm->out_port = OFPP_NONE;
207     fm->flags = ntohs(learn->flags) & OFPFF_SEND_FLOW_REM;
208     fm->actions = NULL;
209     fm->n_actions = 0;
210
211     ofpbuf_init(&actions, 64);
212
213     if (learn->fin_idle_timeout || learn->fin_hard_timeout) {
214         struct nx_action_fin_timeout *naft;
215
216         naft = ofputil_put_NXAST_FIN_TIMEOUT(&actions);
217         naft->fin_idle_timeout = learn->fin_idle_timeout;
218         naft->fin_hard_timeout = learn->fin_hard_timeout;
219     }
220
221     for (p = learn + 1, end = (char *) learn + ntohs(learn->len); p != end; ) {
222         uint16_t header = ntohs(get_be16(&p));
223         int n_bits = header & NX_LEARN_N_BITS_MASK;
224         int src_type = header & NX_LEARN_SRC_MASK;
225         int dst_type = header & NX_LEARN_DST_MASK;
226         uint64_t value;
227
228         struct nx_action_reg_load *load;
229         struct mf_subfield dst;
230
231         if (!header) {
232             break;
233         }
234
235         if (src_type == NX_LEARN_SRC_FIELD) {
236             struct mf_subfield src;
237
238             get_subfield(n_bits, &p, &src);
239             value = mf_get_subfield(&src, flow);
240         } else {
241             value = get_bits(n_bits, &p);
242         }
243
244         switch (dst_type) {
245         case NX_LEARN_DST_MATCH:
246             get_subfield(n_bits, &p, &dst);
247             mf_set_subfield(&dst, value, &fm->cr);
248             break;
249
250         case NX_LEARN_DST_LOAD:
251             get_subfield(n_bits, &p, &dst);
252             load = ofputil_put_NXAST_REG_LOAD(&actions);
253             load->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
254             load->dst = htonl(dst.field->nxm_header);
255             load->value = htonll(value);
256             break;
257
258         case NX_LEARN_DST_OUTPUT:
259             ofputil_put_OFPAT10_OUTPUT(&actions)->port = htons(value);
260             break;
261         }
262     }
263
264     fm->actions = ofpbuf_steal_data(&actions);
265     fm->n_actions = actions.size / sizeof(struct ofp_action_header);
266 }
267
268 static void
269 put_be16(struct ofpbuf *b, ovs_be16 x)
270 {
271     ofpbuf_put(b, &x, sizeof x);
272 }
273
274 static void
275 put_be32(struct ofpbuf *b, ovs_be32 x)
276 {
277     ofpbuf_put(b, &x, sizeof x);
278 }
279
280 static void
281 put_u16(struct ofpbuf *b, uint16_t x)
282 {
283     put_be16(b, htons(x));
284 }
285
286 static void
287 put_u32(struct ofpbuf *b, uint32_t x)
288 {
289     put_be32(b, htonl(x));
290 }
291
292 struct learn_spec {
293     int n_bits;
294
295     int src_type;
296     struct mf_subfield src;
297     uint8_t src_imm[sizeof(union mf_value)];
298
299     int dst_type;
300     struct mf_subfield dst;
301 };
302
303 static void
304 learn_parse_spec(const char *orig, char *name, char *value,
305                  struct learn_spec *spec)
306 {
307     memset(spec, 0, sizeof *spec);
308     if (mf_from_name(name)) {
309         const struct mf_field *dst = mf_from_name(name);
310         union mf_value imm;
311         char *error;
312
313         error = mf_parse_value(dst, value, &imm);
314         if (error) {
315             ovs_fatal(0, "%s", error);
316         }
317
318         spec->n_bits = dst->n_bits;
319         spec->src_type = NX_LEARN_SRC_IMMEDIATE;
320         memcpy(spec->src_imm, &imm, dst->n_bytes);
321         spec->dst_type = NX_LEARN_DST_MATCH;
322         spec->dst.field = dst;
323         spec->dst.ofs = 0;
324         spec->dst.n_bits = dst->n_bits;
325     } else if (strchr(name, '[')) {
326         /* Parse destination and check prerequisites. */
327         if (mf_parse_subfield(&spec->dst, name)[0] != '\0') {
328             ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
329                       orig, name);
330         }
331
332         /* Parse source and check prerequisites. */
333         if (value[0] != '\0') {
334             if (mf_parse_subfield(&spec->src, value)[0] != '\0') {
335                 ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
336                           orig, value);
337             }
338             if (spec->src.n_bits != spec->dst.n_bits) {
339                 ovs_fatal(0, "%s: bit widths of %s (%u) and %s (%u) differ",
340                           orig, name, spec->src.n_bits, value,
341                           spec->dst.n_bits);
342             }
343         } else {
344             spec->src = spec->dst;
345         }
346
347         spec->n_bits = spec->src.n_bits;
348         spec->src_type = NX_LEARN_SRC_FIELD;
349         spec->dst_type = NX_LEARN_DST_MATCH;
350     } else if (!strcmp(name, "load")) {
351         if (value[strcspn(value, "[-")] == '-') {
352             struct nx_action_reg_load load;
353             int nbits, imm_bytes;
354             uint64_t imm;
355             int i;
356
357             nxm_parse_reg_load(&load, value);
358             nbits = nxm_decode_n_bits(load.ofs_nbits);
359             imm_bytes = DIV_ROUND_UP(nbits, 8);
360             imm = ntohll(load.value);
361
362             spec->n_bits = nbits;
363             spec->src_type = NX_LEARN_SRC_IMMEDIATE;
364             for (i = 0; i < imm_bytes; i++) {
365                 spec->src_imm[i] = imm >> ((imm_bytes - i - 1) * 8);
366             }
367             spec->dst_type = NX_LEARN_DST_LOAD;
368             nxm_decode(&spec->dst, load.dst, load.ofs_nbits);
369         } else {
370             struct nx_action_reg_move move;
371
372             nxm_parse_reg_move(&move, value);
373
374             spec->n_bits = ntohs(move.n_bits);
375             spec->src_type = NX_LEARN_SRC_FIELD;
376             nxm_decode_discrete(&spec->src,
377                                 move.src, move.src_ofs, move.n_bits);
378             spec->dst_type = NX_LEARN_DST_LOAD;
379             nxm_decode_discrete(&spec->dst,
380                                 move.dst, move.dst_ofs, move.n_bits);
381         }
382     } else if (!strcmp(name, "output")) {
383         if (mf_parse_subfield(&spec->src, value)[0] != '\0') {
384             ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
385                       orig, name);
386         }
387
388         spec->n_bits = spec->src.n_bits;
389         spec->src_type = NX_LEARN_SRC_FIELD;
390         spec->dst_type = NX_LEARN_DST_OUTPUT;
391     } else {
392         ovs_fatal(0, "%s: unknown keyword %s", orig, name);
393     }
394 }
395
396 /* Parses 'arg' as a set of arguments to the "learn" action and appends a
397  * matching NXAST_LEARN action to 'b'.  The format parsed is described in
398  * ovs-ofctl(8).
399  *
400  * Prints an error on stderr and aborts the program if 'arg' syntax is invalid.
401  *
402  * If 'flow' is nonnull, then it should be the flow from a cls_rule that is
403  * the matching rule for the learning action.  This helps to better validate
404  * the action's arguments.
405  *
406  * Modifies 'arg'. */
407 void
408 learn_parse(struct ofpbuf *b, char *arg, const struct flow *flow)
409 {
410     char *orig = xstrdup(arg);
411     char *name, *value;
412     enum ofperr error;
413     size_t learn_ofs;
414     size_t len;
415
416     struct nx_action_learn *learn;
417     struct cls_rule rule;
418
419     learn_ofs = b->size;
420     learn = ofputil_put_NXAST_LEARN(b);
421     learn->idle_timeout = htons(OFP_FLOW_PERMANENT);
422     learn->hard_timeout = htons(OFP_FLOW_PERMANENT);
423     learn->priority = htons(OFP_DEFAULT_PRIORITY);
424     learn->cookie = htonll(0);
425     learn->flags = htons(0);
426     learn->table_id = 1;
427
428     cls_rule_init_catchall(&rule, 0);
429     while (ofputil_parse_key_value(&arg, &name, &value)) {
430         learn = ofpbuf_at_assert(b, learn_ofs, sizeof *learn);
431         if (!strcmp(name, "table")) {
432             learn->table_id = atoi(value);
433             if (learn->table_id == 255) {
434                 ovs_fatal(0, "%s: table id 255 not valid for `learn' action",
435                           orig);
436             }
437         } else if (!strcmp(name, "priority")) {
438             learn->priority = htons(atoi(value));
439         } else if (!strcmp(name, "idle_timeout")) {
440             learn->idle_timeout = htons(atoi(value));
441         } else if (!strcmp(name, "hard_timeout")) {
442             learn->hard_timeout = htons(atoi(value));
443         } else if (!strcmp(name, "fin_idle_timeout")) {
444             learn->fin_idle_timeout = htons(atoi(value));
445         } else if (!strcmp(name, "fin_hard_timeout")) {
446             learn->fin_hard_timeout = htons(atoi(value));
447         } else if (!strcmp(name, "cookie")) {
448             learn->cookie = htonll(strtoull(value, NULL, 0));
449         } else {
450             struct learn_spec spec;
451
452             learn_parse_spec(orig, name, value, &spec);
453
454             /* Check prerequisites. */
455             if (spec.src_type == NX_LEARN_SRC_FIELD
456                 && flow && !mf_are_prereqs_ok(spec.src.field, flow)) {
457                 ovs_fatal(0, "%s: cannot specify source field %s because "
458                           "prerequisites are not satisfied",
459                           orig, spec.src.field->name);
460             }
461             if ((spec.dst_type == NX_LEARN_DST_MATCH
462                  || spec.dst_type == NX_LEARN_DST_LOAD)
463                 && !mf_are_prereqs_ok(spec.dst.field, &rule.flow)) {
464                 ovs_fatal(0, "%s: cannot specify destination field %s because "
465                           "prerequisites are not satisfied",
466                           orig, spec.dst.field->name);
467             }
468
469             /* Update 'rule' to allow for satisfying destination
470              * prerequisites. */
471             if (spec.src_type == NX_LEARN_SRC_IMMEDIATE
472                 && spec.dst_type == NX_LEARN_DST_MATCH
473                 && spec.dst.ofs == 0
474                 && spec.n_bits == spec.dst.field->n_bytes * 8) {
475                 union mf_value imm;
476
477                 memcpy(&imm, spec.src_imm, spec.dst.field->n_bytes);
478                 mf_set_value(spec.dst.field, &imm, &rule);
479             }
480
481             /* Output the flow_mod_spec. */
482             put_u16(b, spec.n_bits | spec.src_type | spec.dst_type);
483             if (spec.src_type == NX_LEARN_SRC_IMMEDIATE) {
484                 int n_bytes = DIV_ROUND_UP(spec.n_bits, 8);
485                 if (n_bytes % 2) {
486                     ofpbuf_put_zeros(b, 1);
487                 }
488                 ofpbuf_put(b, spec.src_imm, n_bytes);
489             } else {
490                 put_u32(b, spec.src.field->nxm_header);
491                 put_u16(b, spec.src.ofs);
492             }
493             if (spec.dst_type == NX_LEARN_DST_MATCH ||
494                 spec.dst_type == NX_LEARN_DST_LOAD) {
495                 put_u32(b, spec.dst.field->nxm_header);
496                 put_u16(b, spec.dst.ofs);
497             } else {
498                 assert(spec.dst_type == NX_LEARN_DST_OUTPUT);
499             }
500         }
501     }
502
503     put_u16(b, 0);
504
505     len = b->size - learn_ofs;
506     if (len % 8) {
507         ofpbuf_put_zeros(b, 8 - len % 8);
508     }
509
510     learn = ofpbuf_at_assert(b, learn_ofs, sizeof *learn);
511     learn->len = htons(b->size - learn_ofs);
512
513     /* In theory the above should have caught any errors, but... */
514     if (flow) {
515         error = learn_check(learn, flow);
516         if (error) {
517             ovs_fatal(0, "%s: %s", orig, ofperr_to_string(error));
518         }
519     }
520     free(orig);
521 }
522
523 void
524 learn_format(const struct nx_action_learn *learn, struct ds *s)
525 {
526     struct cls_rule rule;
527     const void *p, *end;
528
529     cls_rule_init_catchall(&rule, 0);
530
531     ds_put_format(s, "learn(table=%"PRIu8, learn->table_id);
532     if (learn->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
533         ds_put_format(s, ",idle_timeout=%"PRIu16, ntohs(learn->idle_timeout));
534     }
535     if (learn->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
536         ds_put_format(s, ",hard_timeout=%"PRIu16, ntohs(learn->hard_timeout));
537     }
538     if (learn->fin_idle_timeout) {
539         ds_put_format(s, ",fin_idle_timeout=%"PRIu16,
540                       ntohs(learn->fin_idle_timeout));
541     }
542     if (learn->fin_hard_timeout) {
543         ds_put_format(s, ",fin_hard_timeout=%"PRIu16,
544                       ntohs(learn->fin_hard_timeout));
545     }
546     if (learn->priority != htons(OFP_DEFAULT_PRIORITY)) {
547         ds_put_format(s, ",priority=%"PRIu16, ntohs(learn->priority));
548     }
549     if (learn->flags & htons(OFPFF_SEND_FLOW_REM)) {
550         ds_put_cstr(s, ",OFPFF_SEND_FLOW_REM");
551     }
552     if (learn->flags & htons(~OFPFF_SEND_FLOW_REM)) {
553         ds_put_format(s, ",***flags=%"PRIu16"***",
554                       ntohs(learn->flags) & ~OFPFF_SEND_FLOW_REM);
555     }
556     if (learn->cookie != htonll(0)) {
557         ds_put_format(s, ",cookie=0x%"PRIx64, ntohll(learn->cookie));
558     }
559     if (learn->pad != 0) {
560         ds_put_cstr(s, ",***nonzero pad***");
561     }
562
563     end = (char *) learn + ntohs(learn->len);
564     for (p = learn + 1; p != end; ) {
565         uint16_t header = ntohs(get_be16(&p));
566         int n_bits = header & NX_LEARN_N_BITS_MASK;
567
568         int src_type = header & NX_LEARN_SRC_MASK;
569         struct mf_subfield src;
570         const uint8_t *src_value;
571         int src_value_bytes;
572
573         int dst_type = header & NX_LEARN_DST_MASK;
574         struct mf_subfield dst;
575
576         enum ofperr error;
577         int i;
578
579         if (!header) {
580             break;
581         }
582
583         error = learn_check_header(header, (char *) end - (char *) p);
584         if (error == OFPERR_OFPBAC_BAD_ARGUMENT) {
585             ds_put_format(s, ",***bad flow_mod_spec header %"PRIx16"***)",
586                           header);
587             return;
588         } else if (error == OFPERR_OFPBAC_BAD_LEN) {
589             ds_put_format(s, ",***flow_mod_spec at offset %td is %u bytes "
590                           "long but only %td bytes are left***)",
591                           (char *) p - (char *) (learn + 1) - 2,
592                           learn_min_len(header) + 2,
593                           (char *) end - (char *) p + 2);
594             return;
595         }
596         assert(!error);
597
598         /* Get the source. */
599         if (src_type == NX_LEARN_SRC_FIELD) {
600             get_subfield(n_bits, &p, &src);
601             src_value_bytes = 0;
602             src_value = NULL;
603         } else {
604             src.field = NULL;
605             src.ofs = 0;
606             src.n_bits = 0;
607             src_value_bytes = 2 * DIV_ROUND_UP(n_bits, 16);
608             src_value = p;
609             p = (const void *) ((const uint8_t *) p + src_value_bytes);
610         }
611
612         /* Get the destination. */
613         if (dst_type == NX_LEARN_DST_MATCH || dst_type == NX_LEARN_DST_LOAD) {
614             get_subfield(n_bits, &p, &dst);
615         } else {
616             dst.field = NULL;
617             dst.ofs = 0;
618             dst.n_bits = 0;
619         }
620
621         ds_put_char(s, ',');
622
623         switch (src_type | dst_type) {
624         case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH:
625             if (dst.field && dst.ofs == 0 && n_bits == dst.field->n_bits) {
626                 union mf_value value;
627                 uint8_t *bytes = (uint8_t *) &value;
628
629                 if (src_value_bytes > dst.field->n_bytes) {
630                     /* The destination field is an odd number of bytes, which
631                      * got rounded up to a multiple of 2 to be put into the
632                      * learning action.  Skip over the leading byte, which
633                      * should be zero anyway.  Otherwise the memcpy() below
634                      * will overrun the start of 'value'. */
635                     int diff = src_value_bytes - dst.field->n_bytes;
636                     src_value += diff;
637                     src_value_bytes -= diff;
638                 }
639
640                 memset(&value, 0, sizeof value);
641                 memcpy(&bytes[dst.field->n_bytes - src_value_bytes],
642                        src_value, src_value_bytes);
643                 ds_put_format(s, "%s=", dst.field->name);
644                 mf_format(dst.field, &value, NULL, s);
645             } else {
646                 mf_format_subfield(&dst, s);
647                 ds_put_cstr(s, "=0x");
648                 for (i = 0; i < src_value_bytes; i++) {
649                     ds_put_format(s, "%02"PRIx8, src_value[i]);
650                 }
651             }
652             break;
653
654         case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
655             mf_format_subfield(&dst, s);
656             if (src.field != dst.field || src.ofs != dst.ofs) {
657                 ds_put_char(s, '=');
658                 mf_format_subfield(&src, s);
659             }
660             break;
661
662         case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
663             ds_put_cstr(s, "load:0x");
664             for (i = 0; i < src_value_bytes; i++) {
665                 ds_put_format(s, "%02"PRIx8, src_value[i]);
666             }
667             ds_put_cstr(s, "->");
668             mf_format_subfield(&dst, s);
669             break;
670
671         case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
672             ds_put_cstr(s, "load:");
673             mf_format_subfield(&src, s);
674             ds_put_cstr(s, "->");
675             mf_format_subfield(&dst, s);
676             break;
677
678         case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
679             ds_put_cstr(s, "output:");
680             mf_format_subfield(&src, s);
681             break;
682         }
683     }
684     if (!is_all_zeros(p, (char *) end - (char *) p)) {
685         ds_put_cstr(s, ",***nonzero trailer***");
686     }
687     ds_put_char(s, ')');
688 }