2 * Copyright (c) 2010, 2011, 2012 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"
27 #include "byte-order.h"
28 #include "dynamic-string.h"
30 #include "meta-flow.h"
31 #include "multipath.h"
34 #include "ofp-actions.h"
37 #include "openflow/openflow.h"
39 #include "socket-util.h"
43 VLOG_DEFINE_THIS_MODULE(ofp_parse);
45 static void ofp_fatal(const char *flow, bool verbose, const char *format, ...)
49 str_to_table_id(const char *str)
53 if (!str_to_int(str, 10, &table_id) || table_id < 0 || table_id > 255) {
54 ovs_fatal(0, "invalid table \"%s\"", str);
60 str_to_u16(const char *str, const char *name)
64 if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) {
65 ovs_fatal(0, "invalid %s \"%s\"", name, str);
71 str_to_u32(const char *str)
77 ovs_fatal(0, "missing required numeric argument");
81 value = strtoul(str, &tail, 0);
82 if (errno == EINVAL || errno == ERANGE || *tail) {
83 ovs_fatal(0, "invalid numeric format %s", str);
89 str_to_u64(const char *str)
95 ovs_fatal(0, "missing required numeric argument");
99 value = strtoull(str, &tail, 0);
100 if (errno == EINVAL || errno == ERANGE || *tail) {
101 ovs_fatal(0, "invalid numeric format %s", str);
107 str_to_mac(const char *str, uint8_t mac[6])
109 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
110 != ETH_ADDR_SCAN_COUNT) {
111 ovs_fatal(0, "invalid mac address %s", str);
116 str_to_ip(const char *str, ovs_be32 *ip)
118 struct in_addr in_addr;
120 if (lookup_ip(str, &in_addr)) {
121 ovs_fatal(0, "%s: could not convert to IP address", str);
123 *ip = in_addr.s_addr;
127 parse_enqueue(char *arg, struct ofpbuf *ofpacts)
130 char *port = strtok_r(arg, ":q", &sp);
131 char *queue = strtok_r(NULL, "", &sp);
132 struct ofpact_enqueue *enqueue;
134 if (port == NULL || queue == NULL) {
135 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
138 enqueue = ofpact_put_ENQUEUE(ofpacts);
139 enqueue->port = str_to_u32(port);
140 enqueue->queue = str_to_u32(queue);
144 parse_output(char *arg, struct ofpbuf *ofpacts)
146 if (strchr(arg, '[')) {
147 struct ofpact_output_reg *output_reg;
149 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
150 mf_parse_subfield(&output_reg->src, arg);
151 output_reg->max_len = UINT16_MAX;
153 struct ofpact_output *output;
155 output = ofpact_put_OUTPUT(ofpacts);
156 output->port = str_to_u32(arg);
157 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
162 parse_resubmit(char *arg, struct ofpbuf *ofpacts)
164 struct ofpact_resubmit *resubmit;
165 char *in_port_s, *table_s;
167 resubmit = ofpact_put_RESUBMIT(ofpacts);
169 in_port_s = strsep(&arg, ",");
170 if (in_port_s && in_port_s[0]) {
171 resubmit->in_port = ofputil_port_from_string(in_port_s);
172 if (!resubmit->in_port) {
173 ovs_fatal(0, "%s: resubmit to unknown port", in_port_s);
176 resubmit->in_port = OFPP_IN_PORT;
179 table_s = strsep(&arg, ",");
180 resubmit->table_id = table_s && table_s[0] ? str_to_u32(table_s) : 255;
182 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
183 ovs_fatal(0, "at least one \"in_port\" or \"table\" must be specified "
189 parse_note(const char *arg, struct ofpbuf *ofpacts)
191 struct ofpact_note *note;
193 note = ofpact_put_NOTE(ofpacts);
194 while (*arg != '\0') {
205 byte = hexits_value(arg, 2, &ok);
207 ovs_fatal(0, "bad hex digit in `note' argument");
209 ofpbuf_put(ofpacts, &byte, 1);
216 ofpact_update_len(ofpacts, ¬e->ofpact);
220 parse_fin_timeout(struct ofpbuf *b, char *arg)
222 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(b);
225 while (ofputil_parse_key_value(&arg, &key, &value)) {
226 if (!strcmp(key, "idle_timeout")) {
227 oft->fin_idle_timeout = str_to_u16(value, key);
228 } else if (!strcmp(key, "hard_timeout")) {
229 oft->fin_hard_timeout = str_to_u16(value, key);
231 ovs_fatal(0, "invalid key '%s' in 'fin_timeout' argument", key);
237 parse_controller(struct ofpbuf *b, char *arg)
239 enum ofp_packet_in_reason reason = OFPR_ACTION;
240 uint16_t controller_id = 0;
241 uint16_t max_len = UINT16_MAX;
245 } else if (strspn(arg, "0123456789") == strlen(arg)) {
246 max_len = str_to_u16(arg, "max_len");
250 while (ofputil_parse_key_value(&arg, &name, &value)) {
251 if (!strcmp(name, "reason")) {
252 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
253 ovs_fatal(0, "unknown reason \"%s\"", value);
255 } else if (!strcmp(name, "max_len")) {
256 max_len = str_to_u16(value, "max_len");
257 } else if (!strcmp(name, "id")) {
258 controller_id = str_to_u16(value, "id");
260 ovs_fatal(0, "unknown key \"%s\" parsing controller action",
266 if (reason == OFPR_ACTION && controller_id == 0) {
267 struct ofpact_output *output;
269 output = ofpact_put_OUTPUT(b);
270 output->port = OFPP_CONTROLLER;
271 output->max_len = max_len;
273 struct ofpact_controller *controller;
275 controller = ofpact_put_CONTROLLER(b);
276 controller->max_len = max_len;
277 controller->reason = reason;
278 controller->controller_id = controller_id;
283 parse_dec_ttl(struct ofpbuf *b, char *arg)
285 struct ofpact_cnt_ids *ids;
287 ids = ofpact_put_DEC_TTL(b);
292 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL;
293 ofpbuf_put(b, &id, sizeof id);
295 ids->n_controllers++;
299 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
300 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
301 cntr = strtok_r(NULL, ", ", &arg)) {
302 uint16_t id = atoi(cntr);
304 ofpbuf_put(b, &id, sizeof id);
306 ids->n_controllers++;
308 if (!ids->n_controllers) {
309 ovs_fatal(0, "dec_ttl_cnt_ids: expected at least one controller "
314 ofpact_update_len(b, &ids->ofpact);
318 set_field_parse(const char *arg, struct ofpbuf *ofpacts)
320 char *orig = xstrdup(arg);
321 struct ofpact_reg_load *load = ofpact_put_REG_LOAD(ofpacts);
325 const struct mf_field *mf;
327 union mf_value mf_value;
330 delim = strstr(orig, "->");
332 ovs_fatal(0, "%s: missing `->'", orig);
334 if (strlen(delim) <= strlen("->")) {
335 ovs_fatal(0, "%s: missing field name following `->'", orig);
338 key = delim + strlen("->");
339 mf = mf_from_name(key);
341 ovs_fatal(0, "%s is not valid oxm field name", key);
344 ovs_fatal(0, "%s is not allowed to set", key);
348 error = mf_parse_value(mf, value, &mf_value);
350 ovs_fatal(0, "%s", error);
352 if (!mf_is_value_valid(mf, &mf_value)) {
353 ovs_fatal(0, "%s is not valid valid for field %s", value, key);
355 ofpact_set_field_init(load, mf, &mf_value);
360 parse_named_action(enum ofputil_action_code code, const struct flow *flow,
361 char *arg, struct ofpbuf *ofpacts)
363 struct ofpact_tunnel *tunnel;
370 case OFPUTIL_ACTION_INVALID:
373 case OFPUTIL_OFPAT10_OUTPUT:
374 case OFPUTIL_OFPAT11_OUTPUT:
375 parse_output(arg, ofpacts);
378 case OFPUTIL_OFPAT10_SET_VLAN_VID:
379 case OFPUTIL_OFPAT11_SET_VLAN_VID:
380 vid = str_to_u32(arg);
381 if (vid & ~VLAN_VID_MASK) {
382 ovs_fatal(0, "%s: not a valid VLAN VID", arg);
384 ofpact_put_SET_VLAN_VID(ofpacts)->vlan_vid = vid;
387 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
388 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
389 pcp = str_to_u32(arg);
391 ovs_fatal(0, "%s: not a valid VLAN PCP", arg);
393 ofpact_put_SET_VLAN_PCP(ofpacts)->vlan_pcp = pcp;
396 case OFPUTIL_OFPAT12_SET_FIELD:
397 set_field_parse(arg, ofpacts);
400 case OFPUTIL_OFPAT10_STRIP_VLAN:
401 ofpact_put_STRIP_VLAN(ofpacts);
404 case OFPUTIL_OFPAT10_SET_DL_SRC:
405 case OFPUTIL_OFPAT11_SET_DL_SRC:
406 str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
409 case OFPUTIL_OFPAT10_SET_DL_DST:
410 case OFPUTIL_OFPAT11_SET_DL_DST:
411 str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
414 case OFPUTIL_OFPAT10_SET_NW_SRC:
415 case OFPUTIL_OFPAT11_SET_NW_SRC:
417 ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4 = ip;
420 case OFPUTIL_OFPAT10_SET_NW_DST:
421 case OFPUTIL_OFPAT11_SET_NW_DST:
423 ofpact_put_SET_IPV4_DST(ofpacts)->ipv4 = ip;
426 case OFPUTIL_OFPAT10_SET_NW_TOS:
427 case OFPUTIL_OFPAT11_SET_NW_TOS:
428 tos = str_to_u32(arg);
429 if (tos & ~IP_DSCP_MASK) {
430 ovs_fatal(0, "%s: not a valid TOS", arg);
432 ofpact_put_SET_IPV4_DSCP(ofpacts)->dscp = tos;
435 case OFPUTIL_OFPAT10_SET_TP_SRC:
436 case OFPUTIL_OFPAT11_SET_TP_SRC:
437 ofpact_put_SET_L4_SRC_PORT(ofpacts)->port = str_to_u32(arg);
440 case OFPUTIL_OFPAT10_SET_TP_DST:
441 case OFPUTIL_OFPAT11_SET_TP_DST:
442 ofpact_put_SET_L4_DST_PORT(ofpacts)->port = str_to_u32(arg);
445 case OFPUTIL_OFPAT10_ENQUEUE:
446 parse_enqueue(arg, ofpacts);
449 case OFPUTIL_NXAST_RESUBMIT:
450 parse_resubmit(arg, ofpacts);
453 case OFPUTIL_NXAST_SET_TUNNEL:
454 case OFPUTIL_NXAST_SET_TUNNEL64:
455 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
456 tunnel->ofpact.compat = code;
457 tunnel->tun_id = str_to_u64(arg);
460 case OFPUTIL_NXAST_SET_QUEUE:
461 ofpact_put_SET_QUEUE(ofpacts)->queue_id = str_to_u32(arg);
464 case OFPUTIL_NXAST_POP_QUEUE:
465 ofpact_put_POP_QUEUE(ofpacts);
468 case OFPUTIL_NXAST_REG_MOVE:
469 nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts), arg);
472 case OFPUTIL_NXAST_REG_LOAD:
473 nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts), arg);
476 case OFPUTIL_NXAST_NOTE:
477 parse_note(arg, ofpacts);
480 case OFPUTIL_NXAST_MULTIPATH:
481 multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
484 case OFPUTIL_NXAST_AUTOPATH__DEPRECATED:
485 autopath_parse(ofpact_put_AUTOPATH(ofpacts), arg);
488 case OFPUTIL_NXAST_BUNDLE:
489 bundle_parse(arg, ofpacts);
492 case OFPUTIL_NXAST_BUNDLE_LOAD:
493 bundle_parse_load(arg, ofpacts);
496 case OFPUTIL_NXAST_RESUBMIT_TABLE:
497 case OFPUTIL_NXAST_OUTPUT_REG:
498 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
501 case OFPUTIL_NXAST_LEARN:
502 learn_parse(arg, flow, ofpacts);
505 case OFPUTIL_NXAST_EXIT:
506 ofpact_put_EXIT(ofpacts);
509 case OFPUTIL_NXAST_DEC_TTL:
510 parse_dec_ttl(ofpacts, arg);
513 case OFPUTIL_NXAST_FIN_TIMEOUT:
514 parse_fin_timeout(ofpacts, arg);
517 case OFPUTIL_NXAST_CONTROLLER:
518 parse_controller(ofpacts, arg);
524 str_to_ofpact__(const struct flow *flow, char *pos, char *act, char *arg,
525 struct ofpbuf *ofpacts, int n_actions)
527 int code = ofputil_action_code_from_name(act);
529 parse_named_action(code, flow, arg, ofpacts);
530 } else if (!strcasecmp(act, "drop")) {
532 ovs_fatal(0, "Drop actions must not be preceded by other "
534 } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
535 ovs_fatal(0, "Drop actions must not be followed by other "
540 uint16_t port = ofputil_port_from_string(act);
542 ofpact_put_OUTPUT(ofpacts)->port = port;
544 ovs_fatal(0, "Unknown action: %s", act);
552 str_to_ofpacts(const struct flow *flow, char *str, struct ofpbuf *ofpacts)
554 char *pos, *act, *arg;
559 while (ofputil_parse_key_value(&pos, &act, &arg)) {
560 if (!str_to_ofpact__(flow, pos, act, arg, ofpacts, n_actions)) {
569 parse_named_instruction(enum ovs_instruction_type type,
570 char *arg, struct ofpbuf *ofpacts)
573 case OVSINST_OFPIT11_APPLY_ACTIONS:
574 NOT_REACHED(); /* This case is handled by str_to_inst_ofpacts() */
577 case OVSINST_OFPIT11_WRITE_ACTIONS:
579 ovs_fatal(0, "instruction write-actions is not supported yet");
582 case OVSINST_OFPIT11_CLEAR_ACTIONS:
584 ovs_fatal(0, "instruction clear-actions is not supported yet");
587 case OVSINST_OFPIT11_WRITE_METADATA:
589 ovs_fatal(0, "instruction write-metadata is not supported yet");
592 case OVSINST_OFPIT11_GOTO_TABLE: {
593 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
594 char *table_s = strsep(&arg, ",");
595 if (!table_s || !table_s[0]) {
596 ovs_fatal(0, "instruction goto-table needs table id");
598 ogt->table_id = str_to_table_id(table_s);
605 str_to_inst_ofpacts(const struct flow *flow, char *str, struct ofpbuf *ofpacts)
607 char *pos, *inst, *arg;
609 const char *prev_inst = NULL;
614 while (ofputil_parse_key_value(&pos, &inst, &arg)) {
615 type = ofpact_instruction_type_from_name(inst);
617 if (!str_to_ofpact__(flow, pos, inst, arg, ofpacts, n_actions)) {
621 type = OVSINST_OFPIT11_APPLY_ACTIONS;
622 if (prev_type == type) {
626 } else if (type == OVSINST_OFPIT11_APPLY_ACTIONS) {
627 ovs_fatal(0, "%s isn't supported. Just write actions then "
628 "it is interpreted as apply_actions", inst);
630 parse_named_instruction(type, arg, ofpacts);
633 if (type == prev_type) {
634 ovs_fatal(0, "instruction can be specified at most once: %s",
637 if (type <= prev_type) {
638 ovs_fatal(0, "Instruction %s must be specified before %s",
656 parse_protocol(const char *name, const struct protocol **p_out)
658 static const struct protocol protocols[] = {
659 { "ip", ETH_TYPE_IP, 0 },
660 { "arp", ETH_TYPE_ARP, 0 },
661 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
662 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
663 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
664 { "ipv6", ETH_TYPE_IPV6, 0 },
665 { "ip6", ETH_TYPE_IPV6, 0 },
666 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
667 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
668 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
670 const struct protocol *p;
672 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
673 if (!strcmp(p->name, name)) {
683 ofp_fatal(const char *flow, bool verbose, const char *format, ...)
688 fprintf(stderr, "%s:\n", flow);
691 va_start(args, format);
692 ovs_fatal_valist(0, format, args);
696 parse_field(const struct mf_field *mf, const char *s, struct match *match)
698 union mf_value value, mask;
701 error = mf_parse(mf, s, &value, &mask);
703 ovs_fatal(0, "%s", error);
706 mf_set(mf, &value, &mask, match);
709 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
710 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
711 * If 'actions' is specified, an action must be in 'string' and may be expanded
714 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
715 * constant for 'command'. To parse syntax for an OFPST_FLOW or
716 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
718 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
728 char *string = xstrdup(str_);
729 char *save_ptr = NULL;
730 char *act_str = NULL;
739 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
746 case OFPFC_DELETE_STRICT:
747 fields = F_OUT_PORT | F_PRIORITY;
751 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
754 case OFPFC_MODIFY_STRICT:
755 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
762 match_init_catchall(&fm->match);
763 fm->priority = OFP_DEFAULT_PRIORITY;
764 fm->cookie = htonll(0);
765 fm->cookie_mask = htonll(0);
766 if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
767 /* For modify, by default, don't update the cookie. */
768 fm->new_cookie = htonll(UINT64_MAX);
770 fm->new_cookie = htonll(0);
773 fm->command = command;
774 fm->idle_timeout = OFP_FLOW_PERMANENT;
775 fm->hard_timeout = OFP_FLOW_PERMANENT;
776 fm->buffer_id = UINT32_MAX;
777 fm->out_port = OFPP_NONE;
779 if (fields & F_ACTIONS) {
780 act_str = strstr(string, "action");
782 ofp_fatal(str_, verbose, "must specify an action");
786 act_str = strchr(act_str + 1, '=');
788 ofp_fatal(str_, verbose, "must specify an action");
793 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
794 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
795 const struct protocol *p;
797 if (parse_protocol(name, &p)) {
798 match_set_dl_type(&fm->match, htons(p->dl_type));
800 match_set_nw_proto(&fm->match, p->nw_proto);
802 } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
803 fm->flags |= OFPFF_SEND_FLOW_REM;
804 } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
805 fm->flags |= OFPFF_CHECK_OVERLAP;
809 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
811 ofp_fatal(str_, verbose, "field %s missing value", name);
814 if (!strcmp(name, "table")) {
815 fm->table_id = str_to_table_id(value);
816 } else if (!strcmp(name, "out_port")) {
817 fm->out_port = ofputil_port_from_string(name);
819 ofp_fatal(str_, verbose, "%s is not a valid OpenFlow port",
822 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
823 fm->priority = str_to_u16(value, name);
824 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
825 fm->idle_timeout = str_to_u16(value, name);
826 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
827 fm->hard_timeout = str_to_u16(value, name);
828 } else if (!strcmp(name, "cookie")) {
829 char *mask = strchr(value, '/');
832 /* A mask means we're searching for a cookie. */
833 if (command == OFPFC_ADD) {
834 ofp_fatal(str_, verbose, "flow additions cannot use "
838 fm->cookie = htonll(str_to_u64(value));
839 fm->cookie_mask = htonll(str_to_u64(mask+1));
841 /* No mask means that the cookie is being set. */
842 if (command != OFPFC_ADD && command != OFPFC_MODIFY
843 && command != OFPFC_MODIFY_STRICT) {
844 ofp_fatal(str_, verbose, "cannot set cookie");
846 fm->new_cookie = htonll(str_to_u64(value));
848 } else if (mf_from_name(name)) {
849 parse_field(mf_from_name(name), value, &fm->match);
850 } else if (!strcmp(name, "duration")
851 || !strcmp(name, "n_packets")
852 || !strcmp(name, "n_bytes")) {
853 /* Ignore these, so that users can feed the output of
854 * "ovs-ofctl dump-flows" back into commands that parse
857 ofp_fatal(str_, verbose, "unknown keyword %s", name);
861 if (!fm->cookie_mask && fm->new_cookie == htonll(UINT64_MAX)
862 && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
863 /* On modifies without a mask, we are supposed to add a flow if
864 * one does not exist. If a cookie wasn't been specified, use a
865 * default of zero. */
866 fm->new_cookie = htonll(0);
868 if (fields & F_ACTIONS) {
869 struct ofpbuf ofpacts;
871 ofpbuf_init(&ofpacts, 32);
872 str_to_inst_ofpacts(&fm->match.flow, act_str, &ofpacts);
873 fm->ofpacts_len = ofpacts.size;
874 fm->ofpacts = ofpbuf_steal_data(&ofpacts);
883 /* Convert 'str_' (as described in the documentation for the "monitor" command
884 * in the ovs-ofctl man page) into 'fmr'. */
886 parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
891 char *string = xstrdup(str_);
892 char *save_ptr = NULL;
896 fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
897 | NXFMF_OWN | NXFMF_ACTIONS);
898 fmr->out_port = OFPP_NONE;
899 fmr->table_id = 0xff;
900 match_init_catchall(&fmr->match);
902 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
903 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
904 const struct protocol *p;
906 if (!strcmp(name, "!initial")) {
907 fmr->flags &= ~NXFMF_INITIAL;
908 } else if (!strcmp(name, "!add")) {
909 fmr->flags &= ~NXFMF_ADD;
910 } else if (!strcmp(name, "!delete")) {
911 fmr->flags &= ~NXFMF_DELETE;
912 } else if (!strcmp(name, "!modify")) {
913 fmr->flags &= ~NXFMF_MODIFY;
914 } else if (!strcmp(name, "!actions")) {
915 fmr->flags &= ~NXFMF_ACTIONS;
916 } else if (!strcmp(name, "!own")) {
917 fmr->flags &= ~NXFMF_OWN;
918 } else if (parse_protocol(name, &p)) {
919 match_set_dl_type(&fmr->match, htons(p->dl_type));
921 match_set_nw_proto(&fmr->match, p->nw_proto);
926 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
928 ovs_fatal(0, "%s: field %s missing value", str_, name);
931 if (!strcmp(name, "table")) {
932 fmr->table_id = str_to_table_id(value);
933 } else if (!strcmp(name, "out_port")) {
934 fmr->out_port = atoi(value);
935 } else if (mf_from_name(name)) {
936 parse_field(mf_from_name(name), value, &fmr->match);
938 ovs_fatal(0, "%s: unknown keyword %s", str_, name);
945 /* Parses 's' as a set of OpenFlow actions and appends the actions to
948 * Prints an error on stderr and aborts the program if 's' syntax is
951 parse_ofpacts(const char *s_, struct ofpbuf *ofpacts)
953 char *s = xstrdup(s_);
954 str_to_ofpacts(NULL, s, ofpacts);
958 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
959 * (one of OFPFC_*) into 'fm'. */
961 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
962 uint16_t command, bool verbose)
964 struct match match_copy;
966 parse_ofp_str(fm, command, string, verbose);
968 /* Normalize a copy of the match. This ensures that non-normalized flows
969 * get logged but doesn't affect what gets sent to the switch, so that the
970 * switch can do whatever it likes with the flow. */
971 match_copy = fm->match;
972 ofputil_normalize_match(&match_copy);
976 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
977 struct ofputil_flow_mod **fms, size_t *n_fms)
979 size_t allocated_fms;
983 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
984 if (stream == NULL) {
985 ovs_fatal(errno, "%s: open", file_name);
988 allocated_fms = *n_fms;
990 while (!ds_get_preprocessed_line(&s, stream)) {
991 if (*n_fms >= allocated_fms) {
992 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
994 parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command, false);
999 if (stream != stdin) {
1005 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
1006 bool aggregate, const char *string)
1008 struct ofputil_flow_mod fm;
1010 parse_ofp_str(&fm, -1, string, false);
1011 fsr->aggregate = aggregate;
1012 fsr->cookie = fm.cookie;
1013 fsr->cookie_mask = fm.cookie_mask;
1014 fsr->match = fm.match;
1015 fsr->out_port = fm.out_port;
1016 fsr->table_id = fm.table_id;
1019 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
1020 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1021 * mf_field. Fields must be specified in a natural order for satisfying
1024 * Returns NULL on success, otherwise a malloc()'d string that explains the
1027 parse_ofp_exact_flow(struct flow *flow, const char *s)
1029 char *pos, *key, *value_s;
1033 memset(flow, 0, sizeof *flow);
1035 pos = copy = xstrdup(s);
1036 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
1037 const struct protocol *p;
1038 if (parse_protocol(key, &p)) {
1039 if (flow->dl_type) {
1040 error = xasprintf("%s: Ethernet type set multiple times", s);
1043 flow->dl_type = htons(p->dl_type);
1046 if (flow->nw_proto) {
1047 error = xasprintf("%s: network protocol set "
1048 "multiple times", s);
1051 flow->nw_proto = p->nw_proto;
1054 const struct mf_field *mf;
1055 union mf_value value;
1058 mf = mf_from_name(key);
1060 error = xasprintf("%s: unknown field %s", s, key);
1064 if (!mf_are_prereqs_ok(mf, flow)) {
1065 error = xasprintf("%s: prerequisites not met for setting %s",
1070 if (!mf_is_zero(mf, flow)) {
1071 error = xasprintf("%s: field %s set multiple times", s, key);
1075 field_error = mf_parse_value(mf, value_s, &value);
1077 error = xasprintf("%s: bad value for %s (%s)",
1078 s, key, field_error);
1083 mf_set_flow_value(mf, &value, flow);
1091 memset(flow, 0, sizeof *flow);