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