]>
Commit | Line | Data |
---|---|---|
f22716dc | 1 | /* |
50f96b10 | 2 | * Copyright (c) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc. |
f22716dc JP |
3 | * |
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: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
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. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | ||
00b1c62f | 19 | #include <ctype.h> |
f22716dc JP |
20 | #include <errno.h> |
21 | #include <stdlib.h> | |
b2befd5b | 22 | #include <sys/types.h> |
d787ad39 | 23 | #include <netinet/in.h> |
f22716dc | 24 | |
10a24935 | 25 | #include "byte-order.h" |
6dd3c787 | 26 | #include "dp-packet.h" |
75a75043 | 27 | #include "learn.h" |
53ddd40a | 28 | #include "multipath.h" |
f25d0cf3 | 29 | #include "netdev.h" |
f393f81e | 30 | #include "nx-match.h" |
b598f214 BW |
31 | #include "openflow/openflow.h" |
32 | #include "openvswitch/dynamic-string.h" | |
33 | #include "openvswitch/meta-flow.h" | |
34 | #include "openvswitch/ofp-actions.h" | |
35 | #include "openvswitch/ofp-parse.h" | |
f4248336 | 36 | #include "openvswitch/ofp-util.h" |
64c96779 | 37 | #include "openvswitch/ofpbuf.h" |
b598f214 | 38 | #include "openvswitch/vconn.h" |
a944ef40 | 39 | #include "ovs-thread.h" |
f22716dc | 40 | #include "packets.h" |
5a0a5702 | 41 | #include "simap.h" |
f22716dc | 42 | #include "socket-util.h" |
ee89ea7b | 43 | #include "util.h" |
f22716dc | 44 | |
bdda5aca BP |
45 | /* Parses 'str' as an 8-bit unsigned integer into '*valuep'. |
46 | * | |
47 | * 'name' describes the value parsed in an error message, if any. | |
48 | * | |
49 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
50 | * error. The caller is responsible for freeing the returned string. */ | |
cab50449 | 51 | char * OVS_WARN_UNUSED_RESULT |
bdda5aca | 52 | str_to_u8(const char *str, const char *name, uint8_t *valuep) |
c3636ffc | 53 | { |
638a19b0 | 54 | int value; |
c3636ffc | 55 | |
bdda5aca BP |
56 | if (!str_to_int(str, 0, &value) || value < 0 || value > 255) { |
57 | return xasprintf("invalid %s \"%s\"", name, str); | |
c3636ffc | 58 | } |
bdda5aca BP |
59 | *valuep = value; |
60 | return NULL; | |
c3636ffc BP |
61 | } |
62 | ||
bdda5aca BP |
63 | /* Parses 'str' as a 16-bit unsigned integer into '*valuep'. |
64 | * | |
65 | * 'name' describes the value parsed in an error message, if any. | |
66 | * | |
67 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
68 | * error. The caller is responsible for freeing the returned string. */ | |
cab50449 | 69 | char * OVS_WARN_UNUSED_RESULT |
bdda5aca | 70 | str_to_u16(const char *str, const char *name, uint16_t *valuep) |
c3636ffc BP |
71 | { |
72 | int value; | |
73 | ||
74 | if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) { | |
bdda5aca | 75 | return xasprintf("invalid %s \"%s\"", name, str); |
c3636ffc | 76 | } |
bdda5aca BP |
77 | *valuep = value; |
78 | return NULL; | |
c3636ffc BP |
79 | } |
80 | ||
bdda5aca BP |
81 | /* Parses 'str' as a 32-bit unsigned integer into '*valuep'. |
82 | * | |
83 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
84 | * error. The caller is responsible for freeing the returned string. */ | |
cab50449 | 85 | char * OVS_WARN_UNUSED_RESULT |
bdda5aca | 86 | str_to_u32(const char *str, uint32_t *valuep) |
f22716dc JP |
87 | { |
88 | char *tail; | |
89 | uint32_t value; | |
90 | ||
c4894ed4 | 91 | if (!str[0]) { |
bdda5aca | 92 | return xstrdup("missing required numeric argument"); |
ce5452cf EJ |
93 | } |
94 | ||
f22716dc JP |
95 | errno = 0; |
96 | value = strtoul(str, &tail, 0); | |
97 | if (errno == EINVAL || errno == ERANGE || *tail) { | |
bdda5aca | 98 | return xasprintf("invalid numeric format %s", str); |
f22716dc | 99 | } |
bdda5aca BP |
100 | *valuep = value; |
101 | return NULL; | |
f22716dc JP |
102 | } |
103 | ||
bdda5aca BP |
104 | /* Parses 'str' as an 64-bit unsigned integer into '*valuep'. |
105 | * | |
106 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
107 | * error. The caller is responsible for freeing the returned string. */ | |
cab50449 | 108 | char * OVS_WARN_UNUSED_RESULT |
bdda5aca | 109 | str_to_u64(const char *str, uint64_t *valuep) |
f22716dc JP |
110 | { |
111 | char *tail; | |
112 | uint64_t value; | |
113 | ||
c4894ed4 | 114 | if (!str[0]) { |
bdda5aca | 115 | return xstrdup("missing required numeric argument"); |
c4894ed4 BP |
116 | } |
117 | ||
f22716dc JP |
118 | errno = 0; |
119 | value = strtoull(str, &tail, 0); | |
120 | if (errno == EINVAL || errno == ERANGE || *tail) { | |
bdda5aca | 121 | return xasprintf("invalid numeric format %s", str); |
f22716dc | 122 | } |
bdda5aca BP |
123 | *valuep = value; |
124 | return NULL; | |
f22716dc JP |
125 | } |
126 | ||
bdda5aca BP |
127 | /* Parses 'str' as an 64-bit unsigned integer in network byte order into |
128 | * '*valuep'. | |
129 | * | |
130 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
131 | * error. The caller is responsible for freeing the returned string. */ | |
cab50449 | 132 | char * OVS_WARN_UNUSED_RESULT |
bdda5aca BP |
133 | str_to_be64(const char *str, ovs_be64 *valuep) |
134 | { | |
4be17953 | 135 | uint64_t value = 0; |
bdda5aca BP |
136 | char *error; |
137 | ||
138 | error = str_to_u64(str, &value); | |
139 | if (!error) { | |
140 | *valuep = htonll(value); | |
141 | } | |
142 | return error; | |
143 | } | |
144 | ||
145 | /* Parses 'str' as an Ethernet address into 'mac'. | |
146 | * | |
147 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
148 | * error. The caller is responsible for freeing the returned string. */ | |
cab50449 | 149 | char * OVS_WARN_UNUSED_RESULT |
74ff3298 | 150 | str_to_mac(const char *str, struct eth_addr *mac) |
f22716dc | 151 | { |
74ff3298 | 152 | if (!ovs_scan(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(*mac))) { |
bdda5aca | 153 | return xasprintf("invalid mac address %s", str); |
f22716dc | 154 | } |
bdda5aca | 155 | return NULL; |
f22716dc JP |
156 | } |
157 | ||
bdda5aca BP |
158 | /* Parses 'str' as an IP address into '*ip'. |
159 | * | |
160 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
161 | * error. The caller is responsible for freeing the returned string. */ | |
cab50449 | 162 | char * OVS_WARN_UNUSED_RESULT |
6a885fd0 | 163 | str_to_ip(const char *str, ovs_be32 *ip) |
cb8ca532 | 164 | { |
f22716dc | 165 | struct in_addr in_addr; |
f22716dc | 166 | |
6a885fd0 | 167 | if (lookup_ip(str, &in_addr)) { |
bdda5aca | 168 | return xasprintf("%s: could not convert to IP address", str); |
f22716dc JP |
169 | } |
170 | *ip = in_addr.s_addr; | |
bdda5aca | 171 | return NULL; |
d31f1109 JP |
172 | } |
173 | ||
d787ad39 JS |
174 | /* Parses 'str' as a conntrack helper into 'alg'. |
175 | * | |
176 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
177 | * error. The caller is responsible for freeing the returned string. */ | |
178 | char * OVS_WARN_UNUSED_RESULT | |
179 | str_to_connhelper(const char *str, uint16_t *alg) | |
180 | { | |
181 | if (!strcmp(str, "ftp")) { | |
182 | *alg = IPPORT_FTP; | |
183 | return NULL; | |
40c7b2fc JS |
184 | } |
185 | if (!strcmp(str, "tftp")) { | |
186 | *alg = IPPORT_TFTP; | |
187 | return NULL; | |
d787ad39 JS |
188 | } |
189 | return xasprintf("invalid conntrack helper \"%s\"", str); | |
190 | } | |
191 | ||
f22716dc JP |
192 | struct protocol { |
193 | const char *name; | |
194 | uint16_t dl_type; | |
195 | uint8_t nw_proto; | |
196 | }; | |
197 | ||
198 | static bool | |
199 | parse_protocol(const char *name, const struct protocol **p_out) | |
200 | { | |
201 | static const struct protocol protocols[] = { | |
202 | { "ip", ETH_TYPE_IP, 0 }, | |
64cd4a05 JP |
203 | { "ipv4", ETH_TYPE_IP, 0 }, |
204 | { "ip4", ETH_TYPE_IP, 0 }, | |
f22716dc | 205 | { "arp", ETH_TYPE_ARP, 0 }, |
6767a2cc JP |
206 | { "icmp", ETH_TYPE_IP, IPPROTO_ICMP }, |
207 | { "tcp", ETH_TYPE_IP, IPPROTO_TCP }, | |
208 | { "udp", ETH_TYPE_IP, IPPROTO_UDP }, | |
0d56eaf2 | 209 | { "sctp", ETH_TYPE_IP, IPPROTO_SCTP }, |
d31f1109 JP |
210 | { "ipv6", ETH_TYPE_IPV6, 0 }, |
211 | { "ip6", ETH_TYPE_IPV6, 0 }, | |
212 | { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 }, | |
213 | { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP }, | |
214 | { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP }, | |
0d56eaf2 | 215 | { "sctp6", ETH_TYPE_IPV6, IPPROTO_SCTP }, |
8087f5ff | 216 | { "rarp", ETH_TYPE_RARP, 0}, |
b02475c5 SH |
217 | { "mpls", ETH_TYPE_MPLS, 0 }, |
218 | { "mplsm", ETH_TYPE_MPLS_MCAST, 0 }, | |
219 | }; | |
f22716dc JP |
220 | const struct protocol *p; |
221 | ||
222 | for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) { | |
223 | if (!strcmp(p->name, name)) { | |
224 | *p_out = p; | |
225 | return true; | |
226 | } | |
227 | } | |
228 | *p_out = NULL; | |
229 | return false; | |
230 | } | |
231 | ||
bdda5aca | 232 | /* Parses 's' as the (possibly masked) value of field 'mf', and updates |
db0b6c29 JR |
233 | * 'match' appropriately. Restricts the set of usable protocols to ones |
234 | * supporting the parsed field. | |
bdda5aca BP |
235 | * |
236 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
237 | * error. The caller is responsible for freeing the returned string. */ | |
cab50449 | 238 | static char * OVS_WARN_UNUSED_RESULT |
50f96b10 BP |
239 | parse_field(const struct mf_field *mf, const char *s, |
240 | const struct ofputil_port_map *port_map, struct match *match, | |
db0b6c29 | 241 | enum ofputil_protocol *usable_protocols) |
8050b31d | 242 | { |
6a885fd0 BP |
243 | union mf_value value, mask; |
244 | char *error; | |
bad68a99 | 245 | |
0eede6b6 JG |
246 | if (!*s) { |
247 | /* If there's no string, we're just trying to match on the | |
248 | * existence of the field, so use a no-op value. */ | |
249 | s = "0/0"; | |
250 | } | |
251 | ||
50f96b10 | 252 | error = mf_parse(mf, s, port_map, &value, &mask); |
bdda5aca | 253 | if (!error) { |
4f7b100c | 254 | *usable_protocols &= mf_set(mf, &value, &mask, match, &error); |
3d4b2e6e | 255 | match_add_ethernet_prereq(match, mf); |
8050b31d | 256 | } |
bdda5aca | 257 | return error; |
00b1c62f BP |
258 | } |
259 | ||
21b2fa61 JR |
260 | /* Parses 'str_value' as the value of subfield 'name', and updates |
261 | * 'match' appropriately. Restricts the set of usable protocols to ones | |
262 | * supporting the parsed field. | |
263 | * | |
264 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
265 | * error. The caller is responsible for freeing the returned string. */ | |
266 | static char * OVS_WARN_UNUSED_RESULT | |
267 | parse_subfield(const char *name, const char *str_value, struct match *match, | |
268 | enum ofputil_protocol *usable_protocols) | |
269 | { | |
270 | struct mf_subfield sf; | |
271 | char *error; | |
272 | ||
273 | error = mf_parse_subfield(&sf, name); | |
274 | if (!error) { | |
275 | union mf_value val; | |
276 | char *tail; | |
277 | if (parse_int_string(str_value, (uint8_t *)&val, sf.field->n_bytes, | |
278 | &tail) || *tail != 0) { | |
279 | return xasprintf("%s: cannot parse integer value: %s", name, | |
280 | str_value); | |
281 | } | |
282 | if (!bitwise_is_all_zeros(&val, sf.field->n_bytes, sf.n_bits, | |
283 | sf.field->n_bytes * 8 - sf.n_bits)) { | |
284 | struct ds ds; | |
285 | ||
286 | ds_init(&ds); | |
50f96b10 | 287 | mf_format(sf.field, &val, NULL, NULL, &ds); |
21b2fa61 JR |
288 | error = xasprintf("%s: value %s does not fit into %d bits", |
289 | name, ds_cstr(&ds), sf.n_bits); | |
290 | ds_destroy(&ds); | |
291 | return error; | |
292 | } | |
293 | ||
294 | const struct mf_field *field = sf.field; | |
295 | union mf_value value, mask; | |
70bd4e6d | 296 | unsigned int size = field->n_bytes; |
21b2fa61 JR |
297 | |
298 | mf_get(field, match, &value, &mask); | |
70bd4e6d JR |
299 | bitwise_copy(&val, size, 0, &value, size, sf.ofs, sf.n_bits); |
300 | bitwise_one ( &mask, size, sf.ofs, sf.n_bits); | |
21b2fa61 | 301 | *usable_protocols &= mf_set(field, &value, &mask, match, &error); |
3d4b2e6e JS |
302 | |
303 | match_add_ethernet_prereq(match, sf.field); | |
21b2fa61 JR |
304 | } |
305 | return error; | |
306 | } | |
307 | ||
c2d936a4 BP |
308 | static char * |
309 | extract_actions(char *s) | |
310 | { | |
311 | s = strstr(s, "action"); | |
312 | if (s) { | |
313 | *s = '\0'; | |
314 | s = strchr(s + 1, '='); | |
315 | return s ? s + 1 : NULL; | |
316 | } else { | |
317 | return NULL; | |
318 | } | |
319 | } | |
320 | ||
321 | ||
cab50449 | 322 | static char * OVS_WARN_UNUSED_RESULT |
db0b6c29 | 323 | parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string, |
50f96b10 | 324 | const struct ofputil_port_map *port_map, |
ba2fe8e9 | 325 | enum ofputil_protocol *usable_protocols) |
f22716dc | 326 | { |
c821124b BP |
327 | enum { |
328 | F_OUT_PORT = 1 << 0, | |
329 | F_ACTIONS = 1 << 1, | |
ca26eb44 | 330 | F_IMPORTANCE = 1 << 2, |
c821124b | 331 | F_TIMEOUT = 1 << 3, |
a993007b BP |
332 | F_PRIORITY = 1 << 4, |
333 | F_FLAGS = 1 << 5, | |
c821124b | 334 | } fields; |
75a75043 | 335 | char *act_str = NULL; |
bc78455b | 336 | char *name, *value; |
f22716dc | 337 | |
db0b6c29 JR |
338 | *usable_protocols = OFPUTIL_P_ANY; |
339 | ||
db5076ee JR |
340 | if (command == -2) { |
341 | size_t len; | |
342 | ||
343 | string += strspn(string, " \t\r\n"); /* Skip white space. */ | |
344 | len = strcspn(string, ", \t\r\n"); /* Get length of the first token. */ | |
345 | ||
346 | if (!strncmp(string, "add", len)) { | |
347 | command = OFPFC_ADD; | |
348 | } else if (!strncmp(string, "delete", len)) { | |
349 | command = OFPFC_DELETE; | |
350 | } else if (!strncmp(string, "delete_strict", len)) { | |
351 | command = OFPFC_DELETE_STRICT; | |
352 | } else if (!strncmp(string, "modify", len)) { | |
353 | command = OFPFC_MODIFY; | |
354 | } else if (!strncmp(string, "modify_strict", len)) { | |
355 | command = OFPFC_MODIFY_STRICT; | |
356 | } else { | |
357 | len = 0; | |
358 | command = OFPFC_ADD; | |
359 | } | |
360 | string += len; | |
361 | } | |
362 | ||
c821124b BP |
363 | switch (command) { |
364 | case -1: | |
365 | fields = F_OUT_PORT; | |
366 | break; | |
367 | ||
368 | case OFPFC_ADD: | |
ca26eb44 | 369 | fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS | F_IMPORTANCE; |
c821124b BP |
370 | break; |
371 | ||
372 | case OFPFC_DELETE: | |
373 | fields = F_OUT_PORT; | |
374 | break; | |
375 | ||
376 | case OFPFC_DELETE_STRICT: | |
377 | fields = F_OUT_PORT | F_PRIORITY; | |
378 | break; | |
379 | ||
380 | case OFPFC_MODIFY: | |
a993007b | 381 | fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS; |
c821124b BP |
382 | break; |
383 | ||
384 | case OFPFC_MODIFY_STRICT: | |
a993007b | 385 | fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS; |
c821124b BP |
386 | break; |
387 | ||
388 | default: | |
428b2edd | 389 | OVS_NOT_REACHED(); |
c821124b BP |
390 | } |
391 | ||
39cc5c4a BP |
392 | *fm = (struct ofputil_flow_mod) { |
393 | .match = MATCH_CATCHALL_INITIALIZER, | |
394 | .priority = OFP_DEFAULT_PRIORITY, | |
395 | .table_id = 0xff, | |
396 | .command = command, | |
397 | .buffer_id = UINT32_MAX, | |
398 | .out_port = OFPP_ANY, | |
399 | .out_group = OFPG_ANY, | |
39cc5c4a BP |
400 | }; |
401 | /* For modify, by default, don't update the cookie. */ | |
623e1caf | 402 | if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) { |
b8266395 | 403 | fm->new_cookie = OVS_BE64_MAX; |
623e1caf | 404 | } |
39cc5c4a | 405 | |
c821124b | 406 | if (fields & F_ACTIONS) { |
c2d936a4 | 407 | act_str = extract_actions(string); |
f22716dc | 408 | if (!act_str) { |
bdda5aca | 409 | return xstrdup("must specify an action"); |
f22716dc | 410 | } |
f22716dc | 411 | } |
bc78455b JG |
412 | |
413 | while (ofputil_parse_key_value(&string, &name, &value)) { | |
f22716dc | 414 | const struct protocol *p; |
21b2fa61 | 415 | const struct mf_field *mf; |
bdda5aca | 416 | char *error = NULL; |
f22716dc JP |
417 | |
418 | if (parse_protocol(name, &p)) { | |
81a76618 | 419 | match_set_dl_type(&fm->match, htons(p->dl_type)); |
f22716dc | 420 | if (p->nw_proto) { |
81a76618 | 421 | match_set_nw_proto(&fm->match, p->nw_proto); |
f22716dc | 422 | } |
3d4b2e6e JS |
423 | match_set_default_packet_type(&fm->match); |
424 | } else if (!strcmp(name, "eth")) { | |
425 | match_set_packet_type(&fm->match, htonl(PT_ETH)); | |
a993007b | 426 | } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) { |
0fb88c18 | 427 | fm->flags |= OFPUTIL_FF_SEND_FLOW_REM; |
a993007b | 428 | } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) { |
0fb88c18 | 429 | fm->flags |= OFPUTIL_FF_CHECK_OVERLAP; |
2e1ae200 | 430 | } else if (fields & F_FLAGS && !strcmp(name, "reset_counts")) { |
0fb88c18 | 431 | fm->flags |= OFPUTIL_FF_RESET_COUNTS; |
db0b6c29 | 432 | *usable_protocols &= OFPUTIL_P_OF12_UP; |
2e1ae200 | 433 | } else if (fields & F_FLAGS && !strcmp(name, "no_packet_counts")) { |
0fb88c18 | 434 | fm->flags |= OFPUTIL_FF_NO_PKT_COUNTS; |
db0b6c29 | 435 | *usable_protocols &= OFPUTIL_P_OF13_UP; |
2e1ae200 | 436 | } else if (fields & F_FLAGS && !strcmp(name, "no_byte_counts")) { |
0fb88c18 | 437 | fm->flags |= OFPUTIL_FF_NO_BYT_COUNTS; |
db0b6c29 | 438 | *usable_protocols &= OFPUTIL_P_OF13_UP; |
adcf00ba AZ |
439 | } else if (!strcmp(name, "no_readonly_table") |
440 | || !strcmp(name, "allow_hidden_fields")) { | |
441 | /* ignore these fields. */ | |
21b2fa61 | 442 | } else if ((mf = mf_from_name(name)) != NULL) { |
50f96b10 BP |
443 | error = parse_field(mf, value, port_map, |
444 | &fm->match, usable_protocols); | |
21b2fa61 JR |
445 | } else if (strchr(name, '[')) { |
446 | error = parse_subfield(name, value, &fm->match, usable_protocols); | |
f22716dc | 447 | } else { |
bc78455b | 448 | if (!*value) { |
bdda5aca | 449 | return xasprintf("field %s missing value", name); |
f22716dc JP |
450 | } |
451 | ||
6c1491fb | 452 | if (!strcmp(name, "table")) { |
bdda5aca | 453 | error = str_to_u8(value, "table", &fm->table_id); |
db0b6c29 JR |
454 | if (fm->table_id != 0xff) { |
455 | *usable_protocols &= OFPUTIL_P_TID; | |
456 | } | |
19ba1422 | 457 | } else if (fields & F_OUT_PORT && !strcmp(name, "out_port")) { |
50f96b10 BP |
458 | if (!ofputil_port_from_string(value, port_map, |
459 | &fm->out_port)) { | |
bdda5aca BP |
460 | error = xasprintf("%s is not a valid OpenFlow port", |
461 | value); | |
c6100d92 | 462 | } |
6d5d1f3b BP |
463 | } else if (fields & F_OUT_PORT && !strcmp(name, "out_group")) { |
464 | *usable_protocols &= OFPUTIL_P_OF11_UP; | |
465 | if (!ofputil_group_from_string(value, &fm->out_group)) { | |
466 | error = xasprintf("%s is not a valid OpenFlow group", | |
467 | value); | |
468 | } | |
c821124b | 469 | } else if (fields & F_PRIORITY && !strcmp(name, "priority")) { |
4be17953 | 470 | uint16_t priority = 0; |
bdda5aca BP |
471 | |
472 | error = str_to_u16(value, name, &priority); | |
473 | fm->priority = priority; | |
c821124b | 474 | } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) { |
bdda5aca | 475 | error = str_to_u16(value, name, &fm->idle_timeout); |
c821124b | 476 | } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) { |
bdda5aca | 477 | error = str_to_u16(value, name, &fm->hard_timeout); |
ca26eb44 RB |
478 | } else if (fields & F_IMPORTANCE && !strcmp(name, "importance")) { |
479 | error = str_to_u16(value, name, &fm->importance); | |
e729e793 JP |
480 | } else if (!strcmp(name, "cookie")) { |
481 | char *mask = strchr(value, '/'); | |
623e1caf | 482 | |
e729e793 | 483 | if (mask) { |
623e1caf | 484 | /* A mask means we're searching for a cookie. */ |
e729e793 | 485 | if (command == OFPFC_ADD) { |
bdda5aca BP |
486 | return xstrdup("flow additions cannot use " |
487 | "a cookie mask"); | |
e729e793 JP |
488 | } |
489 | *mask = '\0'; | |
bdda5aca BP |
490 | error = str_to_be64(value, &fm->cookie); |
491 | if (error) { | |
492 | return error; | |
493 | } | |
494 | error = str_to_be64(mask + 1, &fm->cookie_mask); | |
db0b6c29 JR |
495 | |
496 | /* Matching of the cookie is only supported through NXM or | |
497 | * OF1.1+. */ | |
498 | if (fm->cookie_mask != htonll(0)) { | |
499 | *usable_protocols &= OFPUTIL_P_NXM_OF11_UP; | |
500 | } | |
e729e793 | 501 | } else { |
623e1caf JP |
502 | /* No mask means that the cookie is being set. */ |
503 | if (command != OFPFC_ADD && command != OFPFC_MODIFY | |
bdda5aca BP |
504 | && command != OFPFC_MODIFY_STRICT) { |
505 | return xstrdup("cannot set cookie"); | |
623e1caf | 506 | } |
bdda5aca | 507 | error = str_to_be64(value, &fm->new_cookie); |
23342857 | 508 | fm->modify_cookie = true; |
e729e793 | 509 | } |
2c6d8411 BP |
510 | } else if (!strcmp(name, "duration") |
511 | || !strcmp(name, "n_packets") | |
146356e9 JP |
512 | || !strcmp(name, "n_bytes") |
513 | || !strcmp(name, "idle_age") | |
514 | || !strcmp(name, "hard_age")) { | |
2c6d8411 BP |
515 | /* Ignore these, so that users can feed the output of |
516 | * "ovs-ofctl dump-flows" back into commands that parse | |
517 | * flows. */ | |
f22716dc | 518 | } else { |
bdda5aca BP |
519 | error = xasprintf("unknown keyword %s", name); |
520 | } | |
bc78455b | 521 | } |
bdda5aca | 522 | |
bc78455b JG |
523 | if (error) { |
524 | return error; | |
f22716dc JP |
525 | } |
526 | } | |
3d4b2e6e JS |
527 | /* Copy ethertype to flow->dl_type for matches on packet_type |
528 | * (OFPHTN_ETHERTYPE, ethertype). */ | |
529 | if (fm->match.wc.masks.packet_type == OVS_BE32_MAX && | |
530 | pt_ns(fm->match.flow.packet_type) == OFPHTN_ETHERTYPE) { | |
531 | fm->match.flow.dl_type = pt_ns_type_be(fm->match.flow.packet_type); | |
532 | } | |
db0b6c29 JR |
533 | /* Check for usable protocol interdependencies between match fields. */ |
534 | if (fm->match.flow.dl_type == htons(ETH_TYPE_IPV6)) { | |
535 | const struct flow_wildcards *wc = &fm->match.wc; | |
536 | /* Only NXM and OXM support matching L3 and L4 fields within IPv6. | |
537 | * | |
538 | * (IPv6 specific fields as well as arp_sha, arp_tha, nw_frag, and | |
539 | * nw_ttl are covered elsewhere so they don't need to be included in | |
540 | * this test too.) | |
541 | */ | |
542 | if (wc->masks.nw_proto || wc->masks.nw_tos | |
543 | || wc->masks.tp_src || wc->masks.tp_dst) { | |
544 | *usable_protocols &= OFPUTIL_P_NXM_OXM_ANY; | |
545 | } | |
546 | } | |
b8266395 | 547 | if (!fm->cookie_mask && fm->new_cookie == OVS_BE64_MAX |
bdda5aca | 548 | && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) { |
623e1caf JP |
549 | /* On modifies without a mask, we are supposed to add a flow if |
550 | * one does not exist. If a cookie wasn't been specified, use a | |
551 | * default of zero. */ | |
552 | fm->new_cookie = htonll(0); | |
553 | } | |
75a75043 | 554 | if (fields & F_ACTIONS) { |
c2d936a4 | 555 | enum ofputil_protocol action_usable_protocols; |
f25d0cf3 | 556 | struct ofpbuf ofpacts; |
bdda5aca | 557 | char *error; |
75a75043 | 558 | |
f25d0cf3 | 559 | ofpbuf_init(&ofpacts, 32); |
50f96b10 | 560 | error = ofpacts_parse_instructions(act_str, port_map, &ofpacts, |
c2d936a4 BP |
561 | &action_usable_protocols); |
562 | *usable_protocols &= action_usable_protocols; | |
bdda5aca BP |
563 | if (!error) { |
564 | enum ofperr err; | |
565 | ||
67210a55 | 566 | err = ofpacts_check(ofpacts.data, ofpacts.size, &fm->match, |
ba2fe8e9 | 567 | OFPP_MAX, fm->table_id, 255, usable_protocols); |
a805558c | 568 | if (!err && !*usable_protocols) { |
ba2fe8e9 BP |
569 | err = OFPERR_OFPBAC_MATCH_INCONSISTENT; |
570 | } | |
bdda5aca | 571 | if (err) { |
ba2fe8e9 BP |
572 | error = xasprintf("actions are invalid with specified match " |
573 | "(%s)", ofperr_to_string(err)); | |
bdda5aca | 574 | } |
ba2fe8e9 | 575 | |
bdda5aca BP |
576 | } |
577 | if (error) { | |
578 | ofpbuf_uninit(&ofpacts); | |
579 | return error; | |
b019d34d SH |
580 | } |
581 | ||
6fd6ed71 | 582 | fm->ofpacts_len = ofpacts.size; |
bdda5aca | 583 | fm->ofpacts = ofpbuf_steal_data(&ofpacts); |
75a75043 | 584 | } else { |
f25d0cf3 BP |
585 | fm->ofpacts_len = 0; |
586 | fm->ofpacts = NULL; | |
75a75043 | 587 | } |
ec610b7b | 588 | |
bdda5aca | 589 | return NULL; |
f22716dc | 590 | } |
15f1f1b6 | 591 | |
638a19b0 | 592 | /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man |
bdda5aca | 593 | * page) into 'fm' for sending the specified flow_mod 'command' to a switch. |
db0b6c29 | 594 | * Returns the set of usable protocols in '*usable_protocols'. |
bdda5aca BP |
595 | * |
596 | * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_* | |
597 | * constant for 'command'. To parse syntax for an OFPST_FLOW or | |
598 | * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. | |
599 | * | |
db5076ee JR |
600 | * If 'command' is given as -2, 'str_' may begin with a command name ("add", |
601 | * "modify", "delete", "modify_strict", or "delete_strict"). A missing command | |
602 | * name is treated as "add". | |
603 | * | |
bdda5aca BP |
604 | * Returns NULL if successful, otherwise a malloc()'d string describing the |
605 | * error. The caller is responsible for freeing the returned string. */ | |
cab50449 | 606 | char * OVS_WARN_UNUSED_RESULT |
db0b6c29 | 607 | parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_, |
50f96b10 | 608 | const struct ofputil_port_map *port_map, |
ba2fe8e9 | 609 | enum ofputil_protocol *usable_protocols) |
bdda5aca BP |
610 | { |
611 | char *string = xstrdup(str_); | |
612 | char *error; | |
613 | ||
50f96b10 | 614 | error = parse_ofp_str__(fm, command, string, port_map, usable_protocols); |
bdda5aca BP |
615 | if (error) { |
616 | fm->ofpacts = NULL; | |
617 | fm->ofpacts_len = 0; | |
618 | } | |
619 | ||
620 | free(string); | |
621 | return error; | |
622 | } | |
623 | ||
6dd3c787 JR |
624 | /* Parse a string representation of a OFPT_PACKET_OUT to '*po'. If successful, |
625 | * both 'po->ofpacts' and 'po->packet' must be free()d by the caller. */ | |
626 | static char * OVS_WARN_UNUSED_RESULT | |
627 | parse_ofp_packet_out_str__(struct ofputil_packet_out *po, char *string, | |
50f96b10 | 628 | const struct ofputil_port_map *port_map, |
6dd3c787 JR |
629 | enum ofputil_protocol *usable_protocols) |
630 | { | |
631 | enum ofputil_protocol action_usable_protocols; | |
632 | uint64_t stub[256 / 8]; | |
633 | struct ofpbuf ofpacts = OFPBUF_STUB_INITIALIZER(stub); | |
634 | struct dp_packet *packet = NULL; | |
635 | char *act_str = NULL; | |
636 | char *name, *value; | |
637 | char *error = NULL; | |
638 | ||
639 | *usable_protocols = OFPUTIL_P_ANY; | |
640 | ||
641 | *po = (struct ofputil_packet_out) { | |
642 | .buffer_id = UINT32_MAX, | |
6dd3c787 | 643 | }; |
880b1458 | 644 | match_init_catchall(&po->flow_metadata); |
35eb6326 | 645 | match_set_in_port(&po->flow_metadata, OFPP_CONTROLLER); |
6dd3c787 JR |
646 | |
647 | act_str = extract_actions(string); | |
648 | ||
649 | while (ofputil_parse_key_value(&string, &name, &value)) { | |
650 | if (!*value) { | |
651 | error = xasprintf("field %s missing value", name); | |
652 | goto out; | |
653 | } | |
654 | ||
655 | if (!strcmp(name, "in_port")) { | |
35eb6326 | 656 | ofp_port_t in_port; |
50f96b10 | 657 | if (!ofputil_port_from_string(value, port_map, &in_port)) { |
6dd3c787 JR |
658 | error = xasprintf("%s is not a valid OpenFlow port", value); |
659 | goto out; | |
660 | } | |
35eb6326 YHW |
661 | if (ofp_to_u16(in_port) > ofp_to_u16(OFPP_MAX) |
662 | && in_port != OFPP_LOCAL | |
663 | && in_port != OFPP_NONE | |
664 | && in_port != OFPP_CONTROLLER) { | |
6dd3c787 JR |
665 | error = xasprintf( |
666 | "%s is not a valid OpenFlow port for PACKET_OUT", | |
667 | value); | |
668 | goto out; | |
669 | } | |
35eb6326 | 670 | match_set_in_port(&po->flow_metadata, in_port); |
cb1145d1 ZB |
671 | } else if (!strcmp(name, "packet_type")) { |
672 | char *ns = value; | |
673 | char *ns_type = strstr(value, ","); | |
674 | if (ns_type) { | |
675 | ovs_be32 packet_type; | |
676 | *ns_type = '\0'; | |
677 | packet_type = PACKET_TYPE_BE(strtoul(ns, NULL, 0), | |
678 | strtoul(++ns_type, NULL, 0)); | |
679 | match_set_packet_type(&po->flow_metadata, packet_type); | |
680 | } else { | |
681 | error = xasprintf("%s(%s) can't be interpreted", name, value); | |
682 | goto out; | |
683 | } | |
6dd3c787 JR |
684 | } else if (!strcmp(name, "packet")) { |
685 | const char *error_msg = eth_from_hex(value, &packet); | |
686 | if (error_msg) { | |
687 | error = xasprintf("%s: %s", name, error_msg); | |
688 | goto out; | |
689 | } | |
690 | } else { | |
880b1458 YHW |
691 | const struct mf_field *mf = mf_from_name(name); |
692 | if (!mf) { | |
693 | error = xasprintf("unknown keyword %s", name); | |
694 | goto out; | |
695 | } | |
696 | ||
50f96b10 | 697 | error = parse_field(mf, value, port_map, &po->flow_metadata, |
880b1458 YHW |
698 | usable_protocols); |
699 | if (error) { | |
700 | goto out; | |
701 | } | |
702 | if (!mf_is_pipeline_field(mf)) { | |
703 | error = xasprintf("%s is not a valid pipeline field " | |
704 | "for PACKET_OUT", name); | |
705 | goto out; | |
706 | } | |
6dd3c787 JR |
707 | } |
708 | } | |
709 | ||
710 | if (!packet || !dp_packet_size(packet)) { | |
711 | error = xstrdup("must specify packet"); | |
712 | goto out; | |
713 | } | |
714 | ||
715 | if (act_str) { | |
50f96b10 | 716 | error = ofpacts_parse_actions(act_str, port_map, &ofpacts, |
6dd3c787 JR |
717 | &action_usable_protocols); |
718 | *usable_protocols &= action_usable_protocols; | |
719 | if (error) { | |
720 | goto out; | |
721 | } | |
722 | } | |
723 | po->ofpacts_len = ofpacts.size; | |
724 | po->ofpacts = ofpbuf_steal_data(&ofpacts); | |
725 | ||
726 | po->packet_len = dp_packet_size(packet); | |
727 | po->packet = dp_packet_steal_data(packet); | |
728 | out: | |
729 | ofpbuf_uninit(&ofpacts); | |
730 | dp_packet_delete(packet); | |
731 | return error; | |
732 | } | |
733 | ||
734 | /* Convert 'str_' (as described in the Packet-Out Syntax section of the | |
735 | * ovs-ofctl man page) into 'po' for sending a OFPT_PACKET_OUT message to a | |
736 | * switch. Returns the set of usable protocols in '*usable_protocols'. | |
737 | * | |
738 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
665c4688 JP |
739 | * error. The caller is responsible for freeing the returned string. |
740 | * If successful, both 'po->ofpacts' and 'po->packet' must be free()d by | |
741 | * the caller. */ | |
6dd3c787 JR |
742 | char * OVS_WARN_UNUSED_RESULT |
743 | parse_ofp_packet_out_str(struct ofputil_packet_out *po, const char *str_, | |
50f96b10 | 744 | const struct ofputil_port_map *port_map, |
6dd3c787 JR |
745 | enum ofputil_protocol *usable_protocols) |
746 | { | |
747 | char *string = xstrdup(str_); | |
748 | char *error; | |
749 | ||
50f96b10 | 750 | error = parse_ofp_packet_out_str__(po, string, port_map, usable_protocols); |
6dd3c787 JR |
751 | if (error) { |
752 | po->ofpacts = NULL; | |
753 | po->ofpacts_len = 0; | |
754 | } | |
755 | ||
756 | free(string); | |
757 | return error; | |
758 | } | |
759 | ||
2f0b69ac JP |
760 | /* Parse a string representation of a meter modification message to '*mm'. |
761 | * If successful, 'mm->meter.bands' must be free()d by the caller. */ | |
cab50449 | 762 | static char * OVS_WARN_UNUSED_RESULT |
bdda5aca | 763 | parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string, |
db0b6c29 JR |
764 | struct ofpbuf *bands, int command, |
765 | enum ofputil_protocol *usable_protocols) | |
638a19b0 JR |
766 | { |
767 | enum { | |
768 | F_METER = 1 << 0, | |
769 | F_FLAGS = 1 << 1, | |
770 | F_BANDS = 1 << 2, | |
771 | } fields; | |
638a19b0 JR |
772 | char *save_ptr = NULL; |
773 | char *band_str = NULL; | |
774 | char *name; | |
775 | ||
db0b6c29 | 776 | /* Meters require at least OF 1.3. */ |
040f4db8 | 777 | *usable_protocols = OFPUTIL_P_OF13_UP; |
db0b6c29 | 778 | |
638a19b0 JR |
779 | switch (command) { |
780 | case -1: | |
781 | fields = F_METER; | |
782 | break; | |
783 | ||
784 | case OFPMC13_ADD: | |
785 | fields = F_METER | F_FLAGS | F_BANDS; | |
786 | break; | |
787 | ||
788 | case OFPMC13_DELETE: | |
789 | fields = F_METER; | |
790 | break; | |
791 | ||
792 | case OFPMC13_MODIFY: | |
793 | fields = F_METER | F_FLAGS | F_BANDS; | |
794 | break; | |
795 | ||
796 | default: | |
428b2edd | 797 | OVS_NOT_REACHED(); |
638a19b0 JR |
798 | } |
799 | ||
800 | mm->command = command; | |
801 | mm->meter.meter_id = 0; | |
802 | mm->meter.flags = 0; | |
2f0b69ac JP |
803 | mm->meter.n_bands = 0; |
804 | mm->meter.bands = NULL; | |
805 | ||
638a19b0 JR |
806 | if (fields & F_BANDS) { |
807 | band_str = strstr(string, "band"); | |
808 | if (!band_str) { | |
bdda5aca | 809 | return xstrdup("must specify bands"); |
638a19b0 JR |
810 | } |
811 | *band_str = '\0'; | |
812 | ||
813 | band_str = strchr(band_str + 1, '='); | |
814 | if (!band_str) { | |
bdda5aca | 815 | return xstrdup("must specify bands"); |
638a19b0 JR |
816 | } |
817 | ||
818 | band_str++; | |
819 | } | |
820 | for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name; | |
821 | name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) { | |
822 | ||
823 | if (fields & F_FLAGS && !strcmp(name, "kbps")) { | |
824 | mm->meter.flags |= OFPMF13_KBPS; | |
825 | } else if (fields & F_FLAGS && !strcmp(name, "pktps")) { | |
826 | mm->meter.flags |= OFPMF13_PKTPS; | |
827 | } else if (fields & F_FLAGS && !strcmp(name, "burst")) { | |
828 | mm->meter.flags |= OFPMF13_BURST; | |
829 | } else if (fields & F_FLAGS && !strcmp(name, "stats")) { | |
830 | mm->meter.flags |= OFPMF13_STATS; | |
831 | } else { | |
832 | char *value; | |
833 | ||
834 | value = strtok_r(NULL, ", \t\r\n", &save_ptr); | |
835 | if (!value) { | |
bdda5aca | 836 | return xasprintf("field %s missing value", name); |
638a19b0 JR |
837 | } |
838 | ||
839 | if (!strcmp(name, "meter")) { | |
840 | if (!strcmp(value, "all")) { | |
841 | mm->meter.meter_id = OFPM13_ALL; | |
842 | } else if (!strcmp(value, "controller")) { | |
843 | mm->meter.meter_id = OFPM13_CONTROLLER; | |
844 | } else if (!strcmp(value, "slowpath")) { | |
845 | mm->meter.meter_id = OFPM13_SLOWPATH; | |
846 | } else { | |
bdda5aca BP |
847 | char *error = str_to_u32(value, &mm->meter.meter_id); |
848 | if (error) { | |
849 | return error; | |
850 | } | |
dc8a858f JS |
851 | if (mm->meter.meter_id > OFPM13_MAX |
852 | || !mm->meter.meter_id) { | |
bdda5aca | 853 | return xasprintf("invalid value for %s", name); |
638a19b0 JR |
854 | } |
855 | } | |
856 | } else { | |
bdda5aca | 857 | return xasprintf("unknown keyword %s", name); |
638a19b0 JR |
858 | } |
859 | } | |
860 | } | |
861 | if (fields & F_METER && !mm->meter.meter_id) { | |
bdda5aca | 862 | return xstrdup("must specify 'meter'"); |
638a19b0 JR |
863 | } |
864 | if (fields & F_FLAGS && !mm->meter.flags) { | |
bdda5aca | 865 | return xstrdup("meter must specify either 'kbps' or 'pktps'"); |
638a19b0 JR |
866 | } |
867 | ||
868 | if (fields & F_BANDS) { | |
638a19b0 JR |
869 | uint16_t n_bands = 0; |
870 | struct ofputil_meter_band *band = NULL; | |
871 | int i; | |
872 | ||
638a19b0 JR |
873 | for (name = strtok_r(band_str, "=, \t\r\n", &save_ptr); name; |
874 | name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) { | |
875 | ||
876 | char *value; | |
877 | ||
878 | value = strtok_r(NULL, ", \t\r\n", &save_ptr); | |
879 | if (!value) { | |
bdda5aca | 880 | return xasprintf("field %s missing value", name); |
638a19b0 JR |
881 | } |
882 | ||
883 | if (!strcmp(name, "type")) { | |
884 | /* Start a new band */ | |
bdda5aca | 885 | band = ofpbuf_put_zeros(bands, sizeof *band); |
638a19b0 JR |
886 | n_bands++; |
887 | ||
888 | if (!strcmp(value, "drop")) { | |
889 | band->type = OFPMBT13_DROP; | |
890 | } else if (!strcmp(value, "dscp_remark")) { | |
891 | band->type = OFPMBT13_DSCP_REMARK; | |
892 | } else { | |
bdda5aca | 893 | return xasprintf("field %s unknown value %s", name, value); |
638a19b0 JR |
894 | } |
895 | } else if (!band || !band->type) { | |
bdda5aca | 896 | return xstrdup("band must start with the 'type' keyword"); |
638a19b0 | 897 | } else if (!strcmp(name, "rate")) { |
bdda5aca BP |
898 | char *error = str_to_u32(value, &band->rate); |
899 | if (error) { | |
900 | return error; | |
901 | } | |
638a19b0 | 902 | } else if (!strcmp(name, "burst_size")) { |
bdda5aca BP |
903 | char *error = str_to_u32(value, &band->burst_size); |
904 | if (error) { | |
905 | return error; | |
906 | } | |
638a19b0 | 907 | } else if (!strcmp(name, "prec_level")) { |
bdda5aca BP |
908 | char *error = str_to_u8(value, name, &band->prec_level); |
909 | if (error) { | |
910 | return error; | |
911 | } | |
638a19b0 | 912 | } else { |
bdda5aca | 913 | return xasprintf("unknown keyword %s", name); |
638a19b0 JR |
914 | } |
915 | } | |
916 | /* validate bands */ | |
917 | if (!n_bands) { | |
bdda5aca | 918 | return xstrdup("meter must have bands"); |
638a19b0 JR |
919 | } |
920 | ||
921 | mm->meter.n_bands = n_bands; | |
bdda5aca | 922 | mm->meter.bands = ofpbuf_steal_data(bands); |
638a19b0 JR |
923 | |
924 | for (i = 0; i < n_bands; ++i) { | |
925 | band = &mm->meter.bands[i]; | |
926 | ||
927 | if (!band->type) { | |
bdda5aca | 928 | return xstrdup("band must have 'type'"); |
638a19b0 JR |
929 | } |
930 | if (band->type == OFPMBT13_DSCP_REMARK) { | |
931 | if (!band->prec_level) { | |
bdda5aca BP |
932 | return xstrdup("'dscp_remark' band must have" |
933 | " 'prec_level'"); | |
638a19b0 JR |
934 | } |
935 | } else { | |
936 | if (band->prec_level) { | |
bdda5aca BP |
937 | return xstrdup("Only 'dscp_remark' band may have" |
938 | " 'prec_level'"); | |
638a19b0 JR |
939 | } |
940 | } | |
941 | if (!band->rate) { | |
bdda5aca | 942 | return xstrdup("band must have 'rate'"); |
638a19b0 JR |
943 | } |
944 | if (mm->meter.flags & OFPMF13_BURST) { | |
945 | if (!band->burst_size) { | |
bdda5aca BP |
946 | return xstrdup("band must have 'burst_size' " |
947 | "when 'burst' flag is set"); | |
638a19b0 JR |
948 | } |
949 | } else { | |
950 | if (band->burst_size) { | |
bdda5aca BP |
951 | return xstrdup("band may have 'burst_size' only " |
952 | "when 'burst' flag is set"); | |
638a19b0 JR |
953 | } |
954 | } | |
955 | } | |
638a19b0 JR |
956 | } |
957 | ||
bdda5aca BP |
958 | return NULL; |
959 | } | |
960 | ||
961 | /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man | |
962 | * page) into 'mm' for sending the specified meter_mod 'command' to a switch. | |
963 | * | |
964 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
2f0b69ac JP |
965 | * error. The caller is responsible for freeing the returned string. |
966 | * If successful, 'mm->meter.bands' must be free()d by the caller. */ | |
cab50449 | 967 | char * OVS_WARN_UNUSED_RESULT |
bdda5aca | 968 | parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_, |
db0b6c29 | 969 | int command, enum ofputil_protocol *usable_protocols) |
bdda5aca BP |
970 | { |
971 | struct ofpbuf bands; | |
972 | char *string; | |
973 | char *error; | |
974 | ||
975 | ofpbuf_init(&bands, 64); | |
976 | string = xstrdup(str_); | |
977 | ||
db0b6c29 JR |
978 | error = parse_ofp_meter_mod_str__(mm, string, &bands, command, |
979 | usable_protocols); | |
bdda5aca | 980 | |
638a19b0 | 981 | free(string); |
bdda5aca BP |
982 | ofpbuf_uninit(&bands); |
983 | ||
984 | return error; | |
638a19b0 JR |
985 | } |
986 | ||
cab50449 | 987 | static char * OVS_WARN_UNUSED_RESULT |
bdda5aca | 988 | parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr, |
50f96b10 BP |
989 | const char *str_, |
990 | const struct ofputil_port_map *port_map, | |
991 | char *string, | |
db0b6c29 | 992 | enum ofputil_protocol *usable_protocols) |
2b07c8b1 | 993 | { |
ca4fbdfe | 994 | static atomic_count id = ATOMIC_COUNT_INIT(0); |
bc78455b | 995 | char *name, *value; |
2b07c8b1 | 996 | |
ca4fbdfe | 997 | fmr->id = atomic_count_inc(&id); |
a944ef40 | 998 | |
2b07c8b1 BP |
999 | fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY |
1000 | | NXFMF_OWN | NXFMF_ACTIONS); | |
1001 | fmr->out_port = OFPP_NONE; | |
1002 | fmr->table_id = 0xff; | |
81a76618 | 1003 | match_init_catchall(&fmr->match); |
2b07c8b1 | 1004 | |
bc78455b | 1005 | while (ofputil_parse_key_value(&string, &name, &value)) { |
2b07c8b1 | 1006 | const struct protocol *p; |
0eede6b6 | 1007 | char *error = NULL; |
2b07c8b1 BP |
1008 | |
1009 | if (!strcmp(name, "!initial")) { | |
1010 | fmr->flags &= ~NXFMF_INITIAL; | |
1011 | } else if (!strcmp(name, "!add")) { | |
1012 | fmr->flags &= ~NXFMF_ADD; | |
1013 | } else if (!strcmp(name, "!delete")) { | |
1014 | fmr->flags &= ~NXFMF_DELETE; | |
1015 | } else if (!strcmp(name, "!modify")) { | |
1016 | fmr->flags &= ~NXFMF_MODIFY; | |
1017 | } else if (!strcmp(name, "!actions")) { | |
1018 | fmr->flags &= ~NXFMF_ACTIONS; | |
1019 | } else if (!strcmp(name, "!own")) { | |
1020 | fmr->flags &= ~NXFMF_OWN; | |
1021 | } else if (parse_protocol(name, &p)) { | |
81a76618 | 1022 | match_set_dl_type(&fmr->match, htons(p->dl_type)); |
2b07c8b1 | 1023 | if (p->nw_proto) { |
81a76618 | 1024 | match_set_nw_proto(&fmr->match, p->nw_proto); |
2b07c8b1 | 1025 | } |
0eede6b6 | 1026 | } else if (mf_from_name(name)) { |
50f96b10 BP |
1027 | error = parse_field(mf_from_name(name), value, port_map, |
1028 | &fmr->match, usable_protocols); | |
2b07c8b1 | 1029 | } else { |
bc78455b | 1030 | if (!*value) { |
bdda5aca | 1031 | return xasprintf("%s: field %s missing value", str_, name); |
2b07c8b1 BP |
1032 | } |
1033 | ||
1034 | if (!strcmp(name, "table")) { | |
0eede6b6 | 1035 | error = str_to_u8(value, "table", &fmr->table_id); |
2b07c8b1 | 1036 | } else if (!strcmp(name, "out_port")) { |
4e022ec0 | 1037 | fmr->out_port = u16_to_ofp(atoi(value)); |
2b07c8b1 | 1038 | } else { |
bdda5aca | 1039 | return xasprintf("%s: unknown keyword %s", str_, name); |
2b07c8b1 BP |
1040 | } |
1041 | } | |
0eede6b6 JG |
1042 | |
1043 | if (error) { | |
1044 | return error; | |
1045 | } | |
2b07c8b1 | 1046 | } |
bdda5aca BP |
1047 | return NULL; |
1048 | } | |
1049 | ||
1050 | /* Convert 'str_' (as described in the documentation for the "monitor" command | |
1051 | * in the ovs-ofctl man page) into 'fmr'. | |
1052 | * | |
1053 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
1054 | * error. The caller is responsible for freeing the returned string. */ | |
cab50449 | 1055 | char * OVS_WARN_UNUSED_RESULT |
bdda5aca | 1056 | parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr, |
db0b6c29 | 1057 | const char *str_, |
50f96b10 | 1058 | const struct ofputil_port_map *port_map, |
db0b6c29 | 1059 | enum ofputil_protocol *usable_protocols) |
bdda5aca BP |
1060 | { |
1061 | char *string = xstrdup(str_); | |
50f96b10 | 1062 | char *error = parse_flow_monitor_request__(fmr, str_, port_map, string, |
db0b6c29 | 1063 | usable_protocols); |
2b07c8b1 | 1064 | free(string); |
bdda5aca | 1065 | return error; |
2b07c8b1 BP |
1066 | } |
1067 | ||
88ca35ee | 1068 | /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command' |
bdda5aca BP |
1069 | * (one of OFPFC_*) into 'fm'. |
1070 | * | |
db5076ee JR |
1071 | * If 'command' is given as -2, 'string' may begin with a command name ("add", |
1072 | * "modify", "delete", "modify_strict", or "delete_strict"). A missing command | |
1073 | * name is treated as "add". | |
1074 | * | |
bdda5aca BP |
1075 | * Returns NULL if successful, otherwise a malloc()'d string describing the |
1076 | * error. The caller is responsible for freeing the returned string. */ | |
cab50449 | 1077 | char * OVS_WARN_UNUSED_RESULT |
27527aa0 | 1078 | parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string, |
50f96b10 | 1079 | const struct ofputil_port_map *port_map, int command, |
ba2fe8e9 | 1080 | enum ofputil_protocol *usable_protocols) |
15f1f1b6 | 1081 | { |
50f96b10 BP |
1082 | char *error = parse_ofp_str(fm, command, string, port_map, |
1083 | usable_protocols); | |
db5076ee | 1084 | |
bdda5aca BP |
1085 | if (!error) { |
1086 | /* Normalize a copy of the match. This ensures that non-normalized | |
1087 | * flows get logged but doesn't affect what gets sent to the switch, so | |
1088 | * that the switch can do whatever it likes with the flow. */ | |
1089 | struct match match_copy = fm->match; | |
1090 | ofputil_normalize_match(&match_copy); | |
1091 | } | |
88ca35ee | 1092 | |
bdda5aca | 1093 | return error; |
15f1f1b6 BP |
1094 | } |
1095 | ||
de7d3c07 SJ |
1096 | /* Convert 'setting' (as described for the "mod-table" command |
1097 | * in ovs-ofctl man page) into 'tm->table_vacancy->vacancy_up' and | |
1098 | * 'tm->table_vacancy->vacancy_down' threshold values. | |
1099 | * For the two threshold values, value of vacancy_up is always greater | |
1100 | * than value of vacancy_down. | |
1101 | * | |
1102 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
1103 | * error. The caller is responsible for freeing the returned string. */ | |
1104 | char * OVS_WARN_UNUSED_RESULT | |
1105 | parse_ofp_table_vacancy(struct ofputil_table_mod *tm, const char *setting) | |
1106 | { | |
1107 | char *save_ptr = NULL; | |
1108 | char *vac_up, *vac_down; | |
b7407f27 | 1109 | char *value = xstrdup(setting); |
ee68e779 | 1110 | char *ret_msg; |
de7d3c07 SJ |
1111 | int vacancy_up, vacancy_down; |
1112 | ||
1113 | strtok_r(value, ":", &save_ptr); | |
1114 | vac_down = strtok_r(NULL, ",", &save_ptr); | |
1115 | if (!vac_down) { | |
ee68e779 WT |
1116 | ret_msg = xasprintf("Vacancy down value missing"); |
1117 | goto exit; | |
de7d3c07 SJ |
1118 | } |
1119 | if (!str_to_int(vac_down, 0, &vacancy_down) || | |
1120 | vacancy_down < 0 || vacancy_down > 100) { | |
ee68e779 WT |
1121 | ret_msg = xasprintf("Invalid vacancy down value \"%s\"", vac_down); |
1122 | goto exit; | |
de7d3c07 SJ |
1123 | } |
1124 | vac_up = strtok_r(NULL, ",", &save_ptr); | |
1125 | if (!vac_up) { | |
ee68e779 WT |
1126 | ret_msg = xasprintf("Vacancy up value missing"); |
1127 | goto exit; | |
de7d3c07 SJ |
1128 | } |
1129 | if (!str_to_int(vac_up, 0, &vacancy_up) || | |
1130 | vacancy_up < 0 || vacancy_up > 100) { | |
ee68e779 WT |
1131 | ret_msg = xasprintf("Invalid vacancy up value \"%s\"", vac_up); |
1132 | goto exit; | |
de7d3c07 SJ |
1133 | } |
1134 | if (vacancy_down > vacancy_up) { | |
ee68e779 WT |
1135 | ret_msg = xasprintf("Invalid vacancy range, vacancy up should be " |
1136 | "greater than vacancy down (%s)", | |
1137 | ofperr_to_string(OFPERR_OFPBPC_BAD_VALUE)); | |
1138 | goto exit; | |
de7d3c07 | 1139 | } |
ee68e779 WT |
1140 | |
1141 | free(value); | |
de7d3c07 SJ |
1142 | tm->table_vacancy.vacancy_down = vacancy_down; |
1143 | tm->table_vacancy.vacancy_up = vacancy_up; | |
1144 | return NULL; | |
ee68e779 WT |
1145 | |
1146 | exit: | |
1147 | free(value); | |
1148 | return ret_msg; | |
de7d3c07 SJ |
1149 | } |
1150 | ||
82c22d34 BP |
1151 | /* Convert 'table_id' and 'setting' (as described for the "mod-table" command |
1152 | * in the ovs-ofctl man page) into 'tm' for sending a table_mod command to a | |
1153 | * switch. | |
1154 | * | |
1155 | * Stores a bitmap of the OpenFlow versions that are usable for 'tm' into | |
1156 | * '*usable_versions'. | |
918f2b82 AZ |
1157 | * |
1158 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
1159 | * error. The caller is responsible for freeing the returned string. */ | |
cab50449 | 1160 | char * OVS_WARN_UNUSED_RESULT |
918f2b82 | 1161 | parse_ofp_table_mod(struct ofputil_table_mod *tm, const char *table_id, |
82c22d34 | 1162 | const char *setting, uint32_t *usable_versions) |
918f2b82 | 1163 | { |
82c22d34 | 1164 | *usable_versions = 0; |
918f2b82 | 1165 | if (!strcasecmp(table_id, "all")) { |
083761ad | 1166 | tm->table_id = OFPTT_ALL; |
918f2b82 AZ |
1167 | } else { |
1168 | char *error = str_to_u8(table_id, "table_id", &tm->table_id); | |
1169 | if (error) { | |
1170 | return error; | |
1171 | } | |
1172 | } | |
1173 | ||
82c22d34 BP |
1174 | tm->miss = OFPUTIL_TABLE_MISS_DEFAULT; |
1175 | tm->eviction = OFPUTIL_TABLE_EVICTION_DEFAULT; | |
1176 | tm->eviction_flags = UINT32_MAX; | |
de7d3c07 SJ |
1177 | tm->vacancy = OFPUTIL_TABLE_VACANCY_DEFAULT; |
1178 | tm->table_vacancy.vacancy_down = 0; | |
1179 | tm->table_vacancy.vacancy_up = 0; | |
1180 | tm->table_vacancy.vacancy = 0; | |
82c22d34 | 1181 | /* Only OpenFlow 1.1 and 1.2 can configure table-miss via table_mod. |
de7d3c07 SJ |
1182 | * Only OpenFlow 1.4+ can configure eviction and vacancy events |
1183 | * via table_mod. | |
82c22d34 BP |
1184 | */ |
1185 | if (!strcmp(setting, "controller")) { | |
1186 | tm->miss = OFPUTIL_TABLE_MISS_CONTROLLER; | |
1187 | *usable_versions = (1u << OFP11_VERSION) | (1u << OFP12_VERSION); | |
1188 | } else if (!strcmp(setting, "continue")) { | |
1189 | tm->miss = OFPUTIL_TABLE_MISS_CONTINUE; | |
1190 | *usable_versions = (1u << OFP11_VERSION) | (1u << OFP12_VERSION); | |
1191 | } else if (!strcmp(setting, "drop")) { | |
1192 | tm->miss = OFPUTIL_TABLE_MISS_DROP; | |
1193 | *usable_versions = (1u << OFP11_VERSION) | (1u << OFP12_VERSION); | |
1194 | } else if (!strcmp(setting, "evict")) { | |
1195 | tm->eviction = OFPUTIL_TABLE_EVICTION_ON; | |
1196 | *usable_versions = (1 << OFP14_VERSION) | (1u << OFP15_VERSION); | |
1197 | } else if (!strcmp(setting, "noevict")) { | |
1198 | tm->eviction = OFPUTIL_TABLE_EVICTION_OFF; | |
1199 | *usable_versions = (1 << OFP14_VERSION) | (1u << OFP15_VERSION); | |
de7d3c07 SJ |
1200 | } else if (!strncmp(setting, "vacancy", strcspn(setting, ":"))) { |
1201 | tm->vacancy = OFPUTIL_TABLE_VACANCY_ON; | |
1202 | *usable_versions = (1 << OFP14_VERSION) | (1u << OFP15_VERSION); | |
1203 | char *error = parse_ofp_table_vacancy(tm, setting); | |
1204 | if (error) { | |
1205 | return error; | |
1206 | } | |
1207 | } else if (!strcmp(setting, "novacancy")) { | |
1208 | tm->vacancy = OFPUTIL_TABLE_VACANCY_OFF; | |
1209 | *usable_versions = (1 << OFP14_VERSION) | (1u << OFP15_VERSION); | |
918f2b82 | 1210 | } else { |
82c22d34 | 1211 | return xasprintf("invalid table_mod setting %s", setting); |
918f2b82 AZ |
1212 | } |
1213 | ||
3c1bb396 | 1214 | if (tm->table_id == 0xfe |
82c22d34 | 1215 | && tm->miss == OFPUTIL_TABLE_MISS_CONTINUE) { |
918f2b82 AZ |
1216 | return xstrdup("last table's flow miss handling can not be continue"); |
1217 | } | |
1218 | ||
1219 | return NULL; | |
1220 | } | |
1221 | ||
1222 | ||
bdda5aca BP |
1223 | /* Opens file 'file_name' and reads each line as a flow_mod of the specified |
1224 | * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated | |
1225 | * on the caller's behalf, and the number of flow_mods in '*n_fms'. | |
1226 | * | |
db5076ee JR |
1227 | * If 'command' is given as -2, each line may start with a command name |
1228 | * ("add", "modify", "delete", "modify_strict", or "delete_strict"). A missing | |
1229 | * command name is treated as "add". | |
1230 | * | |
bdda5aca BP |
1231 | * Returns NULL if successful, otherwise a malloc()'d string describing the |
1232 | * error. The caller is responsible for freeing the returned string. */ | |
cab50449 | 1233 | char * OVS_WARN_UNUSED_RESULT |
50f96b10 BP |
1234 | parse_ofp_flow_mod_file(const char *file_name, |
1235 | const struct ofputil_port_map *port_map, int command, | |
db0b6c29 | 1236 | struct ofputil_flow_mod **fms, size_t *n_fms, |
ba2fe8e9 | 1237 | enum ofputil_protocol *usable_protocols) |
15f1f1b6 | 1238 | { |
27527aa0 | 1239 | size_t allocated_fms; |
bdda5aca | 1240 | int line_number; |
27527aa0 | 1241 | FILE *stream; |
dd8101bc | 1242 | struct ds s; |
15f1f1b6 | 1243 | |
db0b6c29 JR |
1244 | *usable_protocols = OFPUTIL_P_ANY; |
1245 | ||
bdda5aca BP |
1246 | *fms = NULL; |
1247 | *n_fms = 0; | |
1248 | ||
27527aa0 BP |
1249 | stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r"); |
1250 | if (stream == NULL) { | |
bdda5aca BP |
1251 | return xasprintf("%s: open failed (%s)", |
1252 | file_name, ovs_strerror(errno)); | |
27527aa0 BP |
1253 | } |
1254 | ||
1255 | allocated_fms = *n_fms; | |
dd8101bc | 1256 | ds_init(&s); |
bdda5aca BP |
1257 | line_number = 0; |
1258 | while (!ds_get_preprocessed_line(&s, stream, &line_number)) { | |
1259 | char *error; | |
db0b6c29 | 1260 | enum ofputil_protocol usable; |
bdda5aca | 1261 | |
27527aa0 BP |
1262 | if (*n_fms >= allocated_fms) { |
1263 | *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms); | |
1264 | } | |
50f96b10 BP |
1265 | error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), port_map, |
1266 | command, &usable); | |
bdda5aca | 1267 | if (error) { |
43d68d98 | 1268 | char *err_msg; |
bdda5aca BP |
1269 | size_t i; |
1270 | ||
1271 | for (i = 0; i < *n_fms; i++) { | |
dc723c44 | 1272 | free(CONST_CAST(struct ofpact *, (*fms)[i].ofpacts)); |
bdda5aca BP |
1273 | } |
1274 | free(*fms); | |
1275 | *fms = NULL; | |
1276 | *n_fms = 0; | |
1277 | ||
1278 | ds_destroy(&s); | |
1279 | if (stream != stdin) { | |
1280 | fclose(stream); | |
1281 | } | |
1282 | ||
43d68d98 WT |
1283 | err_msg = xasprintf("%s:%d: %s", file_name, line_number, error); |
1284 | free(error); | |
1285 | return err_msg; | |
bdda5aca | 1286 | } |
db0b6c29 | 1287 | *usable_protocols &= usable; /* Each line can narrow the set. */ |
27527aa0 | 1288 | *n_fms += 1; |
15f1f1b6 | 1289 | } |
15f1f1b6 | 1290 | |
bdda5aca | 1291 | ds_destroy(&s); |
27527aa0 BP |
1292 | if (stream != stdin) { |
1293 | fclose(stream); | |
1294 | } | |
bdda5aca | 1295 | return NULL; |
88ca35ee BP |
1296 | } |
1297 | ||
cab50449 | 1298 | char * OVS_WARN_UNUSED_RESULT |
81d1ea94 | 1299 | parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr, |
db0b6c29 | 1300 | bool aggregate, const char *string, |
50f96b10 | 1301 | const struct ofputil_port_map *port_map, |
ba2fe8e9 | 1302 | enum ofputil_protocol *usable_protocols) |
88ca35ee | 1303 | { |
a9a2da38 | 1304 | struct ofputil_flow_mod fm; |
bdda5aca BP |
1305 | char *error; |
1306 | ||
50f96b10 | 1307 | error = parse_ofp_str(&fm, -1, string, port_map, usable_protocols); |
bdda5aca BP |
1308 | if (error) { |
1309 | return error; | |
1310 | } | |
88ca35ee | 1311 | |
db0b6c29 JR |
1312 | /* Special table ID support not required for stats requests. */ |
1313 | if (*usable_protocols & OFPUTIL_P_OF10_STD_TID) { | |
1314 | *usable_protocols |= OFPUTIL_P_OF10_STD; | |
1315 | } | |
1316 | if (*usable_protocols & OFPUTIL_P_OF10_NXM_TID) { | |
1317 | *usable_protocols |= OFPUTIL_P_OF10_NXM; | |
1318 | } | |
1319 | ||
88ca35ee | 1320 | fsr->aggregate = aggregate; |
e729e793 JP |
1321 | fsr->cookie = fm.cookie; |
1322 | fsr->cookie_mask = fm.cookie_mask; | |
81a76618 | 1323 | fsr->match = fm.match; |
88ca35ee | 1324 | fsr->out_port = fm.out_port; |
7395c052 | 1325 | fsr->out_group = fm.out_group; |
6c1491fb | 1326 | fsr->table_id = fm.table_id; |
bdda5aca | 1327 | return NULL; |
15f1f1b6 | 1328 | } |
ccbe50f8 BP |
1329 | |
1330 | /* Parses a specification of a flow from 's' into 'flow'. 's' must take the | |
1331 | * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a | |
1332 | * mf_field. Fields must be specified in a natural order for satisfying | |
5cd4ead5 | 1333 | * prerequisites. If 'wc' is specified, masks the field in 'wc' for each of the |
5a0a5702 GS |
1334 | * field specified in flow. If the map, 'names_portno' is specfied, converts |
1335 | * the in_port name into port no while setting the 'flow'. | |
ccbe50f8 BP |
1336 | * |
1337 | * Returns NULL on success, otherwise a malloc()'d string that explains the | |
1338 | * problem. */ | |
1339 | char * | |
5cd4ead5 | 1340 | parse_ofp_exact_flow(struct flow *flow, struct flow_wildcards *wc, |
8d8ab6c2 | 1341 | const struct tun_table *tun_table, const char *s, |
50f96b10 | 1342 | const struct ofputil_port_map *port_map) |
ccbe50f8 BP |
1343 | { |
1344 | char *pos, *key, *value_s; | |
1345 | char *error = NULL; | |
1346 | char *copy; | |
1347 | ||
1348 | memset(flow, 0, sizeof *flow); | |
5cd4ead5 JR |
1349 | if (wc) { |
1350 | memset(wc, 0, sizeof *wc); | |
5a0a5702 | 1351 | } |
8d8ab6c2 | 1352 | flow->tunnel.metadata.tab = tun_table; |
ccbe50f8 BP |
1353 | |
1354 | pos = copy = xstrdup(s); | |
1355 | while (ofputil_parse_key_value(&pos, &key, &value_s)) { | |
1356 | const struct protocol *p; | |
1357 | if (parse_protocol(key, &p)) { | |
1358 | if (flow->dl_type) { | |
1359 | error = xasprintf("%s: Ethernet type set multiple times", s); | |
1360 | goto exit; | |
1361 | } | |
1362 | flow->dl_type = htons(p->dl_type); | |
5cd4ead5 JR |
1363 | if (wc) { |
1364 | wc->masks.dl_type = OVS_BE16_MAX; | |
5a0a5702 | 1365 | } |
ccbe50f8 BP |
1366 | |
1367 | if (p->nw_proto) { | |
1368 | if (flow->nw_proto) { | |
1369 | error = xasprintf("%s: network protocol set " | |
1370 | "multiple times", s); | |
1371 | goto exit; | |
1372 | } | |
1373 | flow->nw_proto = p->nw_proto; | |
5cd4ead5 JR |
1374 | if (wc) { |
1375 | wc->masks.nw_proto = UINT8_MAX; | |
5a0a5702 | 1376 | } |
ccbe50f8 BP |
1377 | } |
1378 | } else { | |
1379 | const struct mf_field *mf; | |
1380 | union mf_value value; | |
1381 | char *field_error; | |
1382 | ||
1383 | mf = mf_from_name(key); | |
1384 | if (!mf) { | |
1385 | error = xasprintf("%s: unknown field %s", s, key); | |
1386 | goto exit; | |
1387 | } | |
1388 | ||
aff49b8c | 1389 | if (!mf_are_prereqs_ok(mf, flow, NULL)) { |
ccbe50f8 BP |
1390 | error = xasprintf("%s: prerequisites not met for setting %s", |
1391 | s, key); | |
1392 | goto exit; | |
1393 | } | |
1394 | ||
1cb20095 | 1395 | if (mf_is_set(mf, flow)) { |
ccbe50f8 BP |
1396 | error = xasprintf("%s: field %s set multiple times", s, key); |
1397 | goto exit; | |
1398 | } | |
1399 | ||
50f96b10 BP |
1400 | field_error = mf_parse_value(mf, value_s, port_map, &value); |
1401 | if (field_error) { | |
1402 | error = xasprintf("%s: bad value for %s (%s)", | |
1403 | s, key, field_error); | |
1404 | free(field_error); | |
1405 | goto exit; | |
1406 | } | |
ccbe50f8 | 1407 | |
50f96b10 BP |
1408 | mf_set_flow_value(mf, &value, flow); |
1409 | if (wc) { | |
1410 | mf_mask_field(mf, wc); | |
5a0a5702 | 1411 | } |
ccbe50f8 BP |
1412 | } |
1413 | } | |
1414 | ||
4e022ec0 AW |
1415 | if (!flow->in_port.ofp_port) { |
1416 | flow->in_port.ofp_port = OFPP_NONE; | |
72d64e33 EJ |
1417 | } |
1418 | ||
ccbe50f8 BP |
1419 | exit: |
1420 | free(copy); | |
1421 | ||
1422 | if (error) { | |
1423 | memset(flow, 0, sizeof *flow); | |
5cd4ead5 JR |
1424 | if (wc) { |
1425 | memset(wc, 0, sizeof *wc); | |
5a0a5702 | 1426 | } |
ccbe50f8 BP |
1427 | } |
1428 | return error; | |
1429 | } | |
7395c052 | 1430 | |
cab50449 | 1431 | static char * OVS_WARN_UNUSED_RESULT |
50f96b10 BP |
1432 | parse_bucket_str(struct ofputil_bucket *bucket, char *str_, |
1433 | const struct ofputil_port_map *port_map, uint8_t group_type, | |
1434 | enum ofputil_protocol *usable_protocols) | |
7395c052 | 1435 | { |
c2d936a4 | 1436 | char *pos, *key, *value; |
7395c052 | 1437 | struct ofpbuf ofpacts; |
c2d936a4 BP |
1438 | struct ds actions; |
1439 | char *error; | |
7395c052 | 1440 | |
64e8c446 | 1441 | bucket->weight = group_type == OFPGT11_SELECT ? 1 : 0; |
18ac06d3 | 1442 | bucket->bucket_id = OFPG15_BUCKET_ALL; |
7395c052 | 1443 | bucket->watch_port = OFPP_ANY; |
30ef36c6 | 1444 | bucket->watch_group = OFPG_ANY; |
7395c052 | 1445 | |
c2d936a4 | 1446 | ds_init(&actions); |
7395c052 | 1447 | |
c2d936a4 BP |
1448 | pos = str_; |
1449 | error = NULL; | |
1450 | while (ofputil_parse_key_value(&pos, &key, &value)) { | |
1451 | if (!strcasecmp(key, "weight")) { | |
1452 | error = str_to_u16(value, "weight", &bucket->weight); | |
1453 | } else if (!strcasecmp(key, "watch_port")) { | |
50f96b10 | 1454 | if (!ofputil_port_from_string(value, port_map, &bucket->watch_port) |
7395c052 NZ |
1455 | || (ofp_to_u16(bucket->watch_port) >= ofp_to_u16(OFPP_MAX) |
1456 | && bucket->watch_port != OFPP_ANY)) { | |
c2d936a4 | 1457 | error = xasprintf("%s: invalid watch_port", value); |
7395c052 | 1458 | } |
c2d936a4 BP |
1459 | } else if (!strcasecmp(key, "watch_group")) { |
1460 | error = str_to_u32(value, &bucket->watch_group); | |
7395c052 NZ |
1461 | if (!error && bucket->watch_group > OFPG_MAX) { |
1462 | error = xasprintf("invalid watch_group id %"PRIu32, | |
1463 | bucket->watch_group); | |
1464 | } | |
2d5d050c SH |
1465 | } else if (!strcasecmp(key, "bucket_id")) { |
1466 | error = str_to_u32(value, &bucket->bucket_id); | |
1467 | if (!error && bucket->bucket_id > OFPG15_BUCKET_MAX) { | |
1468 | error = xasprintf("invalid bucket_id id %"PRIu32, | |
1469 | bucket->bucket_id); | |
1470 | } | |
1471 | *usable_protocols &= OFPUTIL_P_OF15_UP; | |
c2d936a4 BP |
1472 | } else if (!strcasecmp(key, "action") || !strcasecmp(key, "actions")) { |
1473 | ds_put_format(&actions, "%s,", value); | |
7395c052 | 1474 | } else { |
c2d936a4 | 1475 | ds_put_format(&actions, "%s(%s),", key, value); |
7395c052 NZ |
1476 | } |
1477 | ||
1478 | if (error) { | |
c2d936a4 | 1479 | ds_destroy(&actions); |
7395c052 NZ |
1480 | return error; |
1481 | } | |
1482 | } | |
1483 | ||
c2d936a4 BP |
1484 | if (!actions.length) { |
1485 | return xstrdup("bucket must specify actions"); | |
1486 | } | |
1487 | ds_chomp(&actions, ','); | |
1488 | ||
1489 | ofpbuf_init(&ofpacts, 0); | |
50f96b10 | 1490 | error = ofpacts_parse_actions(ds_cstr(&actions), port_map, &ofpacts, |
c2d936a4 BP |
1491 | usable_protocols); |
1492 | ds_destroy(&actions); | |
1493 | if (error) { | |
1494 | ofpbuf_uninit(&ofpacts); | |
1495 | return error; | |
1496 | } | |
6fd6ed71 PS |
1497 | bucket->ofpacts = ofpacts.data; |
1498 | bucket->ofpacts_len = ofpacts.size; | |
7395c052 NZ |
1499 | |
1500 | return NULL; | |
1501 | } | |
1502 | ||
b879391e | 1503 | static char * OVS_WARN_UNUSED_RESULT |
50f96b10 BP |
1504 | parse_select_group_field(char *s, const struct ofputil_port_map *port_map, |
1505 | struct field_array *fa, | |
b879391e SH |
1506 | enum ofputil_protocol *usable_protocols) |
1507 | { | |
68dfc25b | 1508 | char *name, *value_str; |
b879391e | 1509 | |
68dfc25b | 1510 | while (ofputil_parse_key_value(&s, &name, &value_str)) { |
b879391e SH |
1511 | const struct mf_field *mf = mf_from_name(name); |
1512 | ||
1513 | if (mf) { | |
1514 | char *error; | |
b879391e SH |
1515 | union mf_value value; |
1516 | ||
1517 | if (bitmap_is_set(fa->used.bm, mf->id)) { | |
1518 | return xasprintf("%s: duplicate field", name); | |
1519 | } | |
1520 | ||
68dfc25b | 1521 | if (*value_str) { |
50f96b10 | 1522 | error = mf_parse_value(mf, value_str, port_map, &value); |
b879391e SH |
1523 | if (error) { |
1524 | return error; | |
1525 | } | |
1526 | ||
1527 | /* The mask cannot be all-zeros */ | |
1cb20095 JG |
1528 | if (!mf_is_tun_metadata(mf) && |
1529 | is_all_zeros(&value, mf->n_bytes)) { | |
b879391e SH |
1530 | return xasprintf("%s: values are wildcards here " |
1531 | "and must not be all-zeros", s); | |
1532 | } | |
1533 | ||
1534 | /* The values parsed are masks for fields used | |
1535 | * by the selection method */ | |
1536 | if (!mf_is_mask_valid(mf, &value)) { | |
1537 | return xasprintf("%s: invalid mask for field %s", | |
1538 | value_str, mf->name); | |
1539 | } | |
1540 | } else { | |
1541 | memset(&value, 0xff, mf->n_bytes); | |
1542 | } | |
1543 | ||
1544 | field_array_set(mf->id, &value, fa); | |
1545 | ||
1546 | if (is_all_ones(&value, mf->n_bytes)) { | |
1547 | *usable_protocols &= mf->usable_protocols_exact; | |
1548 | } else if (mf->usable_protocols_bitwise == mf->usable_protocols_cidr | |
1549 | || ip_is_cidr(value.be32)) { | |
1550 | *usable_protocols &= mf->usable_protocols_cidr; | |
1551 | } else { | |
1552 | *usable_protocols &= mf->usable_protocols_bitwise; | |
1553 | } | |
1554 | } else { | |
1555 | return xasprintf("%s: unknown field %s", s, name); | |
1556 | } | |
1557 | } | |
1558 | ||
1559 | return NULL; | |
1560 | } | |
1561 | ||
cab50449 | 1562 | static char * OVS_WARN_UNUSED_RESULT |
25070e04 | 1563 | parse_ofp_group_mod_str__(struct ofputil_group_mod *gm, int command, |
7395c052 | 1564 | char *string, |
50f96b10 | 1565 | const struct ofputil_port_map *port_map, |
7395c052 NZ |
1566 | enum ofputil_protocol *usable_protocols) |
1567 | { | |
1568 | enum { | |
45952551 SH |
1569 | F_GROUP_TYPE = 1 << 0, |
1570 | F_BUCKETS = 1 << 1, | |
1571 | F_COMMAND_BUCKET_ID = 1 << 2, | |
1572 | F_COMMAND_BUCKET_ID_ALL = 1 << 3, | |
7395c052 | 1573 | } fields; |
7395c052 | 1574 | bool had_type = false; |
45952551 | 1575 | bool had_command_bucket_id = false; |
7395c052 NZ |
1576 | struct ofputil_bucket *bucket; |
1577 | char *error = NULL; | |
1578 | ||
1579 | *usable_protocols = OFPUTIL_P_OF11_UP; | |
1580 | ||
25070e04 JR |
1581 | if (command == -2) { |
1582 | size_t len; | |
1583 | ||
1584 | string += strspn(string, " \t\r\n"); /* Skip white space. */ | |
1585 | len = strcspn(string, ", \t\r\n"); /* Get length of the first token. */ | |
1586 | ||
1587 | if (!strncmp(string, "add", len)) { | |
1588 | command = OFPGC11_ADD; | |
1589 | } else if (!strncmp(string, "delete", len)) { | |
1590 | command = OFPGC11_DELETE; | |
1591 | } else if (!strncmp(string, "modify", len)) { | |
1592 | command = OFPGC11_MODIFY; | |
1593 | } else if (!strncmp(string, "add_or_mod", len)) { | |
1594 | command = OFPGC11_ADD_OR_MOD; | |
1595 | } else if (!strncmp(string, "insert_bucket", len)) { | |
1596 | command = OFPGC15_INSERT_BUCKET; | |
1597 | } else if (!strncmp(string, "remove_bucket", len)) { | |
1598 | command = OFPGC15_REMOVE_BUCKET; | |
1599 | } else { | |
1600 | len = 0; | |
1601 | command = OFPGC11_ADD; | |
1602 | } | |
1603 | string += len; | |
1604 | } | |
1605 | ||
7395c052 NZ |
1606 | switch (command) { |
1607 | case OFPGC11_ADD: | |
1608 | fields = F_GROUP_TYPE | F_BUCKETS; | |
1609 | break; | |
1610 | ||
1611 | case OFPGC11_DELETE: | |
1612 | fields = 0; | |
1613 | break; | |
1614 | ||
1615 | case OFPGC11_MODIFY: | |
1616 | fields = F_GROUP_TYPE | F_BUCKETS; | |
1617 | break; | |
1618 | ||
88b87a36 JS |
1619 | case OFPGC11_ADD_OR_MOD: |
1620 | fields = F_GROUP_TYPE | F_BUCKETS; | |
1621 | break; | |
1622 | ||
45952551 SH |
1623 | case OFPGC15_INSERT_BUCKET: |
1624 | fields = F_BUCKETS | F_COMMAND_BUCKET_ID; | |
1625 | *usable_protocols &= OFPUTIL_P_OF15_UP; | |
1626 | break; | |
1627 | ||
1628 | case OFPGC15_REMOVE_BUCKET: | |
1629 | fields = F_COMMAND_BUCKET_ID | F_COMMAND_BUCKET_ID_ALL; | |
1630 | *usable_protocols &= OFPUTIL_P_OF15_UP; | |
1631 | break; | |
1632 | ||
7395c052 | 1633 | default: |
428b2edd | 1634 | OVS_NOT_REACHED(); |
7395c052 NZ |
1635 | } |
1636 | ||
1637 | memset(gm, 0, sizeof *gm); | |
1638 | gm->command = command; | |
1639 | gm->group_id = OFPG_ANY; | |
18ac06d3 | 1640 | gm->command_bucket_id = OFPG15_BUCKET_ALL; |
417e7e66 | 1641 | ovs_list_init(&gm->buckets); |
7395c052 NZ |
1642 | if (command == OFPGC11_DELETE && string[0] == '\0') { |
1643 | gm->group_id = OFPG_ALL; | |
1644 | return NULL; | |
1645 | } | |
1646 | ||
1647 | *usable_protocols = OFPUTIL_P_OF11_UP; | |
1648 | ||
64e8c446 BP |
1649 | /* Strip the buckets off the end of 'string', if there are any, saving a |
1650 | * pointer for later. We want to parse the buckets last because the bucket | |
1651 | * type influences bucket defaults. */ | |
1652 | char *bkt_str = strstr(string, "bucket="); | |
1653 | if (bkt_str) { | |
1654 | if (!(fields & F_BUCKETS)) { | |
1655 | error = xstrdup("bucket is not needed"); | |
1656 | goto out; | |
7395c052 | 1657 | } |
64e8c446 | 1658 | *bkt_str = '\0'; |
7395c052 NZ |
1659 | } |
1660 | ||
64e8c446 | 1661 | /* Parse everything before the buckets. */ |
68dfc25b BP |
1662 | char *pos = string; |
1663 | char *name, *value; | |
1664 | while (ofputil_parse_key_value(&pos, &name, &value)) { | |
45952551 SH |
1665 | if (!strcmp(name, "command_bucket_id")) { |
1666 | if (!(fields & F_COMMAND_BUCKET_ID)) { | |
1667 | error = xstrdup("command bucket id is not needed"); | |
1668 | goto out; | |
1669 | } | |
1670 | if (!strcmp(value, "all")) { | |
1671 | gm->command_bucket_id = OFPG15_BUCKET_ALL; | |
1672 | } else if (!strcmp(value, "first")) { | |
1673 | gm->command_bucket_id = OFPG15_BUCKET_FIRST; | |
1674 | } else if (!strcmp(value, "last")) { | |
1675 | gm->command_bucket_id = OFPG15_BUCKET_LAST; | |
1676 | } else { | |
908995c5 | 1677 | error = str_to_u32(value, &gm->command_bucket_id); |
45952551 SH |
1678 | if (error) { |
1679 | goto out; | |
1680 | } | |
1681 | if (gm->command_bucket_id > OFPG15_BUCKET_MAX | |
1682 | && (gm->command_bucket_id != OFPG15_BUCKET_FIRST | |
1683 | && gm->command_bucket_id != OFPG15_BUCKET_LAST | |
1684 | && gm->command_bucket_id != OFPG15_BUCKET_ALL)) { | |
1685 | error = xasprintf("invalid command bucket id %"PRIu32, | |
1686 | gm->command_bucket_id); | |
1687 | goto out; | |
1688 | } | |
1689 | } | |
1690 | if (gm->command_bucket_id == OFPG15_BUCKET_ALL | |
1691 | && !(fields & F_COMMAND_BUCKET_ID_ALL)) { | |
1692 | error = xstrdup("command_bucket_id=all is not permitted"); | |
1693 | goto out; | |
1694 | } | |
1695 | had_command_bucket_id = true; | |
1696 | } else if (!strcmp(name, "group_id")) { | |
7395c052 NZ |
1697 | if(!strcmp(value, "all")) { |
1698 | gm->group_id = OFPG_ALL; | |
1699 | } else { | |
aa6fb077 | 1700 | error = str_to_u32(value, &gm->group_id); |
7395c052 NZ |
1701 | if (error) { |
1702 | goto out; | |
1703 | } | |
1704 | if (gm->group_id != OFPG_ALL && gm->group_id > OFPG_MAX) { | |
1705 | error = xasprintf("invalid group id %"PRIu32, | |
1706 | gm->group_id); | |
1707 | goto out; | |
1708 | } | |
1709 | } | |
1710 | } else if (!strcmp(name, "type")){ | |
1711 | if (!(fields & F_GROUP_TYPE)) { | |
1712 | error = xstrdup("type is not needed"); | |
1713 | goto out; | |
1714 | } | |
1715 | if (!strcmp(value, "all")) { | |
1716 | gm->type = OFPGT11_ALL; | |
1717 | } else if (!strcmp(value, "select")) { | |
1718 | gm->type = OFPGT11_SELECT; | |
1719 | } else if (!strcmp(value, "indirect")) { | |
1720 | gm->type = OFPGT11_INDIRECT; | |
1721 | } else if (!strcmp(value, "ff") || | |
1722 | !strcmp(value, "fast_failover")) { | |
1723 | gm->type = OFPGT11_FF; | |
1724 | } else { | |
1725 | error = xasprintf("invalid group type %s", value); | |
1726 | goto out; | |
1727 | } | |
1728 | had_type = true; | |
b879391e SH |
1729 | } else if (!strcmp(name, "selection_method")) { |
1730 | if (!(fields & F_GROUP_TYPE)) { | |
1731 | error = xstrdup("selection method is not needed"); | |
1732 | goto out; | |
1733 | } | |
1734 | if (strlen(value) >= NTR_MAX_SELECTION_METHOD_LEN) { | |
1735 | error = xasprintf("selection method is longer than %u" | |
1736 | " bytes long", | |
1737 | NTR_MAX_SELECTION_METHOD_LEN - 1); | |
1738 | goto out; | |
1739 | } | |
1740 | memset(gm->props.selection_method, '\0', | |
1741 | NTR_MAX_SELECTION_METHOD_LEN); | |
1742 | strcpy(gm->props.selection_method, value); | |
1743 | *usable_protocols &= OFPUTIL_P_OF15_UP; | |
1744 | } else if (!strcmp(name, "selection_method_param")) { | |
1745 | if (!(fields & F_GROUP_TYPE)) { | |
1746 | error = xstrdup("selection method param is not needed"); | |
1747 | goto out; | |
1748 | } | |
1749 | error = str_to_u64(value, &gm->props.selection_method_param); | |
596be0ad BP |
1750 | if (error) { |
1751 | goto out; | |
1752 | } | |
b879391e SH |
1753 | *usable_protocols &= OFPUTIL_P_OF15_UP; |
1754 | } else if (!strcmp(name, "fields")) { | |
1755 | if (!(fields & F_GROUP_TYPE)) { | |
1756 | error = xstrdup("fields are not needed"); | |
1757 | goto out; | |
1758 | } | |
50f96b10 BP |
1759 | error = parse_select_group_field(value, port_map, |
1760 | &gm->props.fields, | |
b879391e SH |
1761 | usable_protocols); |
1762 | if (error) { | |
1763 | goto out; | |
1764 | } | |
1765 | *usable_protocols &= OFPUTIL_P_OF15_UP; | |
7395c052 NZ |
1766 | } else { |
1767 | error = xasprintf("unknown keyword %s", name); | |
1768 | goto out; | |
1769 | } | |
1770 | } | |
1771 | if (gm->group_id == OFPG_ANY) { | |
1772 | error = xstrdup("must specify a group_id"); | |
1773 | goto out; | |
1774 | } | |
1775 | if (fields & F_GROUP_TYPE && !had_type) { | |
1776 | error = xstrdup("must specify a type"); | |
1777 | goto out; | |
1778 | } | |
1779 | ||
60dfb5ed JR |
1780 | /* Exclude fields for non "hash" selection method. */ |
1781 | if (strcmp(gm->props.selection_method, "hash") && | |
1782 | gm->props.fields.values_size) { | |
1783 | error = xstrdup("fields may only be specified with \"selection_method=hash\""); | |
1784 | goto out; | |
1785 | } | |
1786 | /* Exclude selection_method_param if no selection_method is given. */ | |
1787 | if (gm->props.selection_method[0] == 0 | |
1788 | && gm->props.selection_method_param != 0) { | |
1789 | error = xstrdup("selection_method_param is only allowed with \"selection_method\""); | |
1790 | goto out; | |
1791 | } | |
45952551 SH |
1792 | if (fields & F_COMMAND_BUCKET_ID) { |
1793 | if (!(fields & F_COMMAND_BUCKET_ID_ALL || had_command_bucket_id)) { | |
1794 | error = xstrdup("must specify a command bucket id"); | |
1795 | goto out; | |
1796 | } | |
1797 | } else if (had_command_bucket_id) { | |
1798 | error = xstrdup("command bucket id is not needed"); | |
1799 | goto out; | |
1800 | } | |
1801 | ||
64e8c446 BP |
1802 | /* Now parse the buckets, if any. */ |
1803 | while (bkt_str) { | |
1804 | char *next_bkt_str; | |
1805 | ||
1806 | bkt_str = strchr(bkt_str + 1, '='); | |
1807 | if (!bkt_str) { | |
1808 | error = xstrdup("must specify bucket content"); | |
1809 | goto out; | |
1810 | } | |
1811 | bkt_str++; | |
1812 | ||
1813 | next_bkt_str = strstr(bkt_str, "bucket="); | |
1814 | if (next_bkt_str) { | |
1815 | *next_bkt_str = '\0'; | |
1816 | } | |
1817 | ||
1818 | bucket = xzalloc(sizeof(struct ofputil_bucket)); | |
50f96b10 BP |
1819 | error = parse_bucket_str(bucket, bkt_str, port_map, |
1820 | gm->type, usable_protocols); | |
64e8c446 BP |
1821 | if (error) { |
1822 | free(bucket); | |
1823 | goto out; | |
1824 | } | |
417e7e66 | 1825 | ovs_list_push_back(&gm->buckets, &bucket->list_node); |
64e8c446 BP |
1826 | |
1827 | if (gm->type != OFPGT11_SELECT && bucket->weight) { | |
7395c052 NZ |
1828 | error = xstrdup("Only select groups can have bucket weights."); |
1829 | goto out; | |
1830 | } | |
64e8c446 BP |
1831 | |
1832 | bkt_str = next_bkt_str; | |
7395c052 | 1833 | } |
417e7e66 | 1834 | if (gm->type == OFPGT11_INDIRECT && !ovs_list_is_short(&gm->buckets)) { |
7395c052 NZ |
1835 | error = xstrdup("Indirect groups can have at most one bucket."); |
1836 | goto out; | |
1837 | } | |
1838 | ||
1839 | return NULL; | |
1840 | out: | |
75868d0e | 1841 | ofputil_uninit_group_mod(gm); |
7395c052 NZ |
1842 | return error; |
1843 | } | |
1844 | ||
25070e04 JR |
1845 | /* If 'command' is given as -2, each line may start with a command name ("add", |
1846 | * "modify", "add_or_mod", "delete", "insert_bucket", or "remove_bucket"). A | |
1847 | * missing command name is treated as "add". | |
1848 | */ | |
cab50449 | 1849 | char * OVS_WARN_UNUSED_RESULT |
25070e04 | 1850 | parse_ofp_group_mod_str(struct ofputil_group_mod *gm, int command, |
7395c052 | 1851 | const char *str_, |
50f96b10 | 1852 | const struct ofputil_port_map *port_map, |
7395c052 NZ |
1853 | enum ofputil_protocol *usable_protocols) |
1854 | { | |
1855 | char *string = xstrdup(str_); | |
1856 | char *error = parse_ofp_group_mod_str__(gm, command, string, | |
50f96b10 | 1857 | port_map, usable_protocols); |
7395c052 | 1858 | free(string); |
7395c052 NZ |
1859 | return error; |
1860 | } | |
1861 | ||
25070e04 JR |
1862 | /* If 'command' is given as -2, each line may start with a command name ("add", |
1863 | * "modify", "add_or_mod", "delete", "insert_bucket", or "remove_bucket"). A | |
1864 | * missing command name is treated as "add". | |
1865 | */ | |
cab50449 | 1866 | char * OVS_WARN_UNUSED_RESULT |
50f96b10 BP |
1867 | parse_ofp_group_mod_file(const char *file_name, |
1868 | const struct ofputil_port_map *port_map, | |
1869 | int command, | |
7395c052 NZ |
1870 | struct ofputil_group_mod **gms, size_t *n_gms, |
1871 | enum ofputil_protocol *usable_protocols) | |
1872 | { | |
1873 | size_t allocated_gms; | |
1874 | int line_number; | |
1875 | FILE *stream; | |
1876 | struct ds s; | |
1877 | ||
1878 | *gms = NULL; | |
1879 | *n_gms = 0; | |
1880 | ||
1881 | stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r"); | |
1882 | if (stream == NULL) { | |
1883 | return xasprintf("%s: open failed (%s)", | |
1884 | file_name, ovs_strerror(errno)); | |
1885 | } | |
1886 | ||
1887 | allocated_gms = *n_gms; | |
1888 | ds_init(&s); | |
1889 | line_number = 0; | |
1890 | *usable_protocols = OFPUTIL_P_OF11_UP; | |
1891 | while (!ds_get_preprocessed_line(&s, stream, &line_number)) { | |
1892 | enum ofputil_protocol usable; | |
1893 | char *error; | |
1894 | ||
1895 | if (*n_gms >= allocated_gms) { | |
c7ecbf1e | 1896 | struct ofputil_group_mod *new_gms; |
2134b5ec SH |
1897 | size_t i; |
1898 | ||
c7ecbf1e | 1899 | new_gms = x2nrealloc(*gms, &allocated_gms, sizeof **gms); |
2134b5ec | 1900 | for (i = 0; i < *n_gms; i++) { |
417e7e66 | 1901 | ovs_list_moved(&new_gms[i].buckets, &(*gms)[i].buckets); |
2134b5ec | 1902 | } |
c7ecbf1e | 1903 | *gms = new_gms; |
7395c052 NZ |
1904 | } |
1905 | error = parse_ofp_group_mod_str(&(*gms)[*n_gms], command, ds_cstr(&s), | |
50f96b10 | 1906 | port_map, &usable); |
7395c052 NZ |
1907 | if (error) { |
1908 | size_t i; | |
1909 | ||
1910 | for (i = 0; i < *n_gms; i++) { | |
75868d0e | 1911 | ofputil_uninit_group_mod(&(*gms)[i]); |
7395c052 NZ |
1912 | } |
1913 | free(*gms); | |
1914 | *gms = NULL; | |
1915 | *n_gms = 0; | |
1916 | ||
1917 | ds_destroy(&s); | |
1918 | if (stream != stdin) { | |
1919 | fclose(stream); | |
1920 | } | |
1921 | ||
88a74b20 BP |
1922 | char *ret = xasprintf("%s:%d: %s", file_name, line_number, error); |
1923 | free(error); | |
1924 | return ret; | |
7395c052 NZ |
1925 | } |
1926 | *usable_protocols &= usable; | |
1927 | *n_gms += 1; | |
1928 | } | |
1929 | ||
1930 | ds_destroy(&s); | |
1931 | if (stream != stdin) { | |
1932 | fclose(stream); | |
1933 | } | |
1934 | return NULL; | |
1935 | } | |
6159c531 | 1936 | |
25070e04 JR |
1937 | /* Opens file 'file_name' and reads each line as a flow_mod or a group_mod, |
1938 | * depending on the first keyword on each line. Stores each flow and group | |
1939 | * mods in '*bms', an array allocated on the caller's behalf, and the number of | |
1940 | * messages in '*n_bms'. | |
1941 | * | |
1942 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
1943 | * error. The caller is responsible for freeing the returned string. */ | |
1944 | char * OVS_WARN_UNUSED_RESULT | |
1945 | parse_ofp_bundle_file(const char *file_name, | |
50f96b10 | 1946 | const struct ofputil_port_map *port_map, |
25070e04 JR |
1947 | struct ofputil_bundle_msg **bms, size_t *n_bms, |
1948 | enum ofputil_protocol *usable_protocols) | |
1949 | { | |
1950 | size_t allocated_bms; | |
1951 | char *error = NULL; | |
1952 | int line_number; | |
1953 | FILE *stream; | |
1954 | struct ds ds; | |
1955 | ||
1956 | *usable_protocols = OFPUTIL_P_ANY; | |
1957 | ||
1958 | *bms = NULL; | |
1959 | *n_bms = 0; | |
1960 | ||
1961 | stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r"); | |
1962 | if (stream == NULL) { | |
1963 | return xasprintf("%s: open failed (%s)", | |
1964 | file_name, ovs_strerror(errno)); | |
1965 | } | |
1966 | ||
1967 | allocated_bms = *n_bms; | |
1968 | ds_init(&ds); | |
1969 | line_number = 0; | |
1970 | while (!ds_get_preprocessed_line(&ds, stream, &line_number)) { | |
1971 | enum ofputil_protocol usable; | |
1972 | char *s = ds_cstr(&ds); | |
1973 | size_t len; | |
1974 | ||
1975 | if (*n_bms >= allocated_bms) { | |
1976 | struct ofputil_bundle_msg *new_bms; | |
1977 | ||
1978 | new_bms = x2nrealloc(*bms, &allocated_bms, sizeof **bms); | |
1979 | for (size_t i = 0; i < *n_bms; i++) { | |
1980 | if (new_bms[i].type == OFPTYPE_GROUP_MOD) { | |
1981 | ovs_list_moved(&new_bms[i].gm.buckets, | |
1982 | &(*bms)[i].gm.buckets); | |
1983 | } | |
1984 | } | |
1985 | *bms = new_bms; | |
1986 | } | |
1987 | ||
1988 | s += strspn(s, " \t\r\n"); /* Skip white space. */ | |
1989 | len = strcspn(s, ", \t\r\n"); /* Get length of the first token. */ | |
1990 | ||
1991 | if (!strncmp(s, "flow", len)) { | |
1992 | s += len; | |
50f96b10 BP |
1993 | error = parse_ofp_flow_mod_str(&(*bms)[*n_bms].fm, s, port_map, |
1994 | -2, &usable); | |
25070e04 JR |
1995 | if (error) { |
1996 | break; | |
1997 | } | |
1998 | (*bms)[*n_bms].type = OFPTYPE_FLOW_MOD; | |
1999 | } else if (!strncmp(s, "group", len)) { | |
2000 | s += len; | |
2001 | error = parse_ofp_group_mod_str(&(*bms)[*n_bms].gm, -2, s, | |
50f96b10 | 2002 | port_map, &usable); |
25070e04 JR |
2003 | if (error) { |
2004 | break; | |
2005 | } | |
2006 | (*bms)[*n_bms].type = OFPTYPE_GROUP_MOD; | |
6dd3c787 JR |
2007 | } else if (!strncmp(s, "packet-out", len)) { |
2008 | s += len; | |
50f96b10 BP |
2009 | error = parse_ofp_packet_out_str(&(*bms)[*n_bms].po, s, port_map, |
2010 | &usable); | |
6dd3c787 JR |
2011 | if (error) { |
2012 | break; | |
2013 | } | |
2014 | (*bms)[*n_bms].type = OFPTYPE_PACKET_OUT; | |
25070e04 JR |
2015 | } else { |
2016 | error = xasprintf("Unsupported bundle message type: %.*s", | |
2017 | (int)len, s); | |
2018 | break; | |
2019 | } | |
2020 | ||
2021 | *usable_protocols &= usable; /* Each line can narrow the set. */ | |
2022 | *n_bms += 1; | |
2023 | } | |
2024 | ||
2025 | ds_destroy(&ds); | |
2026 | if (stream != stdin) { | |
2027 | fclose(stream); | |
2028 | } | |
2029 | ||
2030 | if (error) { | |
2031 | char *err_msg = xasprintf("%s:%d: %s", file_name, line_number, error); | |
2032 | free(error); | |
2033 | ||
6dd3c787 JR |
2034 | ofputil_free_bundle_msgs(*bms, *n_bms); |
2035 | *bms = NULL; | |
2036 | *n_bms = 0; | |
25070e04 JR |
2037 | return err_msg; |
2038 | } | |
2039 | return NULL; | |
2040 | } | |
2041 | ||
6159c531 | 2042 | char * OVS_WARN_UNUSED_RESULT |
4e548ad9 | 2043 | parse_ofp_tlv_table_mod_str(struct ofputil_tlv_table_mod *ttm, |
6159c531 JG |
2044 | uint16_t command, const char *s, |
2045 | enum ofputil_protocol *usable_protocols) | |
2046 | { | |
2047 | *usable_protocols = OFPUTIL_P_NXM_OXM_ANY; | |
2048 | ||
4e548ad9 | 2049 | ttm->command = command; |
417e7e66 | 2050 | ovs_list_init(&ttm->mappings); |
6159c531 JG |
2051 | |
2052 | while (*s) { | |
4e548ad9 | 2053 | struct ofputil_tlv_map *map = xmalloc(sizeof *map); |
6159c531 JG |
2054 | int n; |
2055 | ||
2056 | if (*s == ',') { | |
2057 | s++; | |
2058 | } | |
2059 | ||
417e7e66 | 2060 | ovs_list_push_back(&ttm->mappings, &map->list_node); |
6159c531 JG |
2061 | |
2062 | if (!ovs_scan(s, "{class=%"SCNi16",type=%"SCNi8",len=%"SCNi8"}->tun_metadata%"SCNi16"%n", | |
2063 | &map->option_class, &map->option_type, &map->option_len, | |
2064 | &map->index, &n)) { | |
4e548ad9 ML |
2065 | ofputil_uninit_tlv_table(&ttm->mappings); |
2066 | return xstrdup("invalid tlv mapping"); | |
6159c531 JG |
2067 | } |
2068 | ||
2069 | s += n; | |
2070 | } | |
2071 | ||
2072 | return NULL; | |
2073 | } |