2 * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "ofp-parse.h"
26 #include "byte-order.h"
27 #include "dynamic-string.h"
29 #include "meta-flow.h"
30 #include "multipath.h"
33 #include "ofp-actions.h"
36 #include "openflow/openflow.h"
37 #include "ovs-thread.h"
39 #include "socket-util.h"
43 VLOG_DEFINE_THIS_MODULE(ofp_parse);
45 /* Parses 'str' as an 8-bit unsigned integer into '*valuep'.
47 * 'name' describes the value parsed in an error message, if any.
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)
56 if (!str_to_int(str, 0, &value) || value < 0 || value > 255) {
57 return xasprintf("invalid %s \"%s\"", name, str);
63 /* Parses 'str' as a 16-bit unsigned integer into '*valuep'.
65 * 'name' describes the value parsed in an error message, if any.
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)
74 if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) {
75 return xasprintf("invalid %s \"%s\"", name, str);
81 /* Parses 'str' as a 32-bit unsigned integer into '*valuep'.
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)
92 return xstrdup("missing required numeric argument");
96 value = strtoul(str, &tail, 0);
97 if (errno == EINVAL || errno == ERANGE || *tail) {
98 return xasprintf("invalid numeric format %s", str);
104 /* Parses 'str' as an 64-bit unsigned integer into '*valuep'.
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)
115 return xstrdup("missing required numeric argument");
119 value = strtoull(str, &tail, 0);
120 if (errno == EINVAL || errno == ERANGE || *tail) {
121 return xasprintf("invalid numeric format %s", str);
127 /* Parses 'str' as an 64-bit unsigned integer in network byte order into
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)
138 error = str_to_u64(str, &value);
140 *valuep = htonll(value);
145 /* Parses 'str' as an Ethernet address into 'mac'.
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
150 str_to_mac(const char *str, uint8_t mac[6])
152 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
153 != ETH_ADDR_SCAN_COUNT) {
154 return xasprintf("invalid mac address %s", str);
159 /* Parses 'str' as an IP address into '*ip'.
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
164 str_to_ip(const char *str, ovs_be32 *ip)
166 struct in_addr in_addr;
168 if (lookup_ip(str, &in_addr)) {
169 return xasprintf("%s: could not convert to IP address", str);
171 *ip = in_addr.s_addr;
175 /* Parses 'arg' as the argument to an "enqueue" action, and appends such an
176 * action to 'ofpacts'.
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
181 parse_enqueue(char *arg, struct ofpbuf *ofpacts)
184 char *port = strtok_r(arg, ":q", &sp);
185 char *queue = strtok_r(NULL, "", &sp);
186 struct ofpact_enqueue *enqueue;
188 if (port == NULL || queue == NULL) {
189 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
192 enqueue = ofpact_put_ENQUEUE(ofpacts);
193 if (!ofputil_port_from_string(port, &enqueue->port)) {
194 return xasprintf("%s: enqueue to unknown port", port);
196 return str_to_u32(queue, &enqueue->queue);
199 /* Parses 'arg' as the argument to an "output" action, and appends such an
200 * action to 'ofpacts'.
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)
207 if (strchr(arg, '[')) {
208 struct ofpact_output_reg *output_reg;
210 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
211 output_reg->max_len = UINT16_MAX;
212 return mf_parse_subfield(&output_reg->src, arg);
214 struct ofpact_output *output;
216 output = ofpact_put_OUTPUT(ofpacts);
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);
225 /* Parses 'arg' as the argument to an "resubmit" action, and appends such an
226 * action to 'ofpacts'.
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
231 parse_resubmit(char *arg, struct ofpbuf *ofpacts)
233 struct ofpact_resubmit *resubmit;
234 char *in_port_s, *table_s;
236 resubmit = ofpact_put_RESUBMIT(ofpacts);
238 in_port_s = strsep(&arg, ",");
239 if (in_port_s && in_port_s[0]) {
240 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
241 return xasprintf("%s: resubmit to unknown port", in_port_s);
244 resubmit->in_port = OFPP_IN_PORT;
247 table_s = strsep(&arg, ",");
248 if (table_s && table_s[0]) {
249 uint32_t table_id = 0;
252 error = str_to_u32(table_s, &table_id);
256 resubmit->table_id = table_id;
258 resubmit->table_id = 255;
261 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
262 return xstrdup("at least one \"in_port\" or \"table\" must be "
263 "specified on resubmit");
268 /* Parses 'arg' as the argument to a "note" action, and appends such an action
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
274 parse_note(const char *arg, struct ofpbuf *ofpacts)
276 struct ofpact_note *note;
278 note = ofpact_put_NOTE(ofpacts);
279 while (*arg != '\0') {
290 byte = hexits_value(arg, 2, &ok);
292 return xstrdup("bad hex digit in `note' argument");
294 ofpbuf_put(ofpacts, &byte, 1);
301 ofpact_update_len(ofpacts, ¬e->ofpact);
305 /* Parses 'arg' as the argument to a "fin_timeout" action, and appends such an
306 * action to 'ofpacts'.
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
311 parse_fin_timeout(struct ofpbuf *b, char *arg)
313 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(b);
316 while (ofputil_parse_key_value(&arg, &key, &value)) {
319 if (!strcmp(key, "idle_timeout")) {
320 error = str_to_u16(value, key, &oft->fin_idle_timeout);
321 } else if (!strcmp(key, "hard_timeout")) {
322 error = str_to_u16(value, key, &oft->fin_hard_timeout);
324 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
335 /* Parses 'arg' as the argument to a "controller" action, and appends such an
336 * action to 'ofpacts'.
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
341 parse_controller(struct ofpbuf *b, char *arg)
343 enum ofp_packet_in_reason reason = OFPR_ACTION;
344 uint16_t controller_id = 0;
345 uint16_t max_len = UINT16_MAX;
349 } else if (strspn(arg, "0123456789") == strlen(arg)) {
350 char *error = str_to_u16(arg, "max_len", &max_len);
357 while (ofputil_parse_key_value(&arg, &name, &value)) {
358 if (!strcmp(name, "reason")) {
359 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
360 return xasprintf("unknown reason \"%s\"", value);
362 } else if (!strcmp(name, "max_len")) {
363 char *error = str_to_u16(value, "max_len", &max_len);
367 } else if (!strcmp(name, "id")) {
368 char *error = str_to_u16(value, "id", &controller_id);
373 return xasprintf("unknown key \"%s\" parsing controller "
379 if (reason == OFPR_ACTION && controller_id == 0) {
380 struct ofpact_output *output;
382 output = ofpact_put_OUTPUT(b);
383 output->port = OFPP_CONTROLLER;
384 output->max_len = max_len;
386 struct ofpact_controller *controller;
388 controller = ofpact_put_CONTROLLER(b);
389 controller->max_len = max_len;
390 controller->reason = reason;
391 controller->controller_id = controller_id;
398 parse_noargs_dec_ttl(struct ofpbuf *b)
400 struct ofpact_cnt_ids *ids;
403 ids = ofpact_put_DEC_TTL(b);
404 ofpbuf_put(b, &id, sizeof id);
406 ids->n_controllers++;
407 ofpact_update_len(b, &ids->ofpact);
410 /* Parses 'arg' as the argument to a "dec_ttl" action, and appends such an
411 * action to 'ofpacts'.
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
416 parse_dec_ttl(struct ofpbuf *b, char *arg)
419 parse_noargs_dec_ttl(b);
421 struct ofpact_cnt_ids *ids;
424 ids = ofpact_put_DEC_TTL(b);
425 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
426 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
427 cntr = strtok_r(NULL, ", ", &arg)) {
428 uint16_t id = atoi(cntr);
430 ofpbuf_put(b, &id, sizeof id);
432 ids->n_controllers++;
434 if (!ids->n_controllers) {
435 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
438 ofpact_update_len(b, &ids->ofpact);
443 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
444 * action to 'ofpacts'.
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)
451 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(b);
454 return xstrdup("parse_set_mpls_ttl: expected ttl.");
457 mpls_ttl->ttl = atoi(arg);
461 /* Parses a "set_field" action with argument 'arg', appending the parsed
462 * action to 'ofpacts'.
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)
469 struct ofpact_reg_load *load = ofpact_put_REG_LOAD(ofpacts);
473 const struct mf_field *mf;
475 union mf_value mf_value;
478 delim = strstr(arg, "->");
480 return xasprintf("%s: missing `->'", arg);
482 if (strlen(delim) <= strlen("->")) {
483 return xasprintf("%s: missing field name following `->'", arg);
486 key = delim + strlen("->");
487 mf = mf_from_name(key);
489 return xasprintf("%s is not a valid OXM field name", key);
492 return xasprintf("%s is read-only", key);
496 error = mf_parse_value(mf, value, &mf_value);
500 if (!mf_is_value_valid(mf, &mf_value)) {
501 return xasprintf("%s is not a valid value for field %s", value, key);
503 ofpact_set_field_init(load, mf, &mf_value);
508 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
509 * action to 'ofpacts'.
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)
516 char *copy = xstrdup(arg);
517 char *error = set_field_parse__(copy, ofpacts);
522 /* Parses 'arg' as the argument to a "write_metadata" instruction, and appends
523 * such an action to 'ofpacts'.
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
528 parse_metadata(struct ofpbuf *b, char *arg)
530 struct ofpact_metadata *om;
531 char *mask = strchr(arg, '/');
533 om = ofpact_put_WRITE_METADATA(b);
539 error = str_to_be64(mask + 1, &om->mask);
544 om->mask = htonll(UINT64_MAX);
547 return str_to_be64(arg, &om->metadata);
550 /* Parses 'arg' as the argument to a "sample" action, and appends such an
551 * action to 'ofpacts'.
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)
558 struct ofpact_sample *os = ofpact_put_SAMPLE(b);
561 while (ofputil_parse_key_value(&arg, &key, &value)) {
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);
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);
576 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
583 if (os->probability == 0) {
584 return xstrdup("non-zero \"probability\" must be specified on sample");
589 /* Parses 'arg' as the argument to action 'code', and appends such an action to
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,
596 char *arg, struct ofpbuf *ofpacts)
598 size_t orig_size = ofpacts->size;
599 struct ofpact_tunnel *tunnel;
601 uint16_t ethertype = 0;
607 case OFPUTIL_ACTION_INVALID:
610 case OFPUTIL_OFPAT10_OUTPUT:
611 case OFPUTIL_OFPAT11_OUTPUT:
612 error = parse_output(arg, ofpacts);
615 case OFPUTIL_OFPAT10_SET_VLAN_VID:
616 case OFPUTIL_OFPAT11_SET_VLAN_VID:
617 error = str_to_u16(arg, "VLAN VID", &vid);
622 if (vid & ~VLAN_VID_MASK) {
623 return xasprintf("%s: not a valid VLAN VID", arg);
625 ofpact_put_SET_VLAN_VID(ofpacts)->vlan_vid = vid;
628 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
629 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
630 error = str_to_u8(arg, "VLAN PCP", &pcp);
636 return xasprintf("%s: not a valid VLAN PCP", arg);
638 ofpact_put_SET_VLAN_PCP(ofpacts)->vlan_pcp = pcp;
641 case OFPUTIL_OFPAT12_SET_FIELD:
642 return set_field_parse(arg, ofpacts);
644 case OFPUTIL_OFPAT10_STRIP_VLAN:
645 case OFPUTIL_OFPAT11_POP_VLAN:
646 ofpact_put_STRIP_VLAN(ofpacts);
649 case OFPUTIL_OFPAT11_PUSH_VLAN:
650 error = str_to_u16(arg, "ethertype", ðertype);
655 if (ethertype != ETH_TYPE_VLAN_8021Q) {
656 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
657 return xasprintf("%s: not a valid VLAN ethertype", arg);
660 ofpact_put_PUSH_VLAN(ofpacts);
663 case OFPUTIL_OFPAT11_SET_QUEUE:
664 error = str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
667 case OFPUTIL_OFPAT10_SET_DL_SRC:
668 case OFPUTIL_OFPAT11_SET_DL_SRC:
669 error = str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
672 case OFPUTIL_OFPAT10_SET_DL_DST:
673 case OFPUTIL_OFPAT11_SET_DL_DST:
674 error = str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
677 case OFPUTIL_OFPAT10_SET_NW_SRC:
678 case OFPUTIL_OFPAT11_SET_NW_SRC:
679 error = str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
682 case OFPUTIL_OFPAT10_SET_NW_DST:
683 case OFPUTIL_OFPAT11_SET_NW_DST:
684 error = str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
687 case OFPUTIL_OFPAT10_SET_NW_TOS:
688 case OFPUTIL_OFPAT11_SET_NW_TOS:
689 error = str_to_u8(arg, "TOS", &tos);
694 if (tos & ~IP_DSCP_MASK) {
695 return xasprintf("%s: not a valid TOS", arg);
697 ofpact_put_SET_IPV4_DSCP(ofpacts)->dscp = tos;
700 case OFPUTIL_OFPAT11_DEC_NW_TTL:
703 case OFPUTIL_OFPAT10_SET_TP_SRC:
704 case OFPUTIL_OFPAT11_SET_TP_SRC:
705 error = str_to_u16(arg, "source port",
706 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
709 case OFPUTIL_OFPAT10_SET_TP_DST:
710 case OFPUTIL_OFPAT11_SET_TP_DST:
711 error = str_to_u16(arg, "destination port",
712 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
715 case OFPUTIL_OFPAT10_ENQUEUE:
716 error = parse_enqueue(arg, ofpacts);
719 case OFPUTIL_NXAST_RESUBMIT:
720 error = parse_resubmit(arg, ofpacts);
723 case OFPUTIL_NXAST_SET_TUNNEL:
724 case OFPUTIL_NXAST_SET_TUNNEL64:
725 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
726 tunnel->ofpact.compat = code;
727 error = str_to_u64(arg, &tunnel->tun_id);
730 case OFPUTIL_NXAST_WRITE_METADATA:
731 error = parse_metadata(ofpacts, arg);
734 case OFPUTIL_NXAST_SET_QUEUE:
735 error = str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
738 case OFPUTIL_NXAST_POP_QUEUE:
739 ofpact_put_POP_QUEUE(ofpacts);
742 case OFPUTIL_NXAST_REG_MOVE:
743 error = nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts), arg);
746 case OFPUTIL_NXAST_REG_LOAD:
747 error = nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts), arg);
750 case OFPUTIL_NXAST_NOTE:
751 error = parse_note(arg, ofpacts);
754 case OFPUTIL_NXAST_MULTIPATH:
755 error = multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
758 case OFPUTIL_NXAST_BUNDLE:
759 error = bundle_parse(arg, ofpacts);
762 case OFPUTIL_NXAST_BUNDLE_LOAD:
763 error = bundle_parse_load(arg, ofpacts);
766 case OFPUTIL_NXAST_RESUBMIT_TABLE:
767 case OFPUTIL_NXAST_OUTPUT_REG:
768 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
771 case OFPUTIL_NXAST_LEARN:
772 error = learn_parse(arg, ofpacts);
775 case OFPUTIL_NXAST_EXIT:
776 ofpact_put_EXIT(ofpacts);
779 case OFPUTIL_NXAST_DEC_TTL:
780 error = parse_dec_ttl(ofpacts, arg);
783 case OFPUTIL_NXAST_SET_MPLS_TTL:
784 case OFPUTIL_OFPAT11_SET_MPLS_TTL:
785 error = parse_set_mpls_ttl(ofpacts, arg);
788 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
789 case OFPUTIL_NXAST_DEC_MPLS_TTL:
790 ofpact_put_DEC_MPLS_TTL(ofpacts);
793 case OFPUTIL_NXAST_FIN_TIMEOUT:
794 error = parse_fin_timeout(ofpacts, arg);
797 case OFPUTIL_NXAST_CONTROLLER:
798 error = parse_controller(ofpacts, arg);
801 case OFPUTIL_OFPAT11_PUSH_MPLS:
802 case OFPUTIL_NXAST_PUSH_MPLS:
803 error = str_to_u16(arg, "push_mpls", ðertype);
805 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
809 case OFPUTIL_OFPAT11_POP_MPLS:
810 case OFPUTIL_NXAST_POP_MPLS:
811 error = str_to_u16(arg, "pop_mpls", ðertype);
813 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
817 case OFPUTIL_NXAST_STACK_PUSH:
818 error = nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
820 case OFPUTIL_NXAST_STACK_POP:
821 error = nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
824 case OFPUTIL_NXAST_SAMPLE:
825 error = parse_sample(ofpacts, arg);
830 ofpacts->size = orig_size;
835 /* Parses action 'act', with argument 'arg', and appends a parsed version to
838 * 'n_actions' specifies the number of actions already parsed (for proper
839 * handling of "drop" actions).
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,
845 struct ofpbuf *ofpacts, int n_actions)
847 int code = ofputil_action_code_from_name(act);
849 return parse_named_action(code, arg, ofpacts);
850 } else if (!strcasecmp(act, "drop")) {
852 return xstrdup("Drop actions must not be preceded by other "
854 } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
855 return xstrdup("Drop actions must not be followed by other "
860 if (ofputil_port_from_string(act, &port)) {
861 ofpact_put_OUTPUT(ofpacts)->port = port;
863 return xasprintf("Unknown action: %s", act);
870 /* Parses 'str' as a series of actions, and appends them to 'ofpacts'.
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)
877 size_t orig_size = ofpacts->size;
878 char *pos, *act, *arg;
884 while (ofputil_parse_key_value(&pos, &act, &arg)) {
885 char *error = str_to_ofpact__(pos, act, arg, ofpacts, n_actions);
887 ofpacts->size = orig_size;
893 error = ofpacts_verify(ofpacts->data, ofpacts->size);
895 ofpacts->size = orig_size;
896 return xstrdup("Incorrect action ordering");
903 /* Parses 'arg' as the argument to instruction 'type', and appends such an
904 * instruction to 'ofpacts'.
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
909 parse_named_instruction(enum ovs_instruction_type type,
910 char *arg, struct ofpbuf *ofpacts)
912 char *error_s = NULL;
916 case OVSINST_OFPIT11_APPLY_ACTIONS:
917 NOT_REACHED(); /* This case is handled by str_to_inst_ofpacts() */
920 case OVSINST_OFPIT11_WRITE_ACTIONS:
922 error_s = xstrdup("instruction write-actions is not supported yet");
925 case OVSINST_OFPIT11_CLEAR_ACTIONS:
926 ofpact_put_CLEAR_ACTIONS(ofpacts);
929 case OVSINST_OFPIT13_METER:
930 error_s = str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
933 case OVSINST_OFPIT11_WRITE_METADATA:
934 error_s = parse_metadata(ofpacts, arg);
937 case OVSINST_OFPIT11_GOTO_TABLE: {
938 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
939 char *table_s = strsep(&arg, ",");
940 if (!table_s || !table_s[0]) {
941 return xstrdup("instruction goto-table needs table id");
943 error_s = str_to_u8(table_s, "table", &ogt->table_id);
952 /* If write_metadata is specified as an action AND an instruction, ofpacts
954 error = ofpacts_verify(ofpacts->data, ofpacts->size);
956 return xstrdup("Incorrect instruction ordering");
961 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
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)
968 size_t orig_size = ofpacts->size;
969 char *pos, *inst, *arg;
971 const char *prev_inst = NULL;
976 while (ofputil_parse_key_value(&pos, &inst, &arg)) {
977 type = ovs_instruction_type_from_name(inst);
979 char *error = str_to_ofpact__(pos, inst, arg, ofpacts, n_actions);
981 ofpacts->size = orig_size;
985 type = OVSINST_OFPIT11_APPLY_ACTIONS;
986 if (prev_type == type) {
990 } else if (type == OVSINST_OFPIT11_APPLY_ACTIONS) {
991 ofpacts->size = orig_size;
992 return xasprintf("%s isn't supported. Just write actions then "
993 "it is interpreted as apply_actions", inst);
995 char *error = parse_named_instruction(type, arg, ofpacts);
997 ofpacts->size = orig_size;
1002 if (type <= prev_type) {
1003 ofpacts->size = orig_size;
1004 if (type == prev_type) {
1005 return xasprintf("instruction %s may be specified only once",
1008 return xasprintf("instruction %s must be specified before %s",
1017 ofpact_pad(ofpacts);
1029 parse_protocol(const char *name, const struct protocol **p_out)
1031 static const struct protocol protocols[] = {
1032 { "ip", ETH_TYPE_IP, 0 },
1033 { "arp", ETH_TYPE_ARP, 0 },
1034 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
1035 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
1036 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
1037 { "sctp", ETH_TYPE_IP, IPPROTO_SCTP },
1038 { "ipv6", ETH_TYPE_IPV6, 0 },
1039 { "ip6", ETH_TYPE_IPV6, 0 },
1040 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
1041 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
1042 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
1043 { "sctp6", ETH_TYPE_IPV6, IPPROTO_SCTP },
1044 { "rarp", ETH_TYPE_RARP, 0},
1045 { "mpls", ETH_TYPE_MPLS, 0 },
1046 { "mplsm", ETH_TYPE_MPLS_MCAST, 0 },
1048 const struct protocol *p;
1050 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
1051 if (!strcmp(p->name, name)) {
1060 /* Parses 's' as the (possibly masked) value of field 'mf', and updates
1061 * 'match' appropriately.
1063 * Returns NULL if successful, otherwise a malloc()'d string describing the
1064 * error. The caller is responsible for freeing the returned string. */
1065 static char * WARN_UNUSED_RESULT
1066 parse_field(const struct mf_field *mf, const char *s, struct match *match)
1068 union mf_value value, mask;
1071 error = mf_parse(mf, s, &value, &mask);
1073 mf_set(mf, &value, &mask, match);
1078 static char * WARN_UNUSED_RESULT
1079 parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string)
1082 F_OUT_PORT = 1 << 0,
1085 F_PRIORITY = 1 << 4,
1088 char *save_ptr = NULL;
1089 char *act_str = NULL;
1094 fields = F_OUT_PORT;
1098 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
1102 fields = F_OUT_PORT;
1105 case OFPFC_DELETE_STRICT:
1106 fields = F_OUT_PORT | F_PRIORITY;
1110 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
1113 case OFPFC_MODIFY_STRICT:
1114 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
1121 match_init_catchall(&fm->match);
1122 fm->priority = OFP_DEFAULT_PRIORITY;
1123 fm->cookie = htonll(0);
1124 fm->cookie_mask = htonll(0);
1125 if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
1126 /* For modify, by default, don't update the cookie. */
1127 fm->new_cookie = htonll(UINT64_MAX);
1129 fm->new_cookie = htonll(0);
1131 fm->modify_cookie = false;
1132 fm->table_id = 0xff;
1133 fm->command = command;
1134 fm->idle_timeout = OFP_FLOW_PERMANENT;
1135 fm->hard_timeout = OFP_FLOW_PERMANENT;
1136 fm->buffer_id = UINT32_MAX;
1137 fm->out_port = OFPP_ANY;
1139 if (fields & F_ACTIONS) {
1140 act_str = strstr(string, "action");
1142 return xstrdup("must specify an action");
1146 act_str = strchr(act_str + 1, '=');
1148 return xstrdup("must specify an action");
1153 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1154 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1155 const struct protocol *p;
1158 if (parse_protocol(name, &p)) {
1159 match_set_dl_type(&fm->match, htons(p->dl_type));
1161 match_set_nw_proto(&fm->match, p->nw_proto);
1163 } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
1164 fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
1165 } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
1166 fm->flags |= OFPUTIL_FF_CHECK_OVERLAP;
1167 } else if (fields & F_FLAGS && !strcmp(name, "reset_counts")) {
1168 fm->flags |= OFPUTIL_FF_RESET_COUNTS;
1169 } else if (fields & F_FLAGS && !strcmp(name, "no_packet_counts")) {
1170 fm->flags |= OFPUTIL_FF_NO_PKT_COUNTS;
1171 } else if (fields & F_FLAGS && !strcmp(name, "no_byte_counts")) {
1172 fm->flags |= OFPUTIL_FF_NO_BYT_COUNTS;
1176 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1178 return xasprintf("field %s missing value", name);
1181 if (!strcmp(name, "table")) {
1182 error = str_to_u8(value, "table", &fm->table_id);
1183 } else if (!strcmp(name, "out_port")) {
1184 if (!ofputil_port_from_string(value, &fm->out_port)) {
1185 error = xasprintf("%s is not a valid OpenFlow port",
1188 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
1189 uint16_t priority = 0;
1191 error = str_to_u16(value, name, &priority);
1192 fm->priority = priority;
1193 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
1194 error = str_to_u16(value, name, &fm->idle_timeout);
1195 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
1196 error = str_to_u16(value, name, &fm->hard_timeout);
1197 } else if (!strcmp(name, "cookie")) {
1198 char *mask = strchr(value, '/');
1201 /* A mask means we're searching for a cookie. */
1202 if (command == OFPFC_ADD) {
1203 return xstrdup("flow additions cannot use "
1207 error = str_to_be64(value, &fm->cookie);
1211 error = str_to_be64(mask + 1, &fm->cookie_mask);
1213 /* No mask means that the cookie is being set. */
1214 if (command != OFPFC_ADD && command != OFPFC_MODIFY
1215 && command != OFPFC_MODIFY_STRICT) {
1216 return xstrdup("cannot set cookie");
1218 error = str_to_be64(value, &fm->new_cookie);
1219 fm->modify_cookie = true;
1221 } else if (mf_from_name(name)) {
1222 error = parse_field(mf_from_name(name), value, &fm->match);
1223 } else if (!strcmp(name, "duration")
1224 || !strcmp(name, "n_packets")
1225 || !strcmp(name, "n_bytes")
1226 || !strcmp(name, "idle_age")
1227 || !strcmp(name, "hard_age")) {
1228 /* Ignore these, so that users can feed the output of
1229 * "ovs-ofctl dump-flows" back into commands that parse
1232 error = xasprintf("unknown keyword %s", name);
1240 if (!fm->cookie_mask && fm->new_cookie == htonll(UINT64_MAX)
1241 && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
1242 /* On modifies without a mask, we are supposed to add a flow if
1243 * one does not exist. If a cookie wasn't been specified, use a
1244 * default of zero. */
1245 fm->new_cookie = htonll(0);
1247 if (fields & F_ACTIONS) {
1248 struct ofpbuf ofpacts;
1251 ofpbuf_init(&ofpacts, 32);
1252 error = str_to_inst_ofpacts(act_str, &ofpacts);
1256 err = ofpacts_check(ofpacts.data, ofpacts.size, &fm->match.flow,
1259 error = xasprintf("actions are invalid with specified match "
1260 "(%s)", ofperr_to_string(err));
1264 ofpbuf_uninit(&ofpacts);
1268 fm->ofpacts_len = ofpacts.size;
1269 fm->ofpacts = ofpbuf_steal_data(&ofpacts);
1271 fm->ofpacts_len = 0;
1278 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
1279 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
1281 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
1282 * constant for 'command'. To parse syntax for an OFPST_FLOW or
1283 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'.
1285 * Returns NULL if successful, otherwise a malloc()'d string describing the
1286 * error. The caller is responsible for freeing the returned string. */
1287 char * WARN_UNUSED_RESULT
1288 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_)
1290 char *string = xstrdup(str_);
1293 error = parse_ofp_str__(fm, command, string);
1296 fm->ofpacts_len = 0;
1303 static char * WARN_UNUSED_RESULT
1304 parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string,
1305 struct ofpbuf *bands, int command)
1312 char *save_ptr = NULL;
1313 char *band_str = NULL;
1322 fields = F_METER | F_FLAGS | F_BANDS;
1325 case OFPMC13_DELETE:
1329 case OFPMC13_MODIFY:
1330 fields = F_METER | F_FLAGS | F_BANDS;
1337 mm->command = command;
1338 mm->meter.meter_id = 0;
1339 mm->meter.flags = 0;
1340 if (fields & F_BANDS) {
1341 band_str = strstr(string, "band");
1343 return xstrdup("must specify bands");
1347 band_str = strchr(band_str + 1, '=');
1349 return xstrdup("must specify bands");
1354 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1355 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1357 if (fields & F_FLAGS && !strcmp(name, "kbps")) {
1358 mm->meter.flags |= OFPMF13_KBPS;
1359 } else if (fields & F_FLAGS && !strcmp(name, "pktps")) {
1360 mm->meter.flags |= OFPMF13_PKTPS;
1361 } else if (fields & F_FLAGS && !strcmp(name, "burst")) {
1362 mm->meter.flags |= OFPMF13_BURST;
1363 } else if (fields & F_FLAGS && !strcmp(name, "stats")) {
1364 mm->meter.flags |= OFPMF13_STATS;
1368 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1370 return xasprintf("field %s missing value", name);
1373 if (!strcmp(name, "meter")) {
1374 if (!strcmp(value, "all")) {
1375 mm->meter.meter_id = OFPM13_ALL;
1376 } else if (!strcmp(value, "controller")) {
1377 mm->meter.meter_id = OFPM13_CONTROLLER;
1378 } else if (!strcmp(value, "slowpath")) {
1379 mm->meter.meter_id = OFPM13_SLOWPATH;
1381 char *error = str_to_u32(value, &mm->meter.meter_id);
1385 if (mm->meter.meter_id > OFPM13_MAX) {
1386 return xasprintf("invalid value for %s", name);
1390 return xasprintf("unknown keyword %s", name);
1394 if (fields & F_METER && !mm->meter.meter_id) {
1395 return xstrdup("must specify 'meter'");
1397 if (fields & F_FLAGS && !mm->meter.flags) {
1398 return xstrdup("meter must specify either 'kbps' or 'pktps'");
1401 if (fields & F_BANDS) {
1402 uint16_t n_bands = 0;
1403 struct ofputil_meter_band *band = NULL;
1406 for (name = strtok_r(band_str, "=, \t\r\n", &save_ptr); name;
1407 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1411 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1413 return xasprintf("field %s missing value", name);
1416 if (!strcmp(name, "type")) {
1417 /* Start a new band */
1418 band = ofpbuf_put_zeros(bands, sizeof *band);
1421 if (!strcmp(value, "drop")) {
1422 band->type = OFPMBT13_DROP;
1423 } else if (!strcmp(value, "dscp_remark")) {
1424 band->type = OFPMBT13_DSCP_REMARK;
1426 return xasprintf("field %s unknown value %s", name, value);
1428 } else if (!band || !band->type) {
1429 return xstrdup("band must start with the 'type' keyword");
1430 } else if (!strcmp(name, "rate")) {
1431 char *error = str_to_u32(value, &band->rate);
1435 } else if (!strcmp(name, "burst_size")) {
1436 char *error = str_to_u32(value, &band->burst_size);
1440 } else if (!strcmp(name, "prec_level")) {
1441 char *error = str_to_u8(value, name, &band->prec_level);
1446 return xasprintf("unknown keyword %s", name);
1449 /* validate bands */
1451 return xstrdup("meter must have bands");
1454 mm->meter.n_bands = n_bands;
1455 mm->meter.bands = ofpbuf_steal_data(bands);
1457 for (i = 0; i < n_bands; ++i) {
1458 band = &mm->meter.bands[i];
1461 return xstrdup("band must have 'type'");
1463 if (band->type == OFPMBT13_DSCP_REMARK) {
1464 if (!band->prec_level) {
1465 return xstrdup("'dscp_remark' band must have"
1469 if (band->prec_level) {
1470 return xstrdup("Only 'dscp_remark' band may have"
1475 return xstrdup("band must have 'rate'");
1477 if (mm->meter.flags & OFPMF13_BURST) {
1478 if (!band->burst_size) {
1479 return xstrdup("band must have 'burst_size' "
1480 "when 'burst' flag is set");
1483 if (band->burst_size) {
1484 return xstrdup("band may have 'burst_size' only "
1485 "when 'burst' flag is set");
1490 mm->meter.n_bands = 0;
1491 mm->meter.bands = NULL;
1497 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
1498 * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
1500 * Returns NULL if successful, otherwise a malloc()'d string describing the
1501 * error. The caller is responsible for freeing the returned string. */
1502 char * WARN_UNUSED_RESULT
1503 parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_,
1506 struct ofpbuf bands;
1510 ofpbuf_init(&bands, 64);
1511 string = xstrdup(str_);
1513 error = parse_ofp_meter_mod_str__(mm, string, &bands, command);
1516 ofpbuf_uninit(&bands);
1521 static char * WARN_UNUSED_RESULT
1522 parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
1523 const char *str_, char *string)
1525 static atomic_uint32_t id = ATOMIC_VAR_INIT(0);
1526 char *save_ptr = NULL;
1529 atomic_add(&id, 1, &fmr->id);
1531 fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
1532 | NXFMF_OWN | NXFMF_ACTIONS);
1533 fmr->out_port = OFPP_NONE;
1534 fmr->table_id = 0xff;
1535 match_init_catchall(&fmr->match);
1537 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1538 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1539 const struct protocol *p;
1541 if (!strcmp(name, "!initial")) {
1542 fmr->flags &= ~NXFMF_INITIAL;
1543 } else if (!strcmp(name, "!add")) {
1544 fmr->flags &= ~NXFMF_ADD;
1545 } else if (!strcmp(name, "!delete")) {
1546 fmr->flags &= ~NXFMF_DELETE;
1547 } else if (!strcmp(name, "!modify")) {
1548 fmr->flags &= ~NXFMF_MODIFY;
1549 } else if (!strcmp(name, "!actions")) {
1550 fmr->flags &= ~NXFMF_ACTIONS;
1551 } else if (!strcmp(name, "!own")) {
1552 fmr->flags &= ~NXFMF_OWN;
1553 } else if (parse_protocol(name, &p)) {
1554 match_set_dl_type(&fmr->match, htons(p->dl_type));
1556 match_set_nw_proto(&fmr->match, p->nw_proto);
1561 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1563 return xasprintf("%s: field %s missing value", str_, name);
1566 if (!strcmp(name, "table")) {
1567 char *error = str_to_u8(value, "table", &fmr->table_id);
1571 } else if (!strcmp(name, "out_port")) {
1572 fmr->out_port = u16_to_ofp(atoi(value));
1573 } else if (mf_from_name(name)) {
1576 error = parse_field(mf_from_name(name), value, &fmr->match);
1581 return xasprintf("%s: unknown keyword %s", str_, name);
1588 /* Convert 'str_' (as described in the documentation for the "monitor" command
1589 * in the ovs-ofctl man page) into 'fmr'.
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_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
1597 char *string = xstrdup(str_);
1598 char *error = parse_flow_monitor_request__(fmr, str_, string);
1603 /* Parses 's' as a set of OpenFlow actions and appends the actions to
1606 * Returns NULL if successful, otherwise a malloc()'d string describing the
1607 * error. The caller is responsible for freeing the returned string. */
1608 char * WARN_UNUSED_RESULT
1609 parse_ofpacts(const char *s_, struct ofpbuf *ofpacts)
1611 char *s = xstrdup(s_);
1612 char *error = str_to_ofpacts(s, ofpacts);
1618 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
1619 * (one of OFPFC_*) into 'fm'.
1621 * Returns NULL if successful, otherwise a malloc()'d string describing the
1622 * error. The caller is responsible for freeing the returned string. */
1623 char * WARN_UNUSED_RESULT
1624 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
1627 char *error = parse_ofp_str(fm, command, string);
1629 /* Normalize a copy of the match. This ensures that non-normalized
1630 * flows get logged but doesn't affect what gets sent to the switch, so
1631 * that the switch can do whatever it likes with the flow. */
1632 struct match match_copy = fm->match;
1633 ofputil_normalize_match(&match_copy);
1639 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
1640 * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated
1641 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
1643 * Returns NULL if successful, otherwise a malloc()'d string describing the
1644 * error. The caller is responsible for freeing the returned string. */
1645 char * WARN_UNUSED_RESULT
1646 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
1647 struct ofputil_flow_mod **fms, size_t *n_fms)
1649 size_t allocated_fms;
1657 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1658 if (stream == NULL) {
1659 return xasprintf("%s: open failed (%s)",
1660 file_name, ovs_strerror(errno));
1663 allocated_fms = *n_fms;
1666 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
1669 if (*n_fms >= allocated_fms) {
1670 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
1672 error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command);
1676 for (i = 0; i < *n_fms; i++) {
1677 free((*fms)[i].ofpacts);
1684 if (stream != stdin) {
1688 return xasprintf("%s:%d: %s", file_name, line_number, error);
1694 if (stream != stdin) {
1700 char * WARN_UNUSED_RESULT
1701 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
1702 bool aggregate, const char *string)
1704 struct ofputil_flow_mod fm;
1707 error = parse_ofp_str(&fm, -1, string);
1712 fsr->aggregate = aggregate;
1713 fsr->cookie = fm.cookie;
1714 fsr->cookie_mask = fm.cookie_mask;
1715 fsr->match = fm.match;
1716 fsr->out_port = fm.out_port;
1717 fsr->table_id = fm.table_id;
1721 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
1722 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1723 * mf_field. Fields must be specified in a natural order for satisfying
1726 * Returns NULL on success, otherwise a malloc()'d string that explains the
1729 parse_ofp_exact_flow(struct flow *flow, const char *s)
1731 char *pos, *key, *value_s;
1735 memset(flow, 0, sizeof *flow);
1737 pos = copy = xstrdup(s);
1738 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
1739 const struct protocol *p;
1740 if (parse_protocol(key, &p)) {
1741 if (flow->dl_type) {
1742 error = xasprintf("%s: Ethernet type set multiple times", s);
1745 flow->dl_type = htons(p->dl_type);
1748 if (flow->nw_proto) {
1749 error = xasprintf("%s: network protocol set "
1750 "multiple times", s);
1753 flow->nw_proto = p->nw_proto;
1756 const struct mf_field *mf;
1757 union mf_value value;
1760 mf = mf_from_name(key);
1762 error = xasprintf("%s: unknown field %s", s, key);
1766 if (!mf_are_prereqs_ok(mf, flow)) {
1767 error = xasprintf("%s: prerequisites not met for setting %s",
1772 if (!mf_is_zero(mf, flow)) {
1773 error = xasprintf("%s: field %s set multiple times", s, key);
1777 field_error = mf_parse_value(mf, value_s, &value);
1779 error = xasprintf("%s: bad value for %s (%s)",
1780 s, key, field_error);
1785 mf_set_flow_value(mf, &value, flow);
1789 if (!flow->in_port.ofp_port) {
1790 flow->in_port.ofp_port = OFPP_NONE;
1797 memset(flow, 0, sizeof *flow);