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 resubmit->in_port = str_to_u32(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_named_action(enum ofputil_action_code code, const struct flow *flow,
283 char *arg, struct ofpbuf *ofpacts)
285 struct ofpact_tunnel *tunnel;
292 case OFPUTIL_ACTION_INVALID:
295 case OFPUTIL_OFPAT10_OUTPUT:
296 parse_output(arg, ofpacts);
299 case OFPUTIL_OFPAT10_SET_VLAN_VID:
300 vid = str_to_u32(arg);
301 if (vid & ~VLAN_VID_MASK) {
302 ovs_fatal(0, "%s: not a valid VLAN VID", arg);
304 ofpact_put_SET_VLAN_VID(ofpacts)->vlan_vid = vid;
307 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
308 pcp = str_to_u32(arg);
310 ovs_fatal(0, "%s: not a valid VLAN PCP", arg);
312 ofpact_put_SET_VLAN_PCP(ofpacts)->vlan_pcp = pcp;
315 case OFPUTIL_OFPAT10_STRIP_VLAN:
316 ofpact_put_STRIP_VLAN(ofpacts);
319 case OFPUTIL_OFPAT10_SET_DL_SRC:
320 str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
323 case OFPUTIL_OFPAT10_SET_DL_DST:
324 str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
327 case OFPUTIL_OFPAT10_SET_NW_SRC:
329 ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4 = ip;
332 case OFPUTIL_OFPAT10_SET_NW_DST:
334 ofpact_put_SET_IPV4_DST(ofpacts)->ipv4 = ip;
337 case OFPUTIL_OFPAT10_SET_NW_TOS:
338 tos = str_to_u32(arg);
339 if (tos & ~IP_DSCP_MASK) {
340 ovs_fatal(0, "%s: not a valid TOS", arg);
342 ofpact_put_SET_IPV4_DSCP(ofpacts)->dscp = tos;
345 case OFPUTIL_OFPAT10_SET_TP_SRC:
346 ofpact_put_SET_L4_SRC_PORT(ofpacts)->port = str_to_u32(arg);
349 case OFPUTIL_OFPAT10_SET_TP_DST:
350 ofpact_put_SET_L4_DST_PORT(ofpacts)->port = str_to_u32(arg);
353 case OFPUTIL_OFPAT10_ENQUEUE:
354 parse_enqueue(arg, ofpacts);
357 case OFPUTIL_NXAST_RESUBMIT:
358 parse_resubmit(arg, ofpacts);
361 case OFPUTIL_NXAST_SET_TUNNEL:
362 case OFPUTIL_NXAST_SET_TUNNEL64:
363 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
364 tunnel->ofpact.compat = code;
365 tunnel->tun_id = str_to_u64(arg);
368 case OFPUTIL_NXAST_SET_QUEUE:
369 ofpact_put_SET_QUEUE(ofpacts)->queue_id = str_to_u32(arg);
372 case OFPUTIL_NXAST_POP_QUEUE:
373 ofpact_put_POP_QUEUE(ofpacts);
376 case OFPUTIL_NXAST_REG_MOVE:
377 nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts), arg);
380 case OFPUTIL_NXAST_REG_LOAD:
381 nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts), arg);
384 case OFPUTIL_NXAST_NOTE:
385 parse_note(arg, ofpacts);
388 case OFPUTIL_NXAST_MULTIPATH:
389 multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
392 case OFPUTIL_NXAST_AUTOPATH:
393 autopath_parse(ofpact_put_AUTOPATH(ofpacts), arg);
396 case OFPUTIL_NXAST_BUNDLE:
397 bundle_parse(arg, ofpacts);
400 case OFPUTIL_NXAST_BUNDLE_LOAD:
401 bundle_parse_load(arg, ofpacts);
404 case OFPUTIL_NXAST_RESUBMIT_TABLE:
405 case OFPUTIL_NXAST_OUTPUT_REG:
408 case OFPUTIL_NXAST_LEARN:
409 learn_parse(arg, flow, ofpacts);
412 case OFPUTIL_NXAST_EXIT:
413 ofpact_put_EXIT(ofpacts);
416 case OFPUTIL_NXAST_DEC_TTL:
417 ofpact_put_DEC_TTL(ofpacts);
420 case OFPUTIL_NXAST_FIN_TIMEOUT:
421 parse_fin_timeout(ofpacts, arg);
424 case OFPUTIL_NXAST_CONTROLLER:
425 parse_controller(ofpacts, arg);
431 str_to_ofpacts(const struct flow *flow, char *str, struct ofpbuf *ofpacts)
433 char *pos, *act, *arg;
438 while (ofputil_parse_key_value(&pos, &act, &arg)) {
442 code = ofputil_action_code_from_name(act);
444 parse_named_action(code, flow, arg, ofpacts);
445 } else if (!strcasecmp(act, "drop")) {
447 ovs_fatal(0, "Drop actions must not be preceded by other "
449 } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
450 ovs_fatal(0, "Drop actions must not be followed by other "
454 } else if (ofputil_port_from_string(act, &port)) {
455 ofpact_put_OUTPUT(ofpacts)->port = port;
457 ovs_fatal(0, "Unknown action: %s", act);
471 parse_protocol(const char *name, const struct protocol **p_out)
473 static const struct protocol protocols[] = {
474 { "ip", ETH_TYPE_IP, 0 },
475 { "arp", ETH_TYPE_ARP, 0 },
476 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
477 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
478 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
479 { "ipv6", ETH_TYPE_IPV6, 0 },
480 { "ip6", ETH_TYPE_IPV6, 0 },
481 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
482 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
483 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
485 const struct protocol *p;
487 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
488 if (!strcmp(p->name, name)) {
498 ofp_fatal(const char *flow, bool verbose, const char *format, ...)
503 fprintf(stderr, "%s:\n", flow);
506 va_start(args, format);
507 ovs_fatal_valist(0, format, args);
511 parse_field(const struct mf_field *mf, const char *s, struct cls_rule *rule)
513 union mf_value value, mask;
516 error = mf_parse(mf, s, &value, &mask);
518 ovs_fatal(0, "%s", error);
521 mf_set(mf, &value, &mask, rule);
524 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
525 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
526 * If 'actions' is specified, an action must be in 'string' and may be expanded
529 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
530 * constant for 'command'. To parse syntax for an OFPST_FLOW or
531 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
533 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
543 char *string = xstrdup(str_);
544 char *save_ptr = NULL;
545 char *act_str = NULL;
554 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
561 case OFPFC_DELETE_STRICT:
562 fields = F_OUT_PORT | F_PRIORITY;
566 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
569 case OFPFC_MODIFY_STRICT:
570 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
577 cls_rule_init_catchall(&fm->cr, OFP_DEFAULT_PRIORITY);
578 fm->cookie = htonll(0);
579 fm->cookie_mask = htonll(0);
580 if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
581 /* For modify, by default, don't update the cookie. */
582 fm->new_cookie = htonll(UINT64_MAX);
584 fm->new_cookie = htonll(0);
587 fm->command = command;
588 fm->idle_timeout = OFP_FLOW_PERMANENT;
589 fm->hard_timeout = OFP_FLOW_PERMANENT;
590 fm->buffer_id = UINT32_MAX;
591 fm->out_port = OFPP_NONE;
593 if (fields & F_ACTIONS) {
594 act_str = strstr(string, "action");
596 ofp_fatal(str_, verbose, "must specify an action");
600 act_str = strchr(act_str + 1, '=');
602 ofp_fatal(str_, verbose, "must specify an action");
607 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
608 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
609 const struct protocol *p;
611 if (parse_protocol(name, &p)) {
612 cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
614 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
616 } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
617 fm->flags |= OFPFF_SEND_FLOW_REM;
618 } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
619 fm->flags |= OFPFF_CHECK_OVERLAP;
623 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
625 ofp_fatal(str_, verbose, "field %s missing value", name);
628 if (!strcmp(name, "table")) {
629 fm->table_id = str_to_table_id(value);
630 } else if (!strcmp(name, "out_port")) {
631 fm->out_port = atoi(value);
632 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
633 fm->cr.priority = str_to_u16(value, name);
634 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
635 fm->idle_timeout = str_to_u16(value, name);
636 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
637 fm->hard_timeout = str_to_u16(value, name);
638 } else if (!strcmp(name, "cookie")) {
639 char *mask = strchr(value, '/');
642 /* A mask means we're searching for a cookie. */
643 if (command == OFPFC_ADD) {
644 ofp_fatal(str_, verbose, "flow additions cannot use "
648 fm->cookie = htonll(str_to_u64(value));
649 fm->cookie_mask = htonll(str_to_u64(mask+1));
651 /* No mask means that the cookie is being set. */
652 if (command != OFPFC_ADD && command != OFPFC_MODIFY
653 && command != OFPFC_MODIFY_STRICT) {
654 ofp_fatal(str_, verbose, "cannot set cookie");
656 fm->new_cookie = htonll(str_to_u64(value));
658 } else if (mf_from_name(name)) {
659 parse_field(mf_from_name(name), value, &fm->cr);
660 } else if (!strcmp(name, "duration")
661 || !strcmp(name, "n_packets")
662 || !strcmp(name, "n_bytes")) {
663 /* Ignore these, so that users can feed the output of
664 * "ovs-ofctl dump-flows" back into commands that parse
667 ofp_fatal(str_, verbose, "unknown keyword %s", name);
671 if (!fm->cookie_mask && fm->new_cookie == htonll(UINT64_MAX)
672 && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
673 /* On modifies without a mask, we are supposed to add a flow if
674 * one does not exist. If a cookie wasn't been specified, use a
675 * default of zero. */
676 fm->new_cookie = htonll(0);
678 if (fields & F_ACTIONS) {
679 struct ofpbuf ofpacts;
681 ofpbuf_init(&ofpacts, 32);
682 str_to_ofpacts(&fm->cr.flow, act_str, &ofpacts);
683 fm->ofpacts_len = ofpacts.size;
684 fm->ofpacts = ofpbuf_steal_data(&ofpacts);
693 /* Parses 's' as a set of OpenFlow actions and appends the actions to
696 * Prints an error on stderr and aborts the program if 's' syntax is
699 parse_ofpacts(const char *s_, struct ofpbuf *ofpacts)
701 char *s = xstrdup(s_);
702 str_to_ofpacts(NULL, s, ofpacts);
706 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
707 * (one of OFPFC_*) into 'fm'. */
709 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
710 uint16_t command, bool verbose)
712 struct cls_rule rule_copy;
714 parse_ofp_str(fm, command, string, verbose);
716 /* Normalize a copy of the rule. This ensures that non-normalized flows
717 * get logged but doesn't affect what gets sent to the switch, so that the
718 * switch can do whatever it likes with the flow. */
720 ofputil_normalize_rule(&rule_copy);
724 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
725 struct ofputil_flow_mod **fms, size_t *n_fms)
727 size_t allocated_fms;
731 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
732 if (stream == NULL) {
733 ovs_fatal(errno, "%s: open", file_name);
736 allocated_fms = *n_fms;
738 while (!ds_get_preprocessed_line(&s, stream)) {
739 if (*n_fms >= allocated_fms) {
740 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
742 parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command, false);
747 if (stream != stdin) {
753 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
754 bool aggregate, const char *string)
756 struct ofputil_flow_mod fm;
758 parse_ofp_str(&fm, -1, string, false);
759 fsr->aggregate = aggregate;
760 fsr->cookie = fm.cookie;
761 fsr->cookie_mask = fm.cookie_mask;
763 fsr->out_port = fm.out_port;
764 fsr->table_id = fm.table_id;
767 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
768 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
769 * mf_field. Fields must be specified in a natural order for satisfying
772 * Returns NULL on success, otherwise a malloc()'d string that explains the
775 parse_ofp_exact_flow(struct flow *flow, const char *s)
777 char *pos, *key, *value_s;
781 memset(flow, 0, sizeof *flow);
783 pos = copy = xstrdup(s);
784 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
785 const struct protocol *p;
786 if (parse_protocol(key, &p)) {
788 error = xasprintf("%s: Ethernet type set multiple times", s);
791 flow->dl_type = htons(p->dl_type);
794 if (flow->nw_proto) {
795 error = xasprintf("%s: network protocol set "
796 "multiple times", s);
799 flow->nw_proto = p->nw_proto;
802 const struct mf_field *mf;
803 union mf_value value;
806 mf = mf_from_name(key);
808 error = xasprintf("%s: unknown field %s", s, key);
812 if (!mf_are_prereqs_ok(mf, flow)) {
813 error = xasprintf("%s: prerequisites not met for setting %s",
818 if (!mf_is_zero(mf, flow)) {
819 error = xasprintf("%s: field %s set multiple times", s, key);
823 field_error = mf_parse_value(mf, value_s, &value);
825 error = xasprintf("%s: bad value for %s (%s)",
826 s, key, field_error);
831 mf_set_flow_value(mf, &value, flow);
839 memset(flow, 0, sizeof *flow);