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