Coverage Report

Created: 2023-03-26 07:41

/src/openvswitch/lib/ofp-monitor.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2008-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
#include "openvswitch/ofp-monitor.h"
19
#include "byte-order.h"
20
#include "nx-match.h"
21
#include "ovs-atomic.h"
22
#include "openvswitch/ofp-actions.h"
23
#include "openvswitch/ofp-errors.h"
24
#include "openvswitch/ofp-group.h"
25
#include "openvswitch/ofp-match.h"
26
#include "openvswitch/ofp-meter.h"
27
#include "openvswitch/ofp-msgs.h"
28
#include "openvswitch/ofp-parse.h"
29
#include "openvswitch/ofp-print.h"
30
#include "openvswitch/ofp-table.h"
31
#include "openvswitch/vlog.h"
32
#include "ox-stat.h"
33
34
VLOG_DEFINE_THIS_MODULE(ofp_monitor);
35
36
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
37
38
/* Returns a string form of 'reason'.  The return value is either a statically
39
 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
40
 * 'bufsize' should be at least OFP_FLOW_REMOVED_REASON_BUFSIZE. */
41
const char *
42
ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason,
43
                                  char *reasonbuf, size_t bufsize)
44
0
{
45
0
    switch (reason) {
46
0
    case OFPRR_IDLE_TIMEOUT:
47
0
        return "idle";
48
0
    case OFPRR_HARD_TIMEOUT:
49
0
        return "hard";
50
0
    case OFPRR_DELETE:
51
0
        return "delete";
52
0
    case OFPRR_GROUP_DELETE:
53
0
        return "group_delete";
54
0
    case OFPRR_EVICTION:
55
0
        return "eviction";
56
0
    case OFPRR_METER_DELETE:
57
0
        return "meter_delete";
58
0
    case OVS_OFPRR_NONE:
59
0
    default:
60
0
        snprintf(reasonbuf, bufsize, "%d", (int) reason);
61
0
        return reasonbuf;
62
0
    }
63
0
}
64
65
/* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
66
 * abstract ofputil_flow_removed in 'fr'.  Returns 0 if successful, otherwise
67
 * an OpenFlow error code. */
68
enum ofperr
69
ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
70
                            const struct ofp_header *oh)
71
0
{
72
0
    struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
73
0
    enum ofpraw raw = ofpraw_pull_assert(&b);
74
0
    if (raw == OFPRAW_OFPT15_FLOW_REMOVED) {
75
0
        const struct ofp15_flow_removed *ofr;
76
0
        enum ofperr error;
77
78
0
        ofr = ofpbuf_pull(&b, sizeof *ofr);
79
80
0
        error = ofputil_pull_ofp11_match(&b, NULL, NULL,  &fr->match, NULL);
81
0
        if (error) {
82
0
            return error;
83
0
        }
84
85
0
        struct oxs_stats stats;
86
0
        uint16_t statlen;
87
0
        uint8_t oxs_field_set;
88
0
        error = oxs_pull_stat(&b, &stats, &statlen, &oxs_field_set);
89
0
        if (error) {
90
0
            return error;
91
0
        }
92
93
0
        fr->cookie = ofr->cookie;
94
0
        fr->priority = ntohs(ofr->priority);
95
0
        fr->reason = ofr->reason;
96
0
        fr->table_id = ofr->table_id;
97
0
        fr->duration_sec = stats.duration_sec;
98
0
        fr->duration_nsec = stats.duration_nsec;
99
0
        fr->idle_timeout = ntohs(ofr->idle_timeout);
100
0
        fr->hard_timeout = ntohs(ofr->hard_timeout);
101
0
        fr->packet_count = stats.packet_count;
102
0
        fr->byte_count = stats.byte_count;
103
0
    } else if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
104
0
        const struct ofp12_flow_removed *ofr;
105
0
        enum ofperr error;
106
107
0
        ofr = ofpbuf_pull(&b, sizeof *ofr);
108
109
0
        error = ofputil_pull_ofp11_match(&b, NULL, NULL, &fr->match, NULL);
110
0
        if (error) {
111
0
            return error;
112
0
        }
113
114
0
        fr->priority = ntohs(ofr->priority);
115
0
        fr->cookie = ofr->cookie;
116
0
        fr->reason = ofr->reason;
117
0
        fr->table_id = ofr->table_id;
118
0
        fr->duration_sec = ntohl(ofr->duration_sec);
119
0
        fr->duration_nsec = ntohl(ofr->duration_nsec);
120
0
        fr->idle_timeout = ntohs(ofr->idle_timeout);
121
0
        fr->hard_timeout = ntohs(ofr->hard_timeout);
122
0
        fr->packet_count = ntohll(ofr->packet_count);
123
0
        fr->byte_count = ntohll(ofr->byte_count);
124
0
    } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
125
0
        const struct ofp10_flow_removed *ofr;
126
127
0
        ofr = ofpbuf_pull(&b, sizeof *ofr);
128
129
0
        ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
130
0
        fr->priority = ntohs(ofr->priority);
131
0
        fr->cookie = ofr->cookie;
132
0
        fr->reason = ofr->reason;
133
0
        fr->table_id = 255;
134
0
        fr->duration_sec = ntohl(ofr->duration_sec);
135
0
        fr->duration_nsec = ntohl(ofr->duration_nsec);
136
0
        fr->idle_timeout = ntohs(ofr->idle_timeout);
137
0
        fr->hard_timeout = 0;
138
0
        fr->packet_count = ntohll(ofr->packet_count);
139
0
        fr->byte_count = ntohll(ofr->byte_count);
140
0
    } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
141
0
        struct nx_flow_removed *nfr;
142
0
        enum ofperr error;
143
144
0
        nfr = ofpbuf_pull(&b, sizeof *nfr);
145
0
        error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match, NULL,
146
0
                              NULL, false, NULL, NULL);
147
0
        if (error) {
148
0
            return error;
149
0
        }
150
0
        if (b.size) {
151
0
            return OFPERR_OFPBRC_BAD_LEN;
152
0
        }
153
154
0
        fr->priority = ntohs(nfr->priority);
155
0
        fr->cookie = nfr->cookie;
156
0
        fr->reason = nfr->reason;
157
0
        fr->table_id = nfr->table_id ? nfr->table_id - 1 : 255;
158
0
        fr->duration_sec = ntohl(nfr->duration_sec);
159
0
        fr->duration_nsec = ntohl(nfr->duration_nsec);
160
0
        fr->idle_timeout = ntohs(nfr->idle_timeout);
161
0
        fr->hard_timeout = 0;
162
0
        fr->packet_count = ntohll(nfr->packet_count);
163
0
        fr->byte_count = ntohll(nfr->byte_count);
164
0
    } else {
165
0
        OVS_NOT_REACHED();
166
0
    }
167
168
0
    return 0;
169
0
}
170
171
/* Returns 'count' unchanged except that UINT64_MAX becomes 0.
172
 *
173
 * We use this in situations where OVS internally uses UINT64_MAX to mean
174
 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
175
static uint64_t
176
unknown_to_zero(uint64_t count)
177
0
{
178
0
    return count != UINT64_MAX ? count : 0;
179
0
}
180
181
/* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
182
 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
183
 * message. */
184
struct ofpbuf *
185
ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
186
                            enum ofputil_protocol protocol)
187
0
{
188
0
    struct ofpbuf *msg;
189
0
    enum ofp_flow_removed_reason reason = fr->reason;
190
191
0
    if (reason == OFPRR_METER_DELETE && !(protocol & OFPUTIL_P_OF14_UP)) {
192
0
        reason = OFPRR_DELETE;
193
0
    }
194
195
0
    switch (protocol) {
196
0
    case OFPUTIL_P_OF11_STD:
197
0
    case OFPUTIL_P_OF12_OXM:
198
0
    case OFPUTIL_P_OF13_OXM:
199
0
    case OFPUTIL_P_OF14_OXM: {
200
0
        struct ofp12_flow_removed *ofr;
201
202
0
        msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
203
0
                               ofputil_protocol_to_ofp_version(protocol),
204
0
                               htonl(0),
205
0
                               ofputil_match_typical_len(protocol));
206
0
        ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
207
0
        ofr->cookie = fr->cookie;
208
0
        ofr->priority = htons(fr->priority);
209
0
        ofr->reason = reason;
210
0
        ofr->table_id = fr->table_id;
211
0
        ofr->duration_sec = htonl(fr->duration_sec);
212
0
        ofr->duration_nsec = htonl(fr->duration_nsec);
213
0
        ofr->idle_timeout = htons(fr->idle_timeout);
214
0
        ofr->hard_timeout = htons(fr->hard_timeout);
215
0
        ofr->packet_count = htonll(fr->packet_count);
216
0
        ofr->byte_count = htonll(fr->byte_count);
217
0
        ofputil_put_ofp11_match(msg, &fr->match, protocol);
218
0
        break;
219
0
    }
220
0
    case OFPUTIL_P_OF15_OXM: {
221
0
        struct ofp15_flow_removed *ofr;
222
223
0
        msg = ofpraw_alloc_xid(OFPRAW_OFPT15_FLOW_REMOVED,
224
0
                               ofputil_protocol_to_ofp_version(protocol),
225
0
                               htonl(0),
226
0
                               ofputil_match_typical_len(protocol));
227
0
        ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
228
0
        ofr->cookie = fr->cookie;
229
0
        ofr->priority = htons(fr->priority);
230
0
        ofr->reason = reason;
231
0
        ofr->table_id = fr->table_id;
232
0
        ofr->idle_timeout = htons(fr->idle_timeout);
233
0
        ofr->hard_timeout = htons(fr->hard_timeout);
234
0
        ofputil_put_ofp11_match(msg, &fr->match, protocol);
235
236
0
        const struct oxs_stats oxs = {
237
0
            .duration_sec = fr->duration_sec,
238
0
            .duration_nsec = fr->duration_nsec,
239
0
            .idle_age = UINT32_MAX,
240
0
            .packet_count = fr->packet_count,
241
0
            .byte_count = fr->byte_count,
242
0
            .flow_count = UINT32_MAX,
243
0
        };
244
0
        oxs_put_stats(msg, &oxs);
245
0
        break;
246
0
    }
247
0
    case OFPUTIL_P_OF10_STD:
248
0
    case OFPUTIL_P_OF10_STD_TID: {
249
0
        struct ofp10_flow_removed *ofr;
250
251
0
        msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
252
0
                               htonl(0), 0);
253
0
        ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
254
0
        ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
255
0
        ofr->cookie = fr->cookie;
256
0
        ofr->priority = htons(fr->priority);
257
0
        ofr->reason = reason;
258
0
        ofr->duration_sec = htonl(fr->duration_sec);
259
0
        ofr->duration_nsec = htonl(fr->duration_nsec);
260
0
        ofr->idle_timeout = htons(fr->idle_timeout);
261
0
        ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
262
0
        ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
263
0
        break;
264
0
    }
265
266
0
    case OFPUTIL_P_OF10_NXM:
267
0
    case OFPUTIL_P_OF10_NXM_TID: {
268
0
        struct nx_flow_removed *nfr;
269
0
        int match_len;
270
271
0
        msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
272
0
                               htonl(0), NXM_TYPICAL_LEN);
273
0
        ofpbuf_put_zeros(msg, sizeof *nfr);
274
0
        match_len = nx_put_match(msg, &fr->match, 0, 0);
275
276
0
        nfr = msg->msg;
277
0
        nfr->cookie = fr->cookie;
278
0
        nfr->priority = htons(fr->priority);
279
0
        nfr->reason = reason;
280
0
        nfr->table_id = fr->table_id + 1;
281
0
        nfr->duration_sec = htonl(fr->duration_sec);
282
0
        nfr->duration_nsec = htonl(fr->duration_nsec);
283
0
        nfr->idle_timeout = htons(fr->idle_timeout);
284
0
        nfr->match_len = htons(match_len);
285
0
        nfr->packet_count = htonll(fr->packet_count);
286
0
        nfr->byte_count = htonll(fr->byte_count);
287
0
        break;
288
0
    }
289
290
0
    default:
291
0
        OVS_NOT_REACHED();
292
0
    }
293
294
0
    return msg;
295
0
}
296
297
void
298
ofputil_flow_removed_format(struct ds *s,
299
                            const struct ofputil_flow_removed *fr,
300
                            const struct ofputil_port_map *port_map,
301
                            const struct ofputil_table_map *table_map)
302
0
{
303
0
    char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
304
305
0
    ds_put_char(s, ' ');
306
0
    match_format(&fr->match, port_map, s, fr->priority);
307
308
0
    ds_put_format(s, " reason=%s",
309
0
                  ofp_flow_removed_reason_to_string(fr->reason, reasonbuf,
310
0
                                                    sizeof reasonbuf));
311
312
0
    if (fr->table_id != 255) {
313
0
        ds_put_format(s, " table_id=");
314
0
        ofputil_format_table(fr->table_id, table_map, s);
315
0
    }
316
317
0
    if (fr->cookie != htonll(0)) {
318
0
        ds_put_format(s, " cookie:0x%"PRIx64, ntohll(fr->cookie));
319
0
    }
320
0
    ds_put_cstr(s, " duration");
321
0
    ofp_print_duration(s, fr->duration_sec, fr->duration_nsec);
322
0
    ds_put_format(s, " idle%"PRIu16, fr->idle_timeout);
323
0
    if (fr->hard_timeout) {
324
        /* The hard timeout was only added in OF1.2, so only print it if it is
325
         * actually in use to avoid gratuitous change to the formatting. */
326
0
        ds_put_format(s, " hard%"PRIu16, fr->hard_timeout);
327
0
    }
328
0
    ds_put_format(s, " pkts%"PRIu64" bytes%"PRIu64"\n",
329
0
                  fr->packet_count, fr->byte_count);
330
0
}
331
332
static uint16_t
333
nx_to_ofp_flow_monitor_flags(uint16_t flags)
334
0
{
335
0
    uint16_t oxm_flags = 0;
336
337
0
    if (flags & NXFMF_INITIAL) {
338
0
        oxm_flags |= OFPFMF_INITIAL;
339
0
    }
340
0
    if (flags & NXFMF_ADD) {
341
0
        oxm_flags |= OFPFMF_ADD;
342
0
    }
343
0
    if (flags & NXFMF_DELETE) {
344
0
        oxm_flags |= OFPFMF_REMOVED;
345
0
    }
346
0
    if (flags & NXFMF_MODIFY) {
347
0
        oxm_flags |= OFPFMF_MODIFY;
348
0
    }
349
0
    if (flags & NXFMF_ACTIONS) {
350
0
        oxm_flags |= OFPFMF_INSTRUCTIONS;
351
0
    }
352
0
    if (flags & NXFMF_OWN) {
353
0
        oxm_flags |= OFPFMF_ONLY_OWN;
354
0
    }
355
356
0
    return oxm_flags;
357
0
}
358
359
static uint16_t
360
ofp_to_nx_flow_monitor_flags(uint16_t flags)
361
0
{
362
0
    uint16_t nx_flags = 0;
363
364
0
    if (flags & OFPFMF_INITIAL) {
365
0
        nx_flags |= NXFMF_INITIAL;
366
0
    }
367
0
    if (flags & OFPFMF_ADD) {
368
0
        nx_flags |= NXFMF_ADD;
369
0
    }
370
0
    if (flags & OFPFMF_REMOVED) {
371
0
        nx_flags |= NXFMF_DELETE;
372
0
    }
373
0
    if (flags & OFPFMF_MODIFY) {
374
0
        nx_flags |= NXFMF_MODIFY;
375
0
    }
376
0
    if (flags & OFPFMF_INSTRUCTIONS) {
377
0
        nx_flags |= NXFMF_ACTIONS;
378
0
    }
379
0
    if (flags & OFPFMF_ONLY_OWN) {
380
0
        nx_flags |= NXFMF_OWN;
381
0
    }
382
383
0
    return nx_flags;
384
0
}
385
386
static enum ofp_flow_update_event
387
nx_to_ofp_flow_update_event(enum nx_flow_update_event event)
388
0
{
389
0
    switch (event) {
390
0
    case NXFME_ADDED:
391
0
        return OFPFME_ADDED;
392
0
    case NXFME_DELETED:
393
0
        return OFPFME_REMOVED;
394
0
    case NXFME_MODIFIED:
395
0
        return OFPFME_MODIFIED;
396
0
    case NXFME_ABBREV:
397
0
        return OFPFME_ABBREV;
398
0
     default:
399
0
        OVS_NOT_REACHED();
400
0
    }
401
0
}
402
403
static enum nx_flow_update_event
404
ofp_to_nx_flow_update_event(enum ofp_flow_update_event event)
405
0
{
406
0
    switch (event) {
407
0
    case OFPFME_INITIAL:
408
0
    case OFPFME_ADDED:
409
0
        return NXFME_ADDED;
410
0
    case OFPFME_REMOVED:
411
0
        return NXFME_DELETED;
412
0
    case OFPFME_MODIFIED:
413
0
        return NXFME_MODIFIED;
414
0
    case OFPFME_ABBREV:
415
0
        return NXFME_ABBREV;
416
0
    default:
417
0
    case OFPFME_PAUSED:
418
0
    case OFPFME_RESUMED:
419
0
        OVS_NOT_REACHED();
420
0
    }
421
0
}
422
423

424
/* ofputil_flow_monitor_request */
425
426
/* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
427
 * ofputil_flow_monitor_request in 'rq'.
428
 *
429
 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
430
 * message.  Calling this function multiple times for a single 'msg' iterates
431
 * through the requests.  The caller must initially leave 'msg''s layer
432
 * pointers null and not modify them between calls.
433
 *
434
 * Returns 0 if successful, EOF if no requests were left in this 'msg',
435
 * otherwise an OFPERR_* value. */
436
int
437
ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
438
                                    struct ofpbuf *msg)
439
0
{
440
0
    uint16_t flags;
441
0
    enum ofperr error;
442
0
    enum ofpraw raw;
443
444
0
    error = (msg->header ? ofpraw_decode(&raw, msg->header)
445
0
             : ofpraw_pull(&raw, msg));
446
0
    if (error) {
447
0
        return error;
448
0
    }
449
450
0
    if (!msg->size) {
451
0
        return EOF;
452
0
    }
453
454
0
    switch ((int) raw) {
455
0
    case OFPRAW_NXST_FLOW_MONITOR_REQUEST: {
456
0
        struct nx_flow_monitor_request *nfmr;
457
458
0
        nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
459
0
        if (!nfmr) {
460
0
            VLOG_WARN_RL(&rl, "NXST_FLOW_MONITOR request has %"PRIu32" "
461
0
                    "leftover bytes at end", msg->size);
462
0
            return OFPERR_OFPBRC_BAD_LEN;
463
0
        }
464
465
0
        flags = ntohs(nfmr->flags);
466
0
        if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
467
0
            || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
468
0
                         | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
469
0
            VLOG_WARN_RL(&rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
470
0
                         flags);
471
0
            return OFPERR_OFPMOFC_BAD_FLAGS;
472
0
        }
473
474
0
        if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
475
0
            return OFPERR_NXBRC_MUST_BE_ZERO;
476
0
        }
477
478
0
        rq->id = ntohl(nfmr->id);
479
0
        rq->command = OFPFMC_ADD;
480
0
        rq->flags = nx_to_ofp_flow_monitor_flags(flags);
481
0
        rq->out_port = u16_to_ofp(ntohs(nfmr->out_port));
482
0
        rq->table_id = nfmr->table_id;
483
0
        rq->out_group = OFPG_ANY;
484
485
0
        return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL,
486
0
                NULL, false, NULL, NULL);
487
0
    }
488
0
    case OFPRAW_ONFST13_FLOW_MONITOR_REQUEST: {
489
0
        struct onf_flow_monitor_request *ofmr;
490
491
0
        ofmr = ofpbuf_try_pull(msg, sizeof *ofmr);
492
0
        if (!ofmr) {
493
0
            VLOG_WARN_RL(&rl, "ONFST_FLOW_MONITOR request has %"PRIu32" "
494
0
                         "leftover bytes at end", msg->size);
495
0
            return OFPERR_OFPBRC_BAD_LEN;
496
0
        }
497
498
0
        flags = ntohs(ofmr->flags);
499
0
        if (!(flags & (ONFFMF_ADD | ONFFMF_DELETE | ONFFMF_MODIFY))
500
0
            || flags & ~(ONFFMF_INITIAL | ONFFMF_ADD | ONFFMF_DELETE
501
0
                         | ONFFMF_MODIFY | ONFFMF_ACTIONS | ONFFMF_OWN)) {
502
0
            VLOG_WARN_RL(&rl, "ONFST_FLOW_MONITOR has bad flags %#"PRIx16,
503
0
                         flags);
504
0
            return OFPERR_OFPMOFC_BAD_FLAGS;
505
0
        }
506
507
0
        if (!is_all_zeros(ofmr->zeros, sizeof ofmr->zeros)) {
508
0
            return OFPERR_NXBRC_MUST_BE_ZERO;
509
0
        }
510
511
0
        rq->id = ntohl(ofmr->id);
512
0
        rq->command = OFPFMC_ADD;
513
0
        rq->flags = nx_to_ofp_flow_monitor_flags(flags);
514
0
        error = ofputil_port_from_ofp11(ofmr->out_port, &rq->out_port);
515
0
        if (error) {
516
0
            return error;
517
0
        }
518
0
        rq->table_id = ofmr->table_id;
519
0
        rq->out_group = OFPG_ANY;
520
521
0
        return ofputil_pull_ofp11_match(msg, NULL, NULL, &rq->match, NULL);
522
0
    }
523
0
    case OFPRAW_OFPST14_FLOW_MONITOR_REQUEST: {
524
0
        struct ofp14_flow_monitor_request *ofmr;
525
526
0
        ofmr = ofpbuf_try_pull(msg, sizeof *ofmr);
527
0
        if (!ofmr) {
528
0
            VLOG_WARN_RL(&rl, "OFPST_FLOW_MONITOR request has %"PRIu32" "
529
0
                    "leftover bytes at end", msg->size);
530
0
            return OFPERR_OFPBRC_BAD_LEN;
531
0
        }
532
533
0
        flags = ntohs(ofmr->flags);
534
0
        rq->id = ntohl(ofmr->monitor_id);
535
0
        rq->command = ofmr->command;
536
537
0
        if (ofmr->command == OFPFMC_DELETE) {
538
0
            return ofputil_pull_ofp11_match(msg, NULL, NULL, &rq->match, NULL);
539
0
        }
540
541
0
        if (!(flags & (OFPFMF_ADD | OFPFMF_REMOVED | OFPFMF_MODIFY))
542
0
                || flags & ~(OFPFMF_INITIAL | OFPFMF_ADD | OFPFMF_REMOVED
543
0
                    | OFPFMF_MODIFY | OFPFMF_INSTRUCTIONS | OFPFMF_ONLY_OWN)) {
544
0
            VLOG_WARN_RL(&rl, "OFPST_FLOW_MONITOR has bad flags %#"PRIx16,
545
0
                         flags);
546
0
            return OFPERR_OFPMOFC_BAD_FLAGS;
547
0
        }
548
549
0
        rq->command = ofmr->command;
550
0
        rq->flags = flags;
551
0
        error = ofputil_port_from_ofp11(ofmr->out_port, &rq->out_port);
552
0
        if (error) {
553
0
            return error;
554
0
        }
555
0
        rq->out_group = ntohl(ofmr->out_group);
556
0
        rq->table_id = ofmr->table_id;
557
558
0
        return ofputil_pull_ofp11_match(msg, NULL, NULL, &rq->match, NULL);
559
0
    }
560
0
    default:
561
0
        OVS_NOT_REACHED();
562
0
    }
563
0
}
564
565
void
566
ofputil_append_flow_monitor_request(
567
    const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg,
568
    enum ofputil_protocol protocol)
569
0
{
570
0
    size_t start_ofs;
571
0
    int match_len;
572
0
    enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
573
574
0
    if (!msg->size) {
575
0
        switch (version) {
576
0
        case OFP10_VERSION:
577
0
        case OFP11_VERSION:
578
0
        case OFP12_VERSION: {
579
0
            struct nx_flow_monitor_request *nfmr;
580
581
0
            if (!msg->size) {
582
0
                ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, version, msg);
583
0
            }
584
585
0
            start_ofs = msg->size;
586
0
            ofpbuf_put_zeros(msg, sizeof *nfmr);
587
0
            match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
588
589
0
            nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
590
0
            nfmr->id = htonl(rq->id);
591
0
            nfmr->flags = htons(ofp_to_nx_flow_monitor_flags(rq->flags));
592
0
            nfmr->out_port = htons(ofp_to_u16(rq->out_port));
593
0
            nfmr->match_len = htons(match_len);
594
0
            nfmr->table_id = rq->table_id;
595
0
            break;
596
0
        }
597
0
        case OFP13_VERSION: {
598
0
            struct onf_flow_monitor_request *ofmr;
599
600
0
            if (!msg->size) {
601
0
                ofpraw_put(OFPRAW_ONFST13_FLOW_MONITOR_REQUEST, version, msg);
602
0
            }
603
604
0
            start_ofs = msg->size;
605
0
            ofpbuf_put_zeros(msg, sizeof *ofmr);
606
0
            match_len = oxm_put_match(msg, &rq->match, version);
607
608
0
            ofmr = ofpbuf_at_assert(msg, start_ofs, sizeof *ofmr);
609
0
            ofmr->id = htonl(rq->id);
610
0
            ofmr->flags = htons(ofp_to_nx_flow_monitor_flags(rq->flags));
611
0
            ofmr->match_len = htons(match_len);
612
0
            ofmr->out_port = ofputil_port_to_ofp11(rq->out_port);
613
0
            ofmr->table_id = rq->table_id;
614
0
            break;
615
0
        }
616
0
        case OFP14_VERSION:
617
0
        case OFP15_VERSION: {
618
0
            struct ofp14_flow_monitor_request *ofmr;
619
620
0
            if (!msg->size) {
621
0
                ofpraw_put(OFPRAW_OFPST14_FLOW_MONITOR_REQUEST, version, msg);
622
0
            }
623
624
0
            start_ofs = msg->size;
625
0
            ofpbuf_put_zeros(msg, sizeof *ofmr);
626
0
            oxm_put_match(msg, &rq->match, version);
627
628
0
            ofmr = ofpbuf_at_assert(msg, start_ofs, sizeof *ofmr);
629
0
            ofmr->monitor_id = htonl(rq->id);
630
0
            ofmr->command = OFPFMC_ADD;
631
0
            ofmr->out_port = ofputil_port_to_ofp11(rq->out_port);
632
0
            ofmr->out_group = htonl(rq->out_group);
633
0
            ofmr->flags = htons(rq->flags);
634
0
            ofmr->table_id = rq->table_id;
635
0
            break;
636
0
        }
637
0
        default:
638
0
            OVS_NOT_REACHED();
639
0
        }
640
0
    }
641
0
}
642
643
static const char *
644
ofp_flow_monitor_flags_to_name(uint32_t bit)
645
0
{
646
0
    enum ofp14_flow_monitor_flags fmf = bit;
647
648
0
    switch (fmf) {
649
0
    case OFPFMF_INITIAL: return "initial";
650
0
    case OFPFMF_ADD: return "add";
651
0
    case OFPFMF_REMOVED: return "delete";
652
0
    case OFPFMF_MODIFY: return "modify";
653
0
    case OFPFMF_INSTRUCTIONS: return "actions";
654
0
    case OFPFMF_NO_ABBREV: return "no-abbrev";
655
0
    case OFPFMF_ONLY_OWN: return "own";
656
0
    }
657
658
0
    return NULL;
659
0
}
660
661
static const char *
662
ofp_flow_monitor_command_to_string(enum ofp14_flow_monitor_command command)
663
0
{
664
0
    switch (command) {
665
0
    case OFPFMC_ADD: return "add";
666
0
    case OFPFMC_MODIFY: return "modify";
667
0
    case OFPFMC_DELETE: return "delete";
668
0
    default:
669
0
        OVS_NOT_REACHED();
670
0
    }
671
0
}
672
673
void
674
ofputil_flow_monitor_request_format(
675
    struct ds *s, const struct ofputil_flow_monitor_request *request,
676
    const struct ofputil_port_map *port_map,
677
    const struct ofputil_table_map *table_map)
678
0
{
679
0
    if (request->command == OFPFMC_DELETE) {
680
0
        ds_put_format(s, "\n id=%"PRIu32" command=%s", request->id,
681
0
                      ofp_flow_monitor_command_to_string(request->command));
682
0
        return;
683
0
    }
684
0
    ds_put_format(s, "\n id=%"PRIu32" flags=", request->id);
685
0
    ofp_print_bit_names(s, request->flags,
686
0
                        ofp_flow_monitor_flags_to_name, ',');
687
688
0
    if (request->out_port != OFPP_NONE) {
689
0
        ds_put_cstr(s, " out_port=");
690
0
        ofputil_format_port(request->out_port, port_map, s);
691
0
    }
692
693
0
    if (request->out_group && (request->out_group != OFPG_ANY)) {
694
0
        ds_put_format(s, " out_group=%d", request->out_group);
695
0
    }
696
697
0
    if (request->table_id != 0xff) {
698
0
        ds_put_format(s, " table=");
699
0
        ofputil_format_table(request->table_id, table_map, s);
700
0
    }
701
702
0
    if (request->command != OFPFMC_DELETE) {
703
0
        ds_put_char(s, ' ');
704
0
        match_format(&request->match, port_map, s, OFP_DEFAULT_PRIORITY);
705
0
        ds_chomp(s, ' ');
706
0
    }
707
0
}
708
709
static char * OVS_WARN_UNUSED_RESULT
710
parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
711
                             const char *str_,
712
                             const struct ofputil_port_map *port_map,
713
                             const struct ofputil_table_map *table_map,
714
                             char *string,
715
                             enum ofputil_protocol *usable_protocols)
716
0
{
717
0
    static atomic_count id = ATOMIC_COUNT_INIT(0);
718
0
    char *name, *value;
719
720
0
    fmr->id = atomic_count_inc(&id);
721
722
0
    fmr->flags = (OFPFMF_INITIAL | OFPFMF_ADD | OFPFMF_REMOVED | OFPFMF_MODIFY
723
0
                  | OFPFMF_ONLY_OWN | OFPFMF_INSTRUCTIONS);
724
0
    fmr->out_port = OFPP_NONE;
725
0
    fmr->out_group = OFPG_ANY;
726
0
    fmr->table_id = 0xff;
727
0
    match_init_catchall(&fmr->match);
728
729
0
    *usable_protocols = OFPUTIL_P_ANY;
730
0
    while (ofputil_parse_key_value(&string, &name, &value)) {
731
0
        const struct ofp_protocol *p;
732
0
        char *error = NULL;
733
734
0
        if (!strcmp(name, "!initial")) {
735
0
            fmr->flags &= ~OFPFMF_INITIAL;
736
0
        } else if (!strcmp(name, "!add")) {
737
0
            fmr->flags &= ~OFPFMF_ADD;
738
0
        } else if (!strcmp(name, "!delete")) {
739
0
            fmr->flags &= ~OFPFMF_REMOVED;
740
0
        } else if (!strcmp(name, "!modify")) {
741
0
            fmr->flags &= ~OFPFMF_MODIFY;
742
0
        } else if (!strcmp(name, "!actions")) {
743
0
            fmr->flags &= ~OFPFMF_INSTRUCTIONS;
744
0
        } else if (!strcmp(name, "!abbrev")) {
745
0
            fmr->flags &= ~OFPFMF_NO_ABBREV;
746
0
        } else if (!strcmp(name, "!own")) {
747
0
            fmr->flags &= ~OFPFMF_ONLY_OWN;
748
0
        } else if (ofp_parse_protocol(name, &p)) {
749
0
            match_set_dl_type(&fmr->match, htons(p->dl_type));
750
0
            if (p->nw_proto) {
751
0
                match_set_nw_proto(&fmr->match, p->nw_proto);
752
0
            }
753
0
        } else if (mf_from_name(name)) {
754
0
            error = ofp_parse_field(mf_from_name(name), value, port_map,
755
0
                                    &fmr->match, usable_protocols);
756
0
            if (!error && !(*usable_protocols & OFPUTIL_P_OF10_ANY)) {
757
0
                return xasprintf("%s: match field is not supported for "
758
0
                                 "flow monitor", name);
759
0
            }
760
0
        } else {
761
0
            if (!*value) {
762
0
                return xasprintf("%s: field %s missing value", str_, name);
763
0
            }
764
765
0
            if (!strcmp(name, "table")) {
766
0
                if (!ofputil_table_from_string(value, table_map,
767
0
                                               &fmr->table_id)) {
768
0
                    error = xasprintf("unknown table \"%s\"", value);
769
0
                }
770
0
            } else if (!strcmp(name, "out_port")) {
771
0
                fmr->out_port = u16_to_ofp(atoi(value));
772
0
            } else if (!strcmp(name, "out_group")) {
773
0
                fmr->out_group = atoi(value);
774
0
            } else {
775
0
                return xasprintf("%s: unknown keyword %s", str_, name);
776
0
            }
777
0
        }
778
779
0
        if (error) {
780
0
            return error;
781
0
        }
782
0
    }
783
0
    return NULL;
784
0
}
785
786
/* Convert 'str_' (as described in the documentation for the "monitor" command
787
 * in the ovs-ofctl man page) into 'fmr'.
788
 *
789
 * Returns NULL if successful, otherwise a malloc()'d string describing the
790
 * error.  The caller is responsible for freeing the returned string. */
791
char * OVS_WARN_UNUSED_RESULT
792
parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
793
                           const char *str_,
794
                           const struct ofputil_port_map *port_map,
795
                           const struct ofputil_table_map *table_map,
796
                           enum ofputil_protocol *usable_protocols)
797
0
{
798
0
    char *string = xstrdup(str_);
799
0
    char *error = parse_flow_monitor_request__(fmr, str_, port_map, table_map,
800
0
                                               string, usable_protocols);
801
0
    free(string);
802
0
    return error;
803
0
}
804
805
/* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
806
 * into an abstract ofputil_flow_update in 'update'.  The caller must have
807
 * initialized update->match to point to space allocated for a match.
808
 *
809
 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
810
 * actions (except for NXFME_ABBREV, which never includes actions).  The caller
811
 * must initialize 'ofpacts' and retains ownership of it.  'update->ofpacts'
812
 * will point into the 'ofpacts' buffer.
813
 *
814
 * Multiple flow updates can be packed into a single OpenFlow message.  Calling
815
 * this function multiple times for a single 'msg' iterates through the
816
 * updates.  The caller must initially leave 'msg''s layer pointers null and
817
 * not modify them between calls.
818
 *
819
 * Returns 0 if successful, EOF if no updates were left in this 'msg',
820
 * otherwise an OFPERR_* value. */
821
int
822
ofputil_decode_flow_update(struct ofputil_flow_update *update,
823
                           struct ofpbuf *msg, struct ofpbuf *ofpacts)
824
0
{
825
0
    unsigned int length;
826
0
    struct ofp_header *oh;
827
0
    enum ofperr error;
828
0
    enum ofpraw raw;
829
830
0
    if (!msg->header) {
831
0
        ofpraw_pull_assert(msg);
832
0
    }
833
834
0
    error = ofpraw_decode(&raw, msg->header);
835
0
    if (error) {
836
0
        return error;
837
0
    }
838
839
0
    ofpbuf_clear(ofpacts);
840
0
    if (!msg->size) {
841
0
        return EOF;
842
0
    }
843
844
0
    oh = msg->header;
845
846
0
    switch ((int) raw) {
847
0
    case OFPRAW_ONFST13_FLOW_MONITOR_REPLY:
848
0
    case OFPRAW_NXST_FLOW_MONITOR_REPLY: {
849
0
        struct nx_flow_update_header *nfuh;
850
0
        enum nx_flow_update_event nx_event;
851
852
0
        if (msg->size < sizeof(struct nx_flow_update_header)) {
853
0
            goto bad_len;
854
0
        }
855
856
0
        nfuh = msg->data;
857
0
        length = ntohs(nfuh->length);
858
0
        if (length > msg->size || length % 8) {
859
0
            goto bad_len;
860
0
        }
861
862
0
        nx_event = ntohs(nfuh->event);
863
0
        if (nx_event == NXFME_ABBREV) {
864
0
            struct nx_flow_update_abbrev *nfua;
865
866
0
            if (length != sizeof *nfua) {
867
0
                goto bad_len;
868
0
            }
869
870
0
            nfua = ofpbuf_pull(msg, sizeof *nfua);
871
0
            update->xid = nfua->xid;
872
0
        } else if (nx_event == NXFME_ADDED
873
0
                   || nx_event == NXFME_DELETED
874
0
                   || nx_event == NXFME_MODIFIED) {
875
0
            struct nx_flow_update_full *nfuf;
876
0
            unsigned int actions_len;
877
0
            unsigned int match_len;
878
879
0
            if (length < sizeof *nfuf) {
880
0
                goto bad_len;
881
0
            }
882
883
0
            nfuf = ofpbuf_pull(msg, sizeof *nfuf);
884
0
            match_len = ntohs(nfuf->match_len);
885
0
            if (sizeof *nfuf + match_len > length) {
886
0
                goto bad_len;
887
0
            }
888
889
0
            update->reason = ntohs(nfuf->reason);
890
0
            update->idle_timeout = ntohs(nfuf->idle_timeout);
891
0
            update->hard_timeout = ntohs(nfuf->hard_timeout);
892
0
            update->table_id = nfuf->table_id;
893
0
            update->cookie = nfuf->cookie;
894
0
            update->priority = ntohs(nfuf->priority);
895
896
0
            if (raw == OFPRAW_ONFST13_FLOW_MONITOR_REPLY) {
897
0
                uint16_t padded_match_len = 0;
898
0
                unsigned int instructions_len;
899
900
0
                error = ofputil_pull_ofp11_match(
901
0
                    msg, NULL, NULL, &update->match, &padded_match_len);
902
0
                if (error) {
903
0
                    return error;
904
0
                }
905
906
0
                instructions_len = length - sizeof *nfuf - padded_match_len;
907
0
                error = ofpacts_pull_openflow_instructions(
908
0
                    msg, instructions_len, oh->version, NULL, NULL, ofpacts);
909
0
                if (error) {
910
0
                    return error;
911
0
                }
912
0
            } else {
913
0
                error = nx_pull_match(msg, match_len, &update->match, NULL,
914
0
                                      NULL, false, NULL, NULL);
915
0
                if (error) {
916
0
                    return error;
917
0
                }
918
919
0
                actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
920
0
                error = ofpacts_pull_openflow_actions(
921
0
                    msg, actions_len, oh->version, NULL, NULL, ofpacts);
922
0
                if (error) {
923
0
                    return error;
924
0
                }
925
0
            }
926
927
0
            update->ofpacts = ofpacts->data;
928
0
            update->ofpacts_len = ofpacts->size;
929
0
        } else {
930
0
            VLOG_WARN_RL(&rl, "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
931
0
                         ntohs(nfuh->event));
932
0
            return OFPERR_NXBRC_FM_BAD_EVENT;
933
0
        }
934
935
0
        update->event = nx_to_ofp_flow_update_event(nx_event);
936
0
        return 0;
937
0
    }
938
0
    case OFPRAW_OFPST14_FLOW_MONITOR_REPLY: {
939
0
        struct ofp_flow_update_header *ofuh;
940
0
        uint16_t padded_match_len = 0;
941
942
0
        if (msg->size < sizeof(struct ofp_flow_update_header)) {
943
0
            goto bad_len;
944
0
        }
945
946
0
        ofuh = msg->data;
947
0
        update->event = ntohs(ofuh->event);
948
0
        length = ntohs(ofuh->length);
949
0
        if (length > msg->size || length % 8) {
950
0
            goto bad_len;
951
0
        }
952
953
0
        if (update->event == OFPFME_ABBREV) {
954
0
            struct ofp_flow_update_abbrev *ofua;
955
956
0
            if (length != sizeof *ofua) {
957
0
                goto bad_len;
958
0
            }
959
960
0
            ofua = ofpbuf_pull(msg, sizeof *ofua);
961
0
            update->xid = ofua->xid;
962
0
            return 0;
963
0
        } else if (update->event == OFPFME_PAUSED
964
0
                   || update->event == OFPFME_RESUMED) {
965
0
            struct ofp_flow_update_paused *ofup;
966
967
0
            if (length != sizeof *ofup) {
968
0
                goto bad_len;
969
0
            }
970
971
0
            ofup = ofpbuf_pull(msg, sizeof *ofup);
972
0
            return 0;
973
0
        } else if (update->event == OFPFME_INITIAL
974
0
                   || update->event == OFPFME_ADDED
975
0
                   || update->event == OFPFME_REMOVED
976
0
                   || update->event == OFPFME_MODIFIED) {
977
0
            struct ofp_flow_update_full *ofuf;
978
0
            unsigned int instructions_len;
979
980
0
            if (length < sizeof *ofuf) {
981
0
                goto bad_len;
982
0
            }
983
984
0
            ofuf = ofpbuf_pull(msg, sizeof *ofuf);
985
0
            if (sizeof *ofuf > length) {
986
0
                goto bad_len;
987
0
            }
988
989
0
            update->reason = ofuf->reason;
990
0
            update->idle_timeout = ntohs(ofuf->idle_timeout);
991
0
            update->hard_timeout = ntohs(ofuf->hard_timeout);
992
0
            update->table_id = ofuf->table_id;
993
0
            update->cookie = ofuf->cookie;
994
0
            update->priority = ntohs(ofuf->priority);
995
996
0
            error = ofputil_pull_ofp11_match(
997
0
                msg, NULL, NULL, &update->match, &padded_match_len);
998
0
            if (error) {
999
0
                return error;
1000
0
            }
1001
1002
0
            instructions_len = length - sizeof *ofuf - padded_match_len;
1003
0
            error = ofpacts_pull_openflow_instructions(
1004
0
                msg, instructions_len, oh->version, NULL, NULL, ofpacts);
1005
0
            if (error) {
1006
0
                return error;
1007
0
            }
1008
1009
0
            update->ofpacts = ofpacts->data;
1010
0
            update->ofpacts_len = ofpacts->size;
1011
0
            return 0;
1012
0
        } else {
1013
0
            VLOG_WARN_RL(&rl, "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
1014
0
                         ntohs(ofuh->event));
1015
0
            return OFPERR_NXBRC_FM_BAD_EVENT;
1016
0
        }
1017
0
    }
1018
0
    default:
1019
0
        OVS_NOT_REACHED();
1020
0
    }
1021
0
bad_len:
1022
0
    VLOG_WARN_RL(&rl, "%s has %"PRIu32" leftover bytes at end",
1023
0
                 ofpraw_get_name(raw), msg->size);
1024
0
    return OFPERR_OFPBRC_BAD_LEN;
1025
0
}
1026
1027
uint32_t
1028
ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
1029
0
{
1030
0
    enum ofperr error;
1031
0
    enum ofpraw raw;
1032
1033
0
    error = ofpraw_decode(&raw, oh);
1034
0
    if (error) {
1035
0
        return error;
1036
0
    }
1037
1038
0
    switch ((int) raw) {
1039
0
    case OFPRAW_ONFT13_FLOW_MONITOR_CANCEL:
1040
0
    case OFPRAW_NXT_FLOW_MONITOR_CANCEL: {
1041
0
        const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
1042
0
        return ntohl(cancel->id);
1043
0
    }
1044
0
    default:
1045
0
        OVS_NOT_REACHED();
1046
0
    }
1047
0
}
1048
1049
struct ofpbuf *
1050
ofputil_encode_flow_monitor_cancel(uint32_t id, enum ofputil_protocol protocol)
1051
0
{
1052
0
    struct nx_flow_monitor_cancel *nfmc;
1053
0
    enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
1054
0
    struct ofpbuf *msg;
1055
1056
0
    switch (version) {
1057
0
    case OFP10_VERSION:
1058
0
    case OFP11_VERSION:
1059
0
    case OFP12_VERSION:
1060
0
    case OFP13_VERSION: {
1061
0
        if (version == OFP13_VERSION) {
1062
0
            msg = ofpraw_alloc(OFPRAW_ONFT13_FLOW_MONITOR_CANCEL, version, 0);
1063
0
        } else {
1064
0
            msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, version, 0);
1065
0
        }
1066
0
        nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
1067
0
        nfmc->id = htonl(id);
1068
0
        break;
1069
0
    }
1070
0
    case OFP14_VERSION:
1071
0
    case OFP15_VERSION: {
1072
0
        struct ofp14_flow_monitor_request *ofmr;
1073
1074
0
        msg = ofpbuf_new(0);
1075
1076
0
        ofpraw_put(OFPRAW_OFPST14_FLOW_MONITOR_REQUEST, version, msg);
1077
1078
0
        size_t start_ofs = msg->size;
1079
0
        ofpbuf_put_zeros(msg, sizeof *ofmr);
1080
1081
0
        ofmr = ofpbuf_at_assert(msg, start_ofs, sizeof *ofmr);
1082
0
        ofmr->monitor_id = htonl(id);
1083
0
        ofmr->command = OFPFMC_DELETE;
1084
0
        break;
1085
0
    }
1086
0
    default:
1087
0
        OVS_NOT_REACHED();
1088
0
    }
1089
0
    return msg;
1090
0
}
1091
1092
struct ofpbuf *
1093
ofputil_encode_flow_monitor_pause(enum ofp_flow_update_event command,
1094
                                  enum ofputil_protocol protocol)
1095
0
{
1096
0
    struct ofpbuf *msg;
1097
0
    enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
1098
1099
0
    if (!(command == OFPFME_PAUSED || command == OFPFME_RESUMED)) {
1100
0
        OVS_NOT_REACHED();
1101
0
    }
1102
1103
0
    switch (version) {
1104
0
    case OFP10_VERSION:
1105
0
    case OFP11_VERSION:
1106
0
    case OFP12_VERSION:
1107
0
        if (command == OFPFME_PAUSED) {
1108
0
            msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_PAUSED,
1109
0
                                   version, htonl(0), 0);
1110
0
        } else {
1111
0
            msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_RESUMED,
1112
0
                                   version, htonl(0), 0);
1113
0
        }
1114
0
        break;
1115
0
    case OFP13_VERSION:
1116
0
        if (command == OFPFME_PAUSED) {
1117
0
            msg = ofpraw_alloc_xid(OFPRAW_ONFT13_FLOW_MONITOR_PAUSED,
1118
0
                                   version, htonl(0), 0);
1119
0
        } else {
1120
0
            msg = ofpraw_alloc_xid(OFPRAW_ONFT13_FLOW_MONITOR_RESUMED,
1121
0
                                   version, htonl(0), 0);
1122
0
        }
1123
0
        break;
1124
0
    case OFP14_VERSION:
1125
0
    case OFP15_VERSION: {
1126
0
        msg = ofpraw_alloc_xid(OFPRAW_OFPST14_FLOW_MONITOR_REPLY, version,
1127
0
                                                   htonl(0), 1024);
1128
0
        struct ofp_flow_update_header *ofuh;
1129
0
        size_t start_ofs = msg->size;
1130
1131
0
        struct ofp_flow_update_paused *ofup;
1132
1133
0
        ofpbuf_put_zeros(msg, sizeof *ofup);
1134
0
        ofup = ofpbuf_at_assert(msg, start_ofs, sizeof *ofup);
1135
0
        ofup->event = htons(command);
1136
0
        ofup->length = htons(8);
1137
1138
0
        ofuh = ofpbuf_at_assert(msg, start_ofs, sizeof *ofuh);
1139
0
        ofuh->length = htons(msg->size - start_ofs);
1140
0
        ofuh->event = htons(command);
1141
1142
0
        ofpmsg_update_length(msg);
1143
0
        break;
1144
0
    }
1145
0
    default:
1146
0
        OVS_NOT_REACHED();
1147
0
    }
1148
1149
0
    return msg;
1150
0
}
1151
1152
void
1153
ofputil_start_flow_update(struct ovs_list *replies,
1154
                          enum ofputil_protocol protocol)
1155
0
{
1156
0
    struct ofpbuf *msg;
1157
0
    enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
1158
1159
0
    switch (version) {
1160
0
    case OFP10_VERSION:
1161
0
    case OFP11_VERSION:
1162
0
    case OFP12_VERSION:
1163
0
        msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, version,
1164
0
                               htonl(0), 1024);
1165
0
        break;
1166
0
    case OFP13_VERSION:
1167
0
        msg = ofpraw_alloc_xid(OFPRAW_ONFST13_FLOW_MONITOR_REPLY, version,
1168
0
                               htonl(0), 1024);
1169
0
        break;
1170
0
    case OFP14_VERSION:
1171
0
    case OFP15_VERSION:
1172
0
        msg = ofpraw_alloc_xid(OFPRAW_OFPST14_FLOW_MONITOR_REPLY, version,
1173
0
                               htonl(0), 1024);
1174
0
        break;
1175
0
    default:
1176
0
        OVS_NOT_REACHED();
1177
0
    }
1178
1179
0
    ovs_list_init(replies);
1180
0
    ovs_list_push_back(replies, &msg->list_node);
1181
0
}
1182
1183
void
1184
ofputil_append_flow_update(const struct ofputil_flow_update *update,
1185
                           struct ovs_list *replies,
1186
                           const struct tun_table *tun_table)
1187
0
{
1188
0
    struct ofputil_flow_update *update_ =
1189
0
        CONST_CAST(struct ofputil_flow_update *, update);
1190
0
    const struct tun_table *orig_tun_table;
1191
0
    enum ofp_version version = ofpmp_version(replies);
1192
0
    struct ofpbuf *msg;
1193
0
    size_t start_ofs;
1194
1195
0
    orig_tun_table = update->match.flow.tunnel.metadata.tab;
1196
0
    update_->match.flow.tunnel.metadata.tab = tun_table;
1197
1198
0
    msg = ofpbuf_from_list(ovs_list_back(replies));
1199
0
    start_ofs = msg->size;
1200
1201
0
    switch (version) {
1202
0
        case OFP10_VERSION:
1203
0
        case OFP11_VERSION:
1204
0
        case OFP12_VERSION:
1205
0
        case OFP13_VERSION: {
1206
0
             struct nx_flow_update_header *nfuh;
1207
1208
0
            if (update->event == OFPFME_ABBREV) {
1209
0
                struct nx_flow_update_abbrev *nfua;
1210
1211
0
                nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
1212
0
                nfua->xid = update->xid;
1213
0
            } else {
1214
0
                struct nx_flow_update_full *nfuf;
1215
0
                int match_len;
1216
1217
0
                ofpbuf_put_zeros(msg, sizeof *nfuf);
1218
0
                if (version == OFP13_VERSION) {
1219
0
                    match_len = oxm_put_match(msg, &update->match, version);
1220
0
                    ofpacts_put_openflow_instructions(
1221
0
                        update->ofpacts, update->ofpacts_len, msg, version);
1222
0
                } else {
1223
0
                    match_len = nx_put_match(msg, &update->match,
1224
0
                                             htonll(0), htonll(0));
1225
0
                    ofpacts_put_openflow_actions(
1226
0
                        update->ofpacts, update->ofpacts_len, msg, version);
1227
0
                }
1228
0
                nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
1229
0
                nfuf->reason = htons(update->reason);
1230
0
                nfuf->priority = htons(update->priority);
1231
0
                nfuf->idle_timeout = htons(update->idle_timeout);
1232
0
                nfuf->hard_timeout = htons(update->hard_timeout);
1233
0
                nfuf->match_len = htons(match_len);
1234
0
                nfuf->table_id = update->table_id;
1235
0
                nfuf->cookie = update->cookie;
1236
0
            }
1237
1238
0
            nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
1239
0
            nfuh->length = htons(msg->size - start_ofs);
1240
0
            nfuh->event = htons(ofp_to_nx_flow_update_event(update->event));
1241
0
            break;
1242
0
        }
1243
0
        case OFP14_VERSION:
1244
0
        case OFP15_VERSION: {
1245
0
            struct ofp_flow_update_header *ofuh;
1246
1247
0
            if (update->event == OFPFME_ABBREV) {
1248
0
                struct ofp_flow_update_abbrev *ofua;
1249
1250
0
                ofua = ofpbuf_put_zeros(msg, sizeof *ofua);
1251
0
                ofua->xid = update->xid;
1252
0
            } else {
1253
0
                struct ofp_flow_update_full *ofuf;
1254
1255
0
                ofpbuf_put_zeros(msg, sizeof *ofuf);
1256
0
                oxm_put_match(msg, &update->match, version);
1257
0
                ofpacts_put_openflow_instructions(update->ofpacts,
1258
0
                                                  update->ofpacts_len,
1259
0
                                                  msg, version);
1260
0
                ofuf = ofpbuf_at_assert(msg, start_ofs, sizeof *ofuf);
1261
0
                ofuf->reason = update->reason;
1262
0
                ofuf->priority = htons(update->priority);
1263
0
                ofuf->idle_timeout = htons(update->idle_timeout);
1264
0
                ofuf->hard_timeout = htons(update->hard_timeout);
1265
0
                ofuf->table_id = update->table_id;
1266
0
                ofuf->cookie = update->cookie;
1267
0
            }
1268
1269
0
            ofuh = ofpbuf_at_assert(msg, start_ofs, sizeof *ofuh);
1270
0
            ofuh->length = htons(msg->size - start_ofs);
1271
0
            ofuh->event = htons(update->event);
1272
0
            break;
1273
0
        }
1274
0
    }
1275
1276
0
    ofpmp_postappend(replies, start_ofs);
1277
0
    update_->match.flow.tunnel.metadata.tab = orig_tun_table;
1278
0
}
1279
1280
void
1281
ofputil_flow_update_format(struct ds *s,
1282
                           const struct ofputil_flow_update *update,
1283
                           const struct ofputil_port_map *port_map,
1284
                           const struct ofputil_table_map *table_map)
1285
0
{
1286
0
    char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
1287
1288
0
    ds_put_cstr(s, "\n event=");
1289
0
    switch (update->event) {
1290
0
    case OFPFME_INITIAL:
1291
0
        ds_put_cstr(s, "INITIAL");
1292
0
        break;
1293
1294
0
    case OFPFME_ADDED:
1295
0
        ds_put_cstr(s, "ADDED");
1296
0
        break;
1297
1298
0
    case OFPFME_REMOVED:
1299
0
        ds_put_format(s, "DELETED reason=%s",
1300
0
                      ofp_flow_removed_reason_to_string(update->reason,
1301
0
                                                        reasonbuf,
1302
0
                                                        sizeof reasonbuf));
1303
0
        break;
1304
1305
0
    case OFPFME_MODIFIED:
1306
0
        ds_put_cstr(s, "MODIFIED");
1307
0
        break;
1308
1309
0
    case OFPFME_ABBREV:
1310
0
        ds_put_format(s, "ABBREV xid=0x%"PRIx32, ntohl(update->xid));
1311
0
        return;
1312
1313
0
    case OFPFME_PAUSED:
1314
0
        ds_put_cstr(s, "PAUSED");
1315
0
        return;
1316
1317
0
    case OFPFME_RESUMED:
1318
0
        ds_put_cstr(s, "RESUMED");
1319
0
        return;
1320
1321
0
    }
1322
1323
0
    ds_put_format(s, " table=");
1324
0
    ofputil_format_table(update->table_id, table_map, s);
1325
0
    if (update->idle_timeout != OFP_FLOW_PERMANENT) {
1326
0
        ds_put_format(s, " idle_timeout=%"PRIu16, update->idle_timeout);
1327
0
    }
1328
0
    if (update->hard_timeout != OFP_FLOW_PERMANENT) {
1329
0
        ds_put_format(s, " hard_timeout=%"PRIu16, update->hard_timeout);
1330
0
    }
1331
0
    ds_put_format(s, " cookie=%#"PRIx64, ntohll(update->cookie));
1332
1333
0
    ds_put_char(s, ' ');
1334
0
    match_format(&update->match, port_map, s, OFP_DEFAULT_PRIORITY);
1335
1336
0
    if (update->ofpacts_len) {
1337
0
        if (s->string[s->length - 1] != ' ') {
1338
0
            ds_put_char(s, ' ');
1339
0
        }
1340
0
        ds_put_cstr(s, "actions=");
1341
0
        struct ofpact_format_params fp = {
1342
0
            .port_map = port_map,
1343
0
            .table_map = table_map,
1344
0
            .s = s,
1345
0
        };
1346
0
        ofpacts_format(update->ofpacts, update->ofpacts_len, &fp);
1347
0
    }
1348
0
}
1349

1350
/* Encodes 'rf' according to 'protocol', and returns the encoded message. */
1351
struct ofpbuf *
1352
ofputil_encode_requestforward(const struct ofputil_requestforward *rf,
1353
                              enum ofputil_protocol protocol)
1354
0
{
1355
0
    enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
1356
0
    enum ofpraw raw_msg_type;
1357
0
    struct ofpbuf *inner;
1358
1359
0
    switch (rf->reason) {
1360
0
    case OFPRFR_GROUP_MOD:
1361
0
        inner = ofputil_encode_group_mod(ofp_version, rf->group_mod,
1362
0
                                         rf->new_buckets, rf->group_existed);
1363
0
        break;
1364
1365
0
    case OFPRFR_METER_MOD:
1366
0
        inner = ofputil_encode_meter_mod(ofp_version, rf->meter_mod);
1367
0
        break;
1368
1369
0
    case OFPRFR_N_REASONS:
1370
0
    default:
1371
0
        OVS_NOT_REACHED();
1372
0
    }
1373
1374
0
    struct ofp_header *inner_oh = inner->data;
1375
0
    inner_oh->xid = rf->xid;
1376
0
    inner_oh->length = htons(inner->size);
1377
1378
0
    if (ofp_version < OFP13_VERSION) {
1379
0
        raw_msg_type = OFPRAW_NXT_REQUESTFORWARD;
1380
0
    } else if (ofp_version == OFP13_VERSION) {
1381
0
        raw_msg_type = OFPRAW_ONFT13_REQUESTFORWARD;
1382
0
    } else {
1383
0
        raw_msg_type = OFPRAW_OFPT14_REQUESTFORWARD;
1384
0
    }
1385
1386
0
    struct ofpbuf *outer = ofpraw_alloc_xid(raw_msg_type, ofp_version,
1387
0
                                            htonl(0), inner->size);
1388
0
    ofpbuf_put(outer, inner->data, inner->size);
1389
0
    ofpbuf_delete(inner);
1390
1391
0
    return outer;
1392
0
}
1393
1394
/* Decodes OFPT_REQUESTFORWARD message 'outer'.  On success, puts the decoded
1395
 * form into '*rf' and returns 0, and the caller is later responsible for
1396
 * freeing the content of 'rf', with ofputil_destroy_requestforward(rf).  On
1397
 * failure, returns an ofperr and '*rf' is indeterminate. */
1398
enum ofperr
1399
ofputil_decode_requestforward(const struct ofp_header *outer,
1400
                              struct ofputil_requestforward *rf)
1401
0
{
1402
0
    rf->new_buckets = NULL;
1403
0
    rf->group_existed = -1;
1404
1405
0
    struct ofpbuf b = ofpbuf_const_initializer(outer, ntohs(outer->length));
1406
1407
    /* Skip past outer message. */
1408
0
    enum ofpraw raw_msg_type = ofpraw_pull_assert(&b);
1409
0
    ovs_assert(raw_msg_type == OFPRAW_OFPT14_REQUESTFORWARD ||
1410
0
               raw_msg_type == OFPRAW_ONFT13_REQUESTFORWARD ||
1411
0
               raw_msg_type == OFPRAW_NXT_REQUESTFORWARD);
1412
1413
    /* Validate inner message. */
1414
0
    if (b.size < sizeof(struct ofp_header)) {
1415
0
        return OFPERR_OFPBFC_MSG_BAD_LEN;
1416
0
    }
1417
0
    const struct ofp_header *inner = b.data;
1418
0
    unsigned int inner_len = ntohs(inner->length);
1419
0
    if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) {
1420
0
        return OFPERR_OFPBFC_MSG_BAD_LEN;
1421
0
    }
1422
0
    if (inner->version != outer->version) {
1423
0
        return OFPERR_OFPBRC_BAD_VERSION;
1424
0
    }
1425
1426
    /* Parse inner message. */
1427
0
    enum ofptype type;
1428
0
    enum ofperr error = ofptype_decode(&type, inner);
1429
0
    if (error) {
1430
0
        return error;
1431
0
    }
1432
1433
0
    rf->xid = inner->xid;
1434
0
    if (type == OFPTYPE_GROUP_MOD) {
1435
0
        rf->reason = OFPRFR_GROUP_MOD;
1436
0
        rf->group_mod = xmalloc(sizeof *rf->group_mod);
1437
0
        error = ofputil_decode_group_mod(inner, rf->group_mod);
1438
0
        if (error) {
1439
0
            free(rf->group_mod);
1440
0
            return error;
1441
0
        }
1442
0
    } else if (type == OFPTYPE_METER_MOD) {
1443
0
        rf->reason = OFPRFR_METER_MOD;
1444
0
        rf->meter_mod = xmalloc(sizeof *rf->meter_mod);
1445
0
        ofpbuf_init(&rf->bands, 64);
1446
0
        error = ofputil_decode_meter_mod(inner, rf->meter_mod, &rf->bands);
1447
0
        if (error) {
1448
0
            free(rf->meter_mod);
1449
0
            ofpbuf_uninit(&rf->bands);
1450
0
            return error;
1451
0
        }
1452
0
    } else {
1453
0
        return OFPERR_OFPBFC_MSG_UNSUP;
1454
0
    }
1455
1456
0
    return 0;
1457
0
}
1458
1459
void
1460
ofputil_format_requestforward(struct ds *string,
1461
                              enum ofp_version ofp_version,
1462
                              const struct ofputil_requestforward *rf,
1463
                              const struct ofputil_port_map *port_map,
1464
                              const struct ofputil_table_map *table_map)
1465
0
{
1466
0
    ds_put_cstr(string, " reason=");
1467
1468
0
    switch (rf->reason) {
1469
0
    case OFPRFR_GROUP_MOD:
1470
0
        ds_put_cstr(string, "group_mod");
1471
0
        ofputil_group_mod_format__(string, ofp_version, rf->group_mod,
1472
0
                                   port_map, table_map);
1473
0
        break;
1474
1475
0
    case OFPRFR_METER_MOD:
1476
0
        ds_put_cstr(string, "meter_mod");
1477
0
        ofputil_format_meter_mod(string, rf->meter_mod);
1478
0
        break;
1479
1480
0
    case OFPRFR_N_REASONS:
1481
0
        OVS_NOT_REACHED();
1482
0
    }
1483
0
}
1484
1485
1486
/* Frees the content of 'rf', which should have been initialized through a
1487
 * successful call to ofputil_decode_requestforward(). */
1488
void
1489
ofputil_destroy_requestforward(struct ofputil_requestforward *rf)
1490
0
{
1491
0
    if (!rf) {
1492
0
        return;
1493
0
    }
1494
1495
0
    switch (rf->reason) {
1496
0
    case OFPRFR_GROUP_MOD:
1497
0
        ofputil_uninit_group_mod(rf->group_mod);
1498
0
        free(rf->group_mod);
1499
        /* 'rf' does not own rf->new_buckets. */
1500
0
        break;
1501
1502
0
    case OFPRFR_METER_MOD:
1503
0
        ofpbuf_uninit(&rf->bands);
1504
0
        free(rf->meter_mod);
1505
0
        break;
1506
1507
0
    case OFPRFR_N_REASONS:
1508
0
        OVS_NOT_REACHED();
1509
0
    }
1510
0
}