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"
42 /* Parses 'str' as an 8-bit unsigned integer into '*valuep'.
44 * 'name' describes the value parsed in an error message, if any.
46 * Returns NULL if successful, otherwise a malloc()'d string describing the
47 * error. The caller is responsible for freeing the returned string. */
48 static char * WARN_UNUSED_RESULT
49 str_to_u8(const char *str, const char *name, uint8_t *valuep)
53 if (!str_to_int(str, 0, &value) || value < 0 || value > 255) {
54 return xasprintf("invalid %s \"%s\"", name, str);
60 /* Parses 'str' as a 16-bit unsigned integer into '*valuep'.
62 * 'name' describes the value parsed in an error message, if any.
64 * Returns NULL if successful, otherwise a malloc()'d string describing the
65 * error. The caller is responsible for freeing the returned string. */
66 static char * WARN_UNUSED_RESULT
67 str_to_u16(const char *str, const char *name, uint16_t *valuep)
71 if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) {
72 return xasprintf("invalid %s \"%s\"", name, str);
78 /* Parses 'str' as a 32-bit unsigned integer into '*valuep'.
80 * Returns NULL if successful, otherwise a malloc()'d string describing the
81 * error. The caller is responsible for freeing the returned string. */
82 static char * WARN_UNUSED_RESULT
83 str_to_u32(const char *str, uint32_t *valuep)
89 return xstrdup("missing required numeric argument");
93 value = strtoul(str, &tail, 0);
94 if (errno == EINVAL || errno == ERANGE || *tail) {
95 return xasprintf("invalid numeric format %s", str);
101 /* Parses 'str' as an 64-bit unsigned integer into '*valuep'.
103 * Returns NULL if successful, otherwise a malloc()'d string describing the
104 * error. The caller is responsible for freeing the returned string. */
105 static char * WARN_UNUSED_RESULT
106 str_to_u64(const char *str, uint64_t *valuep)
112 return xstrdup("missing required numeric argument");
116 value = strtoull(str, &tail, 0);
117 if (errno == EINVAL || errno == ERANGE || *tail) {
118 return xasprintf("invalid numeric format %s", str);
124 /* Parses 'str' as an 64-bit unsigned integer in network byte order into
127 * Returns NULL if successful, otherwise a malloc()'d string describing the
128 * error. The caller is responsible for freeing the returned string. */
129 static char * WARN_UNUSED_RESULT
130 str_to_be64(const char *str, ovs_be64 *valuep)
135 error = str_to_u64(str, &value);
137 *valuep = htonll(value);
142 /* Parses 'str' as an Ethernet address into 'mac'.
144 * Returns NULL if successful, otherwise a malloc()'d string describing the
145 * error. The caller is responsible for freeing the returned string. */
146 static char * WARN_UNUSED_RESULT
147 str_to_mac(const char *str, uint8_t mac[6])
149 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
150 != ETH_ADDR_SCAN_COUNT) {
151 return xasprintf("invalid mac address %s", str);
156 /* Parses 'str' as an IP address into '*ip'.
158 * Returns NULL if successful, otherwise a malloc()'d string describing the
159 * error. The caller is responsible for freeing the returned string. */
160 static char * WARN_UNUSED_RESULT
161 str_to_ip(const char *str, ovs_be32 *ip)
163 struct in_addr in_addr;
165 if (lookup_ip(str, &in_addr)) {
166 return xasprintf("%s: could not convert to IP address", str);
168 *ip = in_addr.s_addr;
172 /* Parses 'arg' as the argument to an "enqueue" action, and appends such an
173 * action to 'ofpacts'.
175 * Returns NULL if successful, otherwise a malloc()'d string describing the
176 * error. The caller is responsible for freeing the returned string. */
177 static char * WARN_UNUSED_RESULT
178 parse_enqueue(char *arg, struct ofpbuf *ofpacts)
181 char *port = strtok_r(arg, ":q", &sp);
182 char *queue = strtok_r(NULL, "", &sp);
183 struct ofpact_enqueue *enqueue;
185 if (port == NULL || queue == NULL) {
186 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
189 enqueue = ofpact_put_ENQUEUE(ofpacts);
190 if (!ofputil_port_from_string(port, &enqueue->port)) {
191 return xasprintf("%s: enqueue to unknown port", port);
193 return str_to_u32(queue, &enqueue->queue);
196 /* Parses 'arg' as the argument to an "output" action, and appends such an
197 * action to 'ofpacts'.
199 * Returns NULL if successful, otherwise a malloc()'d string describing the
200 * error. The caller is responsible for freeing the returned string. */
201 static char * WARN_UNUSED_RESULT
202 parse_output(const char *arg, struct ofpbuf *ofpacts)
204 if (strchr(arg, '[')) {
205 struct ofpact_output_reg *output_reg;
207 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
208 output_reg->max_len = UINT16_MAX;
209 return mf_parse_subfield(&output_reg->src, arg);
211 struct ofpact_output *output;
213 output = ofpact_put_OUTPUT(ofpacts);
214 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
215 if (!ofputil_port_from_string(arg, &output->port)) {
216 return xasprintf("%s: output to unknown port", arg);
222 /* Parses 'arg' as the argument to an "resubmit" action, and appends such an
223 * action to 'ofpacts'.
225 * Returns NULL if successful, otherwise a malloc()'d string describing the
226 * error. The caller is responsible for freeing the returned string. */
227 static char * WARN_UNUSED_RESULT
228 parse_resubmit(char *arg, struct ofpbuf *ofpacts)
230 struct ofpact_resubmit *resubmit;
231 char *in_port_s, *table_s;
233 resubmit = ofpact_put_RESUBMIT(ofpacts);
235 in_port_s = strsep(&arg, ",");
236 if (in_port_s && in_port_s[0]) {
237 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
238 return xasprintf("%s: resubmit to unknown port", in_port_s);
241 resubmit->in_port = OFPP_IN_PORT;
244 table_s = strsep(&arg, ",");
245 if (table_s && table_s[0]) {
246 uint32_t table_id = 0;
249 error = str_to_u32(table_s, &table_id);
253 resubmit->table_id = table_id;
255 resubmit->table_id = 255;
258 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
259 return xstrdup("at least one \"in_port\" or \"table\" must be "
260 "specified on resubmit");
265 /* Parses 'arg' as the argument to a "note" action, and appends such an action
268 * Returns NULL if successful, otherwise a malloc()'d string describing the
269 * error. The caller is responsible for freeing the returned string. */
270 static char * WARN_UNUSED_RESULT
271 parse_note(const char *arg, struct ofpbuf *ofpacts)
273 struct ofpact_note *note;
275 note = ofpact_put_NOTE(ofpacts);
276 while (*arg != '\0') {
287 byte = hexits_value(arg, 2, &ok);
289 return xstrdup("bad hex digit in `note' argument");
291 ofpbuf_put(ofpacts, &byte, 1);
298 ofpact_update_len(ofpacts, ¬e->ofpact);
302 /* Parses 'arg' as the argument to a "fin_timeout" action, and appends such an
303 * action to 'ofpacts'.
305 * Returns NULL if successful, otherwise a malloc()'d string describing the
306 * error. The caller is responsible for freeing the returned string. */
307 static char * WARN_UNUSED_RESULT
308 parse_fin_timeout(struct ofpbuf *b, char *arg)
310 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(b);
313 while (ofputil_parse_key_value(&arg, &key, &value)) {
316 if (!strcmp(key, "idle_timeout")) {
317 error = str_to_u16(value, key, &oft->fin_idle_timeout);
318 } else if (!strcmp(key, "hard_timeout")) {
319 error = str_to_u16(value, key, &oft->fin_hard_timeout);
321 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
332 /* Parses 'arg' as the argument to a "controller" action, and appends such an
333 * action to 'ofpacts'.
335 * Returns NULL if successful, otherwise a malloc()'d string describing the
336 * error. The caller is responsible for freeing the returned string. */
337 static char * WARN_UNUSED_RESULT
338 parse_controller(struct ofpbuf *b, char *arg)
340 enum ofp_packet_in_reason reason = OFPR_ACTION;
341 uint16_t controller_id = 0;
342 uint16_t max_len = UINT16_MAX;
346 } else if (strspn(arg, "0123456789") == strlen(arg)) {
347 char *error = str_to_u16(arg, "max_len", &max_len);
354 while (ofputil_parse_key_value(&arg, &name, &value)) {
355 if (!strcmp(name, "reason")) {
356 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
357 return xasprintf("unknown reason \"%s\"", value);
359 } else if (!strcmp(name, "max_len")) {
360 char *error = str_to_u16(value, "max_len", &max_len);
364 } else if (!strcmp(name, "id")) {
365 char *error = str_to_u16(value, "id", &controller_id);
370 return xasprintf("unknown key \"%s\" parsing controller "
376 if (reason == OFPR_ACTION && controller_id == 0) {
377 struct ofpact_output *output;
379 output = ofpact_put_OUTPUT(b);
380 output->port = OFPP_CONTROLLER;
381 output->max_len = max_len;
383 struct ofpact_controller *controller;
385 controller = ofpact_put_CONTROLLER(b);
386 controller->max_len = max_len;
387 controller->reason = reason;
388 controller->controller_id = controller_id;
395 parse_noargs_dec_ttl(struct ofpbuf *b)
397 struct ofpact_cnt_ids *ids;
400 ids = ofpact_put_DEC_TTL(b);
401 ofpbuf_put(b, &id, sizeof id);
403 ids->n_controllers++;
404 ofpact_update_len(b, &ids->ofpact);
407 /* Parses 'arg' as the argument to a "dec_ttl" action, and appends such an
408 * action to 'ofpacts'.
410 * Returns NULL if successful, otherwise a malloc()'d string describing the
411 * error. The caller is responsible for freeing the returned string. */
412 static char * WARN_UNUSED_RESULT
413 parse_dec_ttl(struct ofpbuf *b, char *arg)
416 parse_noargs_dec_ttl(b);
418 struct ofpact_cnt_ids *ids;
421 ids = ofpact_put_DEC_TTL(b);
422 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
423 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
424 cntr = strtok_r(NULL, ", ", &arg)) {
425 uint16_t id = atoi(cntr);
427 ofpbuf_put(b, &id, sizeof id);
429 ids->n_controllers++;
431 if (!ids->n_controllers) {
432 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
435 ofpact_update_len(b, &ids->ofpact);
440 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
441 * action to 'ofpacts'.
443 * Returns NULL if successful, otherwise a malloc()'d string describing the
444 * error. The caller is responsible for freeing the returned string. */
445 static char * WARN_UNUSED_RESULT
446 parse_set_mpls_ttl(struct ofpbuf *b, const char *arg)
448 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(b);
451 return xstrdup("parse_set_mpls_ttl: expected ttl.");
454 mpls_ttl->ttl = atoi(arg);
458 /* Parses a "set_field" action with argument 'arg', appending the parsed
459 * action to 'ofpacts'.
461 * Returns NULL if successful, otherwise a malloc()'d string describing the
462 * error. The caller is responsible for freeing the returned string. */
463 static char * WARN_UNUSED_RESULT
464 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
465 enum ofputil_protocol *usable_protocols)
467 struct ofpact_reg_load *load = ofpact_put_REG_LOAD(ofpacts);
471 const struct mf_field *mf;
473 union mf_value mf_value;
476 delim = strstr(arg, "->");
478 return xasprintf("%s: missing `->'", arg);
480 if (strlen(delim) <= strlen("->")) {
481 return xasprintf("%s: missing field name following `->'", arg);
484 key = delim + strlen("->");
485 mf = mf_from_name(key);
487 return xasprintf("%s is not a valid OXM field name", key);
490 return xasprintf("%s is read-only", key);
494 error = mf_parse_value(mf, value, &mf_value);
498 if (!mf_is_value_valid(mf, &mf_value)) {
499 return xasprintf("%s is not a valid value for field %s", value, key);
501 ofpact_set_field_init(load, mf, &mf_value);
503 *usable_protocols &= mf->usable_protocols;
507 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
508 * action to 'ofpacts'.
510 * Returns NULL if successful, otherwise a malloc()'d string describing the
511 * error. The caller is responsible for freeing the returned string. */
512 static char * WARN_UNUSED_RESULT
513 set_field_parse(const char *arg, struct ofpbuf *ofpacts,
514 enum ofputil_protocol *usable_protocols)
516 char *copy = xstrdup(arg);
517 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
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 = OVS_BE64_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,
597 enum ofputil_protocol *usable_protocols)
599 size_t orig_size = ofpacts->size;
600 struct ofpact_tunnel *tunnel;
602 uint16_t ethertype = 0;
608 case OFPUTIL_ACTION_INVALID:
611 case OFPUTIL_OFPAT10_OUTPUT:
612 case OFPUTIL_OFPAT11_OUTPUT:
613 error = parse_output(arg, ofpacts);
616 case OFPUTIL_OFPAT10_SET_VLAN_VID:
617 case OFPUTIL_OFPAT11_SET_VLAN_VID:
618 error = str_to_u16(arg, "VLAN VID", &vid);
623 if (vid & ~VLAN_VID_MASK) {
624 return xasprintf("%s: not a valid VLAN VID", arg);
626 ofpact_put_SET_VLAN_VID(ofpacts)->vlan_vid = vid;
629 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
630 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
631 error = str_to_u8(arg, "VLAN PCP", &pcp);
637 return xasprintf("%s: not a valid VLAN PCP", arg);
639 ofpact_put_SET_VLAN_PCP(ofpacts)->vlan_pcp = pcp;
642 case OFPUTIL_OFPAT12_SET_FIELD:
643 return set_field_parse(arg, ofpacts, usable_protocols);
645 case OFPUTIL_OFPAT10_STRIP_VLAN:
646 case OFPUTIL_OFPAT11_POP_VLAN:
647 ofpact_put_STRIP_VLAN(ofpacts);
650 case OFPUTIL_OFPAT11_PUSH_VLAN:
651 *usable_protocols &= OFPUTIL_P_OF11_UP;
652 error = str_to_u16(arg, "ethertype", ðertype);
657 if (ethertype != ETH_TYPE_VLAN_8021Q) {
658 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
659 return xasprintf("%s: not a valid VLAN ethertype", arg);
662 ofpact_put_PUSH_VLAN(ofpacts);
665 case OFPUTIL_OFPAT11_SET_QUEUE:
666 error = str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
669 case OFPUTIL_OFPAT10_SET_DL_SRC:
670 case OFPUTIL_OFPAT11_SET_DL_SRC:
671 error = str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
674 case OFPUTIL_OFPAT10_SET_DL_DST:
675 case OFPUTIL_OFPAT11_SET_DL_DST:
676 error = str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
679 case OFPUTIL_OFPAT10_SET_NW_SRC:
680 case OFPUTIL_OFPAT11_SET_NW_SRC:
681 error = str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
684 case OFPUTIL_OFPAT10_SET_NW_DST:
685 case OFPUTIL_OFPAT11_SET_NW_DST:
686 error = str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
689 case OFPUTIL_OFPAT10_SET_NW_TOS:
690 case OFPUTIL_OFPAT11_SET_NW_TOS:
691 error = str_to_u8(arg, "TOS", &tos);
696 if (tos & ~IP_DSCP_MASK) {
697 return xasprintf("%s: not a valid TOS", arg);
699 ofpact_put_SET_IPV4_DSCP(ofpacts)->dscp = tos;
702 case OFPUTIL_OFPAT11_DEC_NW_TTL:
705 case OFPUTIL_OFPAT10_SET_TP_SRC:
706 case OFPUTIL_OFPAT11_SET_TP_SRC:
707 error = str_to_u16(arg, "source port",
708 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
711 case OFPUTIL_OFPAT10_SET_TP_DST:
712 case OFPUTIL_OFPAT11_SET_TP_DST:
713 error = str_to_u16(arg, "destination port",
714 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
717 case OFPUTIL_OFPAT10_ENQUEUE:
718 error = parse_enqueue(arg, ofpacts);
721 case OFPUTIL_NXAST_RESUBMIT:
722 error = parse_resubmit(arg, ofpacts);
725 case OFPUTIL_NXAST_SET_TUNNEL:
726 case OFPUTIL_NXAST_SET_TUNNEL64:
727 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
728 tunnel->ofpact.compat = code;
729 error = str_to_u64(arg, &tunnel->tun_id);
732 case OFPUTIL_NXAST_WRITE_METADATA:
733 error = parse_metadata(ofpacts, arg);
736 case OFPUTIL_NXAST_SET_QUEUE:
737 error = str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
740 case OFPUTIL_NXAST_POP_QUEUE:
741 ofpact_put_POP_QUEUE(ofpacts);
744 case OFPUTIL_NXAST_REG_MOVE:
745 error = nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts), arg);
748 case OFPUTIL_NXAST_REG_LOAD:
749 error = nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts), arg);
752 case OFPUTIL_NXAST_NOTE:
753 error = parse_note(arg, ofpacts);
756 case OFPUTIL_NXAST_MULTIPATH:
757 error = multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
760 case OFPUTIL_NXAST_BUNDLE:
761 error = bundle_parse(arg, ofpacts);
764 case OFPUTIL_NXAST_BUNDLE_LOAD:
765 error = bundle_parse_load(arg, ofpacts);
768 case OFPUTIL_NXAST_RESUBMIT_TABLE:
769 case OFPUTIL_NXAST_OUTPUT_REG:
770 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
773 case OFPUTIL_NXAST_LEARN:
774 error = learn_parse(arg, ofpacts);
777 case OFPUTIL_NXAST_EXIT:
778 ofpact_put_EXIT(ofpacts);
781 case OFPUTIL_NXAST_DEC_TTL:
782 error = parse_dec_ttl(ofpacts, arg);
785 case OFPUTIL_NXAST_SET_MPLS_TTL:
786 case OFPUTIL_OFPAT11_SET_MPLS_TTL:
787 error = parse_set_mpls_ttl(ofpacts, arg);
790 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
791 case OFPUTIL_NXAST_DEC_MPLS_TTL:
792 ofpact_put_DEC_MPLS_TTL(ofpacts);
795 case OFPUTIL_NXAST_FIN_TIMEOUT:
796 error = parse_fin_timeout(ofpacts, arg);
799 case OFPUTIL_NXAST_CONTROLLER:
800 error = parse_controller(ofpacts, arg);
803 case OFPUTIL_OFPAT11_PUSH_MPLS:
804 case OFPUTIL_NXAST_PUSH_MPLS:
805 error = str_to_u16(arg, "push_mpls", ðertype);
807 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
811 case OFPUTIL_OFPAT11_POP_MPLS:
812 case OFPUTIL_NXAST_POP_MPLS:
813 error = str_to_u16(arg, "pop_mpls", ðertype);
815 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
819 case OFPUTIL_OFPAT11_GROUP:
820 error = str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
823 case OFPUTIL_NXAST_STACK_PUSH:
824 error = nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
826 case OFPUTIL_NXAST_STACK_POP:
827 error = nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
830 case OFPUTIL_NXAST_SAMPLE:
831 error = parse_sample(ofpacts, arg);
836 ofpacts->size = orig_size;
841 /* Parses action 'act', with argument 'arg', and appends a parsed version to
844 * 'n_actions' specifies the number of actions already parsed (for proper
845 * handling of "drop" actions).
847 * Returns NULL if successful, otherwise a malloc()'d string describing the
848 * error. The caller is responsible for freeing the returned string. */
849 static char * WARN_UNUSED_RESULT
850 str_to_ofpact__(char *pos, char *act, char *arg,
851 struct ofpbuf *ofpacts, int n_actions,
852 enum ofputil_protocol *usable_protocols)
854 int code = ofputil_action_code_from_name(act);
856 return parse_named_action(code, arg, ofpacts, usable_protocols);
857 } else if (!strcasecmp(act, "drop")) {
859 return xstrdup("Drop actions must not be preceded by other "
861 } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
862 return xstrdup("Drop actions must not be followed by other "
867 if (ofputil_port_from_string(act, &port)) {
868 ofpact_put_OUTPUT(ofpacts)->port = port;
870 return xasprintf("Unknown action: %s", act);
877 /* Parses 'str' as a series of actions, and appends them to 'ofpacts'.
879 * Returns NULL if successful, otherwise a malloc()'d string describing the
880 * error. The caller is responsible for freeing the returned string. */
881 static char * WARN_UNUSED_RESULT
882 str_to_ofpacts__(char *str, struct ofpbuf *ofpacts,
883 enum ofputil_protocol *usable_protocols)
885 size_t orig_size = ofpacts->size;
886 char *pos, *act, *arg;
891 while (ofputil_parse_key_value(&pos, &act, &arg)) {
892 char *error = str_to_ofpact__(pos, act, arg, ofpacts, n_actions,
895 ofpacts->size = orig_size;
906 /* Parses 'str' as a series of actions, and appends them to 'ofpacts'.
908 * Returns NULL if successful, otherwise a malloc()'d string describing the
909 * error. The caller is responsible for freeing the returned string. */
910 static char * WARN_UNUSED_RESULT
911 str_to_ofpacts(char *str, struct ofpbuf *ofpacts,
912 enum ofputil_protocol *usable_protocols)
914 size_t orig_size = ofpacts->size;
918 error_s = str_to_ofpacts__(str, ofpacts, usable_protocols);
923 error = ofpacts_verify(ofpacts->data, ofpacts->size);
925 ofpacts->size = orig_size;
926 return xstrdup("Incorrect action ordering");
932 /* Parses 'arg' as the argument to instruction 'type', and appends such an
933 * instruction to 'ofpacts'.
935 * Returns NULL if successful, otherwise a malloc()'d string describing the
936 * error. The caller is responsible for freeing the returned string. */
937 static char * WARN_UNUSED_RESULT
938 parse_named_instruction(enum ovs_instruction_type type,
939 char *arg, struct ofpbuf *ofpacts,
940 enum ofputil_protocol *usable_protocols)
942 char *error_s = NULL;
945 *usable_protocols &= OFPUTIL_P_OF11_UP;
948 case OVSINST_OFPIT11_APPLY_ACTIONS:
949 NOT_REACHED(); /* This case is handled by str_to_inst_ofpacts() */
952 case OVSINST_OFPIT11_WRITE_ACTIONS: {
953 struct ofpact_nest *on;
958 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
959 offsetof(struct ofpact_nest, actions));
960 error_s = str_to_ofpacts__(arg, ofpacts, usable_protocols);
962 on = ofpbuf_at_assert(ofpacts, ofs, sizeof *on);
963 on->ofpact.len = ofpacts->size - ofs;
971 case OVSINST_OFPIT11_CLEAR_ACTIONS:
972 ofpact_put_CLEAR_ACTIONS(ofpacts);
975 case OVSINST_OFPIT13_METER:
976 *usable_protocols &= OFPUTIL_P_OF13_UP;
977 error_s = str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
980 case OVSINST_OFPIT11_WRITE_METADATA:
981 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
982 error_s = parse_metadata(ofpacts, arg);
985 case OVSINST_OFPIT11_GOTO_TABLE: {
986 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
987 char *table_s = strsep(&arg, ",");
988 if (!table_s || !table_s[0]) {
989 return xstrdup("instruction goto-table needs table id");
991 error_s = str_to_u8(table_s, "table", &ogt->table_id);
1000 /* If write_metadata is specified as an action AND an instruction, ofpacts
1001 could be invalid. */
1002 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1004 return xstrdup("Incorrect instruction ordering");
1009 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
1011 * Returns NULL if successful, otherwise a malloc()'d string describing the
1012 * error. The caller is responsible for freeing the returned string. */
1013 static char * WARN_UNUSED_RESULT
1014 str_to_inst_ofpacts(char *str, struct ofpbuf *ofpacts,
1015 enum ofputil_protocol *usable_protocols)
1017 size_t orig_size = ofpacts->size;
1018 char *pos, *inst, *arg;
1020 const char *prev_inst = NULL;
1025 while (ofputil_parse_key_value(&pos, &inst, &arg)) {
1026 type = ovs_instruction_type_from_name(inst);
1028 char *error = str_to_ofpact__(pos, inst, arg, ofpacts, n_actions,
1031 ofpacts->size = orig_size;
1035 type = OVSINST_OFPIT11_APPLY_ACTIONS;
1036 if (prev_type == type) {
1040 } else if (type == OVSINST_OFPIT11_APPLY_ACTIONS) {
1041 ofpacts->size = orig_size;
1042 return xasprintf("%s isn't supported. Just write actions then "
1043 "it is interpreted as apply_actions", inst);
1045 char *error = parse_named_instruction(type, arg, ofpacts,
1048 ofpacts->size = orig_size;
1053 if (type <= prev_type) {
1054 ofpacts->size = orig_size;
1055 if (type == prev_type) {
1056 return xasprintf("instruction %s may be specified only once",
1059 return xasprintf("instruction %s must be specified before %s",
1068 ofpact_pad(ofpacts);
1080 parse_protocol(const char *name, const struct protocol **p_out)
1082 static const struct protocol protocols[] = {
1083 { "ip", ETH_TYPE_IP, 0 },
1084 { "arp", ETH_TYPE_ARP, 0 },
1085 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
1086 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
1087 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
1088 { "sctp", ETH_TYPE_IP, IPPROTO_SCTP },
1089 { "ipv6", ETH_TYPE_IPV6, 0 },
1090 { "ip6", ETH_TYPE_IPV6, 0 },
1091 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
1092 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
1093 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
1094 { "sctp6", ETH_TYPE_IPV6, IPPROTO_SCTP },
1095 { "rarp", ETH_TYPE_RARP, 0},
1096 { "mpls", ETH_TYPE_MPLS, 0 },
1097 { "mplsm", ETH_TYPE_MPLS_MCAST, 0 },
1099 const struct protocol *p;
1101 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
1102 if (!strcmp(p->name, name)) {
1111 /* Parses 's' as the (possibly masked) value of field 'mf', and updates
1112 * 'match' appropriately. Restricts the set of usable protocols to ones
1113 * supporting the parsed field.
1115 * Returns NULL if successful, otherwise a malloc()'d string describing the
1116 * error. The caller is responsible for freeing the returned string. */
1117 static char * WARN_UNUSED_RESULT
1118 parse_field(const struct mf_field *mf, const char *s, struct match *match,
1119 enum ofputil_protocol *usable_protocols)
1121 union mf_value value, mask;
1124 error = mf_parse(mf, s, &value, &mask);
1126 *usable_protocols &= mf_set(mf, &value, &mask, match);
1131 static char * WARN_UNUSED_RESULT
1132 parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string,
1133 enum ofputil_protocol *usable_protocols)
1136 F_OUT_PORT = 1 << 0,
1139 F_PRIORITY = 1 << 4,
1142 char *save_ptr = NULL;
1143 char *act_str = NULL;
1146 *usable_protocols = OFPUTIL_P_ANY;
1150 fields = F_OUT_PORT;
1154 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
1158 fields = F_OUT_PORT;
1161 case OFPFC_DELETE_STRICT:
1162 fields = F_OUT_PORT | F_PRIORITY;
1166 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
1169 case OFPFC_MODIFY_STRICT:
1170 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
1177 match_init_catchall(&fm->match);
1178 fm->priority = OFP_DEFAULT_PRIORITY;
1179 fm->cookie = htonll(0);
1180 fm->cookie_mask = htonll(0);
1181 if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
1182 /* For modify, by default, don't update the cookie. */
1183 fm->new_cookie = OVS_BE64_MAX;
1185 fm->new_cookie = htonll(0);
1187 fm->modify_cookie = false;
1188 fm->table_id = 0xff;
1189 fm->command = command;
1190 fm->idle_timeout = OFP_FLOW_PERMANENT;
1191 fm->hard_timeout = OFP_FLOW_PERMANENT;
1192 fm->buffer_id = UINT32_MAX;
1193 fm->out_port = OFPP_ANY;
1195 fm->out_group = OFPG11_ANY;
1196 if (fields & F_ACTIONS) {
1197 act_str = strstr(string, "action");
1199 return xstrdup("must specify an action");
1203 act_str = strchr(act_str + 1, '=');
1205 return xstrdup("must specify an action");
1210 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1211 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1212 const struct protocol *p;
1215 if (parse_protocol(name, &p)) {
1216 match_set_dl_type(&fm->match, htons(p->dl_type));
1218 match_set_nw_proto(&fm->match, p->nw_proto);
1220 } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
1221 fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
1222 } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
1223 fm->flags |= OFPUTIL_FF_CHECK_OVERLAP;
1224 } else if (fields & F_FLAGS && !strcmp(name, "reset_counts")) {
1225 fm->flags |= OFPUTIL_FF_RESET_COUNTS;
1226 *usable_protocols &= OFPUTIL_P_OF12_UP;
1227 } else if (fields & F_FLAGS && !strcmp(name, "no_packet_counts")) {
1228 fm->flags |= OFPUTIL_FF_NO_PKT_COUNTS;
1229 *usable_protocols &= OFPUTIL_P_OF13_UP;
1230 } else if (fields & F_FLAGS && !strcmp(name, "no_byte_counts")) {
1231 fm->flags |= OFPUTIL_FF_NO_BYT_COUNTS;
1232 *usable_protocols &= OFPUTIL_P_OF13_UP;
1236 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1238 return xasprintf("field %s missing value", name);
1241 if (!strcmp(name, "table")) {
1242 error = str_to_u8(value, "table", &fm->table_id);
1243 if (fm->table_id != 0xff) {
1244 *usable_protocols &= OFPUTIL_P_TID;
1246 } else if (!strcmp(name, "out_port")) {
1247 if (!ofputil_port_from_string(value, &fm->out_port)) {
1248 error = xasprintf("%s is not a valid OpenFlow port",
1251 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
1252 uint16_t priority = 0;
1254 error = str_to_u16(value, name, &priority);
1255 fm->priority = priority;
1256 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
1257 error = str_to_u16(value, name, &fm->idle_timeout);
1258 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
1259 error = str_to_u16(value, name, &fm->hard_timeout);
1260 } else if (!strcmp(name, "cookie")) {
1261 char *mask = strchr(value, '/');
1264 /* A mask means we're searching for a cookie. */
1265 if (command == OFPFC_ADD) {
1266 return xstrdup("flow additions cannot use "
1270 error = str_to_be64(value, &fm->cookie);
1274 error = str_to_be64(mask + 1, &fm->cookie_mask);
1276 /* Matching of the cookie is only supported through NXM or
1278 if (fm->cookie_mask != htonll(0)) {
1279 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
1282 /* No mask means that the cookie is being set. */
1283 if (command != OFPFC_ADD && command != OFPFC_MODIFY
1284 && command != OFPFC_MODIFY_STRICT) {
1285 return xstrdup("cannot set cookie");
1287 error = str_to_be64(value, &fm->new_cookie);
1288 fm->modify_cookie = true;
1290 } else if (mf_from_name(name)) {
1291 error = parse_field(mf_from_name(name), value, &fm->match,
1293 } else if (!strcmp(name, "duration")
1294 || !strcmp(name, "n_packets")
1295 || !strcmp(name, "n_bytes")
1296 || !strcmp(name, "idle_age")
1297 || !strcmp(name, "hard_age")) {
1298 /* Ignore these, so that users can feed the output of
1299 * "ovs-ofctl dump-flows" back into commands that parse
1302 error = xasprintf("unknown keyword %s", name);
1310 /* Check for usable protocol interdependencies between match fields. */
1311 if (fm->match.flow.dl_type == htons(ETH_TYPE_IPV6)) {
1312 const struct flow_wildcards *wc = &fm->match.wc;
1313 /* Only NXM and OXM support matching L3 and L4 fields within IPv6.
1315 * (IPv6 specific fields as well as arp_sha, arp_tha, nw_frag, and
1316 * nw_ttl are covered elsewhere so they don't need to be included in
1319 if (wc->masks.nw_proto || wc->masks.nw_tos
1320 || wc->masks.tp_src || wc->masks.tp_dst) {
1321 *usable_protocols &= OFPUTIL_P_NXM_OXM_ANY;
1324 if (!fm->cookie_mask && fm->new_cookie == OVS_BE64_MAX
1325 && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
1326 /* On modifies without a mask, we are supposed to add a flow if
1327 * one does not exist. If a cookie wasn't been specified, use a
1328 * default of zero. */
1329 fm->new_cookie = htonll(0);
1331 if (fields & F_ACTIONS) {
1332 struct ofpbuf ofpacts;
1335 ofpbuf_init(&ofpacts, 32);
1336 error = str_to_inst_ofpacts(act_str, &ofpacts, usable_protocols);
1340 err = ofpacts_check(ofpacts.data, ofpacts.size, &fm->match.flow,
1343 error = xasprintf("actions are invalid with specified match "
1344 "(%s)", ofperr_to_string(err));
1348 ofpbuf_uninit(&ofpacts);
1352 fm->ofpacts_len = ofpacts.size;
1353 fm->ofpacts = ofpbuf_steal_data(&ofpacts);
1355 fm->ofpacts_len = 0;
1362 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
1363 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
1364 * Returns the set of usable protocols in '*usable_protocols'.
1366 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
1367 * constant for 'command'. To parse syntax for an OFPST_FLOW or
1368 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'.
1370 * Returns NULL if successful, otherwise a malloc()'d string describing the
1371 * error. The caller is responsible for freeing the returned string. */
1372 char * WARN_UNUSED_RESULT
1373 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
1374 enum ofputil_protocol *usable_protocols)
1376 char *string = xstrdup(str_);
1379 error = parse_ofp_str__(fm, command, string, usable_protocols);
1382 fm->ofpacts_len = 0;
1389 static char * WARN_UNUSED_RESULT
1390 parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string,
1391 struct ofpbuf *bands, int command,
1392 enum ofputil_protocol *usable_protocols)
1399 char *save_ptr = NULL;
1400 char *band_str = NULL;
1403 /* Meters require at least OF 1.3. */
1404 *usable_protocols = OFPUTIL_P_OF13_UP;
1412 fields = F_METER | F_FLAGS | F_BANDS;
1415 case OFPMC13_DELETE:
1419 case OFPMC13_MODIFY:
1420 fields = F_METER | F_FLAGS | F_BANDS;
1427 mm->command = command;
1428 mm->meter.meter_id = 0;
1429 mm->meter.flags = 0;
1430 if (fields & F_BANDS) {
1431 band_str = strstr(string, "band");
1433 return xstrdup("must specify bands");
1437 band_str = strchr(band_str + 1, '=');
1439 return xstrdup("must specify bands");
1444 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1445 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1447 if (fields & F_FLAGS && !strcmp(name, "kbps")) {
1448 mm->meter.flags |= OFPMF13_KBPS;
1449 } else if (fields & F_FLAGS && !strcmp(name, "pktps")) {
1450 mm->meter.flags |= OFPMF13_PKTPS;
1451 } else if (fields & F_FLAGS && !strcmp(name, "burst")) {
1452 mm->meter.flags |= OFPMF13_BURST;
1453 } else if (fields & F_FLAGS && !strcmp(name, "stats")) {
1454 mm->meter.flags |= OFPMF13_STATS;
1458 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1460 return xasprintf("field %s missing value", name);
1463 if (!strcmp(name, "meter")) {
1464 if (!strcmp(value, "all")) {
1465 mm->meter.meter_id = OFPM13_ALL;
1466 } else if (!strcmp(value, "controller")) {
1467 mm->meter.meter_id = OFPM13_CONTROLLER;
1468 } else if (!strcmp(value, "slowpath")) {
1469 mm->meter.meter_id = OFPM13_SLOWPATH;
1471 char *error = str_to_u32(value, &mm->meter.meter_id);
1475 if (mm->meter.meter_id > OFPM13_MAX) {
1476 return xasprintf("invalid value for %s", name);
1480 return xasprintf("unknown keyword %s", name);
1484 if (fields & F_METER && !mm->meter.meter_id) {
1485 return xstrdup("must specify 'meter'");
1487 if (fields & F_FLAGS && !mm->meter.flags) {
1488 return xstrdup("meter must specify either 'kbps' or 'pktps'");
1491 if (fields & F_BANDS) {
1492 uint16_t n_bands = 0;
1493 struct ofputil_meter_band *band = NULL;
1496 for (name = strtok_r(band_str, "=, \t\r\n", &save_ptr); name;
1497 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1501 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1503 return xasprintf("field %s missing value", name);
1506 if (!strcmp(name, "type")) {
1507 /* Start a new band */
1508 band = ofpbuf_put_zeros(bands, sizeof *band);
1511 if (!strcmp(value, "drop")) {
1512 band->type = OFPMBT13_DROP;
1513 } else if (!strcmp(value, "dscp_remark")) {
1514 band->type = OFPMBT13_DSCP_REMARK;
1516 return xasprintf("field %s unknown value %s", name, value);
1518 } else if (!band || !band->type) {
1519 return xstrdup("band must start with the 'type' keyword");
1520 } else if (!strcmp(name, "rate")) {
1521 char *error = str_to_u32(value, &band->rate);
1525 } else if (!strcmp(name, "burst_size")) {
1526 char *error = str_to_u32(value, &band->burst_size);
1530 } else if (!strcmp(name, "prec_level")) {
1531 char *error = str_to_u8(value, name, &band->prec_level);
1536 return xasprintf("unknown keyword %s", name);
1539 /* validate bands */
1541 return xstrdup("meter must have bands");
1544 mm->meter.n_bands = n_bands;
1545 mm->meter.bands = ofpbuf_steal_data(bands);
1547 for (i = 0; i < n_bands; ++i) {
1548 band = &mm->meter.bands[i];
1551 return xstrdup("band must have 'type'");
1553 if (band->type == OFPMBT13_DSCP_REMARK) {
1554 if (!band->prec_level) {
1555 return xstrdup("'dscp_remark' band must have"
1559 if (band->prec_level) {
1560 return xstrdup("Only 'dscp_remark' band may have"
1565 return xstrdup("band must have 'rate'");
1567 if (mm->meter.flags & OFPMF13_BURST) {
1568 if (!band->burst_size) {
1569 return xstrdup("band must have 'burst_size' "
1570 "when 'burst' flag is set");
1573 if (band->burst_size) {
1574 return xstrdup("band may have 'burst_size' only "
1575 "when 'burst' flag is set");
1580 mm->meter.n_bands = 0;
1581 mm->meter.bands = NULL;
1587 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
1588 * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
1590 * Returns NULL if successful, otherwise a malloc()'d string describing the
1591 * error. The caller is responsible for freeing the returned string. */
1592 char * WARN_UNUSED_RESULT
1593 parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_,
1594 int command, enum ofputil_protocol *usable_protocols)
1596 struct ofpbuf bands;
1600 ofpbuf_init(&bands, 64);
1601 string = xstrdup(str_);
1603 error = parse_ofp_meter_mod_str__(mm, string, &bands, command,
1607 ofpbuf_uninit(&bands);
1612 static char * WARN_UNUSED_RESULT
1613 parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
1614 const char *str_, char *string,
1615 enum ofputil_protocol *usable_protocols)
1617 static atomic_uint32_t id = ATOMIC_VAR_INIT(0);
1618 char *save_ptr = NULL;
1621 atomic_add(&id, 1, &fmr->id);
1623 fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
1624 | NXFMF_OWN | NXFMF_ACTIONS);
1625 fmr->out_port = OFPP_NONE;
1626 fmr->table_id = 0xff;
1627 match_init_catchall(&fmr->match);
1629 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1630 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1631 const struct protocol *p;
1633 if (!strcmp(name, "!initial")) {
1634 fmr->flags &= ~NXFMF_INITIAL;
1635 } else if (!strcmp(name, "!add")) {
1636 fmr->flags &= ~NXFMF_ADD;
1637 } else if (!strcmp(name, "!delete")) {
1638 fmr->flags &= ~NXFMF_DELETE;
1639 } else if (!strcmp(name, "!modify")) {
1640 fmr->flags &= ~NXFMF_MODIFY;
1641 } else if (!strcmp(name, "!actions")) {
1642 fmr->flags &= ~NXFMF_ACTIONS;
1643 } else if (!strcmp(name, "!own")) {
1644 fmr->flags &= ~NXFMF_OWN;
1645 } else if (parse_protocol(name, &p)) {
1646 match_set_dl_type(&fmr->match, htons(p->dl_type));
1648 match_set_nw_proto(&fmr->match, p->nw_proto);
1653 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1655 return xasprintf("%s: field %s missing value", str_, name);
1658 if (!strcmp(name, "table")) {
1659 char *error = str_to_u8(value, "table", &fmr->table_id);
1663 } else if (!strcmp(name, "out_port")) {
1664 fmr->out_port = u16_to_ofp(atoi(value));
1665 } else if (mf_from_name(name)) {
1668 error = parse_field(mf_from_name(name), value, &fmr->match,
1674 return xasprintf("%s: unknown keyword %s", str_, name);
1681 /* Convert 'str_' (as described in the documentation for the "monitor" command
1682 * in the ovs-ofctl man page) into 'fmr'.
1684 * Returns NULL if successful, otherwise a malloc()'d string describing the
1685 * error. The caller is responsible for freeing the returned string. */
1686 char * WARN_UNUSED_RESULT
1687 parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
1689 enum ofputil_protocol *usable_protocols)
1691 char *string = xstrdup(str_);
1692 char *error = parse_flow_monitor_request__(fmr, str_, string,
1698 /* Parses 's' as a set of OpenFlow actions and appends the actions to
1701 * Returns NULL if successful, otherwise a malloc()'d string describing the
1702 * error. The caller is responsible for freeing the returned string. */
1703 char * WARN_UNUSED_RESULT
1704 parse_ofpacts(const char *s_, struct ofpbuf *ofpacts,
1705 enum ofputil_protocol *usable_protocols)
1707 char *s = xstrdup(s_);
1710 *usable_protocols = OFPUTIL_P_ANY;
1712 error = str_to_ofpacts(s, ofpacts, usable_protocols);
1718 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
1719 * (one of OFPFC_*) into 'fm'.
1721 * Returns NULL if successful, otherwise a malloc()'d string describing the
1722 * error. The caller is responsible for freeing the returned string. */
1723 char * WARN_UNUSED_RESULT
1724 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
1726 enum ofputil_protocol *usable_protocols)
1728 char *error = parse_ofp_str(fm, command, string, usable_protocols);
1730 /* Normalize a copy of the match. This ensures that non-normalized
1731 * flows get logged but doesn't affect what gets sent to the switch, so
1732 * that the switch can do whatever it likes with the flow. */
1733 struct match match_copy = fm->match;
1734 ofputil_normalize_match(&match_copy);
1740 /* Convert 'table_id' and 'flow_miss_handling' (as described for the
1741 * "mod-table" command in the ovs-ofctl man page) into 'tm' for sending the
1742 * specified table_mod 'command' to a switch.
1744 * Returns NULL if successful, otherwise a malloc()'d string describing the
1745 * error. The caller is responsible for freeing the returned string. */
1746 char * WARN_UNUSED_RESULT
1747 parse_ofp_table_mod(struct ofputil_table_mod *tm, const char *table_id,
1748 const char *flow_miss_handling,
1749 enum ofputil_protocol *usable_protocols)
1751 /* Table mod requires at least OF 1.1. */
1752 *usable_protocols = OFPUTIL_P_OF11_UP;
1754 if (!strcasecmp(table_id, "all")) {
1757 char *error = str_to_u8(table_id, "table_id", &tm->table_id);
1763 if (strcmp(flow_miss_handling, "controller") == 0) {
1764 tm->config = OFPTC11_TABLE_MISS_CONTROLLER;
1765 } else if (strcmp(flow_miss_handling, "continue") == 0) {
1766 tm->config = OFPTC11_TABLE_MISS_CONTINUE;
1767 } else if (strcmp(flow_miss_handling, "drop") == 0) {
1768 tm->config = OFPTC11_TABLE_MISS_DROP;
1770 return xasprintf("invalid flow_miss_handling %s", flow_miss_handling);
1773 if (tm->table_id == 0xfe && tm->config == OFPTC11_TABLE_MISS_CONTINUE) {
1774 return xstrdup("last table's flow miss handling can not be continue");
1781 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
1782 * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated
1783 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
1785 * Returns NULL if successful, otherwise a malloc()'d string describing the
1786 * error. The caller is responsible for freeing the returned string. */
1787 char * WARN_UNUSED_RESULT
1788 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
1789 struct ofputil_flow_mod **fms, size_t *n_fms,
1790 enum ofputil_protocol *usable_protocols)
1792 size_t allocated_fms;
1797 *usable_protocols = OFPUTIL_P_ANY;
1802 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1803 if (stream == NULL) {
1804 return xasprintf("%s: open failed (%s)",
1805 file_name, ovs_strerror(errno));
1808 allocated_fms = *n_fms;
1811 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
1813 enum ofputil_protocol usable;
1815 if (*n_fms >= allocated_fms) {
1816 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
1818 error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command,
1823 for (i = 0; i < *n_fms; i++) {
1824 free((*fms)[i].ofpacts);
1831 if (stream != stdin) {
1835 return xasprintf("%s:%d: %s", file_name, line_number, error);
1837 *usable_protocols &= usable; /* Each line can narrow the set. */
1842 if (stream != stdin) {
1848 char * WARN_UNUSED_RESULT
1849 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
1850 bool aggregate, const char *string,
1851 enum ofputil_protocol *usable_protocols)
1853 struct ofputil_flow_mod fm;
1856 error = parse_ofp_str(&fm, -1, string, usable_protocols);
1861 /* Special table ID support not required for stats requests. */
1862 if (*usable_protocols & OFPUTIL_P_OF10_STD_TID) {
1863 *usable_protocols |= OFPUTIL_P_OF10_STD;
1865 if (*usable_protocols & OFPUTIL_P_OF10_NXM_TID) {
1866 *usable_protocols |= OFPUTIL_P_OF10_NXM;
1869 fsr->aggregate = aggregate;
1870 fsr->cookie = fm.cookie;
1871 fsr->cookie_mask = fm.cookie_mask;
1872 fsr->match = fm.match;
1873 fsr->out_port = fm.out_port;
1874 fsr->out_group = fm.out_group;
1875 fsr->table_id = fm.table_id;
1879 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
1880 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1881 * mf_field. Fields must be specified in a natural order for satisfying
1884 * Returns NULL on success, otherwise a malloc()'d string that explains the
1887 parse_ofp_exact_flow(struct flow *flow, const char *s)
1889 char *pos, *key, *value_s;
1893 memset(flow, 0, sizeof *flow);
1895 pos = copy = xstrdup(s);
1896 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
1897 const struct protocol *p;
1898 if (parse_protocol(key, &p)) {
1899 if (flow->dl_type) {
1900 error = xasprintf("%s: Ethernet type set multiple times", s);
1903 flow->dl_type = htons(p->dl_type);
1906 if (flow->nw_proto) {
1907 error = xasprintf("%s: network protocol set "
1908 "multiple times", s);
1911 flow->nw_proto = p->nw_proto;
1914 const struct mf_field *mf;
1915 union mf_value value;
1918 mf = mf_from_name(key);
1920 error = xasprintf("%s: unknown field %s", s, key);
1924 if (!mf_are_prereqs_ok(mf, flow)) {
1925 error = xasprintf("%s: prerequisites not met for setting %s",
1930 if (!mf_is_zero(mf, flow)) {
1931 error = xasprintf("%s: field %s set multiple times", s, key);
1935 field_error = mf_parse_value(mf, value_s, &value);
1937 error = xasprintf("%s: bad value for %s (%s)",
1938 s, key, field_error);
1943 mf_set_flow_value(mf, &value, flow);
1947 if (!flow->in_port.ofp_port) {
1948 flow->in_port.ofp_port = OFPP_NONE;
1955 memset(flow, 0, sizeof *flow);
1960 static char * WARN_UNUSED_RESULT
1961 parse_bucket_str(struct ofputil_bucket *bucket, char *str_,
1962 enum ofputil_protocol *usable_protocols)
1964 struct ofpbuf ofpacts;
1965 char *pos, *act, *arg;
1969 bucket->watch_port = OFPP_ANY;
1970 bucket->watch_group = OFPG11_ANY;
1974 ofpbuf_init(&ofpacts, 64);
1975 while (ofputil_parse_key_value(&pos, &act, &arg)) {
1978 if (!strcasecmp(act, "weight")) {
1979 error = str_to_u16(arg, "weight", &bucket->weight);
1980 } else if (!strcasecmp(act, "watch_port")) {
1981 if (!ofputil_port_from_string(arg, &bucket->watch_port)
1982 || (ofp_to_u16(bucket->watch_port) >= ofp_to_u16(OFPP_MAX)
1983 && bucket->watch_port != OFPP_ANY)) {
1984 error = xasprintf("%s: invalid watch_port", arg);
1986 } else if (!strcasecmp(act, "watch_group")) {
1987 error = str_to_u32(arg, &bucket->watch_group);
1988 if (!error && bucket->watch_group > OFPG_MAX) {
1989 error = xasprintf("invalid watch_group id %"PRIu32,
1990 bucket->watch_group);
1993 error = str_to_ofpact__(pos, act, arg, &ofpacts, n_actions,
1999 ofpbuf_uninit(&ofpacts);
2004 ofpact_pad(&ofpacts);
2005 bucket->ofpacts = ofpacts.data;
2006 bucket->ofpacts_len = ofpacts.size;
2011 static char * WARN_UNUSED_RESULT
2012 parse_ofp_group_mod_str__(struct ofputil_group_mod *gm, uint16_t command,
2014 enum ofputil_protocol *usable_protocols)
2017 F_GROUP_TYPE = 1 << 0,
2020 char *save_ptr = NULL;
2021 bool had_type = false;
2023 struct ofputil_bucket *bucket;
2026 *usable_protocols = OFPUTIL_P_OF11_UP;
2030 fields = F_GROUP_TYPE | F_BUCKETS;
2033 case OFPGC11_DELETE:
2037 case OFPGC11_MODIFY:
2038 fields = F_GROUP_TYPE | F_BUCKETS;
2045 memset(gm, 0, sizeof *gm);
2046 gm->command = command;
2047 gm->group_id = OFPG_ANY;
2048 list_init(&gm->buckets);
2049 if (command == OFPGC11_DELETE && string[0] == '\0') {
2050 gm->group_id = OFPG_ALL;
2054 *usable_protocols = OFPUTIL_P_OF11_UP;
2056 if (fields & F_BUCKETS) {
2057 char *bkt_str = strstr(string, "bucket");
2066 bkt_str = strchr(bkt_str + 1, '=');
2068 error = xstrdup("must specify bucket content");
2073 next_bkt_str = strstr(bkt_str, "bucket");
2075 *next_bkt_str = '\0';
2078 bucket = xzalloc(sizeof(struct ofputil_bucket));
2079 error = parse_bucket_str(bucket, bkt_str, usable_protocols);
2084 list_push_back(&gm->buckets, &bucket->list_node);
2086 bkt_str = next_bkt_str;
2090 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
2091 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
2094 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
2096 error = xasprintf("field %s missing value", name);
2100 if (!strcmp(name, "group_id")) {
2101 if(!strcmp(value, "all")) {
2102 gm->group_id = OFPG_ALL;
2104 char *error = str_to_u32(value, &gm->group_id);
2108 if (gm->group_id != OFPG_ALL && gm->group_id > OFPG_MAX) {
2109 error = xasprintf("invalid group id %"PRIu32,
2114 } else if (!strcmp(name, "type")){
2115 if (!(fields & F_GROUP_TYPE)) {
2116 error = xstrdup("type is not needed");
2119 if (!strcmp(value, "all")) {
2120 gm->type = OFPGT11_ALL;
2121 } else if (!strcmp(value, "select")) {
2122 gm->type = OFPGT11_SELECT;
2123 } else if (!strcmp(value, "indirect")) {
2124 gm->type = OFPGT11_INDIRECT;
2125 } else if (!strcmp(value, "ff") ||
2126 !strcmp(value, "fast_failover")) {
2127 gm->type = OFPGT11_FF;
2129 error = xasprintf("invalid group type %s", value);
2133 } else if (!strcmp(name, "bucket")) {
2134 error = xstrdup("bucket is not needed");
2137 error = xasprintf("unknown keyword %s", name);
2141 if (gm->group_id == OFPG_ANY) {
2142 error = xstrdup("must specify a group_id");
2145 if (fields & F_GROUP_TYPE && !had_type) {
2146 error = xstrdup("must specify a type");
2150 /* Validate buckets. */
2151 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
2152 if (bucket->weight != 1 && gm->type != OFPGT11_SELECT) {
2153 error = xstrdup("Only select groups can have bucket weights.");
2157 if (gm->type == OFPGT11_INDIRECT && !list_is_short(&gm->buckets)) {
2158 error = xstrdup("Indirect groups can have at most one bucket.");
2164 ofputil_bucket_list_destroy(&gm->buckets);
2168 char * WARN_UNUSED_RESULT
2169 parse_ofp_group_mod_str(struct ofputil_group_mod *gm, uint16_t command,
2171 enum ofputil_protocol *usable_protocols)
2173 char *string = xstrdup(str_);
2174 char *error = parse_ofp_group_mod_str__(gm, command, string,
2179 ofputil_bucket_list_destroy(&gm->buckets);
2184 char * WARN_UNUSED_RESULT
2185 parse_ofp_group_mod_file(const char *file_name, uint16_t command,
2186 struct ofputil_group_mod **gms, size_t *n_gms,
2187 enum ofputil_protocol *usable_protocols)
2189 size_t allocated_gms;
2197 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
2198 if (stream == NULL) {
2199 return xasprintf("%s: open failed (%s)",
2200 file_name, ovs_strerror(errno));
2203 allocated_gms = *n_gms;
2206 *usable_protocols = OFPUTIL_P_OF11_UP;
2207 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
2208 enum ofputil_protocol usable;
2211 if (*n_gms >= allocated_gms) {
2212 *gms = x2nrealloc(*gms, &allocated_gms, sizeof **gms);
2214 error = parse_ofp_group_mod_str(&(*gms)[*n_gms], command, ds_cstr(&s),
2219 for (i = 0; i < *n_gms; i++) {
2220 ofputil_bucket_list_destroy(&(*gms)[i].buckets);
2227 if (stream != stdin) {
2231 return xasprintf("%s:%d: %s", file_name, line_number, error);
2233 *usable_protocols &= usable;
2238 if (stream != stdin) {