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"
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"
38 #include "socket-util.h"
42 VLOG_DEFINE_THIS_MODULE(ofp_parse);
44 static void ofp_fatal(const char *flow, bool verbose, const char *format, ...)
48 str_to_table_id(const char *str)
52 if (!str_to_int(str, 10, &table_id) || table_id < 0 || table_id > 255) {
53 ovs_fatal(0, "invalid table \"%s\"", str);
59 str_to_u16(const char *str, const char *name)
63 if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) {
64 ovs_fatal(0, "invalid %s \"%s\"", name, str);
70 str_to_u32(const char *str)
76 ovs_fatal(0, "missing required numeric argument");
80 value = strtoul(str, &tail, 0);
81 if (errno == EINVAL || errno == ERANGE || *tail) {
82 ovs_fatal(0, "invalid numeric format %s", str);
88 str_to_u64(const char *str)
94 ovs_fatal(0, "missing required numeric argument");
98 value = strtoull(str, &tail, 0);
99 if (errno == EINVAL || errno == ERANGE || *tail) {
100 ovs_fatal(0, "invalid numeric format %s", str);
106 str_to_mac(const char *str, uint8_t mac[6])
108 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
109 != ETH_ADDR_SCAN_COUNT) {
110 ovs_fatal(0, "invalid mac address %s", str);
115 str_to_ip(const char *str, ovs_be32 *ip)
117 struct in_addr in_addr;
119 if (lookup_ip(str, &in_addr)) {
120 ovs_fatal(0, "%s: could not convert to IP address", str);
122 *ip = in_addr.s_addr;
126 parse_enqueue(char *arg, struct ofpbuf *ofpacts)
129 char *port = strtok_r(arg, ":q", &sp);
130 char *queue = strtok_r(NULL, "", &sp);
131 struct ofpact_enqueue *enqueue;
133 if (port == NULL || queue == NULL) {
134 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
137 enqueue = ofpact_put_ENQUEUE(ofpacts);
138 enqueue->port = str_to_u32(port);
139 enqueue->queue = str_to_u32(queue);
143 parse_output(char *arg, struct ofpbuf *ofpacts)
145 if (strchr(arg, '[')) {
146 struct ofpact_output_reg *output_reg;
148 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
149 mf_parse_subfield(&output_reg->src, arg);
150 output_reg->max_len = UINT16_MAX;
152 struct ofpact_output *output;
154 output = ofpact_put_OUTPUT(ofpacts);
155 output->port = str_to_u32(arg);
156 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
161 parse_resubmit(char *arg, struct ofpbuf *ofpacts)
163 struct ofpact_resubmit *resubmit;
164 char *in_port_s, *table_s;
166 resubmit = ofpact_put_RESUBMIT(ofpacts);
168 in_port_s = strsep(&arg, ",");
169 if (in_port_s && in_port_s[0]) {
170 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
171 ovs_fatal(0, "%s: resubmit to unknown port", in_port_s);
174 resubmit->in_port = OFPP_IN_PORT;
177 table_s = strsep(&arg, ",");
178 resubmit->table_id = table_s && table_s[0] ? str_to_u32(table_s) : 255;
180 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
181 ovs_fatal(0, "at least one \"in_port\" or \"table\" must be specified "
187 parse_note(const char *arg, struct ofpbuf *ofpacts)
189 struct ofpact_note *note;
191 note = ofpact_put_NOTE(ofpacts);
192 while (*arg != '\0') {
203 byte = hexits_value(arg, 2, &ok);
205 ovs_fatal(0, "bad hex digit in `note' argument");
207 ofpbuf_put(ofpacts, &byte, 1);
214 ofpact_update_len(ofpacts, ¬e->ofpact);
218 parse_fin_timeout(struct ofpbuf *b, char *arg)
220 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(b);
223 while (ofputil_parse_key_value(&arg, &key, &value)) {
224 if (!strcmp(key, "idle_timeout")) {
225 oft->fin_idle_timeout = str_to_u16(value, key);
226 } else if (!strcmp(key, "hard_timeout")) {
227 oft->fin_hard_timeout = str_to_u16(value, key);
229 ovs_fatal(0, "invalid key '%s' in 'fin_timeout' argument", key);
235 parse_controller(struct ofpbuf *b, char *arg)
237 enum ofp_packet_in_reason reason = OFPR_ACTION;
238 uint16_t controller_id = 0;
239 uint16_t max_len = UINT16_MAX;
243 } else if (strspn(arg, "0123456789") == strlen(arg)) {
244 max_len = str_to_u16(arg, "max_len");
248 while (ofputil_parse_key_value(&arg, &name, &value)) {
249 if (!strcmp(name, "reason")) {
250 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
251 ovs_fatal(0, "unknown reason \"%s\"", value);
253 } else if (!strcmp(name, "max_len")) {
254 max_len = str_to_u16(value, "max_len");
255 } else if (!strcmp(name, "id")) {
256 controller_id = str_to_u16(value, "id");
258 ovs_fatal(0, "unknown key \"%s\" parsing controller action",
264 if (reason == OFPR_ACTION && controller_id == 0) {
265 struct ofpact_output *output;
267 output = ofpact_put_OUTPUT(b);
268 output->port = OFPP_CONTROLLER;
269 output->max_len = max_len;
271 struct ofpact_controller *controller;
273 controller = ofpact_put_CONTROLLER(b);
274 controller->max_len = max_len;
275 controller->reason = reason;
276 controller->controller_id = controller_id;
281 parse_noargs_dec_ttl(struct ofpbuf *b)
283 struct ofpact_cnt_ids *ids;
286 ids = ofpact_put_DEC_TTL(b);
287 ofpbuf_put(b, &id, sizeof id);
289 ids->n_controllers++;
290 ofpact_update_len(b, &ids->ofpact);
294 parse_dec_ttl(struct ofpbuf *b, char *arg)
297 parse_noargs_dec_ttl(b);
299 struct ofpact_cnt_ids *ids;
302 ids = ofpact_put_DEC_TTL(b);
303 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
304 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
305 cntr = strtok_r(NULL, ", ", &arg)) {
306 uint16_t id = atoi(cntr);
308 ofpbuf_put(b, &id, sizeof id);
310 ids->n_controllers++;
312 if (!ids->n_controllers) {
313 ovs_fatal(0, "dec_ttl_cnt_ids: expected at least one controller "
316 ofpact_update_len(b, &ids->ofpact);
321 parse_set_mpls_ttl(struct ofpbuf *b, const char *arg)
323 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(b);
326 ovs_fatal(0, "parse_set_mpls_ttl: expected ttl.");
329 mpls_ttl->ttl = atoi(arg);
333 set_field_parse(const char *arg, struct ofpbuf *ofpacts)
335 char *orig = xstrdup(arg);
336 struct ofpact_reg_load *load = ofpact_put_REG_LOAD(ofpacts);
340 const struct mf_field *mf;
342 union mf_value mf_value;
345 delim = strstr(orig, "->");
347 ovs_fatal(0, "%s: missing `->'", orig);
349 if (strlen(delim) <= strlen("->")) {
350 ovs_fatal(0, "%s: missing field name following `->'", orig);
353 key = delim + strlen("->");
354 mf = mf_from_name(key);
356 ovs_fatal(0, "%s is not valid oxm field name", key);
359 ovs_fatal(0, "%s is not allowed to set", key);
363 error = mf_parse_value(mf, value, &mf_value);
365 ovs_fatal(0, "%s", error);
367 if (!mf_is_value_valid(mf, &mf_value)) {
368 ovs_fatal(0, "%s is not valid valid for field %s", value, key);
370 ofpact_set_field_init(load, mf, &mf_value);
375 parse_metadata(struct ofpbuf *b, char *arg)
377 struct ofpact_metadata *om;
378 char *mask = strchr(arg, '/');
380 om = ofpact_put_WRITE_METADATA(b);
384 om->mask = htonll(str_to_u64(mask + 1));
386 om->mask = htonll(UINT64_MAX);
389 om->metadata = htonll(str_to_u64(arg));
393 parse_named_action(enum ofputil_action_code code, const struct flow *flow,
394 char *arg, struct ofpbuf *ofpacts)
396 struct ofpact_tunnel *tunnel;
404 case OFPUTIL_ACTION_INVALID:
407 case OFPUTIL_OFPAT10_OUTPUT:
408 case OFPUTIL_OFPAT11_OUTPUT:
409 parse_output(arg, ofpacts);
412 case OFPUTIL_OFPAT10_SET_VLAN_VID:
413 case OFPUTIL_OFPAT11_SET_VLAN_VID:
414 vid = str_to_u32(arg);
415 if (vid & ~VLAN_VID_MASK) {
416 ovs_fatal(0, "%s: not a valid VLAN VID", arg);
418 ofpact_put_SET_VLAN_VID(ofpacts)->vlan_vid = vid;
421 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
422 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
423 pcp = str_to_u32(arg);
425 ovs_fatal(0, "%s: not a valid VLAN PCP", arg);
427 ofpact_put_SET_VLAN_PCP(ofpacts)->vlan_pcp = pcp;
430 case OFPUTIL_OFPAT12_SET_FIELD:
431 set_field_parse(arg, ofpacts);
434 case OFPUTIL_OFPAT10_STRIP_VLAN:
435 case OFPUTIL_OFPAT11_POP_VLAN:
436 ofpact_put_STRIP_VLAN(ofpacts);
439 case OFPUTIL_OFPAT11_PUSH_VLAN:
440 ethertype = str_to_u16(arg, "ethertype");
441 if (ethertype != ETH_TYPE_VLAN_8021Q) {
442 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
443 ovs_fatal(0, "%s: not a valid VLAN ethertype", arg);
445 ofpact_put_PUSH_VLAN(ofpacts);
448 case OFPUTIL_OFPAT11_SET_QUEUE:
449 ofpact_put_SET_QUEUE(ofpacts)->queue_id = str_to_u32(arg);
453 case OFPUTIL_OFPAT10_SET_DL_SRC:
454 case OFPUTIL_OFPAT11_SET_DL_SRC:
455 str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
458 case OFPUTIL_OFPAT10_SET_DL_DST:
459 case OFPUTIL_OFPAT11_SET_DL_DST:
460 str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
463 case OFPUTIL_OFPAT10_SET_NW_SRC:
464 case OFPUTIL_OFPAT11_SET_NW_SRC:
466 ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4 = ip;
469 case OFPUTIL_OFPAT10_SET_NW_DST:
470 case OFPUTIL_OFPAT11_SET_NW_DST:
472 ofpact_put_SET_IPV4_DST(ofpacts)->ipv4 = ip;
475 case OFPUTIL_OFPAT10_SET_NW_TOS:
476 case OFPUTIL_OFPAT11_SET_NW_TOS:
477 tos = str_to_u32(arg);
478 if (tos & ~IP_DSCP_MASK) {
479 ovs_fatal(0, "%s: not a valid TOS", arg);
481 ofpact_put_SET_IPV4_DSCP(ofpacts)->dscp = tos;
484 case OFPUTIL_OFPAT11_DEC_NW_TTL:
487 case OFPUTIL_OFPAT10_SET_TP_SRC:
488 case OFPUTIL_OFPAT11_SET_TP_SRC:
489 ofpact_put_SET_L4_SRC_PORT(ofpacts)->port = str_to_u32(arg);
492 case OFPUTIL_OFPAT10_SET_TP_DST:
493 case OFPUTIL_OFPAT11_SET_TP_DST:
494 ofpact_put_SET_L4_DST_PORT(ofpacts)->port = str_to_u32(arg);
497 case OFPUTIL_OFPAT10_ENQUEUE:
498 parse_enqueue(arg, ofpacts);
501 case OFPUTIL_NXAST_RESUBMIT:
502 parse_resubmit(arg, ofpacts);
505 case OFPUTIL_NXAST_SET_TUNNEL:
506 case OFPUTIL_NXAST_SET_TUNNEL64:
507 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
508 tunnel->ofpact.compat = code;
509 tunnel->tun_id = str_to_u64(arg);
512 case OFPUTIL_NXAST_WRITE_METADATA:
513 parse_metadata(ofpacts, arg);
516 case OFPUTIL_NXAST_SET_QUEUE:
517 ofpact_put_SET_QUEUE(ofpacts)->queue_id = str_to_u32(arg);
520 case OFPUTIL_NXAST_POP_QUEUE:
521 ofpact_put_POP_QUEUE(ofpacts);
524 case OFPUTIL_NXAST_REG_MOVE:
525 nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts), arg);
528 case OFPUTIL_NXAST_REG_LOAD:
529 nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts), arg);
532 case OFPUTIL_NXAST_NOTE:
533 parse_note(arg, ofpacts);
536 case OFPUTIL_NXAST_MULTIPATH:
537 multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
540 case OFPUTIL_NXAST_BUNDLE:
541 bundle_parse(arg, ofpacts);
544 case OFPUTIL_NXAST_BUNDLE_LOAD:
545 bundle_parse_load(arg, ofpacts);
548 case OFPUTIL_NXAST_RESUBMIT_TABLE:
549 case OFPUTIL_NXAST_OUTPUT_REG:
550 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
553 case OFPUTIL_NXAST_LEARN:
554 learn_parse(arg, flow, ofpacts);
557 case OFPUTIL_NXAST_EXIT:
558 ofpact_put_EXIT(ofpacts);
561 case OFPUTIL_NXAST_DEC_TTL:
562 parse_dec_ttl(ofpacts, arg);
565 case OFPUTIL_NXAST_SET_MPLS_TTL:
566 case OFPUTIL_OFPAT11_SET_MPLS_TTL:
567 parse_set_mpls_ttl(ofpacts, arg);
570 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
571 case OFPUTIL_NXAST_DEC_MPLS_TTL:
572 ofpact_put_DEC_MPLS_TTL(ofpacts);
575 case OFPUTIL_NXAST_FIN_TIMEOUT:
576 parse_fin_timeout(ofpacts, arg);
579 case OFPUTIL_NXAST_CONTROLLER:
580 parse_controller(ofpacts, arg);
583 case OFPUTIL_OFPAT11_PUSH_MPLS:
584 case OFPUTIL_NXAST_PUSH_MPLS:
585 ofpact_put_PUSH_MPLS(ofpacts)->ethertype =
586 htons(str_to_u16(arg, "push_mpls"));
589 case OFPUTIL_OFPAT11_POP_MPLS:
590 case OFPUTIL_NXAST_POP_MPLS:
591 ofpact_put_POP_MPLS(ofpacts)->ethertype =
592 htons(str_to_u16(arg, "pop_mpls"));
594 case OFPUTIL_NXAST_STACK_PUSH:
595 nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
597 case OFPUTIL_NXAST_STACK_POP:
598 nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
604 str_to_ofpact__(const struct flow *flow, char *pos, char *act, char *arg,
605 struct ofpbuf *ofpacts, int n_actions)
607 int code = ofputil_action_code_from_name(act);
609 parse_named_action(code, flow, arg, ofpacts);
610 } else if (!strcasecmp(act, "drop")) {
612 ovs_fatal(0, "Drop actions must not be preceded by other "
614 } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
615 ovs_fatal(0, "Drop actions must not be followed by other "
621 if (ofputil_port_from_string(act, &port)) {
622 ofpact_put_OUTPUT(ofpacts)->port = port;
624 ovs_fatal(0, "Unknown action: %s", act);
632 str_to_ofpacts(const struct flow *flow, char *str, struct ofpbuf *ofpacts)
634 char *pos, *act, *arg;
640 while (ofputil_parse_key_value(&pos, &act, &arg)) {
641 if (!str_to_ofpact__(flow, pos, act, arg, ofpacts, n_actions)) {
647 error = ofpacts_verify(ofpacts->data, ofpacts->size);
649 ovs_fatal(0, "Incorrect action ordering");
656 parse_named_instruction(enum ovs_instruction_type type,
657 char *arg, struct ofpbuf *ofpacts)
662 case OVSINST_OFPIT11_APPLY_ACTIONS:
663 NOT_REACHED(); /* This case is handled by str_to_inst_ofpacts() */
666 case OVSINST_OFPIT11_WRITE_ACTIONS:
668 ovs_fatal(0, "instruction write-actions is not supported yet");
671 case OVSINST_OFPIT11_CLEAR_ACTIONS:
672 ofpact_put_CLEAR_ACTIONS(ofpacts);
675 case OVSINST_OFPIT11_WRITE_METADATA:
676 parse_metadata(ofpacts, arg);
679 case OVSINST_OFPIT11_GOTO_TABLE: {
680 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
681 char *table_s = strsep(&arg, ",");
682 if (!table_s || !table_s[0]) {
683 ovs_fatal(0, "instruction goto-table needs table id");
685 ogt->table_id = str_to_table_id(table_s);
690 /* If write_metadata is specified as an action AND an instruction, ofpacts
692 error = ofpacts_verify(ofpacts->data, ofpacts->size);
694 ovs_fatal(0, "Incorrect instruction ordering");
699 str_to_inst_ofpacts(const struct flow *flow, char *str, struct ofpbuf *ofpacts)
701 char *pos, *inst, *arg;
703 const char *prev_inst = NULL;
708 while (ofputil_parse_key_value(&pos, &inst, &arg)) {
709 type = ofpact_instruction_type_from_name(inst);
711 if (!str_to_ofpact__(flow, pos, inst, arg, ofpacts, n_actions)) {
715 type = OVSINST_OFPIT11_APPLY_ACTIONS;
716 if (prev_type == type) {
720 } else if (type == OVSINST_OFPIT11_APPLY_ACTIONS) {
721 ovs_fatal(0, "%s isn't supported. Just write actions then "
722 "it is interpreted as apply_actions", inst);
724 parse_named_instruction(type, arg, ofpacts);
727 if (type == prev_type) {
728 ovs_fatal(0, "instruction can be specified at most once: %s",
731 if (type <= prev_type) {
732 ovs_fatal(0, "Instruction %s must be specified before %s",
750 parse_protocol(const char *name, const struct protocol **p_out)
752 static const struct protocol protocols[] = {
753 { "ip", ETH_TYPE_IP, 0 },
754 { "arp", ETH_TYPE_ARP, 0 },
755 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
756 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
757 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
758 { "ipv6", ETH_TYPE_IPV6, 0 },
759 { "ip6", ETH_TYPE_IPV6, 0 },
760 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
761 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
762 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
763 { "rarp", ETH_TYPE_RARP, 0},
764 { "mpls", ETH_TYPE_MPLS, 0 },
765 { "mplsm", ETH_TYPE_MPLS_MCAST, 0 },
767 const struct protocol *p;
769 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
770 if (!strcmp(p->name, name)) {
780 ofp_fatal(const char *flow, bool verbose, const char *format, ...)
785 fprintf(stderr, "%s:\n", flow);
788 va_start(args, format);
789 ovs_fatal_valist(0, format, args);
793 parse_field(const struct mf_field *mf, const char *s, struct match *match)
795 union mf_value value, mask;
798 error = mf_parse(mf, s, &value, &mask);
800 ovs_fatal(0, "%s", error);
803 mf_set(mf, &value, &mask, match);
806 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
807 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
808 * If 'actions' is specified, an action must be in 'string' and may be expanded
811 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
812 * constant for 'command'. To parse syntax for an OFPST_FLOW or
813 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
815 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
825 char *string = xstrdup(str_);
826 char *save_ptr = NULL;
827 char *act_str = NULL;
836 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
843 case OFPFC_DELETE_STRICT:
844 fields = F_OUT_PORT | F_PRIORITY;
848 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
851 case OFPFC_MODIFY_STRICT:
852 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
859 match_init_catchall(&fm->match);
860 fm->priority = OFP_DEFAULT_PRIORITY;
861 fm->cookie = htonll(0);
862 fm->cookie_mask = htonll(0);
863 if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
864 /* For modify, by default, don't update the cookie. */
865 fm->new_cookie = htonll(UINT64_MAX);
867 fm->new_cookie = htonll(0);
870 fm->command = command;
871 fm->idle_timeout = OFP_FLOW_PERMANENT;
872 fm->hard_timeout = OFP_FLOW_PERMANENT;
873 fm->buffer_id = UINT32_MAX;
874 fm->out_port = OFPP_ANY;
876 if (fields & F_ACTIONS) {
877 act_str = strstr(string, "action");
879 ofp_fatal(str_, verbose, "must specify an action");
883 act_str = strchr(act_str + 1, '=');
885 ofp_fatal(str_, verbose, "must specify an action");
890 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
891 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
892 const struct protocol *p;
894 if (parse_protocol(name, &p)) {
895 match_set_dl_type(&fm->match, htons(p->dl_type));
897 match_set_nw_proto(&fm->match, p->nw_proto);
899 } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
900 fm->flags |= OFPFF_SEND_FLOW_REM;
901 } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
902 fm->flags |= OFPFF_CHECK_OVERLAP;
903 } else if (fields & F_FLAGS && !strcmp(name, "reset_counts")) {
904 fm->flags |= OFPFF12_RESET_COUNTS;
905 } else if (fields & F_FLAGS && !strcmp(name, "no_packet_counts")) {
906 fm->flags |= OFPFF13_NO_PKT_COUNTS;
907 } else if (fields & F_FLAGS && !strcmp(name, "no_byte_counts")) {
908 fm->flags |= OFPFF13_NO_BYT_COUNTS;
912 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
914 ofp_fatal(str_, verbose, "field %s missing value", name);
917 if (!strcmp(name, "table")) {
918 fm->table_id = str_to_table_id(value);
919 } else if (!strcmp(name, "out_port")) {
920 if (!ofputil_port_from_string(name, &fm->out_port)) {
921 ofp_fatal(str_, verbose, "%s is not a valid OpenFlow port",
924 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
925 fm->priority = str_to_u16(value, name);
926 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
927 fm->idle_timeout = str_to_u16(value, name);
928 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
929 fm->hard_timeout = str_to_u16(value, name);
930 } else if (!strcmp(name, "cookie")) {
931 char *mask = strchr(value, '/');
934 /* A mask means we're searching for a cookie. */
935 if (command == OFPFC_ADD) {
936 ofp_fatal(str_, verbose, "flow additions cannot use "
940 fm->cookie = htonll(str_to_u64(value));
941 fm->cookie_mask = htonll(str_to_u64(mask+1));
943 /* No mask means that the cookie is being set. */
944 if (command != OFPFC_ADD && command != OFPFC_MODIFY
945 && command != OFPFC_MODIFY_STRICT) {
946 ofp_fatal(str_, verbose, "cannot set cookie");
948 fm->new_cookie = htonll(str_to_u64(value));
950 } else if (mf_from_name(name)) {
951 parse_field(mf_from_name(name), value, &fm->match);
952 } else if (!strcmp(name, "duration")
953 || !strcmp(name, "n_packets")
954 || !strcmp(name, "n_bytes")
955 || !strcmp(name, "idle_age")
956 || !strcmp(name, "hard_age")) {
957 /* Ignore these, so that users can feed the output of
958 * "ovs-ofctl dump-flows" back into commands that parse
961 ofp_fatal(str_, verbose, "unknown keyword %s", name);
965 if (!fm->cookie_mask && fm->new_cookie == htonll(UINT64_MAX)
966 && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
967 /* On modifies without a mask, we are supposed to add a flow if
968 * one does not exist. If a cookie wasn't been specified, use a
969 * default of zero. */
970 fm->new_cookie = htonll(0);
972 if (fields & F_ACTIONS) {
973 struct ofpbuf ofpacts;
975 ofpbuf_init(&ofpacts, 32);
976 str_to_inst_ofpacts(&fm->match.flow, act_str, &ofpacts);
977 fm->ofpacts_len = ofpacts.size;
978 fm->ofpacts = ofpbuf_steal_data(&ofpacts);
987 /* Convert 'str_' (as described in the documentation for the "monitor" command
988 * in the ovs-ofctl man page) into 'fmr'. */
990 parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
995 char *string = xstrdup(str_);
996 char *save_ptr = NULL;
1000 fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
1001 | NXFMF_OWN | NXFMF_ACTIONS);
1002 fmr->out_port = OFPP_NONE;
1003 fmr->table_id = 0xff;
1004 match_init_catchall(&fmr->match);
1006 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1007 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1008 const struct protocol *p;
1010 if (!strcmp(name, "!initial")) {
1011 fmr->flags &= ~NXFMF_INITIAL;
1012 } else if (!strcmp(name, "!add")) {
1013 fmr->flags &= ~NXFMF_ADD;
1014 } else if (!strcmp(name, "!delete")) {
1015 fmr->flags &= ~NXFMF_DELETE;
1016 } else if (!strcmp(name, "!modify")) {
1017 fmr->flags &= ~NXFMF_MODIFY;
1018 } else if (!strcmp(name, "!actions")) {
1019 fmr->flags &= ~NXFMF_ACTIONS;
1020 } else if (!strcmp(name, "!own")) {
1021 fmr->flags &= ~NXFMF_OWN;
1022 } else if (parse_protocol(name, &p)) {
1023 match_set_dl_type(&fmr->match, htons(p->dl_type));
1025 match_set_nw_proto(&fmr->match, p->nw_proto);
1030 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1032 ovs_fatal(0, "%s: field %s missing value", str_, name);
1035 if (!strcmp(name, "table")) {
1036 fmr->table_id = str_to_table_id(value);
1037 } else if (!strcmp(name, "out_port")) {
1038 fmr->out_port = atoi(value);
1039 } else if (mf_from_name(name)) {
1040 parse_field(mf_from_name(name), value, &fmr->match);
1042 ovs_fatal(0, "%s: unknown keyword %s", str_, name);
1049 /* Parses 's' as a set of OpenFlow actions and appends the actions to
1052 * Prints an error on stderr and aborts the program if 's' syntax is
1055 parse_ofpacts(const char *s_, struct ofpbuf *ofpacts)
1057 char *s = xstrdup(s_);
1058 str_to_ofpacts(NULL, s, ofpacts);
1062 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
1063 * (one of OFPFC_*) into 'fm'. */
1065 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
1066 uint16_t command, bool verbose)
1068 struct match match_copy;
1070 parse_ofp_str(fm, command, string, verbose);
1072 /* Normalize a copy of the match. This ensures that non-normalized flows
1073 * get logged but doesn't affect what gets sent to the switch, so that the
1074 * switch can do whatever it likes with the flow. */
1075 match_copy = fm->match;
1076 ofputil_normalize_match(&match_copy);
1080 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
1081 struct ofputil_flow_mod **fms, size_t *n_fms)
1083 size_t allocated_fms;
1087 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1088 if (stream == NULL) {
1089 ovs_fatal(errno, "%s: open", file_name);
1092 allocated_fms = *n_fms;
1094 while (!ds_get_preprocessed_line(&s, stream)) {
1095 if (*n_fms >= allocated_fms) {
1096 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
1098 parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command, false);
1103 if (stream != stdin) {
1109 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
1110 bool aggregate, const char *string)
1112 struct ofputil_flow_mod fm;
1114 parse_ofp_str(&fm, -1, string, false);
1115 fsr->aggregate = aggregate;
1116 fsr->cookie = fm.cookie;
1117 fsr->cookie_mask = fm.cookie_mask;
1118 fsr->match = fm.match;
1119 fsr->out_port = fm.out_port;
1120 fsr->table_id = fm.table_id;
1123 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
1124 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1125 * mf_field. Fields must be specified in a natural order for satisfying
1128 * Returns NULL on success, otherwise a malloc()'d string that explains the
1131 parse_ofp_exact_flow(struct flow *flow, const char *s)
1133 char *pos, *key, *value_s;
1137 memset(flow, 0, sizeof *flow);
1139 pos = copy = xstrdup(s);
1140 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
1141 const struct protocol *p;
1142 if (parse_protocol(key, &p)) {
1143 if (flow->dl_type) {
1144 error = xasprintf("%s: Ethernet type set multiple times", s);
1147 flow->dl_type = htons(p->dl_type);
1150 if (flow->nw_proto) {
1151 error = xasprintf("%s: network protocol set "
1152 "multiple times", s);
1155 flow->nw_proto = p->nw_proto;
1158 const struct mf_field *mf;
1159 union mf_value value;
1162 mf = mf_from_name(key);
1164 error = xasprintf("%s: unknown field %s", s, key);
1168 if (!mf_are_prereqs_ok(mf, flow)) {
1169 error = xasprintf("%s: prerequisites not met for setting %s",
1174 if (!mf_is_zero(mf, flow)) {
1175 error = xasprintf("%s: field %s set multiple times", s, key);
1179 field_error = mf_parse_value(mf, value_s, &value);
1181 error = xasprintf("%s: bad value for %s (%s)",
1182 s, key, field_error);
1187 mf_set_flow_value(mf, &value, flow);
1191 if (!flow->in_port) {
1192 flow->in_port = OFPP_NONE;
1199 memset(flow, 0, sizeof *flow);