]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ofp-parse.c
tunnel: Support matching on the presence of Geneve options.
[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 if (mf_from_name(name)) {
369 char *value;
370
371 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
372 if (!value) {
373 /* If there's no value, we're just trying to match on the
374 * existence of the field, so use a no-op value. */
375 value = "0/0";
376 }
377
378 error = parse_field(mf_from_name(name), value, &fm->match,
379 usable_protocols);
380 if (error) {
381 return error;
382 }
383 } else {
384 char *value;
385
386 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
387 if (!value) {
388 return xasprintf("field %s missing value", name);
389 }
390
391 if (!strcmp(name, "table")) {
392 error = str_to_u8(value, "table", &fm->table_id);
393 if (fm->table_id != 0xff) {
394 *usable_protocols &= OFPUTIL_P_TID;
395 }
396 } else if (fields & F_OUT_PORT && !strcmp(name, "out_port")) {
397 if (!ofputil_port_from_string(value, &fm->out_port)) {
398 error = xasprintf("%s is not a valid OpenFlow port",
399 value);
400 }
401 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
402 uint16_t priority = 0;
403
404 error = str_to_u16(value, name, &priority);
405 fm->priority = priority;
406 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
407 error = str_to_u16(value, name, &fm->idle_timeout);
408 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
409 error = str_to_u16(value, name, &fm->hard_timeout);
410 } else if (fields & F_IMPORTANCE && !strcmp(name, "importance")) {
411 error = str_to_u16(value, name, &fm->importance);
412 } else if (!strcmp(name, "cookie")) {
413 char *mask = strchr(value, '/');
414
415 if (mask) {
416 /* A mask means we're searching for a cookie. */
417 if (command == OFPFC_ADD) {
418 return xstrdup("flow additions cannot use "
419 "a cookie mask");
420 }
421 *mask = '\0';
422 error = str_to_be64(value, &fm->cookie);
423 if (error) {
424 return error;
425 }
426 error = str_to_be64(mask + 1, &fm->cookie_mask);
427
428 /* Matching of the cookie is only supported through NXM or
429 * OF1.1+. */
430 if (fm->cookie_mask != htonll(0)) {
431 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
432 }
433 } else {
434 /* No mask means that the cookie is being set. */
435 if (command != OFPFC_ADD && command != OFPFC_MODIFY
436 && command != OFPFC_MODIFY_STRICT) {
437 return xstrdup("cannot set cookie");
438 }
439 error = str_to_be64(value, &fm->new_cookie);
440 fm->modify_cookie = true;
441 }
442 } else if (!strcmp(name, "duration")
443 || !strcmp(name, "n_packets")
444 || !strcmp(name, "n_bytes")
445 || !strcmp(name, "idle_age")
446 || !strcmp(name, "hard_age")) {
447 /* Ignore these, so that users can feed the output of
448 * "ovs-ofctl dump-flows" back into commands that parse
449 * flows. */
450 } else {
451 error = xasprintf("unknown keyword %s", name);
452 }
453
454 if (error) {
455 return error;
456 }
457 }
458 }
459 /* Check for usable protocol interdependencies between match fields. */
460 if (fm->match.flow.dl_type == htons(ETH_TYPE_IPV6)) {
461 const struct flow_wildcards *wc = &fm->match.wc;
462 /* Only NXM and OXM support matching L3 and L4 fields within IPv6.
463 *
464 * (IPv6 specific fields as well as arp_sha, arp_tha, nw_frag, and
465 * nw_ttl are covered elsewhere so they don't need to be included in
466 * this test too.)
467 */
468 if (wc->masks.nw_proto || wc->masks.nw_tos
469 || wc->masks.tp_src || wc->masks.tp_dst) {
470 *usable_protocols &= OFPUTIL_P_NXM_OXM_ANY;
471 }
472 }
473 if (!fm->cookie_mask && fm->new_cookie == OVS_BE64_MAX
474 && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
475 /* On modifies without a mask, we are supposed to add a flow if
476 * one does not exist. If a cookie wasn't been specified, use a
477 * default of zero. */
478 fm->new_cookie = htonll(0);
479 }
480 if (fields & F_ACTIONS) {
481 enum ofputil_protocol action_usable_protocols;
482 struct ofpbuf ofpacts;
483 char *error;
484
485 ofpbuf_init(&ofpacts, 32);
486 error = ofpacts_parse_instructions(act_str, &ofpacts,
487 &action_usable_protocols);
488 *usable_protocols &= action_usable_protocols;
489 if (!error) {
490 enum ofperr err;
491
492 err = ofpacts_check(ofpacts.data, ofpacts.size, &fm->match.flow,
493 OFPP_MAX, fm->table_id, 255, usable_protocols);
494 if (!err && !*usable_protocols) {
495 err = OFPERR_OFPBAC_MATCH_INCONSISTENT;
496 }
497 if (err) {
498 error = xasprintf("actions are invalid with specified match "
499 "(%s)", ofperr_to_string(err));
500 }
501
502 }
503 if (error) {
504 ofpbuf_uninit(&ofpacts);
505 return error;
506 }
507
508 fm->ofpacts_len = ofpacts.size;
509 fm->ofpacts = ofpbuf_steal_data(&ofpacts);
510 } else {
511 fm->ofpacts_len = 0;
512 fm->ofpacts = NULL;
513 }
514
515 return NULL;
516 }
517
518 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
519 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
520 * Returns the set of usable protocols in '*usable_protocols'.
521 *
522 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
523 * constant for 'command'. To parse syntax for an OFPST_FLOW or
524 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'.
525 *
526 * If 'command' is given as -2, 'str_' may begin with a command name ("add",
527 * "modify", "delete", "modify_strict", or "delete_strict"). A missing command
528 * name is treated as "add".
529 *
530 * Returns NULL if successful, otherwise a malloc()'d string describing the
531 * error. The caller is responsible for freeing the returned string. */
532 char * OVS_WARN_UNUSED_RESULT
533 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
534 enum ofputil_protocol *usable_protocols)
535 {
536 char *string = xstrdup(str_);
537 char *error;
538
539 error = parse_ofp_str__(fm, command, string, usable_protocols);
540 if (error) {
541 fm->ofpacts = NULL;
542 fm->ofpacts_len = 0;
543 }
544
545 free(string);
546 return error;
547 }
548
549 static char * OVS_WARN_UNUSED_RESULT
550 parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string,
551 struct ofpbuf *bands, int command,
552 enum ofputil_protocol *usable_protocols)
553 {
554 enum {
555 F_METER = 1 << 0,
556 F_FLAGS = 1 << 1,
557 F_BANDS = 1 << 2,
558 } fields;
559 char *save_ptr = NULL;
560 char *band_str = NULL;
561 char *name;
562
563 /* Meters require at least OF 1.3. */
564 *usable_protocols = OFPUTIL_P_OF13_UP;
565
566 switch (command) {
567 case -1:
568 fields = F_METER;
569 break;
570
571 case OFPMC13_ADD:
572 fields = F_METER | F_FLAGS | F_BANDS;
573 break;
574
575 case OFPMC13_DELETE:
576 fields = F_METER;
577 break;
578
579 case OFPMC13_MODIFY:
580 fields = F_METER | F_FLAGS | F_BANDS;
581 break;
582
583 default:
584 OVS_NOT_REACHED();
585 }
586
587 mm->command = command;
588 mm->meter.meter_id = 0;
589 mm->meter.flags = 0;
590 if (fields & F_BANDS) {
591 band_str = strstr(string, "band");
592 if (!band_str) {
593 return xstrdup("must specify bands");
594 }
595 *band_str = '\0';
596
597 band_str = strchr(band_str + 1, '=');
598 if (!band_str) {
599 return xstrdup("must specify bands");
600 }
601
602 band_str++;
603 }
604 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
605 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
606
607 if (fields & F_FLAGS && !strcmp(name, "kbps")) {
608 mm->meter.flags |= OFPMF13_KBPS;
609 } else if (fields & F_FLAGS && !strcmp(name, "pktps")) {
610 mm->meter.flags |= OFPMF13_PKTPS;
611 } else if (fields & F_FLAGS && !strcmp(name, "burst")) {
612 mm->meter.flags |= OFPMF13_BURST;
613 } else if (fields & F_FLAGS && !strcmp(name, "stats")) {
614 mm->meter.flags |= OFPMF13_STATS;
615 } else {
616 char *value;
617
618 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
619 if (!value) {
620 return xasprintf("field %s missing value", name);
621 }
622
623 if (!strcmp(name, "meter")) {
624 if (!strcmp(value, "all")) {
625 mm->meter.meter_id = OFPM13_ALL;
626 } else if (!strcmp(value, "controller")) {
627 mm->meter.meter_id = OFPM13_CONTROLLER;
628 } else if (!strcmp(value, "slowpath")) {
629 mm->meter.meter_id = OFPM13_SLOWPATH;
630 } else {
631 char *error = str_to_u32(value, &mm->meter.meter_id);
632 if (error) {
633 return error;
634 }
635 if (mm->meter.meter_id > OFPM13_MAX
636 || !mm->meter.meter_id) {
637 return xasprintf("invalid value for %s", name);
638 }
639 }
640 } else {
641 return xasprintf("unknown keyword %s", name);
642 }
643 }
644 }
645 if (fields & F_METER && !mm->meter.meter_id) {
646 return xstrdup("must specify 'meter'");
647 }
648 if (fields & F_FLAGS && !mm->meter.flags) {
649 return xstrdup("meter must specify either 'kbps' or 'pktps'");
650 }
651
652 if (fields & F_BANDS) {
653 uint16_t n_bands = 0;
654 struct ofputil_meter_band *band = NULL;
655 int i;
656
657 for (name = strtok_r(band_str, "=, \t\r\n", &save_ptr); name;
658 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
659
660 char *value;
661
662 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
663 if (!value) {
664 return xasprintf("field %s missing value", name);
665 }
666
667 if (!strcmp(name, "type")) {
668 /* Start a new band */
669 band = ofpbuf_put_zeros(bands, sizeof *band);
670 n_bands++;
671
672 if (!strcmp(value, "drop")) {
673 band->type = OFPMBT13_DROP;
674 } else if (!strcmp(value, "dscp_remark")) {
675 band->type = OFPMBT13_DSCP_REMARK;
676 } else {
677 return xasprintf("field %s unknown value %s", name, value);
678 }
679 } else if (!band || !band->type) {
680 return xstrdup("band must start with the 'type' keyword");
681 } else if (!strcmp(name, "rate")) {
682 char *error = str_to_u32(value, &band->rate);
683 if (error) {
684 return error;
685 }
686 } else if (!strcmp(name, "burst_size")) {
687 char *error = str_to_u32(value, &band->burst_size);
688 if (error) {
689 return error;
690 }
691 } else if (!strcmp(name, "prec_level")) {
692 char *error = str_to_u8(value, name, &band->prec_level);
693 if (error) {
694 return error;
695 }
696 } else {
697 return xasprintf("unknown keyword %s", name);
698 }
699 }
700 /* validate bands */
701 if (!n_bands) {
702 return xstrdup("meter must have bands");
703 }
704
705 mm->meter.n_bands = n_bands;
706 mm->meter.bands = ofpbuf_steal_data(bands);
707
708 for (i = 0; i < n_bands; ++i) {
709 band = &mm->meter.bands[i];
710
711 if (!band->type) {
712 return xstrdup("band must have 'type'");
713 }
714 if (band->type == OFPMBT13_DSCP_REMARK) {
715 if (!band->prec_level) {
716 return xstrdup("'dscp_remark' band must have"
717 " 'prec_level'");
718 }
719 } else {
720 if (band->prec_level) {
721 return xstrdup("Only 'dscp_remark' band may have"
722 " 'prec_level'");
723 }
724 }
725 if (!band->rate) {
726 return xstrdup("band must have 'rate'");
727 }
728 if (mm->meter.flags & OFPMF13_BURST) {
729 if (!band->burst_size) {
730 return xstrdup("band must have 'burst_size' "
731 "when 'burst' flag is set");
732 }
733 } else {
734 if (band->burst_size) {
735 return xstrdup("band may have 'burst_size' only "
736 "when 'burst' flag is set");
737 }
738 }
739 }
740 } else {
741 mm->meter.n_bands = 0;
742 mm->meter.bands = NULL;
743 }
744
745 return NULL;
746 }
747
748 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
749 * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
750 *
751 * Returns NULL if successful, otherwise a malloc()'d string describing the
752 * error. The caller is responsible for freeing the returned string. */
753 char * OVS_WARN_UNUSED_RESULT
754 parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_,
755 int command, enum ofputil_protocol *usable_protocols)
756 {
757 struct ofpbuf bands;
758 char *string;
759 char *error;
760
761 ofpbuf_init(&bands, 64);
762 string = xstrdup(str_);
763
764 error = parse_ofp_meter_mod_str__(mm, string, &bands, command,
765 usable_protocols);
766
767 free(string);
768 ofpbuf_uninit(&bands);
769
770 return error;
771 }
772
773 static char * OVS_WARN_UNUSED_RESULT
774 parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
775 const char *str_, char *string,
776 enum ofputil_protocol *usable_protocols)
777 {
778 static atomic_count id = ATOMIC_COUNT_INIT(0);
779 char *save_ptr = NULL;
780 char *name;
781
782 fmr->id = atomic_count_inc(&id);
783
784 fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
785 | NXFMF_OWN | NXFMF_ACTIONS);
786 fmr->out_port = OFPP_NONE;
787 fmr->table_id = 0xff;
788 match_init_catchall(&fmr->match);
789
790 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
791 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
792 const struct protocol *p;
793
794 if (!strcmp(name, "!initial")) {
795 fmr->flags &= ~NXFMF_INITIAL;
796 } else if (!strcmp(name, "!add")) {
797 fmr->flags &= ~NXFMF_ADD;
798 } else if (!strcmp(name, "!delete")) {
799 fmr->flags &= ~NXFMF_DELETE;
800 } else if (!strcmp(name, "!modify")) {
801 fmr->flags &= ~NXFMF_MODIFY;
802 } else if (!strcmp(name, "!actions")) {
803 fmr->flags &= ~NXFMF_ACTIONS;
804 } else if (!strcmp(name, "!own")) {
805 fmr->flags &= ~NXFMF_OWN;
806 } else if (parse_protocol(name, &p)) {
807 match_set_dl_type(&fmr->match, htons(p->dl_type));
808 if (p->nw_proto) {
809 match_set_nw_proto(&fmr->match, p->nw_proto);
810 }
811 } else {
812 char *value;
813
814 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
815 if (!value) {
816 return xasprintf("%s: field %s missing value", str_, name);
817 }
818
819 if (!strcmp(name, "table")) {
820 char *error = str_to_u8(value, "table", &fmr->table_id);
821 if (error) {
822 return error;
823 }
824 } else if (!strcmp(name, "out_port")) {
825 fmr->out_port = u16_to_ofp(atoi(value));
826 } else if (mf_from_name(name)) {
827 char *error;
828
829 error = parse_field(mf_from_name(name), value, &fmr->match,
830 usable_protocols);
831 if (error) {
832 return error;
833 }
834 } else {
835 return xasprintf("%s: unknown keyword %s", str_, name);
836 }
837 }
838 }
839 return NULL;
840 }
841
842 /* Convert 'str_' (as described in the documentation for the "monitor" command
843 * in the ovs-ofctl man page) into 'fmr'.
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_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
849 const char *str_,
850 enum ofputil_protocol *usable_protocols)
851 {
852 char *string = xstrdup(str_);
853 char *error = parse_flow_monitor_request__(fmr, str_, string,
854 usable_protocols);
855 free(string);
856 return error;
857 }
858
859 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
860 * (one of OFPFC_*) into 'fm'.
861 *
862 * If 'command' is given as -2, 'string' may begin with a command name ("add",
863 * "modify", "delete", "modify_strict", or "delete_strict"). A missing command
864 * name is treated as "add".
865 *
866 * Returns NULL if successful, otherwise a malloc()'d string describing the
867 * error. The caller is responsible for freeing the returned string. */
868 char * OVS_WARN_UNUSED_RESULT
869 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
870 int command,
871 enum ofputil_protocol *usable_protocols)
872 {
873 char *error = parse_ofp_str(fm, command, string, usable_protocols);
874
875 if (!error) {
876 /* Normalize a copy of the match. This ensures that non-normalized
877 * flows get logged but doesn't affect what gets sent to the switch, so
878 * that the switch can do whatever it likes with the flow. */
879 struct match match_copy = fm->match;
880 ofputil_normalize_match(&match_copy);
881 }
882
883 return error;
884 }
885
886 /* Convert 'table_id' and 'setting' (as described for the "mod-table" command
887 * in the ovs-ofctl man page) into 'tm' for sending a table_mod command to a
888 * switch.
889 *
890 * Stores a bitmap of the OpenFlow versions that are usable for 'tm' into
891 * '*usable_versions'.
892 *
893 * Returns NULL if successful, otherwise a malloc()'d string describing the
894 * error. The caller is responsible for freeing the returned string. */
895 char * OVS_WARN_UNUSED_RESULT
896 parse_ofp_table_mod(struct ofputil_table_mod *tm, const char *table_id,
897 const char *setting, uint32_t *usable_versions)
898 {
899 *usable_versions = 0;
900 if (!strcasecmp(table_id, "all")) {
901 tm->table_id = OFPTT_ALL;
902 } else {
903 char *error = str_to_u8(table_id, "table_id", &tm->table_id);
904 if (error) {
905 return error;
906 }
907 }
908
909 tm->miss = OFPUTIL_TABLE_MISS_DEFAULT;
910 tm->eviction = OFPUTIL_TABLE_EVICTION_DEFAULT;
911 tm->eviction_flags = UINT32_MAX;
912
913 /* Only OpenFlow 1.1 and 1.2 can configure table-miss via table_mod.
914 * Only OpenFlow 1.4+ can configure eviction via table_mod.
915 *
916 * (OpenFlow 1.4+ can also configure vacancy events via table_mod, but OVS
917 * doesn't support those yet and they're also logically a per-OpenFlow
918 * session setting so it wouldn't make sense to support them here anyway.)
919 */
920 if (!strcmp(setting, "controller")) {
921 tm->miss = OFPUTIL_TABLE_MISS_CONTROLLER;
922 *usable_versions = (1u << OFP11_VERSION) | (1u << OFP12_VERSION);
923 } else if (!strcmp(setting, "continue")) {
924 tm->miss = OFPUTIL_TABLE_MISS_CONTINUE;
925 *usable_versions = (1u << OFP11_VERSION) | (1u << OFP12_VERSION);
926 } else if (!strcmp(setting, "drop")) {
927 tm->miss = OFPUTIL_TABLE_MISS_DROP;
928 *usable_versions = (1u << OFP11_VERSION) | (1u << OFP12_VERSION);
929 } else if (!strcmp(setting, "evict")) {
930 tm->eviction = OFPUTIL_TABLE_EVICTION_ON;
931 *usable_versions = (1 << OFP14_VERSION) | (1u << OFP15_VERSION);
932 } else if (!strcmp(setting, "noevict")) {
933 tm->eviction = OFPUTIL_TABLE_EVICTION_OFF;
934 *usable_versions = (1 << OFP14_VERSION) | (1u << OFP15_VERSION);
935 } else {
936 return xasprintf("invalid table_mod setting %s", setting);
937 }
938
939 if (tm->table_id == 0xfe
940 && tm->miss == OFPUTIL_TABLE_MISS_CONTINUE) {
941 return xstrdup("last table's flow miss handling can not be continue");
942 }
943
944 return NULL;
945 }
946
947
948 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
949 * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated
950 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
951 *
952 * If 'command' is given as -2, each line may start with a command name
953 * ("add", "modify", "delete", "modify_strict", or "delete_strict"). A missing
954 * command name is treated as "add".
955 *
956 * Returns NULL if successful, otherwise a malloc()'d string describing the
957 * error. The caller is responsible for freeing the returned string. */
958 char * OVS_WARN_UNUSED_RESULT
959 parse_ofp_flow_mod_file(const char *file_name, int command,
960 struct ofputil_flow_mod **fms, size_t *n_fms,
961 enum ofputil_protocol *usable_protocols)
962 {
963 size_t allocated_fms;
964 int line_number;
965 FILE *stream;
966 struct ds s;
967
968 *usable_protocols = OFPUTIL_P_ANY;
969
970 *fms = NULL;
971 *n_fms = 0;
972
973 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
974 if (stream == NULL) {
975 return xasprintf("%s: open failed (%s)",
976 file_name, ovs_strerror(errno));
977 }
978
979 allocated_fms = *n_fms;
980 ds_init(&s);
981 line_number = 0;
982 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
983 char *error;
984 enum ofputil_protocol usable;
985
986 if (*n_fms >= allocated_fms) {
987 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
988 }
989 error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command,
990 &usable);
991 if (error) {
992 size_t i;
993
994 for (i = 0; i < *n_fms; i++) {
995 free(CONST_CAST(struct ofpact *, (*fms)[i].ofpacts));
996 }
997 free(*fms);
998 *fms = NULL;
999 *n_fms = 0;
1000
1001 ds_destroy(&s);
1002 if (stream != stdin) {
1003 fclose(stream);
1004 }
1005
1006 return xasprintf("%s:%d: %s", file_name, line_number, error);
1007 }
1008 *usable_protocols &= usable; /* Each line can narrow the set. */
1009 *n_fms += 1;
1010 }
1011
1012 ds_destroy(&s);
1013 if (stream != stdin) {
1014 fclose(stream);
1015 }
1016 return NULL;
1017 }
1018
1019 char * OVS_WARN_UNUSED_RESULT
1020 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
1021 bool aggregate, const char *string,
1022 enum ofputil_protocol *usable_protocols)
1023 {
1024 struct ofputil_flow_mod fm;
1025 char *error;
1026
1027 error = parse_ofp_str(&fm, -1, string, usable_protocols);
1028 if (error) {
1029 return error;
1030 }
1031
1032 /* Special table ID support not required for stats requests. */
1033 if (*usable_protocols & OFPUTIL_P_OF10_STD_TID) {
1034 *usable_protocols |= OFPUTIL_P_OF10_STD;
1035 }
1036 if (*usable_protocols & OFPUTIL_P_OF10_NXM_TID) {
1037 *usable_protocols |= OFPUTIL_P_OF10_NXM;
1038 }
1039
1040 fsr->aggregate = aggregate;
1041 fsr->cookie = fm.cookie;
1042 fsr->cookie_mask = fm.cookie_mask;
1043 fsr->match = fm.match;
1044 fsr->out_port = fm.out_port;
1045 fsr->out_group = fm.out_group;
1046 fsr->table_id = fm.table_id;
1047 return NULL;
1048 }
1049
1050 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
1051 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1052 * mf_field. Fields must be specified in a natural order for satisfying
1053 * prerequisites. If 'mask' is specified, fills the mask field for each of the
1054 * field specified in flow. If the map, 'names_portno' is specfied, converts
1055 * the in_port name into port no while setting the 'flow'.
1056 *
1057 * Returns NULL on success, otherwise a malloc()'d string that explains the
1058 * problem. */
1059 char *
1060 parse_ofp_exact_flow(struct flow *flow, struct flow *mask, const char *s,
1061 const struct simap *portno_names)
1062 {
1063 char *pos, *key, *value_s;
1064 char *error = NULL;
1065 char *copy;
1066
1067 memset(flow, 0, sizeof *flow);
1068 if (mask) {
1069 memset(mask, 0, sizeof *mask);
1070 }
1071
1072 pos = copy = xstrdup(s);
1073 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
1074 const struct protocol *p;
1075 if (parse_protocol(key, &p)) {
1076 if (flow->dl_type) {
1077 error = xasprintf("%s: Ethernet type set multiple times", s);
1078 goto exit;
1079 }
1080 flow->dl_type = htons(p->dl_type);
1081 if (mask) {
1082 mask->dl_type = OVS_BE16_MAX;
1083 }
1084
1085 if (p->nw_proto) {
1086 if (flow->nw_proto) {
1087 error = xasprintf("%s: network protocol set "
1088 "multiple times", s);
1089 goto exit;
1090 }
1091 flow->nw_proto = p->nw_proto;
1092 if (mask) {
1093 mask->nw_proto = UINT8_MAX;
1094 }
1095 }
1096 } else {
1097 const struct mf_field *mf;
1098 union mf_value value;
1099 char *field_error;
1100
1101 mf = mf_from_name(key);
1102 if (!mf) {
1103 error = xasprintf("%s: unknown field %s", s, key);
1104 goto exit;
1105 }
1106
1107 if (!mf_are_prereqs_ok(mf, flow)) {
1108 error = xasprintf("%s: prerequisites not met for setting %s",
1109 s, key);
1110 goto exit;
1111 }
1112
1113 if (mf_is_set(mf, flow)) {
1114 error = xasprintf("%s: field %s set multiple times", s, key);
1115 goto exit;
1116 }
1117
1118 if (!strcmp(key, "in_port")
1119 && portno_names
1120 && simap_contains(portno_names, value_s)) {
1121 flow->in_port.ofp_port = u16_to_ofp(
1122 simap_get(portno_names, value_s));
1123 if (mask) {
1124 mask->in_port.ofp_port = u16_to_ofp(ntohs(OVS_BE16_MAX));
1125 }
1126 } else {
1127 field_error = mf_parse_value(mf, value_s, &value);
1128 if (field_error) {
1129 error = xasprintf("%s: bad value for %s (%s)",
1130 s, key, field_error);
1131 free(field_error);
1132 goto exit;
1133 }
1134
1135 mf_set_flow_value(mf, &value, flow);
1136 if (mask) {
1137 mf_mask_field(mf, mask);
1138 }
1139 }
1140 }
1141 }
1142
1143 if (!flow->in_port.ofp_port) {
1144 flow->in_port.ofp_port = OFPP_NONE;
1145 }
1146
1147 exit:
1148 free(copy);
1149
1150 if (error) {
1151 memset(flow, 0, sizeof *flow);
1152 if (mask) {
1153 memset(mask, 0, sizeof *mask);
1154 }
1155 }
1156 return error;
1157 }
1158
1159 static char * OVS_WARN_UNUSED_RESULT
1160 parse_bucket_str(struct ofputil_bucket *bucket, char *str_, uint8_t group_type,
1161 enum ofputil_protocol *usable_protocols)
1162 {
1163 char *pos, *key, *value;
1164 struct ofpbuf ofpacts;
1165 struct ds actions;
1166 char *error;
1167
1168 bucket->weight = group_type == OFPGT11_SELECT ? 1 : 0;
1169 bucket->bucket_id = OFPG15_BUCKET_ALL;
1170 bucket->watch_port = OFPP_ANY;
1171 bucket->watch_group = OFPG11_ANY;
1172
1173 ds_init(&actions);
1174
1175 pos = str_;
1176 error = NULL;
1177 while (ofputil_parse_key_value(&pos, &key, &value)) {
1178 if (!strcasecmp(key, "weight")) {
1179 error = str_to_u16(value, "weight", &bucket->weight);
1180 } else if (!strcasecmp(key, "watch_port")) {
1181 if (!ofputil_port_from_string(value, &bucket->watch_port)
1182 || (ofp_to_u16(bucket->watch_port) >= ofp_to_u16(OFPP_MAX)
1183 && bucket->watch_port != OFPP_ANY)) {
1184 error = xasprintf("%s: invalid watch_port", value);
1185 }
1186 } else if (!strcasecmp(key, "watch_group")) {
1187 error = str_to_u32(value, &bucket->watch_group);
1188 if (!error && bucket->watch_group > OFPG_MAX) {
1189 error = xasprintf("invalid watch_group id %"PRIu32,
1190 bucket->watch_group);
1191 }
1192 } else if (!strcasecmp(key, "bucket_id")) {
1193 error = str_to_u32(value, &bucket->bucket_id);
1194 if (!error && bucket->bucket_id > OFPG15_BUCKET_MAX) {
1195 error = xasprintf("invalid bucket_id id %"PRIu32,
1196 bucket->bucket_id);
1197 }
1198 *usable_protocols &= OFPUTIL_P_OF15_UP;
1199 } else if (!strcasecmp(key, "action") || !strcasecmp(key, "actions")) {
1200 ds_put_format(&actions, "%s,", value);
1201 } else {
1202 ds_put_format(&actions, "%s(%s),", key, value);
1203 }
1204
1205 if (error) {
1206 ds_destroy(&actions);
1207 return error;
1208 }
1209 }
1210
1211 if (!actions.length) {
1212 return xstrdup("bucket must specify actions");
1213 }
1214 ds_chomp(&actions, ',');
1215
1216 ofpbuf_init(&ofpacts, 0);
1217 error = ofpacts_parse_actions(ds_cstr(&actions), &ofpacts,
1218 usable_protocols);
1219 ds_destroy(&actions);
1220 if (error) {
1221 ofpbuf_uninit(&ofpacts);
1222 return error;
1223 }
1224 bucket->ofpacts = ofpacts.data;
1225 bucket->ofpacts_len = ofpacts.size;
1226
1227 return NULL;
1228 }
1229
1230 static char * OVS_WARN_UNUSED_RESULT
1231 parse_select_group_field(char *s, struct field_array *fa,
1232 enum ofputil_protocol *usable_protocols)
1233 {
1234 char *save_ptr = NULL;
1235 char *name;
1236
1237 for (name = strtok_r(s, "=, \t\r\n", &save_ptr); name;
1238 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1239 const struct mf_field *mf = mf_from_name(name);
1240
1241 if (mf) {
1242 char *error;
1243 const char *value_str;
1244 union mf_value value;
1245
1246 if (bitmap_is_set(fa->used.bm, mf->id)) {
1247 return xasprintf("%s: duplicate field", name);
1248 }
1249
1250 value_str = strtok_r(NULL, ", \t\r\n", &save_ptr);
1251 if (value_str) {
1252 error = mf_parse_value(mf, value_str, &value);
1253 if (error) {
1254 return error;
1255 }
1256
1257 /* The mask cannot be all-zeros */
1258 if (!mf_is_tun_metadata(mf) &&
1259 is_all_zeros(&value, mf->n_bytes)) {
1260 return xasprintf("%s: values are wildcards here "
1261 "and must not be all-zeros", s);
1262 }
1263
1264 /* The values parsed are masks for fields used
1265 * by the selection method */
1266 if (!mf_is_mask_valid(mf, &value)) {
1267 return xasprintf("%s: invalid mask for field %s",
1268 value_str, mf->name);
1269 }
1270 } else {
1271 memset(&value, 0xff, mf->n_bytes);
1272 }
1273
1274 field_array_set(mf->id, &value, fa);
1275
1276 if (is_all_ones(&value, mf->n_bytes)) {
1277 *usable_protocols &= mf->usable_protocols_exact;
1278 } else if (mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1279 || ip_is_cidr(value.be32)) {
1280 *usable_protocols &= mf->usable_protocols_cidr;
1281 } else {
1282 *usable_protocols &= mf->usable_protocols_bitwise;
1283 }
1284 } else {
1285 return xasprintf("%s: unknown field %s", s, name);
1286 }
1287 }
1288
1289 return NULL;
1290 }
1291
1292 static char * OVS_WARN_UNUSED_RESULT
1293 parse_ofp_group_mod_str__(struct ofputil_group_mod *gm, uint16_t command,
1294 char *string,
1295 enum ofputil_protocol *usable_protocols)
1296 {
1297 enum {
1298 F_GROUP_TYPE = 1 << 0,
1299 F_BUCKETS = 1 << 1,
1300 F_COMMAND_BUCKET_ID = 1 << 2,
1301 F_COMMAND_BUCKET_ID_ALL = 1 << 3,
1302 } fields;
1303 char *save_ptr = NULL;
1304 bool had_type = false;
1305 bool had_command_bucket_id = false;
1306 char *name;
1307 struct ofputil_bucket *bucket;
1308 char *error = NULL;
1309
1310 *usable_protocols = OFPUTIL_P_OF11_UP;
1311
1312 switch (command) {
1313 case OFPGC11_ADD:
1314 fields = F_GROUP_TYPE | F_BUCKETS;
1315 break;
1316
1317 case OFPGC11_DELETE:
1318 fields = 0;
1319 break;
1320
1321 case OFPGC11_MODIFY:
1322 fields = F_GROUP_TYPE | F_BUCKETS;
1323 break;
1324
1325 case OFPGC15_INSERT_BUCKET:
1326 fields = F_BUCKETS | F_COMMAND_BUCKET_ID;
1327 *usable_protocols &= OFPUTIL_P_OF15_UP;
1328 break;
1329
1330 case OFPGC15_REMOVE_BUCKET:
1331 fields = F_COMMAND_BUCKET_ID | F_COMMAND_BUCKET_ID_ALL;
1332 *usable_protocols &= OFPUTIL_P_OF15_UP;
1333 break;
1334
1335 default:
1336 OVS_NOT_REACHED();
1337 }
1338
1339 memset(gm, 0, sizeof *gm);
1340 gm->command = command;
1341 gm->group_id = OFPG_ANY;
1342 gm->command_bucket_id = OFPG15_BUCKET_ALL;
1343 list_init(&gm->buckets);
1344 if (command == OFPGC11_DELETE && string[0] == '\0') {
1345 gm->group_id = OFPG_ALL;
1346 return NULL;
1347 }
1348
1349 *usable_protocols = OFPUTIL_P_OF11_UP;
1350
1351 /* Strip the buckets off the end of 'string', if there are any, saving a
1352 * pointer for later. We want to parse the buckets last because the bucket
1353 * type influences bucket defaults. */
1354 char *bkt_str = strstr(string, "bucket=");
1355 if (bkt_str) {
1356 if (!(fields & F_BUCKETS)) {
1357 error = xstrdup("bucket is not needed");
1358 goto out;
1359 }
1360 *bkt_str = '\0';
1361 }
1362
1363 /* Parse everything before the buckets. */
1364 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1365 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1366 char *value;
1367
1368 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1369 if (!value) {
1370 error = xasprintf("field %s missing value", name);
1371 goto out;
1372 }
1373
1374 if (!strcmp(name, "command_bucket_id")) {
1375 if (!(fields & F_COMMAND_BUCKET_ID)) {
1376 error = xstrdup("command bucket id is not needed");
1377 goto out;
1378 }
1379 if (!strcmp(value, "all")) {
1380 gm->command_bucket_id = OFPG15_BUCKET_ALL;
1381 } else if (!strcmp(value, "first")) {
1382 gm->command_bucket_id = OFPG15_BUCKET_FIRST;
1383 } else if (!strcmp(value, "last")) {
1384 gm->command_bucket_id = OFPG15_BUCKET_LAST;
1385 } else {
1386 error = str_to_u32(value, &gm->command_bucket_id);
1387 if (error) {
1388 goto out;
1389 }
1390 if (gm->command_bucket_id > OFPG15_BUCKET_MAX
1391 && (gm->command_bucket_id != OFPG15_BUCKET_FIRST
1392 && gm->command_bucket_id != OFPG15_BUCKET_LAST
1393 && gm->command_bucket_id != OFPG15_BUCKET_ALL)) {
1394 error = xasprintf("invalid command bucket id %"PRIu32,
1395 gm->command_bucket_id);
1396 goto out;
1397 }
1398 }
1399 if (gm->command_bucket_id == OFPG15_BUCKET_ALL
1400 && !(fields & F_COMMAND_BUCKET_ID_ALL)) {
1401 error = xstrdup("command_bucket_id=all is not permitted");
1402 goto out;
1403 }
1404 had_command_bucket_id = true;
1405 } else if (!strcmp(name, "group_id")) {
1406 if(!strcmp(value, "all")) {
1407 gm->group_id = OFPG_ALL;
1408 } else {
1409 error = str_to_u32(value, &gm->group_id);
1410 if (error) {
1411 goto out;
1412 }
1413 if (gm->group_id != OFPG_ALL && gm->group_id > OFPG_MAX) {
1414 error = xasprintf("invalid group id %"PRIu32,
1415 gm->group_id);
1416 goto out;
1417 }
1418 }
1419 } else if (!strcmp(name, "type")){
1420 if (!(fields & F_GROUP_TYPE)) {
1421 error = xstrdup("type is not needed");
1422 goto out;
1423 }
1424 if (!strcmp(value, "all")) {
1425 gm->type = OFPGT11_ALL;
1426 } else if (!strcmp(value, "select")) {
1427 gm->type = OFPGT11_SELECT;
1428 } else if (!strcmp(value, "indirect")) {
1429 gm->type = OFPGT11_INDIRECT;
1430 } else if (!strcmp(value, "ff") ||
1431 !strcmp(value, "fast_failover")) {
1432 gm->type = OFPGT11_FF;
1433 } else {
1434 error = xasprintf("invalid group type %s", value);
1435 goto out;
1436 }
1437 had_type = true;
1438 } else if (!strcmp(name, "selection_method")) {
1439 if (!(fields & F_GROUP_TYPE)) {
1440 error = xstrdup("selection method is not needed");
1441 goto out;
1442 }
1443 if (strlen(value) >= NTR_MAX_SELECTION_METHOD_LEN) {
1444 error = xasprintf("selection method is longer than %u"
1445 " bytes long",
1446 NTR_MAX_SELECTION_METHOD_LEN - 1);
1447 goto out;
1448 }
1449 memset(gm->props.selection_method, '\0',
1450 NTR_MAX_SELECTION_METHOD_LEN);
1451 strcpy(gm->props.selection_method, value);
1452 *usable_protocols &= OFPUTIL_P_OF15_UP;
1453 } else if (!strcmp(name, "selection_method_param")) {
1454 if (!(fields & F_GROUP_TYPE)) {
1455 error = xstrdup("selection method param is not needed");
1456 goto out;
1457 }
1458 error = str_to_u64(value, &gm->props.selection_method_param);
1459 if (error) {
1460 goto out;
1461 }
1462 *usable_protocols &= OFPUTIL_P_OF15_UP;
1463 } else if (!strcmp(name, "fields")) {
1464 if (!(fields & F_GROUP_TYPE)) {
1465 error = xstrdup("fields are not needed");
1466 goto out;
1467 }
1468 error = parse_select_group_field(value, &gm->props.fields,
1469 usable_protocols);
1470 if (error) {
1471 goto out;
1472 }
1473 *usable_protocols &= OFPUTIL_P_OF15_UP;
1474 } else {
1475 error = xasprintf("unknown keyword %s", name);
1476 goto out;
1477 }
1478 }
1479 if (gm->group_id == OFPG_ANY) {
1480 error = xstrdup("must specify a group_id");
1481 goto out;
1482 }
1483 if (fields & F_GROUP_TYPE && !had_type) {
1484 error = xstrdup("must specify a type");
1485 goto out;
1486 }
1487
1488 if (fields & F_COMMAND_BUCKET_ID) {
1489 if (!(fields & F_COMMAND_BUCKET_ID_ALL || had_command_bucket_id)) {
1490 error = xstrdup("must specify a command bucket id");
1491 goto out;
1492 }
1493 } else if (had_command_bucket_id) {
1494 error = xstrdup("command bucket id is not needed");
1495 goto out;
1496 }
1497
1498 /* Now parse the buckets, if any. */
1499 while (bkt_str) {
1500 char *next_bkt_str;
1501
1502 bkt_str = strchr(bkt_str + 1, '=');
1503 if (!bkt_str) {
1504 error = xstrdup("must specify bucket content");
1505 goto out;
1506 }
1507 bkt_str++;
1508
1509 next_bkt_str = strstr(bkt_str, "bucket=");
1510 if (next_bkt_str) {
1511 *next_bkt_str = '\0';
1512 }
1513
1514 bucket = xzalloc(sizeof(struct ofputil_bucket));
1515 error = parse_bucket_str(bucket, bkt_str, gm->type, usable_protocols);
1516 if (error) {
1517 free(bucket);
1518 goto out;
1519 }
1520 list_push_back(&gm->buckets, &bucket->list_node);
1521
1522 if (gm->type != OFPGT11_SELECT && bucket->weight) {
1523 error = xstrdup("Only select groups can have bucket weights.");
1524 goto out;
1525 }
1526
1527 bkt_str = next_bkt_str;
1528 }
1529 if (gm->type == OFPGT11_INDIRECT && !list_is_short(&gm->buckets)) {
1530 error = xstrdup("Indirect groups can have at most one bucket.");
1531 goto out;
1532 }
1533
1534 return NULL;
1535 out:
1536 ofputil_bucket_list_destroy(&gm->buckets);
1537 return error;
1538 }
1539
1540 char * OVS_WARN_UNUSED_RESULT
1541 parse_ofp_group_mod_str(struct ofputil_group_mod *gm, uint16_t command,
1542 const char *str_,
1543 enum ofputil_protocol *usable_protocols)
1544 {
1545 char *string = xstrdup(str_);
1546 char *error = parse_ofp_group_mod_str__(gm, command, string,
1547 usable_protocols);
1548 free(string);
1549
1550 if (error) {
1551 ofputil_bucket_list_destroy(&gm->buckets);
1552 }
1553 return error;
1554 }
1555
1556 char * OVS_WARN_UNUSED_RESULT
1557 parse_ofp_group_mod_file(const char *file_name, uint16_t command,
1558 struct ofputil_group_mod **gms, size_t *n_gms,
1559 enum ofputil_protocol *usable_protocols)
1560 {
1561 size_t allocated_gms;
1562 int line_number;
1563 FILE *stream;
1564 struct ds s;
1565
1566 *gms = NULL;
1567 *n_gms = 0;
1568
1569 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1570 if (stream == NULL) {
1571 return xasprintf("%s: open failed (%s)",
1572 file_name, ovs_strerror(errno));
1573 }
1574
1575 allocated_gms = *n_gms;
1576 ds_init(&s);
1577 line_number = 0;
1578 *usable_protocols = OFPUTIL_P_OF11_UP;
1579 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
1580 enum ofputil_protocol usable;
1581 char *error;
1582
1583 if (*n_gms >= allocated_gms) {
1584 struct ofputil_group_mod *new_gms;
1585 size_t i;
1586
1587 new_gms = x2nrealloc(*gms, &allocated_gms, sizeof **gms);
1588 for (i = 0; i < *n_gms; i++) {
1589 list_moved(&new_gms[i].buckets, &(*gms)[i].buckets);
1590 }
1591 *gms = new_gms;
1592 }
1593 error = parse_ofp_group_mod_str(&(*gms)[*n_gms], command, ds_cstr(&s),
1594 &usable);
1595 if (error) {
1596 size_t i;
1597
1598 for (i = 0; i < *n_gms; i++) {
1599 ofputil_bucket_list_destroy(&(*gms)[i].buckets);
1600 }
1601 free(*gms);
1602 *gms = NULL;
1603 *n_gms = 0;
1604
1605 ds_destroy(&s);
1606 if (stream != stdin) {
1607 fclose(stream);
1608 }
1609
1610 return xasprintf("%s:%d: %s", file_name, line_number, error);
1611 }
1612 *usable_protocols &= usable;
1613 *n_gms += 1;
1614 }
1615
1616 ds_destroy(&s);
1617 if (stream != stdin) {
1618 fclose(stream);
1619 }
1620 return NULL;
1621 }
1622
1623 char * OVS_WARN_UNUSED_RESULT
1624 parse_ofp_geneve_table_mod_str(struct ofputil_geneve_table_mod *gtm,
1625 uint16_t command, const char *s,
1626 enum ofputil_protocol *usable_protocols)
1627 {
1628 *usable_protocols = OFPUTIL_P_NXM_OXM_ANY;
1629
1630 gtm->command = command;
1631 list_init(&gtm->mappings);
1632
1633 while (*s) {
1634 struct ofputil_geneve_map *map = xmalloc(sizeof *map);
1635 int n;
1636
1637 if (*s == ',') {
1638 s++;
1639 }
1640
1641 list_push_back(&gtm->mappings, &map->list_node);
1642
1643 if (!ovs_scan(s, "{class=%"SCNi16",type=%"SCNi8",len=%"SCNi8"}->tun_metadata%"SCNi16"%n",
1644 &map->option_class, &map->option_type, &map->option_len,
1645 &map->index, &n)) {
1646 ofputil_uninit_geneve_table(&gtm->mappings);
1647 return xstrdup("invalid geneve mapping");
1648 }
1649
1650 s += n;
1651 }
1652
1653 return NULL;
1654 }