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"
32 #include "multipath.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;
125 static struct ofp_action_output *
126 put_output_action(struct ofpbuf *b, uint16_t port)
128 struct ofp_action_output *oao;
130 oao = ofputil_put_OFPAT10_OUTPUT(b);
131 oao->port = htons(port);
136 parse_enqueue(struct ofpbuf *b, char *arg)
139 char *port = strtok_r(arg, ":q", &sp);
140 char *queue = strtok_r(NULL, "", &sp);
141 struct ofp_action_enqueue *oae;
143 if (port == NULL || queue == NULL) {
144 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
147 oae = ofputil_put_OFPAT10_ENQUEUE(b);
148 oae->port = htons(str_to_u32(port));
149 oae->queue_id = htonl(str_to_u32(queue));
153 parse_output(struct ofpbuf *b, char *arg)
155 if (strchr(arg, '[')) {
156 struct nx_action_output_reg *naor;
157 struct mf_subfield src;
159 mf_parse_subfield(&src, arg);
161 naor = ofputil_put_NXAST_OUTPUT_REG(b);
162 naor->ofs_nbits = nxm_encode_ofs_nbits(src.ofs, src.n_bits);
163 naor->src = htonl(src.field->nxm_header);
164 naor->max_len = htons(UINT16_MAX);
166 put_output_action(b, str_to_u32(arg));
171 parse_resubmit(struct ofpbuf *b, char *arg)
173 struct nx_action_resubmit *nar;
174 char *in_port_s, *table_s;
178 in_port_s = strsep(&arg, ",");
179 if (in_port_s && in_port_s[0]) {
180 if (!ofputil_port_from_string(in_port_s, &in_port)) {
181 in_port = str_to_u32(in_port_s);
184 in_port = OFPP_IN_PORT;
187 table_s = strsep(&arg, ",");
188 table = table_s && table_s[0] ? str_to_u32(table_s) : 255;
190 if (in_port == OFPP_IN_PORT && table == 255) {
191 ovs_fatal(0, "at least one \"in_port\" or \"table\" must be specified "
195 if (in_port != OFPP_IN_PORT && table == 255) {
196 nar = ofputil_put_NXAST_RESUBMIT(b);
198 nar = ofputil_put_NXAST_RESUBMIT_TABLE(b);
201 nar->in_port = htons(in_port);
205 parse_set_tunnel(struct ofpbuf *b, const char *arg)
207 uint64_t tun_id = str_to_u64(arg);
208 if (tun_id > UINT32_MAX) {
209 ofputil_put_NXAST_SET_TUNNEL64(b)->tun_id = htonll(tun_id);
211 ofputil_put_NXAST_SET_TUNNEL(b)->tun_id = htonl(tun_id);
216 parse_note(struct ofpbuf *b, const char *arg)
218 size_t start_ofs = b->size;
219 struct nx_action_note *nan;
223 nan = ofputil_put_NXAST_NOTE(b);
225 b->size -= sizeof nan->note;
226 while (*arg != '\0') {
237 byte = hexits_value(arg, 2, &ok);
239 ovs_fatal(0, "bad hex digit in `note' argument");
241 ofpbuf_put(b, &byte, 1);
246 len = b->size - start_ofs;
247 remainder = len % OFP_ACTION_ALIGN;
249 ofpbuf_put_zeros(b, OFP_ACTION_ALIGN - remainder);
251 nan = (struct nx_action_note *)((char *)b->data + start_ofs);
252 nan->len = htons(b->size - start_ofs);
256 parse_fin_timeout(struct ofpbuf *b, char *arg)
258 struct nx_action_fin_timeout *naft;
261 naft = ofputil_put_NXAST_FIN_TIMEOUT(b);
262 while (ofputil_parse_key_value(&arg, &key, &value)) {
263 if (!strcmp(key, "idle_timeout")) {
264 naft->fin_idle_timeout = htons(str_to_u16(value, key));
265 } else if (!strcmp(key, "hard_timeout")) {
266 naft->fin_hard_timeout = htons(str_to_u16(value, key));
268 ovs_fatal(0, "invalid key '%s' in 'fin_timeout' argument", key);
274 parse_controller(struct ofpbuf *b, char *arg)
276 enum ofp_packet_in_reason reason = OFPR_ACTION;
277 uint16_t controller_id = 0;
278 uint16_t max_len = UINT16_MAX;
282 } else if (strspn(arg, "0123456789") == strlen(arg)) {
283 max_len = str_to_u16(arg, "max_len");
287 while (ofputil_parse_key_value(&arg, &name, &value)) {
288 if (!strcmp(name, "reason")) {
289 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
290 ovs_fatal(0, "unknown reason \"%s\"", value);
292 } else if (!strcmp(name, "max_len")) {
293 max_len = str_to_u16(value, "max_len");
294 } else if (!strcmp(name, "id")) {
295 controller_id = str_to_u16(value, "id");
297 ovs_fatal(0, "unknown key \"%s\" parsing controller action",
303 if (reason == OFPR_ACTION && controller_id == 0) {
304 put_output_action(b, OFPP_CONTROLLER)->max_len = htons(max_len);
306 struct nx_action_controller *nac;
308 nac = ofputil_put_NXAST_CONTROLLER(b);
309 nac->max_len = htons(max_len);
310 nac->reason = reason;
311 nac->controller_id = htons(controller_id);
316 parse_named_action(enum ofputil_action_code code, const struct flow *flow,
317 struct ofpbuf *b, char *arg)
319 struct ofp_action_dl_addr *oada;
320 struct ofp_action_vlan_pcp *oavp;
321 struct ofp_action_vlan_vid *oavv;
322 struct ofp_action_nw_addr *oana;
323 struct ofp_action_tp_port *oata;
326 case OFPUTIL_OFPAT10_OUTPUT:
327 parse_output(b, arg);
330 case OFPUTIL_OFPAT10_SET_VLAN_VID:
331 oavv = ofputil_put_OFPAT10_SET_VLAN_VID(b);
332 oavv->vlan_vid = htons(str_to_u32(arg));
335 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
336 oavp = ofputil_put_OFPAT10_SET_VLAN_PCP(b);
337 oavp->vlan_pcp = str_to_u32(arg);
340 case OFPUTIL_OFPAT10_STRIP_VLAN:
341 ofputil_put_OFPAT10_STRIP_VLAN(b);
344 case OFPUTIL_OFPAT10_SET_DL_SRC:
345 case OFPUTIL_OFPAT10_SET_DL_DST:
346 oada = ofputil_put_action(code, b);
347 str_to_mac(arg, oada->dl_addr);
350 case OFPUTIL_OFPAT10_SET_NW_SRC:
351 case OFPUTIL_OFPAT10_SET_NW_DST:
352 oana = ofputil_put_action(code, b);
353 str_to_ip(arg, &oana->nw_addr);
356 case OFPUTIL_OFPAT10_SET_NW_TOS:
357 ofputil_put_OFPAT10_SET_NW_TOS(b)->nw_tos = str_to_u32(arg);
360 case OFPUTIL_OFPAT10_SET_TP_SRC:
361 case OFPUTIL_OFPAT10_SET_TP_DST:
362 oata = ofputil_put_action(code, b);
363 oata->tp_port = htons(str_to_u32(arg));
366 case OFPUTIL_OFPAT10_ENQUEUE:
367 parse_enqueue(b, arg);
370 case OFPUTIL_NXAST_RESUBMIT:
371 parse_resubmit(b, arg);
374 case OFPUTIL_NXAST_SET_TUNNEL:
375 parse_set_tunnel(b, arg);
378 case OFPUTIL_NXAST_SET_QUEUE:
379 ofputil_put_NXAST_SET_QUEUE(b)->queue_id = htonl(str_to_u32(arg));
382 case OFPUTIL_NXAST_POP_QUEUE:
383 ofputil_put_NXAST_POP_QUEUE(b);
386 case OFPUTIL_NXAST_REG_MOVE:
387 nxm_parse_reg_move(ofputil_put_NXAST_REG_MOVE(b), arg);
390 case OFPUTIL_NXAST_REG_LOAD:
391 nxm_parse_reg_load(ofputil_put_NXAST_REG_LOAD(b), arg);
394 case OFPUTIL_NXAST_NOTE:
398 case OFPUTIL_NXAST_SET_TUNNEL64:
399 ofputil_put_NXAST_SET_TUNNEL64(b)->tun_id = htonll(str_to_u64(arg));
402 case OFPUTIL_NXAST_MULTIPATH:
403 multipath_parse(ofputil_put_NXAST_MULTIPATH(b), arg);
406 case OFPUTIL_NXAST_AUTOPATH:
407 autopath_parse(ofputil_put_NXAST_AUTOPATH(b), arg);
410 case OFPUTIL_NXAST_BUNDLE:
411 bundle_parse(b, arg);
414 case OFPUTIL_NXAST_BUNDLE_LOAD:
415 bundle_parse_load(b, arg);
418 case OFPUTIL_NXAST_RESUBMIT_TABLE:
419 case OFPUTIL_NXAST_OUTPUT_REG:
422 case OFPUTIL_NXAST_LEARN:
423 learn_parse(b, arg, flow);
426 case OFPUTIL_NXAST_EXIT:
427 ofputil_put_NXAST_EXIT(b);
430 case OFPUTIL_NXAST_DEC_TTL:
431 ofputil_put_NXAST_DEC_TTL(b);
434 case OFPUTIL_NXAST_FIN_TIMEOUT:
435 parse_fin_timeout(b, arg);
438 case OFPUTIL_NXAST_CONTROLLER:
439 parse_controller(b, arg);
445 str_to_action(const struct flow *flow, char *str, struct ofpbuf *b)
447 char *pos, *act, *arg;
452 while (ofputil_parse_key_value(&pos, &act, &arg)) {
456 code = ofputil_action_code_from_name(act);
458 parse_named_action(code, flow, b, arg);
459 } else if (!strcasecmp(act, "drop")) {
460 /* A drop action in OpenFlow occurs by just not setting
463 ovs_fatal(0, "Drop actions must not be preceded by other "
465 } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
466 ovs_fatal(0, "Drop actions must not be followed by other "
470 } else if (ofputil_port_from_string(act, &port)) {
471 put_output_action(b, port);
473 ovs_fatal(0, "Unknown action: %s", act);
486 parse_protocol(const char *name, const struct protocol **p_out)
488 static const struct protocol protocols[] = {
489 { "ip", ETH_TYPE_IP, 0 },
490 { "arp", ETH_TYPE_ARP, 0 },
491 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
492 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
493 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
494 { "ipv6", ETH_TYPE_IPV6, 0 },
495 { "ip6", ETH_TYPE_IPV6, 0 },
496 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
497 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
498 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
500 const struct protocol *p;
502 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
503 if (!strcmp(p->name, name)) {
513 ofp_fatal(const char *flow, bool verbose, const char *format, ...)
518 fprintf(stderr, "%s:\n", flow);
521 va_start(args, format);
522 ovs_fatal_valist(0, format, args);
526 parse_field(const struct mf_field *mf, const char *s, struct cls_rule *rule)
528 union mf_value value, mask;
531 error = mf_parse(mf, s, &value, &mask);
533 ovs_fatal(0, "%s", error);
536 mf_set(mf, &value, &mask, rule);
539 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
540 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
541 * If 'actions' is specified, an action must be in 'string' and may be expanded
544 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
545 * constant for 'command'. To parse syntax for an OFPST_FLOW or
546 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
548 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
558 char *string = xstrdup(str_);
559 char *save_ptr = NULL;
560 char *act_str = NULL;
569 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
576 case OFPFC_DELETE_STRICT:
577 fields = F_OUT_PORT | F_PRIORITY;
581 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
584 case OFPFC_MODIFY_STRICT:
585 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
592 cls_rule_init_catchall(&fm->cr, OFP_DEFAULT_PRIORITY);
593 fm->cookie = htonll(0);
594 fm->cookie_mask = htonll(0);
595 if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
596 /* For modify, by default, don't update the cookie. */
597 fm->new_cookie = htonll(UINT64_MAX);
599 fm->new_cookie = htonll(0);
602 fm->command = command;
603 fm->idle_timeout = OFP_FLOW_PERMANENT;
604 fm->hard_timeout = OFP_FLOW_PERMANENT;
605 fm->buffer_id = UINT32_MAX;
606 fm->out_port = OFPP_NONE;
608 if (fields & F_ACTIONS) {
609 act_str = strstr(string, "action");
611 ofp_fatal(str_, verbose, "must specify an action");
615 act_str = strchr(act_str + 1, '=');
617 ofp_fatal(str_, verbose, "must specify an action");
622 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
623 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
624 const struct protocol *p;
626 if (parse_protocol(name, &p)) {
627 cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
629 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
631 } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
632 fm->flags |= OFPFF_SEND_FLOW_REM;
633 } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
634 fm->flags |= OFPFF_CHECK_OVERLAP;
638 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
640 ofp_fatal(str_, verbose, "field %s missing value", name);
643 if (!strcmp(name, "table")) {
644 fm->table_id = str_to_table_id(value);
645 } else if (!strcmp(name, "out_port")) {
646 fm->out_port = atoi(value);
647 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
648 fm->cr.priority = str_to_u16(value, name);
649 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
650 fm->idle_timeout = str_to_u16(value, name);
651 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
652 fm->hard_timeout = str_to_u16(value, name);
653 } else if (!strcmp(name, "cookie")) {
654 char *mask = strchr(value, '/');
657 /* A mask means we're searching for a cookie. */
658 if (command == OFPFC_ADD) {
659 ofp_fatal(str_, verbose, "flow additions cannot use "
663 fm->cookie = htonll(str_to_u64(value));
664 fm->cookie_mask = htonll(str_to_u64(mask+1));
666 /* No mask means that the cookie is being set. */
667 if (command != OFPFC_ADD && command != OFPFC_MODIFY
668 && command != OFPFC_MODIFY_STRICT) {
669 ofp_fatal(str_, verbose, "cannot set cookie");
671 fm->new_cookie = htonll(str_to_u64(value));
673 } else if (mf_from_name(name)) {
674 parse_field(mf_from_name(name), value, &fm->cr);
675 } else if (!strcmp(name, "duration")
676 || !strcmp(name, "n_packets")
677 || !strcmp(name, "n_bytes")) {
678 /* Ignore these, so that users can feed the output of
679 * "ovs-ofctl dump-flows" back into commands that parse
682 ofp_fatal(str_, verbose, "unknown keyword %s", name);
686 if (!fm->cookie_mask && fm->new_cookie == htonll(UINT64_MAX)
687 && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
688 /* On modifies without a mask, we are supposed to add a flow if
689 * one does not exist. If a cookie wasn't been specified, use a
690 * default of zero. */
691 fm->new_cookie = htonll(0);
693 if (fields & F_ACTIONS) {
694 struct ofpbuf actions;
696 ofpbuf_init(&actions, sizeof(union ofp_action));
697 str_to_action(&fm->cr.flow, act_str, &actions);
698 fm->actions = ofpbuf_steal_data(&actions);
699 fm->n_actions = actions.size / sizeof(union ofp_action);
708 /* Parses 's' as a set of OpenFlow actions and appends the actions to
711 * Prints an error on stderr and aborts the program if 's' syntax is
714 parse_ofp_actions(const char *s_, struct ofpbuf *actions)
716 char *s = xstrdup(s_);
717 str_to_action(NULL, s, actions);
721 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
722 * (one of OFPFC_*) into 'fm'. */
724 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
725 uint16_t command, bool verbose)
727 struct cls_rule rule_copy;
729 parse_ofp_str(fm, command, string, verbose);
731 /* Normalize a copy of the rule. This ensures that non-normalized flows
732 * get logged but doesn't affect what gets sent to the switch, so that the
733 * switch can do whatever it likes with the flow. */
735 ofputil_normalize_rule(&rule_copy);
739 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
740 struct ofputil_flow_mod **fms, size_t *n_fms)
742 size_t allocated_fms;
746 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
747 if (stream == NULL) {
748 ovs_fatal(errno, "%s: open", file_name);
751 allocated_fms = *n_fms;
753 while (!ds_get_preprocessed_line(&s, stream)) {
754 if (*n_fms >= allocated_fms) {
755 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
757 parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command, false);
762 if (stream != stdin) {
768 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
769 bool aggregate, const char *string)
771 struct ofputil_flow_mod fm;
773 parse_ofp_str(&fm, -1, string, false);
774 fsr->aggregate = aggregate;
775 fsr->cookie = fm.cookie;
776 fsr->cookie_mask = fm.cookie_mask;
778 fsr->out_port = fm.out_port;
779 fsr->table_id = fm.table_id;
782 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
783 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
784 * mf_field. Fields must be specified in a natural order for satisfying
787 * Returns NULL on success, otherwise a malloc()'d string that explains the
790 parse_ofp_exact_flow(struct flow *flow, const char *s)
792 char *pos, *key, *value_s;
796 memset(flow, 0, sizeof *flow);
798 pos = copy = xstrdup(s);
799 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
800 const struct protocol *p;
801 if (parse_protocol(key, &p)) {
803 error = xasprintf("%s: Ethernet type set multiple times", s);
806 flow->dl_type = htons(p->dl_type);
809 if (flow->nw_proto) {
810 error = xasprintf("%s: network protocol set "
811 "multiple times", s);
814 flow->nw_proto = p->nw_proto;
817 const struct mf_field *mf;
818 union mf_value value;
821 mf = mf_from_name(key);
823 error = xasprintf("%s: unknown field %s", s, key);
827 if (!mf_are_prereqs_ok(mf, flow)) {
828 error = xasprintf("%s: prerequisites not met for setting %s",
833 if (!mf_is_zero(mf, flow)) {
834 error = xasprintf("%s: field %s set multiple times", s, key);
838 field_error = mf_parse_value(mf, value_s, &value);
840 error = xasprintf("%s: bad value for %s (%s)",
841 s, key, field_error);
846 mf_set_flow_value(mf, &value, flow);
854 memset(flow, 0, sizeof *flow);