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 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
172 ovs_fatal(0, "%s: resubmit to unknown port", in_port_s);
175 resubmit->in_port = OFPP_IN_PORT;
178 table_s = strsep(&arg, ",");
179 resubmit->table_id = table_s && table_s[0] ? str_to_u32(table_s) : 255;
181 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
182 ovs_fatal(0, "at least one \"in_port\" or \"table\" must be specified "
188 parse_note(const char *arg, struct ofpbuf *ofpacts)
190 struct ofpact_note *note;
192 note = ofpact_put_NOTE(ofpacts);
193 while (*arg != '\0') {
204 byte = hexits_value(arg, 2, &ok);
206 ovs_fatal(0, "bad hex digit in `note' argument");
208 ofpbuf_put(ofpacts, &byte, 1);
215 ofpact_update_len(ofpacts, ¬e->ofpact);
219 parse_fin_timeout(struct ofpbuf *b, char *arg)
221 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(b);
224 while (ofputil_parse_key_value(&arg, &key, &value)) {
225 if (!strcmp(key, "idle_timeout")) {
226 oft->fin_idle_timeout = str_to_u16(value, key);
227 } else if (!strcmp(key, "hard_timeout")) {
228 oft->fin_hard_timeout = str_to_u16(value, key);
230 ovs_fatal(0, "invalid key '%s' in 'fin_timeout' argument", key);
236 parse_controller(struct ofpbuf *b, char *arg)
238 enum ofp_packet_in_reason reason = OFPR_ACTION;
239 uint16_t controller_id = 0;
240 uint16_t max_len = UINT16_MAX;
244 } else if (strspn(arg, "0123456789") == strlen(arg)) {
245 max_len = str_to_u16(arg, "max_len");
249 while (ofputil_parse_key_value(&arg, &name, &value)) {
250 if (!strcmp(name, "reason")) {
251 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
252 ovs_fatal(0, "unknown reason \"%s\"", value);
254 } else if (!strcmp(name, "max_len")) {
255 max_len = str_to_u16(value, "max_len");
256 } else if (!strcmp(name, "id")) {
257 controller_id = str_to_u16(value, "id");
259 ovs_fatal(0, "unknown key \"%s\" parsing controller action",
265 if (reason == OFPR_ACTION && controller_id == 0) {
266 struct ofpact_output *output;
268 output = ofpact_put_OUTPUT(b);
269 output->port = OFPP_CONTROLLER;
270 output->max_len = max_len;
272 struct ofpact_controller *controller;
274 controller = ofpact_put_CONTROLLER(b);
275 controller->max_len = max_len;
276 controller->reason = reason;
277 controller->controller_id = controller_id;
282 parse_noargs_dec_ttl(struct ofpbuf *b)
284 struct ofpact_cnt_ids *ids;
287 ids = ofpact_put_DEC_TTL(b);
288 ofpbuf_put(b, &id, sizeof id);
290 ids->n_controllers++;
291 ofpact_update_len(b, &ids->ofpact);
295 parse_dec_ttl(struct ofpbuf *b, char *arg)
298 parse_noargs_dec_ttl(b);
300 struct ofpact_cnt_ids *ids;
303 ids = ofpact_put_DEC_TTL(b);
304 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
305 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
306 cntr = strtok_r(NULL, ", ", &arg)) {
307 uint16_t id = atoi(cntr);
309 ofpbuf_put(b, &id, sizeof id);
311 ids->n_controllers++;
313 if (!ids->n_controllers) {
314 ovs_fatal(0, "dec_ttl_cnt_ids: expected at least one controller "
317 ofpact_update_len(b, &ids->ofpact);
322 set_field_parse(const char *arg, struct ofpbuf *ofpacts)
324 char *orig = xstrdup(arg);
325 struct ofpact_reg_load *load = ofpact_put_REG_LOAD(ofpacts);
329 const struct mf_field *mf;
331 union mf_value mf_value;
334 delim = strstr(orig, "->");
336 ovs_fatal(0, "%s: missing `->'", orig);
338 if (strlen(delim) <= strlen("->")) {
339 ovs_fatal(0, "%s: missing field name following `->'", orig);
342 key = delim + strlen("->");
343 mf = mf_from_name(key);
345 ovs_fatal(0, "%s is not valid oxm field name", key);
348 ovs_fatal(0, "%s is not allowed to set", key);
352 error = mf_parse_value(mf, value, &mf_value);
354 ovs_fatal(0, "%s", error);
356 if (!mf_is_value_valid(mf, &mf_value)) {
357 ovs_fatal(0, "%s is not valid valid for field %s", value, key);
359 ofpact_set_field_init(load, mf, &mf_value);
364 parse_metadata(struct ofpbuf *b, char *arg)
366 struct ofpact_metadata *om;
367 char *mask = strchr(arg, '/');
369 om = ofpact_put_WRITE_METADATA(b);
373 om->mask = htonll(str_to_u64(mask + 1));
375 om->mask = htonll(UINT64_MAX);
378 om->metadata = htonll(str_to_u64(arg));
382 parse_named_action(enum ofputil_action_code code, const struct flow *flow,
383 char *arg, struct ofpbuf *ofpacts)
385 struct ofpact_tunnel *tunnel;
393 case OFPUTIL_ACTION_INVALID:
396 case OFPUTIL_OFPAT10_OUTPUT:
397 case OFPUTIL_OFPAT11_OUTPUT:
398 parse_output(arg, ofpacts);
401 case OFPUTIL_OFPAT10_SET_VLAN_VID:
402 case OFPUTIL_OFPAT11_SET_VLAN_VID:
403 vid = str_to_u32(arg);
404 if (vid & ~VLAN_VID_MASK) {
405 ovs_fatal(0, "%s: not a valid VLAN VID", arg);
407 ofpact_put_SET_VLAN_VID(ofpacts)->vlan_vid = vid;
410 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
411 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
412 pcp = str_to_u32(arg);
414 ovs_fatal(0, "%s: not a valid VLAN PCP", arg);
416 ofpact_put_SET_VLAN_PCP(ofpacts)->vlan_pcp = pcp;
419 case OFPUTIL_OFPAT12_SET_FIELD:
420 set_field_parse(arg, ofpacts);
423 case OFPUTIL_OFPAT10_STRIP_VLAN:
424 case OFPUTIL_OFPAT11_POP_VLAN:
425 ofpact_put_STRIP_VLAN(ofpacts);
428 case OFPUTIL_OFPAT11_PUSH_VLAN:
429 ethertype = str_to_u16(arg, "ethertype");
430 if (ethertype != ETH_TYPE_VLAN_8021Q) {
431 /* TODO:XXXX ETH_TYPE_VLAN_8021AD case isn't supported */
432 ovs_fatal(0, "%s: not a valid VLAN ethertype", arg);
434 ofpact_put_PUSH_VLAN(ofpacts);
437 case OFPUTIL_OFPAT10_SET_DL_SRC:
438 case OFPUTIL_OFPAT11_SET_DL_SRC:
439 str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
442 case OFPUTIL_OFPAT10_SET_DL_DST:
443 case OFPUTIL_OFPAT11_SET_DL_DST:
444 str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
447 case OFPUTIL_OFPAT10_SET_NW_SRC:
448 case OFPUTIL_OFPAT11_SET_NW_SRC:
450 ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4 = ip;
453 case OFPUTIL_OFPAT10_SET_NW_DST:
454 case OFPUTIL_OFPAT11_SET_NW_DST:
456 ofpact_put_SET_IPV4_DST(ofpacts)->ipv4 = ip;
459 case OFPUTIL_OFPAT10_SET_NW_TOS:
460 case OFPUTIL_OFPAT11_SET_NW_TOS:
461 tos = str_to_u32(arg);
462 if (tos & ~IP_DSCP_MASK) {
463 ovs_fatal(0, "%s: not a valid TOS", arg);
465 ofpact_put_SET_IPV4_DSCP(ofpacts)->dscp = tos;
468 case OFPUTIL_OFPAT11_DEC_NW_TTL:
471 case OFPUTIL_OFPAT10_SET_TP_SRC:
472 case OFPUTIL_OFPAT11_SET_TP_SRC:
473 ofpact_put_SET_L4_SRC_PORT(ofpacts)->port = str_to_u32(arg);
476 case OFPUTIL_OFPAT10_SET_TP_DST:
477 case OFPUTIL_OFPAT11_SET_TP_DST:
478 ofpact_put_SET_L4_DST_PORT(ofpacts)->port = str_to_u32(arg);
481 case OFPUTIL_OFPAT10_ENQUEUE:
482 parse_enqueue(arg, ofpacts);
485 case OFPUTIL_NXAST_RESUBMIT:
486 parse_resubmit(arg, ofpacts);
489 case OFPUTIL_NXAST_SET_TUNNEL:
490 case OFPUTIL_NXAST_SET_TUNNEL64:
491 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
492 tunnel->ofpact.compat = code;
493 tunnel->tun_id = str_to_u64(arg);
496 case OFPUTIL_NXAST_WRITE_METADATA:
497 parse_metadata(ofpacts, arg);
500 case OFPUTIL_NXAST_SET_QUEUE:
501 ofpact_put_SET_QUEUE(ofpacts)->queue_id = str_to_u32(arg);
504 case OFPUTIL_NXAST_POP_QUEUE:
505 ofpact_put_POP_QUEUE(ofpacts);
508 case OFPUTIL_NXAST_REG_MOVE:
509 nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts), arg);
512 case OFPUTIL_NXAST_REG_LOAD:
513 nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts), arg);
516 case OFPUTIL_NXAST_NOTE:
517 parse_note(arg, ofpacts);
520 case OFPUTIL_NXAST_MULTIPATH:
521 multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
524 case OFPUTIL_NXAST_AUTOPATH__DEPRECATED:
525 autopath_parse(ofpact_put_AUTOPATH(ofpacts), arg);
528 case OFPUTIL_NXAST_BUNDLE:
529 bundle_parse(arg, ofpacts);
532 case OFPUTIL_NXAST_BUNDLE_LOAD:
533 bundle_parse_load(arg, ofpacts);
536 case OFPUTIL_NXAST_RESUBMIT_TABLE:
537 case OFPUTIL_NXAST_OUTPUT_REG:
538 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
541 case OFPUTIL_NXAST_LEARN:
542 learn_parse(arg, flow, ofpacts);
545 case OFPUTIL_NXAST_EXIT:
546 ofpact_put_EXIT(ofpacts);
549 case OFPUTIL_NXAST_DEC_TTL:
550 parse_dec_ttl(ofpacts, arg);
553 case OFPUTIL_NXAST_FIN_TIMEOUT:
554 parse_fin_timeout(ofpacts, arg);
557 case OFPUTIL_NXAST_CONTROLLER:
558 parse_controller(ofpacts, arg);
564 str_to_ofpact__(const struct flow *flow, char *pos, char *act, char *arg,
565 struct ofpbuf *ofpacts, int n_actions)
567 int code = ofputil_action_code_from_name(act);
569 parse_named_action(code, flow, arg, ofpacts);
570 } else if (!strcasecmp(act, "drop")) {
572 ovs_fatal(0, "Drop actions must not be preceded by other "
574 } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
575 ovs_fatal(0, "Drop actions must not be followed by other "
581 if (ofputil_port_from_string(act, &port)) {
582 ofpact_put_OUTPUT(ofpacts)->port = port;
584 ovs_fatal(0, "Unknown action: %s", act);
592 str_to_ofpacts(const struct flow *flow, char *str, struct ofpbuf *ofpacts)
594 char *pos, *act, *arg;
600 while (ofputil_parse_key_value(&pos, &act, &arg)) {
601 if (!str_to_ofpact__(flow, pos, act, arg, ofpacts, n_actions)) {
607 error = ofpacts_verify(ofpacts->data, ofpacts->size);
609 ovs_fatal(0, "Incorrect action ordering");
616 parse_named_instruction(enum ovs_instruction_type type,
617 char *arg, struct ofpbuf *ofpacts)
622 case OVSINST_OFPIT11_APPLY_ACTIONS:
623 NOT_REACHED(); /* This case is handled by str_to_inst_ofpacts() */
626 case OVSINST_OFPIT11_WRITE_ACTIONS:
628 ovs_fatal(0, "instruction write-actions is not supported yet");
631 case OVSINST_OFPIT11_CLEAR_ACTIONS:
632 ofpact_put_CLEAR_ACTIONS(ofpacts);
635 case OVSINST_OFPIT11_WRITE_METADATA:
636 parse_metadata(ofpacts, arg);
639 case OVSINST_OFPIT11_GOTO_TABLE: {
640 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
641 char *table_s = strsep(&arg, ",");
642 if (!table_s || !table_s[0]) {
643 ovs_fatal(0, "instruction goto-table needs table id");
645 ogt->table_id = str_to_table_id(table_s);
650 /* If write_metadata is specified as an action AND an instruction, ofpacts
652 error = ofpacts_verify(ofpacts->data, ofpacts->size);
654 ovs_fatal(0, "Incorrect instruction ordering");
659 str_to_inst_ofpacts(const struct flow *flow, char *str, struct ofpbuf *ofpacts)
661 char *pos, *inst, *arg;
663 const char *prev_inst = NULL;
668 while (ofputil_parse_key_value(&pos, &inst, &arg)) {
669 type = ofpact_instruction_type_from_name(inst);
671 if (!str_to_ofpact__(flow, pos, inst, arg, ofpacts, n_actions)) {
675 type = OVSINST_OFPIT11_APPLY_ACTIONS;
676 if (prev_type == type) {
680 } else if (type == OVSINST_OFPIT11_APPLY_ACTIONS) {
681 ovs_fatal(0, "%s isn't supported. Just write actions then "
682 "it is interpreted as apply_actions", inst);
684 parse_named_instruction(type, arg, ofpacts);
687 if (type == prev_type) {
688 ovs_fatal(0, "instruction can be specified at most once: %s",
691 if (type <= prev_type) {
692 ovs_fatal(0, "Instruction %s must be specified before %s",
710 parse_protocol(const char *name, const struct protocol **p_out)
712 static const struct protocol protocols[] = {
713 { "ip", ETH_TYPE_IP, 0 },
714 { "arp", ETH_TYPE_ARP, 0 },
715 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
716 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
717 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
718 { "ipv6", ETH_TYPE_IPV6, 0 },
719 { "ip6", ETH_TYPE_IPV6, 0 },
720 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
721 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
722 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
724 const struct protocol *p;
726 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
727 if (!strcmp(p->name, name)) {
737 ofp_fatal(const char *flow, bool verbose, const char *format, ...)
742 fprintf(stderr, "%s:\n", flow);
745 va_start(args, format);
746 ovs_fatal_valist(0, format, args);
750 parse_field(const struct mf_field *mf, const char *s, struct match *match)
752 union mf_value value, mask;
755 error = mf_parse(mf, s, &value, &mask);
757 ovs_fatal(0, "%s", error);
760 mf_set(mf, &value, &mask, match);
763 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
764 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
765 * If 'actions' is specified, an action must be in 'string' and may be expanded
768 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
769 * constant for 'command'. To parse syntax for an OFPST_FLOW or
770 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
772 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
782 char *string = xstrdup(str_);
783 char *save_ptr = NULL;
784 char *act_str = NULL;
793 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
800 case OFPFC_DELETE_STRICT:
801 fields = F_OUT_PORT | F_PRIORITY;
805 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
808 case OFPFC_MODIFY_STRICT:
809 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
816 match_init_catchall(&fm->match);
817 fm->priority = OFP_DEFAULT_PRIORITY;
818 fm->cookie = htonll(0);
819 fm->cookie_mask = htonll(0);
820 if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
821 /* For modify, by default, don't update the cookie. */
822 fm->new_cookie = htonll(UINT64_MAX);
824 fm->new_cookie = htonll(0);
827 fm->command = command;
828 fm->idle_timeout = OFP_FLOW_PERMANENT;
829 fm->hard_timeout = OFP_FLOW_PERMANENT;
830 fm->buffer_id = UINT32_MAX;
831 fm->out_port = OFPP_NONE;
833 if (fields & F_ACTIONS) {
834 act_str = strstr(string, "action");
836 ofp_fatal(str_, verbose, "must specify an action");
840 act_str = strchr(act_str + 1, '=');
842 ofp_fatal(str_, verbose, "must specify an action");
847 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
848 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
849 const struct protocol *p;
851 if (parse_protocol(name, &p)) {
852 match_set_dl_type(&fm->match, htons(p->dl_type));
854 match_set_nw_proto(&fm->match, p->nw_proto);
856 } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
857 fm->flags |= OFPFF_SEND_FLOW_REM;
858 } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
859 fm->flags |= OFPFF_CHECK_OVERLAP;
863 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
865 ofp_fatal(str_, verbose, "field %s missing value", name);
868 if (!strcmp(name, "table")) {
869 fm->table_id = str_to_table_id(value);
870 } else if (!strcmp(name, "out_port")) {
871 if (!ofputil_port_from_string(name, &fm->out_port)) {
872 ofp_fatal(str_, verbose, "%s is not a valid OpenFlow port",
875 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
876 fm->priority = str_to_u16(value, name);
877 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
878 fm->idle_timeout = str_to_u16(value, name);
879 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
880 fm->hard_timeout = str_to_u16(value, name);
881 } else if (!strcmp(name, "cookie")) {
882 char *mask = strchr(value, '/');
885 /* A mask means we're searching for a cookie. */
886 if (command == OFPFC_ADD) {
887 ofp_fatal(str_, verbose, "flow additions cannot use "
891 fm->cookie = htonll(str_to_u64(value));
892 fm->cookie_mask = htonll(str_to_u64(mask+1));
894 /* No mask means that the cookie is being set. */
895 if (command != OFPFC_ADD && command != OFPFC_MODIFY
896 && command != OFPFC_MODIFY_STRICT) {
897 ofp_fatal(str_, verbose, "cannot set cookie");
899 fm->new_cookie = htonll(str_to_u64(value));
901 } else if (mf_from_name(name)) {
902 parse_field(mf_from_name(name), value, &fm->match);
903 } else if (!strcmp(name, "duration")
904 || !strcmp(name, "n_packets")
905 || !strcmp(name, "n_bytes")) {
906 /* Ignore these, so that users can feed the output of
907 * "ovs-ofctl dump-flows" back into commands that parse
910 ofp_fatal(str_, verbose, "unknown keyword %s", name);
914 if (!fm->cookie_mask && fm->new_cookie == htonll(UINT64_MAX)
915 && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
916 /* On modifies without a mask, we are supposed to add a flow if
917 * one does not exist. If a cookie wasn't been specified, use a
918 * default of zero. */
919 fm->new_cookie = htonll(0);
921 if (fields & F_ACTIONS) {
922 struct ofpbuf ofpacts;
924 ofpbuf_init(&ofpacts, 32);
925 str_to_inst_ofpacts(&fm->match.flow, act_str, &ofpacts);
926 fm->ofpacts_len = ofpacts.size;
927 fm->ofpacts = ofpbuf_steal_data(&ofpacts);
936 /* Convert 'str_' (as described in the documentation for the "monitor" command
937 * in the ovs-ofctl man page) into 'fmr'. */
939 parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
944 char *string = xstrdup(str_);
945 char *save_ptr = NULL;
949 fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
950 | NXFMF_OWN | NXFMF_ACTIONS);
951 fmr->out_port = OFPP_NONE;
952 fmr->table_id = 0xff;
953 match_init_catchall(&fmr->match);
955 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
956 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
957 const struct protocol *p;
959 if (!strcmp(name, "!initial")) {
960 fmr->flags &= ~NXFMF_INITIAL;
961 } else if (!strcmp(name, "!add")) {
962 fmr->flags &= ~NXFMF_ADD;
963 } else if (!strcmp(name, "!delete")) {
964 fmr->flags &= ~NXFMF_DELETE;
965 } else if (!strcmp(name, "!modify")) {
966 fmr->flags &= ~NXFMF_MODIFY;
967 } else if (!strcmp(name, "!actions")) {
968 fmr->flags &= ~NXFMF_ACTIONS;
969 } else if (!strcmp(name, "!own")) {
970 fmr->flags &= ~NXFMF_OWN;
971 } else if (parse_protocol(name, &p)) {
972 match_set_dl_type(&fmr->match, htons(p->dl_type));
974 match_set_nw_proto(&fmr->match, p->nw_proto);
979 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
981 ovs_fatal(0, "%s: field %s missing value", str_, name);
984 if (!strcmp(name, "table")) {
985 fmr->table_id = str_to_table_id(value);
986 } else if (!strcmp(name, "out_port")) {
987 fmr->out_port = atoi(value);
988 } else if (mf_from_name(name)) {
989 parse_field(mf_from_name(name), value, &fmr->match);
991 ovs_fatal(0, "%s: unknown keyword %s", str_, name);
998 /* Parses 's' as a set of OpenFlow actions and appends the actions to
1001 * Prints an error on stderr and aborts the program if 's' syntax is
1004 parse_ofpacts(const char *s_, struct ofpbuf *ofpacts)
1006 char *s = xstrdup(s_);
1007 str_to_ofpacts(NULL, s, ofpacts);
1011 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
1012 * (one of OFPFC_*) into 'fm'. */
1014 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
1015 uint16_t command, bool verbose)
1017 struct match match_copy;
1019 parse_ofp_str(fm, command, string, verbose);
1021 /* Normalize a copy of the match. This ensures that non-normalized flows
1022 * get logged but doesn't affect what gets sent to the switch, so that the
1023 * switch can do whatever it likes with the flow. */
1024 match_copy = fm->match;
1025 ofputil_normalize_match(&match_copy);
1029 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
1030 struct ofputil_flow_mod **fms, size_t *n_fms)
1032 size_t allocated_fms;
1036 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1037 if (stream == NULL) {
1038 ovs_fatal(errno, "%s: open", file_name);
1041 allocated_fms = *n_fms;
1043 while (!ds_get_preprocessed_line(&s, stream)) {
1044 if (*n_fms >= allocated_fms) {
1045 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
1047 parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command, false);
1052 if (stream != stdin) {
1058 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
1059 bool aggregate, const char *string)
1061 struct ofputil_flow_mod fm;
1063 parse_ofp_str(&fm, -1, string, false);
1064 fsr->aggregate = aggregate;
1065 fsr->cookie = fm.cookie;
1066 fsr->cookie_mask = fm.cookie_mask;
1067 fsr->match = fm.match;
1068 fsr->out_port = fm.out_port;
1069 fsr->table_id = fm.table_id;
1072 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
1073 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1074 * mf_field. Fields must be specified in a natural order for satisfying
1077 * Returns NULL on success, otherwise a malloc()'d string that explains the
1080 parse_ofp_exact_flow(struct flow *flow, const char *s)
1082 char *pos, *key, *value_s;
1086 memset(flow, 0, sizeof *flow);
1088 pos = copy = xstrdup(s);
1089 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
1090 const struct protocol *p;
1091 if (parse_protocol(key, &p)) {
1092 if (flow->dl_type) {
1093 error = xasprintf("%s: Ethernet type set multiple times", s);
1096 flow->dl_type = htons(p->dl_type);
1099 if (flow->nw_proto) {
1100 error = xasprintf("%s: network protocol set "
1101 "multiple times", s);
1104 flow->nw_proto = p->nw_proto;
1107 const struct mf_field *mf;
1108 union mf_value value;
1111 mf = mf_from_name(key);
1113 error = xasprintf("%s: unknown field %s", s, key);
1117 if (!mf_are_prereqs_ok(mf, flow)) {
1118 error = xasprintf("%s: prerequisites not met for setting %s",
1123 if (!mf_is_zero(mf, flow)) {
1124 error = xasprintf("%s: field %s set multiple times", s, key);
1128 field_error = mf_parse_value(mf, value_s, &value);
1130 error = xasprintf("%s: bad value for %s (%s)",
1131 s, key, field_error);
1136 mf_set_flow_value(mf, &value, flow);
1144 memset(flow, 0, sizeof *flow);