]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ofp-parse.c
lib: Move compiler.h to <openvswitch/compiler.h>
[mirror_ovs.git] / lib / ofp-parse.c
1 /*
2 * Copyright (c) 2010, 2011, 2012, 2013, 2014 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 "ofp-parse.h"
20
21 #include <ctype.h>
22 #include <errno.h>
23 #include <stdlib.h>
24
25 #include "bundle.h"
26 #include "byte-order.h"
27 #include "dynamic-string.h"
28 #include "learn.h"
29 #include "meta-flow.h"
30 #include "multipath.h"
31 #include "netdev.h"
32 #include "nx-match.h"
33 #include "ofp-actions.h"
34 #include "ofp-util.h"
35 #include "ofpbuf.h"
36 #include "openflow/openflow.h"
37 #include "ovs-thread.h"
38 #include "packets.h"
39 #include "simap.h"
40 #include "socket-util.h"
41 #include "vconn.h"
42
43 /* Parses 'str' as an 8-bit unsigned integer into '*valuep'.
44 *
45 * 'name' describes the value parsed in an error message, if any.
46 *
47 * Returns NULL if successful, otherwise a malloc()'d string describing the
48 * error. The caller is responsible for freeing the returned string. */
49 char * OVS_WARN_UNUSED_RESULT
50 str_to_u8(const char *str, const char *name, uint8_t *valuep)
51 {
52 int value;
53
54 if (!str_to_int(str, 0, &value) || value < 0 || value > 255) {
55 return xasprintf("invalid %s \"%s\"", name, str);
56 }
57 *valuep = value;
58 return NULL;
59 }
60
61 /* Parses 'str' as a 16-bit unsigned integer into '*valuep'.
62 *
63 * 'name' describes the value parsed in an error message, if any.
64 *
65 * Returns NULL if successful, otherwise a malloc()'d string describing the
66 * error. The caller is responsible for freeing the returned string. */
67 char * OVS_WARN_UNUSED_RESULT
68 str_to_u16(const char *str, const char *name, uint16_t *valuep)
69 {
70 int value;
71
72 if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) {
73 return xasprintf("invalid %s \"%s\"", name, str);
74 }
75 *valuep = value;
76 return NULL;
77 }
78
79 /* Parses 'str' as a 32-bit unsigned integer into '*valuep'.
80 *
81 * Returns NULL if successful, otherwise a malloc()'d string describing the
82 * error. The caller is responsible for freeing the returned string. */
83 char * OVS_WARN_UNUSED_RESULT
84 str_to_u32(const char *str, uint32_t *valuep)
85 {
86 char *tail;
87 uint32_t value;
88
89 if (!str[0]) {
90 return xstrdup("missing required numeric argument");
91 }
92
93 errno = 0;
94 value = strtoul(str, &tail, 0);
95 if (errno == EINVAL || errno == ERANGE || *tail) {
96 return xasprintf("invalid numeric format %s", str);
97 }
98 *valuep = value;
99 return NULL;
100 }
101
102 /* Parses 'str' as an 64-bit unsigned integer into '*valuep'.
103 *
104 * Returns NULL if successful, otherwise a malloc()'d string describing the
105 * error. The caller is responsible for freeing the returned string. */
106 char * OVS_WARN_UNUSED_RESULT
107 str_to_u64(const char *str, uint64_t *valuep)
108 {
109 char *tail;
110 uint64_t value;
111
112 if (!str[0]) {
113 return xstrdup("missing required numeric argument");
114 }
115
116 errno = 0;
117 value = strtoull(str, &tail, 0);
118 if (errno == EINVAL || errno == ERANGE || *tail) {
119 return xasprintf("invalid numeric format %s", str);
120 }
121 *valuep = value;
122 return NULL;
123 }
124
125 /* Parses 'str' as an 64-bit unsigned integer in network byte order into
126 * '*valuep'.
127 *
128 * Returns NULL if successful, otherwise a malloc()'d string describing the
129 * error. The caller is responsible for freeing the returned string. */
130 char * OVS_WARN_UNUSED_RESULT
131 str_to_be64(const char *str, ovs_be64 *valuep)
132 {
133 uint64_t value = 0;
134 char *error;
135
136 error = str_to_u64(str, &value);
137 if (!error) {
138 *valuep = htonll(value);
139 }
140 return error;
141 }
142
143 /* Parses 'str' as an Ethernet address into 'mac'.
144 *
145 * Returns NULL if successful, otherwise a malloc()'d string describing the
146 * error. The caller is responsible for freeing the returned string. */
147 char * OVS_WARN_UNUSED_RESULT
148 str_to_mac(const char *str, uint8_t mac[ETH_ADDR_LEN])
149 {
150 if (!ovs_scan(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))) {
151 return xasprintf("invalid mac address %s", str);
152 }
153 return NULL;
154 }
155
156 /* Parses 'str' as an IP address into '*ip'.
157 *
158 * Returns NULL if successful, otherwise a malloc()'d string describing the
159 * error. The caller is responsible for freeing the returned string. */
160 char * OVS_WARN_UNUSED_RESULT
161 str_to_ip(const char *str, ovs_be32 *ip)
162 {
163 struct in_addr in_addr;
164
165 if (lookup_ip(str, &in_addr)) {
166 return xasprintf("%s: could not convert to IP address", str);
167 }
168 *ip = in_addr.s_addr;
169 return NULL;
170 }
171
172 struct protocol {
173 const char *name;
174 uint16_t dl_type;
175 uint8_t nw_proto;
176 };
177
178 static bool
179 parse_protocol(const char *name, const struct protocol **p_out)
180 {
181 static const struct protocol protocols[] = {
182 { "ip", ETH_TYPE_IP, 0 },
183 { "arp", ETH_TYPE_ARP, 0 },
184 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
185 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
186 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
187 { "sctp", ETH_TYPE_IP, IPPROTO_SCTP },
188 { "ipv6", ETH_TYPE_IPV6, 0 },
189 { "ip6", ETH_TYPE_IPV6, 0 },
190 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
191 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
192 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
193 { "sctp6", ETH_TYPE_IPV6, IPPROTO_SCTP },
194 { "rarp", ETH_TYPE_RARP, 0},
195 { "mpls", ETH_TYPE_MPLS, 0 },
196 { "mplsm", ETH_TYPE_MPLS_MCAST, 0 },
197 };
198 const struct protocol *p;
199
200 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
201 if (!strcmp(p->name, name)) {
202 *p_out = p;
203 return true;
204 }
205 }
206 *p_out = NULL;
207 return false;
208 }
209
210 /* Parses 's' as the (possibly masked) value of field 'mf', and updates
211 * 'match' appropriately. Restricts the set of usable protocols to ones
212 * supporting the parsed field.
213 *
214 * Returns NULL if successful, otherwise a malloc()'d string describing the
215 * error. The caller is responsible for freeing the returned string. */
216 static char * OVS_WARN_UNUSED_RESULT
217 parse_field(const struct mf_field *mf, const char *s, struct match *match,
218 enum ofputil_protocol *usable_protocols)
219 {
220 union mf_value value, mask;
221 char *error;
222
223 error = mf_parse(mf, s, &value, &mask);
224 if (!error) {
225 *usable_protocols &= mf_set(mf, &value, &mask, match);
226 }
227 return error;
228 }
229
230 static char *
231 extract_actions(char *s)
232 {
233 s = strstr(s, "action");
234 if (s) {
235 *s = '\0';
236 s = strchr(s + 1, '=');
237 return s ? s + 1 : NULL;
238 } else {
239 return NULL;
240 }
241 }
242
243
244 static char * OVS_WARN_UNUSED_RESULT
245 parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string,
246 enum ofputil_protocol *usable_protocols)
247 {
248 enum {
249 F_OUT_PORT = 1 << 0,
250 F_ACTIONS = 1 << 1,
251 F_IMPORTANCE = 1 << 2,
252 F_TIMEOUT = 1 << 3,
253 F_PRIORITY = 1 << 4,
254 F_FLAGS = 1 << 5,
255 } fields;
256 char *save_ptr = NULL;
257 char *act_str = NULL;
258 char *name;
259
260 *usable_protocols = OFPUTIL_P_ANY;
261
262 switch (command) {
263 case -1:
264 fields = F_OUT_PORT;
265 break;
266
267 case OFPFC_ADD:
268 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS | F_IMPORTANCE;
269 break;
270
271 case OFPFC_DELETE:
272 fields = F_OUT_PORT;
273 break;
274
275 case OFPFC_DELETE_STRICT:
276 fields = F_OUT_PORT | F_PRIORITY;
277 break;
278
279 case OFPFC_MODIFY:
280 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
281 break;
282
283 case OFPFC_MODIFY_STRICT:
284 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
285 break;
286
287 default:
288 OVS_NOT_REACHED();
289 }
290
291 match_init_catchall(&fm->match);
292 fm->priority = OFP_DEFAULT_PRIORITY;
293 fm->cookie = htonll(0);
294 fm->cookie_mask = htonll(0);
295 if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
296 /* For modify, by default, don't update the cookie. */
297 fm->new_cookie = OVS_BE64_MAX;
298 } else{
299 fm->new_cookie = htonll(0);
300 }
301 fm->modify_cookie = false;
302 fm->table_id = 0xff;
303 fm->command = command;
304 fm->idle_timeout = OFP_FLOW_PERMANENT;
305 fm->hard_timeout = OFP_FLOW_PERMANENT;
306 fm->buffer_id = UINT32_MAX;
307 fm->out_port = OFPP_ANY;
308 fm->flags = 0;
309 fm->importance = 0;
310 fm->out_group = OFPG11_ANY;
311 fm->delete_reason = OFPRR_DELETE;
312 if (fields & F_ACTIONS) {
313 act_str = extract_actions(string);
314 if (!act_str) {
315 return xstrdup("must specify an action");
316 }
317 }
318 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
319 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
320 const struct protocol *p;
321 char *error = NULL;
322
323 if (parse_protocol(name, &p)) {
324 match_set_dl_type(&fm->match, htons(p->dl_type));
325 if (p->nw_proto) {
326 match_set_nw_proto(&fm->match, p->nw_proto);
327 }
328 } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
329 fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
330 } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
331 fm->flags |= OFPUTIL_FF_CHECK_OVERLAP;
332 } else if (fields & F_FLAGS && !strcmp(name, "reset_counts")) {
333 fm->flags |= OFPUTIL_FF_RESET_COUNTS;
334 *usable_protocols &= OFPUTIL_P_OF12_UP;
335 } else if (fields & F_FLAGS && !strcmp(name, "no_packet_counts")) {
336 fm->flags |= OFPUTIL_FF_NO_PKT_COUNTS;
337 *usable_protocols &= OFPUTIL_P_OF13_UP;
338 } else if (fields & F_FLAGS && !strcmp(name, "no_byte_counts")) {
339 fm->flags |= OFPUTIL_FF_NO_BYT_COUNTS;
340 *usable_protocols &= OFPUTIL_P_OF13_UP;
341 } else if (!strcmp(name, "no_readonly_table")
342 || !strcmp(name, "allow_hidden_fields")) {
343 /* ignore these fields. */
344 } else {
345 char *value;
346
347 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
348 if (!value) {
349 return xasprintf("field %s missing value", name);
350 }
351
352 if (!strcmp(name, "table")) {
353 error = str_to_u8(value, "table", &fm->table_id);
354 if (fm->table_id != 0xff) {
355 *usable_protocols &= OFPUTIL_P_TID;
356 }
357 } else if (!strcmp(name, "out_port")) {
358 if (!ofputil_port_from_string(value, &fm->out_port)) {
359 error = xasprintf("%s is not a valid OpenFlow port",
360 value);
361 }
362 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
363 uint16_t priority = 0;
364
365 error = str_to_u16(value, name, &priority);
366 fm->priority = priority;
367 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
368 error = str_to_u16(value, name, &fm->idle_timeout);
369 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
370 error = str_to_u16(value, name, &fm->hard_timeout);
371 } else if (fields & F_IMPORTANCE && !strcmp(name, "importance")) {
372 error = str_to_u16(value, name, &fm->importance);
373 } else if (!strcmp(name, "cookie")) {
374 char *mask = strchr(value, '/');
375
376 if (mask) {
377 /* A mask means we're searching for a cookie. */
378 if (command == OFPFC_ADD) {
379 return xstrdup("flow additions cannot use "
380 "a cookie mask");
381 }
382 *mask = '\0';
383 error = str_to_be64(value, &fm->cookie);
384 if (error) {
385 return error;
386 }
387 error = str_to_be64(mask + 1, &fm->cookie_mask);
388
389 /* Matching of the cookie is only supported through NXM or
390 * OF1.1+. */
391 if (fm->cookie_mask != htonll(0)) {
392 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
393 }
394 } else {
395 /* No mask means that the cookie is being set. */
396 if (command != OFPFC_ADD && command != OFPFC_MODIFY
397 && command != OFPFC_MODIFY_STRICT) {
398 return xstrdup("cannot set cookie");
399 }
400 error = str_to_be64(value, &fm->new_cookie);
401 fm->modify_cookie = true;
402 }
403 } else if (mf_from_name(name)) {
404 error = parse_field(mf_from_name(name), value, &fm->match,
405 usable_protocols);
406 } else if (!strcmp(name, "duration")
407 || !strcmp(name, "n_packets")
408 || !strcmp(name, "n_bytes")
409 || !strcmp(name, "idle_age")
410 || !strcmp(name, "hard_age")) {
411 /* Ignore these, so that users can feed the output of
412 * "ovs-ofctl dump-flows" back into commands that parse
413 * flows. */
414 } else {
415 error = xasprintf("unknown keyword %s", name);
416 }
417
418 if (error) {
419 return error;
420 }
421 }
422 }
423 /* Check for usable protocol interdependencies between match fields. */
424 if (fm->match.flow.dl_type == htons(ETH_TYPE_IPV6)) {
425 const struct flow_wildcards *wc = &fm->match.wc;
426 /* Only NXM and OXM support matching L3 and L4 fields within IPv6.
427 *
428 * (IPv6 specific fields as well as arp_sha, arp_tha, nw_frag, and
429 * nw_ttl are covered elsewhere so they don't need to be included in
430 * this test too.)
431 */
432 if (wc->masks.nw_proto || wc->masks.nw_tos
433 || wc->masks.tp_src || wc->masks.tp_dst) {
434 *usable_protocols &= OFPUTIL_P_NXM_OXM_ANY;
435 }
436 }
437 if (!fm->cookie_mask && fm->new_cookie == OVS_BE64_MAX
438 && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
439 /* On modifies without a mask, we are supposed to add a flow if
440 * one does not exist. If a cookie wasn't been specified, use a
441 * default of zero. */
442 fm->new_cookie = htonll(0);
443 }
444 if (fields & F_ACTIONS) {
445 enum ofputil_protocol action_usable_protocols;
446 struct ofpbuf ofpacts;
447 char *error;
448
449 ofpbuf_init(&ofpacts, 32);
450 error = ofpacts_parse_instructions(act_str, &ofpacts,
451 &action_usable_protocols);
452 *usable_protocols &= action_usable_protocols;
453 if (!error) {
454 enum ofperr err;
455
456 err = ofpacts_check(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &fm->match.flow,
457 OFPP_MAX, fm->table_id, 255, usable_protocols);
458 if (!err && !usable_protocols) {
459 err = OFPERR_OFPBAC_MATCH_INCONSISTENT;
460 }
461 if (err) {
462 error = xasprintf("actions are invalid with specified match "
463 "(%s)", ofperr_to_string(err));
464 }
465
466 }
467 if (error) {
468 ofpbuf_uninit(&ofpacts);
469 return error;
470 }
471
472 fm->ofpacts_len = ofpbuf_size(&ofpacts);
473 fm->ofpacts = ofpbuf_steal_data(&ofpacts);
474 } else {
475 fm->ofpacts_len = 0;
476 fm->ofpacts = NULL;
477 }
478
479 return NULL;
480 }
481
482 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
483 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
484 * Returns the set of usable protocols in '*usable_protocols'.
485 *
486 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
487 * constant for 'command'. To parse syntax for an OFPST_FLOW or
488 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'.
489 *
490 * Returns NULL if successful, otherwise a malloc()'d string describing the
491 * error. The caller is responsible for freeing the returned string. */
492 char * OVS_WARN_UNUSED_RESULT
493 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
494 enum ofputil_protocol *usable_protocols)
495 {
496 char *string = xstrdup(str_);
497 char *error;
498
499 error = parse_ofp_str__(fm, command, string, usable_protocols);
500 if (error) {
501 fm->ofpacts = NULL;
502 fm->ofpacts_len = 0;
503 }
504
505 free(string);
506 return error;
507 }
508
509 static char * OVS_WARN_UNUSED_RESULT
510 parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string,
511 struct ofpbuf *bands, int command,
512 enum ofputil_protocol *usable_protocols)
513 {
514 enum {
515 F_METER = 1 << 0,
516 F_FLAGS = 1 << 1,
517 F_BANDS = 1 << 2,
518 } fields;
519 char *save_ptr = NULL;
520 char *band_str = NULL;
521 char *name;
522
523 /* Meters require at least OF 1.3. */
524 *usable_protocols = OFPUTIL_P_OF13_UP;
525
526 switch (command) {
527 case -1:
528 fields = F_METER;
529 break;
530
531 case OFPMC13_ADD:
532 fields = F_METER | F_FLAGS | F_BANDS;
533 break;
534
535 case OFPMC13_DELETE:
536 fields = F_METER;
537 break;
538
539 case OFPMC13_MODIFY:
540 fields = F_METER | F_FLAGS | F_BANDS;
541 break;
542
543 default:
544 OVS_NOT_REACHED();
545 }
546
547 mm->command = command;
548 mm->meter.meter_id = 0;
549 mm->meter.flags = 0;
550 if (fields & F_BANDS) {
551 band_str = strstr(string, "band");
552 if (!band_str) {
553 return xstrdup("must specify bands");
554 }
555 *band_str = '\0';
556
557 band_str = strchr(band_str + 1, '=');
558 if (!band_str) {
559 return xstrdup("must specify bands");
560 }
561
562 band_str++;
563 }
564 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
565 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
566
567 if (fields & F_FLAGS && !strcmp(name, "kbps")) {
568 mm->meter.flags |= OFPMF13_KBPS;
569 } else if (fields & F_FLAGS && !strcmp(name, "pktps")) {
570 mm->meter.flags |= OFPMF13_PKTPS;
571 } else if (fields & F_FLAGS && !strcmp(name, "burst")) {
572 mm->meter.flags |= OFPMF13_BURST;
573 } else if (fields & F_FLAGS && !strcmp(name, "stats")) {
574 mm->meter.flags |= OFPMF13_STATS;
575 } else {
576 char *value;
577
578 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
579 if (!value) {
580 return xasprintf("field %s missing value", name);
581 }
582
583 if (!strcmp(name, "meter")) {
584 if (!strcmp(value, "all")) {
585 mm->meter.meter_id = OFPM13_ALL;
586 } else if (!strcmp(value, "controller")) {
587 mm->meter.meter_id = OFPM13_CONTROLLER;
588 } else if (!strcmp(value, "slowpath")) {
589 mm->meter.meter_id = OFPM13_SLOWPATH;
590 } else {
591 char *error = str_to_u32(value, &mm->meter.meter_id);
592 if (error) {
593 return error;
594 }
595 if (mm->meter.meter_id > OFPM13_MAX
596 || !mm->meter.meter_id) {
597 return xasprintf("invalid value for %s", name);
598 }
599 }
600 } else {
601 return xasprintf("unknown keyword %s", name);
602 }
603 }
604 }
605 if (fields & F_METER && !mm->meter.meter_id) {
606 return xstrdup("must specify 'meter'");
607 }
608 if (fields & F_FLAGS && !mm->meter.flags) {
609 return xstrdup("meter must specify either 'kbps' or 'pktps'");
610 }
611
612 if (fields & F_BANDS) {
613 uint16_t n_bands = 0;
614 struct ofputil_meter_band *band = NULL;
615 int i;
616
617 for (name = strtok_r(band_str, "=, \t\r\n", &save_ptr); name;
618 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
619
620 char *value;
621
622 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
623 if (!value) {
624 return xasprintf("field %s missing value", name);
625 }
626
627 if (!strcmp(name, "type")) {
628 /* Start a new band */
629 band = ofpbuf_put_zeros(bands, sizeof *band);
630 n_bands++;
631
632 if (!strcmp(value, "drop")) {
633 band->type = OFPMBT13_DROP;
634 } else if (!strcmp(value, "dscp_remark")) {
635 band->type = OFPMBT13_DSCP_REMARK;
636 } else {
637 return xasprintf("field %s unknown value %s", name, value);
638 }
639 } else if (!band || !band->type) {
640 return xstrdup("band must start with the 'type' keyword");
641 } else if (!strcmp(name, "rate")) {
642 char *error = str_to_u32(value, &band->rate);
643 if (error) {
644 return error;
645 }
646 } else if (!strcmp(name, "burst_size")) {
647 char *error = str_to_u32(value, &band->burst_size);
648 if (error) {
649 return error;
650 }
651 } else if (!strcmp(name, "prec_level")) {
652 char *error = str_to_u8(value, name, &band->prec_level);
653 if (error) {
654 return error;
655 }
656 } else {
657 return xasprintf("unknown keyword %s", name);
658 }
659 }
660 /* validate bands */
661 if (!n_bands) {
662 return xstrdup("meter must have bands");
663 }
664
665 mm->meter.n_bands = n_bands;
666 mm->meter.bands = ofpbuf_steal_data(bands);
667
668 for (i = 0; i < n_bands; ++i) {
669 band = &mm->meter.bands[i];
670
671 if (!band->type) {
672 return xstrdup("band must have 'type'");
673 }
674 if (band->type == OFPMBT13_DSCP_REMARK) {
675 if (!band->prec_level) {
676 return xstrdup("'dscp_remark' band must have"
677 " 'prec_level'");
678 }
679 } else {
680 if (band->prec_level) {
681 return xstrdup("Only 'dscp_remark' band may have"
682 " 'prec_level'");
683 }
684 }
685 if (!band->rate) {
686 return xstrdup("band must have 'rate'");
687 }
688 if (mm->meter.flags & OFPMF13_BURST) {
689 if (!band->burst_size) {
690 return xstrdup("band must have 'burst_size' "
691 "when 'burst' flag is set");
692 }
693 } else {
694 if (band->burst_size) {
695 return xstrdup("band may have 'burst_size' only "
696 "when 'burst' flag is set");
697 }
698 }
699 }
700 } else {
701 mm->meter.n_bands = 0;
702 mm->meter.bands = NULL;
703 }
704
705 return NULL;
706 }
707
708 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
709 * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
710 *
711 * Returns NULL if successful, otherwise a malloc()'d string describing the
712 * error. The caller is responsible for freeing the returned string. */
713 char * OVS_WARN_UNUSED_RESULT
714 parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_,
715 int command, enum ofputil_protocol *usable_protocols)
716 {
717 struct ofpbuf bands;
718 char *string;
719 char *error;
720
721 ofpbuf_init(&bands, 64);
722 string = xstrdup(str_);
723
724 error = parse_ofp_meter_mod_str__(mm, string, &bands, command,
725 usable_protocols);
726
727 free(string);
728 ofpbuf_uninit(&bands);
729
730 return error;
731 }
732
733 static char * OVS_WARN_UNUSED_RESULT
734 parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
735 const char *str_, char *string,
736 enum ofputil_protocol *usable_protocols)
737 {
738 static atomic_count id = ATOMIC_COUNT_INIT(0);
739 char *save_ptr = NULL;
740 char *name;
741
742 fmr->id = atomic_count_inc(&id);
743
744 fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
745 | NXFMF_OWN | NXFMF_ACTIONS);
746 fmr->out_port = OFPP_NONE;
747 fmr->table_id = 0xff;
748 match_init_catchall(&fmr->match);
749
750 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
751 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
752 const struct protocol *p;
753
754 if (!strcmp(name, "!initial")) {
755 fmr->flags &= ~NXFMF_INITIAL;
756 } else if (!strcmp(name, "!add")) {
757 fmr->flags &= ~NXFMF_ADD;
758 } else if (!strcmp(name, "!delete")) {
759 fmr->flags &= ~NXFMF_DELETE;
760 } else if (!strcmp(name, "!modify")) {
761 fmr->flags &= ~NXFMF_MODIFY;
762 } else if (!strcmp(name, "!actions")) {
763 fmr->flags &= ~NXFMF_ACTIONS;
764 } else if (!strcmp(name, "!own")) {
765 fmr->flags &= ~NXFMF_OWN;
766 } else if (parse_protocol(name, &p)) {
767 match_set_dl_type(&fmr->match, htons(p->dl_type));
768 if (p->nw_proto) {
769 match_set_nw_proto(&fmr->match, p->nw_proto);
770 }
771 } else {
772 char *value;
773
774 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
775 if (!value) {
776 return xasprintf("%s: field %s missing value", str_, name);
777 }
778
779 if (!strcmp(name, "table")) {
780 char *error = str_to_u8(value, "table", &fmr->table_id);
781 if (error) {
782 return error;
783 }
784 } else if (!strcmp(name, "out_port")) {
785 fmr->out_port = u16_to_ofp(atoi(value));
786 } else if (mf_from_name(name)) {
787 char *error;
788
789 error = parse_field(mf_from_name(name), value, &fmr->match,
790 usable_protocols);
791 if (error) {
792 return error;
793 }
794 } else {
795 return xasprintf("%s: unknown keyword %s", str_, name);
796 }
797 }
798 }
799 return NULL;
800 }
801
802 /* Convert 'str_' (as described in the documentation for the "monitor" command
803 * in the ovs-ofctl man page) into 'fmr'.
804 *
805 * Returns NULL if successful, otherwise a malloc()'d string describing the
806 * error. The caller is responsible for freeing the returned string. */
807 char * OVS_WARN_UNUSED_RESULT
808 parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
809 const char *str_,
810 enum ofputil_protocol *usable_protocols)
811 {
812 char *string = xstrdup(str_);
813 char *error = parse_flow_monitor_request__(fmr, str_, string,
814 usable_protocols);
815 free(string);
816 return error;
817 }
818
819 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
820 * (one of OFPFC_*) into 'fm'.
821 *
822 * Returns NULL if successful, otherwise a malloc()'d string describing the
823 * error. The caller is responsible for freeing the returned string. */
824 char * OVS_WARN_UNUSED_RESULT
825 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
826 uint16_t command,
827 enum ofputil_protocol *usable_protocols)
828 {
829 char *error = parse_ofp_str(fm, command, string, usable_protocols);
830 if (!error) {
831 /* Normalize a copy of the match. This ensures that non-normalized
832 * flows get logged but doesn't affect what gets sent to the switch, so
833 * that the switch can do whatever it likes with the flow. */
834 struct match match_copy = fm->match;
835 ofputil_normalize_match(&match_copy);
836 }
837
838 return error;
839 }
840
841 /* Convert 'table_id' and 'flow_miss_handling' (as described for the
842 * "mod-table" command in the ovs-ofctl man page) into 'tm' for sending the
843 * specified table_mod 'command' to a switch.
844 *
845 * Returns NULL if successful, otherwise a malloc()'d string describing the
846 * error. The caller is responsible for freeing the returned string. */
847 char * OVS_WARN_UNUSED_RESULT
848 parse_ofp_table_mod(struct ofputil_table_mod *tm, const char *table_id,
849 const char *flow_miss_handling,
850 enum ofputil_protocol *usable_protocols)
851 {
852 /* Table mod requires at least OF 1.1. */
853 *usable_protocols = OFPUTIL_P_OF11_UP;
854
855 if (!strcasecmp(table_id, "all")) {
856 tm->table_id = OFPTT_ALL;
857 } else {
858 char *error = str_to_u8(table_id, "table_id", &tm->table_id);
859 if (error) {
860 return error;
861 }
862 }
863
864 if (strcmp(flow_miss_handling, "controller") == 0) {
865 tm->miss_config = OFPUTIL_TABLE_MISS_CONTROLLER;
866 } else if (strcmp(flow_miss_handling, "continue") == 0) {
867 tm->miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
868 } else if (strcmp(flow_miss_handling, "drop") == 0) {
869 tm->miss_config = OFPUTIL_TABLE_MISS_DROP;
870 } else {
871 return xasprintf("invalid flow_miss_handling %s", flow_miss_handling);
872 }
873
874 if (tm->table_id == 0xfe
875 && tm->miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
876 return xstrdup("last table's flow miss handling can not be continue");
877 }
878
879 return NULL;
880 }
881
882
883 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
884 * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated
885 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
886 *
887 * Returns NULL if successful, otherwise a malloc()'d string describing the
888 * error. The caller is responsible for freeing the returned string. */
889 char * OVS_WARN_UNUSED_RESULT
890 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
891 struct ofputil_flow_mod **fms, size_t *n_fms,
892 enum ofputil_protocol *usable_protocols)
893 {
894 size_t allocated_fms;
895 int line_number;
896 FILE *stream;
897 struct ds s;
898
899 *usable_protocols = OFPUTIL_P_ANY;
900
901 *fms = NULL;
902 *n_fms = 0;
903
904 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
905 if (stream == NULL) {
906 return xasprintf("%s: open failed (%s)",
907 file_name, ovs_strerror(errno));
908 }
909
910 allocated_fms = *n_fms;
911 ds_init(&s);
912 line_number = 0;
913 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
914 char *error;
915 enum ofputil_protocol usable;
916
917 if (*n_fms >= allocated_fms) {
918 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
919 }
920 error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command,
921 &usable);
922 if (error) {
923 size_t i;
924
925 for (i = 0; i < *n_fms; i++) {
926 free(CONST_CAST(struct ofpact *, (*fms)[i].ofpacts));
927 }
928 free(*fms);
929 *fms = NULL;
930 *n_fms = 0;
931
932 ds_destroy(&s);
933 if (stream != stdin) {
934 fclose(stream);
935 }
936
937 return xasprintf("%s:%d: %s", file_name, line_number, error);
938 }
939 *usable_protocols &= usable; /* Each line can narrow the set. */
940 *n_fms += 1;
941 }
942
943 ds_destroy(&s);
944 if (stream != stdin) {
945 fclose(stream);
946 }
947 return NULL;
948 }
949
950 char * OVS_WARN_UNUSED_RESULT
951 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
952 bool aggregate, const char *string,
953 enum ofputil_protocol *usable_protocols)
954 {
955 struct ofputil_flow_mod fm;
956 char *error;
957
958 error = parse_ofp_str(&fm, -1, string, usable_protocols);
959 if (error) {
960 return error;
961 }
962
963 /* Special table ID support not required for stats requests. */
964 if (*usable_protocols & OFPUTIL_P_OF10_STD_TID) {
965 *usable_protocols |= OFPUTIL_P_OF10_STD;
966 }
967 if (*usable_protocols & OFPUTIL_P_OF10_NXM_TID) {
968 *usable_protocols |= OFPUTIL_P_OF10_NXM;
969 }
970
971 fsr->aggregate = aggregate;
972 fsr->cookie = fm.cookie;
973 fsr->cookie_mask = fm.cookie_mask;
974 fsr->match = fm.match;
975 fsr->out_port = fm.out_port;
976 fsr->out_group = fm.out_group;
977 fsr->table_id = fm.table_id;
978 return NULL;
979 }
980
981 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
982 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
983 * mf_field. Fields must be specified in a natural order for satisfying
984 * prerequisites. If 'mask' is specified, fills the mask field for each of the
985 * field specified in flow. If the map, 'names_portno' is specfied, converts
986 * the in_port name into port no while setting the 'flow'.
987 *
988 * Returns NULL on success, otherwise a malloc()'d string that explains the
989 * problem. */
990 char *
991 parse_ofp_exact_flow(struct flow *flow, struct flow *mask, const char *s,
992 const struct simap *portno_names)
993 {
994 char *pos, *key, *value_s;
995 char *error = NULL;
996 char *copy;
997
998 memset(flow, 0, sizeof *flow);
999 if (mask) {
1000 memset(mask, 0, sizeof *mask);
1001 }
1002
1003 pos = copy = xstrdup(s);
1004 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
1005 const struct protocol *p;
1006 if (parse_protocol(key, &p)) {
1007 if (flow->dl_type) {
1008 error = xasprintf("%s: Ethernet type set multiple times", s);
1009 goto exit;
1010 }
1011 flow->dl_type = htons(p->dl_type);
1012 if (mask) {
1013 mask->dl_type = OVS_BE16_MAX;
1014 }
1015
1016 if (p->nw_proto) {
1017 if (flow->nw_proto) {
1018 error = xasprintf("%s: network protocol set "
1019 "multiple times", s);
1020 goto exit;
1021 }
1022 flow->nw_proto = p->nw_proto;
1023 if (mask) {
1024 mask->nw_proto = UINT8_MAX;
1025 }
1026 }
1027 } else {
1028 const struct mf_field *mf;
1029 union mf_value value;
1030 char *field_error;
1031
1032 mf = mf_from_name(key);
1033 if (!mf) {
1034 error = xasprintf("%s: unknown field %s", s, key);
1035 goto exit;
1036 }
1037
1038 if (!mf_are_prereqs_ok(mf, flow)) {
1039 error = xasprintf("%s: prerequisites not met for setting %s",
1040 s, key);
1041 goto exit;
1042 }
1043
1044 if (!mf_is_zero(mf, flow)) {
1045 error = xasprintf("%s: field %s set multiple times", s, key);
1046 goto exit;
1047 }
1048
1049 if (!strcmp(key, "in_port")
1050 && portno_names
1051 && simap_contains(portno_names, value_s)) {
1052 flow->in_port.ofp_port = u16_to_ofp(
1053 simap_get(portno_names, value_s));
1054 if (mask) {
1055 mask->in_port.ofp_port = u16_to_ofp(ntohs(OVS_BE16_MAX));
1056 }
1057 } else {
1058 field_error = mf_parse_value(mf, value_s, &value);
1059 if (field_error) {
1060 error = xasprintf("%s: bad value for %s (%s)",
1061 s, key, field_error);
1062 free(field_error);
1063 goto exit;
1064 }
1065
1066 mf_set_flow_value(mf, &value, flow);
1067 if (mask) {
1068 mf_mask_field(mf, mask);
1069 }
1070 }
1071 }
1072 }
1073
1074 if (!flow->in_port.ofp_port) {
1075 flow->in_port.ofp_port = OFPP_NONE;
1076 }
1077
1078 exit:
1079 free(copy);
1080
1081 if (error) {
1082 memset(flow, 0, sizeof *flow);
1083 if (mask) {
1084 memset(mask, 0, sizeof *mask);
1085 }
1086 }
1087 return error;
1088 }
1089
1090 static char * OVS_WARN_UNUSED_RESULT
1091 parse_bucket_str(struct ofputil_bucket *bucket, char *str_,
1092 enum ofputil_protocol *usable_protocols)
1093 {
1094 char *pos, *key, *value;
1095 struct ofpbuf ofpacts;
1096 struct ds actions;
1097 char *error;
1098
1099 bucket->weight = 1;
1100 bucket->bucket_id = OFPG15_BUCKET_ALL;
1101 bucket->watch_port = OFPP_ANY;
1102 bucket->watch_group = OFPG11_ANY;
1103
1104 ds_init(&actions);
1105
1106 pos = str_;
1107 error = NULL;
1108 while (ofputil_parse_key_value(&pos, &key, &value)) {
1109 if (!strcasecmp(key, "weight")) {
1110 error = str_to_u16(value, "weight", &bucket->weight);
1111 } else if (!strcasecmp(key, "watch_port")) {
1112 if (!ofputil_port_from_string(value, &bucket->watch_port)
1113 || (ofp_to_u16(bucket->watch_port) >= ofp_to_u16(OFPP_MAX)
1114 && bucket->watch_port != OFPP_ANY)) {
1115 error = xasprintf("%s: invalid watch_port", value);
1116 }
1117 } else if (!strcasecmp(key, "watch_group")) {
1118 error = str_to_u32(value, &bucket->watch_group);
1119 if (!error && bucket->watch_group > OFPG_MAX) {
1120 error = xasprintf("invalid watch_group id %"PRIu32,
1121 bucket->watch_group);
1122 }
1123 } else if (!strcasecmp(key, "bucket_id")) {
1124 error = str_to_u32(value, &bucket->bucket_id);
1125 if (!error && bucket->bucket_id > OFPG15_BUCKET_MAX) {
1126 error = xasprintf("invalid bucket_id id %"PRIu32,
1127 bucket->bucket_id);
1128 }
1129 *usable_protocols &= OFPUTIL_P_OF15_UP;
1130 } else if (!strcasecmp(key, "action") || !strcasecmp(key, "actions")) {
1131 ds_put_format(&actions, "%s,", value);
1132 } else {
1133 ds_put_format(&actions, "%s(%s),", key, value);
1134 }
1135
1136 if (error) {
1137 ds_destroy(&actions);
1138 return error;
1139 }
1140 }
1141
1142 if (!actions.length) {
1143 return xstrdup("bucket must specify actions");
1144 }
1145 ds_chomp(&actions, ',');
1146
1147 ofpbuf_init(&ofpacts, 0);
1148 error = ofpacts_parse_actions(ds_cstr(&actions), &ofpacts,
1149 usable_protocols);
1150 ds_destroy(&actions);
1151 if (error) {
1152 ofpbuf_uninit(&ofpacts);
1153 return error;
1154 }
1155 bucket->ofpacts = ofpbuf_data(&ofpacts);
1156 bucket->ofpacts_len = ofpbuf_size(&ofpacts);
1157
1158 return NULL;
1159 }
1160
1161 static char * OVS_WARN_UNUSED_RESULT
1162 parse_ofp_group_mod_str__(struct ofputil_group_mod *gm, uint16_t command,
1163 char *string,
1164 enum ofputil_protocol *usable_protocols)
1165 {
1166 enum {
1167 F_GROUP_TYPE = 1 << 0,
1168 F_BUCKETS = 1 << 1,
1169 F_COMMAND_BUCKET_ID = 1 << 2,
1170 F_COMMAND_BUCKET_ID_ALL = 1 << 3,
1171 } fields;
1172 char *save_ptr = NULL;
1173 bool had_type = false;
1174 bool had_command_bucket_id = false;
1175 char *name;
1176 struct ofputil_bucket *bucket;
1177 char *error = NULL;
1178
1179 *usable_protocols = OFPUTIL_P_OF11_UP;
1180
1181 switch (command) {
1182 case OFPGC11_ADD:
1183 fields = F_GROUP_TYPE | F_BUCKETS;
1184 break;
1185
1186 case OFPGC11_DELETE:
1187 fields = 0;
1188 break;
1189
1190 case OFPGC11_MODIFY:
1191 fields = F_GROUP_TYPE | F_BUCKETS;
1192 break;
1193
1194 case OFPGC15_INSERT_BUCKET:
1195 fields = F_BUCKETS | F_COMMAND_BUCKET_ID;
1196 *usable_protocols &= OFPUTIL_P_OF15_UP;
1197 break;
1198
1199 case OFPGC15_REMOVE_BUCKET:
1200 fields = F_COMMAND_BUCKET_ID | F_COMMAND_BUCKET_ID_ALL;
1201 *usable_protocols &= OFPUTIL_P_OF15_UP;
1202 break;
1203
1204 default:
1205 OVS_NOT_REACHED();
1206 }
1207
1208 memset(gm, 0, sizeof *gm);
1209 gm->command = command;
1210 gm->group_id = OFPG_ANY;
1211 gm->command_bucket_id = OFPG15_BUCKET_ALL;
1212 list_init(&gm->buckets);
1213 if (command == OFPGC11_DELETE && string[0] == '\0') {
1214 gm->group_id = OFPG_ALL;
1215 return NULL;
1216 }
1217
1218 *usable_protocols = OFPUTIL_P_OF11_UP;
1219
1220 if (fields & F_BUCKETS) {
1221 char *bkt_str = strstr(string, "bucket=");
1222
1223 if (bkt_str) {
1224 *bkt_str = '\0';
1225 }
1226
1227 while (bkt_str) {
1228 char *next_bkt_str;
1229
1230 bkt_str = strchr(bkt_str + 1, '=');
1231 if (!bkt_str) {
1232 error = xstrdup("must specify bucket content");
1233 goto out;
1234 }
1235 bkt_str++;
1236
1237 next_bkt_str = strstr(bkt_str, "bucket=");
1238 if (next_bkt_str) {
1239 *next_bkt_str = '\0';
1240 }
1241
1242 bucket = xzalloc(sizeof(struct ofputil_bucket));
1243 error = parse_bucket_str(bucket, bkt_str, usable_protocols);
1244 if (error) {
1245 free(bucket);
1246 goto out;
1247 }
1248 list_push_back(&gm->buckets, &bucket->list_node);
1249
1250 bkt_str = next_bkt_str;
1251 }
1252 }
1253
1254 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1255 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1256 char *value;
1257
1258 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1259 if (!value) {
1260 error = xasprintf("field %s missing value", name);
1261 goto out;
1262 }
1263
1264 if (!strcmp(name, "command_bucket_id")) {
1265 if (!(fields & F_COMMAND_BUCKET_ID)) {
1266 error = xstrdup("command bucket id is not needed");
1267 goto out;
1268 }
1269 if (!strcmp(value, "all")) {
1270 gm->command_bucket_id = OFPG15_BUCKET_ALL;
1271 } else if (!strcmp(value, "first")) {
1272 gm->command_bucket_id = OFPG15_BUCKET_FIRST;
1273 } else if (!strcmp(value, "last")) {
1274 gm->command_bucket_id = OFPG15_BUCKET_LAST;
1275 } else {
1276 char *error = str_to_u32(value, &gm->command_bucket_id);
1277 if (error) {
1278 goto out;
1279 }
1280 if (gm->command_bucket_id > OFPG15_BUCKET_MAX
1281 && (gm->command_bucket_id != OFPG15_BUCKET_FIRST
1282 && gm->command_bucket_id != OFPG15_BUCKET_LAST
1283 && gm->command_bucket_id != OFPG15_BUCKET_ALL)) {
1284 error = xasprintf("invalid command bucket id %"PRIu32,
1285 gm->command_bucket_id);
1286 goto out;
1287 }
1288 }
1289 if (gm->command_bucket_id == OFPG15_BUCKET_ALL
1290 && !(fields & F_COMMAND_BUCKET_ID_ALL)) {
1291 error = xstrdup("command_bucket_id=all is not permitted");
1292 goto out;
1293 }
1294 had_command_bucket_id = true;
1295 } else if (!strcmp(name, "group_id")) {
1296 if(!strcmp(value, "all")) {
1297 gm->group_id = OFPG_ALL;
1298 } else {
1299 char *error = str_to_u32(value, &gm->group_id);
1300 if (error) {
1301 goto out;
1302 }
1303 if (gm->group_id != OFPG_ALL && gm->group_id > OFPG_MAX) {
1304 error = xasprintf("invalid group id %"PRIu32,
1305 gm->group_id);
1306 goto out;
1307 }
1308 }
1309 } else if (!strcmp(name, "type")){
1310 if (!(fields & F_GROUP_TYPE)) {
1311 error = xstrdup("type is not needed");
1312 goto out;
1313 }
1314 if (!strcmp(value, "all")) {
1315 gm->type = OFPGT11_ALL;
1316 } else if (!strcmp(value, "select")) {
1317 gm->type = OFPGT11_SELECT;
1318 } else if (!strcmp(value, "indirect")) {
1319 gm->type = OFPGT11_INDIRECT;
1320 } else if (!strcmp(value, "ff") ||
1321 !strcmp(value, "fast_failover")) {
1322 gm->type = OFPGT11_FF;
1323 } else {
1324 error = xasprintf("invalid group type %s", value);
1325 goto out;
1326 }
1327 had_type = true;
1328 } else if (!strcmp(name, "bucket")) {
1329 error = xstrdup("bucket is not needed");
1330 goto out;
1331 } else {
1332 error = xasprintf("unknown keyword %s", name);
1333 goto out;
1334 }
1335 }
1336 if (gm->group_id == OFPG_ANY) {
1337 error = xstrdup("must specify a group_id");
1338 goto out;
1339 }
1340 if (fields & F_GROUP_TYPE && !had_type) {
1341 error = xstrdup("must specify a type");
1342 goto out;
1343 }
1344
1345 if (fields & F_COMMAND_BUCKET_ID) {
1346 if (!(fields & F_COMMAND_BUCKET_ID_ALL || had_command_bucket_id)) {
1347 error = xstrdup("must specify a command bucket id");
1348 goto out;
1349 }
1350 } else if (had_command_bucket_id) {
1351 error = xstrdup("command bucket id is not needed");
1352 goto out;
1353 }
1354
1355 /* Validate buckets. */
1356 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
1357 if (bucket->weight != 1 && gm->type != OFPGT11_SELECT) {
1358 error = xstrdup("Only select groups can have bucket weights.");
1359 goto out;
1360 }
1361 }
1362 if (gm->type == OFPGT11_INDIRECT && !list_is_short(&gm->buckets)) {
1363 error = xstrdup("Indirect groups can have at most one bucket.");
1364 goto out;
1365 }
1366
1367 return NULL;
1368 out:
1369 ofputil_bucket_list_destroy(&gm->buckets);
1370 return error;
1371 }
1372
1373 char * OVS_WARN_UNUSED_RESULT
1374 parse_ofp_group_mod_str(struct ofputil_group_mod *gm, uint16_t command,
1375 const char *str_,
1376 enum ofputil_protocol *usable_protocols)
1377 {
1378 char *string = xstrdup(str_);
1379 char *error = parse_ofp_group_mod_str__(gm, command, string,
1380 usable_protocols);
1381 free(string);
1382
1383 if (error) {
1384 ofputil_bucket_list_destroy(&gm->buckets);
1385 }
1386 return error;
1387 }
1388
1389 char * OVS_WARN_UNUSED_RESULT
1390 parse_ofp_group_mod_file(const char *file_name, uint16_t command,
1391 struct ofputil_group_mod **gms, size_t *n_gms,
1392 enum ofputil_protocol *usable_protocols)
1393 {
1394 size_t allocated_gms;
1395 int line_number;
1396 FILE *stream;
1397 struct ds s;
1398
1399 *gms = NULL;
1400 *n_gms = 0;
1401
1402 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1403 if (stream == NULL) {
1404 return xasprintf("%s: open failed (%s)",
1405 file_name, ovs_strerror(errno));
1406 }
1407
1408 allocated_gms = *n_gms;
1409 ds_init(&s);
1410 line_number = 0;
1411 *usable_protocols = OFPUTIL_P_OF11_UP;
1412 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
1413 enum ofputil_protocol usable;
1414 char *error;
1415
1416 if (*n_gms >= allocated_gms) {
1417 size_t i;
1418
1419 *gms = x2nrealloc(*gms, &allocated_gms, sizeof **gms);
1420 for (i = 0; i < *n_gms; i++) {
1421 list_moved(&(*gms)[i].buckets);
1422 }
1423 }
1424 error = parse_ofp_group_mod_str(&(*gms)[*n_gms], command, ds_cstr(&s),
1425 &usable);
1426 if (error) {
1427 size_t i;
1428
1429 for (i = 0; i < *n_gms; i++) {
1430 ofputil_bucket_list_destroy(&(*gms)[i].buckets);
1431 }
1432 free(*gms);
1433 *gms = NULL;
1434 *n_gms = 0;
1435
1436 ds_destroy(&s);
1437 if (stream != stdin) {
1438 fclose(stream);
1439 }
1440
1441 return xasprintf("%s:%d: %s", file_name, line_number, error);
1442 }
1443 *usable_protocols &= usable;
1444 *n_gms += 1;
1445 }
1446
1447 ds_destroy(&s);
1448 if (stream != stdin) {
1449 fclose(stream);
1450 }
1451 return NULL;
1452 }