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);
596 fm->command = command;
597 fm->idle_timeout = OFP_FLOW_PERMANENT;
598 fm->hard_timeout = OFP_FLOW_PERMANENT;
599 fm->buffer_id = UINT32_MAX;
600 fm->out_port = OFPP_NONE;
602 if (fields & F_ACTIONS) {
603 act_str = strstr(string, "action");
605 ofp_fatal(str_, verbose, "must specify an action");
609 act_str = strchr(act_str + 1, '=');
611 ofp_fatal(str_, verbose, "must specify an action");
616 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
617 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
618 const struct protocol *p;
620 if (parse_protocol(name, &p)) {
621 cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
623 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
625 } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
626 fm->flags |= OFPFF_SEND_FLOW_REM;
627 } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
628 fm->flags |= OFPFF_CHECK_OVERLAP;
632 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
634 ofp_fatal(str_, verbose, "field %s missing value", name);
637 if (!strcmp(name, "table")) {
638 fm->table_id = str_to_table_id(value);
639 } else if (!strcmp(name, "out_port")) {
640 fm->out_port = atoi(value);
641 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
642 fm->cr.priority = str_to_u16(value, name);
643 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
644 fm->idle_timeout = str_to_u16(value, name);
645 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
646 fm->hard_timeout = str_to_u16(value, name);
647 } else if (!strcmp(name, "cookie")) {
648 char *mask = strchr(value, '/');
650 if (command == OFPFC_ADD) {
651 ofp_fatal(str_, verbose, "flow additions cannot use "
655 fm->cookie_mask = htonll(str_to_u64(mask+1));
657 fm->cookie_mask = htonll(UINT64_MAX);
659 fm->cookie = htonll(str_to_u64(value));
660 } else if (mf_from_name(name)) {
661 parse_field(mf_from_name(name), value, &fm->cr);
662 } else if (!strcmp(name, "duration")
663 || !strcmp(name, "n_packets")
664 || !strcmp(name, "n_bytes")) {
665 /* Ignore these, so that users can feed the output of
666 * "ovs-ofctl dump-flows" back into commands that parse
669 ofp_fatal(str_, verbose, "unknown keyword %s", name);
673 if (fields & F_ACTIONS) {
674 struct ofpbuf actions;
676 ofpbuf_init(&actions, sizeof(union ofp_action));
677 str_to_action(&fm->cr.flow, act_str, &actions);
678 fm->actions = ofpbuf_steal_data(&actions);
679 fm->n_actions = actions.size / sizeof(union ofp_action);
688 /* Parses 's' as a set of OpenFlow actions and appends the actions to
691 * Prints an error on stderr and aborts the program if 's' syntax is
694 parse_ofp_actions(const char *s_, struct ofpbuf *actions)
696 char *s = xstrdup(s_);
697 str_to_action(NULL, s, actions);
701 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
702 * (one of OFPFC_*) into 'fm'. */
704 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
705 uint16_t command, bool verbose)
707 struct cls_rule rule_copy;
709 parse_ofp_str(fm, command, string, verbose);
711 /* Normalize a copy of the rule. This ensures that non-normalized flows
712 * get logged but doesn't affect what gets sent to the switch, so that the
713 * switch can do whatever it likes with the flow. */
715 ofputil_normalize_rule(&rule_copy);
719 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
720 struct ofputil_flow_mod **fms, size_t *n_fms)
722 size_t allocated_fms;
726 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
727 if (stream == NULL) {
728 ovs_fatal(errno, "%s: open", file_name);
731 allocated_fms = *n_fms;
733 while (!ds_get_preprocessed_line(&s, stream)) {
734 if (*n_fms >= allocated_fms) {
735 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
737 parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command, false);
742 if (stream != stdin) {
748 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
749 bool aggregate, const char *string)
751 struct ofputil_flow_mod fm;
753 parse_ofp_str(&fm, -1, string, false);
754 fsr->aggregate = aggregate;
755 fsr->cookie = fm.cookie;
756 fsr->cookie_mask = fm.cookie_mask;
758 fsr->out_port = fm.out_port;
759 fsr->table_id = fm.table_id;
762 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
763 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
764 * mf_field. Fields must be specified in a natural order for satisfying
767 * Returns NULL on success, otherwise a malloc()'d string that explains the
770 parse_ofp_exact_flow(struct flow *flow, const char *s)
772 char *pos, *key, *value_s;
776 memset(flow, 0, sizeof *flow);
778 pos = copy = xstrdup(s);
779 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
780 const struct protocol *p;
781 if (parse_protocol(key, &p)) {
783 error = xasprintf("%s: Ethernet type set multiple times", s);
786 flow->dl_type = htons(p->dl_type);
789 if (flow->nw_proto) {
790 error = xasprintf("%s: network protocol set "
791 "multiple times", s);
794 flow->nw_proto = p->nw_proto;
797 const struct mf_field *mf;
798 union mf_value value;
801 mf = mf_from_name(key);
803 error = xasprintf("%s: unknown field %s", s, key);
807 if (!mf_are_prereqs_ok(mf, flow)) {
808 error = xasprintf("%s: prerequisites not met for setting %s",
813 if (!mf_is_zero(mf, flow)) {
814 error = xasprintf("%s: field %s set multiple times", s, key);
818 field_error = mf_parse_value(mf, value_s, &value);
820 error = xasprintf("%s: bad value for %s (%s)",
821 s, key, field_error);
826 mf_set_flow_value(mf, &value, flow);
834 memset(flow, 0, sizeof *flow);