2 * Copyright (c) 2010, 2011, 2012, 2013, 2014 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"
26 #include "byte-order.h"
27 #include "dynamic-string.h"
29 #include "meta-flow.h"
30 #include "multipath.h"
33 #include "ofp-actions.h"
36 #include "openflow/openflow.h"
37 #include "ovs-thread.h"
40 #include "socket-util.h"
43 /* Parses 'str' as an 8-bit unsigned integer into '*valuep'.
45 * 'name' describes the value parsed in an error message, if any.
47 * Returns NULL if successful, otherwise a malloc()'d string describing the
48 * error. The caller is responsible for freeing the returned string. */
49 char * WARN_UNUSED_RESULT
50 str_to_u8(const char *str, const char *name, uint8_t *valuep)
54 if (!str_to_int(str, 0, &value) || value < 0 || value > 255) {
55 return xasprintf("invalid %s \"%s\"", name, str);
61 /* Parses 'str' as a 16-bit unsigned integer into '*valuep'.
63 * 'name' describes the value parsed in an error message, if any.
65 * Returns NULL if successful, otherwise a malloc()'d string describing the
66 * error. The caller is responsible for freeing the returned string. */
67 char * WARN_UNUSED_RESULT
68 str_to_u16(const char *str, const char *name, uint16_t *valuep)
72 if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) {
73 return xasprintf("invalid %s \"%s\"", name, str);
79 /* Parses 'str' as a 32-bit unsigned integer into '*valuep'.
81 * Returns NULL if successful, otherwise a malloc()'d string describing the
82 * error. The caller is responsible for freeing the returned string. */
83 char * WARN_UNUSED_RESULT
84 str_to_u32(const char *str, uint32_t *valuep)
90 return xstrdup("missing required numeric argument");
94 value = strtoul(str, &tail, 0);
95 if (errno == EINVAL || errno == ERANGE || *tail) {
96 return xasprintf("invalid numeric format %s", str);
102 /* Parses 'str' as an 64-bit unsigned integer into '*valuep'.
104 * Returns NULL if successful, otherwise a malloc()'d string describing the
105 * error. The caller is responsible for freeing the returned string. */
106 char * WARN_UNUSED_RESULT
107 str_to_u64(const char *str, uint64_t *valuep)
113 return xstrdup("missing required numeric argument");
117 value = strtoull(str, &tail, 0);
118 if (errno == EINVAL || errno == ERANGE || *tail) {
119 return xasprintf("invalid numeric format %s", str);
125 /* Parses 'str' as an 64-bit unsigned integer in network byte order into
128 * Returns NULL if successful, otherwise a malloc()'d string describing the
129 * error. The caller is responsible for freeing the returned string. */
130 char * WARN_UNUSED_RESULT
131 str_to_be64(const char *str, ovs_be64 *valuep)
136 error = str_to_u64(str, &value);
138 *valuep = htonll(value);
143 /* Parses 'str' as an Ethernet address into 'mac'.
145 * Returns NULL if successful, otherwise a malloc()'d string describing the
146 * error. The caller is responsible for freeing the returned string. */
147 char * WARN_UNUSED_RESULT
148 str_to_mac(const char *str, uint8_t mac[ETH_ADDR_LEN])
150 if (!ovs_scan(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))) {
151 return xasprintf("invalid mac address %s", str);
156 /* Parses 'str' as an IP address into '*ip'.
158 * Returns NULL if successful, otherwise a malloc()'d string describing the
159 * error. The caller is responsible for freeing the returned string. */
160 char * WARN_UNUSED_RESULT
161 str_to_ip(const char *str, ovs_be32 *ip)
163 struct in_addr in_addr;
165 if (lookup_ip(str, &in_addr)) {
166 return xasprintf("%s: could not convert to IP address", str);
168 *ip = in_addr.s_addr;
179 parse_protocol(const char *name, const struct protocol **p_out)
181 static const struct protocol protocols[] = {
182 { "ip", ETH_TYPE_IP, 0 },
183 { "arp", ETH_TYPE_ARP, 0 },
184 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
185 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
186 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
187 { "sctp", ETH_TYPE_IP, IPPROTO_SCTP },
188 { "ipv6", ETH_TYPE_IPV6, 0 },
189 { "ip6", ETH_TYPE_IPV6, 0 },
190 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
191 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
192 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
193 { "sctp6", ETH_TYPE_IPV6, IPPROTO_SCTP },
194 { "rarp", ETH_TYPE_RARP, 0},
195 { "mpls", ETH_TYPE_MPLS, 0 },
196 { "mplsm", ETH_TYPE_MPLS_MCAST, 0 },
198 const struct protocol *p;
200 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
201 if (!strcmp(p->name, name)) {
210 /* Parses 's' as the (possibly masked) value of field 'mf', and updates
211 * 'match' appropriately. Restricts the set of usable protocols to ones
212 * supporting the parsed field.
214 * Returns NULL if successful, otherwise a malloc()'d string describing the
215 * error. The caller is responsible for freeing the returned string. */
216 static char * WARN_UNUSED_RESULT
217 parse_field(const struct mf_field *mf, const char *s, struct match *match,
218 enum ofputil_protocol *usable_protocols)
220 union mf_value value, mask;
223 error = mf_parse(mf, s, &value, &mask);
225 *usable_protocols &= mf_set(mf, &value, &mask, match);
231 extract_actions(char *s)
233 s = strstr(s, "action");
236 s = strchr(s + 1, '=');
237 return s ? s + 1 : NULL;
244 static char * WARN_UNUSED_RESULT
245 parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string,
246 enum ofputil_protocol *usable_protocols)
255 char *save_ptr = NULL;
256 char *act_str = NULL;
259 *usable_protocols = OFPUTIL_P_ANY;
267 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
274 case OFPFC_DELETE_STRICT:
275 fields = F_OUT_PORT | F_PRIORITY;
279 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
282 case OFPFC_MODIFY_STRICT:
283 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
290 match_init_catchall(&fm->match);
291 fm->priority = OFP_DEFAULT_PRIORITY;
292 fm->cookie = htonll(0);
293 fm->cookie_mask = htonll(0);
294 if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
295 /* For modify, by default, don't update the cookie. */
296 fm->new_cookie = OVS_BE64_MAX;
298 fm->new_cookie = htonll(0);
300 fm->modify_cookie = false;
302 fm->command = command;
303 fm->idle_timeout = OFP_FLOW_PERMANENT;
304 fm->hard_timeout = OFP_FLOW_PERMANENT;
305 fm->buffer_id = UINT32_MAX;
306 fm->out_port = OFPP_ANY;
308 fm->out_group = OFPG11_ANY;
309 fm->delete_reason = OFPRR_DELETE;
310 if (fields & F_ACTIONS) {
311 act_str = extract_actions(string);
313 return xstrdup("must specify an action");
316 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
317 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
318 const struct protocol *p;
321 if (parse_protocol(name, &p)) {
322 match_set_dl_type(&fm->match, htons(p->dl_type));
324 match_set_nw_proto(&fm->match, p->nw_proto);
326 } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
327 fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
328 } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
329 fm->flags |= OFPUTIL_FF_CHECK_OVERLAP;
330 } else if (fields & F_FLAGS && !strcmp(name, "reset_counts")) {
331 fm->flags |= OFPUTIL_FF_RESET_COUNTS;
332 *usable_protocols &= OFPUTIL_P_OF12_UP;
333 } else if (fields & F_FLAGS && !strcmp(name, "no_packet_counts")) {
334 fm->flags |= OFPUTIL_FF_NO_PKT_COUNTS;
335 *usable_protocols &= OFPUTIL_P_OF13_UP;
336 } else if (fields & F_FLAGS && !strcmp(name, "no_byte_counts")) {
337 fm->flags |= OFPUTIL_FF_NO_BYT_COUNTS;
338 *usable_protocols &= OFPUTIL_P_OF13_UP;
339 } else if (!strcmp(name, "no_readonly_table")
340 || !strcmp(name, "allow_hidden_fields")) {
341 /* ignore these fields. */
345 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
347 return xasprintf("field %s missing value", name);
350 if (!strcmp(name, "table")) {
351 error = str_to_u8(value, "table", &fm->table_id);
352 if (fm->table_id != 0xff) {
353 *usable_protocols &= OFPUTIL_P_TID;
355 } else if (!strcmp(name, "out_port")) {
356 if (!ofputil_port_from_string(value, &fm->out_port)) {
357 error = xasprintf("%s is not a valid OpenFlow port",
360 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
361 uint16_t priority = 0;
363 error = str_to_u16(value, name, &priority);
364 fm->priority = priority;
365 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
366 error = str_to_u16(value, name, &fm->idle_timeout);
367 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
368 error = str_to_u16(value, name, &fm->hard_timeout);
369 } else if (!strcmp(name, "cookie")) {
370 char *mask = strchr(value, '/');
373 /* A mask means we're searching for a cookie. */
374 if (command == OFPFC_ADD) {
375 return xstrdup("flow additions cannot use "
379 error = str_to_be64(value, &fm->cookie);
383 error = str_to_be64(mask + 1, &fm->cookie_mask);
385 /* Matching of the cookie is only supported through NXM or
387 if (fm->cookie_mask != htonll(0)) {
388 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
391 /* No mask means that the cookie is being set. */
392 if (command != OFPFC_ADD && command != OFPFC_MODIFY
393 && command != OFPFC_MODIFY_STRICT) {
394 return xstrdup("cannot set cookie");
396 error = str_to_be64(value, &fm->new_cookie);
397 fm->modify_cookie = true;
399 } else if (mf_from_name(name)) {
400 error = parse_field(mf_from_name(name), value, &fm->match,
402 } else if (!strcmp(name, "duration")
403 || !strcmp(name, "n_packets")
404 || !strcmp(name, "n_bytes")
405 || !strcmp(name, "idle_age")
406 || !strcmp(name, "hard_age")) {
407 /* Ignore these, so that users can feed the output of
408 * "ovs-ofctl dump-flows" back into commands that parse
411 error = xasprintf("unknown keyword %s", name);
419 /* Check for usable protocol interdependencies between match fields. */
420 if (fm->match.flow.dl_type == htons(ETH_TYPE_IPV6)) {
421 const struct flow_wildcards *wc = &fm->match.wc;
422 /* Only NXM and OXM support matching L3 and L4 fields within IPv6.
424 * (IPv6 specific fields as well as arp_sha, arp_tha, nw_frag, and
425 * nw_ttl are covered elsewhere so they don't need to be included in
428 if (wc->masks.nw_proto || wc->masks.nw_tos
429 || wc->masks.tp_src || wc->masks.tp_dst) {
430 *usable_protocols &= OFPUTIL_P_NXM_OXM_ANY;
433 if (!fm->cookie_mask && fm->new_cookie == OVS_BE64_MAX
434 && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
435 /* On modifies without a mask, we are supposed to add a flow if
436 * one does not exist. If a cookie wasn't been specified, use a
437 * default of zero. */
438 fm->new_cookie = htonll(0);
440 if (fields & F_ACTIONS) {
441 enum ofputil_protocol action_usable_protocols;
442 struct ofpbuf ofpacts;
445 ofpbuf_init(&ofpacts, 32);
446 error = ofpacts_parse_instructions(act_str, &ofpacts,
447 &action_usable_protocols);
448 *usable_protocols &= action_usable_protocols;
452 err = ofpacts_check(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &fm->match.flow,
453 OFPP_MAX, fm->table_id, 255, usable_protocols);
454 if (!err && !usable_protocols) {
455 err = OFPERR_OFPBAC_MATCH_INCONSISTENT;
458 error = xasprintf("actions are invalid with specified match "
459 "(%s)", ofperr_to_string(err));
464 ofpbuf_uninit(&ofpacts);
468 fm->ofpacts_len = ofpbuf_size(&ofpacts);
469 fm->ofpacts = ofpbuf_steal_data(&ofpacts);
478 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
479 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
480 * Returns the set of usable protocols in '*usable_protocols'.
482 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
483 * constant for 'command'. To parse syntax for an OFPST_FLOW or
484 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'.
486 * Returns NULL if successful, otherwise a malloc()'d string describing the
487 * error. The caller is responsible for freeing the returned string. */
488 char * WARN_UNUSED_RESULT
489 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
490 enum ofputil_protocol *usable_protocols)
492 char *string = xstrdup(str_);
495 error = parse_ofp_str__(fm, command, string, usable_protocols);
505 static char * WARN_UNUSED_RESULT
506 parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string,
507 struct ofpbuf *bands, int command,
508 enum ofputil_protocol *usable_protocols)
515 char *save_ptr = NULL;
516 char *band_str = NULL;
519 /* Meters require at least OF 1.3. */
520 *usable_protocols = OFPUTIL_P_OF13_UP;
528 fields = F_METER | F_FLAGS | F_BANDS;
536 fields = F_METER | F_FLAGS | F_BANDS;
543 mm->command = command;
544 mm->meter.meter_id = 0;
546 if (fields & F_BANDS) {
547 band_str = strstr(string, "band");
549 return xstrdup("must specify bands");
553 band_str = strchr(band_str + 1, '=');
555 return xstrdup("must specify bands");
560 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
561 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
563 if (fields & F_FLAGS && !strcmp(name, "kbps")) {
564 mm->meter.flags |= OFPMF13_KBPS;
565 } else if (fields & F_FLAGS && !strcmp(name, "pktps")) {
566 mm->meter.flags |= OFPMF13_PKTPS;
567 } else if (fields & F_FLAGS && !strcmp(name, "burst")) {
568 mm->meter.flags |= OFPMF13_BURST;
569 } else if (fields & F_FLAGS && !strcmp(name, "stats")) {
570 mm->meter.flags |= OFPMF13_STATS;
574 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
576 return xasprintf("field %s missing value", name);
579 if (!strcmp(name, "meter")) {
580 if (!strcmp(value, "all")) {
581 mm->meter.meter_id = OFPM13_ALL;
582 } else if (!strcmp(value, "controller")) {
583 mm->meter.meter_id = OFPM13_CONTROLLER;
584 } else if (!strcmp(value, "slowpath")) {
585 mm->meter.meter_id = OFPM13_SLOWPATH;
587 char *error = str_to_u32(value, &mm->meter.meter_id);
591 if (mm->meter.meter_id > OFPM13_MAX) {
592 return xasprintf("invalid value for %s", name);
596 return xasprintf("unknown keyword %s", name);
600 if (fields & F_METER && !mm->meter.meter_id) {
601 return xstrdup("must specify 'meter'");
603 if (fields & F_FLAGS && !mm->meter.flags) {
604 return xstrdup("meter must specify either 'kbps' or 'pktps'");
607 if (fields & F_BANDS) {
608 uint16_t n_bands = 0;
609 struct ofputil_meter_band *band = NULL;
612 for (name = strtok_r(band_str, "=, \t\r\n", &save_ptr); name;
613 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
617 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
619 return xasprintf("field %s missing value", name);
622 if (!strcmp(name, "type")) {
623 /* Start a new band */
624 band = ofpbuf_put_zeros(bands, sizeof *band);
627 if (!strcmp(value, "drop")) {
628 band->type = OFPMBT13_DROP;
629 } else if (!strcmp(value, "dscp_remark")) {
630 band->type = OFPMBT13_DSCP_REMARK;
632 return xasprintf("field %s unknown value %s", name, value);
634 } else if (!band || !band->type) {
635 return xstrdup("band must start with the 'type' keyword");
636 } else if (!strcmp(name, "rate")) {
637 char *error = str_to_u32(value, &band->rate);
641 } else if (!strcmp(name, "burst_size")) {
642 char *error = str_to_u32(value, &band->burst_size);
646 } else if (!strcmp(name, "prec_level")) {
647 char *error = str_to_u8(value, name, &band->prec_level);
652 return xasprintf("unknown keyword %s", name);
657 return xstrdup("meter must have bands");
660 mm->meter.n_bands = n_bands;
661 mm->meter.bands = ofpbuf_steal_data(bands);
663 for (i = 0; i < n_bands; ++i) {
664 band = &mm->meter.bands[i];
667 return xstrdup("band must have 'type'");
669 if (band->type == OFPMBT13_DSCP_REMARK) {
670 if (!band->prec_level) {
671 return xstrdup("'dscp_remark' band must have"
675 if (band->prec_level) {
676 return xstrdup("Only 'dscp_remark' band may have"
681 return xstrdup("band must have 'rate'");
683 if (mm->meter.flags & OFPMF13_BURST) {
684 if (!band->burst_size) {
685 return xstrdup("band must have 'burst_size' "
686 "when 'burst' flag is set");
689 if (band->burst_size) {
690 return xstrdup("band may have 'burst_size' only "
691 "when 'burst' flag is set");
696 mm->meter.n_bands = 0;
697 mm->meter.bands = NULL;
703 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
704 * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
706 * Returns NULL if successful, otherwise a malloc()'d string describing the
707 * error. The caller is responsible for freeing the returned string. */
708 char * WARN_UNUSED_RESULT
709 parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_,
710 int command, enum ofputil_protocol *usable_protocols)
716 ofpbuf_init(&bands, 64);
717 string = xstrdup(str_);
719 error = parse_ofp_meter_mod_str__(mm, string, &bands, command,
723 ofpbuf_uninit(&bands);
728 static char * WARN_UNUSED_RESULT
729 parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
730 const char *str_, char *string,
731 enum ofputil_protocol *usable_protocols)
733 static atomic_count id = ATOMIC_COUNT_INIT(0);
734 char *save_ptr = NULL;
737 fmr->id = atomic_count_inc(&id);
739 fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
740 | NXFMF_OWN | NXFMF_ACTIONS);
741 fmr->out_port = OFPP_NONE;
742 fmr->table_id = 0xff;
743 match_init_catchall(&fmr->match);
745 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
746 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
747 const struct protocol *p;
749 if (!strcmp(name, "!initial")) {
750 fmr->flags &= ~NXFMF_INITIAL;
751 } else if (!strcmp(name, "!add")) {
752 fmr->flags &= ~NXFMF_ADD;
753 } else if (!strcmp(name, "!delete")) {
754 fmr->flags &= ~NXFMF_DELETE;
755 } else if (!strcmp(name, "!modify")) {
756 fmr->flags &= ~NXFMF_MODIFY;
757 } else if (!strcmp(name, "!actions")) {
758 fmr->flags &= ~NXFMF_ACTIONS;
759 } else if (!strcmp(name, "!own")) {
760 fmr->flags &= ~NXFMF_OWN;
761 } else if (parse_protocol(name, &p)) {
762 match_set_dl_type(&fmr->match, htons(p->dl_type));
764 match_set_nw_proto(&fmr->match, p->nw_proto);
769 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
771 return xasprintf("%s: field %s missing value", str_, name);
774 if (!strcmp(name, "table")) {
775 char *error = str_to_u8(value, "table", &fmr->table_id);
779 } else if (!strcmp(name, "out_port")) {
780 fmr->out_port = u16_to_ofp(atoi(value));
781 } else if (mf_from_name(name)) {
784 error = parse_field(mf_from_name(name), value, &fmr->match,
790 return xasprintf("%s: unknown keyword %s", str_, name);
797 /* Convert 'str_' (as described in the documentation for the "monitor" command
798 * in the ovs-ofctl man page) into 'fmr'.
800 * Returns NULL if successful, otherwise a malloc()'d string describing the
801 * error. The caller is responsible for freeing the returned string. */
802 char * WARN_UNUSED_RESULT
803 parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
805 enum ofputil_protocol *usable_protocols)
807 char *string = xstrdup(str_);
808 char *error = parse_flow_monitor_request__(fmr, str_, string,
814 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
815 * (one of OFPFC_*) into 'fm'.
817 * Returns NULL if successful, otherwise a malloc()'d string describing the
818 * error. The caller is responsible for freeing the returned string. */
819 char * WARN_UNUSED_RESULT
820 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
822 enum ofputil_protocol *usable_protocols)
824 char *error = parse_ofp_str(fm, command, string, usable_protocols);
826 /* Normalize a copy of the match. This ensures that non-normalized
827 * flows get logged but doesn't affect what gets sent to the switch, so
828 * that the switch can do whatever it likes with the flow. */
829 struct match match_copy = fm->match;
830 ofputil_normalize_match(&match_copy);
836 /* Convert 'table_id' and 'flow_miss_handling' (as described for the
837 * "mod-table" command in the ovs-ofctl man page) into 'tm' for sending the
838 * specified table_mod 'command' to a switch.
840 * Returns NULL if successful, otherwise a malloc()'d string describing the
841 * error. The caller is responsible for freeing the returned string. */
842 char * WARN_UNUSED_RESULT
843 parse_ofp_table_mod(struct ofputil_table_mod *tm, const char *table_id,
844 const char *flow_miss_handling,
845 enum ofputil_protocol *usable_protocols)
847 /* Table mod requires at least OF 1.1. */
848 *usable_protocols = OFPUTIL_P_OF11_UP;
850 if (!strcasecmp(table_id, "all")) {
851 tm->table_id = OFPTT_ALL;
853 char *error = str_to_u8(table_id, "table_id", &tm->table_id);
859 if (strcmp(flow_miss_handling, "controller") == 0) {
860 tm->miss_config = OFPUTIL_TABLE_MISS_CONTROLLER;
861 } else if (strcmp(flow_miss_handling, "continue") == 0) {
862 tm->miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
863 } else if (strcmp(flow_miss_handling, "drop") == 0) {
864 tm->miss_config = OFPUTIL_TABLE_MISS_DROP;
866 return xasprintf("invalid flow_miss_handling %s", flow_miss_handling);
869 if (tm->table_id == 0xfe
870 && tm->miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
871 return xstrdup("last table's flow miss handling can not be continue");
878 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
879 * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated
880 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
882 * Returns NULL if successful, otherwise a malloc()'d string describing the
883 * error. The caller is responsible for freeing the returned string. */
884 char * WARN_UNUSED_RESULT
885 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
886 struct ofputil_flow_mod **fms, size_t *n_fms,
887 enum ofputil_protocol *usable_protocols)
889 size_t allocated_fms;
894 *usable_protocols = OFPUTIL_P_ANY;
899 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
900 if (stream == NULL) {
901 return xasprintf("%s: open failed (%s)",
902 file_name, ovs_strerror(errno));
905 allocated_fms = *n_fms;
908 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
910 enum ofputil_protocol usable;
912 if (*n_fms >= allocated_fms) {
913 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
915 error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command,
920 for (i = 0; i < *n_fms; i++) {
921 free(CONST_CAST(struct ofpact *, (*fms)[i].ofpacts));
928 if (stream != stdin) {
932 return xasprintf("%s:%d: %s", file_name, line_number, error);
934 *usable_protocols &= usable; /* Each line can narrow the set. */
939 if (stream != stdin) {
945 char * WARN_UNUSED_RESULT
946 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
947 bool aggregate, const char *string,
948 enum ofputil_protocol *usable_protocols)
950 struct ofputil_flow_mod fm;
953 error = parse_ofp_str(&fm, -1, string, usable_protocols);
958 /* Special table ID support not required for stats requests. */
959 if (*usable_protocols & OFPUTIL_P_OF10_STD_TID) {
960 *usable_protocols |= OFPUTIL_P_OF10_STD;
962 if (*usable_protocols & OFPUTIL_P_OF10_NXM_TID) {
963 *usable_protocols |= OFPUTIL_P_OF10_NXM;
966 fsr->aggregate = aggregate;
967 fsr->cookie = fm.cookie;
968 fsr->cookie_mask = fm.cookie_mask;
969 fsr->match = fm.match;
970 fsr->out_port = fm.out_port;
971 fsr->out_group = fm.out_group;
972 fsr->table_id = fm.table_id;
976 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
977 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
978 * mf_field. Fields must be specified in a natural order for satisfying
979 * prerequisites. If 'mask' is specified, fills the mask field for each of the
980 * field specified in flow. If the map, 'names_portno' is specfied, converts
981 * the in_port name into port no while setting the 'flow'.
983 * Returns NULL on success, otherwise a malloc()'d string that explains the
986 parse_ofp_exact_flow(struct flow *flow, struct flow *mask, const char *s,
987 const struct simap *portno_names)
989 char *pos, *key, *value_s;
993 memset(flow, 0, sizeof *flow);
995 memset(mask, 0, sizeof *mask);
998 pos = copy = xstrdup(s);
999 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
1000 const struct protocol *p;
1001 if (parse_protocol(key, &p)) {
1002 if (flow->dl_type) {
1003 error = xasprintf("%s: Ethernet type set multiple times", s);
1006 flow->dl_type = htons(p->dl_type);
1008 mask->dl_type = OVS_BE16_MAX;
1012 if (flow->nw_proto) {
1013 error = xasprintf("%s: network protocol set "
1014 "multiple times", s);
1017 flow->nw_proto = p->nw_proto;
1019 mask->nw_proto = UINT8_MAX;
1023 const struct mf_field *mf;
1024 union mf_value value;
1027 mf = mf_from_name(key);
1029 error = xasprintf("%s: unknown field %s", s, key);
1033 if (!mf_are_prereqs_ok(mf, flow)) {
1034 error = xasprintf("%s: prerequisites not met for setting %s",
1039 if (!mf_is_zero(mf, flow)) {
1040 error = xasprintf("%s: field %s set multiple times", s, key);
1044 if (!strcmp(key, "in_port")
1046 && simap_contains(portno_names, value_s)) {
1047 flow->in_port.ofp_port = u16_to_ofp(
1048 simap_get(portno_names, value_s));
1050 mask->in_port.ofp_port = u16_to_ofp(ntohs(OVS_BE16_MAX));
1053 field_error = mf_parse_value(mf, value_s, &value);
1055 error = xasprintf("%s: bad value for %s (%s)",
1056 s, key, field_error);
1061 mf_set_flow_value(mf, &value, flow);
1063 mf_mask_field(mf, mask);
1069 if (!flow->in_port.ofp_port) {
1070 flow->in_port.ofp_port = OFPP_NONE;
1077 memset(flow, 0, sizeof *flow);
1079 memset(mask, 0, sizeof *mask);
1085 static char * WARN_UNUSED_RESULT
1086 parse_bucket_str(struct ofputil_bucket *bucket, char *str_,
1087 enum ofputil_protocol *usable_protocols)
1089 char *pos, *key, *value;
1090 struct ofpbuf ofpacts;
1095 bucket->watch_port = OFPP_ANY;
1096 bucket->watch_group = OFPG11_ANY;
1102 while (ofputil_parse_key_value(&pos, &key, &value)) {
1103 if (!strcasecmp(key, "weight")) {
1104 error = str_to_u16(value, "weight", &bucket->weight);
1105 } else if (!strcasecmp(key, "watch_port")) {
1106 if (!ofputil_port_from_string(value, &bucket->watch_port)
1107 || (ofp_to_u16(bucket->watch_port) >= ofp_to_u16(OFPP_MAX)
1108 && bucket->watch_port != OFPP_ANY)) {
1109 error = xasprintf("%s: invalid watch_port", value);
1111 } else if (!strcasecmp(key, "watch_group")) {
1112 error = str_to_u32(value, &bucket->watch_group);
1113 if (!error && bucket->watch_group > OFPG_MAX) {
1114 error = xasprintf("invalid watch_group id %"PRIu32,
1115 bucket->watch_group);
1117 } else if (!strcasecmp(key, "action") || !strcasecmp(key, "actions")) {
1118 ds_put_format(&actions, "%s,", value);
1120 ds_put_format(&actions, "%s(%s),", key, value);
1124 ds_destroy(&actions);
1129 if (!actions.length) {
1130 return xstrdup("bucket must specify actions");
1132 ds_chomp(&actions, ',');
1134 ofpbuf_init(&ofpacts, 0);
1135 error = ofpacts_parse_actions(ds_cstr(&actions), &ofpacts,
1137 ds_destroy(&actions);
1139 ofpbuf_uninit(&ofpacts);
1142 bucket->ofpacts = ofpbuf_data(&ofpacts);
1143 bucket->ofpacts_len = ofpbuf_size(&ofpacts);
1148 static char * WARN_UNUSED_RESULT
1149 parse_ofp_group_mod_str__(struct ofputil_group_mod *gm, uint16_t command,
1151 enum ofputil_protocol *usable_protocols)
1154 F_GROUP_TYPE = 1 << 0,
1157 char *save_ptr = NULL;
1158 bool had_type = false;
1160 struct ofputil_bucket *bucket;
1163 *usable_protocols = OFPUTIL_P_OF11_UP;
1167 fields = F_GROUP_TYPE | F_BUCKETS;
1170 case OFPGC11_DELETE:
1174 case OFPGC11_MODIFY:
1175 fields = F_GROUP_TYPE | F_BUCKETS;
1182 memset(gm, 0, sizeof *gm);
1183 gm->command = command;
1184 gm->group_id = OFPG_ANY;
1185 list_init(&gm->buckets);
1186 if (command == OFPGC11_DELETE && string[0] == '\0') {
1187 gm->group_id = OFPG_ALL;
1191 *usable_protocols = OFPUTIL_P_OF11_UP;
1193 if (fields & F_BUCKETS) {
1194 char *bkt_str = strstr(string, "bucket");
1203 bkt_str = strchr(bkt_str + 1, '=');
1205 error = xstrdup("must specify bucket content");
1210 next_bkt_str = strstr(bkt_str, "bucket");
1212 *next_bkt_str = '\0';
1215 bucket = xzalloc(sizeof(struct ofputil_bucket));
1216 error = parse_bucket_str(bucket, bkt_str, usable_protocols);
1221 list_push_back(&gm->buckets, &bucket->list_node);
1223 bkt_str = next_bkt_str;
1227 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1228 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1231 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1233 error = xasprintf("field %s missing value", name);
1237 if (!strcmp(name, "group_id")) {
1238 if(!strcmp(value, "all")) {
1239 gm->group_id = OFPG_ALL;
1241 char *error = str_to_u32(value, &gm->group_id);
1245 if (gm->group_id != OFPG_ALL && gm->group_id > OFPG_MAX) {
1246 error = xasprintf("invalid group id %"PRIu32,
1251 } else if (!strcmp(name, "type")){
1252 if (!(fields & F_GROUP_TYPE)) {
1253 error = xstrdup("type is not needed");
1256 if (!strcmp(value, "all")) {
1257 gm->type = OFPGT11_ALL;
1258 } else if (!strcmp(value, "select")) {
1259 gm->type = OFPGT11_SELECT;
1260 } else if (!strcmp(value, "indirect")) {
1261 gm->type = OFPGT11_INDIRECT;
1262 } else if (!strcmp(value, "ff") ||
1263 !strcmp(value, "fast_failover")) {
1264 gm->type = OFPGT11_FF;
1266 error = xasprintf("invalid group type %s", value);
1270 } else if (!strcmp(name, "bucket")) {
1271 error = xstrdup("bucket is not needed");
1274 error = xasprintf("unknown keyword %s", name);
1278 if (gm->group_id == OFPG_ANY) {
1279 error = xstrdup("must specify a group_id");
1282 if (fields & F_GROUP_TYPE && !had_type) {
1283 error = xstrdup("must specify a type");
1287 /* Validate buckets. */
1288 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
1289 if (bucket->weight != 1 && gm->type != OFPGT11_SELECT) {
1290 error = xstrdup("Only select groups can have bucket weights.");
1294 if (gm->type == OFPGT11_INDIRECT && !list_is_short(&gm->buckets)) {
1295 error = xstrdup("Indirect groups can have at most one bucket.");
1301 ofputil_bucket_list_destroy(&gm->buckets);
1305 char * WARN_UNUSED_RESULT
1306 parse_ofp_group_mod_str(struct ofputil_group_mod *gm, uint16_t command,
1308 enum ofputil_protocol *usable_protocols)
1310 char *string = xstrdup(str_);
1311 char *error = parse_ofp_group_mod_str__(gm, command, string,
1316 ofputil_bucket_list_destroy(&gm->buckets);
1321 char * WARN_UNUSED_RESULT
1322 parse_ofp_group_mod_file(const char *file_name, uint16_t command,
1323 struct ofputil_group_mod **gms, size_t *n_gms,
1324 enum ofputil_protocol *usable_protocols)
1326 size_t allocated_gms;
1334 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1335 if (stream == NULL) {
1336 return xasprintf("%s: open failed (%s)",
1337 file_name, ovs_strerror(errno));
1340 allocated_gms = *n_gms;
1343 *usable_protocols = OFPUTIL_P_OF11_UP;
1344 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
1345 enum ofputil_protocol usable;
1348 if (*n_gms >= allocated_gms) {
1351 *gms = x2nrealloc(*gms, &allocated_gms, sizeof **gms);
1352 for (i = 0; i < *n_gms; i++) {
1353 list_moved(&(*gms)[i].buckets);
1356 error = parse_ofp_group_mod_str(&(*gms)[*n_gms], command, ds_cstr(&s),
1361 for (i = 0; i < *n_gms; i++) {
1362 ofputil_bucket_list_destroy(&(*gms)[i].buckets);
1369 if (stream != stdin) {
1373 return xasprintf("%s:%d: %s", file_name, line_number, error);
1375 *usable_protocols &= usable;
1380 if (stream != stdin) {