]> git.proxmox.com Git - ovs.git/blob - lib/ofp-parse.c
OF 1.1 pop vlan compatibility.
[ovs.git] / lib / ofp-parse.c
1 /*
2 * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18
19 #include "ofp-parse.h"
20
21 #include <ctype.h>
22 #include <errno.h>
23 #include <stdlib.h>
24
25 #include "bundle.h"
26 #include "byte-order.h"
27 #include "dynamic-string.h"
28 #include "learn.h"
29 #include "meta-flow.h"
30 #include "multipath.h"
31 #include "netdev.h"
32 #include "nx-match.h"
33 #include "ofp-actions.h"
34 #include "ofp-util.h"
35 #include "ofpbuf.h"
36 #include "openflow/openflow.h"
37 #include "ovs-thread.h"
38 #include "packets.h"
39 #include "simap.h"
40 #include "socket-util.h"
41 #include "vconn.h"
42
43 /* Parses 'str' as an 8-bit unsigned integer into '*valuep'.
44 *
45 * 'name' describes the value parsed in an error message, if any.
46 *
47 * Returns NULL if successful, otherwise a malloc()'d string describing the
48 * error. The caller is responsible for freeing the returned string. */
49 static char * 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 static char * 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 static char * 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 static char * 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 static char * 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 static char * WARN_UNUSED_RESULT
148 str_to_mac(const char *str, uint8_t mac[6])
149 {
150 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
151 != ETH_ADDR_SCAN_COUNT) {
152 return xasprintf("invalid mac address %s", str);
153 }
154 return NULL;
155 }
156
157 /* Parses 'str' as an IP address into '*ip'.
158 *
159 * Returns NULL if successful, otherwise a malloc()'d string describing the
160 * error. The caller is responsible for freeing the returned string. */
161 static char * WARN_UNUSED_RESULT
162 str_to_ip(const char *str, ovs_be32 *ip)
163 {
164 struct in_addr in_addr;
165
166 if (lookup_ip(str, &in_addr)) {
167 return xasprintf("%s: could not convert to IP address", str);
168 }
169 *ip = in_addr.s_addr;
170 return NULL;
171 }
172
173 /* Parses 'arg' as the argument to an "enqueue" action, and appends such an
174 * action to 'ofpacts'.
175 *
176 * Returns NULL if successful, otherwise a malloc()'d string describing the
177 * error. The caller is responsible for freeing the returned string. */
178 static char * WARN_UNUSED_RESULT
179 parse_enqueue(char *arg, struct ofpbuf *ofpacts)
180 {
181 char *sp = NULL;
182 char *port = strtok_r(arg, ":q", &sp);
183 char *queue = strtok_r(NULL, "", &sp);
184 struct ofpact_enqueue *enqueue;
185
186 if (port == NULL || queue == NULL) {
187 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
188 }
189
190 enqueue = ofpact_put_ENQUEUE(ofpacts);
191 if (!ofputil_port_from_string(port, &enqueue->port)) {
192 return xasprintf("%s: enqueue to unknown port", port);
193 }
194 return str_to_u32(queue, &enqueue->queue);
195 }
196
197 /* Parses 'arg' as the argument to an "output" action, and appends such an
198 * action to 'ofpacts'.
199 *
200 * Returns NULL if successful, otherwise a malloc()'d string describing the
201 * error. The caller is responsible for freeing the returned string. */
202 static char * WARN_UNUSED_RESULT
203 parse_output(const char *arg, struct ofpbuf *ofpacts)
204 {
205 if (strchr(arg, '[')) {
206 struct ofpact_output_reg *output_reg;
207
208 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
209 output_reg->max_len = UINT16_MAX;
210 return mf_parse_subfield(&output_reg->src, arg);
211 } else {
212 struct ofpact_output *output;
213
214 output = ofpact_put_OUTPUT(ofpacts);
215 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
216 if (!ofputil_port_from_string(arg, &output->port)) {
217 return xasprintf("%s: output to unknown port", arg);
218 }
219 return NULL;
220 }
221 }
222
223 /* Parses 'arg' as the argument to an "resubmit" action, and appends such an
224 * action to 'ofpacts'.
225 *
226 * Returns NULL if successful, otherwise a malloc()'d string describing the
227 * error. The caller is responsible for freeing the returned string. */
228 static char * WARN_UNUSED_RESULT
229 parse_resubmit(char *arg, struct ofpbuf *ofpacts)
230 {
231 struct ofpact_resubmit *resubmit;
232 char *in_port_s, *table_s;
233
234 resubmit = ofpact_put_RESUBMIT(ofpacts);
235
236 in_port_s = strsep(&arg, ",");
237 if (in_port_s && in_port_s[0]) {
238 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
239 return xasprintf("%s: resubmit to unknown port", in_port_s);
240 }
241 } else {
242 resubmit->in_port = OFPP_IN_PORT;
243 }
244
245 table_s = strsep(&arg, ",");
246 if (table_s && table_s[0]) {
247 uint32_t table_id = 0;
248 char *error;
249
250 error = str_to_u32(table_s, &table_id);
251 if (error) {
252 return error;
253 }
254 resubmit->table_id = table_id;
255 } else {
256 resubmit->table_id = 255;
257 }
258
259 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
260 return xstrdup("at least one \"in_port\" or \"table\" must be "
261 "specified on resubmit");
262 }
263 return NULL;
264 }
265
266 /* Parses 'arg' as the argument to a "note" action, and appends such an action
267 * to 'ofpacts'.
268 *
269 * Returns NULL if successful, otherwise a malloc()'d string describing the
270 * error. The caller is responsible for freeing the returned string. */
271 static char * WARN_UNUSED_RESULT
272 parse_note(const char *arg, struct ofpbuf *ofpacts)
273 {
274 struct ofpact_note *note;
275
276 note = ofpact_put_NOTE(ofpacts);
277 while (*arg != '\0') {
278 uint8_t byte;
279 bool ok;
280
281 if (*arg == '.') {
282 arg++;
283 }
284 if (*arg == '\0') {
285 break;
286 }
287
288 byte = hexits_value(arg, 2, &ok);
289 if (!ok) {
290 return xstrdup("bad hex digit in `note' argument");
291 }
292 ofpbuf_put(ofpacts, &byte, 1);
293
294 note = ofpacts->l2;
295 note->length++;
296
297 arg += 2;
298 }
299 ofpact_update_len(ofpacts, &note->ofpact);
300 return NULL;
301 }
302
303 /* Parses 'arg' as the argument to a "fin_timeout" action, and appends such an
304 * action to 'ofpacts'.
305 *
306 * Returns NULL if successful, otherwise a malloc()'d string describing the
307 * error. The caller is responsible for freeing the returned string. */
308 static char * WARN_UNUSED_RESULT
309 parse_fin_timeout(struct ofpbuf *b, char *arg)
310 {
311 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(b);
312 char *key, *value;
313
314 while (ofputil_parse_key_value(&arg, &key, &value)) {
315 char *error;
316
317 if (!strcmp(key, "idle_timeout")) {
318 error = str_to_u16(value, key, &oft->fin_idle_timeout);
319 } else if (!strcmp(key, "hard_timeout")) {
320 error = str_to_u16(value, key, &oft->fin_hard_timeout);
321 } else {
322 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
323 key);
324 }
325
326 if (error) {
327 return error;
328 }
329 }
330 return NULL;
331 }
332
333 /* Parses 'arg' as the argument to a "controller" action, and appends such an
334 * action to 'ofpacts'.
335 *
336 * Returns NULL if successful, otherwise a malloc()'d string describing the
337 * error. The caller is responsible for freeing the returned string. */
338 static char * WARN_UNUSED_RESULT
339 parse_controller(struct ofpbuf *b, char *arg)
340 {
341 enum ofp_packet_in_reason reason = OFPR_ACTION;
342 uint16_t controller_id = 0;
343 uint16_t max_len = UINT16_MAX;
344
345 if (!arg[0]) {
346 /* Use defaults. */
347 } else if (strspn(arg, "0123456789") == strlen(arg)) {
348 char *error = str_to_u16(arg, "max_len", &max_len);
349 if (error) {
350 return error;
351 }
352 } else {
353 char *name, *value;
354
355 while (ofputil_parse_key_value(&arg, &name, &value)) {
356 if (!strcmp(name, "reason")) {
357 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
358 return xasprintf("unknown reason \"%s\"", value);
359 }
360 } else if (!strcmp(name, "max_len")) {
361 char *error = str_to_u16(value, "max_len", &max_len);
362 if (error) {
363 return error;
364 }
365 } else if (!strcmp(name, "id")) {
366 char *error = str_to_u16(value, "id", &controller_id);
367 if (error) {
368 return error;
369 }
370 } else {
371 return xasprintf("unknown key \"%s\" parsing controller "
372 "action", name);
373 }
374 }
375 }
376
377 if (reason == OFPR_ACTION && controller_id == 0) {
378 struct ofpact_output *output;
379
380 output = ofpact_put_OUTPUT(b);
381 output->port = OFPP_CONTROLLER;
382 output->max_len = max_len;
383 } else {
384 struct ofpact_controller *controller;
385
386 controller = ofpact_put_CONTROLLER(b);
387 controller->max_len = max_len;
388 controller->reason = reason;
389 controller->controller_id = controller_id;
390 }
391
392 return NULL;
393 }
394
395 static void
396 parse_noargs_dec_ttl(struct ofpbuf *b)
397 {
398 struct ofpact_cnt_ids *ids;
399 uint16_t id = 0;
400
401 ids = ofpact_put_DEC_TTL(b);
402 ofpbuf_put(b, &id, sizeof id);
403 ids = b->l2;
404 ids->n_controllers++;
405 ofpact_update_len(b, &ids->ofpact);
406 }
407
408 /* Parses 'arg' as the argument to a "dec_ttl" action, and appends such an
409 * action to 'ofpacts'.
410 *
411 * Returns NULL if successful, otherwise a malloc()'d string describing the
412 * error. The caller is responsible for freeing the returned string. */
413 static char * WARN_UNUSED_RESULT
414 parse_dec_ttl(struct ofpbuf *b, char *arg)
415 {
416 if (*arg == '\0') {
417 parse_noargs_dec_ttl(b);
418 } else {
419 struct ofpact_cnt_ids *ids;
420 char *cntr;
421
422 ids = ofpact_put_DEC_TTL(b);
423 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
424 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
425 cntr = strtok_r(NULL, ", ", &arg)) {
426 uint16_t id = atoi(cntr);
427
428 ofpbuf_put(b, &id, sizeof id);
429 ids = b->l2;
430 ids->n_controllers++;
431 }
432 if (!ids->n_controllers) {
433 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
434 "id.");
435 }
436 ofpact_update_len(b, &ids->ofpact);
437 }
438 return NULL;
439 }
440
441 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
442 * action to 'ofpacts'.
443 *
444 * Returns NULL if successful, otherwise a malloc()'d string describing the
445 * error. The caller is responsible for freeing the returned string. */
446 static char * WARN_UNUSED_RESULT
447 parse_set_mpls_ttl(struct ofpbuf *b, const char *arg)
448 {
449 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(b);
450
451 if (*arg == '\0') {
452 return xstrdup("parse_set_mpls_ttl: expected ttl.");
453 }
454
455 mpls_ttl->ttl = atoi(arg);
456 return NULL;
457 }
458
459 /* Parses a "set_field" action with argument 'arg', appending the parsed
460 * action to 'ofpacts'.
461 *
462 * Returns NULL if successful, otherwise a malloc()'d string describing the
463 * error. The caller is responsible for freeing the returned string. */
464 static char * WARN_UNUSED_RESULT
465 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
466 enum ofputil_protocol *usable_protocols)
467 {
468 struct ofpact_reg_load *load = ofpact_put_REG_LOAD(ofpacts);
469 char *value;
470 char *delim;
471 char *key;
472 const struct mf_field *mf;
473 char *error;
474 union mf_value mf_value;
475
476 value = arg;
477 delim = strstr(arg, "->");
478 if (!delim) {
479 return xasprintf("%s: missing `->'", arg);
480 }
481 if (strlen(delim) <= strlen("->")) {
482 return xasprintf("%s: missing field name following `->'", arg);
483 }
484
485 key = delim + strlen("->");
486 mf = mf_from_name(key);
487 if (!mf) {
488 return xasprintf("%s is not a valid OXM field name", key);
489 }
490 if (!mf->writable) {
491 return xasprintf("%s is read-only", key);
492 }
493
494 delim[0] = '\0';
495 error = mf_parse_value(mf, value, &mf_value);
496 if (error) {
497 return error;
498 }
499 if (!mf_is_value_valid(mf, &mf_value)) {
500 return xasprintf("%s is not a valid value for field %s", value, key);
501 }
502 ofpact_set_field_init(load, mf, &mf_value);
503
504 *usable_protocols &= mf->usable_protocols;
505 return NULL;
506 }
507
508 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
509 * action to 'ofpacts'.
510 *
511 * Returns NULL if successful, otherwise a malloc()'d string describing the
512 * error. The caller is responsible for freeing the returned string. */
513 static char * WARN_UNUSED_RESULT
514 set_field_parse(const char *arg, struct ofpbuf *ofpacts,
515 enum ofputil_protocol *usable_protocols)
516 {
517 char *copy = xstrdup(arg);
518 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
519 free(copy);
520 return error;
521 }
522
523 /* Parses 'arg' as the argument to a "write_metadata" instruction, and appends
524 * such an action to 'ofpacts'.
525 *
526 * Returns NULL if successful, otherwise a malloc()'d string describing the
527 * error. The caller is responsible for freeing the returned string. */
528 static char * WARN_UNUSED_RESULT
529 parse_metadata(struct ofpbuf *b, char *arg)
530 {
531 struct ofpact_metadata *om;
532 char *mask = strchr(arg, '/');
533
534 om = ofpact_put_WRITE_METADATA(b);
535
536 if (mask) {
537 char *error;
538
539 *mask = '\0';
540 error = str_to_be64(mask + 1, &om->mask);
541 if (error) {
542 return error;
543 }
544 } else {
545 om->mask = OVS_BE64_MAX;
546 }
547
548 return str_to_be64(arg, &om->metadata);
549 }
550
551 /* Parses 'arg' as the argument to a "sample" action, and appends such an
552 * action to 'ofpacts'.
553 *
554 * Returns NULL if successful, otherwise a malloc()'d string describing the
555 * error. The caller is responsible for freeing the returned string. */
556 static char * WARN_UNUSED_RESULT
557 parse_sample(struct ofpbuf *b, char *arg)
558 {
559 struct ofpact_sample *os = ofpact_put_SAMPLE(b);
560 char *key, *value;
561
562 while (ofputil_parse_key_value(&arg, &key, &value)) {
563 char *error = NULL;
564
565 if (!strcmp(key, "probability")) {
566 error = str_to_u16(value, "probability", &os->probability);
567 if (!error && os->probability == 0) {
568 error = xasprintf("invalid probability value \"%s\"", value);
569 }
570 } else if (!strcmp(key, "collector_set_id")) {
571 error = str_to_u32(value, &os->collector_set_id);
572 } else if (!strcmp(key, "obs_domain_id")) {
573 error = str_to_u32(value, &os->obs_domain_id);
574 } else if (!strcmp(key, "obs_point_id")) {
575 error = str_to_u32(value, &os->obs_point_id);
576 } else {
577 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
578 key);
579 }
580 if (error) {
581 return error;
582 }
583 }
584 if (os->probability == 0) {
585 return xstrdup("non-zero \"probability\" must be specified on sample");
586 }
587 return NULL;
588 }
589
590 /* Parses 'arg' as the argument to action 'code', and appends such an action to
591 * 'ofpacts'.
592 *
593 * Returns NULL if successful, otherwise a malloc()'d string describing the
594 * error. The caller is responsible for freeing the returned string. */
595 static char * WARN_UNUSED_RESULT
596 parse_named_action(enum ofputil_action_code code,
597 char *arg, struct ofpbuf *ofpacts,
598 enum ofputil_protocol *usable_protocols)
599 {
600 size_t orig_size = ofpacts->size;
601 struct ofpact_tunnel *tunnel;
602 struct ofpact_vlan_vid *vlan_vid;
603 struct ofpact_vlan_pcp *vlan_pcp;
604 char *error = NULL;
605 uint16_t ethertype = 0;
606 uint16_t vid = 0;
607 uint8_t tos = 0, ecn, ttl;
608 uint8_t pcp = 0;
609
610 switch (code) {
611 case OFPUTIL_ACTION_INVALID:
612 NOT_REACHED();
613
614 case OFPUTIL_OFPAT10_OUTPUT:
615 case OFPUTIL_OFPAT11_OUTPUT:
616 error = parse_output(arg, ofpacts);
617 break;
618
619 case OFPUTIL_OFPAT10_SET_VLAN_VID:
620 case OFPUTIL_OFPAT11_SET_VLAN_VID:
621 error = str_to_u16(arg, "VLAN VID", &vid);
622 if (error) {
623 return error;
624 }
625
626 if (vid & ~VLAN_VID_MASK) {
627 return xasprintf("%s: not a valid VLAN VID", arg);
628 }
629 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
630 vlan_vid->vlan_vid = vid;
631 vlan_vid->ofpact.compat = code;
632 vlan_vid->push_vlan_if_needed = code == OFPUTIL_OFPAT10_SET_VLAN_VID;
633 break;
634
635 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
636 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
637 error = str_to_u8(arg, "VLAN PCP", &pcp);
638 if (error) {
639 return error;
640 }
641
642 if (pcp & ~7) {
643 return xasprintf("%s: not a valid VLAN PCP", arg);
644 }
645 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
646 vlan_pcp->vlan_pcp = pcp;
647 vlan_pcp->ofpact.compat = code;
648 vlan_pcp->push_vlan_if_needed = code == OFPUTIL_OFPAT10_SET_VLAN_PCP;
649 break;
650
651 case OFPUTIL_OFPAT12_SET_FIELD:
652 return set_field_parse(arg, ofpacts, usable_protocols);
653
654 case OFPUTIL_OFPAT10_STRIP_VLAN:
655 case OFPUTIL_OFPAT11_POP_VLAN:
656 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.compat = code;
657 break;
658
659 case OFPUTIL_OFPAT11_PUSH_VLAN:
660 *usable_protocols &= OFPUTIL_P_OF11_UP;
661 error = str_to_u16(arg, "ethertype", &ethertype);
662 if (error) {
663 return error;
664 }
665
666 if (ethertype != ETH_TYPE_VLAN_8021Q) {
667 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
668 return xasprintf("%s: not a valid VLAN ethertype", arg);
669 }
670
671 ofpact_put_PUSH_VLAN(ofpacts);
672 break;
673
674 case OFPUTIL_OFPAT11_SET_QUEUE:
675 error = str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
676 break;
677
678 case OFPUTIL_OFPAT10_SET_DL_SRC:
679 case OFPUTIL_OFPAT11_SET_DL_SRC:
680 error = str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
681 break;
682
683 case OFPUTIL_OFPAT10_SET_DL_DST:
684 case OFPUTIL_OFPAT11_SET_DL_DST:
685 error = str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
686 break;
687
688 case OFPUTIL_OFPAT10_SET_NW_SRC:
689 case OFPUTIL_OFPAT11_SET_NW_SRC:
690 error = str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
691 break;
692
693 case OFPUTIL_OFPAT10_SET_NW_DST:
694 case OFPUTIL_OFPAT11_SET_NW_DST:
695 error = str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
696 break;
697
698 case OFPUTIL_OFPAT10_SET_NW_TOS:
699 case OFPUTIL_OFPAT11_SET_NW_TOS:
700 error = str_to_u8(arg, "TOS", &tos);
701 if (error) {
702 return error;
703 }
704
705 if (tos & ~IP_DSCP_MASK) {
706 return xasprintf("%s: not a valid TOS", arg);
707 }
708 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
709 break;
710
711 case OFPUTIL_OFPAT11_SET_NW_ECN:
712 error = str_to_u8(arg, "ECN", &ecn);
713 if (error) {
714 return error;
715 }
716
717 if (ecn & ~IP_ECN_MASK) {
718 return xasprintf("%s: not a valid ECN", arg);
719 }
720 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
721 break;
722
723 case OFPUTIL_OFPAT11_SET_NW_TTL:
724 error = str_to_u8(arg, "TTL", &ttl);
725 if (error) {
726 return error;
727 }
728
729 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
730 break;
731
732 case OFPUTIL_OFPAT11_DEC_NW_TTL:
733 NOT_REACHED();
734
735 case OFPUTIL_OFPAT10_SET_TP_SRC:
736 case OFPUTIL_OFPAT11_SET_TP_SRC:
737 error = str_to_u16(arg, "source port",
738 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
739 break;
740
741 case OFPUTIL_OFPAT10_SET_TP_DST:
742 case OFPUTIL_OFPAT11_SET_TP_DST:
743 error = str_to_u16(arg, "destination port",
744 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
745 break;
746
747 case OFPUTIL_OFPAT10_ENQUEUE:
748 error = parse_enqueue(arg, ofpacts);
749 break;
750
751 case OFPUTIL_NXAST_RESUBMIT:
752 error = parse_resubmit(arg, ofpacts);
753 break;
754
755 case OFPUTIL_NXAST_SET_TUNNEL:
756 case OFPUTIL_NXAST_SET_TUNNEL64:
757 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
758 tunnel->ofpact.compat = code;
759 error = str_to_u64(arg, &tunnel->tun_id);
760 break;
761
762 case OFPUTIL_NXAST_WRITE_METADATA:
763 error = parse_metadata(ofpacts, arg);
764 break;
765
766 case OFPUTIL_NXAST_SET_QUEUE:
767 error = str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
768 break;
769
770 case OFPUTIL_NXAST_POP_QUEUE:
771 ofpact_put_POP_QUEUE(ofpacts);
772 break;
773
774 case OFPUTIL_NXAST_REG_MOVE:
775 error = nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts), arg);
776 break;
777
778 case OFPUTIL_NXAST_REG_LOAD:
779 error = nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts), arg);
780 break;
781
782 case OFPUTIL_NXAST_NOTE:
783 error = parse_note(arg, ofpacts);
784 break;
785
786 case OFPUTIL_NXAST_MULTIPATH:
787 error = multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
788 break;
789
790 case OFPUTIL_NXAST_BUNDLE:
791 error = bundle_parse(arg, ofpacts);
792 break;
793
794 case OFPUTIL_NXAST_BUNDLE_LOAD:
795 error = bundle_parse_load(arg, ofpacts);
796 break;
797
798 case OFPUTIL_NXAST_RESUBMIT_TABLE:
799 case OFPUTIL_NXAST_OUTPUT_REG:
800 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
801 NOT_REACHED();
802
803 case OFPUTIL_NXAST_LEARN:
804 error = learn_parse(arg, ofpacts);
805 break;
806
807 case OFPUTIL_NXAST_EXIT:
808 ofpact_put_EXIT(ofpacts);
809 break;
810
811 case OFPUTIL_NXAST_DEC_TTL:
812 error = parse_dec_ttl(ofpacts, arg);
813 break;
814
815 case OFPUTIL_NXAST_SET_MPLS_TTL:
816 case OFPUTIL_OFPAT11_SET_MPLS_TTL:
817 error = parse_set_mpls_ttl(ofpacts, arg);
818 break;
819
820 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
821 case OFPUTIL_NXAST_DEC_MPLS_TTL:
822 ofpact_put_DEC_MPLS_TTL(ofpacts);
823 break;
824
825 case OFPUTIL_NXAST_FIN_TIMEOUT:
826 error = parse_fin_timeout(ofpacts, arg);
827 break;
828
829 case OFPUTIL_NXAST_CONTROLLER:
830 error = parse_controller(ofpacts, arg);
831 break;
832
833 case OFPUTIL_OFPAT11_PUSH_MPLS:
834 case OFPUTIL_NXAST_PUSH_MPLS:
835 error = str_to_u16(arg, "push_mpls", &ethertype);
836 if (!error) {
837 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
838 }
839 break;
840
841 case OFPUTIL_OFPAT11_POP_MPLS:
842 case OFPUTIL_NXAST_POP_MPLS:
843 error = str_to_u16(arg, "pop_mpls", &ethertype);
844 if (!error) {
845 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
846 }
847 break;
848
849 case OFPUTIL_OFPAT11_GROUP:
850 error = str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
851 break;
852
853 case OFPUTIL_NXAST_STACK_PUSH:
854 error = nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
855 break;
856 case OFPUTIL_NXAST_STACK_POP:
857 error = nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
858 break;
859
860 case OFPUTIL_NXAST_SAMPLE:
861 error = parse_sample(ofpacts, arg);
862 break;
863 }
864
865 if (error) {
866 ofpacts->size = orig_size;
867 }
868 return error;
869 }
870
871 /* Parses action 'act', with argument 'arg', and appends a parsed version to
872 * 'ofpacts'.
873 *
874 * 'n_actions' specifies the number of actions already parsed (for proper
875 * handling of "drop" actions).
876 *
877 * Returns NULL if successful, otherwise a malloc()'d string describing the
878 * error. The caller is responsible for freeing the returned string. */
879 static char * WARN_UNUSED_RESULT
880 str_to_ofpact__(char *pos, char *act, char *arg,
881 struct ofpbuf *ofpacts, int n_actions,
882 enum ofputil_protocol *usable_protocols)
883 {
884 int code = ofputil_action_code_from_name(act);
885 if (code >= 0) {
886 return parse_named_action(code, arg, ofpacts, usable_protocols);
887 } else if (!strcasecmp(act, "drop")) {
888 if (n_actions) {
889 return xstrdup("Drop actions must not be preceded by other "
890 "actions");
891 } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
892 return xstrdup("Drop actions must not be followed by other "
893 "actions");
894 }
895 } else {
896 ofp_port_t port;
897 if (ofputil_port_from_string(act, &port)) {
898 ofpact_put_OUTPUT(ofpacts)->port = port;
899 } else {
900 return xasprintf("Unknown action: %s", act);
901 }
902 }
903
904 return NULL;
905 }
906
907 /* Parses 'str' as a series of actions, and appends them to 'ofpacts'.
908 *
909 * Returns NULL if successful, otherwise a malloc()'d string describing the
910 * error. The caller is responsible for freeing the returned string. */
911 static char * WARN_UNUSED_RESULT
912 str_to_ofpacts__(char *str, struct ofpbuf *ofpacts,
913 enum ofputil_protocol *usable_protocols)
914 {
915 size_t orig_size = ofpacts->size;
916 char *pos, *act, *arg;
917 int n_actions;
918
919 pos = str;
920 n_actions = 0;
921 while (ofputil_parse_key_value(&pos, &act, &arg)) {
922 char *error = str_to_ofpact__(pos, act, arg, ofpacts, n_actions,
923 usable_protocols);
924 if (error) {
925 ofpacts->size = orig_size;
926 return error;
927 }
928 n_actions++;
929 }
930
931 ofpact_pad(ofpacts);
932 return NULL;
933 }
934
935
936 /* Parses 'str' as a series of actions, and appends them to 'ofpacts'.
937 *
938 * Returns NULL if successful, otherwise a malloc()'d string describing the
939 * error. The caller is responsible for freeing the returned string. */
940 static char * WARN_UNUSED_RESULT
941 str_to_ofpacts(char *str, struct ofpbuf *ofpacts,
942 enum ofputil_protocol *usable_protocols)
943 {
944 size_t orig_size = ofpacts->size;
945 char *error_s;
946 enum ofperr error;
947
948 error_s = str_to_ofpacts__(str, ofpacts, usable_protocols);
949 if (error_s) {
950 return error_s;
951 }
952
953 error = ofpacts_verify(ofpacts->data, ofpacts->size);
954 if (error) {
955 ofpacts->size = orig_size;
956 return xstrdup("Incorrect action ordering");
957 }
958
959 return NULL;
960 }
961
962 /* Parses 'arg' as the argument to instruction 'type', and appends such an
963 * instruction to 'ofpacts'.
964 *
965 * Returns NULL if successful, otherwise a malloc()'d string describing the
966 * error. The caller is responsible for freeing the returned string. */
967 static char * WARN_UNUSED_RESULT
968 parse_named_instruction(enum ovs_instruction_type type,
969 char *arg, struct ofpbuf *ofpacts,
970 enum ofputil_protocol *usable_protocols)
971 {
972 char *error_s = NULL;
973 enum ofperr error;
974
975 *usable_protocols &= OFPUTIL_P_OF11_UP;
976
977 switch (type) {
978 case OVSINST_OFPIT11_APPLY_ACTIONS:
979 NOT_REACHED(); /* This case is handled by str_to_inst_ofpacts() */
980 break;
981
982 case OVSINST_OFPIT11_WRITE_ACTIONS: {
983 struct ofpact_nest *on;
984 size_t ofs;
985
986 ofpact_pad(ofpacts);
987 ofs = ofpacts->size;
988 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
989 offsetof(struct ofpact_nest, actions));
990 error_s = str_to_ofpacts__(arg, ofpacts, usable_protocols);
991
992 on = ofpbuf_at_assert(ofpacts, ofs, sizeof *on);
993 on->ofpact.len = ofpacts->size - ofs;
994
995 if (error_s) {
996 ofpacts->size = ofs;
997 }
998 break;
999 }
1000
1001 case OVSINST_OFPIT11_CLEAR_ACTIONS:
1002 ofpact_put_CLEAR_ACTIONS(ofpacts);
1003 break;
1004
1005 case OVSINST_OFPIT13_METER:
1006 *usable_protocols &= OFPUTIL_P_OF13_UP;
1007 error_s = str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
1008 break;
1009
1010 case OVSINST_OFPIT11_WRITE_METADATA:
1011 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
1012 error_s = parse_metadata(ofpacts, arg);
1013 break;
1014
1015 case OVSINST_OFPIT11_GOTO_TABLE: {
1016 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
1017 char *table_s = strsep(&arg, ",");
1018 if (!table_s || !table_s[0]) {
1019 return xstrdup("instruction goto-table needs table id");
1020 }
1021 error_s = str_to_u8(table_s, "table", &ogt->table_id);
1022 break;
1023 }
1024 }
1025
1026 if (error_s) {
1027 return error_s;
1028 }
1029
1030 /* If write_metadata is specified as an action AND an instruction, ofpacts
1031 could be invalid. */
1032 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1033 if (error) {
1034 return xstrdup("Incorrect instruction ordering");
1035 }
1036 return NULL;
1037 }
1038
1039 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
1040 *
1041 * Returns NULL if successful, otherwise a malloc()'d string describing the
1042 * error. The caller is responsible for freeing the returned string. */
1043 static char * WARN_UNUSED_RESULT
1044 str_to_inst_ofpacts(char *str, struct ofpbuf *ofpacts,
1045 enum ofputil_protocol *usable_protocols)
1046 {
1047 size_t orig_size = ofpacts->size;
1048 char *pos, *inst, *arg;
1049 int type;
1050 const char *prev_inst = NULL;
1051 int prev_type = -1;
1052 int n_actions = 0;
1053
1054 pos = str;
1055 while (ofputil_parse_key_value(&pos, &inst, &arg)) {
1056 type = ovs_instruction_type_from_name(inst);
1057 if (type < 0) {
1058 char *error = str_to_ofpact__(pos, inst, arg, ofpacts, n_actions,
1059 usable_protocols);
1060 if (error) {
1061 ofpacts->size = orig_size;
1062 return error;
1063 }
1064
1065 type = OVSINST_OFPIT11_APPLY_ACTIONS;
1066 if (prev_type == type) {
1067 n_actions++;
1068 continue;
1069 }
1070 } else if (type == OVSINST_OFPIT11_APPLY_ACTIONS) {
1071 ofpacts->size = orig_size;
1072 return xasprintf("%s isn't supported. Just write actions then "
1073 "it is interpreted as apply_actions", inst);
1074 } else {
1075 char *error = parse_named_instruction(type, arg, ofpacts,
1076 usable_protocols);
1077 if (error) {
1078 ofpacts->size = orig_size;
1079 return error;
1080 }
1081 }
1082
1083 if (type <= prev_type) {
1084 ofpacts->size = orig_size;
1085 if (type == prev_type) {
1086 return xasprintf("instruction %s may be specified only once",
1087 inst);
1088 } else {
1089 return xasprintf("instruction %s must be specified before %s",
1090 inst, prev_inst);
1091 }
1092 }
1093
1094 prev_inst = inst;
1095 prev_type = type;
1096 n_actions++;
1097 }
1098 ofpact_pad(ofpacts);
1099
1100 return NULL;
1101 }
1102
1103 struct protocol {
1104 const char *name;
1105 uint16_t dl_type;
1106 uint8_t nw_proto;
1107 };
1108
1109 static bool
1110 parse_protocol(const char *name, const struct protocol **p_out)
1111 {
1112 static const struct protocol protocols[] = {
1113 { "ip", ETH_TYPE_IP, 0 },
1114 { "arp", ETH_TYPE_ARP, 0 },
1115 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
1116 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
1117 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
1118 { "sctp", ETH_TYPE_IP, IPPROTO_SCTP },
1119 { "ipv6", ETH_TYPE_IPV6, 0 },
1120 { "ip6", ETH_TYPE_IPV6, 0 },
1121 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
1122 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
1123 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
1124 { "sctp6", ETH_TYPE_IPV6, IPPROTO_SCTP },
1125 { "rarp", ETH_TYPE_RARP, 0},
1126 { "mpls", ETH_TYPE_MPLS, 0 },
1127 { "mplsm", ETH_TYPE_MPLS_MCAST, 0 },
1128 };
1129 const struct protocol *p;
1130
1131 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
1132 if (!strcmp(p->name, name)) {
1133 *p_out = p;
1134 return true;
1135 }
1136 }
1137 *p_out = NULL;
1138 return false;
1139 }
1140
1141 /* Parses 's' as the (possibly masked) value of field 'mf', and updates
1142 * 'match' appropriately. Restricts the set of usable protocols to ones
1143 * supporting the parsed field.
1144 *
1145 * Returns NULL if successful, otherwise a malloc()'d string describing the
1146 * error. The caller is responsible for freeing the returned string. */
1147 static char * WARN_UNUSED_RESULT
1148 parse_field(const struct mf_field *mf, const char *s, struct match *match,
1149 enum ofputil_protocol *usable_protocols)
1150 {
1151 union mf_value value, mask;
1152 char *error;
1153
1154 error = mf_parse(mf, s, &value, &mask);
1155 if (!error) {
1156 *usable_protocols &= mf_set(mf, &value, &mask, match);
1157 }
1158 return error;
1159 }
1160
1161 static char * WARN_UNUSED_RESULT
1162 parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string,
1163 enum ofputil_protocol *usable_protocols,
1164 bool enforce_consistency)
1165 {
1166 enum {
1167 F_OUT_PORT = 1 << 0,
1168 F_ACTIONS = 1 << 1,
1169 F_TIMEOUT = 1 << 3,
1170 F_PRIORITY = 1 << 4,
1171 F_FLAGS = 1 << 5,
1172 } fields;
1173 char *save_ptr = NULL;
1174 char *act_str = NULL;
1175 char *name;
1176
1177 *usable_protocols = OFPUTIL_P_ANY;
1178
1179 switch (command) {
1180 case -1:
1181 fields = F_OUT_PORT;
1182 break;
1183
1184 case OFPFC_ADD:
1185 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
1186 break;
1187
1188 case OFPFC_DELETE:
1189 fields = F_OUT_PORT;
1190 break;
1191
1192 case OFPFC_DELETE_STRICT:
1193 fields = F_OUT_PORT | F_PRIORITY;
1194 break;
1195
1196 case OFPFC_MODIFY:
1197 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
1198 break;
1199
1200 case OFPFC_MODIFY_STRICT:
1201 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
1202 break;
1203
1204 default:
1205 NOT_REACHED();
1206 }
1207
1208 match_init_catchall(&fm->match);
1209 fm->priority = OFP_DEFAULT_PRIORITY;
1210 fm->cookie = htonll(0);
1211 fm->cookie_mask = htonll(0);
1212 if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
1213 /* For modify, by default, don't update the cookie. */
1214 fm->new_cookie = OVS_BE64_MAX;
1215 } else{
1216 fm->new_cookie = htonll(0);
1217 }
1218 fm->modify_cookie = false;
1219 fm->table_id = 0xff;
1220 fm->command = command;
1221 fm->idle_timeout = OFP_FLOW_PERMANENT;
1222 fm->hard_timeout = OFP_FLOW_PERMANENT;
1223 fm->buffer_id = UINT32_MAX;
1224 fm->out_port = OFPP_ANY;
1225 fm->flags = 0;
1226 fm->out_group = OFPG11_ANY;
1227 if (fields & F_ACTIONS) {
1228 act_str = strstr(string, "action");
1229 if (!act_str) {
1230 return xstrdup("must specify an action");
1231 }
1232 *act_str = '\0';
1233
1234 act_str = strchr(act_str + 1, '=');
1235 if (!act_str) {
1236 return xstrdup("must specify an action");
1237 }
1238
1239 act_str++;
1240 }
1241 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1242 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1243 const struct protocol *p;
1244 char *error = NULL;
1245
1246 if (parse_protocol(name, &p)) {
1247 match_set_dl_type(&fm->match, htons(p->dl_type));
1248 if (p->nw_proto) {
1249 match_set_nw_proto(&fm->match, p->nw_proto);
1250 }
1251 } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
1252 fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
1253 } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
1254 fm->flags |= OFPUTIL_FF_CHECK_OVERLAP;
1255 } else if (fields & F_FLAGS && !strcmp(name, "reset_counts")) {
1256 fm->flags |= OFPUTIL_FF_RESET_COUNTS;
1257 *usable_protocols &= OFPUTIL_P_OF12_UP;
1258 } else if (fields & F_FLAGS && !strcmp(name, "no_packet_counts")) {
1259 fm->flags |= OFPUTIL_FF_NO_PKT_COUNTS;
1260 *usable_protocols &= OFPUTIL_P_OF13_UP;
1261 } else if (fields & F_FLAGS && !strcmp(name, "no_byte_counts")) {
1262 fm->flags |= OFPUTIL_FF_NO_BYT_COUNTS;
1263 *usable_protocols &= OFPUTIL_P_OF13_UP;
1264 } else {
1265 char *value;
1266
1267 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1268 if (!value) {
1269 return xasprintf("field %s missing value", name);
1270 }
1271
1272 if (!strcmp(name, "table")) {
1273 error = str_to_u8(value, "table", &fm->table_id);
1274 if (fm->table_id != 0xff) {
1275 *usable_protocols &= OFPUTIL_P_TID;
1276 }
1277 } else if (!strcmp(name, "out_port")) {
1278 if (!ofputil_port_from_string(value, &fm->out_port)) {
1279 error = xasprintf("%s is not a valid OpenFlow port",
1280 value);
1281 }
1282 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
1283 uint16_t priority = 0;
1284
1285 error = str_to_u16(value, name, &priority);
1286 fm->priority = priority;
1287 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
1288 error = str_to_u16(value, name, &fm->idle_timeout);
1289 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
1290 error = str_to_u16(value, name, &fm->hard_timeout);
1291 } else if (!strcmp(name, "cookie")) {
1292 char *mask = strchr(value, '/');
1293
1294 if (mask) {
1295 /* A mask means we're searching for a cookie. */
1296 if (command == OFPFC_ADD) {
1297 return xstrdup("flow additions cannot use "
1298 "a cookie mask");
1299 }
1300 *mask = '\0';
1301 error = str_to_be64(value, &fm->cookie);
1302 if (error) {
1303 return error;
1304 }
1305 error = str_to_be64(mask + 1, &fm->cookie_mask);
1306
1307 /* Matching of the cookie is only supported through NXM or
1308 * OF1.1+. */
1309 if (fm->cookie_mask != htonll(0)) {
1310 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
1311 }
1312 } else {
1313 /* No mask means that the cookie is being set. */
1314 if (command != OFPFC_ADD && command != OFPFC_MODIFY
1315 && command != OFPFC_MODIFY_STRICT) {
1316 return xstrdup("cannot set cookie");
1317 }
1318 error = str_to_be64(value, &fm->new_cookie);
1319 fm->modify_cookie = true;
1320 }
1321 } else if (mf_from_name(name)) {
1322 error = parse_field(mf_from_name(name), value, &fm->match,
1323 usable_protocols);
1324 } else if (!strcmp(name, "duration")
1325 || !strcmp(name, "n_packets")
1326 || !strcmp(name, "n_bytes")
1327 || !strcmp(name, "idle_age")
1328 || !strcmp(name, "hard_age")) {
1329 /* Ignore these, so that users can feed the output of
1330 * "ovs-ofctl dump-flows" back into commands that parse
1331 * flows. */
1332 } else {
1333 error = xasprintf("unknown keyword %s", name);
1334 }
1335
1336 if (error) {
1337 return error;
1338 }
1339 }
1340 }
1341 /* Check for usable protocol interdependencies between match fields. */
1342 if (fm->match.flow.dl_type == htons(ETH_TYPE_IPV6)) {
1343 const struct flow_wildcards *wc = &fm->match.wc;
1344 /* Only NXM and OXM support matching L3 and L4 fields within IPv6.
1345 *
1346 * (IPv6 specific fields as well as arp_sha, arp_tha, nw_frag, and
1347 * nw_ttl are covered elsewhere so they don't need to be included in
1348 * this test too.)
1349 */
1350 if (wc->masks.nw_proto || wc->masks.nw_tos
1351 || wc->masks.tp_src || wc->masks.tp_dst) {
1352 *usable_protocols &= OFPUTIL_P_NXM_OXM_ANY;
1353 }
1354 }
1355 if (!fm->cookie_mask && fm->new_cookie == OVS_BE64_MAX
1356 && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
1357 /* On modifies without a mask, we are supposed to add a flow if
1358 * one does not exist. If a cookie wasn't been specified, use a
1359 * default of zero. */
1360 fm->new_cookie = htonll(0);
1361 }
1362 if (fields & F_ACTIONS) {
1363 struct ofpbuf ofpacts;
1364 char *error;
1365
1366 ofpbuf_init(&ofpacts, 32);
1367 error = str_to_inst_ofpacts(act_str, &ofpacts, usable_protocols);
1368 if (!error) {
1369 enum ofperr err;
1370
1371 err = ofpacts_check(ofpacts.data, ofpacts.size, &fm->match.flow,
1372 OFPP_MAX, 0, true);
1373 if (err) {
1374 if (!enforce_consistency &&
1375 err == OFPERR_OFPBAC_MATCH_INCONSISTENT) {
1376 /* Allow inconsistent actions to be used with OF 1.0. */
1377 *usable_protocols &= OFPUTIL_P_OF10_ANY;
1378 /* Try again, allowing for inconsistency.
1379 * XXX: As a side effect, logging may be duplicated. */
1380 err = ofpacts_check(ofpacts.data, ofpacts.size,
1381 &fm->match.flow, OFPP_MAX, 0, false);
1382 }
1383 if (err) {
1384 error = xasprintf("actions are invalid with specified match "
1385 "(%s)", ofperr_to_string(err));
1386 }
1387 }
1388 }
1389 if (error) {
1390 ofpbuf_uninit(&ofpacts);
1391 return error;
1392 }
1393
1394 fm->ofpacts_len = ofpacts.size;
1395 fm->ofpacts = ofpbuf_steal_data(&ofpacts);
1396 } else {
1397 fm->ofpacts_len = 0;
1398 fm->ofpacts = NULL;
1399 }
1400
1401 return NULL;
1402 }
1403
1404 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
1405 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
1406 * Returns the set of usable protocols in '*usable_protocols'.
1407 *
1408 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
1409 * constant for 'command'. To parse syntax for an OFPST_FLOW or
1410 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'.
1411 *
1412 * Returns NULL if successful, otherwise a malloc()'d string describing the
1413 * error. The caller is responsible for freeing the returned string. */
1414 char * WARN_UNUSED_RESULT
1415 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
1416 enum ofputil_protocol *usable_protocols,
1417 bool enforce_consistency)
1418 {
1419 char *string = xstrdup(str_);
1420 char *error;
1421
1422 error = parse_ofp_str__(fm, command, string, usable_protocols,
1423 enforce_consistency);
1424 if (error) {
1425 fm->ofpacts = NULL;
1426 fm->ofpacts_len = 0;
1427 }
1428
1429 free(string);
1430 return error;
1431 }
1432
1433 static char * WARN_UNUSED_RESULT
1434 parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string,
1435 struct ofpbuf *bands, int command,
1436 enum ofputil_protocol *usable_protocols)
1437 {
1438 enum {
1439 F_METER = 1 << 0,
1440 F_FLAGS = 1 << 1,
1441 F_BANDS = 1 << 2,
1442 } fields;
1443 char *save_ptr = NULL;
1444 char *band_str = NULL;
1445 char *name;
1446
1447 /* Meters require at least OF 1.3. */
1448 *usable_protocols = OFPUTIL_P_OF13_UP;
1449
1450 switch (command) {
1451 case -1:
1452 fields = F_METER;
1453 break;
1454
1455 case OFPMC13_ADD:
1456 fields = F_METER | F_FLAGS | F_BANDS;
1457 break;
1458
1459 case OFPMC13_DELETE:
1460 fields = F_METER;
1461 break;
1462
1463 case OFPMC13_MODIFY:
1464 fields = F_METER | F_FLAGS | F_BANDS;
1465 break;
1466
1467 default:
1468 NOT_REACHED();
1469 }
1470
1471 mm->command = command;
1472 mm->meter.meter_id = 0;
1473 mm->meter.flags = 0;
1474 if (fields & F_BANDS) {
1475 band_str = strstr(string, "band");
1476 if (!band_str) {
1477 return xstrdup("must specify bands");
1478 }
1479 *band_str = '\0';
1480
1481 band_str = strchr(band_str + 1, '=');
1482 if (!band_str) {
1483 return xstrdup("must specify bands");
1484 }
1485
1486 band_str++;
1487 }
1488 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1489 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1490
1491 if (fields & F_FLAGS && !strcmp(name, "kbps")) {
1492 mm->meter.flags |= OFPMF13_KBPS;
1493 } else if (fields & F_FLAGS && !strcmp(name, "pktps")) {
1494 mm->meter.flags |= OFPMF13_PKTPS;
1495 } else if (fields & F_FLAGS && !strcmp(name, "burst")) {
1496 mm->meter.flags |= OFPMF13_BURST;
1497 } else if (fields & F_FLAGS && !strcmp(name, "stats")) {
1498 mm->meter.flags |= OFPMF13_STATS;
1499 } else {
1500 char *value;
1501
1502 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1503 if (!value) {
1504 return xasprintf("field %s missing value", name);
1505 }
1506
1507 if (!strcmp(name, "meter")) {
1508 if (!strcmp(value, "all")) {
1509 mm->meter.meter_id = OFPM13_ALL;
1510 } else if (!strcmp(value, "controller")) {
1511 mm->meter.meter_id = OFPM13_CONTROLLER;
1512 } else if (!strcmp(value, "slowpath")) {
1513 mm->meter.meter_id = OFPM13_SLOWPATH;
1514 } else {
1515 char *error = str_to_u32(value, &mm->meter.meter_id);
1516 if (error) {
1517 return error;
1518 }
1519 if (mm->meter.meter_id > OFPM13_MAX) {
1520 return xasprintf("invalid value for %s", name);
1521 }
1522 }
1523 } else {
1524 return xasprintf("unknown keyword %s", name);
1525 }
1526 }
1527 }
1528 if (fields & F_METER && !mm->meter.meter_id) {
1529 return xstrdup("must specify 'meter'");
1530 }
1531 if (fields & F_FLAGS && !mm->meter.flags) {
1532 return xstrdup("meter must specify either 'kbps' or 'pktps'");
1533 }
1534
1535 if (fields & F_BANDS) {
1536 uint16_t n_bands = 0;
1537 struct ofputil_meter_band *band = NULL;
1538 int i;
1539
1540 for (name = strtok_r(band_str, "=, \t\r\n", &save_ptr); name;
1541 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1542
1543 char *value;
1544
1545 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1546 if (!value) {
1547 return xasprintf("field %s missing value", name);
1548 }
1549
1550 if (!strcmp(name, "type")) {
1551 /* Start a new band */
1552 band = ofpbuf_put_zeros(bands, sizeof *band);
1553 n_bands++;
1554
1555 if (!strcmp(value, "drop")) {
1556 band->type = OFPMBT13_DROP;
1557 } else if (!strcmp(value, "dscp_remark")) {
1558 band->type = OFPMBT13_DSCP_REMARK;
1559 } else {
1560 return xasprintf("field %s unknown value %s", name, value);
1561 }
1562 } else if (!band || !band->type) {
1563 return xstrdup("band must start with the 'type' keyword");
1564 } else if (!strcmp(name, "rate")) {
1565 char *error = str_to_u32(value, &band->rate);
1566 if (error) {
1567 return error;
1568 }
1569 } else if (!strcmp(name, "burst_size")) {
1570 char *error = str_to_u32(value, &band->burst_size);
1571 if (error) {
1572 return error;
1573 }
1574 } else if (!strcmp(name, "prec_level")) {
1575 char *error = str_to_u8(value, name, &band->prec_level);
1576 if (error) {
1577 return error;
1578 }
1579 } else {
1580 return xasprintf("unknown keyword %s", name);
1581 }
1582 }
1583 /* validate bands */
1584 if (!n_bands) {
1585 return xstrdup("meter must have bands");
1586 }
1587
1588 mm->meter.n_bands = n_bands;
1589 mm->meter.bands = ofpbuf_steal_data(bands);
1590
1591 for (i = 0; i < n_bands; ++i) {
1592 band = &mm->meter.bands[i];
1593
1594 if (!band->type) {
1595 return xstrdup("band must have 'type'");
1596 }
1597 if (band->type == OFPMBT13_DSCP_REMARK) {
1598 if (!band->prec_level) {
1599 return xstrdup("'dscp_remark' band must have"
1600 " 'prec_level'");
1601 }
1602 } else {
1603 if (band->prec_level) {
1604 return xstrdup("Only 'dscp_remark' band may have"
1605 " 'prec_level'");
1606 }
1607 }
1608 if (!band->rate) {
1609 return xstrdup("band must have 'rate'");
1610 }
1611 if (mm->meter.flags & OFPMF13_BURST) {
1612 if (!band->burst_size) {
1613 return xstrdup("band must have 'burst_size' "
1614 "when 'burst' flag is set");
1615 }
1616 } else {
1617 if (band->burst_size) {
1618 return xstrdup("band may have 'burst_size' only "
1619 "when 'burst' flag is set");
1620 }
1621 }
1622 }
1623 } else {
1624 mm->meter.n_bands = 0;
1625 mm->meter.bands = NULL;
1626 }
1627
1628 return NULL;
1629 }
1630
1631 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
1632 * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
1633 *
1634 * Returns NULL if successful, otherwise a malloc()'d string describing the
1635 * error. The caller is responsible for freeing the returned string. */
1636 char * WARN_UNUSED_RESULT
1637 parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_,
1638 int command, enum ofputil_protocol *usable_protocols)
1639 {
1640 struct ofpbuf bands;
1641 char *string;
1642 char *error;
1643
1644 ofpbuf_init(&bands, 64);
1645 string = xstrdup(str_);
1646
1647 error = parse_ofp_meter_mod_str__(mm, string, &bands, command,
1648 usable_protocols);
1649
1650 free(string);
1651 ofpbuf_uninit(&bands);
1652
1653 return error;
1654 }
1655
1656 static char * WARN_UNUSED_RESULT
1657 parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
1658 const char *str_, char *string,
1659 enum ofputil_protocol *usable_protocols)
1660 {
1661 static atomic_uint32_t id = ATOMIC_VAR_INIT(0);
1662 char *save_ptr = NULL;
1663 char *name;
1664
1665 atomic_add(&id, 1, &fmr->id);
1666
1667 fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
1668 | NXFMF_OWN | NXFMF_ACTIONS);
1669 fmr->out_port = OFPP_NONE;
1670 fmr->table_id = 0xff;
1671 match_init_catchall(&fmr->match);
1672
1673 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1674 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1675 const struct protocol *p;
1676
1677 if (!strcmp(name, "!initial")) {
1678 fmr->flags &= ~NXFMF_INITIAL;
1679 } else if (!strcmp(name, "!add")) {
1680 fmr->flags &= ~NXFMF_ADD;
1681 } else if (!strcmp(name, "!delete")) {
1682 fmr->flags &= ~NXFMF_DELETE;
1683 } else if (!strcmp(name, "!modify")) {
1684 fmr->flags &= ~NXFMF_MODIFY;
1685 } else if (!strcmp(name, "!actions")) {
1686 fmr->flags &= ~NXFMF_ACTIONS;
1687 } else if (!strcmp(name, "!own")) {
1688 fmr->flags &= ~NXFMF_OWN;
1689 } else if (parse_protocol(name, &p)) {
1690 match_set_dl_type(&fmr->match, htons(p->dl_type));
1691 if (p->nw_proto) {
1692 match_set_nw_proto(&fmr->match, p->nw_proto);
1693 }
1694 } else {
1695 char *value;
1696
1697 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1698 if (!value) {
1699 return xasprintf("%s: field %s missing value", str_, name);
1700 }
1701
1702 if (!strcmp(name, "table")) {
1703 char *error = str_to_u8(value, "table", &fmr->table_id);
1704 if (error) {
1705 return error;
1706 }
1707 } else if (!strcmp(name, "out_port")) {
1708 fmr->out_port = u16_to_ofp(atoi(value));
1709 } else if (mf_from_name(name)) {
1710 char *error;
1711
1712 error = parse_field(mf_from_name(name), value, &fmr->match,
1713 usable_protocols);
1714 if (error) {
1715 return error;
1716 }
1717 } else {
1718 return xasprintf("%s: unknown keyword %s", str_, name);
1719 }
1720 }
1721 }
1722 return NULL;
1723 }
1724
1725 /* Convert 'str_' (as described in the documentation for the "monitor" command
1726 * in the ovs-ofctl man page) into 'fmr'.
1727 *
1728 * Returns NULL if successful, otherwise a malloc()'d string describing the
1729 * error. The caller is responsible for freeing the returned string. */
1730 char * WARN_UNUSED_RESULT
1731 parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
1732 const char *str_,
1733 enum ofputil_protocol *usable_protocols)
1734 {
1735 char *string = xstrdup(str_);
1736 char *error = parse_flow_monitor_request__(fmr, str_, string,
1737 usable_protocols);
1738 free(string);
1739 return error;
1740 }
1741
1742 /* Parses 's' as a set of OpenFlow actions and appends the actions to
1743 * 'actions'.
1744 *
1745 * Returns NULL if successful, otherwise a malloc()'d string describing the
1746 * error. The caller is responsible for freeing the returned string. */
1747 char * WARN_UNUSED_RESULT
1748 parse_ofpacts(const char *s_, struct ofpbuf *ofpacts,
1749 enum ofputil_protocol *usable_protocols)
1750 {
1751 char *s = xstrdup(s_);
1752 char *error;
1753
1754 *usable_protocols = OFPUTIL_P_ANY;
1755
1756 error = str_to_ofpacts(s, ofpacts, usable_protocols);
1757 free(s);
1758
1759 return error;
1760 }
1761
1762 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
1763 * (one of OFPFC_*) into 'fm'.
1764 *
1765 * Returns NULL if successful, otherwise a malloc()'d string describing the
1766 * error. The caller is responsible for freeing the returned string. */
1767 char * WARN_UNUSED_RESULT
1768 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
1769 uint16_t command,
1770 enum ofputil_protocol *usable_protocols,
1771 bool enforce_consistency)
1772 {
1773 char *error = parse_ofp_str(fm, command, string, usable_protocols,
1774 enforce_consistency);
1775 if (!error) {
1776 /* Normalize a copy of the match. This ensures that non-normalized
1777 * flows get logged but doesn't affect what gets sent to the switch, so
1778 * that the switch can do whatever it likes with the flow. */
1779 struct match match_copy = fm->match;
1780 ofputil_normalize_match(&match_copy);
1781 }
1782
1783 return error;
1784 }
1785
1786 /* Convert 'table_id' and 'flow_miss_handling' (as described for the
1787 * "mod-table" command in the ovs-ofctl man page) into 'tm' for sending the
1788 * specified table_mod 'command' to a switch.
1789 *
1790 * Returns NULL if successful, otherwise a malloc()'d string describing the
1791 * error. The caller is responsible for freeing the returned string. */
1792 char * WARN_UNUSED_RESULT
1793 parse_ofp_table_mod(struct ofputil_table_mod *tm, const char *table_id,
1794 const char *flow_miss_handling,
1795 enum ofputil_protocol *usable_protocols)
1796 {
1797 /* Table mod requires at least OF 1.1. */
1798 *usable_protocols = OFPUTIL_P_OF11_UP;
1799
1800 if (!strcasecmp(table_id, "all")) {
1801 tm->table_id = 255;
1802 } else {
1803 char *error = str_to_u8(table_id, "table_id", &tm->table_id);
1804 if (error) {
1805 return error;
1806 }
1807 }
1808
1809 if (strcmp(flow_miss_handling, "controller") == 0) {
1810 tm->config = OFPTC11_TABLE_MISS_CONTROLLER;
1811 } else if (strcmp(flow_miss_handling, "continue") == 0) {
1812 tm->config = OFPTC11_TABLE_MISS_CONTINUE;
1813 } else if (strcmp(flow_miss_handling, "drop") == 0) {
1814 tm->config = OFPTC11_TABLE_MISS_DROP;
1815 } else {
1816 return xasprintf("invalid flow_miss_handling %s", flow_miss_handling);
1817 }
1818
1819 if (tm->table_id == 0xfe && tm->config == OFPTC11_TABLE_MISS_CONTINUE) {
1820 return xstrdup("last table's flow miss handling can not be continue");
1821 }
1822
1823 return NULL;
1824 }
1825
1826
1827 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
1828 * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated
1829 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
1830 *
1831 * Returns NULL if successful, otherwise a malloc()'d string describing the
1832 * error. The caller is responsible for freeing the returned string. */
1833 char * WARN_UNUSED_RESULT
1834 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
1835 struct ofputil_flow_mod **fms, size_t *n_fms,
1836 enum ofputil_protocol *usable_protocols,
1837 bool enforce_consistency)
1838 {
1839 size_t allocated_fms;
1840 int line_number;
1841 FILE *stream;
1842 struct ds s;
1843
1844 *usable_protocols = OFPUTIL_P_ANY;
1845
1846 *fms = NULL;
1847 *n_fms = 0;
1848
1849 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1850 if (stream == NULL) {
1851 return xasprintf("%s: open failed (%s)",
1852 file_name, ovs_strerror(errno));
1853 }
1854
1855 allocated_fms = *n_fms;
1856 ds_init(&s);
1857 line_number = 0;
1858 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
1859 char *error;
1860 enum ofputil_protocol usable;
1861
1862 if (*n_fms >= allocated_fms) {
1863 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
1864 }
1865 error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command,
1866 &usable, enforce_consistency);
1867 if (error) {
1868 size_t i;
1869
1870 for (i = 0; i < *n_fms; i++) {
1871 free((*fms)[i].ofpacts);
1872 }
1873 free(*fms);
1874 *fms = NULL;
1875 *n_fms = 0;
1876
1877 ds_destroy(&s);
1878 if (stream != stdin) {
1879 fclose(stream);
1880 }
1881
1882 return xasprintf("%s:%d: %s", file_name, line_number, error);
1883 }
1884 *usable_protocols &= usable; /* Each line can narrow the set. */
1885 *n_fms += 1;
1886 }
1887
1888 ds_destroy(&s);
1889 if (stream != stdin) {
1890 fclose(stream);
1891 }
1892 return NULL;
1893 }
1894
1895 char * WARN_UNUSED_RESULT
1896 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
1897 bool aggregate, const char *string,
1898 enum ofputil_protocol *usable_protocols,
1899 bool enforce_consistency)
1900 {
1901 struct ofputil_flow_mod fm;
1902 char *error;
1903
1904 error = parse_ofp_str(&fm, -1, string, usable_protocols,
1905 enforce_consistency);
1906 if (error) {
1907 return error;
1908 }
1909
1910 /* Special table ID support not required for stats requests. */
1911 if (*usable_protocols & OFPUTIL_P_OF10_STD_TID) {
1912 *usable_protocols |= OFPUTIL_P_OF10_STD;
1913 }
1914 if (*usable_protocols & OFPUTIL_P_OF10_NXM_TID) {
1915 *usable_protocols |= OFPUTIL_P_OF10_NXM;
1916 }
1917
1918 fsr->aggregate = aggregate;
1919 fsr->cookie = fm.cookie;
1920 fsr->cookie_mask = fm.cookie_mask;
1921 fsr->match = fm.match;
1922 fsr->out_port = fm.out_port;
1923 fsr->out_group = fm.out_group;
1924 fsr->table_id = fm.table_id;
1925 return NULL;
1926 }
1927
1928 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
1929 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1930 * mf_field. Fields must be specified in a natural order for satisfying
1931 * prerequisites. If 'mask' is specified, fills the mask field for each of the
1932 * field specified in flow. If the map, 'names_portno' is specfied, converts
1933 * the in_port name into port no while setting the 'flow'.
1934 *
1935 * Returns NULL on success, otherwise a malloc()'d string that explains the
1936 * problem. */
1937 char *
1938 parse_ofp_exact_flow(struct flow *flow, struct flow *mask, const char *s,
1939 const struct simap *portno_names)
1940 {
1941 char *pos, *key, *value_s;
1942 char *error = NULL;
1943 char *copy;
1944
1945 memset(flow, 0, sizeof *flow);
1946 if (mask) {
1947 memset(mask, 0, sizeof *mask);
1948 }
1949
1950 pos = copy = xstrdup(s);
1951 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
1952 const struct protocol *p;
1953 if (parse_protocol(key, &p)) {
1954 if (flow->dl_type) {
1955 error = xasprintf("%s: Ethernet type set multiple times", s);
1956 goto exit;
1957 }
1958 flow->dl_type = htons(p->dl_type);
1959 if (mask) {
1960 mask->dl_type = OVS_BE16_MAX;
1961 }
1962
1963 if (p->nw_proto) {
1964 if (flow->nw_proto) {
1965 error = xasprintf("%s: network protocol set "
1966 "multiple times", s);
1967 goto exit;
1968 }
1969 flow->nw_proto = p->nw_proto;
1970 if (mask) {
1971 mask->nw_proto = UINT8_MAX;
1972 }
1973 }
1974 } else {
1975 const struct mf_field *mf;
1976 union mf_value value;
1977 char *field_error;
1978
1979 mf = mf_from_name(key);
1980 if (!mf) {
1981 error = xasprintf("%s: unknown field %s", s, key);
1982 goto exit;
1983 }
1984
1985 if (!mf_are_prereqs_ok(mf, flow)) {
1986 error = xasprintf("%s: prerequisites not met for setting %s",
1987 s, key);
1988 goto exit;
1989 }
1990
1991 if (!mf_is_zero(mf, flow)) {
1992 error = xasprintf("%s: field %s set multiple times", s, key);
1993 goto exit;
1994 }
1995
1996 if (!strcmp(key, "in_port")
1997 && portno_names
1998 && simap_contains(portno_names, value_s)) {
1999 flow->in_port.ofp_port = u16_to_ofp(
2000 simap_get(portno_names, value_s));
2001 if (mask) {
2002 mask->in_port.ofp_port = u16_to_ofp(ntohs(OVS_BE16_MAX));
2003 }
2004 } else {
2005 field_error = mf_parse_value(mf, value_s, &value);
2006 if (field_error) {
2007 error = xasprintf("%s: bad value for %s (%s)",
2008 s, key, field_error);
2009 free(field_error);
2010 goto exit;
2011 }
2012
2013 mf_set_flow_value(mf, &value, flow);
2014 if (mask) {
2015 mf_mask_field(mf, mask);
2016 }
2017 }
2018 }
2019 }
2020
2021 if (!flow->in_port.ofp_port) {
2022 flow->in_port.ofp_port = OFPP_NONE;
2023 }
2024
2025 exit:
2026 free(copy);
2027
2028 if (error) {
2029 memset(flow, 0, sizeof *flow);
2030 if (mask) {
2031 memset(mask, 0, sizeof *mask);
2032 }
2033 }
2034 return error;
2035 }
2036
2037 static char * WARN_UNUSED_RESULT
2038 parse_bucket_str(struct ofputil_bucket *bucket, char *str_,
2039 enum ofputil_protocol *usable_protocols)
2040 {
2041 struct ofpbuf ofpacts;
2042 char *pos, *act, *arg;
2043 int n_actions;
2044
2045 bucket->weight = 1;
2046 bucket->watch_port = OFPP_ANY;
2047 bucket->watch_group = OFPG11_ANY;
2048
2049 pos = str_;
2050 n_actions = 0;
2051 ofpbuf_init(&ofpacts, 64);
2052 while (ofputil_parse_key_value(&pos, &act, &arg)) {
2053 char *error = NULL;
2054
2055 if (!strcasecmp(act, "weight")) {
2056 error = str_to_u16(arg, "weight", &bucket->weight);
2057 } else if (!strcasecmp(act, "watch_port")) {
2058 if (!ofputil_port_from_string(arg, &bucket->watch_port)
2059 || (ofp_to_u16(bucket->watch_port) >= ofp_to_u16(OFPP_MAX)
2060 && bucket->watch_port != OFPP_ANY)) {
2061 error = xasprintf("%s: invalid watch_port", arg);
2062 }
2063 } else if (!strcasecmp(act, "watch_group")) {
2064 error = str_to_u32(arg, &bucket->watch_group);
2065 if (!error && bucket->watch_group > OFPG_MAX) {
2066 error = xasprintf("invalid watch_group id %"PRIu32,
2067 bucket->watch_group);
2068 }
2069 } else {
2070 error = str_to_ofpact__(pos, act, arg, &ofpacts, n_actions,
2071 usable_protocols);
2072 n_actions++;
2073 }
2074
2075 if (error) {
2076 ofpbuf_uninit(&ofpacts);
2077 return error;
2078 }
2079 }
2080
2081 ofpact_pad(&ofpacts);
2082 bucket->ofpacts = ofpacts.data;
2083 bucket->ofpacts_len = ofpacts.size;
2084
2085 return NULL;
2086 }
2087
2088 static char * WARN_UNUSED_RESULT
2089 parse_ofp_group_mod_str__(struct ofputil_group_mod *gm, uint16_t command,
2090 char *string,
2091 enum ofputil_protocol *usable_protocols)
2092 {
2093 enum {
2094 F_GROUP_TYPE = 1 << 0,
2095 F_BUCKETS = 1 << 1,
2096 } fields;
2097 char *save_ptr = NULL;
2098 bool had_type = false;
2099 char *name;
2100 struct ofputil_bucket *bucket;
2101 char *error = NULL;
2102
2103 *usable_protocols = OFPUTIL_P_OF11_UP;
2104
2105 switch (command) {
2106 case OFPGC11_ADD:
2107 fields = F_GROUP_TYPE | F_BUCKETS;
2108 break;
2109
2110 case OFPGC11_DELETE:
2111 fields = 0;
2112 break;
2113
2114 case OFPGC11_MODIFY:
2115 fields = F_GROUP_TYPE | F_BUCKETS;
2116 break;
2117
2118 default:
2119 NOT_REACHED();
2120 }
2121
2122 memset(gm, 0, sizeof *gm);
2123 gm->command = command;
2124 gm->group_id = OFPG_ANY;
2125 list_init(&gm->buckets);
2126 if (command == OFPGC11_DELETE && string[0] == '\0') {
2127 gm->group_id = OFPG_ALL;
2128 return NULL;
2129 }
2130
2131 *usable_protocols = OFPUTIL_P_OF11_UP;
2132
2133 if (fields & F_BUCKETS) {
2134 char *bkt_str = strstr(string, "bucket");
2135
2136 if (bkt_str) {
2137 *bkt_str = '\0';
2138 }
2139
2140 while (bkt_str) {
2141 char *next_bkt_str;
2142
2143 bkt_str = strchr(bkt_str + 1, '=');
2144 if (!bkt_str) {
2145 error = xstrdup("must specify bucket content");
2146 goto out;
2147 }
2148 bkt_str++;
2149
2150 next_bkt_str = strstr(bkt_str, "bucket");
2151 if (next_bkt_str) {
2152 *next_bkt_str = '\0';
2153 }
2154
2155 bucket = xzalloc(sizeof(struct ofputil_bucket));
2156 error = parse_bucket_str(bucket, bkt_str, usable_protocols);
2157 if (error) {
2158 free(bucket);
2159 goto out;
2160 }
2161 list_push_back(&gm->buckets, &bucket->list_node);
2162
2163 bkt_str = next_bkt_str;
2164 }
2165 }
2166
2167 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
2168 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
2169 char *value;
2170
2171 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
2172 if (!value) {
2173 error = xasprintf("field %s missing value", name);
2174 goto out;
2175 }
2176
2177 if (!strcmp(name, "group_id")) {
2178 if(!strcmp(value, "all")) {
2179 gm->group_id = OFPG_ALL;
2180 } else {
2181 char *error = str_to_u32(value, &gm->group_id);
2182 if (error) {
2183 goto out;
2184 }
2185 if (gm->group_id != OFPG_ALL && gm->group_id > OFPG_MAX) {
2186 error = xasprintf("invalid group id %"PRIu32,
2187 gm->group_id);
2188 goto out;
2189 }
2190 }
2191 } else if (!strcmp(name, "type")){
2192 if (!(fields & F_GROUP_TYPE)) {
2193 error = xstrdup("type is not needed");
2194 goto out;
2195 }
2196 if (!strcmp(value, "all")) {
2197 gm->type = OFPGT11_ALL;
2198 } else if (!strcmp(value, "select")) {
2199 gm->type = OFPGT11_SELECT;
2200 } else if (!strcmp(value, "indirect")) {
2201 gm->type = OFPGT11_INDIRECT;
2202 } else if (!strcmp(value, "ff") ||
2203 !strcmp(value, "fast_failover")) {
2204 gm->type = OFPGT11_FF;
2205 } else {
2206 error = xasprintf("invalid group type %s", value);
2207 goto out;
2208 }
2209 had_type = true;
2210 } else if (!strcmp(name, "bucket")) {
2211 error = xstrdup("bucket is not needed");
2212 goto out;
2213 } else {
2214 error = xasprintf("unknown keyword %s", name);
2215 goto out;
2216 }
2217 }
2218 if (gm->group_id == OFPG_ANY) {
2219 error = xstrdup("must specify a group_id");
2220 goto out;
2221 }
2222 if (fields & F_GROUP_TYPE && !had_type) {
2223 error = xstrdup("must specify a type");
2224 goto out;
2225 }
2226
2227 /* Validate buckets. */
2228 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
2229 if (bucket->weight != 1 && gm->type != OFPGT11_SELECT) {
2230 error = xstrdup("Only select groups can have bucket weights.");
2231 goto out;
2232 }
2233 }
2234 if (gm->type == OFPGT11_INDIRECT && !list_is_short(&gm->buckets)) {
2235 error = xstrdup("Indirect groups can have at most one bucket.");
2236 goto out;
2237 }
2238
2239 return NULL;
2240 out:
2241 ofputil_bucket_list_destroy(&gm->buckets);
2242 return error;
2243 }
2244
2245 char * WARN_UNUSED_RESULT
2246 parse_ofp_group_mod_str(struct ofputil_group_mod *gm, uint16_t command,
2247 const char *str_,
2248 enum ofputil_protocol *usable_protocols)
2249 {
2250 char *string = xstrdup(str_);
2251 char *error = parse_ofp_group_mod_str__(gm, command, string,
2252 usable_protocols);
2253 free(string);
2254
2255 if (error) {
2256 ofputil_bucket_list_destroy(&gm->buckets);
2257 }
2258 return error;
2259 }
2260
2261 char * WARN_UNUSED_RESULT
2262 parse_ofp_group_mod_file(const char *file_name, uint16_t command,
2263 struct ofputil_group_mod **gms, size_t *n_gms,
2264 enum ofputil_protocol *usable_protocols)
2265 {
2266 size_t allocated_gms;
2267 int line_number;
2268 FILE *stream;
2269 struct ds s;
2270
2271 *gms = NULL;
2272 *n_gms = 0;
2273
2274 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
2275 if (stream == NULL) {
2276 return xasprintf("%s: open failed (%s)",
2277 file_name, ovs_strerror(errno));
2278 }
2279
2280 allocated_gms = *n_gms;
2281 ds_init(&s);
2282 line_number = 0;
2283 *usable_protocols = OFPUTIL_P_OF11_UP;
2284 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
2285 enum ofputil_protocol usable;
2286 char *error;
2287
2288 if (*n_gms >= allocated_gms) {
2289 *gms = x2nrealloc(*gms, &allocated_gms, sizeof **gms);
2290 }
2291 error = parse_ofp_group_mod_str(&(*gms)[*n_gms], command, ds_cstr(&s),
2292 &usable);
2293 if (error) {
2294 size_t i;
2295
2296 for (i = 0; i < *n_gms; i++) {
2297 ofputil_bucket_list_destroy(&(*gms)[i].buckets);
2298 }
2299 free(*gms);
2300 *gms = NULL;
2301 *n_gms = 0;
2302
2303 ds_destroy(&s);
2304 if (stream != stdin) {
2305 fclose(stream);
2306 }
2307
2308 return xasprintf("%s:%d: %s", file_name, line_number, error);
2309 }
2310 *usable_protocols &= usable;
2311 *n_gms += 1;
2312 }
2313
2314 ds_destroy(&s);
2315 if (stream != stdin) {
2316 fclose(stream);
2317 }
2318 return NULL;
2319 }