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