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