Coverage Report

Created: 2025-08-29 06:59

/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
53.3k
{
43
53.3k
    const struct ofpact_learn_spec *spec;
44
53.3k
    struct match dst_match;
45
46
53.3k
    match_init_catchall(&dst_match);
47
87.0k
    OFPACT_LEARN_SPEC_FOR_EACH (spec, learn) {
48
87.0k
        enum ofperr error;
49
50
        /* Check the source. */
51
87.0k
        if (spec->src_type == NX_LEARN_SRC_FIELD) {
52
5.37k
            error = mf_check_src(&spec->src, src_match);
53
5.37k
            if (error) {
54
386
                return error;
55
386
            }
56
5.37k
        }
57
58
        /* Check the destination. */
59
86.6k
        switch (spec->dst_type) {
60
83.4k
        case NX_LEARN_DST_MATCH:
61
83.4k
            error = mf_check_src(&spec->dst, &dst_match);
62
83.4k
            if (error) {
63
388
                return error;
64
388
            }
65
83.0k
            if (spec->src_type & NX_LEARN_SRC_IMMEDIATE) {
66
80.8k
                mf_write_subfield_value(&spec->dst,
67
80.8k
                                        ofpact_learn_spec_imm(spec),
68
80.8k
                                        &dst_match);
69
80.8k
            }
70
83.0k
            break;
71
72
696
        case NX_LEARN_DST_LOAD:
73
696
            error = mf_check_dst(&spec->dst, &dst_match);
74
696
            if (error) {
75
12
                return error;
76
12
            }
77
684
            break;
78
79
2.50k
        case NX_LEARN_DST_OUTPUT:
80
            /* Nothing to do. */
81
2.50k
            break;
82
86.6k
        }
83
86.6k
    }
84
52.5k
    return 0;
85
53.3k
}
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
575
{
207
575
    struct mf_subfield dst;
208
575
    char *error;
209
210
575
    error = mf_parse_subfield(&dst, s);
211
575
    if (error) {
212
5
        return error;
213
5
    }
214
570
    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
569
    if (!bitwise_is_all_zeros(imm, sizeof *imm, dst.n_bits,
220
569
                              (8 * sizeof *imm) - dst.n_bits)) {
221
1
        return xasprintf("%s: value does not fit into %u bits",
222
1
                         full_s, dst.n_bits);
223
1
    }
224
225
568
    spec->n_bits = dst.n_bits;
226
568
    spec->src_type = NX_LEARN_SRC_IMMEDIATE;
227
568
    spec->dst_type = NX_LEARN_DST_LOAD;
228
568
    spec->dst = dst;
229
230
    /* Push value last, as this may reallocate 'spec'! */
231
568
    unsigned int n_bytes = DIV_ROUND_UP(dst.n_bits, 8);
232
568
    uint8_t *src_imm = ofpbuf_put_zeros(ofpacts, OFPACT_ALIGN(n_bytes));
233
568
    memcpy(src_imm, &imm->u8[sizeof imm->u8 - n_bytes], n_bytes);
234
235
568
    return NULL;
236
569
}
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
51.5k
{
246
    /* Parse destination and check prerequisites. */
247
51.5k
    struct mf_subfield dst;
248
249
51.5k
    char *error = mf_parse_subfield(&dst, name);
250
51.5k
    bool parse_error = error != NULL;
251
51.5k
    free(error);
252
253
51.5k
    if (!parse_error) {
254
49.6k
        if (!mf_nxm_header(dst.field->id)) {
255
2
            return xasprintf("%s: experimenter OXM field '%s' not supported",
256
2
                             orig, name);
257
2
        }
258
49.6k
        spec->dst = dst;
259
49.6k
        spec->n_bits = dst.n_bits;
260
49.6k
        spec->dst_type = NX_LEARN_DST_MATCH;
261
262
        /* Parse source and check prerequisites. */
263
49.6k
        if (value[0] != '\0') {
264
47.1k
            struct mf_subfield src;
265
47.1k
            error = mf_parse_subfield(&src, value);
266
47.1k
            if (error) {
267
46.9k
                union mf_value imm;
268
46.9k
                char *imm_error = NULL;
269
270
                /* Try an immediate value. */
271
46.9k
                if (dst.ofs == 0 && dst.n_bits == dst.field->n_bits) {
272
                    /* Full field value. */
273
28.9k
                    imm_error = mf_parse_value(dst.field, value, port_map,
274
28.9k
                                               &imm);
275
28.9k
                } else {
276
17.9k
                    char *tail;
277
                    /* Partial field value. */
278
17.9k
                    if (parse_int_string(value, imm.b,
279
17.9k
                                          dst.field->n_bytes, &tail)
280
17.9k
                        || *tail != 0) {
281
12
                        imm_error = xasprintf("%s: cannot parse integer value", orig);
282
12
                    }
283
284
17.9k
                    if (!imm_error &&
285
17.9k
                        !bitwise_is_all_zeros(imm.b, dst.field->n_bytes,
286
17.9k
                                              dst.n_bits,
287
17.9k
                                              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
17.9k
                }
297
46.9k
                if (imm_error) {
298
31
                    char *err = xasprintf("%s: %s value %s cannot be parsed as a subfield (%s) or an immediate value (%s)",
299
31
                                          orig, name, value, error, imm_error);
300
31
                    free(error);
301
31
                    free(imm_error);
302
31
                    return err;
303
31
                }
304
305
46.8k
                spec->src_type = NX_LEARN_SRC_IMMEDIATE;
306
307
                /* Push value last, as this may reallocate 'spec'! */
308
46.8k
                unsigned int imm_bytes = DIV_ROUND_UP(dst.n_bits, 8);
309
46.8k
                uint8_t *src_imm = ofpbuf_put_zeros(ofpacts,
310
46.8k
                                                    OFPACT_ALIGN(imm_bytes));
311
312
46.8k
                memcpy(src_imm, &imm.b[dst.field->n_bytes - imm_bytes],
313
46.8k
                       imm_bytes);
314
315
46.8k
                free(error);
316
46.8k
                return NULL;
317
46.9k
            }
318
196
            spec->src = src;
319
196
            if (spec->src.n_bits != spec->dst.n_bits) {
320
2
                return xasprintf("%s: bit widths of %s (%u) and %s (%u) "
321
2
                                 "differ", orig, name, spec->src.n_bits, value,
322
2
                                 spec->dst.n_bits);
323
2
            }
324
2.55k
        } else {
325
2.55k
            spec->src = spec->dst;
326
2.55k
        }
327
328
2.74k
        spec->src_type = NX_LEARN_SRC_FIELD;
329
2.74k
    } else if (!strcmp(name, "load")) {
330
1.00k
        union mf_subvalue imm;
331
1.00k
        char *tail;
332
1.00k
        char *dst_value = strstr(value, "->");
333
334
1.00k
        if (dst_value == value) {
335
1
            return xasprintf("%s: missing source before `->' in `%s'", name,
336
1
                             value);
337
1
        }
338
1.00k
        if (!dst_value) {
339
1
            return xasprintf("%s: missing `->' in `%s'", name, value);
340
1
        }
341
342
1.00k
        if (!parse_int_string(value, imm.u8, sizeof imm.u8, (char **) &tail)
343
1.00k
            && tail != value) {
344
576
            if (tail != dst_value) {
345
1
                return xasprintf("%s: garbage before `->' in `%s'",
346
1
                                 name, value);
347
1
            }
348
349
575
            error = learn_parse_load_immediate(&imm, dst_value + 2, value, spec,
350
575
                                               ofpacts);
351
575
            if (error) {
352
7
                return error;
353
7
            }
354
575
        } else {
355
426
            struct ofpact_reg_move move;
356
357
426
            error = nxm_parse_reg_move(&move, value);
358
426
            if (error) {
359
5
                return error;
360
5
            }
361
362
421
            spec->n_bits = move.src.n_bits;
363
421
            spec->src_type = NX_LEARN_SRC_FIELD;
364
421
            spec->src = move.src;
365
421
            spec->dst_type = NX_LEARN_DST_LOAD;
366
421
            spec->dst = move.dst;
367
421
        }
368
1.00k
    } else if (!strcmp(name, "output")) {
369
250
        error = mf_parse_subfield(&spec->src, value);
370
250
        if (error) {
371
2
            return error;
372
2
        }
373
374
248
        spec->n_bits = spec->src.n_bits;
375
248
        spec->src_type = NX_LEARN_SRC_FIELD;
376
248
        spec->dst_type = NX_LEARN_DST_OUTPUT;
377
614
    } else {
378
614
        return xasprintf("%s: unknown keyword %s", orig, name);
379
614
    }
380
381
3.98k
    return NULL;
382
51.5k
}
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
34.9k
{
391
34.9k
    struct ofpact_learn *learn;
392
34.9k
    char *name, *value;
393
394
34.9k
    learn = ofpact_put_LEARN(ofpacts);
395
34.9k
    learn->idle_timeout = OFP_FLOW_PERMANENT;
396
34.9k
    learn->hard_timeout = OFP_FLOW_PERMANENT;
397
34.9k
    learn->priority = OFP_DEFAULT_PRIORITY;
398
34.9k
    learn->table_id = 1;
399
400
89.3k
    while (ofputil_parse_key_value(&arg, &name, &value)) {
401
55.0k
        if (!strcmp(name, "table")) {
402
1.12k
            if (!ofputil_table_from_string(value, table_map,
403
1.12k
                                           &learn->table_id)) {
404
1
                return xasprintf("unknown table \"%s\"", value);
405
1.12k
            } 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
53.9k
        } else if (!strcmp(name, "priority")) {
410
295
            learn->priority = atoi(value);
411
53.6k
        } else if (!strcmp(name, "idle_timeout")) {
412
190
            learn->idle_timeout = atoi(value);
413
53.4k
        } else if (!strcmp(name, "hard_timeout")) {
414
208
            learn->hard_timeout = atoi(value);
415
53.2k
        } else if (!strcmp(name, "fin_idle_timeout")) {
416
203
            learn->fin_idle_timeout = atoi(value);
417
53.0k
        } else if (!strcmp(name, "fin_hard_timeout")) {
418
336
            learn->fin_hard_timeout = atoi(value);
419
52.7k
        } else if (!strcmp(name, "cookie")) {
420
372
            learn->cookie = htonll(strtoull(value, NULL, 0));
421
52.3k
        } else if (!strcmp(name, "send_flow_rem")) {
422
87
            learn->flags |= NX_LEARN_F_SEND_FLOW_REM;
423
52.2k
        } else if (!strcmp(name, "delete_learned")) {
424
222
            learn->flags |= NX_LEARN_F_DELETE_LEARNED;
425
52.0k
        } else if (!strcmp(name, "limit")) {
426
431
            learn->limit = atoi(value);
427
51.5k
        } else if (!strcmp(name, "result_dst")) {
428
48
            char *error;
429
48
            learn->flags |= NX_LEARN_F_WRITE_RESULT;
430
48
            error = mf_parse_subfield(&learn->result_dst, value);
431
48
            if (error) {
432
1
                return error;
433
1
            }
434
47
            if (!learn->result_dst.field->writable) {
435
1
                return xasprintf("%s is read-only", value);
436
1
            }
437
46
            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
51.5k
        } else {
442
51.5k
            struct ofpact_learn_spec *spec;
443
51.5k
            char *error;
444
445
51.5k
            spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
446
51.5k
            error = learn_parse_spec(orig, name, value, port_map,
447
51.5k
                                     spec, ofpacts);
448
51.5k
            if (error) {
449
666
                return error;
450
666
            }
451
50.8k
            learn = ofpacts->header;
452
50.8k
        }
453
55.0k
    }
454
455
34.2k
    if (ofpbuf_oversized(ofpacts)) {
456
1
        return xasprintf("input too big");
457
1
    }
458
459
34.2k
    ofpact_finish_LEARN(ofpacts, &learn);
460
461
34.2k
    return NULL;
462
34.2k
}
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
34.9k
{
481
34.9k
    char *orig = xstrdup(arg);
482
34.9k
    char *error = learn_parse__(orig, arg, port_map, table_map, ofpacts);
483
34.9k
    free(orig);
484
34.9k
    return error;
485
34.9k
}
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
19.4k
{
495
19.4k
    const struct ofpact_learn_spec *spec;
496
19.4k
    struct match match;
497
498
19.4k
    match_init_catchall(&match);
499
500
19.4k
    ds_put_format(s, "%slearn(%s%stable=%s",
501
19.4k
                  colors.learn, colors.end, colors.special, colors.end);
502
19.4k
    ofputil_format_table(learn->table_id, table_map, s);
503
19.4k
    if (learn->idle_timeout != OFP_FLOW_PERMANENT) {
504
19.3k
        ds_put_format(s, ",%sidle_timeout=%s%"PRIu16,
505
19.3k
                      colors.param, colors.end, learn->idle_timeout);
506
19.3k
    }
507
19.4k
    if (learn->hard_timeout != OFP_FLOW_PERMANENT) {
508
18.0k
        ds_put_format(s, ",%shard_timeout=%s%"PRIu16,
509
18.0k
                      colors.param, colors.end, learn->hard_timeout);
510
18.0k
    }
511
19.4k
    if (learn->fin_idle_timeout) {
512
18.5k
        ds_put_format(s, ",%sfin_idle_timeout=%s%"PRIu16,
513
18.5k
                      colors.param, colors.end, learn->fin_idle_timeout);
514
18.5k
    }
515
19.4k
    if (learn->fin_hard_timeout) {
516
17.0k
        ds_put_format(s, "%s,fin_hard_timeout=%s%"PRIu16,
517
17.0k
                      colors.param, colors.end, learn->fin_hard_timeout);
518
17.0k
    }
519
19.4k
    if (learn->priority != OFP_DEFAULT_PRIORITY) {
520
16.9k
        ds_put_format(s, "%s,priority=%s%"PRIu16,
521
16.9k
                      colors.special, colors.end, learn->priority);
522
16.9k
    }
523
19.4k
    if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
524
4.28k
        ds_put_format(s, ",%ssend_flow_rem%s", colors.value, colors.end);
525
4.28k
    }
526
19.4k
    if (learn->flags & NX_LEARN_F_DELETE_LEARNED) {
527
5.86k
        ds_put_format(s, ",%sdelete_learned%s", colors.value, colors.end);
528
5.86k
    }
529
19.4k
    if (learn->cookie != 0) {
530
19.4k
        ds_put_format(s, ",%scookie=%s%#"PRIx64,
531
19.4k
                      colors.param, colors.end, ntohll(learn->cookie));
532
19.4k
    }
533
19.4k
    if (learn->limit != 0) {
534
113
        ds_put_format(s, ",%slimit=%s%"PRIu32,
535
113
                      colors.param, colors.end, learn->limit);
536
113
    }
537
19.4k
    if (learn->flags & NX_LEARN_F_WRITE_RESULT) {
538
7
        ds_put_format(s, ",%sresult_dst=%s", colors.param, colors.end);
539
7
        mf_format_subfield(&learn->result_dst, s);
540
7
    }
541
542
38.4k
    OFPACT_LEARN_SPEC_FOR_EACH (spec, learn) {
543
38.4k
        unsigned int n_bytes = DIV_ROUND_UP(spec->n_bits, 8);
544
38.4k
        ds_put_char(s, ',');
545
546
38.4k
        switch (spec->src_type | spec->dst_type) {
547
35.9k
        case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH: {
548
35.9k
            if (spec->dst.ofs == 0
549
35.9k
                && spec->dst.n_bits == spec->dst.field->n_bits) {
550
3.08k
                union mf_value value;
551
552
3.08k
                memset(&value, 0, sizeof value);
553
3.08k
                memcpy(&value.b[spec->dst.field->n_bytes - n_bytes],
554
3.08k
                       ofpact_learn_spec_imm(spec), n_bytes);
555
3.08k
                ds_put_format(s, "%s%s=%s", colors.param,
556
3.08k
                              spec->dst.field->name, colors.end);
557
3.08k
                mf_format(spec->dst.field, &value, NULL, port_map, s);
558
32.8k
            } else {
559
32.8k
                ds_put_format(s, "%s", colors.param);
560
32.8k
                mf_format_subfield(&spec->dst, s);
561
32.8k
                ds_put_format(s, "=%s", colors.end);
562
32.8k
                ds_put_hex(s, ofpact_learn_spec_imm(spec), n_bytes);
563
32.8k
            }
564
35.9k
            break;
565
0
        }
566
36
        case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
567
36
            ds_put_format(s, "%s", colors.param);
568
36
            mf_format_subfield(&spec->dst, s);
569
36
            ds_put_format(s, "%s", colors.end);
570
36
            if (spec->src.field != spec->dst.field ||
571
36
                spec->src.ofs != spec->dst.ofs) {
572
18
                ds_put_format(s, "%s=%s", colors.param, colors.end);
573
18
                mf_format_subfield(&spec->src, s);
574
18
            }
575
36
            break;
576
577
493
        case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
578
493
            ds_put_format(s, "%sload:%s", colors.special, colors.end);
579
493
            ds_put_hex(s, ofpact_learn_spec_imm(spec), n_bytes);
580
493
            ds_put_format(s, "%s->%s", colors.special, colors.end);
581
493
            mf_format_subfield(&spec->dst, s);
582
493
            break;
583
584
10
        case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
585
10
            ds_put_format(s, "%sload:%s", colors.special, colors.end);
586
10
            mf_format_subfield(&spec->src, s);
587
10
            ds_put_format(s, "%s->%s", colors.special, colors.end);
588
10
            mf_format_subfield(&spec->dst, s);
589
10
            break;
590
591
1.96k
        case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
592
1.96k
            ds_put_format(s, "%soutput:%s", colors.special, colors.end);
593
1.96k
            mf_format_subfield(&spec->src, s);
594
1.96k
            break;
595
38.4k
        }
596
38.4k
    }
597
19.4k
    ds_put_format(s, "%s)%s", colors.learn, colors.end);
598
19.4k
}