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)
251 F_IMPORTANCE = 1 << 2,
256 char *save_ptr = NULL;
257 char *act_str = NULL;
260 *usable_protocols = OFPUTIL_P_ANY;
268 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS | F_IMPORTANCE;
275 case OFPFC_DELETE_STRICT:
276 fields = F_OUT_PORT | F_PRIORITY;
280 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
283 case OFPFC_MODIFY_STRICT:
284 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
291 match_init_catchall(&fm->match);
292 fm->priority = OFP_DEFAULT_PRIORITY;
293 fm->cookie = htonll(0);
294 fm->cookie_mask = htonll(0);
295 if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
296 /* For modify, by default, don't update the cookie. */
297 fm->new_cookie = OVS_BE64_MAX;
299 fm->new_cookie = htonll(0);
301 fm->modify_cookie = false;
303 fm->command = command;
304 fm->idle_timeout = OFP_FLOW_PERMANENT;
305 fm->hard_timeout = OFP_FLOW_PERMANENT;
306 fm->buffer_id = UINT32_MAX;
307 fm->out_port = OFPP_ANY;
310 fm->out_group = OFPG11_ANY;
311 fm->delete_reason = OFPRR_DELETE;
312 if (fields & F_ACTIONS) {
313 act_str = extract_actions(string);
315 return xstrdup("must specify an action");
318 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
319 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
320 const struct protocol *p;
323 if (parse_protocol(name, &p)) {
324 match_set_dl_type(&fm->match, htons(p->dl_type));
326 match_set_nw_proto(&fm->match, p->nw_proto);
328 } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
329 fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
330 } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
331 fm->flags |= OFPUTIL_FF_CHECK_OVERLAP;
332 } else if (fields & F_FLAGS && !strcmp(name, "reset_counts")) {
333 fm->flags |= OFPUTIL_FF_RESET_COUNTS;
334 *usable_protocols &= OFPUTIL_P_OF12_UP;
335 } else if (fields & F_FLAGS && !strcmp(name, "no_packet_counts")) {
336 fm->flags |= OFPUTIL_FF_NO_PKT_COUNTS;
337 *usable_protocols &= OFPUTIL_P_OF13_UP;
338 } else if (fields & F_FLAGS && !strcmp(name, "no_byte_counts")) {
339 fm->flags |= OFPUTIL_FF_NO_BYT_COUNTS;
340 *usable_protocols &= OFPUTIL_P_OF13_UP;
341 } else if (!strcmp(name, "no_readonly_table")
342 || !strcmp(name, "allow_hidden_fields")) {
343 /* ignore these fields. */
347 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
349 return xasprintf("field %s missing value", name);
352 if (!strcmp(name, "table")) {
353 error = str_to_u8(value, "table", &fm->table_id);
354 if (fm->table_id != 0xff) {
355 *usable_protocols &= OFPUTIL_P_TID;
357 } else if (!strcmp(name, "out_port")) {
358 if (!ofputil_port_from_string(value, &fm->out_port)) {
359 error = xasprintf("%s is not a valid OpenFlow port",
362 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
363 uint16_t priority = 0;
365 error = str_to_u16(value, name, &priority);
366 fm->priority = priority;
367 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
368 error = str_to_u16(value, name, &fm->idle_timeout);
369 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
370 error = str_to_u16(value, name, &fm->hard_timeout);
371 } else if (fields & F_IMPORTANCE && !strcmp(name, "importance")) {
372 error = str_to_u16(value, name, &fm->importance);
373 } else if (!strcmp(name, "cookie")) {
374 char *mask = strchr(value, '/');
377 /* A mask means we're searching for a cookie. */
378 if (command == OFPFC_ADD) {
379 return xstrdup("flow additions cannot use "
383 error = str_to_be64(value, &fm->cookie);
387 error = str_to_be64(mask + 1, &fm->cookie_mask);
389 /* Matching of the cookie is only supported through NXM or
391 if (fm->cookie_mask != htonll(0)) {
392 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
395 /* No mask means that the cookie is being set. */
396 if (command != OFPFC_ADD && command != OFPFC_MODIFY
397 && command != OFPFC_MODIFY_STRICT) {
398 return xstrdup("cannot set cookie");
400 error = str_to_be64(value, &fm->new_cookie);
401 fm->modify_cookie = true;
403 } else if (mf_from_name(name)) {
404 error = parse_field(mf_from_name(name), value, &fm->match,
406 } else if (!strcmp(name, "duration")
407 || !strcmp(name, "n_packets")
408 || !strcmp(name, "n_bytes")
409 || !strcmp(name, "idle_age")
410 || !strcmp(name, "hard_age")) {
411 /* Ignore these, so that users can feed the output of
412 * "ovs-ofctl dump-flows" back into commands that parse
415 error = xasprintf("unknown keyword %s", name);
423 /* Check for usable protocol interdependencies between match fields. */
424 if (fm->match.flow.dl_type == htons(ETH_TYPE_IPV6)) {
425 const struct flow_wildcards *wc = &fm->match.wc;
426 /* Only NXM and OXM support matching L3 and L4 fields within IPv6.
428 * (IPv6 specific fields as well as arp_sha, arp_tha, nw_frag, and
429 * nw_ttl are covered elsewhere so they don't need to be included in
432 if (wc->masks.nw_proto || wc->masks.nw_tos
433 || wc->masks.tp_src || wc->masks.tp_dst) {
434 *usable_protocols &= OFPUTIL_P_NXM_OXM_ANY;
437 if (!fm->cookie_mask && fm->new_cookie == OVS_BE64_MAX
438 && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
439 /* On modifies without a mask, we are supposed to add a flow if
440 * one does not exist. If a cookie wasn't been specified, use a
441 * default of zero. */
442 fm->new_cookie = htonll(0);
444 if (fields & F_ACTIONS) {
445 enum ofputil_protocol action_usable_protocols;
446 struct ofpbuf ofpacts;
449 ofpbuf_init(&ofpacts, 32);
450 error = ofpacts_parse_instructions(act_str, &ofpacts,
451 &action_usable_protocols);
452 *usable_protocols &= action_usable_protocols;
456 err = ofpacts_check(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &fm->match.flow,
457 OFPP_MAX, fm->table_id, 255, usable_protocols);
458 if (!err && !usable_protocols) {
459 err = OFPERR_OFPBAC_MATCH_INCONSISTENT;
462 error = xasprintf("actions are invalid with specified match "
463 "(%s)", ofperr_to_string(err));
468 ofpbuf_uninit(&ofpacts);
472 fm->ofpacts_len = ofpbuf_size(&ofpacts);
473 fm->ofpacts = ofpbuf_steal_data(&ofpacts);
482 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
483 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
484 * Returns the set of usable protocols in '*usable_protocols'.
486 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
487 * constant for 'command'. To parse syntax for an OFPST_FLOW or
488 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'.
490 * Returns NULL if successful, otherwise a malloc()'d string describing the
491 * error. The caller is responsible for freeing the returned string. */
492 char * WARN_UNUSED_RESULT
493 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
494 enum ofputil_protocol *usable_protocols)
496 char *string = xstrdup(str_);
499 error = parse_ofp_str__(fm, command, string, usable_protocols);
509 static char * WARN_UNUSED_RESULT
510 parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string,
511 struct ofpbuf *bands, int command,
512 enum ofputil_protocol *usable_protocols)
519 char *save_ptr = NULL;
520 char *band_str = NULL;
523 /* Meters require at least OF 1.3. */
524 *usable_protocols = OFPUTIL_P_OF13_UP;
532 fields = F_METER | F_FLAGS | F_BANDS;
540 fields = F_METER | F_FLAGS | F_BANDS;
547 mm->command = command;
548 mm->meter.meter_id = 0;
550 if (fields & F_BANDS) {
551 band_str = strstr(string, "band");
553 return xstrdup("must specify bands");
557 band_str = strchr(band_str + 1, '=');
559 return xstrdup("must specify bands");
564 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
565 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
567 if (fields & F_FLAGS && !strcmp(name, "kbps")) {
568 mm->meter.flags |= OFPMF13_KBPS;
569 } else if (fields & F_FLAGS && !strcmp(name, "pktps")) {
570 mm->meter.flags |= OFPMF13_PKTPS;
571 } else if (fields & F_FLAGS && !strcmp(name, "burst")) {
572 mm->meter.flags |= OFPMF13_BURST;
573 } else if (fields & F_FLAGS && !strcmp(name, "stats")) {
574 mm->meter.flags |= OFPMF13_STATS;
578 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
580 return xasprintf("field %s missing value", name);
583 if (!strcmp(name, "meter")) {
584 if (!strcmp(value, "all")) {
585 mm->meter.meter_id = OFPM13_ALL;
586 } else if (!strcmp(value, "controller")) {
587 mm->meter.meter_id = OFPM13_CONTROLLER;
588 } else if (!strcmp(value, "slowpath")) {
589 mm->meter.meter_id = OFPM13_SLOWPATH;
591 char *error = str_to_u32(value, &mm->meter.meter_id);
595 if (mm->meter.meter_id > OFPM13_MAX
596 || !mm->meter.meter_id) {
597 return xasprintf("invalid value for %s", name);
601 return xasprintf("unknown keyword %s", name);
605 if (fields & F_METER && !mm->meter.meter_id) {
606 return xstrdup("must specify 'meter'");
608 if (fields & F_FLAGS && !mm->meter.flags) {
609 return xstrdup("meter must specify either 'kbps' or 'pktps'");
612 if (fields & F_BANDS) {
613 uint16_t n_bands = 0;
614 struct ofputil_meter_band *band = NULL;
617 for (name = strtok_r(band_str, "=, \t\r\n", &save_ptr); name;
618 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
622 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
624 return xasprintf("field %s missing value", name);
627 if (!strcmp(name, "type")) {
628 /* Start a new band */
629 band = ofpbuf_put_zeros(bands, sizeof *band);
632 if (!strcmp(value, "drop")) {
633 band->type = OFPMBT13_DROP;
634 } else if (!strcmp(value, "dscp_remark")) {
635 band->type = OFPMBT13_DSCP_REMARK;
637 return xasprintf("field %s unknown value %s", name, value);
639 } else if (!band || !band->type) {
640 return xstrdup("band must start with the 'type' keyword");
641 } else if (!strcmp(name, "rate")) {
642 char *error = str_to_u32(value, &band->rate);
646 } else if (!strcmp(name, "burst_size")) {
647 char *error = str_to_u32(value, &band->burst_size);
651 } else if (!strcmp(name, "prec_level")) {
652 char *error = str_to_u8(value, name, &band->prec_level);
657 return xasprintf("unknown keyword %s", name);
662 return xstrdup("meter must have bands");
665 mm->meter.n_bands = n_bands;
666 mm->meter.bands = ofpbuf_steal_data(bands);
668 for (i = 0; i < n_bands; ++i) {
669 band = &mm->meter.bands[i];
672 return xstrdup("band must have 'type'");
674 if (band->type == OFPMBT13_DSCP_REMARK) {
675 if (!band->prec_level) {
676 return xstrdup("'dscp_remark' band must have"
680 if (band->prec_level) {
681 return xstrdup("Only 'dscp_remark' band may have"
686 return xstrdup("band must have 'rate'");
688 if (mm->meter.flags & OFPMF13_BURST) {
689 if (!band->burst_size) {
690 return xstrdup("band must have 'burst_size' "
691 "when 'burst' flag is set");
694 if (band->burst_size) {
695 return xstrdup("band may have 'burst_size' only "
696 "when 'burst' flag is set");
701 mm->meter.n_bands = 0;
702 mm->meter.bands = NULL;
708 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
709 * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
711 * Returns NULL if successful, otherwise a malloc()'d string describing the
712 * error. The caller is responsible for freeing the returned string. */
713 char * WARN_UNUSED_RESULT
714 parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_,
715 int command, enum ofputil_protocol *usable_protocols)
721 ofpbuf_init(&bands, 64);
722 string = xstrdup(str_);
724 error = parse_ofp_meter_mod_str__(mm, string, &bands, command,
728 ofpbuf_uninit(&bands);
733 static char * WARN_UNUSED_RESULT
734 parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
735 const char *str_, char *string,
736 enum ofputil_protocol *usable_protocols)
738 static atomic_count id = ATOMIC_COUNT_INIT(0);
739 char *save_ptr = NULL;
742 fmr->id = atomic_count_inc(&id);
744 fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
745 | NXFMF_OWN | NXFMF_ACTIONS);
746 fmr->out_port = OFPP_NONE;
747 fmr->table_id = 0xff;
748 match_init_catchall(&fmr->match);
750 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
751 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
752 const struct protocol *p;
754 if (!strcmp(name, "!initial")) {
755 fmr->flags &= ~NXFMF_INITIAL;
756 } else if (!strcmp(name, "!add")) {
757 fmr->flags &= ~NXFMF_ADD;
758 } else if (!strcmp(name, "!delete")) {
759 fmr->flags &= ~NXFMF_DELETE;
760 } else if (!strcmp(name, "!modify")) {
761 fmr->flags &= ~NXFMF_MODIFY;
762 } else if (!strcmp(name, "!actions")) {
763 fmr->flags &= ~NXFMF_ACTIONS;
764 } else if (!strcmp(name, "!own")) {
765 fmr->flags &= ~NXFMF_OWN;
766 } else if (parse_protocol(name, &p)) {
767 match_set_dl_type(&fmr->match, htons(p->dl_type));
769 match_set_nw_proto(&fmr->match, p->nw_proto);
774 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
776 return xasprintf("%s: field %s missing value", str_, name);
779 if (!strcmp(name, "table")) {
780 char *error = str_to_u8(value, "table", &fmr->table_id);
784 } else if (!strcmp(name, "out_port")) {
785 fmr->out_port = u16_to_ofp(atoi(value));
786 } else if (mf_from_name(name)) {
789 error = parse_field(mf_from_name(name), value, &fmr->match,
795 return xasprintf("%s: unknown keyword %s", str_, name);
802 /* Convert 'str_' (as described in the documentation for the "monitor" command
803 * in the ovs-ofctl man page) into 'fmr'.
805 * Returns NULL if successful, otherwise a malloc()'d string describing the
806 * error. The caller is responsible for freeing the returned string. */
807 char * WARN_UNUSED_RESULT
808 parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
810 enum ofputil_protocol *usable_protocols)
812 char *string = xstrdup(str_);
813 char *error = parse_flow_monitor_request__(fmr, str_, string,
819 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
820 * (one of OFPFC_*) into 'fm'.
822 * Returns NULL if successful, otherwise a malloc()'d string describing the
823 * error. The caller is responsible for freeing the returned string. */
824 char * WARN_UNUSED_RESULT
825 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
827 enum ofputil_protocol *usable_protocols)
829 char *error = parse_ofp_str(fm, command, string, usable_protocols);
831 /* Normalize a copy of the match. This ensures that non-normalized
832 * flows get logged but doesn't affect what gets sent to the switch, so
833 * that the switch can do whatever it likes with the flow. */
834 struct match match_copy = fm->match;
835 ofputil_normalize_match(&match_copy);
841 /* Convert 'table_id' and 'flow_miss_handling' (as described for the
842 * "mod-table" command in the ovs-ofctl man page) into 'tm' for sending the
843 * specified table_mod 'command' to a switch.
845 * Returns NULL if successful, otherwise a malloc()'d string describing the
846 * error. The caller is responsible for freeing the returned string. */
847 char * WARN_UNUSED_RESULT
848 parse_ofp_table_mod(struct ofputil_table_mod *tm, const char *table_id,
849 const char *flow_miss_handling,
850 enum ofputil_protocol *usable_protocols)
852 /* Table mod requires at least OF 1.1. */
853 *usable_protocols = OFPUTIL_P_OF11_UP;
855 if (!strcasecmp(table_id, "all")) {
856 tm->table_id = OFPTT_ALL;
858 char *error = str_to_u8(table_id, "table_id", &tm->table_id);
864 if (strcmp(flow_miss_handling, "controller") == 0) {
865 tm->miss_config = OFPUTIL_TABLE_MISS_CONTROLLER;
866 } else if (strcmp(flow_miss_handling, "continue") == 0) {
867 tm->miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
868 } else if (strcmp(flow_miss_handling, "drop") == 0) {
869 tm->miss_config = OFPUTIL_TABLE_MISS_DROP;
871 return xasprintf("invalid flow_miss_handling %s", flow_miss_handling);
874 if (tm->table_id == 0xfe
875 && tm->miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
876 return xstrdup("last table's flow miss handling can not be continue");
883 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
884 * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated
885 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
887 * Returns NULL if successful, otherwise a malloc()'d string describing the
888 * error. The caller is responsible for freeing the returned string. */
889 char * WARN_UNUSED_RESULT
890 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
891 struct ofputil_flow_mod **fms, size_t *n_fms,
892 enum ofputil_protocol *usable_protocols)
894 size_t allocated_fms;
899 *usable_protocols = OFPUTIL_P_ANY;
904 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
905 if (stream == NULL) {
906 return xasprintf("%s: open failed (%s)",
907 file_name, ovs_strerror(errno));
910 allocated_fms = *n_fms;
913 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
915 enum ofputil_protocol usable;
917 if (*n_fms >= allocated_fms) {
918 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
920 error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command,
925 for (i = 0; i < *n_fms; i++) {
926 free(CONST_CAST(struct ofpact *, (*fms)[i].ofpacts));
933 if (stream != stdin) {
937 return xasprintf("%s:%d: %s", file_name, line_number, error);
939 *usable_protocols &= usable; /* Each line can narrow the set. */
944 if (stream != stdin) {
950 char * WARN_UNUSED_RESULT
951 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
952 bool aggregate, const char *string,
953 enum ofputil_protocol *usable_protocols)
955 struct ofputil_flow_mod fm;
958 error = parse_ofp_str(&fm, -1, string, usable_protocols);
963 /* Special table ID support not required for stats requests. */
964 if (*usable_protocols & OFPUTIL_P_OF10_STD_TID) {
965 *usable_protocols |= OFPUTIL_P_OF10_STD;
967 if (*usable_protocols & OFPUTIL_P_OF10_NXM_TID) {
968 *usable_protocols |= OFPUTIL_P_OF10_NXM;
971 fsr->aggregate = aggregate;
972 fsr->cookie = fm.cookie;
973 fsr->cookie_mask = fm.cookie_mask;
974 fsr->match = fm.match;
975 fsr->out_port = fm.out_port;
976 fsr->out_group = fm.out_group;
977 fsr->table_id = fm.table_id;
981 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
982 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
983 * mf_field. Fields must be specified in a natural order for satisfying
984 * prerequisites. If 'mask' is specified, fills the mask field for each of the
985 * field specified in flow. If the map, 'names_portno' is specfied, converts
986 * the in_port name into port no while setting the 'flow'.
988 * Returns NULL on success, otherwise a malloc()'d string that explains the
991 parse_ofp_exact_flow(struct flow *flow, struct flow *mask, const char *s,
992 const struct simap *portno_names)
994 char *pos, *key, *value_s;
998 memset(flow, 0, sizeof *flow);
1000 memset(mask, 0, sizeof *mask);
1003 pos = copy = xstrdup(s);
1004 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
1005 const struct protocol *p;
1006 if (parse_protocol(key, &p)) {
1007 if (flow->dl_type) {
1008 error = xasprintf("%s: Ethernet type set multiple times", s);
1011 flow->dl_type = htons(p->dl_type);
1013 mask->dl_type = OVS_BE16_MAX;
1017 if (flow->nw_proto) {
1018 error = xasprintf("%s: network protocol set "
1019 "multiple times", s);
1022 flow->nw_proto = p->nw_proto;
1024 mask->nw_proto = UINT8_MAX;
1028 const struct mf_field *mf;
1029 union mf_value value;
1032 mf = mf_from_name(key);
1034 error = xasprintf("%s: unknown field %s", s, key);
1038 if (!mf_are_prereqs_ok(mf, flow)) {
1039 error = xasprintf("%s: prerequisites not met for setting %s",
1044 if (!mf_is_zero(mf, flow)) {
1045 error = xasprintf("%s: field %s set multiple times", s, key);
1049 if (!strcmp(key, "in_port")
1051 && simap_contains(portno_names, value_s)) {
1052 flow->in_port.ofp_port = u16_to_ofp(
1053 simap_get(portno_names, value_s));
1055 mask->in_port.ofp_port = u16_to_ofp(ntohs(OVS_BE16_MAX));
1058 field_error = mf_parse_value(mf, value_s, &value);
1060 error = xasprintf("%s: bad value for %s (%s)",
1061 s, key, field_error);
1066 mf_set_flow_value(mf, &value, flow);
1068 mf_mask_field(mf, mask);
1074 if (!flow->in_port.ofp_port) {
1075 flow->in_port.ofp_port = OFPP_NONE;
1082 memset(flow, 0, sizeof *flow);
1084 memset(mask, 0, sizeof *mask);
1090 static char * WARN_UNUSED_RESULT
1091 parse_bucket_str(struct ofputil_bucket *bucket, char *str_,
1092 enum ofputil_protocol *usable_protocols)
1094 char *pos, *key, *value;
1095 struct ofpbuf ofpacts;
1100 bucket->bucket_id = OFPG15_BUCKET_ALL;
1101 bucket->watch_port = OFPP_ANY;
1102 bucket->watch_group = OFPG11_ANY;
1108 while (ofputil_parse_key_value(&pos, &key, &value)) {
1109 if (!strcasecmp(key, "weight")) {
1110 error = str_to_u16(value, "weight", &bucket->weight);
1111 } else if (!strcasecmp(key, "watch_port")) {
1112 if (!ofputil_port_from_string(value, &bucket->watch_port)
1113 || (ofp_to_u16(bucket->watch_port) >= ofp_to_u16(OFPP_MAX)
1114 && bucket->watch_port != OFPP_ANY)) {
1115 error = xasprintf("%s: invalid watch_port", value);
1117 } else if (!strcasecmp(key, "watch_group")) {
1118 error = str_to_u32(value, &bucket->watch_group);
1119 if (!error && bucket->watch_group > OFPG_MAX) {
1120 error = xasprintf("invalid watch_group id %"PRIu32,
1121 bucket->watch_group);
1123 } else if (!strcasecmp(key, "bucket_id")) {
1124 error = str_to_u32(value, &bucket->bucket_id);
1125 if (!error && bucket->bucket_id > OFPG15_BUCKET_MAX) {
1126 error = xasprintf("invalid bucket_id id %"PRIu32,
1129 *usable_protocols &= OFPUTIL_P_OF15_UP;
1130 } else if (!strcasecmp(key, "action") || !strcasecmp(key, "actions")) {
1131 ds_put_format(&actions, "%s,", value);
1133 ds_put_format(&actions, "%s(%s),", key, value);
1137 ds_destroy(&actions);
1142 if (!actions.length) {
1143 return xstrdup("bucket must specify actions");
1145 ds_chomp(&actions, ',');
1147 ofpbuf_init(&ofpacts, 0);
1148 error = ofpacts_parse_actions(ds_cstr(&actions), &ofpacts,
1150 ds_destroy(&actions);
1152 ofpbuf_uninit(&ofpacts);
1155 bucket->ofpacts = ofpbuf_data(&ofpacts);
1156 bucket->ofpacts_len = ofpbuf_size(&ofpacts);
1161 static char * WARN_UNUSED_RESULT
1162 parse_ofp_group_mod_str__(struct ofputil_group_mod *gm, uint16_t command,
1164 enum ofputil_protocol *usable_protocols)
1167 F_GROUP_TYPE = 1 << 0,
1170 char *save_ptr = NULL;
1171 bool had_type = false;
1173 struct ofputil_bucket *bucket;
1176 *usable_protocols = OFPUTIL_P_OF11_UP;
1180 fields = F_GROUP_TYPE | F_BUCKETS;
1183 case OFPGC11_DELETE:
1187 case OFPGC11_MODIFY:
1188 fields = F_GROUP_TYPE | F_BUCKETS;
1195 memset(gm, 0, sizeof *gm);
1196 gm->command = command;
1197 gm->group_id = OFPG_ANY;
1198 gm->command_bucket_id = OFPG15_BUCKET_ALL;
1199 list_init(&gm->buckets);
1200 if (command == OFPGC11_DELETE && string[0] == '\0') {
1201 gm->group_id = OFPG_ALL;
1205 *usable_protocols = OFPUTIL_P_OF11_UP;
1207 if (fields & F_BUCKETS) {
1208 char *bkt_str = strstr(string, "bucket=");
1217 bkt_str = strchr(bkt_str + 1, '=');
1219 error = xstrdup("must specify bucket content");
1224 next_bkt_str = strstr(bkt_str, "bucket=");
1226 *next_bkt_str = '\0';
1229 bucket = xzalloc(sizeof(struct ofputil_bucket));
1230 error = parse_bucket_str(bucket, bkt_str, usable_protocols);
1235 list_push_back(&gm->buckets, &bucket->list_node);
1237 bkt_str = next_bkt_str;
1241 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1242 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1245 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1247 error = xasprintf("field %s missing value", name);
1251 if (!strcmp(name, "group_id")) {
1252 if(!strcmp(value, "all")) {
1253 gm->group_id = OFPG_ALL;
1255 char *error = str_to_u32(value, &gm->group_id);
1259 if (gm->group_id != OFPG_ALL && gm->group_id > OFPG_MAX) {
1260 error = xasprintf("invalid group id %"PRIu32,
1265 } else if (!strcmp(name, "type")){
1266 if (!(fields & F_GROUP_TYPE)) {
1267 error = xstrdup("type is not needed");
1270 if (!strcmp(value, "all")) {
1271 gm->type = OFPGT11_ALL;
1272 } else if (!strcmp(value, "select")) {
1273 gm->type = OFPGT11_SELECT;
1274 } else if (!strcmp(value, "indirect")) {
1275 gm->type = OFPGT11_INDIRECT;
1276 } else if (!strcmp(value, "ff") ||
1277 !strcmp(value, "fast_failover")) {
1278 gm->type = OFPGT11_FF;
1280 error = xasprintf("invalid group type %s", value);
1284 } else if (!strcmp(name, "bucket")) {
1285 error = xstrdup("bucket is not needed");
1288 error = xasprintf("unknown keyword %s", name);
1292 if (gm->group_id == OFPG_ANY) {
1293 error = xstrdup("must specify a group_id");
1296 if (fields & F_GROUP_TYPE && !had_type) {
1297 error = xstrdup("must specify a type");
1301 /* Validate buckets. */
1302 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
1303 if (bucket->weight != 1 && gm->type != OFPGT11_SELECT) {
1304 error = xstrdup("Only select groups can have bucket weights.");
1308 if (gm->type == OFPGT11_INDIRECT && !list_is_short(&gm->buckets)) {
1309 error = xstrdup("Indirect groups can have at most one bucket.");
1315 ofputil_bucket_list_destroy(&gm->buckets);
1319 char * WARN_UNUSED_RESULT
1320 parse_ofp_group_mod_str(struct ofputil_group_mod *gm, uint16_t command,
1322 enum ofputil_protocol *usable_protocols)
1324 char *string = xstrdup(str_);
1325 char *error = parse_ofp_group_mod_str__(gm, command, string,
1330 ofputil_bucket_list_destroy(&gm->buckets);
1335 char * WARN_UNUSED_RESULT
1336 parse_ofp_group_mod_file(const char *file_name, uint16_t command,
1337 struct ofputil_group_mod **gms, size_t *n_gms,
1338 enum ofputil_protocol *usable_protocols)
1340 size_t allocated_gms;
1348 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1349 if (stream == NULL) {
1350 return xasprintf("%s: open failed (%s)",
1351 file_name, ovs_strerror(errno));
1354 allocated_gms = *n_gms;
1357 *usable_protocols = OFPUTIL_P_OF11_UP;
1358 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
1359 enum ofputil_protocol usable;
1362 if (*n_gms >= allocated_gms) {
1365 *gms = x2nrealloc(*gms, &allocated_gms, sizeof **gms);
1366 for (i = 0; i < *n_gms; i++) {
1367 list_moved(&(*gms)[i].buckets);
1370 error = parse_ofp_group_mod_str(&(*gms)[*n_gms], command, ds_cstr(&s),
1375 for (i = 0; i < *n_gms; i++) {
1376 ofputil_bucket_list_destroy(&(*gms)[i].buckets);
1383 if (stream != stdin) {
1387 return xasprintf("%s:%d: %s", file_name, line_number, error);
1389 *usable_protocols &= usable;
1394 if (stream != stdin) {