]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ofp-parse.c
ofp-util: Add flow metadata to ofputil_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_set_in_port(&po->flow_metadata, OFPP_CONTROLLER);
626
627 act_str = extract_actions(string);
628
629 while (ofputil_parse_key_value(&string, &name, &value)) {
630 if (!*value) {
631 error = xasprintf("field %s missing value", name);
632 goto out;
633 }
634
635 if (!strcmp(name, "in_port")) {
636 ofp_port_t in_port;
637 if (!ofputil_port_from_string(value, &in_port)) {
638 error = xasprintf("%s is not a valid OpenFlow port", value);
639 goto out;
640 }
641 if (ofp_to_u16(in_port) > ofp_to_u16(OFPP_MAX)
642 && in_port != OFPP_LOCAL
643 && in_port != OFPP_NONE
644 && in_port != OFPP_CONTROLLER) {
645 error = xasprintf(
646 "%s is not a valid OpenFlow port for PACKET_OUT",
647 value);
648 goto out;
649 }
650 match_set_in_port(&po->flow_metadata, in_port);
651 } else if (!strcmp(name, "packet")) {
652 const char *error_msg = eth_from_hex(value, &packet);
653 if (error_msg) {
654 error = xasprintf("%s: %s", name, error_msg);
655 goto out;
656 }
657 } else {
658 error = xasprintf("unknown keyword %s", name);
659 goto out;
660 }
661 }
662
663 if (!packet || !dp_packet_size(packet)) {
664 error = xstrdup("must specify packet");
665 goto out;
666 }
667
668 if (act_str) {
669 error = ofpacts_parse_actions(act_str, &ofpacts,
670 &action_usable_protocols);
671 *usable_protocols &= action_usable_protocols;
672 if (error) {
673 goto out;
674 }
675 }
676 po->ofpacts_len = ofpacts.size;
677 po->ofpacts = ofpbuf_steal_data(&ofpacts);
678
679 po->packet_len = dp_packet_size(packet);
680 po->packet = dp_packet_steal_data(packet);
681 out:
682 ofpbuf_uninit(&ofpacts);
683 dp_packet_delete(packet);
684 return error;
685 }
686
687 /* Convert 'str_' (as described in the Packet-Out Syntax section of the
688 * ovs-ofctl man page) into 'po' for sending a OFPT_PACKET_OUT message to a
689 * switch. Returns the set of usable protocols in '*usable_protocols'.
690 *
691 * Returns NULL if successful, otherwise a malloc()'d string describing the
692 * error. The caller is responsible for freeing the returned string. */
693 char * OVS_WARN_UNUSED_RESULT
694 parse_ofp_packet_out_str(struct ofputil_packet_out *po, const char *str_,
695 enum ofputil_protocol *usable_protocols)
696 {
697 char *string = xstrdup(str_);
698 char *error;
699
700 error = parse_ofp_packet_out_str__(po, string, usable_protocols);
701 if (error) {
702 po->ofpacts = NULL;
703 po->ofpacts_len = 0;
704 }
705
706 free(string);
707 return error;
708 }
709
710 static char * OVS_WARN_UNUSED_RESULT
711 parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string,
712 struct ofpbuf *bands, int command,
713 enum ofputil_protocol *usable_protocols)
714 {
715 enum {
716 F_METER = 1 << 0,
717 F_FLAGS = 1 << 1,
718 F_BANDS = 1 << 2,
719 } fields;
720 char *save_ptr = NULL;
721 char *band_str = NULL;
722 char *name;
723
724 /* Meters require at least OF 1.3. */
725 *usable_protocols = OFPUTIL_P_OF13_UP;
726
727 switch (command) {
728 case -1:
729 fields = F_METER;
730 break;
731
732 case OFPMC13_ADD:
733 fields = F_METER | F_FLAGS | F_BANDS;
734 break;
735
736 case OFPMC13_DELETE:
737 fields = F_METER;
738 break;
739
740 case OFPMC13_MODIFY:
741 fields = F_METER | F_FLAGS | F_BANDS;
742 break;
743
744 default:
745 OVS_NOT_REACHED();
746 }
747
748 mm->command = command;
749 mm->meter.meter_id = 0;
750 mm->meter.flags = 0;
751 if (fields & F_BANDS) {
752 band_str = strstr(string, "band");
753 if (!band_str) {
754 return xstrdup("must specify bands");
755 }
756 *band_str = '\0';
757
758 band_str = strchr(band_str + 1, '=');
759 if (!band_str) {
760 return xstrdup("must specify bands");
761 }
762
763 band_str++;
764 }
765 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
766 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
767
768 if (fields & F_FLAGS && !strcmp(name, "kbps")) {
769 mm->meter.flags |= OFPMF13_KBPS;
770 } else if (fields & F_FLAGS && !strcmp(name, "pktps")) {
771 mm->meter.flags |= OFPMF13_PKTPS;
772 } else if (fields & F_FLAGS && !strcmp(name, "burst")) {
773 mm->meter.flags |= OFPMF13_BURST;
774 } else if (fields & F_FLAGS && !strcmp(name, "stats")) {
775 mm->meter.flags |= OFPMF13_STATS;
776 } else {
777 char *value;
778
779 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
780 if (!value) {
781 return xasprintf("field %s missing value", name);
782 }
783
784 if (!strcmp(name, "meter")) {
785 if (!strcmp(value, "all")) {
786 mm->meter.meter_id = OFPM13_ALL;
787 } else if (!strcmp(value, "controller")) {
788 mm->meter.meter_id = OFPM13_CONTROLLER;
789 } else if (!strcmp(value, "slowpath")) {
790 mm->meter.meter_id = OFPM13_SLOWPATH;
791 } else {
792 char *error = str_to_u32(value, &mm->meter.meter_id);
793 if (error) {
794 return error;
795 }
796 if (mm->meter.meter_id > OFPM13_MAX
797 || !mm->meter.meter_id) {
798 return xasprintf("invalid value for %s", name);
799 }
800 }
801 } else {
802 return xasprintf("unknown keyword %s", name);
803 }
804 }
805 }
806 if (fields & F_METER && !mm->meter.meter_id) {
807 return xstrdup("must specify 'meter'");
808 }
809 if (fields & F_FLAGS && !mm->meter.flags) {
810 return xstrdup("meter must specify either 'kbps' or 'pktps'");
811 }
812
813 if (fields & F_BANDS) {
814 uint16_t n_bands = 0;
815 struct ofputil_meter_band *band = NULL;
816 int i;
817
818 for (name = strtok_r(band_str, "=, \t\r\n", &save_ptr); name;
819 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
820
821 char *value;
822
823 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
824 if (!value) {
825 return xasprintf("field %s missing value", name);
826 }
827
828 if (!strcmp(name, "type")) {
829 /* Start a new band */
830 band = ofpbuf_put_zeros(bands, sizeof *band);
831 n_bands++;
832
833 if (!strcmp(value, "drop")) {
834 band->type = OFPMBT13_DROP;
835 } else if (!strcmp(value, "dscp_remark")) {
836 band->type = OFPMBT13_DSCP_REMARK;
837 } else {
838 return xasprintf("field %s unknown value %s", name, value);
839 }
840 } else if (!band || !band->type) {
841 return xstrdup("band must start with the 'type' keyword");
842 } else if (!strcmp(name, "rate")) {
843 char *error = str_to_u32(value, &band->rate);
844 if (error) {
845 return error;
846 }
847 } else if (!strcmp(name, "burst_size")) {
848 char *error = str_to_u32(value, &band->burst_size);
849 if (error) {
850 return error;
851 }
852 } else if (!strcmp(name, "prec_level")) {
853 char *error = str_to_u8(value, name, &band->prec_level);
854 if (error) {
855 return error;
856 }
857 } else {
858 return xasprintf("unknown keyword %s", name);
859 }
860 }
861 /* validate bands */
862 if (!n_bands) {
863 return xstrdup("meter must have bands");
864 }
865
866 mm->meter.n_bands = n_bands;
867 mm->meter.bands = ofpbuf_steal_data(bands);
868
869 for (i = 0; i < n_bands; ++i) {
870 band = &mm->meter.bands[i];
871
872 if (!band->type) {
873 return xstrdup("band must have 'type'");
874 }
875 if (band->type == OFPMBT13_DSCP_REMARK) {
876 if (!band->prec_level) {
877 return xstrdup("'dscp_remark' band must have"
878 " 'prec_level'");
879 }
880 } else {
881 if (band->prec_level) {
882 return xstrdup("Only 'dscp_remark' band may have"
883 " 'prec_level'");
884 }
885 }
886 if (!band->rate) {
887 return xstrdup("band must have 'rate'");
888 }
889 if (mm->meter.flags & OFPMF13_BURST) {
890 if (!band->burst_size) {
891 return xstrdup("band must have 'burst_size' "
892 "when 'burst' flag is set");
893 }
894 } else {
895 if (band->burst_size) {
896 return xstrdup("band may have 'burst_size' only "
897 "when 'burst' flag is set");
898 }
899 }
900 }
901 } else {
902 mm->meter.n_bands = 0;
903 mm->meter.bands = NULL;
904 }
905
906 return NULL;
907 }
908
909 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
910 * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
911 *
912 * Returns NULL if successful, otherwise a malloc()'d string describing the
913 * error. The caller is responsible for freeing the returned string. */
914 char * OVS_WARN_UNUSED_RESULT
915 parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_,
916 int command, enum ofputil_protocol *usable_protocols)
917 {
918 struct ofpbuf bands;
919 char *string;
920 char *error;
921
922 ofpbuf_init(&bands, 64);
923 string = xstrdup(str_);
924
925 error = parse_ofp_meter_mod_str__(mm, string, &bands, command,
926 usable_protocols);
927
928 free(string);
929 ofpbuf_uninit(&bands);
930
931 return error;
932 }
933
934 static char * OVS_WARN_UNUSED_RESULT
935 parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
936 const char *str_, char *string,
937 enum ofputil_protocol *usable_protocols)
938 {
939 static atomic_count id = ATOMIC_COUNT_INIT(0);
940 char *name, *value;
941
942 fmr->id = atomic_count_inc(&id);
943
944 fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
945 | NXFMF_OWN | NXFMF_ACTIONS);
946 fmr->out_port = OFPP_NONE;
947 fmr->table_id = 0xff;
948 match_init_catchall(&fmr->match);
949
950 while (ofputil_parse_key_value(&string, &name, &value)) {
951 const struct protocol *p;
952 char *error = NULL;
953
954 if (!strcmp(name, "!initial")) {
955 fmr->flags &= ~NXFMF_INITIAL;
956 } else if (!strcmp(name, "!add")) {
957 fmr->flags &= ~NXFMF_ADD;
958 } else if (!strcmp(name, "!delete")) {
959 fmr->flags &= ~NXFMF_DELETE;
960 } else if (!strcmp(name, "!modify")) {
961 fmr->flags &= ~NXFMF_MODIFY;
962 } else if (!strcmp(name, "!actions")) {
963 fmr->flags &= ~NXFMF_ACTIONS;
964 } else if (!strcmp(name, "!own")) {
965 fmr->flags &= ~NXFMF_OWN;
966 } else if (parse_protocol(name, &p)) {
967 match_set_dl_type(&fmr->match, htons(p->dl_type));
968 if (p->nw_proto) {
969 match_set_nw_proto(&fmr->match, p->nw_proto);
970 }
971 } else if (mf_from_name(name)) {
972 error = parse_field(mf_from_name(name), value, &fmr->match,
973 usable_protocols);
974 } else {
975 if (!*value) {
976 return xasprintf("%s: field %s missing value", str_, name);
977 }
978
979 if (!strcmp(name, "table")) {
980 error = str_to_u8(value, "table", &fmr->table_id);
981 } else if (!strcmp(name, "out_port")) {
982 fmr->out_port = u16_to_ofp(atoi(value));
983 } else {
984 return xasprintf("%s: unknown keyword %s", str_, name);
985 }
986 }
987
988 if (error) {
989 return error;
990 }
991 }
992 return NULL;
993 }
994
995 /* Convert 'str_' (as described in the documentation for the "monitor" command
996 * in the ovs-ofctl man page) into 'fmr'.
997 *
998 * Returns NULL if successful, otherwise a malloc()'d string describing the
999 * error. The caller is responsible for freeing the returned string. */
1000 char * OVS_WARN_UNUSED_RESULT
1001 parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
1002 const char *str_,
1003 enum ofputil_protocol *usable_protocols)
1004 {
1005 char *string = xstrdup(str_);
1006 char *error = parse_flow_monitor_request__(fmr, str_, string,
1007 usable_protocols);
1008 free(string);
1009 return error;
1010 }
1011
1012 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
1013 * (one of OFPFC_*) into 'fm'.
1014 *
1015 * If 'command' is given as -2, 'string' may begin with a command name ("add",
1016 * "modify", "delete", "modify_strict", or "delete_strict"). A missing command
1017 * name is treated as "add".
1018 *
1019 * Returns NULL if successful, otherwise a malloc()'d string describing the
1020 * error. The caller is responsible for freeing the returned string. */
1021 char * OVS_WARN_UNUSED_RESULT
1022 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
1023 int command,
1024 enum ofputil_protocol *usable_protocols)
1025 {
1026 char *error = parse_ofp_str(fm, command, string, usable_protocols);
1027
1028 if (!error) {
1029 /* Normalize a copy of the match. This ensures that non-normalized
1030 * flows get logged but doesn't affect what gets sent to the switch, so
1031 * that the switch can do whatever it likes with the flow. */
1032 struct match match_copy = fm->match;
1033 ofputil_normalize_match(&match_copy);
1034 }
1035
1036 return error;
1037 }
1038
1039 /* Convert 'setting' (as described for the "mod-table" command
1040 * in ovs-ofctl man page) into 'tm->table_vacancy->vacancy_up' and
1041 * 'tm->table_vacancy->vacancy_down' threshold values.
1042 * For the two threshold values, value of vacancy_up is always greater
1043 * than value of vacancy_down.
1044 *
1045 * Returns NULL if successful, otherwise a malloc()'d string describing the
1046 * error. The caller is responsible for freeing the returned string. */
1047 char * OVS_WARN_UNUSED_RESULT
1048 parse_ofp_table_vacancy(struct ofputil_table_mod *tm, const char *setting)
1049 {
1050 char *save_ptr = NULL;
1051 char *vac_up, *vac_down;
1052 char *value = xstrdup(setting);
1053 char *ret_msg;
1054 int vacancy_up, vacancy_down;
1055
1056 strtok_r(value, ":", &save_ptr);
1057 vac_down = strtok_r(NULL, ",", &save_ptr);
1058 if (!vac_down) {
1059 ret_msg = xasprintf("Vacancy down value missing");
1060 goto exit;
1061 }
1062 if (!str_to_int(vac_down, 0, &vacancy_down) ||
1063 vacancy_down < 0 || vacancy_down > 100) {
1064 ret_msg = xasprintf("Invalid vacancy down value \"%s\"", vac_down);
1065 goto exit;
1066 }
1067 vac_up = strtok_r(NULL, ",", &save_ptr);
1068 if (!vac_up) {
1069 ret_msg = xasprintf("Vacancy up value missing");
1070 goto exit;
1071 }
1072 if (!str_to_int(vac_up, 0, &vacancy_up) ||
1073 vacancy_up < 0 || vacancy_up > 100) {
1074 ret_msg = xasprintf("Invalid vacancy up value \"%s\"", vac_up);
1075 goto exit;
1076 }
1077 if (vacancy_down > vacancy_up) {
1078 ret_msg = xasprintf("Invalid vacancy range, vacancy up should be "
1079 "greater than vacancy down (%s)",
1080 ofperr_to_string(OFPERR_OFPBPC_BAD_VALUE));
1081 goto exit;
1082 }
1083
1084 free(value);
1085 tm->table_vacancy.vacancy_down = vacancy_down;
1086 tm->table_vacancy.vacancy_up = vacancy_up;
1087 return NULL;
1088
1089 exit:
1090 free(value);
1091 return ret_msg;
1092 }
1093
1094 /* Convert 'table_id' and 'setting' (as described for the "mod-table" command
1095 * in the ovs-ofctl man page) into 'tm' for sending a table_mod command to a
1096 * switch.
1097 *
1098 * Stores a bitmap of the OpenFlow versions that are usable for 'tm' into
1099 * '*usable_versions'.
1100 *
1101 * Returns NULL if successful, otherwise a malloc()'d string describing the
1102 * error. The caller is responsible for freeing the returned string. */
1103 char * OVS_WARN_UNUSED_RESULT
1104 parse_ofp_table_mod(struct ofputil_table_mod *tm, const char *table_id,
1105 const char *setting, uint32_t *usable_versions)
1106 {
1107 *usable_versions = 0;
1108 if (!strcasecmp(table_id, "all")) {
1109 tm->table_id = OFPTT_ALL;
1110 } else {
1111 char *error = str_to_u8(table_id, "table_id", &tm->table_id);
1112 if (error) {
1113 return error;
1114 }
1115 }
1116
1117 tm->miss = OFPUTIL_TABLE_MISS_DEFAULT;
1118 tm->eviction = OFPUTIL_TABLE_EVICTION_DEFAULT;
1119 tm->eviction_flags = UINT32_MAX;
1120 tm->vacancy = OFPUTIL_TABLE_VACANCY_DEFAULT;
1121 tm->table_vacancy.vacancy_down = 0;
1122 tm->table_vacancy.vacancy_up = 0;
1123 tm->table_vacancy.vacancy = 0;
1124 /* Only OpenFlow 1.1 and 1.2 can configure table-miss via table_mod.
1125 * Only OpenFlow 1.4+ can configure eviction and vacancy events
1126 * via table_mod.
1127 */
1128 if (!strcmp(setting, "controller")) {
1129 tm->miss = OFPUTIL_TABLE_MISS_CONTROLLER;
1130 *usable_versions = (1u << OFP11_VERSION) | (1u << OFP12_VERSION);
1131 } else if (!strcmp(setting, "continue")) {
1132 tm->miss = OFPUTIL_TABLE_MISS_CONTINUE;
1133 *usable_versions = (1u << OFP11_VERSION) | (1u << OFP12_VERSION);
1134 } else if (!strcmp(setting, "drop")) {
1135 tm->miss = OFPUTIL_TABLE_MISS_DROP;
1136 *usable_versions = (1u << OFP11_VERSION) | (1u << OFP12_VERSION);
1137 } else if (!strcmp(setting, "evict")) {
1138 tm->eviction = OFPUTIL_TABLE_EVICTION_ON;
1139 *usable_versions = (1 << OFP14_VERSION) | (1u << OFP15_VERSION);
1140 } else if (!strcmp(setting, "noevict")) {
1141 tm->eviction = OFPUTIL_TABLE_EVICTION_OFF;
1142 *usable_versions = (1 << OFP14_VERSION) | (1u << OFP15_VERSION);
1143 } else if (!strncmp(setting, "vacancy", strcspn(setting, ":"))) {
1144 tm->vacancy = OFPUTIL_TABLE_VACANCY_ON;
1145 *usable_versions = (1 << OFP14_VERSION) | (1u << OFP15_VERSION);
1146 char *error = parse_ofp_table_vacancy(tm, setting);
1147 if (error) {
1148 return error;
1149 }
1150 } else if (!strcmp(setting, "novacancy")) {
1151 tm->vacancy = OFPUTIL_TABLE_VACANCY_OFF;
1152 *usable_versions = (1 << OFP14_VERSION) | (1u << OFP15_VERSION);
1153 } else {
1154 return xasprintf("invalid table_mod setting %s", setting);
1155 }
1156
1157 if (tm->table_id == 0xfe
1158 && tm->miss == OFPUTIL_TABLE_MISS_CONTINUE) {
1159 return xstrdup("last table's flow miss handling can not be continue");
1160 }
1161
1162 return NULL;
1163 }
1164
1165
1166 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
1167 * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated
1168 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
1169 *
1170 * If 'command' is given as -2, each line may start with a command name
1171 * ("add", "modify", "delete", "modify_strict", or "delete_strict"). A missing
1172 * command name is treated as "add".
1173 *
1174 * Returns NULL if successful, otherwise a malloc()'d string describing the
1175 * error. The caller is responsible for freeing the returned string. */
1176 char * OVS_WARN_UNUSED_RESULT
1177 parse_ofp_flow_mod_file(const char *file_name, int command,
1178 struct ofputil_flow_mod **fms, size_t *n_fms,
1179 enum ofputil_protocol *usable_protocols)
1180 {
1181 size_t allocated_fms;
1182 int line_number;
1183 FILE *stream;
1184 struct ds s;
1185
1186 *usable_protocols = OFPUTIL_P_ANY;
1187
1188 *fms = NULL;
1189 *n_fms = 0;
1190
1191 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1192 if (stream == NULL) {
1193 return xasprintf("%s: open failed (%s)",
1194 file_name, ovs_strerror(errno));
1195 }
1196
1197 allocated_fms = *n_fms;
1198 ds_init(&s);
1199 line_number = 0;
1200 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
1201 char *error;
1202 enum ofputil_protocol usable;
1203
1204 if (*n_fms >= allocated_fms) {
1205 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
1206 }
1207 error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command,
1208 &usable);
1209 if (error) {
1210 char *err_msg;
1211 size_t i;
1212
1213 for (i = 0; i < *n_fms; i++) {
1214 free(CONST_CAST(struct ofpact *, (*fms)[i].ofpacts));
1215 }
1216 free(*fms);
1217 *fms = NULL;
1218 *n_fms = 0;
1219
1220 ds_destroy(&s);
1221 if (stream != stdin) {
1222 fclose(stream);
1223 }
1224
1225 err_msg = xasprintf("%s:%d: %s", file_name, line_number, error);
1226 free(error);
1227 return err_msg;
1228 }
1229 *usable_protocols &= usable; /* Each line can narrow the set. */
1230 *n_fms += 1;
1231 }
1232
1233 ds_destroy(&s);
1234 if (stream != stdin) {
1235 fclose(stream);
1236 }
1237 return NULL;
1238 }
1239
1240 char * OVS_WARN_UNUSED_RESULT
1241 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
1242 bool aggregate, const char *string,
1243 enum ofputil_protocol *usable_protocols)
1244 {
1245 struct ofputil_flow_mod fm;
1246 char *error;
1247
1248 error = parse_ofp_str(&fm, -1, string, usable_protocols);
1249 if (error) {
1250 return error;
1251 }
1252
1253 /* Special table ID support not required for stats requests. */
1254 if (*usable_protocols & OFPUTIL_P_OF10_STD_TID) {
1255 *usable_protocols |= OFPUTIL_P_OF10_STD;
1256 }
1257 if (*usable_protocols & OFPUTIL_P_OF10_NXM_TID) {
1258 *usable_protocols |= OFPUTIL_P_OF10_NXM;
1259 }
1260
1261 fsr->aggregate = aggregate;
1262 fsr->cookie = fm.cookie;
1263 fsr->cookie_mask = fm.cookie_mask;
1264 fsr->match = fm.match;
1265 fsr->out_port = fm.out_port;
1266 fsr->out_group = fm.out_group;
1267 fsr->table_id = fm.table_id;
1268 return NULL;
1269 }
1270
1271 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
1272 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1273 * mf_field. Fields must be specified in a natural order for satisfying
1274 * prerequisites. If 'wc' is specified, masks the field in 'wc' for each of the
1275 * field specified in flow. If the map, 'names_portno' is specfied, converts
1276 * the in_port name into port no while setting the 'flow'.
1277 *
1278 * Returns NULL on success, otherwise a malloc()'d string that explains the
1279 * problem. */
1280 char *
1281 parse_ofp_exact_flow(struct flow *flow, struct flow_wildcards *wc,
1282 const struct tun_table *tun_table, const char *s,
1283 const struct simap *portno_names)
1284 {
1285 char *pos, *key, *value_s;
1286 char *error = NULL;
1287 char *copy;
1288
1289 memset(flow, 0, sizeof *flow);
1290 if (wc) {
1291 memset(wc, 0, sizeof *wc);
1292 }
1293 flow->tunnel.metadata.tab = tun_table;
1294
1295 pos = copy = xstrdup(s);
1296 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
1297 const struct protocol *p;
1298 if (parse_protocol(key, &p)) {
1299 if (flow->dl_type) {
1300 error = xasprintf("%s: Ethernet type set multiple times", s);
1301 goto exit;
1302 }
1303 flow->dl_type = htons(p->dl_type);
1304 if (wc) {
1305 wc->masks.dl_type = OVS_BE16_MAX;
1306 }
1307
1308 if (p->nw_proto) {
1309 if (flow->nw_proto) {
1310 error = xasprintf("%s: network protocol set "
1311 "multiple times", s);
1312 goto exit;
1313 }
1314 flow->nw_proto = p->nw_proto;
1315 if (wc) {
1316 wc->masks.nw_proto = UINT8_MAX;
1317 }
1318 }
1319 } else {
1320 const struct mf_field *mf;
1321 union mf_value value;
1322 char *field_error;
1323
1324 mf = mf_from_name(key);
1325 if (!mf) {
1326 error = xasprintf("%s: unknown field %s", s, key);
1327 goto exit;
1328 }
1329
1330 if (!mf_are_prereqs_ok(mf, flow, NULL)) {
1331 error = xasprintf("%s: prerequisites not met for setting %s",
1332 s, key);
1333 goto exit;
1334 }
1335
1336 if (mf_is_set(mf, flow)) {
1337 error = xasprintf("%s: field %s set multiple times", s, key);
1338 goto exit;
1339 }
1340
1341 if (!strcmp(key, "in_port")
1342 && portno_names
1343 && simap_contains(portno_names, value_s)) {
1344 flow->in_port.ofp_port = u16_to_ofp(
1345 simap_get(portno_names, value_s));
1346 if (wc) {
1347 wc->masks.in_port.ofp_port
1348 = u16_to_ofp(ntohs(OVS_BE16_MAX));
1349 }
1350 } else {
1351 field_error = mf_parse_value(mf, value_s, &value);
1352 if (field_error) {
1353 error = xasprintf("%s: bad value for %s (%s)",
1354 s, key, field_error);
1355 free(field_error);
1356 goto exit;
1357 }
1358
1359 mf_set_flow_value(mf, &value, flow);
1360 if (wc) {
1361 mf_mask_field(mf, wc);
1362 }
1363 }
1364 }
1365 }
1366
1367 if (!flow->in_port.ofp_port) {
1368 flow->in_port.ofp_port = OFPP_NONE;
1369 }
1370
1371 exit:
1372 free(copy);
1373
1374 if (error) {
1375 memset(flow, 0, sizeof *flow);
1376 if (wc) {
1377 memset(wc, 0, sizeof *wc);
1378 }
1379 }
1380 return error;
1381 }
1382
1383 static char * OVS_WARN_UNUSED_RESULT
1384 parse_bucket_str(struct ofputil_bucket *bucket, char *str_, uint8_t group_type,
1385 enum ofputil_protocol *usable_protocols)
1386 {
1387 char *pos, *key, *value;
1388 struct ofpbuf ofpacts;
1389 struct ds actions;
1390 char *error;
1391
1392 bucket->weight = group_type == OFPGT11_SELECT ? 1 : 0;
1393 bucket->bucket_id = OFPG15_BUCKET_ALL;
1394 bucket->watch_port = OFPP_ANY;
1395 bucket->watch_group = OFPG_ANY;
1396
1397 ds_init(&actions);
1398
1399 pos = str_;
1400 error = NULL;
1401 while (ofputil_parse_key_value(&pos, &key, &value)) {
1402 if (!strcasecmp(key, "weight")) {
1403 error = str_to_u16(value, "weight", &bucket->weight);
1404 } else if (!strcasecmp(key, "watch_port")) {
1405 if (!ofputil_port_from_string(value, &bucket->watch_port)
1406 || (ofp_to_u16(bucket->watch_port) >= ofp_to_u16(OFPP_MAX)
1407 && bucket->watch_port != OFPP_ANY)) {
1408 error = xasprintf("%s: invalid watch_port", value);
1409 }
1410 } else if (!strcasecmp(key, "watch_group")) {
1411 error = str_to_u32(value, &bucket->watch_group);
1412 if (!error && bucket->watch_group > OFPG_MAX) {
1413 error = xasprintf("invalid watch_group id %"PRIu32,
1414 bucket->watch_group);
1415 }
1416 } else if (!strcasecmp(key, "bucket_id")) {
1417 error = str_to_u32(value, &bucket->bucket_id);
1418 if (!error && bucket->bucket_id > OFPG15_BUCKET_MAX) {
1419 error = xasprintf("invalid bucket_id id %"PRIu32,
1420 bucket->bucket_id);
1421 }
1422 *usable_protocols &= OFPUTIL_P_OF15_UP;
1423 } else if (!strcasecmp(key, "action") || !strcasecmp(key, "actions")) {
1424 ds_put_format(&actions, "%s,", value);
1425 } else {
1426 ds_put_format(&actions, "%s(%s),", key, value);
1427 }
1428
1429 if (error) {
1430 ds_destroy(&actions);
1431 return error;
1432 }
1433 }
1434
1435 if (!actions.length) {
1436 return xstrdup("bucket must specify actions");
1437 }
1438 ds_chomp(&actions, ',');
1439
1440 ofpbuf_init(&ofpacts, 0);
1441 error = ofpacts_parse_actions(ds_cstr(&actions), &ofpacts,
1442 usable_protocols);
1443 ds_destroy(&actions);
1444 if (error) {
1445 ofpbuf_uninit(&ofpacts);
1446 return error;
1447 }
1448 bucket->ofpacts = ofpacts.data;
1449 bucket->ofpacts_len = ofpacts.size;
1450
1451 return NULL;
1452 }
1453
1454 static char * OVS_WARN_UNUSED_RESULT
1455 parse_select_group_field(char *s, struct field_array *fa,
1456 enum ofputil_protocol *usable_protocols)
1457 {
1458 char *name, *value_str;
1459
1460 while (ofputil_parse_key_value(&s, &name, &value_str)) {
1461 const struct mf_field *mf = mf_from_name(name);
1462
1463 if (mf) {
1464 char *error;
1465 union mf_value value;
1466
1467 if (bitmap_is_set(fa->used.bm, mf->id)) {
1468 return xasprintf("%s: duplicate field", name);
1469 }
1470
1471 if (*value_str) {
1472 error = mf_parse_value(mf, value_str, &value);
1473 if (error) {
1474 return error;
1475 }
1476
1477 /* The mask cannot be all-zeros */
1478 if (!mf_is_tun_metadata(mf) &&
1479 is_all_zeros(&value, mf->n_bytes)) {
1480 return xasprintf("%s: values are wildcards here "
1481 "and must not be all-zeros", s);
1482 }
1483
1484 /* The values parsed are masks for fields used
1485 * by the selection method */
1486 if (!mf_is_mask_valid(mf, &value)) {
1487 return xasprintf("%s: invalid mask for field %s",
1488 value_str, mf->name);
1489 }
1490 } else {
1491 memset(&value, 0xff, mf->n_bytes);
1492 }
1493
1494 field_array_set(mf->id, &value, fa);
1495
1496 if (is_all_ones(&value, mf->n_bytes)) {
1497 *usable_protocols &= mf->usable_protocols_exact;
1498 } else if (mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1499 || ip_is_cidr(value.be32)) {
1500 *usable_protocols &= mf->usable_protocols_cidr;
1501 } else {
1502 *usable_protocols &= mf->usable_protocols_bitwise;
1503 }
1504 } else {
1505 return xasprintf("%s: unknown field %s", s, name);
1506 }
1507 }
1508
1509 return NULL;
1510 }
1511
1512 static char * OVS_WARN_UNUSED_RESULT
1513 parse_ofp_group_mod_str__(struct ofputil_group_mod *gm, int command,
1514 char *string,
1515 enum ofputil_protocol *usable_protocols)
1516 {
1517 enum {
1518 F_GROUP_TYPE = 1 << 0,
1519 F_BUCKETS = 1 << 1,
1520 F_COMMAND_BUCKET_ID = 1 << 2,
1521 F_COMMAND_BUCKET_ID_ALL = 1 << 3,
1522 } fields;
1523 bool had_type = false;
1524 bool had_command_bucket_id = false;
1525 struct ofputil_bucket *bucket;
1526 char *error = NULL;
1527
1528 *usable_protocols = OFPUTIL_P_OF11_UP;
1529
1530 if (command == -2) {
1531 size_t len;
1532
1533 string += strspn(string, " \t\r\n"); /* Skip white space. */
1534 len = strcspn(string, ", \t\r\n"); /* Get length of the first token. */
1535
1536 if (!strncmp(string, "add", len)) {
1537 command = OFPGC11_ADD;
1538 } else if (!strncmp(string, "delete", len)) {
1539 command = OFPGC11_DELETE;
1540 } else if (!strncmp(string, "modify", len)) {
1541 command = OFPGC11_MODIFY;
1542 } else if (!strncmp(string, "add_or_mod", len)) {
1543 command = OFPGC11_ADD_OR_MOD;
1544 } else if (!strncmp(string, "insert_bucket", len)) {
1545 command = OFPGC15_INSERT_BUCKET;
1546 } else if (!strncmp(string, "remove_bucket", len)) {
1547 command = OFPGC15_REMOVE_BUCKET;
1548 } else {
1549 len = 0;
1550 command = OFPGC11_ADD;
1551 }
1552 string += len;
1553 }
1554
1555 switch (command) {
1556 case OFPGC11_ADD:
1557 fields = F_GROUP_TYPE | F_BUCKETS;
1558 break;
1559
1560 case OFPGC11_DELETE:
1561 fields = 0;
1562 break;
1563
1564 case OFPGC11_MODIFY:
1565 fields = F_GROUP_TYPE | F_BUCKETS;
1566 break;
1567
1568 case OFPGC11_ADD_OR_MOD:
1569 fields = F_GROUP_TYPE | F_BUCKETS;
1570 break;
1571
1572 case OFPGC15_INSERT_BUCKET:
1573 fields = F_BUCKETS | F_COMMAND_BUCKET_ID;
1574 *usable_protocols &= OFPUTIL_P_OF15_UP;
1575 break;
1576
1577 case OFPGC15_REMOVE_BUCKET:
1578 fields = F_COMMAND_BUCKET_ID | F_COMMAND_BUCKET_ID_ALL;
1579 *usable_protocols &= OFPUTIL_P_OF15_UP;
1580 break;
1581
1582 default:
1583 OVS_NOT_REACHED();
1584 }
1585
1586 memset(gm, 0, sizeof *gm);
1587 gm->command = command;
1588 gm->group_id = OFPG_ANY;
1589 gm->command_bucket_id = OFPG15_BUCKET_ALL;
1590 ovs_list_init(&gm->buckets);
1591 if (command == OFPGC11_DELETE && string[0] == '\0') {
1592 gm->group_id = OFPG_ALL;
1593 return NULL;
1594 }
1595
1596 *usable_protocols = OFPUTIL_P_OF11_UP;
1597
1598 /* Strip the buckets off the end of 'string', if there are any, saving a
1599 * pointer for later. We want to parse the buckets last because the bucket
1600 * type influences bucket defaults. */
1601 char *bkt_str = strstr(string, "bucket=");
1602 if (bkt_str) {
1603 if (!(fields & F_BUCKETS)) {
1604 error = xstrdup("bucket is not needed");
1605 goto out;
1606 }
1607 *bkt_str = '\0';
1608 }
1609
1610 /* Parse everything before the buckets. */
1611 char *pos = string;
1612 char *name, *value;
1613 while (ofputil_parse_key_value(&pos, &name, &value)) {
1614 if (!strcmp(name, "command_bucket_id")) {
1615 if (!(fields & F_COMMAND_BUCKET_ID)) {
1616 error = xstrdup("command bucket id is not needed");
1617 goto out;
1618 }
1619 if (!strcmp(value, "all")) {
1620 gm->command_bucket_id = OFPG15_BUCKET_ALL;
1621 } else if (!strcmp(value, "first")) {
1622 gm->command_bucket_id = OFPG15_BUCKET_FIRST;
1623 } else if (!strcmp(value, "last")) {
1624 gm->command_bucket_id = OFPG15_BUCKET_LAST;
1625 } else {
1626 error = str_to_u32(value, &gm->command_bucket_id);
1627 if (error) {
1628 goto out;
1629 }
1630 if (gm->command_bucket_id > OFPG15_BUCKET_MAX
1631 && (gm->command_bucket_id != OFPG15_BUCKET_FIRST
1632 && gm->command_bucket_id != OFPG15_BUCKET_LAST
1633 && gm->command_bucket_id != OFPG15_BUCKET_ALL)) {
1634 error = xasprintf("invalid command bucket id %"PRIu32,
1635 gm->command_bucket_id);
1636 goto out;
1637 }
1638 }
1639 if (gm->command_bucket_id == OFPG15_BUCKET_ALL
1640 && !(fields & F_COMMAND_BUCKET_ID_ALL)) {
1641 error = xstrdup("command_bucket_id=all is not permitted");
1642 goto out;
1643 }
1644 had_command_bucket_id = true;
1645 } else if (!strcmp(name, "group_id")) {
1646 if(!strcmp(value, "all")) {
1647 gm->group_id = OFPG_ALL;
1648 } else {
1649 error = str_to_u32(value, &gm->group_id);
1650 if (error) {
1651 goto out;
1652 }
1653 if (gm->group_id != OFPG_ALL && gm->group_id > OFPG_MAX) {
1654 error = xasprintf("invalid group id %"PRIu32,
1655 gm->group_id);
1656 goto out;
1657 }
1658 }
1659 } else if (!strcmp(name, "type")){
1660 if (!(fields & F_GROUP_TYPE)) {
1661 error = xstrdup("type is not needed");
1662 goto out;
1663 }
1664 if (!strcmp(value, "all")) {
1665 gm->type = OFPGT11_ALL;
1666 } else if (!strcmp(value, "select")) {
1667 gm->type = OFPGT11_SELECT;
1668 } else if (!strcmp(value, "indirect")) {
1669 gm->type = OFPGT11_INDIRECT;
1670 } else if (!strcmp(value, "ff") ||
1671 !strcmp(value, "fast_failover")) {
1672 gm->type = OFPGT11_FF;
1673 } else {
1674 error = xasprintf("invalid group type %s", value);
1675 goto out;
1676 }
1677 had_type = true;
1678 } else if (!strcmp(name, "selection_method")) {
1679 if (!(fields & F_GROUP_TYPE)) {
1680 error = xstrdup("selection method is not needed");
1681 goto out;
1682 }
1683 if (strlen(value) >= NTR_MAX_SELECTION_METHOD_LEN) {
1684 error = xasprintf("selection method is longer than %u"
1685 " bytes long",
1686 NTR_MAX_SELECTION_METHOD_LEN - 1);
1687 goto out;
1688 }
1689 memset(gm->props.selection_method, '\0',
1690 NTR_MAX_SELECTION_METHOD_LEN);
1691 strcpy(gm->props.selection_method, value);
1692 *usable_protocols &= OFPUTIL_P_OF15_UP;
1693 } else if (!strcmp(name, "selection_method_param")) {
1694 if (!(fields & F_GROUP_TYPE)) {
1695 error = xstrdup("selection method param is not needed");
1696 goto out;
1697 }
1698 error = str_to_u64(value, &gm->props.selection_method_param);
1699 if (error) {
1700 goto out;
1701 }
1702 *usable_protocols &= OFPUTIL_P_OF15_UP;
1703 } else if (!strcmp(name, "fields")) {
1704 if (!(fields & F_GROUP_TYPE)) {
1705 error = xstrdup("fields are not needed");
1706 goto out;
1707 }
1708 error = parse_select_group_field(value, &gm->props.fields,
1709 usable_protocols);
1710 if (error) {
1711 goto out;
1712 }
1713 *usable_protocols &= OFPUTIL_P_OF15_UP;
1714 } else {
1715 error = xasprintf("unknown keyword %s", name);
1716 goto out;
1717 }
1718 }
1719 if (gm->group_id == OFPG_ANY) {
1720 error = xstrdup("must specify a group_id");
1721 goto out;
1722 }
1723 if (fields & F_GROUP_TYPE && !had_type) {
1724 error = xstrdup("must specify a type");
1725 goto out;
1726 }
1727
1728 /* Exclude fields for non "hash" selection method. */
1729 if (strcmp(gm->props.selection_method, "hash") &&
1730 gm->props.fields.values_size) {
1731 error = xstrdup("fields may only be specified with \"selection_method=hash\"");
1732 goto out;
1733 }
1734 /* Exclude selection_method_param if no selection_method is given. */
1735 if (gm->props.selection_method[0] == 0
1736 && gm->props.selection_method_param != 0) {
1737 error = xstrdup("selection_method_param is only allowed with \"selection_method\"");
1738 goto out;
1739 }
1740 if (fields & F_COMMAND_BUCKET_ID) {
1741 if (!(fields & F_COMMAND_BUCKET_ID_ALL || had_command_bucket_id)) {
1742 error = xstrdup("must specify a command bucket id");
1743 goto out;
1744 }
1745 } else if (had_command_bucket_id) {
1746 error = xstrdup("command bucket id is not needed");
1747 goto out;
1748 }
1749
1750 /* Now parse the buckets, if any. */
1751 while (bkt_str) {
1752 char *next_bkt_str;
1753
1754 bkt_str = strchr(bkt_str + 1, '=');
1755 if (!bkt_str) {
1756 error = xstrdup("must specify bucket content");
1757 goto out;
1758 }
1759 bkt_str++;
1760
1761 next_bkt_str = strstr(bkt_str, "bucket=");
1762 if (next_bkt_str) {
1763 *next_bkt_str = '\0';
1764 }
1765
1766 bucket = xzalloc(sizeof(struct ofputil_bucket));
1767 error = parse_bucket_str(bucket, bkt_str, gm->type, usable_protocols);
1768 if (error) {
1769 free(bucket);
1770 goto out;
1771 }
1772 ovs_list_push_back(&gm->buckets, &bucket->list_node);
1773
1774 if (gm->type != OFPGT11_SELECT && bucket->weight) {
1775 error = xstrdup("Only select groups can have bucket weights.");
1776 goto out;
1777 }
1778
1779 bkt_str = next_bkt_str;
1780 }
1781 if (gm->type == OFPGT11_INDIRECT && !ovs_list_is_short(&gm->buckets)) {
1782 error = xstrdup("Indirect groups can have at most one bucket.");
1783 goto out;
1784 }
1785
1786 return NULL;
1787 out:
1788 ofputil_uninit_group_mod(gm);
1789 return error;
1790 }
1791
1792 /* If 'command' is given as -2, each line may start with a command name ("add",
1793 * "modify", "add_or_mod", "delete", "insert_bucket", or "remove_bucket"). A
1794 * missing command name is treated as "add".
1795 */
1796 char * OVS_WARN_UNUSED_RESULT
1797 parse_ofp_group_mod_str(struct ofputil_group_mod *gm, int command,
1798 const char *str_,
1799 enum ofputil_protocol *usable_protocols)
1800 {
1801 char *string = xstrdup(str_);
1802 char *error = parse_ofp_group_mod_str__(gm, command, string,
1803 usable_protocols);
1804 free(string);
1805 return error;
1806 }
1807
1808 /* If 'command' is given as -2, each line may start with a command name ("add",
1809 * "modify", "add_or_mod", "delete", "insert_bucket", or "remove_bucket"). A
1810 * missing command name is treated as "add".
1811 */
1812 char * OVS_WARN_UNUSED_RESULT
1813 parse_ofp_group_mod_file(const char *file_name, int command,
1814 struct ofputil_group_mod **gms, size_t *n_gms,
1815 enum ofputil_protocol *usable_protocols)
1816 {
1817 size_t allocated_gms;
1818 int line_number;
1819 FILE *stream;
1820 struct ds s;
1821
1822 *gms = NULL;
1823 *n_gms = 0;
1824
1825 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1826 if (stream == NULL) {
1827 return xasprintf("%s: open failed (%s)",
1828 file_name, ovs_strerror(errno));
1829 }
1830
1831 allocated_gms = *n_gms;
1832 ds_init(&s);
1833 line_number = 0;
1834 *usable_protocols = OFPUTIL_P_OF11_UP;
1835 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
1836 enum ofputil_protocol usable;
1837 char *error;
1838
1839 if (*n_gms >= allocated_gms) {
1840 struct ofputil_group_mod *new_gms;
1841 size_t i;
1842
1843 new_gms = x2nrealloc(*gms, &allocated_gms, sizeof **gms);
1844 for (i = 0; i < *n_gms; i++) {
1845 ovs_list_moved(&new_gms[i].buckets, &(*gms)[i].buckets);
1846 }
1847 *gms = new_gms;
1848 }
1849 error = parse_ofp_group_mod_str(&(*gms)[*n_gms], command, ds_cstr(&s),
1850 &usable);
1851 if (error) {
1852 size_t i;
1853
1854 for (i = 0; i < *n_gms; i++) {
1855 ofputil_uninit_group_mod(&(*gms)[i]);
1856 }
1857 free(*gms);
1858 *gms = NULL;
1859 *n_gms = 0;
1860
1861 ds_destroy(&s);
1862 if (stream != stdin) {
1863 fclose(stream);
1864 }
1865
1866 return xasprintf("%s:%d: %s", file_name, line_number, error);
1867 }
1868 *usable_protocols &= usable;
1869 *n_gms += 1;
1870 }
1871
1872 ds_destroy(&s);
1873 if (stream != stdin) {
1874 fclose(stream);
1875 }
1876 return NULL;
1877 }
1878
1879 /* Opens file 'file_name' and reads each line as a flow_mod or a group_mod,
1880 * depending on the first keyword on each line. Stores each flow and group
1881 * mods in '*bms', an array allocated on the caller's behalf, and the number of
1882 * messages in '*n_bms'.
1883 *
1884 * Returns NULL if successful, otherwise a malloc()'d string describing the
1885 * error. The caller is responsible for freeing the returned string. */
1886 char * OVS_WARN_UNUSED_RESULT
1887 parse_ofp_bundle_file(const char *file_name,
1888 struct ofputil_bundle_msg **bms, size_t *n_bms,
1889 enum ofputil_protocol *usable_protocols)
1890 {
1891 size_t allocated_bms;
1892 char *error = NULL;
1893 int line_number;
1894 FILE *stream;
1895 struct ds ds;
1896
1897 *usable_protocols = OFPUTIL_P_ANY;
1898
1899 *bms = NULL;
1900 *n_bms = 0;
1901
1902 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1903 if (stream == NULL) {
1904 return xasprintf("%s: open failed (%s)",
1905 file_name, ovs_strerror(errno));
1906 }
1907
1908 allocated_bms = *n_bms;
1909 ds_init(&ds);
1910 line_number = 0;
1911 while (!ds_get_preprocessed_line(&ds, stream, &line_number)) {
1912 enum ofputil_protocol usable;
1913 char *s = ds_cstr(&ds);
1914 size_t len;
1915
1916 if (*n_bms >= allocated_bms) {
1917 struct ofputil_bundle_msg *new_bms;
1918
1919 new_bms = x2nrealloc(*bms, &allocated_bms, sizeof **bms);
1920 for (size_t i = 0; i < *n_bms; i++) {
1921 if (new_bms[i].type == OFPTYPE_GROUP_MOD) {
1922 ovs_list_moved(&new_bms[i].gm.buckets,
1923 &(*bms)[i].gm.buckets);
1924 }
1925 }
1926 *bms = new_bms;
1927 }
1928
1929 s += strspn(s, " \t\r\n"); /* Skip white space. */
1930 len = strcspn(s, ", \t\r\n"); /* Get length of the first token. */
1931
1932 if (!strncmp(s, "flow", len)) {
1933 s += len;
1934 error = parse_ofp_flow_mod_str(&(*bms)[*n_bms].fm, s, -2, &usable);
1935 if (error) {
1936 break;
1937 }
1938 (*bms)[*n_bms].type = OFPTYPE_FLOW_MOD;
1939 } else if (!strncmp(s, "group", len)) {
1940 s += len;
1941 error = parse_ofp_group_mod_str(&(*bms)[*n_bms].gm, -2, s,
1942 &usable);
1943 if (error) {
1944 break;
1945 }
1946 (*bms)[*n_bms].type = OFPTYPE_GROUP_MOD;
1947 } else if (!strncmp(s, "packet-out", len)) {
1948 s += len;
1949 error = parse_ofp_packet_out_str(&(*bms)[*n_bms].po, s, &usable);
1950 if (error) {
1951 break;
1952 }
1953 (*bms)[*n_bms].type = OFPTYPE_PACKET_OUT;
1954 } else {
1955 error = xasprintf("Unsupported bundle message type: %.*s",
1956 (int)len, s);
1957 break;
1958 }
1959
1960 *usable_protocols &= usable; /* Each line can narrow the set. */
1961 *n_bms += 1;
1962 }
1963
1964 ds_destroy(&ds);
1965 if (stream != stdin) {
1966 fclose(stream);
1967 }
1968
1969 if (error) {
1970 char *err_msg = xasprintf("%s:%d: %s", file_name, line_number, error);
1971 free(error);
1972
1973 ofputil_free_bundle_msgs(*bms, *n_bms);
1974 *bms = NULL;
1975 *n_bms = 0;
1976 return err_msg;
1977 }
1978 return NULL;
1979 }
1980
1981 char * OVS_WARN_UNUSED_RESULT
1982 parse_ofp_tlv_table_mod_str(struct ofputil_tlv_table_mod *ttm,
1983 uint16_t command, const char *s,
1984 enum ofputil_protocol *usable_protocols)
1985 {
1986 *usable_protocols = OFPUTIL_P_NXM_OXM_ANY;
1987
1988 ttm->command = command;
1989 ovs_list_init(&ttm->mappings);
1990
1991 while (*s) {
1992 struct ofputil_tlv_map *map = xmalloc(sizeof *map);
1993 int n;
1994
1995 if (*s == ',') {
1996 s++;
1997 }
1998
1999 ovs_list_push_back(&ttm->mappings, &map->list_node);
2000
2001 if (!ovs_scan(s, "{class=%"SCNi16",type=%"SCNi8",len=%"SCNi8"}->tun_metadata%"SCNi16"%n",
2002 &map->option_class, &map->option_type, &map->option_len,
2003 &map->index, &n)) {
2004 ofputil_uninit_tlv_table(&ttm->mappings);
2005 return xstrdup("invalid tlv mapping");
2006 }
2007
2008 s += n;
2009 }
2010
2011 return NULL;
2012 }