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_ACTION_INVALID:
329 case OFPUTIL_OFPAT10_OUTPUT:
330 parse_output(b, arg);
333 case OFPUTIL_OFPAT10_SET_VLAN_VID:
334 oavv = ofputil_put_OFPAT10_SET_VLAN_VID(b);
335 oavv->vlan_vid = htons(str_to_u32(arg));
338 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
339 oavp = ofputil_put_OFPAT10_SET_VLAN_PCP(b);
340 oavp->vlan_pcp = str_to_u32(arg);
343 case OFPUTIL_OFPAT10_STRIP_VLAN:
344 ofputil_put_OFPAT10_STRIP_VLAN(b);
347 case OFPUTIL_OFPAT10_SET_DL_SRC:
348 case OFPUTIL_OFPAT10_SET_DL_DST:
349 oada = ofputil_put_action(code, b);
350 str_to_mac(arg, oada->dl_addr);
353 case OFPUTIL_OFPAT10_SET_NW_SRC:
354 case OFPUTIL_OFPAT10_SET_NW_DST:
355 oana = ofputil_put_action(code, b);
356 str_to_ip(arg, &oana->nw_addr);
359 case OFPUTIL_OFPAT10_SET_NW_TOS:
360 ofputil_put_OFPAT10_SET_NW_TOS(b)->nw_tos = str_to_u32(arg);
363 case OFPUTIL_OFPAT10_SET_TP_SRC:
364 case OFPUTIL_OFPAT10_SET_TP_DST:
365 oata = ofputil_put_action(code, b);
366 oata->tp_port = htons(str_to_u32(arg));
369 case OFPUTIL_OFPAT10_ENQUEUE:
370 parse_enqueue(b, arg);
373 case OFPUTIL_NXAST_RESUBMIT:
374 parse_resubmit(b, arg);
377 case OFPUTIL_NXAST_SET_TUNNEL:
378 parse_set_tunnel(b, arg);
381 case OFPUTIL_NXAST_SET_QUEUE:
382 ofputil_put_NXAST_SET_QUEUE(b)->queue_id = htonl(str_to_u32(arg));
385 case OFPUTIL_NXAST_POP_QUEUE:
386 ofputil_put_NXAST_POP_QUEUE(b);
389 case OFPUTIL_NXAST_REG_MOVE:
390 nxm_parse_reg_move(ofputil_put_NXAST_REG_MOVE(b), arg);
393 case OFPUTIL_NXAST_REG_LOAD:
394 nxm_parse_reg_load(ofputil_put_NXAST_REG_LOAD(b), arg);
397 case OFPUTIL_NXAST_NOTE:
401 case OFPUTIL_NXAST_SET_TUNNEL64:
402 ofputil_put_NXAST_SET_TUNNEL64(b)->tun_id = htonll(str_to_u64(arg));
405 case OFPUTIL_NXAST_MULTIPATH:
406 multipath_parse(ofputil_put_NXAST_MULTIPATH(b), arg);
409 case OFPUTIL_NXAST_AUTOPATH:
410 autopath_parse(ofputil_put_NXAST_AUTOPATH(b), arg);
413 case OFPUTIL_NXAST_BUNDLE:
414 bundle_parse(b, arg);
417 case OFPUTIL_NXAST_BUNDLE_LOAD:
418 bundle_parse_load(b, arg);
421 case OFPUTIL_NXAST_RESUBMIT_TABLE:
422 case OFPUTIL_NXAST_OUTPUT_REG:
425 case OFPUTIL_NXAST_LEARN:
426 learn_parse(b, arg, flow);
429 case OFPUTIL_NXAST_EXIT:
430 ofputil_put_NXAST_EXIT(b);
433 case OFPUTIL_NXAST_DEC_TTL:
434 ofputil_put_NXAST_DEC_TTL(b);
437 case OFPUTIL_NXAST_FIN_TIMEOUT:
438 parse_fin_timeout(b, arg);
441 case OFPUTIL_NXAST_CONTROLLER:
442 parse_controller(b, arg);
448 str_to_action(const struct flow *flow, char *str, struct ofpbuf *b)
450 char *pos, *act, *arg;
455 while (ofputil_parse_key_value(&pos, &act, &arg)) {
459 code = ofputil_action_code_from_name(act);
461 parse_named_action(code, flow, b, arg);
462 } else if (!strcasecmp(act, "drop")) {
463 /* A drop action in OpenFlow occurs by just not setting
466 ovs_fatal(0, "Drop actions must not be preceded by other "
468 } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
469 ovs_fatal(0, "Drop actions must not be followed by other "
473 } else if (ofputil_port_from_string(act, &port)) {
474 put_output_action(b, port);
476 ovs_fatal(0, "Unknown action: %s", act);
489 parse_protocol(const char *name, const struct protocol **p_out)
491 static const struct protocol protocols[] = {
492 { "ip", ETH_TYPE_IP, 0 },
493 { "arp", ETH_TYPE_ARP, 0 },
494 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
495 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
496 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
497 { "ipv6", ETH_TYPE_IPV6, 0 },
498 { "ip6", ETH_TYPE_IPV6, 0 },
499 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
500 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
501 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
503 const struct protocol *p;
505 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
506 if (!strcmp(p->name, name)) {
516 ofp_fatal(const char *flow, bool verbose, const char *format, ...)
521 fprintf(stderr, "%s:\n", flow);
524 va_start(args, format);
525 ovs_fatal_valist(0, format, args);
529 parse_field(const struct mf_field *mf, const char *s, struct cls_rule *rule)
531 union mf_value value, mask;
534 error = mf_parse(mf, s, &value, &mask);
536 ovs_fatal(0, "%s", error);
539 mf_set(mf, &value, &mask, rule);
542 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
543 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
544 * If 'actions' is specified, an action must be in 'string' and may be expanded
547 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
548 * constant for 'command'. To parse syntax for an OFPST_FLOW or
549 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
551 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
561 char *string = xstrdup(str_);
562 char *save_ptr = NULL;
563 char *act_str = NULL;
572 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
579 case OFPFC_DELETE_STRICT:
580 fields = F_OUT_PORT | F_PRIORITY;
584 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
587 case OFPFC_MODIFY_STRICT:
588 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
595 cls_rule_init_catchall(&fm->cr, OFP_DEFAULT_PRIORITY);
596 fm->cookie = htonll(0);
597 fm->cookie_mask = htonll(0);
598 if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
599 /* For modify, by default, don't update the cookie. */
600 fm->new_cookie = htonll(UINT64_MAX);
602 fm->new_cookie = htonll(0);
605 fm->command = command;
606 fm->idle_timeout = OFP_FLOW_PERMANENT;
607 fm->hard_timeout = OFP_FLOW_PERMANENT;
608 fm->buffer_id = UINT32_MAX;
609 fm->out_port = OFPP_NONE;
611 if (fields & F_ACTIONS) {
612 act_str = strstr(string, "action");
614 ofp_fatal(str_, verbose, "must specify an action");
618 act_str = strchr(act_str + 1, '=');
620 ofp_fatal(str_, verbose, "must specify an action");
625 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
626 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
627 const struct protocol *p;
629 if (parse_protocol(name, &p)) {
630 cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
632 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
634 } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
635 fm->flags |= OFPFF_SEND_FLOW_REM;
636 } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
637 fm->flags |= OFPFF_CHECK_OVERLAP;
641 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
643 ofp_fatal(str_, verbose, "field %s missing value", name);
646 if (!strcmp(name, "table")) {
647 fm->table_id = str_to_table_id(value);
648 } else if (!strcmp(name, "out_port")) {
649 fm->out_port = atoi(value);
650 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
651 fm->cr.priority = str_to_u16(value, name);
652 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
653 fm->idle_timeout = str_to_u16(value, name);
654 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
655 fm->hard_timeout = str_to_u16(value, name);
656 } else if (!strcmp(name, "cookie")) {
657 char *mask = strchr(value, '/');
660 /* A mask means we're searching for a cookie. */
661 if (command == OFPFC_ADD) {
662 ofp_fatal(str_, verbose, "flow additions cannot use "
666 fm->cookie = htonll(str_to_u64(value));
667 fm->cookie_mask = htonll(str_to_u64(mask+1));
669 /* No mask means that the cookie is being set. */
670 if (command != OFPFC_ADD && command != OFPFC_MODIFY
671 && command != OFPFC_MODIFY_STRICT) {
672 ofp_fatal(str_, verbose, "cannot set cookie");
674 fm->new_cookie = htonll(str_to_u64(value));
676 } else if (mf_from_name(name)) {
677 parse_field(mf_from_name(name), value, &fm->cr);
678 } else if (!strcmp(name, "duration")
679 || !strcmp(name, "n_packets")
680 || !strcmp(name, "n_bytes")) {
681 /* Ignore these, so that users can feed the output of
682 * "ovs-ofctl dump-flows" back into commands that parse
685 ofp_fatal(str_, verbose, "unknown keyword %s", name);
689 if (!fm->cookie_mask && fm->new_cookie == htonll(UINT64_MAX)
690 && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
691 /* On modifies without a mask, we are supposed to add a flow if
692 * one does not exist. If a cookie wasn't been specified, use a
693 * default of zero. */
694 fm->new_cookie = htonll(0);
696 if (fields & F_ACTIONS) {
697 struct ofpbuf actions;
699 ofpbuf_init(&actions, sizeof(union ofp_action));
700 str_to_action(&fm->cr.flow, act_str, &actions);
701 fm->actions = ofpbuf_steal_data(&actions);
702 fm->n_actions = actions.size / sizeof(union ofp_action);
711 /* Parses 's' as a set of OpenFlow actions and appends the actions to
714 * Prints an error on stderr and aborts the program if 's' syntax is
717 parse_ofp_actions(const char *s_, struct ofpbuf *actions)
719 char *s = xstrdup(s_);
720 str_to_action(NULL, s, actions);
724 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
725 * (one of OFPFC_*) into 'fm'. */
727 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
728 uint16_t command, bool verbose)
730 struct cls_rule rule_copy;
732 parse_ofp_str(fm, command, string, verbose);
734 /* Normalize a copy of the rule. This ensures that non-normalized flows
735 * get logged but doesn't affect what gets sent to the switch, so that the
736 * switch can do whatever it likes with the flow. */
738 ofputil_normalize_rule(&rule_copy);
742 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
743 struct ofputil_flow_mod **fms, size_t *n_fms)
745 size_t allocated_fms;
749 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
750 if (stream == NULL) {
751 ovs_fatal(errno, "%s: open", file_name);
754 allocated_fms = *n_fms;
756 while (!ds_get_preprocessed_line(&s, stream)) {
757 if (*n_fms >= allocated_fms) {
758 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
760 parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command, false);
765 if (stream != stdin) {
771 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
772 bool aggregate, const char *string)
774 struct ofputil_flow_mod fm;
776 parse_ofp_str(&fm, -1, string, false);
777 fsr->aggregate = aggregate;
778 fsr->cookie = fm.cookie;
779 fsr->cookie_mask = fm.cookie_mask;
781 fsr->out_port = fm.out_port;
782 fsr->table_id = fm.table_id;
785 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
786 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
787 * mf_field. Fields must be specified in a natural order for satisfying
790 * Returns NULL on success, otherwise a malloc()'d string that explains the
793 parse_ofp_exact_flow(struct flow *flow, const char *s)
795 char *pos, *key, *value_s;
799 memset(flow, 0, sizeof *flow);
801 pos = copy = xstrdup(s);
802 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
803 const struct protocol *p;
804 if (parse_protocol(key, &p)) {
806 error = xasprintf("%s: Ethernet type set multiple times", s);
809 flow->dl_type = htons(p->dl_type);
812 if (flow->nw_proto) {
813 error = xasprintf("%s: network protocol set "
814 "multiple times", s);
817 flow->nw_proto = p->nw_proto;
820 const struct mf_field *mf;
821 union mf_value value;
824 mf = mf_from_name(key);
826 error = xasprintf("%s: unknown field %s", s, key);
830 if (!mf_are_prereqs_ok(mf, flow)) {
831 error = xasprintf("%s: prerequisites not met for setting %s",
836 if (!mf_is_zero(mf, flow)) {
837 error = xasprintf("%s: field %s set multiple times", s, key);
841 field_error = mf_parse_value(mf, value_s, &value);
843 error = xasprintf("%s: bad value for %s (%s)",
844 s, key, field_error);
849 mf_set_flow_value(mf, &value, flow);
857 memset(flow, 0, sizeof *flow);