~james-page/ubuntu/saucy/openvswitch/1.12-snapshot

« back to all changes in this revision

Viewing changes to lib/ofp-parse.c

  • Committer: James Page
  • Date: 2013-08-21 10:16:57 UTC
  • mfrom: (1.1.20)
  • Revision ID: james.page@canonical.com-20130821101657-3o0z0qeiv5zkwlzi
New upstream snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (c) 2010, 2011, 2012 Nicira, Inc.
 
2
 * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc.
3
3
 *
4
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
5
 * you may not use this file except in compliance with the License.
22
22
#include <errno.h>
23
23
#include <stdlib.h>
24
24
 
25
 
#include "autopath.h"
26
25
#include "bundle.h"
27
26
#include "byte-order.h"
28
27
#include "dynamic-string.h"
35
34
#include "ofp-util.h"
36
35
#include "ofpbuf.h"
37
36
#include "openflow/openflow.h"
 
37
#include "ovs-thread.h"
38
38
#include "packets.h"
39
39
#include "socket-util.h"
40
40
#include "vconn.h"
42
42
 
43
43
VLOG_DEFINE_THIS_MODULE(ofp_parse);
44
44
 
45
 
static void ofp_fatal(const char *flow, bool verbose, const char *format, ...)
46
 
    NO_RETURN;
47
 
 
48
 
static uint8_t
49
 
str_to_table_id(const char *str)
 
45
/* Parses 'str' as an 8-bit unsigned integer into '*valuep'.
 
46
 *
 
47
 * 'name' describes the value parsed in an error message, if any.
 
48
 *
 
49
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
50
 * error.  The caller is responsible for freeing the returned string. */
 
51
static char * WARN_UNUSED_RESULT
 
52
str_to_u8(const char *str, const char *name, uint8_t *valuep)
50
53
{
51
 
    int table_id;
 
54
    int value;
52
55
 
53
 
    if (!str_to_int(str, 10, &table_id) || table_id < 0 || table_id > 255) {
54
 
        ovs_fatal(0, "invalid table \"%s\"", str);
 
56
    if (!str_to_int(str, 0, &value) || value < 0 || value > 255) {
 
57
        return xasprintf("invalid %s \"%s\"", name, str);
55
58
    }
56
 
    return table_id;
 
59
    *valuep = value;
 
60
    return NULL;
57
61
}
58
62
 
59
 
static uint16_t
60
 
str_to_u16(const char *str, const char *name)
 
63
/* Parses 'str' as a 16-bit unsigned integer into '*valuep'.
 
64
 *
 
65
 * 'name' describes the value parsed in an error message, if any.
 
66
 *
 
67
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
68
 * error.  The caller is responsible for freeing the returned string. */
 
69
static char * WARN_UNUSED_RESULT
 
70
str_to_u16(const char *str, const char *name, uint16_t *valuep)
61
71
{
62
72
    int value;
63
73
 
64
74
    if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) {
65
 
        ovs_fatal(0, "invalid %s \"%s\"", name, str);
 
75
        return xasprintf("invalid %s \"%s\"", name, str);
66
76
    }
67
 
    return value;
 
77
    *valuep = value;
 
78
    return NULL;
68
79
}
69
80
 
70
 
static uint32_t
71
 
str_to_u32(const char *str)
 
81
/* Parses 'str' as a 32-bit unsigned integer into '*valuep'.
 
82
 *
 
83
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
84
 * error.  The caller is responsible for freeing the returned string. */
 
85
static char * WARN_UNUSED_RESULT
 
86
str_to_u32(const char *str, uint32_t *valuep)
72
87
{
73
88
    char *tail;
74
89
    uint32_t value;
75
90
 
76
91
    if (!str[0]) {
77
 
        ovs_fatal(0, "missing required numeric argument");
 
92
        return xstrdup("missing required numeric argument");
78
93
    }
79
94
 
80
95
    errno = 0;
81
96
    value = strtoul(str, &tail, 0);
82
97
    if (errno == EINVAL || errno == ERANGE || *tail) {
83
 
        ovs_fatal(0, "invalid numeric format %s", str);
 
98
        return xasprintf("invalid numeric format %s", str);
84
99
    }
85
 
    return value;
 
100
    *valuep = value;
 
101
    return NULL;
86
102
}
87
103
 
88
 
static uint64_t
89
 
str_to_u64(const char *str)
 
104
/* Parses 'str' as an 64-bit unsigned integer into '*valuep'.
 
105
 *
 
106
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
107
 * error.  The caller is responsible for freeing the returned string. */
 
108
static char * WARN_UNUSED_RESULT
 
109
str_to_u64(const char *str, uint64_t *valuep)
90
110
{
91
111
    char *tail;
92
112
    uint64_t value;
93
113
 
94
114
    if (!str[0]) {
95
 
        ovs_fatal(0, "missing required numeric argument");
 
115
        return xstrdup("missing required numeric argument");
96
116
    }
97
117
 
98
118
    errno = 0;
99
119
    value = strtoull(str, &tail, 0);
100
120
    if (errno == EINVAL || errno == ERANGE || *tail) {
101
 
        ovs_fatal(0, "invalid numeric format %s", str);
102
 
    }
103
 
    return value;
104
 
}
105
 
 
106
 
static void
 
121
        return xasprintf("invalid numeric format %s", str);
 
122
    }
 
123
    *valuep = value;
 
124
    return NULL;
 
125
}
 
126
 
 
127
/* Parses 'str' as an 64-bit unsigned integer in network byte order into
 
128
 * '*valuep'.
 
129
 *
 
130
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
131
 * error.  The caller is responsible for freeing the returned string. */
 
132
static char * WARN_UNUSED_RESULT
 
133
str_to_be64(const char *str, ovs_be64 *valuep)
 
134
{
 
135
    uint64_t value;
 
136
    char *error;
 
137
 
 
138
    error = str_to_u64(str, &value);
 
139
    if (!error) {
 
140
        *valuep = htonll(value);
 
141
    }
 
142
    return error;
 
143
}
 
144
 
 
145
/* Parses 'str' as an Ethernet address into 'mac'.
 
146
 *
 
147
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
148
 * error.  The caller is responsible for freeing the returned string. */
 
149
static char * WARN_UNUSED_RESULT
107
150
str_to_mac(const char *str, uint8_t mac[6])
108
151
{
109
152
    if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
110
153
        != ETH_ADDR_SCAN_COUNT) {
111
 
        ovs_fatal(0, "invalid mac address %s", str);
 
154
        return xasprintf("invalid mac address %s", str);
112
155
    }
 
156
    return NULL;
113
157
}
114
158
 
115
 
static void
 
159
/* Parses 'str' as an IP address into '*ip'.
 
160
 *
 
161
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
162
 * error.  The caller is responsible for freeing the returned string. */
 
163
static char * WARN_UNUSED_RESULT
116
164
str_to_ip(const char *str, ovs_be32 *ip)
117
165
{
118
166
    struct in_addr in_addr;
119
167
 
120
168
    if (lookup_ip(str, &in_addr)) {
121
 
        ovs_fatal(0, "%s: could not convert to IP address", str);
 
169
        return xasprintf("%s: could not convert to IP address", str);
122
170
    }
123
171
    *ip = in_addr.s_addr;
 
172
    return NULL;
124
173
}
125
174
 
126
 
static void
 
175
/* Parses 'arg' as the argument to an "enqueue" action, and appends such an
 
176
 * action to 'ofpacts'.
 
177
 *
 
178
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
179
 * error.  The caller is responsible for freeing the returned string. */
 
180
static char * WARN_UNUSED_RESULT
127
181
parse_enqueue(char *arg, struct ofpbuf *ofpacts)
128
182
{
129
183
    char *sp = NULL;
132
186
    struct ofpact_enqueue *enqueue;
133
187
 
134
188
    if (port == NULL || queue == NULL) {
135
 
        ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
 
189
        return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
136
190
    }
137
191
 
138
192
    enqueue = ofpact_put_ENQUEUE(ofpacts);
139
 
    enqueue->port = str_to_u32(port);
140
 
    enqueue->queue = str_to_u32(queue);
 
193
    if (!ofputil_port_from_string(port, &enqueue->port)) {
 
194
        return xasprintf("%s: enqueue to unknown port", port);
 
195
    }
 
196
    return str_to_u32(queue, &enqueue->queue);
141
197
}
142
198
 
143
 
static void
144
 
parse_output(char *arg, struct ofpbuf *ofpacts)
 
199
/* Parses 'arg' as the argument to an "output" action, and appends such an
 
200
 * action to 'ofpacts'.
 
201
 *
 
202
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
203
 * error.  The caller is responsible for freeing the returned string. */
 
204
static char * WARN_UNUSED_RESULT
 
205
parse_output(const char *arg, struct ofpbuf *ofpacts)
145
206
{
146
207
    if (strchr(arg, '[')) {
147
208
        struct ofpact_output_reg *output_reg;
148
209
 
149
210
        output_reg = ofpact_put_OUTPUT_REG(ofpacts);
150
 
        mf_parse_subfield(&output_reg->src, arg);
151
211
        output_reg->max_len = UINT16_MAX;
 
212
        return mf_parse_subfield(&output_reg->src, arg);
152
213
    } else {
153
214
        struct ofpact_output *output;
154
215
 
155
216
        output = ofpact_put_OUTPUT(ofpacts);
156
 
        output->port = str_to_u32(arg);
157
217
        output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
 
218
        if (!ofputil_port_from_string(arg, &output->port)) {
 
219
            return xasprintf("%s: output to unknown port", arg);
 
220
        }
 
221
        return NULL;
158
222
    }
159
223
}
160
224
 
161
 
static void
 
225
/* Parses 'arg' as the argument to an "resubmit" action, and appends such an
 
226
 * action to 'ofpacts'.
 
227
 *
 
228
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
229
 * error.  The caller is responsible for freeing the returned string. */
 
230
static char * WARN_UNUSED_RESULT
162
231
parse_resubmit(char *arg, struct ofpbuf *ofpacts)
163
232
{
164
233
    struct ofpact_resubmit *resubmit;
169
238
    in_port_s = strsep(&arg, ",");
170
239
    if (in_port_s && in_port_s[0]) {
171
240
        if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
172
 
            ovs_fatal(0, "%s: resubmit to unknown port", in_port_s);
 
241
            return xasprintf("%s: resubmit to unknown port", in_port_s);
173
242
        }
174
243
    } else {
175
244
        resubmit->in_port = OFPP_IN_PORT;
176
245
    }
177
246
 
178
247
    table_s = strsep(&arg, ",");
179
 
    resubmit->table_id = table_s && table_s[0] ? str_to_u32(table_s) : 255;
 
248
    if (table_s && table_s[0]) {
 
249
        uint32_t table_id;
 
250
        char *error;
 
251
 
 
252
        error = str_to_u32(table_s, &table_id);
 
253
        if (error) {
 
254
            return error;
 
255
        }
 
256
        resubmit->table_id = table_id;
 
257
    } else {
 
258
        resubmit->table_id = 255;
 
259
    }
180
260
 
181
261
    if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
182
 
        ovs_fatal(0, "at least one \"in_port\" or \"table\" must be specified "
183
 
                  " on resubmit");
 
262
        return xstrdup("at least one \"in_port\" or \"table\" must be "
 
263
                       "specified  on resubmit");
184
264
    }
 
265
    return NULL;
185
266
}
186
267
 
187
 
static void
 
268
/* Parses 'arg' as the argument to a "note" action, and appends such an action
 
269
 * to 'ofpacts'.
 
270
 *
 
271
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
272
 * error.  The caller is responsible for freeing the returned string. */
 
273
static char * WARN_UNUSED_RESULT
188
274
parse_note(const char *arg, struct ofpbuf *ofpacts)
189
275
{
190
276
    struct ofpact_note *note;
203
289
 
204
290
        byte = hexits_value(arg, 2, &ok);
205
291
        if (!ok) {
206
 
            ovs_fatal(0, "bad hex digit in `note' argument");
 
292
            return xstrdup("bad hex digit in `note' argument");
207
293
        }
208
294
        ofpbuf_put(ofpacts, &byte, 1);
209
295
 
213
299
        arg += 2;
214
300
    }
215
301
    ofpact_update_len(ofpacts, &note->ofpact);
 
302
    return NULL;
216
303
}
217
304
 
218
 
static void
 
305
/* Parses 'arg' as the argument to a "fin_timeout" action, and appends such an
 
306
 * action to 'ofpacts'.
 
307
 *
 
308
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
309
 * error.  The caller is responsible for freeing the returned string. */
 
310
static char * WARN_UNUSED_RESULT
219
311
parse_fin_timeout(struct ofpbuf *b, char *arg)
220
312
{
221
313
    struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(b);
222
314
    char *key, *value;
223
315
 
224
316
    while (ofputil_parse_key_value(&arg, &key, &value)) {
 
317
        char *error;
 
318
 
225
319
        if (!strcmp(key, "idle_timeout")) {
226
 
            oft->fin_idle_timeout = str_to_u16(value, key);
 
320
            error =  str_to_u16(value, key, &oft->fin_idle_timeout);
227
321
        } else if (!strcmp(key, "hard_timeout")) {
228
 
            oft->fin_hard_timeout = str_to_u16(value, key);
 
322
            error = str_to_u16(value, key, &oft->fin_hard_timeout);
229
323
        } else {
230
 
            ovs_fatal(0, "invalid key '%s' in 'fin_timeout' argument", key);
 
324
            error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
 
325
                              key);
 
326
        }
 
327
 
 
328
        if (error) {
 
329
            return error;
231
330
        }
232
331
    }
 
332
    return NULL;
233
333
}
234
334
 
235
 
static void
 
335
/* Parses 'arg' as the argument to a "controller" action, and appends such an
 
336
 * action to 'ofpacts'.
 
337
 *
 
338
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
339
 * error.  The caller is responsible for freeing the returned string. */
 
340
static char * WARN_UNUSED_RESULT
236
341
parse_controller(struct ofpbuf *b, char *arg)
237
342
{
238
343
    enum ofp_packet_in_reason reason = OFPR_ACTION;
242
347
    if (!arg[0]) {
243
348
        /* Use defaults. */
244
349
    } else if (strspn(arg, "0123456789") == strlen(arg)) {
245
 
        max_len = str_to_u16(arg, "max_len");
 
350
        char *error = str_to_u16(arg, "max_len", &max_len);
 
351
        if (error) {
 
352
            return error;
 
353
        }
246
354
    } else {
247
355
        char *name, *value;
248
356
 
249
357
        while (ofputil_parse_key_value(&arg, &name, &value)) {
250
358
            if (!strcmp(name, "reason")) {
251
359
                if (!ofputil_packet_in_reason_from_string(value, &reason)) {
252
 
                    ovs_fatal(0, "unknown reason \"%s\"", value);
 
360
                    return xasprintf("unknown reason \"%s\"", value);
253
361
                }
254
362
            } else if (!strcmp(name, "max_len")) {
255
 
                max_len = str_to_u16(value, "max_len");
 
363
                char *error = str_to_u16(value, "max_len", &max_len);
 
364
                if (error) {
 
365
                    return error;
 
366
                }
256
367
            } else if (!strcmp(name, "id")) {
257
 
                controller_id = str_to_u16(value, "id");
 
368
                char *error = str_to_u16(value, "id", &controller_id);
 
369
                if (error) {
 
370
                    return error;
 
371
                }
258
372
            } else {
259
 
                ovs_fatal(0, "unknown key \"%s\" parsing controller action",
260
 
                          name);
 
373
                return xasprintf("unknown key \"%s\" parsing controller "
 
374
                                 "action", name);
261
375
            }
262
376
        }
263
377
    }
276
390
        controller->reason = reason;
277
391
        controller->controller_id = controller_id;
278
392
    }
 
393
 
 
394
    return NULL;
279
395
}
280
396
 
281
397
static void
291
407
    ofpact_update_len(b, &ids->ofpact);
292
408
}
293
409
 
294
 
static void
 
410
/* Parses 'arg' as the argument to a "dec_ttl" action, and appends such an
 
411
 * action to 'ofpacts'.
 
412
 *
 
413
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
414
 * error.  The caller is responsible for freeing the returned string. */
 
415
static char * WARN_UNUSED_RESULT
295
416
parse_dec_ttl(struct ofpbuf *b, char *arg)
296
417
{
297
418
    if (*arg == '\0') {
311
432
            ids->n_controllers++;
312
433
        }
313
434
        if (!ids->n_controllers) {
314
 
            ovs_fatal(0, "dec_ttl_cnt_ids: expected at least one controller "
315
 
                      "id.");
 
435
            return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
 
436
                           "id.");
316
437
        }
317
438
        ofpact_update_len(b, &ids->ofpact);
318
439
    }
319
 
}
320
 
 
321
 
static void
322
 
set_field_parse(const char *arg, struct ofpbuf *ofpacts)
323
 
{
324
 
    char *orig = xstrdup(arg);
 
440
    return NULL;
 
441
}
 
442
 
 
443
/* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
 
444
 * action to 'ofpacts'.
 
445
 *
 
446
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
447
 * error.  The caller is responsible for freeing the returned string. */
 
448
static char * WARN_UNUSED_RESULT
 
449
parse_set_mpls_ttl(struct ofpbuf *b, const char *arg)
 
450
{
 
451
    struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(b);
 
452
 
 
453
    if (*arg == '\0') {
 
454
        return xstrdup("parse_set_mpls_ttl: expected ttl.");
 
455
    }
 
456
 
 
457
    mpls_ttl->ttl = atoi(arg);
 
458
    return NULL;
 
459
}
 
460
 
 
461
/* Parses a "set_field" action with argument 'arg', appending the parsed
 
462
 * action to 'ofpacts'.
 
463
 *
 
464
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
465
 * error.  The caller is responsible for freeing the returned string. */
 
466
static char * WARN_UNUSED_RESULT
 
467
set_field_parse__(char *arg, struct ofpbuf *ofpacts)
 
468
{
325
469
    struct ofpact_reg_load *load = ofpact_put_REG_LOAD(ofpacts);
326
470
    char *value;
327
471
    char *delim;
328
472
    char *key;
329
473
    const struct mf_field *mf;
330
 
    const char *error;
 
474
    char *error;
331
475
    union mf_value mf_value;
332
476
 
333
 
    value = orig;
334
 
    delim = strstr(orig, "->");
 
477
    value = arg;
 
478
    delim = strstr(arg, "->");
335
479
    if (!delim) {
336
 
        ovs_fatal(0, "%s: missing `->'", orig);
 
480
        return xasprintf("%s: missing `->'", arg);
337
481
    }
338
482
    if (strlen(delim) <= strlen("->")) {
339
 
        ovs_fatal(0, "%s: missing field name following `->'", orig);
 
483
        return xasprintf("%s: missing field name following `->'", arg);
340
484
    }
341
485
 
342
486
    key = delim + strlen("->");
343
487
    mf = mf_from_name(key);
344
488
    if (!mf) {
345
 
        ovs_fatal(0, "%s is not valid oxm field name", key);
 
489
        return xasprintf("%s is not a valid OXM field name", key);
346
490
    }
347
491
    if (!mf->writable) {
348
 
        ovs_fatal(0, "%s is not allowed to set", key);
 
492
        return xasprintf("%s is read-only", key);
349
493
    }
350
494
 
351
495
    delim[0] = '\0';
352
496
    error = mf_parse_value(mf, value, &mf_value);
353
497
    if (error) {
354
 
        ovs_fatal(0, "%s", error);
 
498
        return error;
355
499
    }
356
500
    if (!mf_is_value_valid(mf, &mf_value)) {
357
 
        ovs_fatal(0, "%s is not valid valid for field %s", value, key);
 
501
        return xasprintf("%s is not a valid value for field %s", value, key);
358
502
    }
359
503
    ofpact_set_field_init(load, mf, &mf_value);
360
 
    free(orig);
361
 
}
362
 
 
363
 
static void
 
504
 
 
505
    return NULL;
 
506
}
 
507
 
 
508
/* Parses 'arg' as the argument to a "set_field" action, and appends such an
 
509
 * action to 'ofpacts'.
 
510
 *
 
511
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
512
 * error.  The caller is responsible for freeing the returned string. */
 
513
static char * WARN_UNUSED_RESULT
 
514
set_field_parse(const char *arg, struct ofpbuf *ofpacts)
 
515
{
 
516
    char *copy = xstrdup(arg);
 
517
    char *error = set_field_parse__(copy, ofpacts);
 
518
    free(copy);
 
519
    return error;
 
520
}
 
521
 
 
522
/* Parses 'arg' as the argument to a "write_metadata" instruction, and appends
 
523
 * such an action to 'ofpacts'.
 
524
 *
 
525
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
526
 * error.  The caller is responsible for freeing the returned string. */
 
527
static char * WARN_UNUSED_RESULT
364
528
parse_metadata(struct ofpbuf *b, char *arg)
365
529
{
366
530
    struct ofpact_metadata *om;
369
533
    om = ofpact_put_WRITE_METADATA(b);
370
534
 
371
535
    if (mask) {
 
536
        char *error;
 
537
 
372
538
        *mask = '\0';
373
 
        om->mask = htonll(str_to_u64(mask + 1));
 
539
        error = str_to_be64(mask + 1, &om->mask);
 
540
        if (error) {
 
541
            return error;
 
542
        }
374
543
    } else {
375
544
        om->mask = htonll(UINT64_MAX);
376
545
    }
377
546
 
378
 
    om->metadata = htonll(str_to_u64(arg));
379
 
}
380
 
 
381
 
static void
382
 
parse_named_action(enum ofputil_action_code code, const struct flow *flow,
 
547
    return str_to_be64(arg, &om->metadata);
 
548
}
 
549
 
 
550
/* Parses 'arg' as the argument to a "sample" action, and appends such an
 
551
 * action to 'ofpacts'.
 
552
 *
 
553
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
554
 * error.  The caller is responsible for freeing the returned string. */
 
555
static char * WARN_UNUSED_RESULT
 
556
parse_sample(struct ofpbuf *b, char *arg)
 
557
{
 
558
    struct ofpact_sample *os = ofpact_put_SAMPLE(b);
 
559
    char *key, *value;
 
560
 
 
561
    while (ofputil_parse_key_value(&arg, &key, &value)) {
 
562
        char *error = NULL;
 
563
 
 
564
        if (!strcmp(key, "probability")) {
 
565
            error = str_to_u16(value, "probability", &os->probability);
 
566
            if (!error && os->probability == 0) {
 
567
                error = xasprintf("invalid probability value \"%s\"", value);
 
568
            }
 
569
        } else if (!strcmp(key, "collector_set_id")) {
 
570
            error = str_to_u32(value, &os->collector_set_id);
 
571
        } else if (!strcmp(key, "obs_domain_id")) {
 
572
            error = str_to_u32(value, &os->obs_domain_id);
 
573
        } else if (!strcmp(key, "obs_point_id")) {
 
574
            error = str_to_u32(value, &os->obs_point_id);
 
575
        } else {
 
576
            error = xasprintf("invalid key \"%s\" in \"sample\" argument",
 
577
                              key);
 
578
        }
 
579
        if (error) {
 
580
            return error;
 
581
        }
 
582
    }
 
583
    if (os->probability == 0) {
 
584
        return xstrdup("non-zero \"probability\" must be specified on sample");
 
585
    }
 
586
    return NULL;
 
587
}
 
588
 
 
589
/* Parses 'arg' as the argument to action 'code', and appends such an action to
 
590
 * 'ofpacts'.
 
591
 *
 
592
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
593
 * error.  The caller is responsible for freeing the returned string. */
 
594
static char * WARN_UNUSED_RESULT
 
595
parse_named_action(enum ofputil_action_code code,
383
596
                   char *arg, struct ofpbuf *ofpacts)
384
597
{
 
598
    size_t orig_size = ofpacts->size;
385
599
    struct ofpact_tunnel *tunnel;
 
600
    char *error = NULL;
 
601
    uint16_t ethertype;
386
602
    uint16_t vid;
387
 
    uint16_t ethertype;
388
 
    ovs_be32 ip;
389
603
    uint8_t pcp;
390
604
    uint8_t tos;
391
605
 
395
609
 
396
610
    case OFPUTIL_OFPAT10_OUTPUT:
397
611
    case OFPUTIL_OFPAT11_OUTPUT:
398
 
        parse_output(arg, ofpacts);
 
612
        error = parse_output(arg, ofpacts);
399
613
        break;
400
614
 
401
615
    case OFPUTIL_OFPAT10_SET_VLAN_VID:
402
616
    case OFPUTIL_OFPAT11_SET_VLAN_VID:
403
 
        vid = str_to_u32(arg);
 
617
        error = str_to_u16(arg, "VLAN VID", &vid);
 
618
        if (error) {
 
619
            return error;
 
620
        }
 
621
 
404
622
        if (vid & ~VLAN_VID_MASK) {
405
 
            ovs_fatal(0, "%s: not a valid VLAN VID", arg);
 
623
            return xasprintf("%s: not a valid VLAN VID", arg);
406
624
        }
407
625
        ofpact_put_SET_VLAN_VID(ofpacts)->vlan_vid = vid;
408
626
        break;
409
627
 
410
628
    case OFPUTIL_OFPAT10_SET_VLAN_PCP:
411
629
    case OFPUTIL_OFPAT11_SET_VLAN_PCP:
412
 
        pcp = str_to_u32(arg);
 
630
        error = str_to_u8(arg, "VLAN PCP", &pcp);
 
631
        if (error) {
 
632
            return error;
 
633
        }
 
634
 
413
635
        if (pcp & ~7) {
414
 
            ovs_fatal(0, "%s: not a valid VLAN PCP", arg);
 
636
            return xasprintf("%s: not a valid VLAN PCP", arg);
415
637
        }
416
638
        ofpact_put_SET_VLAN_PCP(ofpacts)->vlan_pcp = pcp;
417
639
        break;
418
640
 
419
641
    case OFPUTIL_OFPAT12_SET_FIELD:
420
 
        set_field_parse(arg, ofpacts);
421
 
        break;
 
642
        return set_field_parse(arg, ofpacts);
422
643
 
423
644
    case OFPUTIL_OFPAT10_STRIP_VLAN:
424
645
    case OFPUTIL_OFPAT11_POP_VLAN:
426
647
        break;
427
648
 
428
649
    case OFPUTIL_OFPAT11_PUSH_VLAN:
429
 
        ethertype = str_to_u16(arg, "ethertype");
 
650
        error = str_to_u16(arg, "ethertype", &ethertype);
 
651
        if (error) {
 
652
            return error;
 
653
        }
 
654
 
430
655
        if (ethertype != ETH_TYPE_VLAN_8021Q) {
431
656
            /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
432
 
            ovs_fatal(0, "%s: not a valid VLAN ethertype", arg);
 
657
            return xasprintf("%s: not a valid VLAN ethertype", arg);
433
658
        }
 
659
 
434
660
        ofpact_put_PUSH_VLAN(ofpacts);
435
661
        break;
436
662
 
437
663
    case OFPUTIL_OFPAT11_SET_QUEUE:
438
 
        ofpact_put_SET_QUEUE(ofpacts)->queue_id = str_to_u32(arg);
 
664
        error = str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
439
665
        break;
440
666
 
441
 
 
442
667
    case OFPUTIL_OFPAT10_SET_DL_SRC:
443
668
    case OFPUTIL_OFPAT11_SET_DL_SRC:
444
 
        str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
 
669
        error = str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
445
670
        break;
446
671
 
447
672
    case OFPUTIL_OFPAT10_SET_DL_DST:
448
673
    case OFPUTIL_OFPAT11_SET_DL_DST:
449
 
        str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
 
674
        error = str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
450
675
        break;
451
676
 
452
677
    case OFPUTIL_OFPAT10_SET_NW_SRC:
453
678
    case OFPUTIL_OFPAT11_SET_NW_SRC:
454
 
        str_to_ip(arg, &ip);
455
 
        ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4 = ip;
 
679
        error = str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
456
680
        break;
457
681
 
458
682
    case OFPUTIL_OFPAT10_SET_NW_DST:
459
683
    case OFPUTIL_OFPAT11_SET_NW_DST:
460
 
        str_to_ip(arg, &ip);
461
 
        ofpact_put_SET_IPV4_DST(ofpacts)->ipv4 = ip;
 
684
        error = str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
462
685
        break;
463
686
 
464
687
    case OFPUTIL_OFPAT10_SET_NW_TOS:
465
688
    case OFPUTIL_OFPAT11_SET_NW_TOS:
466
 
        tos = str_to_u32(arg);
 
689
        error = str_to_u8(arg, "TOS", &tos);
 
690
        if (error) {
 
691
            return error;
 
692
        }
 
693
 
467
694
        if (tos & ~IP_DSCP_MASK) {
468
 
            ovs_fatal(0, "%s: not a valid TOS", arg);
 
695
            return xasprintf("%s: not a valid TOS", arg);
469
696
        }
470
697
        ofpact_put_SET_IPV4_DSCP(ofpacts)->dscp = tos;
471
698
        break;
475
702
 
476
703
    case OFPUTIL_OFPAT10_SET_TP_SRC:
477
704
    case OFPUTIL_OFPAT11_SET_TP_SRC:
478
 
        ofpact_put_SET_L4_SRC_PORT(ofpacts)->port = str_to_u32(arg);
 
705
        error = str_to_u16(arg, "source port",
 
706
                           &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
479
707
        break;
480
708
 
481
709
    case OFPUTIL_OFPAT10_SET_TP_DST:
482
710
    case OFPUTIL_OFPAT11_SET_TP_DST:
483
 
        ofpact_put_SET_L4_DST_PORT(ofpacts)->port = str_to_u32(arg);
 
711
        error = str_to_u16(arg, "destination port",
 
712
                           &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
484
713
        break;
485
714
 
486
715
    case OFPUTIL_OFPAT10_ENQUEUE:
487
 
        parse_enqueue(arg, ofpacts);
 
716
        error = parse_enqueue(arg, ofpacts);
488
717
        break;
489
718
 
490
719
    case OFPUTIL_NXAST_RESUBMIT:
491
 
        parse_resubmit(arg, ofpacts);
 
720
        error = parse_resubmit(arg, ofpacts);
492
721
        break;
493
722
 
494
723
    case OFPUTIL_NXAST_SET_TUNNEL:
495
724
    case OFPUTIL_NXAST_SET_TUNNEL64:
496
725
        tunnel = ofpact_put_SET_TUNNEL(ofpacts);
497
726
        tunnel->ofpact.compat = code;
498
 
        tunnel->tun_id = str_to_u64(arg);
 
727
        error = str_to_u64(arg, &tunnel->tun_id);
499
728
        break;
500
729
 
501
730
    case OFPUTIL_NXAST_WRITE_METADATA:
502
 
        parse_metadata(ofpacts, arg);
 
731
        error = parse_metadata(ofpacts, arg);
503
732
        break;
504
733
 
505
734
    case OFPUTIL_NXAST_SET_QUEUE:
506
 
        ofpact_put_SET_QUEUE(ofpacts)->queue_id = str_to_u32(arg);
 
735
        error = str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
507
736
        break;
508
737
 
509
738
    case OFPUTIL_NXAST_POP_QUEUE:
511
740
        break;
512
741
 
513
742
    case OFPUTIL_NXAST_REG_MOVE:
514
 
        nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts), arg);
 
743
        error = nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts), arg);
515
744
        break;
516
745
 
517
746
    case OFPUTIL_NXAST_REG_LOAD:
518
 
        nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts), arg);
 
747
        error = nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts), arg);
519
748
        break;
520
749
 
521
750
    case OFPUTIL_NXAST_NOTE:
522
 
        parse_note(arg, ofpacts);
 
751
        error = parse_note(arg, ofpacts);
523
752
        break;
524
753
 
525
754
    case OFPUTIL_NXAST_MULTIPATH:
526
 
        multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
527
 
        break;
528
 
 
529
 
    case OFPUTIL_NXAST_AUTOPATH__DEPRECATED:
530
 
        autopath_parse(ofpact_put_AUTOPATH(ofpacts), arg);
 
755
        error = multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
531
756
        break;
532
757
 
533
758
    case OFPUTIL_NXAST_BUNDLE:
534
 
        bundle_parse(arg, ofpacts);
 
759
        error = bundle_parse(arg, ofpacts);
535
760
        break;
536
761
 
537
762
    case OFPUTIL_NXAST_BUNDLE_LOAD:
538
 
        bundle_parse_load(arg, ofpacts);
 
763
        error = bundle_parse_load(arg, ofpacts);
539
764
        break;
540
765
 
541
766
    case OFPUTIL_NXAST_RESUBMIT_TABLE:
544
769
        NOT_REACHED();
545
770
 
546
771
    case OFPUTIL_NXAST_LEARN:
547
 
        learn_parse(arg, flow, ofpacts);
 
772
        error = learn_parse(arg, ofpacts);
548
773
        break;
549
774
 
550
775
    case OFPUTIL_NXAST_EXIT:
552
777
        break;
553
778
 
554
779
    case OFPUTIL_NXAST_DEC_TTL:
555
 
        parse_dec_ttl(ofpacts, arg);
 
780
        error = parse_dec_ttl(ofpacts, arg);
 
781
        break;
 
782
 
 
783
    case OFPUTIL_NXAST_SET_MPLS_TTL:
 
784
    case OFPUTIL_OFPAT11_SET_MPLS_TTL:
 
785
        error = parse_set_mpls_ttl(ofpacts, arg);
 
786
        break;
 
787
 
 
788
    case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
 
789
    case OFPUTIL_NXAST_DEC_MPLS_TTL:
 
790
        ofpact_put_DEC_MPLS_TTL(ofpacts);
556
791
        break;
557
792
 
558
793
    case OFPUTIL_NXAST_FIN_TIMEOUT:
559
 
        parse_fin_timeout(ofpacts, arg);
 
794
        error = parse_fin_timeout(ofpacts, arg);
560
795
        break;
561
796
 
562
797
    case OFPUTIL_NXAST_CONTROLLER:
563
 
        parse_controller(ofpacts, arg);
564
 
        break;
565
 
    }
 
798
        error = parse_controller(ofpacts, arg);
 
799
        break;
 
800
 
 
801
    case OFPUTIL_OFPAT11_PUSH_MPLS:
 
802
    case OFPUTIL_NXAST_PUSH_MPLS:
 
803
        error = str_to_u16(arg, "push_mpls", &ethertype);
 
804
        if (!error) {
 
805
            ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
 
806
        }
 
807
        break;
 
808
 
 
809
    case OFPUTIL_OFPAT11_POP_MPLS:
 
810
    case OFPUTIL_NXAST_POP_MPLS:
 
811
        error = str_to_u16(arg, "pop_mpls", &ethertype);
 
812
        if (!error) {
 
813
            ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
 
814
        }
 
815
        break;
 
816
 
 
817
    case OFPUTIL_NXAST_STACK_PUSH:
 
818
        error = nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
 
819
        break;
 
820
    case OFPUTIL_NXAST_STACK_POP:
 
821
        error = nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
 
822
        break;
 
823
 
 
824
    case OFPUTIL_NXAST_SAMPLE:
 
825
        error = parse_sample(ofpacts, arg);
 
826
        break;
 
827
    }
 
828
 
 
829
    if (error) {
 
830
        ofpacts->size = orig_size;
 
831
    }
 
832
    return error;
566
833
}
567
834
 
568
 
static bool
569
 
str_to_ofpact__(const struct flow *flow, char *pos, char *act, char *arg,
 
835
/* Parses action 'act', with argument 'arg', and appends a parsed version to
 
836
 * 'ofpacts'.
 
837
 *
 
838
 * 'n_actions' specifies the number of actions already parsed (for proper
 
839
 * handling of "drop" actions).
 
840
 *
 
841
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
842
 * error.  The caller is responsible for freeing the returned string. */
 
843
static char * WARN_UNUSED_RESULT
 
844
str_to_ofpact__(char *pos, char *act, char *arg,
570
845
                struct ofpbuf *ofpacts, int n_actions)
571
846
{
572
847
    int code = ofputil_action_code_from_name(act);
573
848
    if (code >= 0) {
574
 
        parse_named_action(code, flow, arg, ofpacts);
 
849
        return parse_named_action(code, arg, ofpacts);
575
850
    } else if (!strcasecmp(act, "drop")) {
576
851
        if (n_actions) {
577
 
            ovs_fatal(0, "Drop actions must not be preceded by other "
578
 
                      "actions");
 
852
            return xstrdup("Drop actions must not be preceded by other "
 
853
                           "actions");
579
854
        } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
580
 
            ovs_fatal(0, "Drop actions must not be followed by other "
581
 
                      "actions");
 
855
            return xstrdup("Drop actions must not be followed by other "
 
856
                           "actions");
582
857
        }
583
 
        return false;
584
858
    } else {
585
 
        uint16_t port;
 
859
        ofp_port_t port;
586
860
        if (ofputil_port_from_string(act, &port)) {
587
861
            ofpact_put_OUTPUT(ofpacts)->port = port;
588
862
        } else {
589
 
            ovs_fatal(0, "Unknown action: %s", act);
 
863
            return xasprintf("Unknown action: %s", act);
590
864
        }
591
865
    }
592
866
 
593
 
    return true;
 
867
    return NULL;
594
868
}
595
869
 
596
 
static void
597
 
str_to_ofpacts(const struct flow *flow, char *str, struct ofpbuf *ofpacts)
 
870
/* Parses 'str' as a series of actions, and appends them to 'ofpacts'.
 
871
 *
 
872
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
873
 * error.  The caller is responsible for freeing the returned string. */
 
874
static char * WARN_UNUSED_RESULT
 
875
str_to_ofpacts(char *str, struct ofpbuf *ofpacts)
598
876
{
 
877
    size_t orig_size = ofpacts->size;
599
878
    char *pos, *act, *arg;
600
879
    enum ofperr error;
601
880
    int n_actions;
603
882
    pos = str;
604
883
    n_actions = 0;
605
884
    while (ofputil_parse_key_value(&pos, &act, &arg)) {
606
 
        if (!str_to_ofpact__(flow, pos, act, arg, ofpacts, n_actions)) {
607
 
            break;
 
885
        char *error = str_to_ofpact__(pos, act, arg, ofpacts, n_actions);
 
886
        if (error) {
 
887
            ofpacts->size = orig_size;
 
888
            return error;
608
889
        }
609
890
        n_actions++;
610
891
    }
611
892
 
612
893
    error = ofpacts_verify(ofpacts->data, ofpacts->size);
613
894
    if (error) {
614
 
        ovs_fatal(0, "Incorrect action ordering");
 
895
        ofpacts->size = orig_size;
 
896
        return xstrdup("Incorrect action ordering");
615
897
    }
616
898
 
617
899
    ofpact_pad(ofpacts);
 
900
    return NULL;
618
901
}
619
902
 
620
 
static void
 
903
/* Parses 'arg' as the argument to instruction 'type', and appends such an
 
904
 * instruction to 'ofpacts'.
 
905
 *
 
906
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
907
 * error.  The caller is responsible for freeing the returned string. */
 
908
static char * WARN_UNUSED_RESULT
621
909
parse_named_instruction(enum ovs_instruction_type type,
622
910
                        char *arg, struct ofpbuf *ofpacts)
623
911
{
 
912
    char *error_s = NULL;
624
913
    enum ofperr error;
625
914
 
626
915
    switch (type) {
630
919
 
631
920
    case OVSINST_OFPIT11_WRITE_ACTIONS:
632
921
        /* XXX */
633
 
        ovs_fatal(0, "instruction write-actions is not supported yet");
 
922
        error_s = xstrdup("instruction write-actions is not supported yet");
634
923
        break;
635
924
 
636
925
    case OVSINST_OFPIT11_CLEAR_ACTIONS:
637
926
        ofpact_put_CLEAR_ACTIONS(ofpacts);
638
927
        break;
639
928
 
 
929
    case OVSINST_OFPIT13_METER:
 
930
        error_s = str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
 
931
        break;
 
932
 
640
933
    case OVSINST_OFPIT11_WRITE_METADATA:
641
 
        parse_metadata(ofpacts, arg);
 
934
        error_s = parse_metadata(ofpacts, arg);
642
935
        break;
643
936
 
644
937
    case OVSINST_OFPIT11_GOTO_TABLE: {
645
938
        struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
646
939
        char *table_s = strsep(&arg, ",");
647
940
        if (!table_s || !table_s[0]) {
648
 
            ovs_fatal(0, "instruction goto-table needs table id");
 
941
            return xstrdup("instruction goto-table needs table id");
649
942
        }
650
 
        ogt->table_id = str_to_table_id(table_s);
 
943
        error_s = str_to_u8(table_s, "table", &ogt->table_id);
651
944
        break;
652
945
    }
653
946
    }
654
947
 
 
948
    if (error_s) {
 
949
        return error_s;
 
950
    }
 
951
 
655
952
    /* If write_metadata is specified as an action AND an instruction, ofpacts
656
953
       could be invalid. */
657
954
    error = ofpacts_verify(ofpacts->data, ofpacts->size);
658
955
    if (error) {
659
 
        ovs_fatal(0, "Incorrect instruction ordering");
 
956
        return xstrdup("Incorrect instruction ordering");
660
957
    }
 
958
    return NULL;
661
959
}
662
960
 
663
 
static void
664
 
str_to_inst_ofpacts(const struct flow *flow, char *str, struct ofpbuf *ofpacts)
 
961
/* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
 
962
 *
 
963
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
964
 * error.  The caller is responsible for freeing the returned string. */
 
965
static char * WARN_UNUSED_RESULT
 
966
str_to_inst_ofpacts(char *str, struct ofpbuf *ofpacts)
665
967
{
 
968
    size_t orig_size = ofpacts->size;
666
969
    char *pos, *inst, *arg;
667
970
    int type;
668
971
    const char *prev_inst = NULL;
671
974
 
672
975
    pos = str;
673
976
    while (ofputil_parse_key_value(&pos, &inst, &arg)) {
674
 
        type = ofpact_instruction_type_from_name(inst);
 
977
        type = ovs_instruction_type_from_name(inst);
675
978
        if (type < 0) {
676
 
            if (!str_to_ofpact__(flow, pos, inst, arg, ofpacts, n_actions)) {
677
 
                break;
 
979
            char *error = str_to_ofpact__(pos, inst, arg, ofpacts, n_actions);
 
980
            if (error) {
 
981
                ofpacts->size = orig_size;
 
982
                return error;
678
983
            }
679
984
 
680
985
            type = OVSINST_OFPIT11_APPLY_ACTIONS;
683
988
                continue;
684
989
            }
685
990
        } else if (type == OVSINST_OFPIT11_APPLY_ACTIONS) {
686
 
            ovs_fatal(0, "%s isn't supported. Just write actions then "
687
 
                      "it is interpreted as apply_actions", inst);
 
991
            ofpacts->size = orig_size;
 
992
            return xasprintf("%s isn't supported. Just write actions then "
 
993
                             "it is interpreted as apply_actions", inst);
688
994
        } else {
689
 
            parse_named_instruction(type, arg, ofpacts);
 
995
            char *error = parse_named_instruction(type, arg, ofpacts);
 
996
            if (error) {
 
997
                ofpacts->size = orig_size;
 
998
                return error;
 
999
            }
690
1000
        }
691
1001
 
692
 
        if (type == prev_type) {
693
 
            ovs_fatal(0, "instruction can be specified at most once: %s",
694
 
                      inst);
695
 
        }
696
1002
        if (type <= prev_type) {
697
 
            ovs_fatal(0, "Instruction %s must be specified before %s",
698
 
                      inst, prev_inst);
 
1003
            ofpacts->size = orig_size;
 
1004
            if (type == prev_type) {
 
1005
                return xasprintf("instruction %s may be specified only once",
 
1006
                                 inst);
 
1007
            } else {
 
1008
                return xasprintf("instruction %s must be specified before %s",
 
1009
                                 inst, prev_inst);
 
1010
            }
699
1011
        }
700
1012
 
701
1013
        prev_inst = inst;
703
1015
        n_actions++;
704
1016
    }
705
1017
    ofpact_pad(ofpacts);
 
1018
 
 
1019
    return NULL;
706
1020
}
707
1021
 
708
1022
struct protocol {
726
1040
        { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
727
1041
        { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
728
1042
        { "rarp", ETH_TYPE_RARP, 0},
729
 
};
 
1043
        { "mpls", ETH_TYPE_MPLS, 0 },
 
1044
        { "mplsm", ETH_TYPE_MPLS_MCAST, 0 },
 
1045
    };
730
1046
    const struct protocol *p;
731
1047
 
732
1048
    for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
739
1055
    return false;
740
1056
}
741
1057
 
742
 
static void
743
 
ofp_fatal(const char *flow, bool verbose, const char *format, ...)
744
 
{
745
 
    va_list args;
746
 
 
747
 
    if (verbose) {
748
 
        fprintf(stderr, "%s:\n", flow);
749
 
    }
750
 
 
751
 
    va_start(args, format);
752
 
    ovs_fatal_valist(0, format, args);
753
 
}
754
 
 
755
 
static void
 
1058
/* Parses 's' as the (possibly masked) value of field 'mf', and updates
 
1059
 * 'match' appropriately.
 
1060
 *
 
1061
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
1062
 * error.  The caller is responsible for freeing the returned string. */
 
1063
static char * WARN_UNUSED_RESULT
756
1064
parse_field(const struct mf_field *mf, const char *s, struct match *match)
757
1065
{
758
1066
    union mf_value value, mask;
759
1067
    char *error;
760
1068
 
761
1069
    error = mf_parse(mf, s, &value, &mask);
762
 
    if (error) {
763
 
        ovs_fatal(0, "%s", error);
 
1070
    if (!error) {
 
1071
        mf_set(mf, &value, &mask, match);
764
1072
    }
765
 
 
766
 
    mf_set(mf, &value, &mask, match);
 
1073
    return error;
767
1074
}
768
1075
 
769
 
/* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
770
 
 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
771
 
 * If 'actions' is specified, an action must be in 'string' and may be expanded
772
 
 * or reallocated.
773
 
 *
774
 
 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
775
 
 * constant for 'command'.  To parse syntax for an OFPST_FLOW or
776
 
 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
777
 
void
778
 
parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
779
 
              bool verbose)
 
1076
static char * WARN_UNUSED_RESULT
 
1077
parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string)
780
1078
{
781
1079
    enum {
782
1080
        F_OUT_PORT = 1 << 0,
785
1083
        F_PRIORITY = 1 << 4,
786
1084
        F_FLAGS = 1 << 5,
787
1085
    } fields;
788
 
    char *string = xstrdup(str_);
789
1086
    char *save_ptr = NULL;
790
1087
    char *act_str = NULL;
791
1088
    char *name;
829
1126
    } else{
830
1127
        fm->new_cookie = htonll(0);
831
1128
    }
 
1129
    fm->modify_cookie = false;
832
1130
    fm->table_id = 0xff;
833
1131
    fm->command = command;
834
1132
    fm->idle_timeout = OFP_FLOW_PERMANENT;
839
1137
    if (fields & F_ACTIONS) {
840
1138
        act_str = strstr(string, "action");
841
1139
        if (!act_str) {
842
 
            ofp_fatal(str_, verbose, "must specify an action");
 
1140
            return xstrdup("must specify an action");
843
1141
        }
844
1142
        *act_str = '\0';
845
1143
 
846
1144
        act_str = strchr(act_str + 1, '=');
847
1145
        if (!act_str) {
848
 
            ofp_fatal(str_, verbose, "must specify an action");
 
1146
            return xstrdup("must specify an action");
849
1147
        }
850
1148
 
851
1149
        act_str++;
853
1151
    for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
854
1152
         name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
855
1153
        const struct protocol *p;
 
1154
        char *error = NULL;
856
1155
 
857
1156
        if (parse_protocol(name, &p)) {
858
1157
            match_set_dl_type(&fm->match, htons(p->dl_type));
874
1173
 
875
1174
            value = strtok_r(NULL, ", \t\r\n", &save_ptr);
876
1175
            if (!value) {
877
 
                ofp_fatal(str_, verbose, "field %s missing value", name);
 
1176
                return xasprintf("field %s missing value", name);
878
1177
            }
879
1178
 
880
1179
            if (!strcmp(name, "table")) {
881
 
                fm->table_id = str_to_table_id(value);
 
1180
                error = str_to_u8(value, "table", &fm->table_id);
882
1181
            } else if (!strcmp(name, "out_port")) {
883
 
                if (!ofputil_port_from_string(name, &fm->out_port)) {
884
 
                    ofp_fatal(str_, verbose, "%s is not a valid OpenFlow port",
885
 
                              name);
 
1182
                if (!ofputil_port_from_string(value, &fm->out_port)) {
 
1183
                    error = xasprintf("%s is not a valid OpenFlow port",
 
1184
                                      value);
886
1185
                }
887
1186
            } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
888
 
                fm->priority = str_to_u16(value, name);
 
1187
                uint16_t priority;
 
1188
 
 
1189
                error = str_to_u16(value, name, &priority);
 
1190
                fm->priority = priority;
889
1191
            } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
890
 
                fm->idle_timeout = str_to_u16(value, name);
 
1192
                error = str_to_u16(value, name, &fm->idle_timeout);
891
1193
            } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
892
 
                fm->hard_timeout = str_to_u16(value, name);
 
1194
                error = str_to_u16(value, name, &fm->hard_timeout);
893
1195
            } else if (!strcmp(name, "cookie")) {
894
1196
                char *mask = strchr(value, '/');
895
1197
 
896
1198
                if (mask) {
897
1199
                    /* A mask means we're searching for a cookie. */
898
1200
                    if (command == OFPFC_ADD) {
899
 
                        ofp_fatal(str_, verbose, "flow additions cannot use "
900
 
                                  "a cookie mask");
 
1201
                        return xstrdup("flow additions cannot use "
 
1202
                                       "a cookie mask");
901
1203
                    }
902
1204
                    *mask = '\0';
903
 
                    fm->cookie = htonll(str_to_u64(value));
904
 
                    fm->cookie_mask = htonll(str_to_u64(mask+1));
 
1205
                    error = str_to_be64(value, &fm->cookie);
 
1206
                    if (error) {
 
1207
                        return error;
 
1208
                    }
 
1209
                    error = str_to_be64(mask + 1, &fm->cookie_mask);
905
1210
                } else {
906
1211
                    /* No mask means that the cookie is being set. */
907
1212
                    if (command != OFPFC_ADD && command != OFPFC_MODIFY
908
 
                            && command != OFPFC_MODIFY_STRICT) {
909
 
                        ofp_fatal(str_, verbose, "cannot set cookie");
 
1213
                        && command != OFPFC_MODIFY_STRICT) {
 
1214
                        return xstrdup("cannot set cookie");
910
1215
                    }
911
 
                    fm->new_cookie = htonll(str_to_u64(value));
 
1216
                    error = str_to_be64(value, &fm->new_cookie);
 
1217
                    fm->modify_cookie = true;
912
1218
                }
913
1219
            } else if (mf_from_name(name)) {
914
 
                parse_field(mf_from_name(name), value, &fm->match);
 
1220
                error = parse_field(mf_from_name(name), value, &fm->match);
915
1221
            } else if (!strcmp(name, "duration")
916
1222
                       || !strcmp(name, "n_packets")
917
1223
                       || !strcmp(name, "n_bytes")
921
1227
                 * "ovs-ofctl dump-flows" back into commands that parse
922
1228
                 * flows. */
923
1229
            } else {
924
 
                ofp_fatal(str_, verbose, "unknown keyword %s", name);
 
1230
                error = xasprintf("unknown keyword %s", name);
 
1231
            }
 
1232
 
 
1233
            if (error) {
 
1234
                return error;
925
1235
            }
926
1236
        }
927
1237
    }
928
1238
    if (!fm->cookie_mask && fm->new_cookie == htonll(UINT64_MAX)
929
 
            && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
 
1239
        && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
930
1240
        /* On modifies without a mask, we are supposed to add a flow if
931
1241
         * one does not exist.  If a cookie wasn't been specified, use a
932
1242
         * default of zero. */
934
1244
    }
935
1245
    if (fields & F_ACTIONS) {
936
1246
        struct ofpbuf ofpacts;
 
1247
        char *error;
937
1248
 
938
1249
        ofpbuf_init(&ofpacts, 32);
939
 
        str_to_inst_ofpacts(&fm->match.flow, act_str, &ofpacts);
 
1250
        error = str_to_inst_ofpacts(act_str, &ofpacts);
 
1251
        if (!error) {
 
1252
            enum ofperr err;
 
1253
 
 
1254
            err = ofpacts_check(ofpacts.data, ofpacts.size, &fm->match.flow,
 
1255
                                OFPP_MAX, 0);
 
1256
            if (err) {
 
1257
                error = xasprintf("actions are invalid with specified match "
 
1258
                                  "(%s)", ofperr_to_string(err));
 
1259
            }
 
1260
        }
 
1261
        if (error) {
 
1262
            ofpbuf_uninit(&ofpacts);
 
1263
            return error;
 
1264
        }
 
1265
 
940
1266
        fm->ofpacts_len = ofpacts.size;
941
1267
        fm->ofpacts = ofpbuf_steal_data(&ofpacts);
942
1268
    } else {
944
1270
        fm->ofpacts = NULL;
945
1271
    }
946
1272
 
947
 
    free(string);
 
1273
    return NULL;
948
1274
}
949
1275
 
950
 
/* Convert 'str_' (as described in the documentation for the "monitor" command
951
 
 * in the ovs-ofctl man page) into 'fmr'. */
952
 
void
953
 
parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
954
 
                           const char *str_)
 
1276
/* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
 
1277
 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
 
1278
 *
 
1279
 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
 
1280
 * constant for 'command'.  To parse syntax for an OFPST_FLOW or
 
1281
 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'.
 
1282
 *
 
1283
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
1284
 * error.  The caller is responsible for freeing the returned string. */
 
1285
char * WARN_UNUSED_RESULT
 
1286
parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_)
955
1287
{
956
 
    static uint32_t id;
957
 
 
958
1288
    char *string = xstrdup(str_);
959
 
    char *save_ptr = NULL;
960
 
    char *name;
961
 
 
962
 
    fmr->id = id++;
 
1289
    char *error;
 
1290
 
 
1291
    error = parse_ofp_str__(fm, command, string);
 
1292
    if (error) {
 
1293
        fm->ofpacts = NULL;
 
1294
        fm->ofpacts_len = 0;
 
1295
    }
 
1296
 
 
1297
    free(string);
 
1298
    return error;
 
1299
}
 
1300
 
 
1301
static char * WARN_UNUSED_RESULT
 
1302
parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string,
 
1303
                          struct ofpbuf *bands, int command)
 
1304
{
 
1305
    enum {
 
1306
        F_METER = 1 << 0,
 
1307
        F_FLAGS = 1 << 1,
 
1308
        F_BANDS = 1 << 2,
 
1309
    } fields;
 
1310
    char *save_ptr = NULL;
 
1311
    char *band_str = NULL;
 
1312
    char *name;
 
1313
 
 
1314
    switch (command) {
 
1315
    case -1:
 
1316
        fields = F_METER;
 
1317
        break;
 
1318
 
 
1319
    case OFPMC13_ADD:
 
1320
        fields = F_METER | F_FLAGS | F_BANDS;
 
1321
        break;
 
1322
 
 
1323
    case OFPMC13_DELETE:
 
1324
        fields = F_METER;
 
1325
        break;
 
1326
 
 
1327
    case OFPMC13_MODIFY:
 
1328
        fields = F_METER | F_FLAGS | F_BANDS;
 
1329
        break;
 
1330
 
 
1331
    default:
 
1332
        NOT_REACHED();
 
1333
    }
 
1334
 
 
1335
    mm->command = command;
 
1336
    mm->meter.meter_id = 0;
 
1337
    mm->meter.flags = 0;
 
1338
    if (fields & F_BANDS) {
 
1339
        band_str = strstr(string, "band");
 
1340
        if (!band_str) {
 
1341
            return xstrdup("must specify bands");
 
1342
        }
 
1343
        *band_str = '\0';
 
1344
 
 
1345
        band_str = strchr(band_str + 1, '=');
 
1346
        if (!band_str) {
 
1347
            return xstrdup("must specify bands");
 
1348
        }
 
1349
 
 
1350
        band_str++;
 
1351
    }
 
1352
    for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
 
1353
         name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
 
1354
 
 
1355
        if (fields & F_FLAGS && !strcmp(name, "kbps")) {
 
1356
            mm->meter.flags |= OFPMF13_KBPS;
 
1357
        } else if (fields & F_FLAGS && !strcmp(name, "pktps")) {
 
1358
            mm->meter.flags |= OFPMF13_PKTPS;
 
1359
        } else if (fields & F_FLAGS && !strcmp(name, "burst")) {
 
1360
            mm->meter.flags |= OFPMF13_BURST;
 
1361
        } else if (fields & F_FLAGS && !strcmp(name, "stats")) {
 
1362
            mm->meter.flags |= OFPMF13_STATS;
 
1363
        } else {
 
1364
            char *value;
 
1365
 
 
1366
            value = strtok_r(NULL, ", \t\r\n", &save_ptr);
 
1367
            if (!value) {
 
1368
                return xasprintf("field %s missing value", name);
 
1369
            }
 
1370
 
 
1371
            if (!strcmp(name, "meter")) {
 
1372
                if (!strcmp(value, "all")) {
 
1373
                    mm->meter.meter_id = OFPM13_ALL;
 
1374
                } else if (!strcmp(value, "controller")) {
 
1375
                    mm->meter.meter_id = OFPM13_CONTROLLER;
 
1376
                } else if (!strcmp(value, "slowpath")) {
 
1377
                    mm->meter.meter_id = OFPM13_SLOWPATH;
 
1378
                } else {
 
1379
                    char *error = str_to_u32(value, &mm->meter.meter_id);
 
1380
                    if (error) {
 
1381
                        return error;
 
1382
                    }
 
1383
                    if (mm->meter.meter_id > OFPM13_MAX) {
 
1384
                        return xasprintf("invalid value for %s", name);
 
1385
                    }
 
1386
                }
 
1387
            } else {
 
1388
                return xasprintf("unknown keyword %s", name);
 
1389
            }
 
1390
        }
 
1391
    }
 
1392
    if (fields & F_METER && !mm->meter.meter_id) {
 
1393
        return xstrdup("must specify 'meter'");
 
1394
    }
 
1395
    if (fields & F_FLAGS && !mm->meter.flags) {
 
1396
        return xstrdup("meter must specify either 'kbps' or 'pktps'");
 
1397
    }
 
1398
 
 
1399
    if (fields & F_BANDS) {
 
1400
        uint16_t n_bands = 0;
 
1401
        struct ofputil_meter_band *band = NULL;
 
1402
        int i;
 
1403
 
 
1404
        for (name = strtok_r(band_str, "=, \t\r\n", &save_ptr); name;
 
1405
             name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
 
1406
 
 
1407
            char *value;
 
1408
 
 
1409
            value = strtok_r(NULL, ", \t\r\n", &save_ptr);
 
1410
            if (!value) {
 
1411
                return xasprintf("field %s missing value", name);
 
1412
            }
 
1413
 
 
1414
            if (!strcmp(name, "type")) {
 
1415
                /* Start a new band */
 
1416
                band = ofpbuf_put_zeros(bands, sizeof *band);
 
1417
                n_bands++;
 
1418
 
 
1419
                if (!strcmp(value, "drop")) {
 
1420
                    band->type = OFPMBT13_DROP;
 
1421
                } else if (!strcmp(value, "dscp_remark")) {
 
1422
                    band->type = OFPMBT13_DSCP_REMARK;
 
1423
                } else {
 
1424
                    return xasprintf("field %s unknown value %s", name, value);
 
1425
                }
 
1426
            } else if (!band || !band->type) {
 
1427
                return xstrdup("band must start with the 'type' keyword");
 
1428
            } else if (!strcmp(name, "rate")) {
 
1429
                char *error = str_to_u32(value, &band->rate);
 
1430
                if (error) {
 
1431
                    return error;
 
1432
                }
 
1433
            } else if (!strcmp(name, "burst_size")) {
 
1434
                char *error = str_to_u32(value, &band->burst_size);
 
1435
                if (error) {
 
1436
                    return error;
 
1437
                }
 
1438
            } else if (!strcmp(name, "prec_level")) {
 
1439
                char *error = str_to_u8(value, name, &band->prec_level);
 
1440
                if (error) {
 
1441
                    return error;
 
1442
                }
 
1443
            } else {
 
1444
                return xasprintf("unknown keyword %s", name);
 
1445
            }
 
1446
        }
 
1447
        /* validate bands */
 
1448
        if (!n_bands) {
 
1449
            return xstrdup("meter must have bands");
 
1450
        }
 
1451
 
 
1452
        mm->meter.n_bands = n_bands;
 
1453
        mm->meter.bands = ofpbuf_steal_data(bands);
 
1454
 
 
1455
        for (i = 0; i < n_bands; ++i) {
 
1456
            band = &mm->meter.bands[i];
 
1457
 
 
1458
            if (!band->type) {
 
1459
                return xstrdup("band must have 'type'");
 
1460
            }
 
1461
            if (band->type == OFPMBT13_DSCP_REMARK) {
 
1462
                if (!band->prec_level) {
 
1463
                    return xstrdup("'dscp_remark' band must have"
 
1464
                                   " 'prec_level'");
 
1465
                }
 
1466
            } else {
 
1467
                if (band->prec_level) {
 
1468
                    return xstrdup("Only 'dscp_remark' band may have"
 
1469
                                   " 'prec_level'");
 
1470
                }
 
1471
            }
 
1472
            if (!band->rate) {
 
1473
                return xstrdup("band must have 'rate'");
 
1474
            }
 
1475
            if (mm->meter.flags & OFPMF13_BURST) {
 
1476
                if (!band->burst_size) {
 
1477
                    return xstrdup("band must have 'burst_size' "
 
1478
                                   "when 'burst' flag is set");
 
1479
                }
 
1480
            } else {
 
1481
                if (band->burst_size) {
 
1482
                    return xstrdup("band may have 'burst_size' only "
 
1483
                                   "when 'burst' flag is set");
 
1484
                }
 
1485
            }
 
1486
        }
 
1487
    } else {
 
1488
        mm->meter.n_bands = 0;
 
1489
        mm->meter.bands = NULL;
 
1490
    }
 
1491
 
 
1492
    return NULL;
 
1493
}
 
1494
 
 
1495
/* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
 
1496
 * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
 
1497
 *
 
1498
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
1499
 * error.  The caller is responsible for freeing the returned string. */
 
1500
char * WARN_UNUSED_RESULT
 
1501
parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_,
 
1502
                        int command)
 
1503
{
 
1504
    struct ofpbuf bands;
 
1505
    char *string;
 
1506
    char *error;
 
1507
 
 
1508
    ofpbuf_init(&bands, 64);
 
1509
    string = xstrdup(str_);
 
1510
 
 
1511
    error = parse_ofp_meter_mod_str__(mm, string, &bands, command);
 
1512
 
 
1513
    free(string);
 
1514
    ofpbuf_uninit(&bands);
 
1515
 
 
1516
    return error;
 
1517
}
 
1518
 
 
1519
static char * WARN_UNUSED_RESULT
 
1520
parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
 
1521
                             const char *str_, char *string)
 
1522
{
 
1523
    static atomic_uint32_t id = ATOMIC_VAR_INIT(0);
 
1524
    char *save_ptr = NULL;
 
1525
    char *name;
 
1526
 
 
1527
    atomic_add(&id, 1, &fmr->id);
 
1528
 
963
1529
    fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
964
1530
                  | NXFMF_OWN | NXFMF_ACTIONS);
965
1531
    fmr->out_port = OFPP_NONE;
992
1558
 
993
1559
            value = strtok_r(NULL, ", \t\r\n", &save_ptr);
994
1560
            if (!value) {
995
 
                ovs_fatal(0, "%s: field %s missing value", str_, name);
 
1561
                return xasprintf("%s: field %s missing value", str_, name);
996
1562
            }
997
1563
 
998
1564
            if (!strcmp(name, "table")) {
999
 
                fmr->table_id = str_to_table_id(value);
 
1565
                char *error = str_to_u8(value, "table", &fmr->table_id);
 
1566
                if (error) {
 
1567
                    return error;
 
1568
                }
1000
1569
            } else if (!strcmp(name, "out_port")) {
1001
 
                fmr->out_port = atoi(value);
 
1570
                fmr->out_port = u16_to_ofp(atoi(value));
1002
1571
            } else if (mf_from_name(name)) {
1003
 
                parse_field(mf_from_name(name), value, &fmr->match);
 
1572
                char *error;
 
1573
 
 
1574
                error = parse_field(mf_from_name(name), value, &fmr->match);
 
1575
                if (error) {
 
1576
                    return error;
 
1577
                }
1004
1578
            } else {
1005
 
                ovs_fatal(0, "%s: unknown keyword %s", str_, name);
 
1579
                return xasprintf("%s: unknown keyword %s", str_, name);
1006
1580
            }
1007
1581
        }
1008
1582
    }
 
1583
    return NULL;
 
1584
}
 
1585
 
 
1586
/* Convert 'str_' (as described in the documentation for the "monitor" command
 
1587
 * in the ovs-ofctl man page) into 'fmr'.
 
1588
 *
 
1589
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
1590
 * error.  The caller is responsible for freeing the returned string. */
 
1591
char * WARN_UNUSED_RESULT
 
1592
parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
 
1593
                           const char *str_)
 
1594
{
 
1595
    char *string = xstrdup(str_);
 
1596
    char *error = parse_flow_monitor_request__(fmr, str_, string);
1009
1597
    free(string);
 
1598
    return error;
1010
1599
}
1011
1600
 
1012
1601
/* Parses 's' as a set of OpenFlow actions and appends the actions to
1013
1602
 * 'actions'.
1014
1603
 *
1015
 
 * Prints an error on stderr and aborts the program if 's' syntax is
1016
 
 * invalid. */
1017
 
void
 
1604
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
1605
 * error.  The caller is responsible for freeing the returned string. */
 
1606
char * WARN_UNUSED_RESULT
1018
1607
parse_ofpacts(const char *s_, struct ofpbuf *ofpacts)
1019
1608
{
1020
1609
    char *s = xstrdup(s_);
1021
 
    str_to_ofpacts(NULL, s, ofpacts);
 
1610
    char *error = str_to_ofpacts(s, ofpacts);
1022
1611
    free(s);
 
1612
 
 
1613
    return error;
1023
1614
}
1024
1615
 
1025
1616
/* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
1026
 
 * (one of OFPFC_*) into 'fm'. */
1027
 
void
 
1617
 * (one of OFPFC_*) into 'fm'.
 
1618
 *
 
1619
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
1620
 * error.  The caller is responsible for freeing the returned string. */
 
1621
char * WARN_UNUSED_RESULT
1028
1622
parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
1029
 
                       uint16_t command, bool verbose)
 
1623
                       uint16_t command)
1030
1624
{
1031
 
    struct match match_copy;
1032
 
 
1033
 
    parse_ofp_str(fm, command, string, verbose);
1034
 
 
1035
 
    /* Normalize a copy of the match.  This ensures that non-normalized flows
1036
 
     * get logged but doesn't affect what gets sent to the switch, so that the
1037
 
     * switch can do whatever it likes with the flow. */
1038
 
    match_copy = fm->match;
1039
 
    ofputil_normalize_match(&match_copy);
 
1625
    char *error = parse_ofp_str(fm, command, string);
 
1626
    if (!error) {
 
1627
        /* Normalize a copy of the match.  This ensures that non-normalized
 
1628
         * flows get logged but doesn't affect what gets sent to the switch, so
 
1629
         * that the switch can do whatever it likes with the flow. */
 
1630
        struct match match_copy = fm->match;
 
1631
        ofputil_normalize_match(&match_copy);
 
1632
    }
 
1633
 
 
1634
    return error;
1040
1635
}
1041
1636
 
1042
 
void
 
1637
/* Opens file 'file_name' and reads each line as a flow_mod of the specified
 
1638
 * type (one of OFPFC_*).  Stores each flow_mod in '*fm', an array allocated
 
1639
 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
 
1640
 *
 
1641
 * Returns NULL if successful, otherwise a malloc()'d string describing the
 
1642
 * error.  The caller is responsible for freeing the returned string. */
 
1643
char * WARN_UNUSED_RESULT
1043
1644
parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
1044
1645
                        struct ofputil_flow_mod **fms, size_t *n_fms)
1045
1646
{
1046
1647
    size_t allocated_fms;
 
1648
    int line_number;
1047
1649
    FILE *stream;
1048
1650
    struct ds s;
1049
1651
 
 
1652
    *fms = NULL;
 
1653
    *n_fms = 0;
 
1654
 
1050
1655
    stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1051
1656
    if (stream == NULL) {
1052
 
        ovs_fatal(errno, "%s: open", file_name);
 
1657
        return xasprintf("%s: open failed (%s)",
 
1658
                         file_name, ovs_strerror(errno));
1053
1659
    }
1054
1660
 
1055
1661
    allocated_fms = *n_fms;
1056
1662
    ds_init(&s);
1057
 
    while (!ds_get_preprocessed_line(&s, stream)) {
 
1663
    line_number = 0;
 
1664
    while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
 
1665
        char *error;
 
1666
 
1058
1667
        if (*n_fms >= allocated_fms) {
1059
1668
            *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
1060
1669
        }
1061
 
        parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command, false);
 
1670
        error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command);
 
1671
        if (error) {
 
1672
            size_t i;
 
1673
 
 
1674
            for (i = 0; i < *n_fms; i++) {
 
1675
                free((*fms)[i].ofpacts);
 
1676
            }
 
1677
            free(*fms);
 
1678
            *fms = NULL;
 
1679
            *n_fms = 0;
 
1680
 
 
1681
            ds_destroy(&s);
 
1682
            if (stream != stdin) {
 
1683
                fclose(stream);
 
1684
            }
 
1685
 
 
1686
            return xasprintf("%s:%d: %s", file_name, line_number, error);
 
1687
        }
1062
1688
        *n_fms += 1;
1063
1689
    }
 
1690
 
1064
1691
    ds_destroy(&s);
1065
 
 
1066
1692
    if (stream != stdin) {
1067
1693
        fclose(stream);
1068
1694
    }
 
1695
    return NULL;
1069
1696
}
1070
1697
 
1071
 
void
 
1698
char * WARN_UNUSED_RESULT
1072
1699
parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
1073
1700
                                 bool aggregate, const char *string)
1074
1701
{
1075
1702
    struct ofputil_flow_mod fm;
1076
 
 
1077
 
    parse_ofp_str(&fm, -1, string, false);
 
1703
    char *error;
 
1704
 
 
1705
    error = parse_ofp_str(&fm, -1, string);
 
1706
    if (error) {
 
1707
        return error;
 
1708
    }
 
1709
 
1078
1710
    fsr->aggregate = aggregate;
1079
1711
    fsr->cookie = fm.cookie;
1080
1712
    fsr->cookie_mask = fm.cookie_mask;
1081
1713
    fsr->match = fm.match;
1082
1714
    fsr->out_port = fm.out_port;
1083
1715
    fsr->table_id = fm.table_id;
 
1716
    return NULL;
1084
1717
}
1085
1718
 
1086
1719
/* Parses a specification of a flow from 's' into 'flow'.  's' must take the
1151
1784
        }
1152
1785
    }
1153
1786
 
1154
 
    if (!flow->in_port) {
1155
 
        flow->in_port = OFPP_NONE;
 
1787
    if (!flow->in_port.ofp_port) {
 
1788
        flow->in_port.ofp_port = OFPP_NONE;
1156
1789
    }
1157
1790
 
1158
1791
exit: