Coverage Report

Created: 2025-07-11 06:12

/src/openvswitch/lib/learn.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017 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 "colors.h"
23
#include "nx-match.h"
24
#include "openflow/openflow.h"
25
#include "openvswitch/dynamic-string.h"
26
#include "openvswitch/match.h"
27
#include "openvswitch/meta-flow.h"
28
#include "openvswitch/ofp-actions.h"
29
#include "openvswitch/ofp-errors.h"
30
#include "openvswitch/ofp-flow.h"
31
#include "openvswitch/ofp-parse.h"
32
#include "openvswitch/ofp-table.h"
33
#include "openvswitch/ofpbuf.h"
34
#include "vl-mff-map.h"
35
#include "unaligned.h"
36
37
38
/* Checks that 'learn' is a valid action on 'flow'.  Returns 0 if it is valid,
39
 * otherwise an OFPERR_*. */
40
enum ofperr
41
learn_check(const struct ofpact_learn *learn, const struct match *src_match)
42
56.8k
{
43
56.8k
    const struct ofpact_learn_spec *spec;
44
56.8k
    struct match dst_match;
45
46
56.8k
    match_init_catchall(&dst_match);
47
98.6k
    OFPACT_LEARN_SPEC_FOR_EACH (spec, learn) {
48
98.6k
        enum ofperr error;
49
50
        /* Check the source. */
51
98.6k
        if (spec->src_type == NX_LEARN_SRC_FIELD) {
52
6.86k
            error = mf_check_src(&spec->src, src_match);
53
6.86k
            if (error) {
54
522
                return error;
55
522
            }
56
6.86k
        }
57
58
        /* Check the destination. */
59
98.1k
        switch (spec->dst_type) {
60
94.1k
        case NX_LEARN_DST_MATCH:
61
94.1k
            error = mf_check_src(&spec->dst, &dst_match);
62
94.1k
            if (error) {
63
566
                return error;
64
566
            }
65
93.5k
            if (spec->src_type & NX_LEARN_SRC_IMMEDIATE) {
66
90.5k
                mf_write_subfield_value(&spec->dst,
67
90.5k
                                        ofpact_learn_spec_imm(spec),
68
90.5k
                                        &dst_match);
69
90.5k
            }
70
93.5k
            break;
71
72
994
        case NX_LEARN_DST_LOAD:
73
994
            error = mf_check_dst(&spec->dst, &dst_match);
74
994
            if (error) {
75
36
                return error;
76
36
            }
77
958
            break;
78
79
3.01k
        case NX_LEARN_DST_OUTPUT:
80
            /* Nothing to do. */
81
3.01k
            break;
82
98.1k
        }
83
98.1k
    }
84
55.7k
    return 0;
85
56.8k
}
86
87
/* Composes 'fm' so that executing it will implement 'learn' given that the
88
 * packet being processed has 'flow' as its flow.
89
 *
90
 * Uses 'ofpacts' to store the flow mod's actions.  The caller must initialize
91
 * 'ofpacts' and retains ownership of it.  'fm->ofpacts' will point into the
92
 * 'ofpacts' buffer.
93
 *
94
 * The caller must eventually destroy fm->match.
95
 *
96
 * The caller has to actually execute 'fm'. */
97
void
98
learn_execute(const struct ofpact_learn *learn, const struct flow *flow,
99
              struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
100
0
{
101
0
    const struct ofpact_learn_spec *spec;
102
0
    struct match match;
103
104
0
    match_init_catchall(&match);
105
0
    fm->priority = learn->priority;
106
0
    fm->cookie = htonll(0);
107
0
    fm->cookie_mask = htonll(0);
108
0
    fm->new_cookie = learn->cookie;
109
0
    fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
110
0
    fm->table_id = learn->table_id;
111
0
    fm->command = OFPFC_MODIFY_STRICT;
112
0
    fm->idle_timeout = learn->idle_timeout;
113
0
    fm->hard_timeout = learn->hard_timeout;
114
0
    fm->importance = 0;
115
0
    fm->buffer_id = UINT32_MAX;
116
0
    fm->out_port = OFPP_NONE;
117
0
    fm->ofpacts_tlv_bitmap = 0;
118
0
    fm->flags = 0;
119
0
    if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
120
0
        fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
121
0
    }
122
0
    fm->ofpacts = NULL;
123
0
    fm->ofpacts_len = 0;
124
125
0
    if (learn->fin_idle_timeout || learn->fin_hard_timeout) {
126
0
        struct ofpact_fin_timeout *oft;
127
128
0
        oft = ofpact_put_FIN_TIMEOUT(ofpacts);
129
0
        oft->fin_idle_timeout = learn->fin_idle_timeout;
130
0
        oft->fin_hard_timeout = learn->fin_hard_timeout;
131
0
    }
132
133
0
    OFPACT_LEARN_SPEC_FOR_EACH (spec, learn) {
134
0
        struct ofpact_set_field *sf;
135
0
        union mf_subvalue value;
136
137
0
        if (spec->src_type == NX_LEARN_SRC_FIELD) {
138
0
            mf_read_subfield(&spec->src, flow, &value);
139
0
        } else {
140
0
            mf_subvalue_from_value(&spec->dst, &value,
141
0
                                   ofpact_learn_spec_imm(spec));
142
0
        }
143
144
0
        switch (spec->dst_type) {
145
0
        case NX_LEARN_DST_MATCH:
146
0
            mf_write_subfield(&spec->dst, &value, &match);
147
0
            match_add_ethernet_prereq(&match, spec->dst.field);
148
0
            mf_vl_mff_set_tlv_bitmap(
149
0
                spec->dst.field, &match.flow.tunnel.metadata.present.map);
150
0
            break;
151
152
0
        case NX_LEARN_DST_LOAD:
153
0
            sf = ofpact_put_reg_load(ofpacts, spec->dst.field, NULL, NULL);
154
0
            bitwise_copy(&value, sizeof value, 0,
155
0
                         sf->value, spec->dst.field->n_bytes, spec->dst.ofs,
156
0
                         spec->n_bits);
157
0
            bitwise_one(ofpact_set_field_mask(sf), spec->dst.field->n_bytes,
158
0
                        spec->dst.ofs, spec->n_bits);
159
0
            mf_vl_mff_set_tlv_bitmap(spec->dst.field, &fm->ofpacts_tlv_bitmap);
160
0
            break;
161
162
0
        case NX_LEARN_DST_OUTPUT:
163
0
            if (spec->n_bits <= 16
164
0
                || is_all_zeros(value.u8, sizeof value - 2)) {
165
0
                ofp_port_t port = u16_to_ofp(ntohll(value.integer));
166
167
0
                if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)
168
0
                    || port == OFPP_IN_PORT
169
0
                    || port == OFPP_FLOOD
170
0
                    || port == OFPP_LOCAL
171
0
                    || port == OFPP_ALL) {
172
0
                    ofpact_put_OUTPUT(ofpacts)->port = port;
173
0
                }
174
0
            }
175
0
            break;
176
0
        }
177
0
    }
178
179
0
    minimatch_init(&fm->match, &match);
180
0
    fm->ofpacts = ofpacts->data;
181
0
    fm->ofpacts_len = ofpacts->size;
182
0
}
183
184
/* Perform a bitwise-OR on 'wc''s fields that are relevant as sources in
185
 * the learn action 'learn'. */
186
void
187
learn_mask(const struct ofpact_learn *learn, struct flow_wildcards *wc)
188
0
{
189
0
    const struct ofpact_learn_spec *spec;
190
0
    union mf_subvalue value;
191
192
0
    memset(&value, 0xff, sizeof value);
193
0
    OFPACT_LEARN_SPEC_FOR_EACH (spec, learn) {
194
0
        if (spec->src_type == NX_LEARN_SRC_FIELD) {
195
0
            mf_write_subfield_flow(&spec->src, &value, &wc->masks);
196
0
        }
197
0
    }
198
0
}
199
200
/* Returns NULL if successful, otherwise a malloc()'d string describing the
201
 * error.  The caller is responsible for freeing the returned string. */
202
static char * OVS_WARN_UNUSED_RESULT
203
learn_parse_load_immediate(union mf_subvalue *imm, const char *s,
204
                           const char *full_s, struct ofpact_learn_spec *spec,
205
                           struct ofpbuf *ofpacts)
206
287
{
207
287
    struct mf_subfield dst;
208
287
    char *error;
209
210
287
    error = mf_parse_subfield(&dst, s);
211
287
    if (error) {
212
5
        return error;
213
5
    }
214
282
    if (!mf_nxm_header(dst.field->id)) {
215
1
        return xasprintf("%s: experimenter OXM field '%s' not supported",
216
1
                         full_s, s);
217
1
    }
218
219
281
    if (!bitwise_is_all_zeros(imm, sizeof *imm, dst.n_bits,
220
281
                              (8 * sizeof *imm) - dst.n_bits)) {
221
3
        return xasprintf("%s: value does not fit into %u bits",
222
3
                         full_s, dst.n_bits);
223
3
    }
224
225
278
    spec->n_bits = dst.n_bits;
226
278
    spec->src_type = NX_LEARN_SRC_IMMEDIATE;
227
278
    spec->dst_type = NX_LEARN_DST_LOAD;
228
278
    spec->dst = dst;
229
230
    /* Push value last, as this may reallocate 'spec'! */
231
278
    unsigned int n_bytes = DIV_ROUND_UP(dst.n_bits, 8);
232
278
    uint8_t *src_imm = ofpbuf_put_zeros(ofpacts, OFPACT_ALIGN(n_bytes));
233
278
    memcpy(src_imm, &imm->u8[sizeof imm->u8 - n_bytes], n_bytes);
234
235
278
    return NULL;
236
281
}
237
238
/* Returns NULL if successful, otherwise a malloc()'d string describing the
239
 * error.  The caller is responsible for freeing the returned string. */
240
static char * OVS_WARN_UNUSED_RESULT
241
learn_parse_spec(const char *orig, char *name, char *value,
242
                 const struct ofputil_port_map *port_map,
243
                 struct ofpact_learn_spec *spec,
244
                 struct ofpbuf *ofpacts)
245
57.1k
{
246
    /* Parse destination and check prerequisites. */
247
57.1k
    struct mf_subfield dst;
248
249
57.1k
    char *error = mf_parse_subfield(&dst, name);
250
57.1k
    bool parse_error = error != NULL;
251
57.1k
    free(error);
252
253
57.1k
    if (!parse_error) {
254
54.7k
        if (!mf_nxm_header(dst.field->id)) {
255
3
            return xasprintf("%s: experimenter OXM field '%s' not supported",
256
3
                             orig, name);
257
3
        }
258
54.7k
        spec->dst = dst;
259
54.7k
        spec->n_bits = dst.n_bits;
260
54.7k
        spec->dst_type = NX_LEARN_DST_MATCH;
261
262
        /* Parse source and check prerequisites. */
263
54.7k
        if (value[0] != '\0') {
264
50.9k
            struct mf_subfield src;
265
50.9k
            error = mf_parse_subfield(&src, value);
266
50.9k
            if (error) {
267
50.7k
                union mf_value imm;
268
50.7k
                char *imm_error = NULL;
269
270
                /* Try an immediate value. */
271
50.7k
                if (dst.ofs == 0 && dst.n_bits == dst.field->n_bits) {
272
                    /* Full field value. */
273
33.8k
                    imm_error = mf_parse_value(dst.field, value, port_map,
274
33.8k
                                               &imm);
275
33.8k
                } else {
276
16.8k
                    char *tail;
277
                    /* Partial field value. */
278
16.8k
                    if (parse_int_string(value, imm.b,
279
16.8k
                                          dst.field->n_bytes, &tail)
280
16.8k
                        || *tail != 0) {
281
7
                        imm_error = xasprintf("%s: cannot parse integer value", orig);
282
7
                    }
283
284
16.8k
                    if (!imm_error &&
285
16.8k
                        !bitwise_is_all_zeros(imm.b, dst.field->n_bytes,
286
16.8k
                                              dst.n_bits,
287
16.8k
                                              dst.field->n_bytes * 8 - dst.n_bits)) {
288
2
                        struct ds ds;
289
290
2
                        ds_init(&ds);
291
2
                        mf_format(dst.field, &imm, NULL, NULL, &ds);
292
2
                        imm_error = xasprintf("%s: value %s does not fit into %d bits",
293
2
                                              orig, ds_cstr(&ds), dst.n_bits);
294
2
                        ds_destroy(&ds);
295
2
                    }
296
16.8k
                }
297
50.7k
                if (imm_error) {
298
21
                    char *err = xasprintf("%s: %s value %s cannot be parsed as a subfield (%s) or an immediate value (%s)",
299
21
                                          orig, name, value, error, imm_error);
300
21
                    free(error);
301
21
                    free(imm_error);
302
21
                    return err;
303
21
                }
304
305
50.7k
                spec->src_type = NX_LEARN_SRC_IMMEDIATE;
306
307
                /* Push value last, as this may reallocate 'spec'! */
308
50.7k
                unsigned int imm_bytes = DIV_ROUND_UP(dst.n_bits, 8);
309
50.7k
                uint8_t *src_imm = ofpbuf_put_zeros(ofpacts,
310
50.7k
                                                    OFPACT_ALIGN(imm_bytes));
311
312
50.7k
                memcpy(src_imm, &imm.b[dst.field->n_bytes - imm_bytes],
313
50.7k
                       imm_bytes);
314
315
50.7k
                free(error);
316
50.7k
                return NULL;
317
50.7k
            }
318
254
            spec->src = src;
319
254
            if (spec->src.n_bits != spec->dst.n_bits) {
320
12
                return xasprintf("%s: bit widths of %s (%u) and %s (%u) "
321
12
                                 "differ", orig, name, spec->src.n_bits, value,
322
12
                                 spec->dst.n_bits);
323
12
            }
324
3.78k
        } else {
325
3.78k
            spec->src = spec->dst;
326
3.78k
        }
327
328
4.03k
        spec->src_type = NX_LEARN_SRC_FIELD;
329
4.03k
    } else if (!strcmp(name, "load")) {
330
1.17k
        union mf_subvalue imm;
331
1.17k
        char *tail;
332
1.17k
        char *dst_value = strstr(value, "->");
333
334
1.17k
        if (dst_value == value) {
335
1
            return xasprintf("%s: missing source before `->' in `%s'", name,
336
1
                             value);
337
1
        }
338
1.17k
        if (!dst_value) {
339
1
            return xasprintf("%s: missing `->' in `%s'", name, value);
340
1
        }
341
342
1.17k
        if (!parse_int_string(value, imm.u8, sizeof imm.u8, (char **) &tail)
343
1.17k
            && tail != value) {
344
288
            if (tail != dst_value) {
345
1
                return xasprintf("%s: garbage before `->' in `%s'",
346
1
                                 name, value);
347
1
            }
348
349
287
            error = learn_parse_load_immediate(&imm, dst_value + 2, value, spec,
350
287
                                               ofpacts);
351
287
            if (error) {
352
9
                return error;
353
9
            }
354
886
        } else {
355
886
            struct ofpact_reg_move move;
356
357
886
            error = nxm_parse_reg_move(&move, value);
358
886
            if (error) {
359
10
                return error;
360
10
            }
361
362
876
            spec->n_bits = move.src.n_bits;
363
876
            spec->src_type = NX_LEARN_SRC_FIELD;
364
876
            spec->src = move.src;
365
876
            spec->dst_type = NX_LEARN_DST_LOAD;
366
876
            spec->dst = move.dst;
367
876
        }
368
1.22k
    } else if (!strcmp(name, "output")) {
369
362
        error = mf_parse_subfield(&spec->src, value);
370
362
        if (error) {
371
1
            return error;
372
1
        }
373
374
361
        spec->n_bits = spec->src.n_bits;
375
361
        spec->src_type = NX_LEARN_SRC_FIELD;
376
361
        spec->dst_type = NX_LEARN_DST_OUTPUT;
377
859
    } else {
378
859
        return xasprintf("%s: unknown keyword %s", orig, name);
379
859
    }
380
381
5.54k
    return NULL;
382
57.1k
}
383
384
/* Returns NULL if successful, otherwise a malloc()'d string describing the
385
 * error.  The caller is responsible for freeing the returned string. */
386
static char * OVS_WARN_UNUSED_RESULT
387
learn_parse__(char *orig, char *arg, const struct ofputil_port_map *port_map,
388
              const struct ofputil_table_map *table_map,
389
              struct ofpbuf *ofpacts)
390
35.4k
{
391
35.4k
    struct ofpact_learn *learn;
392
35.4k
    char *name, *value;
393
394
35.4k
    learn = ofpact_put_LEARN(ofpacts);
395
35.4k
    learn->idle_timeout = OFP_FLOW_PERMANENT;
396
35.4k
    learn->hard_timeout = OFP_FLOW_PERMANENT;
397
35.4k
    learn->priority = OFP_DEFAULT_PRIORITY;
398
35.4k
    learn->table_id = 1;
399
400
96.2k
    while (ofputil_parse_key_value(&arg, &name, &value)) {
401
61.7k
        if (!strcmp(name, "table")) {
402
1.35k
            if (!ofputil_table_from_string(value, table_map,
403
1.35k
                                           &learn->table_id)) {
404
1
                return xasprintf("unknown table \"%s\"", value);
405
1.35k
            } else if (learn->table_id == 255) {
406
1
                return xasprintf("%s: table id 255 not valid for `learn' "
407
1
                                 "action", orig);
408
1
            }
409
60.4k
        } else if (!strcmp(name, "priority")) {
410
887
            learn->priority = atoi(value);
411
59.5k
        } else if (!strcmp(name, "idle_timeout")) {
412
225
            learn->idle_timeout = atoi(value);
413
59.3k
        } else if (!strcmp(name, "hard_timeout")) {
414
204
            learn->hard_timeout = atoi(value);
415
59.1k
        } else if (!strcmp(name, "fin_idle_timeout")) {
416
199
            learn->fin_idle_timeout = atoi(value);
417
58.9k
        } else if (!strcmp(name, "fin_hard_timeout")) {
418
343
            learn->fin_hard_timeout = atoi(value);
419
58.5k
        } else if (!strcmp(name, "cookie")) {
420
213
            learn->cookie = htonll(strtoull(value, NULL, 0));
421
58.3k
        } else if (!strcmp(name, "send_flow_rem")) {
422
207
            learn->flags |= NX_LEARN_F_SEND_FLOW_REM;
423
58.1k
        } else if (!strcmp(name, "delete_learned")) {
424
195
            learn->flags |= NX_LEARN_F_DELETE_LEARNED;
425
57.9k
        } else if (!strcmp(name, "limit")) {
426
507
            learn->limit = atoi(value);
427
57.4k
        } else if (!strcmp(name, "result_dst")) {
428
272
            char *error;
429
272
            learn->flags |= NX_LEARN_F_WRITE_RESULT;
430
272
            error = mf_parse_subfield(&learn->result_dst, value);
431
272
            if (error) {
432
1
                return error;
433
1
            }
434
271
            if (!learn->result_dst.field->writable) {
435
1
                return xasprintf("%s is read-only", value);
436
1
            }
437
270
            if (learn->result_dst.n_bits != 1) {
438
1
                return xasprintf("result_dst in 'learn' action must be a "
439
1
                                 "single bit");
440
1
            }
441
57.1k
        } else {
442
57.1k
            struct ofpact_learn_spec *spec;
443
57.1k
            char *error;
444
445
57.1k
            spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
446
57.1k
            error = learn_parse_spec(orig, name, value, port_map,
447
57.1k
                                     spec, ofpacts);
448
57.1k
            if (error) {
449
918
                return error;
450
918
            }
451
56.2k
            learn = ofpacts->header;
452
56.2k
        }
453
61.7k
    }
454
455
34.4k
    if (ofpbuf_oversized(ofpacts)) {
456
3
        return xasprintf("input too big");
457
3
    }
458
459
34.4k
    ofpact_finish_LEARN(ofpacts, &learn);
460
461
34.4k
    return NULL;
462
34.4k
}
463
464
/* Parses 'arg' as a set of arguments to the "learn" action and appends a
465
 * matching OFPACT_LEARN action to 'ofpacts'.  ovs-actions(7) describes the
466
 * format parsed.
467
 *
468
 * Returns NULL if successful, otherwise a malloc()'d string describing the
469
 * error.  The caller is responsible for freeing the returned string.
470
 *
471
 * If 'flow' is nonnull, then it should be the flow from a struct match that is
472
 * the matching rule for the learning action.  This helps to better validate
473
 * the action's arguments.
474
 *
475
 * Modifies 'arg'. */
476
char * OVS_WARN_UNUSED_RESULT
477
learn_parse(char *arg, const struct ofputil_port_map *port_map,
478
            const struct ofputil_table_map *table_map,
479
            struct ofpbuf *ofpacts)
480
35.4k
{
481
35.4k
    char *orig = xstrdup(arg);
482
35.4k
    char *error = learn_parse__(orig, arg, port_map, table_map, ofpacts);
483
35.4k
    free(orig);
484
35.4k
    return error;
485
35.4k
}
486
487
/* Appends a description of 'learn' to 's', in the format that ovs-actions(7)
488
 * describes. */
489
void
490
learn_format(const struct ofpact_learn *learn,
491
             const struct ofputil_port_map *port_map,
492
             const struct ofputil_table_map *table_map,
493
             struct ds *s)
494
23.7k
{
495
23.7k
    const struct ofpact_learn_spec *spec;
496
23.7k
    struct match match;
497
498
23.7k
    match_init_catchall(&match);
499
500
23.7k
    ds_put_format(s, "%slearn(%s%stable=%s",
501
23.7k
                  colors.learn, colors.end, colors.special, colors.end);
502
23.7k
    ofputil_format_table(learn->table_id, table_map, s);
503
23.7k
    if (learn->idle_timeout != OFP_FLOW_PERMANENT) {
504
23.4k
        ds_put_format(s, ",%sidle_timeout=%s%"PRIu16,
505
23.4k
                      colors.param, colors.end, learn->idle_timeout);
506
23.4k
    }
507
23.7k
    if (learn->hard_timeout != OFP_FLOW_PERMANENT) {
508
22.6k
        ds_put_format(s, ",%shard_timeout=%s%"PRIu16,
509
22.6k
                      colors.param, colors.end, learn->hard_timeout);
510
22.6k
    }
511
23.7k
    if (learn->fin_idle_timeout) {
512
23.1k
        ds_put_format(s, ",%sfin_idle_timeout=%s%"PRIu16,
513
23.1k
                      colors.param, colors.end, learn->fin_idle_timeout);
514
23.1k
    }
515
23.7k
    if (learn->fin_hard_timeout) {
516
21.5k
        ds_put_format(s, "%s,fin_hard_timeout=%s%"PRIu16,
517
21.5k
                      colors.param, colors.end, learn->fin_hard_timeout);
518
21.5k
    }
519
23.7k
    if (learn->priority != OFP_DEFAULT_PRIORITY) {
520
21.8k
        ds_put_format(s, "%s,priority=%s%"PRIu16,
521
21.8k
                      colors.special, colors.end, learn->priority);
522
21.8k
    }
523
23.7k
    if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
524
9.74k
        ds_put_format(s, ",%ssend_flow_rem%s", colors.value, colors.end);
525
9.74k
    }
526
23.7k
    if (learn->flags & NX_LEARN_F_DELETE_LEARNED) {
527
9.99k
        ds_put_format(s, ",%sdelete_learned%s", colors.value, colors.end);
528
9.99k
    }
529
23.7k
    if (learn->cookie != 0) {
530
23.5k
        ds_put_format(s, ",%scookie=%s%#"PRIx64,
531
23.5k
                      colors.param, colors.end, ntohll(learn->cookie));
532
23.5k
    }
533
23.7k
    if (learn->limit != 0) {
534
403
        ds_put_format(s, ",%slimit=%s%"PRIu32,
535
403
                      colors.param, colors.end, learn->limit);
536
403
    }
537
23.7k
    if (learn->flags & NX_LEARN_F_WRITE_RESULT) {
538
11
        ds_put_format(s, ",%sresult_dst=%s", colors.param, colors.end);
539
11
        mf_format_subfield(&learn->result_dst, s);
540
11
    }
541
542
46.2k
    OFPACT_LEARN_SPEC_FOR_EACH (spec, learn) {
543
46.2k
        unsigned int n_bytes = DIV_ROUND_UP(spec->n_bits, 8);
544
46.2k
        ds_put_char(s, ',');
545
546
46.2k
        switch (spec->src_type | spec->dst_type) {
547
42.8k
        case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH: {
548
42.8k
            if (spec->dst.ofs == 0
549
42.8k
                && spec->dst.n_bits == spec->dst.field->n_bits) {
550
10.5k
                union mf_value value;
551
552
10.5k
                memset(&value, 0, sizeof value);
553
10.5k
                memcpy(&value.b[spec->dst.field->n_bytes - n_bytes],
554
10.5k
                       ofpact_learn_spec_imm(spec), n_bytes);
555
10.5k
                ds_put_format(s, "%s%s=%s", colors.param,
556
10.5k
                              spec->dst.field->name, colors.end);
557
10.5k
                mf_format(spec->dst.field, &value, NULL, port_map, s);
558
32.3k
            } else {
559
32.3k
                ds_put_format(s, "%s", colors.param);
560
32.3k
                mf_format_subfield(&spec->dst, s);
561
32.3k
                ds_put_format(s, "=%s", colors.end);
562
32.3k
                ds_put_hex(s, ofpact_learn_spec_imm(spec), n_bytes);
563
32.3k
            }
564
42.8k
            break;
565
0
        }
566
240
        case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
567
240
            ds_put_format(s, "%s", colors.param);
568
240
            mf_format_subfield(&spec->dst, s);
569
240
            ds_put_format(s, "%s", colors.end);
570
240
            if (spec->src.field != spec->dst.field ||
571
240
                spec->src.ofs != spec->dst.ofs) {
572
58
                ds_put_format(s, "%s=%s", colors.param, colors.end);
573
58
                mf_format_subfield(&spec->src, s);
574
58
            }
575
240
            break;
576
577
907
        case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
578
907
            ds_put_format(s, "%sload:%s", colors.special, colors.end);
579
907
            ds_put_hex(s, ofpact_learn_spec_imm(spec), n_bytes);
580
907
            ds_put_format(s, "%s->%s", colors.special, colors.end);
581
907
            mf_format_subfield(&spec->dst, s);
582
907
            break;
583
584
20
        case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
585
20
            ds_put_format(s, "%sload:%s", colors.special, colors.end);
586
20
            mf_format_subfield(&spec->src, s);
587
20
            ds_put_format(s, "%s->%s", colors.special, colors.end);
588
20
            mf_format_subfield(&spec->dst, s);
589
20
            break;
590
591
2.22k
        case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
592
2.22k
            ds_put_format(s, "%soutput:%s", colors.special, colors.end);
593
2.22k
            mf_format_subfield(&spec->src, s);
594
2.22k
            break;
595
46.2k
        }
596
46.2k
    }
597
23.7k
    ds_put_format(s, "%s)%s", colors.learn, colors.end);
598
23.7k
}