]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ofp-parse.c
Allow OFPACT_SET_IP_DSCP to act on both IPv4 and IPv6 packets.
[mirror_ovs.git] / lib / ofp-parse.c
1 /*
2 * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18
19 #include "ofp-parse.h"
20
21 #include <ctype.h>
22 #include <errno.h>
23 #include <stdlib.h>
24
25 #include "bundle.h"
26 #include "byte-order.h"
27 #include "dynamic-string.h"
28 #include "learn.h"
29 #include "meta-flow.h"
30 #include "multipath.h"
31 #include "netdev.h"
32 #include "nx-match.h"
33 #include "ofp-actions.h"
34 #include "ofp-util.h"
35 #include "ofpbuf.h"
36 #include "openflow/openflow.h"
37 #include "ovs-thread.h"
38 #include "packets.h"
39 #include "simap.h"
40 #include "socket-util.h"
41 #include "vconn.h"
42
43 /* Parses 'str' as an 8-bit unsigned integer into '*valuep'.
44 *
45 * 'name' describes the value parsed in an error message, if any.
46 *
47 * Returns NULL if successful, otherwise a malloc()'d string describing the
48 * error. The caller is responsible for freeing the returned string. */
49 static char * WARN_UNUSED_RESULT
50 str_to_u8(const char *str, const char *name, uint8_t *valuep)
51 {
52 int value;
53
54 if (!str_to_int(str, 0, &value) || value < 0 || value > 255) {
55 return xasprintf("invalid %s \"%s\"", name, str);
56 }
57 *valuep = value;
58 return NULL;
59 }
60
61 /* Parses 'str' as a 16-bit unsigned integer into '*valuep'.
62 *
63 * 'name' describes the value parsed in an error message, if any.
64 *
65 * Returns NULL if successful, otherwise a malloc()'d string describing the
66 * error. The caller is responsible for freeing the returned string. */
67 static char * WARN_UNUSED_RESULT
68 str_to_u16(const char *str, const char *name, uint16_t *valuep)
69 {
70 int value;
71
72 if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) {
73 return xasprintf("invalid %s \"%s\"", name, str);
74 }
75 *valuep = value;
76 return NULL;
77 }
78
79 /* Parses 'str' as a 32-bit unsigned integer into '*valuep'.
80 *
81 * Returns NULL if successful, otherwise a malloc()'d string describing the
82 * error. The caller is responsible for freeing the returned string. */
83 static char * WARN_UNUSED_RESULT
84 str_to_u32(const char *str, uint32_t *valuep)
85 {
86 char *tail;
87 uint32_t value;
88
89 if (!str[0]) {
90 return xstrdup("missing required numeric argument");
91 }
92
93 errno = 0;
94 value = strtoul(str, &tail, 0);
95 if (errno == EINVAL || errno == ERANGE || *tail) {
96 return xasprintf("invalid numeric format %s", str);
97 }
98 *valuep = value;
99 return NULL;
100 }
101
102 /* Parses 'str' as an 64-bit unsigned integer into '*valuep'.
103 *
104 * Returns NULL if successful, otherwise a malloc()'d string describing the
105 * error. The caller is responsible for freeing the returned string. */
106 static char * WARN_UNUSED_RESULT
107 str_to_u64(const char *str, uint64_t *valuep)
108 {
109 char *tail;
110 uint64_t value;
111
112 if (!str[0]) {
113 return xstrdup("missing required numeric argument");
114 }
115
116 errno = 0;
117 value = strtoull(str, &tail, 0);
118 if (errno == EINVAL || errno == ERANGE || *tail) {
119 return xasprintf("invalid numeric format %s", str);
120 }
121 *valuep = value;
122 return NULL;
123 }
124
125 /* Parses 'str' as an 64-bit unsigned integer in network byte order into
126 * '*valuep'.
127 *
128 * Returns NULL if successful, otherwise a malloc()'d string describing the
129 * error. The caller is responsible for freeing the returned string. */
130 static char * WARN_UNUSED_RESULT
131 str_to_be64(const char *str, ovs_be64 *valuep)
132 {
133 uint64_t value = 0;
134 char *error;
135
136 error = str_to_u64(str, &value);
137 if (!error) {
138 *valuep = htonll(value);
139 }
140 return error;
141 }
142
143 /* Parses 'str' as an Ethernet address into 'mac'.
144 *
145 * Returns NULL if successful, otherwise a malloc()'d string describing the
146 * error. The caller is responsible for freeing the returned string. */
147 static char * WARN_UNUSED_RESULT
148 str_to_mac(const char *str, uint8_t mac[6])
149 {
150 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
151 != ETH_ADDR_SCAN_COUNT) {
152 return xasprintf("invalid mac address %s", str);
153 }
154 return NULL;
155 }
156
157 /* Parses 'str' as an IP address into '*ip'.
158 *
159 * Returns NULL if successful, otherwise a malloc()'d string describing the
160 * error. The caller is responsible for freeing the returned string. */
161 static char * WARN_UNUSED_RESULT
162 str_to_ip(const char *str, ovs_be32 *ip)
163 {
164 struct in_addr in_addr;
165
166 if (lookup_ip(str, &in_addr)) {
167 return xasprintf("%s: could not convert to IP address", str);
168 }
169 *ip = in_addr.s_addr;
170 return NULL;
171 }
172
173 /* Parses 'arg' as the argument to an "enqueue" action, and appends such an
174 * action to 'ofpacts'.
175 *
176 * Returns NULL if successful, otherwise a malloc()'d string describing the
177 * error. The caller is responsible for freeing the returned string. */
178 static char * WARN_UNUSED_RESULT
179 parse_enqueue(char *arg, struct ofpbuf *ofpacts)
180 {
181 char *sp = NULL;
182 char *port = strtok_r(arg, ":q", &sp);
183 char *queue = strtok_r(NULL, "", &sp);
184 struct ofpact_enqueue *enqueue;
185
186 if (port == NULL || queue == NULL) {
187 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
188 }
189
190 enqueue = ofpact_put_ENQUEUE(ofpacts);
191 if (!ofputil_port_from_string(port, &enqueue->port)) {
192 return xasprintf("%s: enqueue to unknown port", port);
193 }
194 return str_to_u32(queue, &enqueue->queue);
195 }
196
197 /* Parses 'arg' as the argument to an "output" action, and appends such an
198 * action to 'ofpacts'.
199 *
200 * Returns NULL if successful, otherwise a malloc()'d string describing the
201 * error. The caller is responsible for freeing the returned string. */
202 static char * WARN_UNUSED_RESULT
203 parse_output(const char *arg, struct ofpbuf *ofpacts)
204 {
205 if (strchr(arg, '[')) {
206 struct ofpact_output_reg *output_reg;
207
208 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
209 output_reg->max_len = UINT16_MAX;
210 return mf_parse_subfield(&output_reg->src, arg);
211 } else {
212 struct ofpact_output *output;
213
214 output = ofpact_put_OUTPUT(ofpacts);
215 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
216 if (!ofputil_port_from_string(arg, &output->port)) {
217 return xasprintf("%s: output to unknown port", arg);
218 }
219 return NULL;
220 }
221 }
222
223 /* Parses 'arg' as the argument to an "resubmit" action, and appends such an
224 * action to 'ofpacts'.
225 *
226 * Returns NULL if successful, otherwise a malloc()'d string describing the
227 * error. The caller is responsible for freeing the returned string. */
228 static char * WARN_UNUSED_RESULT
229 parse_resubmit(char *arg, struct ofpbuf *ofpacts)
230 {
231 struct ofpact_resubmit *resubmit;
232 char *in_port_s, *table_s;
233
234 resubmit = ofpact_put_RESUBMIT(ofpacts);
235
236 in_port_s = strsep(&arg, ",");
237 if (in_port_s && in_port_s[0]) {
238 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
239 return xasprintf("%s: resubmit to unknown port", in_port_s);
240 }
241 } else {
242 resubmit->in_port = OFPP_IN_PORT;
243 }
244
245 table_s = strsep(&arg, ",");
246 if (table_s && table_s[0]) {
247 uint32_t table_id = 0;
248 char *error;
249
250 error = str_to_u32(table_s, &table_id);
251 if (error) {
252 return error;
253 }
254 resubmit->table_id = table_id;
255 } else {
256 resubmit->table_id = 255;
257 }
258
259 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
260 return xstrdup("at least one \"in_port\" or \"table\" must be "
261 "specified on resubmit");
262 }
263 return NULL;
264 }
265
266 /* Parses 'arg' as the argument to a "note" action, and appends such an action
267 * to 'ofpacts'.
268 *
269 * Returns NULL if successful, otherwise a malloc()'d string describing the
270 * error. The caller is responsible for freeing the returned string. */
271 static char * WARN_UNUSED_RESULT
272 parse_note(const char *arg, struct ofpbuf *ofpacts)
273 {
274 struct ofpact_note *note;
275
276 note = ofpact_put_NOTE(ofpacts);
277 while (*arg != '\0') {
278 uint8_t byte;
279 bool ok;
280
281 if (*arg == '.') {
282 arg++;
283 }
284 if (*arg == '\0') {
285 break;
286 }
287
288 byte = hexits_value(arg, 2, &ok);
289 if (!ok) {
290 return xstrdup("bad hex digit in `note' argument");
291 }
292 ofpbuf_put(ofpacts, &byte, 1);
293
294 note = ofpacts->l2;
295 note->length++;
296
297 arg += 2;
298 }
299 ofpact_update_len(ofpacts, &note->ofpact);
300 return NULL;
301 }
302
303 /* Parses 'arg' as the argument to a "fin_timeout" action, and appends such an
304 * action to 'ofpacts'.
305 *
306 * Returns NULL if successful, otherwise a malloc()'d string describing the
307 * error. The caller is responsible for freeing the returned string. */
308 static char * WARN_UNUSED_RESULT
309 parse_fin_timeout(struct ofpbuf *b, char *arg)
310 {
311 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(b);
312 char *key, *value;
313
314 while (ofputil_parse_key_value(&arg, &key, &value)) {
315 char *error;
316
317 if (!strcmp(key, "idle_timeout")) {
318 error = str_to_u16(value, key, &oft->fin_idle_timeout);
319 } else if (!strcmp(key, "hard_timeout")) {
320 error = str_to_u16(value, key, &oft->fin_hard_timeout);
321 } else {
322 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
323 key);
324 }
325
326 if (error) {
327 return error;
328 }
329 }
330 return NULL;
331 }
332
333 /* Parses 'arg' as the argument to a "controller" action, and appends such an
334 * action to 'ofpacts'.
335 *
336 * Returns NULL if successful, otherwise a malloc()'d string describing the
337 * error. The caller is responsible for freeing the returned string. */
338 static char * WARN_UNUSED_RESULT
339 parse_controller(struct ofpbuf *b, char *arg)
340 {
341 enum ofp_packet_in_reason reason = OFPR_ACTION;
342 uint16_t controller_id = 0;
343 uint16_t max_len = UINT16_MAX;
344
345 if (!arg[0]) {
346 /* Use defaults. */
347 } else if (strspn(arg, "0123456789") == strlen(arg)) {
348 char *error = str_to_u16(arg, "max_len", &max_len);
349 if (error) {
350 return error;
351 }
352 } else {
353 char *name, *value;
354
355 while (ofputil_parse_key_value(&arg, &name, &value)) {
356 if (!strcmp(name, "reason")) {
357 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
358 return xasprintf("unknown reason \"%s\"", value);
359 }
360 } else if (!strcmp(name, "max_len")) {
361 char *error = str_to_u16(value, "max_len", &max_len);
362 if (error) {
363 return error;
364 }
365 } else if (!strcmp(name, "id")) {
366 char *error = str_to_u16(value, "id", &controller_id);
367 if (error) {
368 return error;
369 }
370 } else {
371 return xasprintf("unknown key \"%s\" parsing controller "
372 "action", name);
373 }
374 }
375 }
376
377 if (reason == OFPR_ACTION && controller_id == 0) {
378 struct ofpact_output *output;
379
380 output = ofpact_put_OUTPUT(b);
381 output->port = OFPP_CONTROLLER;
382 output->max_len = max_len;
383 } else {
384 struct ofpact_controller *controller;
385
386 controller = ofpact_put_CONTROLLER(b);
387 controller->max_len = max_len;
388 controller->reason = reason;
389 controller->controller_id = controller_id;
390 }
391
392 return NULL;
393 }
394
395 static void
396 parse_noargs_dec_ttl(struct ofpbuf *b)
397 {
398 struct ofpact_cnt_ids *ids;
399 uint16_t id = 0;
400
401 ids = ofpact_put_DEC_TTL(b);
402 ofpbuf_put(b, &id, sizeof id);
403 ids = b->l2;
404 ids->n_controllers++;
405 ofpact_update_len(b, &ids->ofpact);
406 }
407
408 /* Parses 'arg' as the argument to a "dec_ttl" action, and appends such an
409 * action to 'ofpacts'.
410 *
411 * Returns NULL if successful, otherwise a malloc()'d string describing the
412 * error. The caller is responsible for freeing the returned string. */
413 static char * WARN_UNUSED_RESULT
414 parse_dec_ttl(struct ofpbuf *b, char *arg)
415 {
416 if (*arg == '\0') {
417 parse_noargs_dec_ttl(b);
418 } else {
419 struct ofpact_cnt_ids *ids;
420 char *cntr;
421
422 ids = ofpact_put_DEC_TTL(b);
423 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
424 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
425 cntr = strtok_r(NULL, ", ", &arg)) {
426 uint16_t id = atoi(cntr);
427
428 ofpbuf_put(b, &id, sizeof id);
429 ids = b->l2;
430 ids->n_controllers++;
431 }
432 if (!ids->n_controllers) {
433 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
434 "id.");
435 }
436 ofpact_update_len(b, &ids->ofpact);
437 }
438 return NULL;
439 }
440
441 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
442 * action to 'ofpacts'.
443 *
444 * Returns NULL if successful, otherwise a malloc()'d string describing the
445 * error. The caller is responsible for freeing the returned string. */
446 static char * WARN_UNUSED_RESULT
447 parse_set_mpls_ttl(struct ofpbuf *b, const char *arg)
448 {
449 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(b);
450
451 if (*arg == '\0') {
452 return xstrdup("parse_set_mpls_ttl: expected ttl.");
453 }
454
455 mpls_ttl->ttl = atoi(arg);
456 return NULL;
457 }
458
459 /* Parses a "set_field" action with argument 'arg', appending the parsed
460 * action to 'ofpacts'.
461 *
462 * Returns NULL if successful, otherwise a malloc()'d string describing the
463 * error. The caller is responsible for freeing the returned string. */
464 static char * WARN_UNUSED_RESULT
465 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
466 enum ofputil_protocol *usable_protocols)
467 {
468 struct ofpact_reg_load *load = ofpact_put_REG_LOAD(ofpacts);
469 char *value;
470 char *delim;
471 char *key;
472 const struct mf_field *mf;
473 char *error;
474 union mf_value mf_value;
475
476 value = arg;
477 delim = strstr(arg, "->");
478 if (!delim) {
479 return xasprintf("%s: missing `->'", arg);
480 }
481 if (strlen(delim) <= strlen("->")) {
482 return xasprintf("%s: missing field name following `->'", arg);
483 }
484
485 key = delim + strlen("->");
486 mf = mf_from_name(key);
487 if (!mf) {
488 return xasprintf("%s is not a valid OXM field name", key);
489 }
490 if (!mf->writable) {
491 return xasprintf("%s is read-only", key);
492 }
493
494 delim[0] = '\0';
495 error = mf_parse_value(mf, value, &mf_value);
496 if (error) {
497 return error;
498 }
499 if (!mf_is_value_valid(mf, &mf_value)) {
500 return xasprintf("%s is not a valid value for field %s", value, key);
501 }
502 ofpact_set_field_init(load, mf, &mf_value);
503
504 *usable_protocols &= mf->usable_protocols;
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 enum ofputil_protocol *usable_protocols)
516 {
517 char *copy = xstrdup(arg);
518 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
519 free(copy);
520 return error;
521 }
522
523 /* Parses 'arg' as the argument to a "write_metadata" instruction, and appends
524 * such an action to 'ofpacts'.
525 *
526 * Returns NULL if successful, otherwise a malloc()'d string describing the
527 * error. The caller is responsible for freeing the returned string. */
528 static char * WARN_UNUSED_RESULT
529 parse_metadata(struct ofpbuf *b, char *arg)
530 {
531 struct ofpact_metadata *om;
532 char *mask = strchr(arg, '/');
533
534 om = ofpact_put_WRITE_METADATA(b);
535
536 if (mask) {
537 char *error;
538
539 *mask = '\0';
540 error = str_to_be64(mask + 1, &om->mask);
541 if (error) {
542 return error;
543 }
544 } else {
545 om->mask = OVS_BE64_MAX;
546 }
547
548 return str_to_be64(arg, &om->metadata);
549 }
550
551 /* Parses 'arg' as the argument to a "sample" action, and appends such an
552 * action to 'ofpacts'.
553 *
554 * Returns NULL if successful, otherwise a malloc()'d string describing the
555 * error. The caller is responsible for freeing the returned string. */
556 static char * WARN_UNUSED_RESULT
557 parse_sample(struct ofpbuf *b, char *arg)
558 {
559 struct ofpact_sample *os = ofpact_put_SAMPLE(b);
560 char *key, *value;
561
562 while (ofputil_parse_key_value(&arg, &key, &value)) {
563 char *error = NULL;
564
565 if (!strcmp(key, "probability")) {
566 error = str_to_u16(value, "probability", &os->probability);
567 if (!error && os->probability == 0) {
568 error = xasprintf("invalid probability value \"%s\"", value);
569 }
570 } else if (!strcmp(key, "collector_set_id")) {
571 error = str_to_u32(value, &os->collector_set_id);
572 } else if (!strcmp(key, "obs_domain_id")) {
573 error = str_to_u32(value, &os->obs_domain_id);
574 } else if (!strcmp(key, "obs_point_id")) {
575 error = str_to_u32(value, &os->obs_point_id);
576 } else {
577 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
578 key);
579 }
580 if (error) {
581 return error;
582 }
583 }
584 if (os->probability == 0) {
585 return xstrdup("non-zero \"probability\" must be specified on sample");
586 }
587 return NULL;
588 }
589
590 /* Parses 'arg' as the argument to action 'code', and appends such an action to
591 * 'ofpacts'.
592 *
593 * Returns NULL if successful, otherwise a malloc()'d string describing the
594 * error. The caller is responsible for freeing the returned string. */
595 static char * WARN_UNUSED_RESULT
596 parse_named_action(enum ofputil_action_code code,
597 char *arg, struct ofpbuf *ofpacts,
598 enum ofputil_protocol *usable_protocols)
599 {
600 size_t orig_size = ofpacts->size;
601 struct ofpact_tunnel *tunnel;
602 char *error = NULL;
603 uint16_t ethertype = 0;
604 uint16_t vid = 0;
605 uint8_t tos = 0;
606 uint8_t pcp = 0;
607
608 switch (code) {
609 case OFPUTIL_ACTION_INVALID:
610 NOT_REACHED();
611
612 case OFPUTIL_OFPAT10_OUTPUT:
613 case OFPUTIL_OFPAT11_OUTPUT:
614 error = parse_output(arg, ofpacts);
615 break;
616
617 case OFPUTIL_OFPAT10_SET_VLAN_VID:
618 case OFPUTIL_OFPAT11_SET_VLAN_VID:
619 error = str_to_u16(arg, "VLAN VID", &vid);
620 if (error) {
621 return error;
622 }
623
624 if (vid & ~VLAN_VID_MASK) {
625 return xasprintf("%s: not a valid VLAN VID", arg);
626 }
627 ofpact_put_SET_VLAN_VID(ofpacts)->vlan_vid = vid;
628 break;
629
630 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
631 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
632 error = str_to_u8(arg, "VLAN PCP", &pcp);
633 if (error) {
634 return error;
635 }
636
637 if (pcp & ~7) {
638 return xasprintf("%s: not a valid VLAN PCP", arg);
639 }
640 ofpact_put_SET_VLAN_PCP(ofpacts)->vlan_pcp = pcp;
641 break;
642
643 case OFPUTIL_OFPAT12_SET_FIELD:
644 return set_field_parse(arg, ofpacts, usable_protocols);
645
646 case OFPUTIL_OFPAT10_STRIP_VLAN:
647 case OFPUTIL_OFPAT11_POP_VLAN:
648 ofpact_put_STRIP_VLAN(ofpacts);
649 break;
650
651 case OFPUTIL_OFPAT11_PUSH_VLAN:
652 *usable_protocols &= OFPUTIL_P_OF11_UP;
653 error = str_to_u16(arg, "ethertype", &ethertype);
654 if (error) {
655 return error;
656 }
657
658 if (ethertype != ETH_TYPE_VLAN_8021Q) {
659 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
660 return xasprintf("%s: not a valid VLAN ethertype", arg);
661 }
662
663 ofpact_put_PUSH_VLAN(ofpacts);
664 break;
665
666 case OFPUTIL_OFPAT11_SET_QUEUE:
667 error = str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
668 break;
669
670 case OFPUTIL_OFPAT10_SET_DL_SRC:
671 case OFPUTIL_OFPAT11_SET_DL_SRC:
672 error = str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
673 break;
674
675 case OFPUTIL_OFPAT10_SET_DL_DST:
676 case OFPUTIL_OFPAT11_SET_DL_DST:
677 error = str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
678 break;
679
680 case OFPUTIL_OFPAT10_SET_NW_SRC:
681 case OFPUTIL_OFPAT11_SET_NW_SRC:
682 error = str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
683 break;
684
685 case OFPUTIL_OFPAT10_SET_NW_DST:
686 case OFPUTIL_OFPAT11_SET_NW_DST:
687 error = str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
688 break;
689
690 case OFPUTIL_OFPAT10_SET_NW_TOS:
691 case OFPUTIL_OFPAT11_SET_NW_TOS:
692 error = str_to_u8(arg, "TOS", &tos);
693 if (error) {
694 return error;
695 }
696
697 if (tos & ~IP_DSCP_MASK) {
698 return xasprintf("%s: not a valid TOS", arg);
699 }
700 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
701 break;
702
703 case OFPUTIL_OFPAT11_DEC_NW_TTL:
704 NOT_REACHED();
705
706 case OFPUTIL_OFPAT10_SET_TP_SRC:
707 case OFPUTIL_OFPAT11_SET_TP_SRC:
708 error = str_to_u16(arg, "source port",
709 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
710 break;
711
712 case OFPUTIL_OFPAT10_SET_TP_DST:
713 case OFPUTIL_OFPAT11_SET_TP_DST:
714 error = str_to_u16(arg, "destination port",
715 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
716 break;
717
718 case OFPUTIL_OFPAT10_ENQUEUE:
719 error = parse_enqueue(arg, ofpacts);
720 break;
721
722 case OFPUTIL_NXAST_RESUBMIT:
723 error = parse_resubmit(arg, ofpacts);
724 break;
725
726 case OFPUTIL_NXAST_SET_TUNNEL:
727 case OFPUTIL_NXAST_SET_TUNNEL64:
728 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
729 tunnel->ofpact.compat = code;
730 error = str_to_u64(arg, &tunnel->tun_id);
731 break;
732
733 case OFPUTIL_NXAST_WRITE_METADATA:
734 error = parse_metadata(ofpacts, arg);
735 break;
736
737 case OFPUTIL_NXAST_SET_QUEUE:
738 error = str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
739 break;
740
741 case OFPUTIL_NXAST_POP_QUEUE:
742 ofpact_put_POP_QUEUE(ofpacts);
743 break;
744
745 case OFPUTIL_NXAST_REG_MOVE:
746 error = nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts), arg);
747 break;
748
749 case OFPUTIL_NXAST_REG_LOAD:
750 error = nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts), arg);
751 break;
752
753 case OFPUTIL_NXAST_NOTE:
754 error = parse_note(arg, ofpacts);
755 break;
756
757 case OFPUTIL_NXAST_MULTIPATH:
758 error = multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
759 break;
760
761 case OFPUTIL_NXAST_BUNDLE:
762 error = bundle_parse(arg, ofpacts);
763 break;
764
765 case OFPUTIL_NXAST_BUNDLE_LOAD:
766 error = bundle_parse_load(arg, ofpacts);
767 break;
768
769 case OFPUTIL_NXAST_RESUBMIT_TABLE:
770 case OFPUTIL_NXAST_OUTPUT_REG:
771 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
772 NOT_REACHED();
773
774 case OFPUTIL_NXAST_LEARN:
775 error = learn_parse(arg, ofpacts);
776 break;
777
778 case OFPUTIL_NXAST_EXIT:
779 ofpact_put_EXIT(ofpacts);
780 break;
781
782 case OFPUTIL_NXAST_DEC_TTL:
783 error = parse_dec_ttl(ofpacts, arg);
784 break;
785
786 case OFPUTIL_NXAST_SET_MPLS_TTL:
787 case OFPUTIL_OFPAT11_SET_MPLS_TTL:
788 error = parse_set_mpls_ttl(ofpacts, arg);
789 break;
790
791 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
792 case OFPUTIL_NXAST_DEC_MPLS_TTL:
793 ofpact_put_DEC_MPLS_TTL(ofpacts);
794 break;
795
796 case OFPUTIL_NXAST_FIN_TIMEOUT:
797 error = parse_fin_timeout(ofpacts, arg);
798 break;
799
800 case OFPUTIL_NXAST_CONTROLLER:
801 error = parse_controller(ofpacts, arg);
802 break;
803
804 case OFPUTIL_OFPAT11_PUSH_MPLS:
805 case OFPUTIL_NXAST_PUSH_MPLS:
806 error = str_to_u16(arg, "push_mpls", &ethertype);
807 if (!error) {
808 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
809 }
810 break;
811
812 case OFPUTIL_OFPAT11_POP_MPLS:
813 case OFPUTIL_NXAST_POP_MPLS:
814 error = str_to_u16(arg, "pop_mpls", &ethertype);
815 if (!error) {
816 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
817 }
818 break;
819
820 case OFPUTIL_OFPAT11_GROUP:
821 error = str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
822 break;
823
824 case OFPUTIL_NXAST_STACK_PUSH:
825 error = nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
826 break;
827 case OFPUTIL_NXAST_STACK_POP:
828 error = nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
829 break;
830
831 case OFPUTIL_NXAST_SAMPLE:
832 error = parse_sample(ofpacts, arg);
833 break;
834 }
835
836 if (error) {
837 ofpacts->size = orig_size;
838 }
839 return error;
840 }
841
842 /* Parses action 'act', with argument 'arg', and appends a parsed version to
843 * 'ofpacts'.
844 *
845 * 'n_actions' specifies the number of actions already parsed (for proper
846 * handling of "drop" actions).
847 *
848 * Returns NULL if successful, otherwise a malloc()'d string describing the
849 * error. The caller is responsible for freeing the returned string. */
850 static char * WARN_UNUSED_RESULT
851 str_to_ofpact__(char *pos, char *act, char *arg,
852 struct ofpbuf *ofpacts, int n_actions,
853 enum ofputil_protocol *usable_protocols)
854 {
855 int code = ofputil_action_code_from_name(act);
856 if (code >= 0) {
857 return parse_named_action(code, arg, ofpacts, usable_protocols);
858 } else if (!strcasecmp(act, "drop")) {
859 if (n_actions) {
860 return xstrdup("Drop actions must not be preceded by other "
861 "actions");
862 } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
863 return xstrdup("Drop actions must not be followed by other "
864 "actions");
865 }
866 } else {
867 ofp_port_t port;
868 if (ofputil_port_from_string(act, &port)) {
869 ofpact_put_OUTPUT(ofpacts)->port = port;
870 } else {
871 return xasprintf("Unknown action: %s", act);
872 }
873 }
874
875 return NULL;
876 }
877
878 /* Parses 'str' as a series of actions, and appends them to 'ofpacts'.
879 *
880 * Returns NULL if successful, otherwise a malloc()'d string describing the
881 * error. The caller is responsible for freeing the returned string. */
882 static char * WARN_UNUSED_RESULT
883 str_to_ofpacts__(char *str, struct ofpbuf *ofpacts,
884 enum ofputil_protocol *usable_protocols)
885 {
886 size_t orig_size = ofpacts->size;
887 char *pos, *act, *arg;
888 int n_actions;
889
890 pos = str;
891 n_actions = 0;
892 while (ofputil_parse_key_value(&pos, &act, &arg)) {
893 char *error = str_to_ofpact__(pos, act, arg, ofpacts, n_actions,
894 usable_protocols);
895 if (error) {
896 ofpacts->size = orig_size;
897 return error;
898 }
899 n_actions++;
900 }
901
902 ofpact_pad(ofpacts);
903 return NULL;
904 }
905
906
907 /* Parses 'str' as a series of actions, and appends them to 'ofpacts'.
908 *
909 * Returns NULL if successful, otherwise a malloc()'d string describing the
910 * error. The caller is responsible for freeing the returned string. */
911 static char * WARN_UNUSED_RESULT
912 str_to_ofpacts(char *str, struct ofpbuf *ofpacts,
913 enum ofputil_protocol *usable_protocols)
914 {
915 size_t orig_size = ofpacts->size;
916 char *error_s;
917 enum ofperr error;
918
919 error_s = str_to_ofpacts__(str, ofpacts, usable_protocols);
920 if (error_s) {
921 return error_s;
922 }
923
924 error = ofpacts_verify(ofpacts->data, ofpacts->size);
925 if (error) {
926 ofpacts->size = orig_size;
927 return xstrdup("Incorrect action ordering");
928 }
929
930 return NULL;
931 }
932
933 /* Parses 'arg' as the argument to instruction 'type', and appends such an
934 * instruction to 'ofpacts'.
935 *
936 * Returns NULL if successful, otherwise a malloc()'d string describing the
937 * error. The caller is responsible for freeing the returned string. */
938 static char * WARN_UNUSED_RESULT
939 parse_named_instruction(enum ovs_instruction_type type,
940 char *arg, struct ofpbuf *ofpacts,
941 enum ofputil_protocol *usable_protocols)
942 {
943 char *error_s = NULL;
944 enum ofperr error;
945
946 *usable_protocols &= OFPUTIL_P_OF11_UP;
947
948 switch (type) {
949 case OVSINST_OFPIT11_APPLY_ACTIONS:
950 NOT_REACHED(); /* This case is handled by str_to_inst_ofpacts() */
951 break;
952
953 case OVSINST_OFPIT11_WRITE_ACTIONS: {
954 struct ofpact_nest *on;
955 size_t ofs;
956
957 ofpact_pad(ofpacts);
958 ofs = ofpacts->size;
959 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
960 offsetof(struct ofpact_nest, actions));
961 error_s = str_to_ofpacts__(arg, ofpacts, usable_protocols);
962
963 on = ofpbuf_at_assert(ofpacts, ofs, sizeof *on);
964 on->ofpact.len = ofpacts->size - ofs;
965
966 if (error_s) {
967 ofpacts->size = ofs;
968 }
969 break;
970 }
971
972 case OVSINST_OFPIT11_CLEAR_ACTIONS:
973 ofpact_put_CLEAR_ACTIONS(ofpacts);
974 break;
975
976 case OVSINST_OFPIT13_METER:
977 *usable_protocols &= OFPUTIL_P_OF13_UP;
978 error_s = str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
979 break;
980
981 case OVSINST_OFPIT11_WRITE_METADATA:
982 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
983 error_s = parse_metadata(ofpacts, arg);
984 break;
985
986 case OVSINST_OFPIT11_GOTO_TABLE: {
987 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
988 char *table_s = strsep(&arg, ",");
989 if (!table_s || !table_s[0]) {
990 return xstrdup("instruction goto-table needs table id");
991 }
992 error_s = str_to_u8(table_s, "table", &ogt->table_id);
993 break;
994 }
995 }
996
997 if (error_s) {
998 return error_s;
999 }
1000
1001 /* If write_metadata is specified as an action AND an instruction, ofpacts
1002 could be invalid. */
1003 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1004 if (error) {
1005 return xstrdup("Incorrect instruction ordering");
1006 }
1007 return NULL;
1008 }
1009
1010 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
1011 *
1012 * Returns NULL if successful, otherwise a malloc()'d string describing the
1013 * error. The caller is responsible for freeing the returned string. */
1014 static char * WARN_UNUSED_RESULT
1015 str_to_inst_ofpacts(char *str, struct ofpbuf *ofpacts,
1016 enum ofputil_protocol *usable_protocols)
1017 {
1018 size_t orig_size = ofpacts->size;
1019 char *pos, *inst, *arg;
1020 int type;
1021 const char *prev_inst = NULL;
1022 int prev_type = -1;
1023 int n_actions = 0;
1024
1025 pos = str;
1026 while (ofputil_parse_key_value(&pos, &inst, &arg)) {
1027 type = ovs_instruction_type_from_name(inst);
1028 if (type < 0) {
1029 char *error = str_to_ofpact__(pos, inst, arg, ofpacts, n_actions,
1030 usable_protocols);
1031 if (error) {
1032 ofpacts->size = orig_size;
1033 return error;
1034 }
1035
1036 type = OVSINST_OFPIT11_APPLY_ACTIONS;
1037 if (prev_type == type) {
1038 n_actions++;
1039 continue;
1040 }
1041 } else if (type == OVSINST_OFPIT11_APPLY_ACTIONS) {
1042 ofpacts->size = orig_size;
1043 return xasprintf("%s isn't supported. Just write actions then "
1044 "it is interpreted as apply_actions", inst);
1045 } else {
1046 char *error = parse_named_instruction(type, arg, ofpacts,
1047 usable_protocols);
1048 if (error) {
1049 ofpacts->size = orig_size;
1050 return error;
1051 }
1052 }
1053
1054 if (type <= prev_type) {
1055 ofpacts->size = orig_size;
1056 if (type == prev_type) {
1057 return xasprintf("instruction %s may be specified only once",
1058 inst);
1059 } else {
1060 return xasprintf("instruction %s must be specified before %s",
1061 inst, prev_inst);
1062 }
1063 }
1064
1065 prev_inst = inst;
1066 prev_type = type;
1067 n_actions++;
1068 }
1069 ofpact_pad(ofpacts);
1070
1071 return NULL;
1072 }
1073
1074 struct protocol {
1075 const char *name;
1076 uint16_t dl_type;
1077 uint8_t nw_proto;
1078 };
1079
1080 static bool
1081 parse_protocol(const char *name, const struct protocol **p_out)
1082 {
1083 static const struct protocol protocols[] = {
1084 { "ip", ETH_TYPE_IP, 0 },
1085 { "arp", ETH_TYPE_ARP, 0 },
1086 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
1087 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
1088 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
1089 { "sctp", ETH_TYPE_IP, IPPROTO_SCTP },
1090 { "ipv6", ETH_TYPE_IPV6, 0 },
1091 { "ip6", ETH_TYPE_IPV6, 0 },
1092 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
1093 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
1094 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
1095 { "sctp6", ETH_TYPE_IPV6, IPPROTO_SCTP },
1096 { "rarp", ETH_TYPE_RARP, 0},
1097 { "mpls", ETH_TYPE_MPLS, 0 },
1098 { "mplsm", ETH_TYPE_MPLS_MCAST, 0 },
1099 };
1100 const struct protocol *p;
1101
1102 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
1103 if (!strcmp(p->name, name)) {
1104 *p_out = p;
1105 return true;
1106 }
1107 }
1108 *p_out = NULL;
1109 return false;
1110 }
1111
1112 /* Parses 's' as the (possibly masked) value of field 'mf', and updates
1113 * 'match' appropriately. Restricts the set of usable protocols to ones
1114 * supporting the parsed field.
1115 *
1116 * Returns NULL if successful, otherwise a malloc()'d string describing the
1117 * error. The caller is responsible for freeing the returned string. */
1118 static char * WARN_UNUSED_RESULT
1119 parse_field(const struct mf_field *mf, const char *s, struct match *match,
1120 enum ofputil_protocol *usable_protocols)
1121 {
1122 union mf_value value, mask;
1123 char *error;
1124
1125 error = mf_parse(mf, s, &value, &mask);
1126 if (!error) {
1127 *usable_protocols &= mf_set(mf, &value, &mask, match);
1128 }
1129 return error;
1130 }
1131
1132 static char * WARN_UNUSED_RESULT
1133 parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string,
1134 enum ofputil_protocol *usable_protocols)
1135 {
1136 enum {
1137 F_OUT_PORT = 1 << 0,
1138 F_ACTIONS = 1 << 1,
1139 F_TIMEOUT = 1 << 3,
1140 F_PRIORITY = 1 << 4,
1141 F_FLAGS = 1 << 5,
1142 } fields;
1143 char *save_ptr = NULL;
1144 char *act_str = NULL;
1145 char *name;
1146
1147 *usable_protocols = OFPUTIL_P_ANY;
1148
1149 switch (command) {
1150 case -1:
1151 fields = F_OUT_PORT;
1152 break;
1153
1154 case OFPFC_ADD:
1155 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
1156 break;
1157
1158 case OFPFC_DELETE:
1159 fields = F_OUT_PORT;
1160 break;
1161
1162 case OFPFC_DELETE_STRICT:
1163 fields = F_OUT_PORT | F_PRIORITY;
1164 break;
1165
1166 case OFPFC_MODIFY:
1167 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
1168 break;
1169
1170 case OFPFC_MODIFY_STRICT:
1171 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
1172 break;
1173
1174 default:
1175 NOT_REACHED();
1176 }
1177
1178 match_init_catchall(&fm->match);
1179 fm->priority = OFP_DEFAULT_PRIORITY;
1180 fm->cookie = htonll(0);
1181 fm->cookie_mask = htonll(0);
1182 if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
1183 /* For modify, by default, don't update the cookie. */
1184 fm->new_cookie = OVS_BE64_MAX;
1185 } else{
1186 fm->new_cookie = htonll(0);
1187 }
1188 fm->modify_cookie = false;
1189 fm->table_id = 0xff;
1190 fm->command = command;
1191 fm->idle_timeout = OFP_FLOW_PERMANENT;
1192 fm->hard_timeout = OFP_FLOW_PERMANENT;
1193 fm->buffer_id = UINT32_MAX;
1194 fm->out_port = OFPP_ANY;
1195 fm->flags = 0;
1196 fm->out_group = OFPG11_ANY;
1197 if (fields & F_ACTIONS) {
1198 act_str = strstr(string, "action");
1199 if (!act_str) {
1200 return xstrdup("must specify an action");
1201 }
1202 *act_str = '\0';
1203
1204 act_str = strchr(act_str + 1, '=');
1205 if (!act_str) {
1206 return xstrdup("must specify an action");
1207 }
1208
1209 act_str++;
1210 }
1211 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1212 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1213 const struct protocol *p;
1214 char *error = NULL;
1215
1216 if (parse_protocol(name, &p)) {
1217 match_set_dl_type(&fm->match, htons(p->dl_type));
1218 if (p->nw_proto) {
1219 match_set_nw_proto(&fm->match, p->nw_proto);
1220 }
1221 } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
1222 fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
1223 } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
1224 fm->flags |= OFPUTIL_FF_CHECK_OVERLAP;
1225 } else if (fields & F_FLAGS && !strcmp(name, "reset_counts")) {
1226 fm->flags |= OFPUTIL_FF_RESET_COUNTS;
1227 *usable_protocols &= OFPUTIL_P_OF12_UP;
1228 } else if (fields & F_FLAGS && !strcmp(name, "no_packet_counts")) {
1229 fm->flags |= OFPUTIL_FF_NO_PKT_COUNTS;
1230 *usable_protocols &= OFPUTIL_P_OF13_UP;
1231 } else if (fields & F_FLAGS && !strcmp(name, "no_byte_counts")) {
1232 fm->flags |= OFPUTIL_FF_NO_BYT_COUNTS;
1233 *usable_protocols &= OFPUTIL_P_OF13_UP;
1234 } else {
1235 char *value;
1236
1237 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1238 if (!value) {
1239 return xasprintf("field %s missing value", name);
1240 }
1241
1242 if (!strcmp(name, "table")) {
1243 error = str_to_u8(value, "table", &fm->table_id);
1244 if (fm->table_id != 0xff) {
1245 *usable_protocols &= OFPUTIL_P_TID;
1246 }
1247 } else if (!strcmp(name, "out_port")) {
1248 if (!ofputil_port_from_string(value, &fm->out_port)) {
1249 error = xasprintf("%s is not a valid OpenFlow port",
1250 value);
1251 }
1252 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
1253 uint16_t priority = 0;
1254
1255 error = str_to_u16(value, name, &priority);
1256 fm->priority = priority;
1257 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
1258 error = str_to_u16(value, name, &fm->idle_timeout);
1259 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
1260 error = str_to_u16(value, name, &fm->hard_timeout);
1261 } else if (!strcmp(name, "cookie")) {
1262 char *mask = strchr(value, '/');
1263
1264 if (mask) {
1265 /* A mask means we're searching for a cookie. */
1266 if (command == OFPFC_ADD) {
1267 return xstrdup("flow additions cannot use "
1268 "a cookie mask");
1269 }
1270 *mask = '\0';
1271 error = str_to_be64(value, &fm->cookie);
1272 if (error) {
1273 return error;
1274 }
1275 error = str_to_be64(mask + 1, &fm->cookie_mask);
1276
1277 /* Matching of the cookie is only supported through NXM or
1278 * OF1.1+. */
1279 if (fm->cookie_mask != htonll(0)) {
1280 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
1281 }
1282 } else {
1283 /* No mask means that the cookie is being set. */
1284 if (command != OFPFC_ADD && command != OFPFC_MODIFY
1285 && command != OFPFC_MODIFY_STRICT) {
1286 return xstrdup("cannot set cookie");
1287 }
1288 error = str_to_be64(value, &fm->new_cookie);
1289 fm->modify_cookie = true;
1290 }
1291 } else if (mf_from_name(name)) {
1292 error = parse_field(mf_from_name(name), value, &fm->match,
1293 usable_protocols);
1294 } else if (!strcmp(name, "duration")
1295 || !strcmp(name, "n_packets")
1296 || !strcmp(name, "n_bytes")
1297 || !strcmp(name, "idle_age")
1298 || !strcmp(name, "hard_age")) {
1299 /* Ignore these, so that users can feed the output of
1300 * "ovs-ofctl dump-flows" back into commands that parse
1301 * flows. */
1302 } else {
1303 error = xasprintf("unknown keyword %s", name);
1304 }
1305
1306 if (error) {
1307 return error;
1308 }
1309 }
1310 }
1311 /* Check for usable protocol interdependencies between match fields. */
1312 if (fm->match.flow.dl_type == htons(ETH_TYPE_IPV6)) {
1313 const struct flow_wildcards *wc = &fm->match.wc;
1314 /* Only NXM and OXM support matching L3 and L4 fields within IPv6.
1315 *
1316 * (IPv6 specific fields as well as arp_sha, arp_tha, nw_frag, and
1317 * nw_ttl are covered elsewhere so they don't need to be included in
1318 * this test too.)
1319 */
1320 if (wc->masks.nw_proto || wc->masks.nw_tos
1321 || wc->masks.tp_src || wc->masks.tp_dst) {
1322 *usable_protocols &= OFPUTIL_P_NXM_OXM_ANY;
1323 }
1324 }
1325 if (!fm->cookie_mask && fm->new_cookie == OVS_BE64_MAX
1326 && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
1327 /* On modifies without a mask, we are supposed to add a flow if
1328 * one does not exist. If a cookie wasn't been specified, use a
1329 * default of zero. */
1330 fm->new_cookie = htonll(0);
1331 }
1332 if (fields & F_ACTIONS) {
1333 struct ofpbuf ofpacts;
1334 char *error;
1335
1336 ofpbuf_init(&ofpacts, 32);
1337 error = str_to_inst_ofpacts(act_str, &ofpacts, usable_protocols);
1338 if (!error) {
1339 enum ofperr err;
1340
1341 err = ofpacts_check(ofpacts.data, ofpacts.size, &fm->match.flow,
1342 OFPP_MAX, 0);
1343 if (err) {
1344 error = xasprintf("actions are invalid with specified match "
1345 "(%s)", ofperr_to_string(err));
1346 }
1347 }
1348 if (error) {
1349 ofpbuf_uninit(&ofpacts);
1350 return error;
1351 }
1352
1353 fm->ofpacts_len = ofpacts.size;
1354 fm->ofpacts = ofpbuf_steal_data(&ofpacts);
1355 } else {
1356 fm->ofpacts_len = 0;
1357 fm->ofpacts = NULL;
1358 }
1359
1360 return NULL;
1361 }
1362
1363 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
1364 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
1365 * Returns the set of usable protocols in '*usable_protocols'.
1366 *
1367 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
1368 * constant for 'command'. To parse syntax for an OFPST_FLOW or
1369 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'.
1370 *
1371 * Returns NULL if successful, otherwise a malloc()'d string describing the
1372 * error. The caller is responsible for freeing the returned string. */
1373 char * WARN_UNUSED_RESULT
1374 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
1375 enum ofputil_protocol *usable_protocols)
1376 {
1377 char *string = xstrdup(str_);
1378 char *error;
1379
1380 error = parse_ofp_str__(fm, command, string, usable_protocols);
1381 if (error) {
1382 fm->ofpacts = NULL;
1383 fm->ofpacts_len = 0;
1384 }
1385
1386 free(string);
1387 return error;
1388 }
1389
1390 static char * WARN_UNUSED_RESULT
1391 parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string,
1392 struct ofpbuf *bands, int command,
1393 enum ofputil_protocol *usable_protocols)
1394 {
1395 enum {
1396 F_METER = 1 << 0,
1397 F_FLAGS = 1 << 1,
1398 F_BANDS = 1 << 2,
1399 } fields;
1400 char *save_ptr = NULL;
1401 char *band_str = NULL;
1402 char *name;
1403
1404 /* Meters require at least OF 1.3. */
1405 *usable_protocols = OFPUTIL_P_OF13_UP;
1406
1407 switch (command) {
1408 case -1:
1409 fields = F_METER;
1410 break;
1411
1412 case OFPMC13_ADD:
1413 fields = F_METER | F_FLAGS | F_BANDS;
1414 break;
1415
1416 case OFPMC13_DELETE:
1417 fields = F_METER;
1418 break;
1419
1420 case OFPMC13_MODIFY:
1421 fields = F_METER | F_FLAGS | F_BANDS;
1422 break;
1423
1424 default:
1425 NOT_REACHED();
1426 }
1427
1428 mm->command = command;
1429 mm->meter.meter_id = 0;
1430 mm->meter.flags = 0;
1431 if (fields & F_BANDS) {
1432 band_str = strstr(string, "band");
1433 if (!band_str) {
1434 return xstrdup("must specify bands");
1435 }
1436 *band_str = '\0';
1437
1438 band_str = strchr(band_str + 1, '=');
1439 if (!band_str) {
1440 return xstrdup("must specify bands");
1441 }
1442
1443 band_str++;
1444 }
1445 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1446 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1447
1448 if (fields & F_FLAGS && !strcmp(name, "kbps")) {
1449 mm->meter.flags |= OFPMF13_KBPS;
1450 } else if (fields & F_FLAGS && !strcmp(name, "pktps")) {
1451 mm->meter.flags |= OFPMF13_PKTPS;
1452 } else if (fields & F_FLAGS && !strcmp(name, "burst")) {
1453 mm->meter.flags |= OFPMF13_BURST;
1454 } else if (fields & F_FLAGS && !strcmp(name, "stats")) {
1455 mm->meter.flags |= OFPMF13_STATS;
1456 } else {
1457 char *value;
1458
1459 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1460 if (!value) {
1461 return xasprintf("field %s missing value", name);
1462 }
1463
1464 if (!strcmp(name, "meter")) {
1465 if (!strcmp(value, "all")) {
1466 mm->meter.meter_id = OFPM13_ALL;
1467 } else if (!strcmp(value, "controller")) {
1468 mm->meter.meter_id = OFPM13_CONTROLLER;
1469 } else if (!strcmp(value, "slowpath")) {
1470 mm->meter.meter_id = OFPM13_SLOWPATH;
1471 } else {
1472 char *error = str_to_u32(value, &mm->meter.meter_id);
1473 if (error) {
1474 return error;
1475 }
1476 if (mm->meter.meter_id > OFPM13_MAX) {
1477 return xasprintf("invalid value for %s", name);
1478 }
1479 }
1480 } else {
1481 return xasprintf("unknown keyword %s", name);
1482 }
1483 }
1484 }
1485 if (fields & F_METER && !mm->meter.meter_id) {
1486 return xstrdup("must specify 'meter'");
1487 }
1488 if (fields & F_FLAGS && !mm->meter.flags) {
1489 return xstrdup("meter must specify either 'kbps' or 'pktps'");
1490 }
1491
1492 if (fields & F_BANDS) {
1493 uint16_t n_bands = 0;
1494 struct ofputil_meter_band *band = NULL;
1495 int i;
1496
1497 for (name = strtok_r(band_str, "=, \t\r\n", &save_ptr); name;
1498 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1499
1500 char *value;
1501
1502 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1503 if (!value) {
1504 return xasprintf("field %s missing value", name);
1505 }
1506
1507 if (!strcmp(name, "type")) {
1508 /* Start a new band */
1509 band = ofpbuf_put_zeros(bands, sizeof *band);
1510 n_bands++;
1511
1512 if (!strcmp(value, "drop")) {
1513 band->type = OFPMBT13_DROP;
1514 } else if (!strcmp(value, "dscp_remark")) {
1515 band->type = OFPMBT13_DSCP_REMARK;
1516 } else {
1517 return xasprintf("field %s unknown value %s", name, value);
1518 }
1519 } else if (!band || !band->type) {
1520 return xstrdup("band must start with the 'type' keyword");
1521 } else if (!strcmp(name, "rate")) {
1522 char *error = str_to_u32(value, &band->rate);
1523 if (error) {
1524 return error;
1525 }
1526 } else if (!strcmp(name, "burst_size")) {
1527 char *error = str_to_u32(value, &band->burst_size);
1528 if (error) {
1529 return error;
1530 }
1531 } else if (!strcmp(name, "prec_level")) {
1532 char *error = str_to_u8(value, name, &band->prec_level);
1533 if (error) {
1534 return error;
1535 }
1536 } else {
1537 return xasprintf("unknown keyword %s", name);
1538 }
1539 }
1540 /* validate bands */
1541 if (!n_bands) {
1542 return xstrdup("meter must have bands");
1543 }
1544
1545 mm->meter.n_bands = n_bands;
1546 mm->meter.bands = ofpbuf_steal_data(bands);
1547
1548 for (i = 0; i < n_bands; ++i) {
1549 band = &mm->meter.bands[i];
1550
1551 if (!band->type) {
1552 return xstrdup("band must have 'type'");
1553 }
1554 if (band->type == OFPMBT13_DSCP_REMARK) {
1555 if (!band->prec_level) {
1556 return xstrdup("'dscp_remark' band must have"
1557 " 'prec_level'");
1558 }
1559 } else {
1560 if (band->prec_level) {
1561 return xstrdup("Only 'dscp_remark' band may have"
1562 " 'prec_level'");
1563 }
1564 }
1565 if (!band->rate) {
1566 return xstrdup("band must have 'rate'");
1567 }
1568 if (mm->meter.flags & OFPMF13_BURST) {
1569 if (!band->burst_size) {
1570 return xstrdup("band must have 'burst_size' "
1571 "when 'burst' flag is set");
1572 }
1573 } else {
1574 if (band->burst_size) {
1575 return xstrdup("band may have 'burst_size' only "
1576 "when 'burst' flag is set");
1577 }
1578 }
1579 }
1580 } else {
1581 mm->meter.n_bands = 0;
1582 mm->meter.bands = NULL;
1583 }
1584
1585 return NULL;
1586 }
1587
1588 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
1589 * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
1590 *
1591 * Returns NULL if successful, otherwise a malloc()'d string describing the
1592 * error. The caller is responsible for freeing the returned string. */
1593 char * WARN_UNUSED_RESULT
1594 parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_,
1595 int command, enum ofputil_protocol *usable_protocols)
1596 {
1597 struct ofpbuf bands;
1598 char *string;
1599 char *error;
1600
1601 ofpbuf_init(&bands, 64);
1602 string = xstrdup(str_);
1603
1604 error = parse_ofp_meter_mod_str__(mm, string, &bands, command,
1605 usable_protocols);
1606
1607 free(string);
1608 ofpbuf_uninit(&bands);
1609
1610 return error;
1611 }
1612
1613 static char * WARN_UNUSED_RESULT
1614 parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
1615 const char *str_, char *string,
1616 enum ofputil_protocol *usable_protocols)
1617 {
1618 static atomic_uint32_t id = ATOMIC_VAR_INIT(0);
1619 char *save_ptr = NULL;
1620 char *name;
1621
1622 atomic_add(&id, 1, &fmr->id);
1623
1624 fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
1625 | NXFMF_OWN | NXFMF_ACTIONS);
1626 fmr->out_port = OFPP_NONE;
1627 fmr->table_id = 0xff;
1628 match_init_catchall(&fmr->match);
1629
1630 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1631 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1632 const struct protocol *p;
1633
1634 if (!strcmp(name, "!initial")) {
1635 fmr->flags &= ~NXFMF_INITIAL;
1636 } else if (!strcmp(name, "!add")) {
1637 fmr->flags &= ~NXFMF_ADD;
1638 } else if (!strcmp(name, "!delete")) {
1639 fmr->flags &= ~NXFMF_DELETE;
1640 } else if (!strcmp(name, "!modify")) {
1641 fmr->flags &= ~NXFMF_MODIFY;
1642 } else if (!strcmp(name, "!actions")) {
1643 fmr->flags &= ~NXFMF_ACTIONS;
1644 } else if (!strcmp(name, "!own")) {
1645 fmr->flags &= ~NXFMF_OWN;
1646 } else if (parse_protocol(name, &p)) {
1647 match_set_dl_type(&fmr->match, htons(p->dl_type));
1648 if (p->nw_proto) {
1649 match_set_nw_proto(&fmr->match, p->nw_proto);
1650 }
1651 } else {
1652 char *value;
1653
1654 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1655 if (!value) {
1656 return xasprintf("%s: field %s missing value", str_, name);
1657 }
1658
1659 if (!strcmp(name, "table")) {
1660 char *error = str_to_u8(value, "table", &fmr->table_id);
1661 if (error) {
1662 return error;
1663 }
1664 } else if (!strcmp(name, "out_port")) {
1665 fmr->out_port = u16_to_ofp(atoi(value));
1666 } else if (mf_from_name(name)) {
1667 char *error;
1668
1669 error = parse_field(mf_from_name(name), value, &fmr->match,
1670 usable_protocols);
1671 if (error) {
1672 return error;
1673 }
1674 } else {
1675 return xasprintf("%s: unknown keyword %s", str_, name);
1676 }
1677 }
1678 }
1679 return NULL;
1680 }
1681
1682 /* Convert 'str_' (as described in the documentation for the "monitor" command
1683 * in the ovs-ofctl man page) into 'fmr'.
1684 *
1685 * Returns NULL if successful, otherwise a malloc()'d string describing the
1686 * error. The caller is responsible for freeing the returned string. */
1687 char * WARN_UNUSED_RESULT
1688 parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
1689 const char *str_,
1690 enum ofputil_protocol *usable_protocols)
1691 {
1692 char *string = xstrdup(str_);
1693 char *error = parse_flow_monitor_request__(fmr, str_, string,
1694 usable_protocols);
1695 free(string);
1696 return error;
1697 }
1698
1699 /* Parses 's' as a set of OpenFlow actions and appends the actions to
1700 * 'actions'.
1701 *
1702 * Returns NULL if successful, otherwise a malloc()'d string describing the
1703 * error. The caller is responsible for freeing the returned string. */
1704 char * WARN_UNUSED_RESULT
1705 parse_ofpacts(const char *s_, struct ofpbuf *ofpacts,
1706 enum ofputil_protocol *usable_protocols)
1707 {
1708 char *s = xstrdup(s_);
1709 char *error;
1710
1711 *usable_protocols = OFPUTIL_P_ANY;
1712
1713 error = str_to_ofpacts(s, ofpacts, usable_protocols);
1714 free(s);
1715
1716 return error;
1717 }
1718
1719 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
1720 * (one of OFPFC_*) into 'fm'.
1721 *
1722 * Returns NULL if successful, otherwise a malloc()'d string describing the
1723 * error. The caller is responsible for freeing the returned string. */
1724 char * WARN_UNUSED_RESULT
1725 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
1726 uint16_t command,
1727 enum ofputil_protocol *usable_protocols)
1728 {
1729 char *error = parse_ofp_str(fm, command, string, usable_protocols);
1730 if (!error) {
1731 /* Normalize a copy of the match. This ensures that non-normalized
1732 * flows get logged but doesn't affect what gets sent to the switch, so
1733 * that the switch can do whatever it likes with the flow. */
1734 struct match match_copy = fm->match;
1735 ofputil_normalize_match(&match_copy);
1736 }
1737
1738 return error;
1739 }
1740
1741 /* Convert 'table_id' and 'flow_miss_handling' (as described for the
1742 * "mod-table" command in the ovs-ofctl man page) into 'tm' for sending the
1743 * specified table_mod 'command' to a switch.
1744 *
1745 * Returns NULL if successful, otherwise a malloc()'d string describing the
1746 * error. The caller is responsible for freeing the returned string. */
1747 char * WARN_UNUSED_RESULT
1748 parse_ofp_table_mod(struct ofputil_table_mod *tm, const char *table_id,
1749 const char *flow_miss_handling,
1750 enum ofputil_protocol *usable_protocols)
1751 {
1752 /* Table mod requires at least OF 1.1. */
1753 *usable_protocols = OFPUTIL_P_OF11_UP;
1754
1755 if (!strcasecmp(table_id, "all")) {
1756 tm->table_id = 255;
1757 } else {
1758 char *error = str_to_u8(table_id, "table_id", &tm->table_id);
1759 if (error) {
1760 return error;
1761 }
1762 }
1763
1764 if (strcmp(flow_miss_handling, "controller") == 0) {
1765 tm->config = OFPTC11_TABLE_MISS_CONTROLLER;
1766 } else if (strcmp(flow_miss_handling, "continue") == 0) {
1767 tm->config = OFPTC11_TABLE_MISS_CONTINUE;
1768 } else if (strcmp(flow_miss_handling, "drop") == 0) {
1769 tm->config = OFPTC11_TABLE_MISS_DROP;
1770 } else {
1771 return xasprintf("invalid flow_miss_handling %s", flow_miss_handling);
1772 }
1773
1774 if (tm->table_id == 0xfe && tm->config == OFPTC11_TABLE_MISS_CONTINUE) {
1775 return xstrdup("last table's flow miss handling can not be continue");
1776 }
1777
1778 return NULL;
1779 }
1780
1781
1782 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
1783 * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated
1784 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
1785 *
1786 * Returns NULL if successful, otherwise a malloc()'d string describing the
1787 * error. The caller is responsible for freeing the returned string. */
1788 char * WARN_UNUSED_RESULT
1789 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
1790 struct ofputil_flow_mod **fms, size_t *n_fms,
1791 enum ofputil_protocol *usable_protocols)
1792 {
1793 size_t allocated_fms;
1794 int line_number;
1795 FILE *stream;
1796 struct ds s;
1797
1798 *usable_protocols = OFPUTIL_P_ANY;
1799
1800 *fms = NULL;
1801 *n_fms = 0;
1802
1803 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1804 if (stream == NULL) {
1805 return xasprintf("%s: open failed (%s)",
1806 file_name, ovs_strerror(errno));
1807 }
1808
1809 allocated_fms = *n_fms;
1810 ds_init(&s);
1811 line_number = 0;
1812 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
1813 char *error;
1814 enum ofputil_protocol usable;
1815
1816 if (*n_fms >= allocated_fms) {
1817 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
1818 }
1819 error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command,
1820 &usable);
1821 if (error) {
1822 size_t i;
1823
1824 for (i = 0; i < *n_fms; i++) {
1825 free((*fms)[i].ofpacts);
1826 }
1827 free(*fms);
1828 *fms = NULL;
1829 *n_fms = 0;
1830
1831 ds_destroy(&s);
1832 if (stream != stdin) {
1833 fclose(stream);
1834 }
1835
1836 return xasprintf("%s:%d: %s", file_name, line_number, error);
1837 }
1838 *usable_protocols &= usable; /* Each line can narrow the set. */
1839 *n_fms += 1;
1840 }
1841
1842 ds_destroy(&s);
1843 if (stream != stdin) {
1844 fclose(stream);
1845 }
1846 return NULL;
1847 }
1848
1849 char * WARN_UNUSED_RESULT
1850 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
1851 bool aggregate, const char *string,
1852 enum ofputil_protocol *usable_protocols)
1853 {
1854 struct ofputil_flow_mod fm;
1855 char *error;
1856
1857 error = parse_ofp_str(&fm, -1, string, usable_protocols);
1858 if (error) {
1859 return error;
1860 }
1861
1862 /* Special table ID support not required for stats requests. */
1863 if (*usable_protocols & OFPUTIL_P_OF10_STD_TID) {
1864 *usable_protocols |= OFPUTIL_P_OF10_STD;
1865 }
1866 if (*usable_protocols & OFPUTIL_P_OF10_NXM_TID) {
1867 *usable_protocols |= OFPUTIL_P_OF10_NXM;
1868 }
1869
1870 fsr->aggregate = aggregate;
1871 fsr->cookie = fm.cookie;
1872 fsr->cookie_mask = fm.cookie_mask;
1873 fsr->match = fm.match;
1874 fsr->out_port = fm.out_port;
1875 fsr->out_group = fm.out_group;
1876 fsr->table_id = fm.table_id;
1877 return NULL;
1878 }
1879
1880 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
1881 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1882 * mf_field. Fields must be specified in a natural order for satisfying
1883 * prerequisites. If 'mask' is specified, fills the mask field for each of the
1884 * field specified in flow. If the map, 'names_portno' is specfied, converts
1885 * the in_port name into port no while setting the 'flow'.
1886 *
1887 * Returns NULL on success, otherwise a malloc()'d string that explains the
1888 * problem. */
1889 char *
1890 parse_ofp_exact_flow(struct flow *flow, struct flow *mask, const char *s,
1891 const struct simap *portno_names)
1892 {
1893 char *pos, *key, *value_s;
1894 char *error = NULL;
1895 char *copy;
1896
1897 memset(flow, 0, sizeof *flow);
1898 if (mask) {
1899 memset(mask, 0, sizeof *mask);
1900 }
1901
1902 pos = copy = xstrdup(s);
1903 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
1904 const struct protocol *p;
1905 if (parse_protocol(key, &p)) {
1906 if (flow->dl_type) {
1907 error = xasprintf("%s: Ethernet type set multiple times", s);
1908 goto exit;
1909 }
1910 flow->dl_type = htons(p->dl_type);
1911 if (mask) {
1912 mask->dl_type = OVS_BE16_MAX;
1913 }
1914
1915 if (p->nw_proto) {
1916 if (flow->nw_proto) {
1917 error = xasprintf("%s: network protocol set "
1918 "multiple times", s);
1919 goto exit;
1920 }
1921 flow->nw_proto = p->nw_proto;
1922 if (mask) {
1923 mask->nw_proto = UINT8_MAX;
1924 }
1925 }
1926 } else {
1927 const struct mf_field *mf;
1928 union mf_value value;
1929 char *field_error;
1930
1931 mf = mf_from_name(key);
1932 if (!mf) {
1933 error = xasprintf("%s: unknown field %s", s, key);
1934 goto exit;
1935 }
1936
1937 if (!mf_are_prereqs_ok(mf, flow)) {
1938 error = xasprintf("%s: prerequisites not met for setting %s",
1939 s, key);
1940 goto exit;
1941 }
1942
1943 if (!mf_is_zero(mf, flow)) {
1944 error = xasprintf("%s: field %s set multiple times", s, key);
1945 goto exit;
1946 }
1947
1948 if (!strcmp(key, "in_port")
1949 && portno_names
1950 && simap_contains(portno_names, value_s)) {
1951 flow->in_port.ofp_port = u16_to_ofp(
1952 simap_get(portno_names, value_s));
1953 if (mask) {
1954 mask->in_port.ofp_port = u16_to_ofp(ntohs(OVS_BE16_MAX));
1955 }
1956 } else {
1957 field_error = mf_parse_value(mf, value_s, &value);
1958 if (field_error) {
1959 error = xasprintf("%s: bad value for %s (%s)",
1960 s, key, field_error);
1961 free(field_error);
1962 goto exit;
1963 }
1964
1965 mf_set_flow_value(mf, &value, flow);
1966 if (mask) {
1967 mf_mask_field(mf, mask);
1968 }
1969 }
1970 }
1971 }
1972
1973 if (!flow->in_port.ofp_port) {
1974 flow->in_port.ofp_port = OFPP_NONE;
1975 }
1976
1977 exit:
1978 free(copy);
1979
1980 if (error) {
1981 memset(flow, 0, sizeof *flow);
1982 if (mask) {
1983 memset(mask, 0, sizeof *mask);
1984 }
1985 }
1986 return error;
1987 }
1988
1989 static char * WARN_UNUSED_RESULT
1990 parse_bucket_str(struct ofputil_bucket *bucket, char *str_,
1991 enum ofputil_protocol *usable_protocols)
1992 {
1993 struct ofpbuf ofpacts;
1994 char *pos, *act, *arg;
1995 int n_actions;
1996
1997 bucket->weight = 1;
1998 bucket->watch_port = OFPP_ANY;
1999 bucket->watch_group = OFPG11_ANY;
2000
2001 pos = str_;
2002 n_actions = 0;
2003 ofpbuf_init(&ofpacts, 64);
2004 while (ofputil_parse_key_value(&pos, &act, &arg)) {
2005 char *error = NULL;
2006
2007 if (!strcasecmp(act, "weight")) {
2008 error = str_to_u16(arg, "weight", &bucket->weight);
2009 } else if (!strcasecmp(act, "watch_port")) {
2010 if (!ofputil_port_from_string(arg, &bucket->watch_port)
2011 || (ofp_to_u16(bucket->watch_port) >= ofp_to_u16(OFPP_MAX)
2012 && bucket->watch_port != OFPP_ANY)) {
2013 error = xasprintf("%s: invalid watch_port", arg);
2014 }
2015 } else if (!strcasecmp(act, "watch_group")) {
2016 error = str_to_u32(arg, &bucket->watch_group);
2017 if (!error && bucket->watch_group > OFPG_MAX) {
2018 error = xasprintf("invalid watch_group id %"PRIu32,
2019 bucket->watch_group);
2020 }
2021 } else {
2022 error = str_to_ofpact__(pos, act, arg, &ofpacts, n_actions,
2023 usable_protocols);
2024 n_actions++;
2025 }
2026
2027 if (error) {
2028 ofpbuf_uninit(&ofpacts);
2029 return error;
2030 }
2031 }
2032
2033 ofpact_pad(&ofpacts);
2034 bucket->ofpacts = ofpacts.data;
2035 bucket->ofpacts_len = ofpacts.size;
2036
2037 return NULL;
2038 }
2039
2040 static char * WARN_UNUSED_RESULT
2041 parse_ofp_group_mod_str__(struct ofputil_group_mod *gm, uint16_t command,
2042 char *string,
2043 enum ofputil_protocol *usable_protocols)
2044 {
2045 enum {
2046 F_GROUP_TYPE = 1 << 0,
2047 F_BUCKETS = 1 << 1,
2048 } fields;
2049 char *save_ptr = NULL;
2050 bool had_type = false;
2051 char *name;
2052 struct ofputil_bucket *bucket;
2053 char *error = NULL;
2054
2055 *usable_protocols = OFPUTIL_P_OF11_UP;
2056
2057 switch (command) {
2058 case OFPGC11_ADD:
2059 fields = F_GROUP_TYPE | F_BUCKETS;
2060 break;
2061
2062 case OFPGC11_DELETE:
2063 fields = 0;
2064 break;
2065
2066 case OFPGC11_MODIFY:
2067 fields = F_GROUP_TYPE | F_BUCKETS;
2068 break;
2069
2070 default:
2071 NOT_REACHED();
2072 }
2073
2074 memset(gm, 0, sizeof *gm);
2075 gm->command = command;
2076 gm->group_id = OFPG_ANY;
2077 list_init(&gm->buckets);
2078 if (command == OFPGC11_DELETE && string[0] == '\0') {
2079 gm->group_id = OFPG_ALL;
2080 return NULL;
2081 }
2082
2083 *usable_protocols = OFPUTIL_P_OF11_UP;
2084
2085 if (fields & F_BUCKETS) {
2086 char *bkt_str = strstr(string, "bucket");
2087
2088 if (bkt_str) {
2089 *bkt_str = '\0';
2090 }
2091
2092 while (bkt_str) {
2093 char *next_bkt_str;
2094
2095 bkt_str = strchr(bkt_str + 1, '=');
2096 if (!bkt_str) {
2097 error = xstrdup("must specify bucket content");
2098 goto out;
2099 }
2100 bkt_str++;
2101
2102 next_bkt_str = strstr(bkt_str, "bucket");
2103 if (next_bkt_str) {
2104 *next_bkt_str = '\0';
2105 }
2106
2107 bucket = xzalloc(sizeof(struct ofputil_bucket));
2108 error = parse_bucket_str(bucket, bkt_str, usable_protocols);
2109 if (error) {
2110 free(bucket);
2111 goto out;
2112 }
2113 list_push_back(&gm->buckets, &bucket->list_node);
2114
2115 bkt_str = next_bkt_str;
2116 }
2117 }
2118
2119 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
2120 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
2121 char *value;
2122
2123 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
2124 if (!value) {
2125 error = xasprintf("field %s missing value", name);
2126 goto out;
2127 }
2128
2129 if (!strcmp(name, "group_id")) {
2130 if(!strcmp(value, "all")) {
2131 gm->group_id = OFPG_ALL;
2132 } else {
2133 char *error = str_to_u32(value, &gm->group_id);
2134 if (error) {
2135 goto out;
2136 }
2137 if (gm->group_id != OFPG_ALL && gm->group_id > OFPG_MAX) {
2138 error = xasprintf("invalid group id %"PRIu32,
2139 gm->group_id);
2140 goto out;
2141 }
2142 }
2143 } else if (!strcmp(name, "type")){
2144 if (!(fields & F_GROUP_TYPE)) {
2145 error = xstrdup("type is not needed");
2146 goto out;
2147 }
2148 if (!strcmp(value, "all")) {
2149 gm->type = OFPGT11_ALL;
2150 } else if (!strcmp(value, "select")) {
2151 gm->type = OFPGT11_SELECT;
2152 } else if (!strcmp(value, "indirect")) {
2153 gm->type = OFPGT11_INDIRECT;
2154 } else if (!strcmp(value, "ff") ||
2155 !strcmp(value, "fast_failover")) {
2156 gm->type = OFPGT11_FF;
2157 } else {
2158 error = xasprintf("invalid group type %s", value);
2159 goto out;
2160 }
2161 had_type = true;
2162 } else if (!strcmp(name, "bucket")) {
2163 error = xstrdup("bucket is not needed");
2164 goto out;
2165 } else {
2166 error = xasprintf("unknown keyword %s", name);
2167 goto out;
2168 }
2169 }
2170 if (gm->group_id == OFPG_ANY) {
2171 error = xstrdup("must specify a group_id");
2172 goto out;
2173 }
2174 if (fields & F_GROUP_TYPE && !had_type) {
2175 error = xstrdup("must specify a type");
2176 goto out;
2177 }
2178
2179 /* Validate buckets. */
2180 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
2181 if (bucket->weight != 1 && gm->type != OFPGT11_SELECT) {
2182 error = xstrdup("Only select groups can have bucket weights.");
2183 goto out;
2184 }
2185 }
2186 if (gm->type == OFPGT11_INDIRECT && !list_is_short(&gm->buckets)) {
2187 error = xstrdup("Indirect groups can have at most one bucket.");
2188 goto out;
2189 }
2190
2191 return NULL;
2192 out:
2193 ofputil_bucket_list_destroy(&gm->buckets);
2194 return error;
2195 }
2196
2197 char * WARN_UNUSED_RESULT
2198 parse_ofp_group_mod_str(struct ofputil_group_mod *gm, uint16_t command,
2199 const char *str_,
2200 enum ofputil_protocol *usable_protocols)
2201 {
2202 char *string = xstrdup(str_);
2203 char *error = parse_ofp_group_mod_str__(gm, command, string,
2204 usable_protocols);
2205 free(string);
2206
2207 if (error) {
2208 ofputil_bucket_list_destroy(&gm->buckets);
2209 }
2210 return error;
2211 }
2212
2213 char * WARN_UNUSED_RESULT
2214 parse_ofp_group_mod_file(const char *file_name, uint16_t command,
2215 struct ofputil_group_mod **gms, size_t *n_gms,
2216 enum ofputil_protocol *usable_protocols)
2217 {
2218 size_t allocated_gms;
2219 int line_number;
2220 FILE *stream;
2221 struct ds s;
2222
2223 *gms = NULL;
2224 *n_gms = 0;
2225
2226 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
2227 if (stream == NULL) {
2228 return xasprintf("%s: open failed (%s)",
2229 file_name, ovs_strerror(errno));
2230 }
2231
2232 allocated_gms = *n_gms;
2233 ds_init(&s);
2234 line_number = 0;
2235 *usable_protocols = OFPUTIL_P_OF11_UP;
2236 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
2237 enum ofputil_protocol usable;
2238 char *error;
2239
2240 if (*n_gms >= allocated_gms) {
2241 *gms = x2nrealloc(*gms, &allocated_gms, sizeof **gms);
2242 }
2243 error = parse_ofp_group_mod_str(&(*gms)[*n_gms], command, ds_cstr(&s),
2244 &usable);
2245 if (error) {
2246 size_t i;
2247
2248 for (i = 0; i < *n_gms; i++) {
2249 ofputil_bucket_list_destroy(&(*gms)[i].buckets);
2250 }
2251 free(*gms);
2252 *gms = NULL;
2253 *n_gms = 0;
2254
2255 ds_destroy(&s);
2256 if (stream != stdin) {
2257 fclose(stream);
2258 }
2259
2260 return xasprintf("%s:%d: %s", file_name, line_number, error);
2261 }
2262 *usable_protocols &= usable;
2263 *n_gms += 1;
2264 }
2265
2266 ds_destroy(&s);
2267 if (stream != stdin) {
2268 fclose(stream);
2269 }
2270 return NULL;
2271 }