]> git.proxmox.com Git - ovs.git/blob - lib/ofp-parse.c
Implement IPFIX export
[ovs.git] / lib / ofp-parse.c
1 /*
2 * Copyright (c) 2010, 2011, 2012 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 "packets.h"
38 #include "socket-util.h"
39 #include "vconn.h"
40 #include "vlog.h"
41
42 VLOG_DEFINE_THIS_MODULE(ofp_parse);
43
44 static void ofp_fatal(const char *flow, bool verbose, const char *format, ...)
45 NO_RETURN;
46
47 static uint8_t
48 str_to_table_id(const char *str)
49 {
50 int table_id;
51
52 if (!str_to_int(str, 10, &table_id) || table_id < 0 || table_id > 255) {
53 ovs_fatal(0, "invalid table \"%s\"", str);
54 }
55 return table_id;
56 }
57
58 static uint16_t
59 str_to_u16(const char *str, const char *name)
60 {
61 int value;
62
63 if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) {
64 ovs_fatal(0, "invalid %s \"%s\"", name, str);
65 }
66 return value;
67 }
68
69 static uint32_t
70 str_to_u32(const char *str)
71 {
72 char *tail;
73 uint32_t value;
74
75 if (!str[0]) {
76 ovs_fatal(0, "missing required numeric argument");
77 }
78
79 errno = 0;
80 value = strtoul(str, &tail, 0);
81 if (errno == EINVAL || errno == ERANGE || *tail) {
82 ovs_fatal(0, "invalid numeric format %s", str);
83 }
84 return value;
85 }
86
87 static uint64_t
88 str_to_u64(const char *str)
89 {
90 char *tail;
91 uint64_t value;
92
93 if (!str[0]) {
94 ovs_fatal(0, "missing required numeric argument");
95 }
96
97 errno = 0;
98 value = strtoull(str, &tail, 0);
99 if (errno == EINVAL || errno == ERANGE || *tail) {
100 ovs_fatal(0, "invalid numeric format %s", str);
101 }
102 return value;
103 }
104
105 static void
106 str_to_mac(const char *str, uint8_t mac[6])
107 {
108 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
109 != ETH_ADDR_SCAN_COUNT) {
110 ovs_fatal(0, "invalid mac address %s", str);
111 }
112 }
113
114 static void
115 str_to_ip(const char *str, ovs_be32 *ip)
116 {
117 struct in_addr in_addr;
118
119 if (lookup_ip(str, &in_addr)) {
120 ovs_fatal(0, "%s: could not convert to IP address", str);
121 }
122 *ip = in_addr.s_addr;
123 }
124
125 static void
126 parse_enqueue(char *arg, struct ofpbuf *ofpacts)
127 {
128 char *sp = NULL;
129 char *port = strtok_r(arg, ":q", &sp);
130 char *queue = strtok_r(NULL, "", &sp);
131 struct ofpact_enqueue *enqueue;
132
133 if (port == NULL || queue == NULL) {
134 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
135 }
136
137 enqueue = ofpact_put_ENQUEUE(ofpacts);
138 enqueue->port = str_to_u32(port);
139 enqueue->queue = str_to_u32(queue);
140 }
141
142 static void
143 parse_output(char *arg, struct ofpbuf *ofpacts)
144 {
145 if (strchr(arg, '[')) {
146 struct ofpact_output_reg *output_reg;
147
148 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
149 mf_parse_subfield(&output_reg->src, arg);
150 output_reg->max_len = UINT16_MAX;
151 } else {
152 struct ofpact_output *output;
153
154 output = ofpact_put_OUTPUT(ofpacts);
155 output->port = str_to_u32(arg);
156 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
157 }
158 }
159
160 static void
161 parse_resubmit(char *arg, struct ofpbuf *ofpacts)
162 {
163 struct ofpact_resubmit *resubmit;
164 char *in_port_s, *table_s;
165
166 resubmit = ofpact_put_RESUBMIT(ofpacts);
167
168 in_port_s = strsep(&arg, ",");
169 if (in_port_s && in_port_s[0]) {
170 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
171 ovs_fatal(0, "%s: resubmit to unknown port", in_port_s);
172 }
173 } else {
174 resubmit->in_port = OFPP_IN_PORT;
175 }
176
177 table_s = strsep(&arg, ",");
178 resubmit->table_id = table_s && table_s[0] ? str_to_u32(table_s) : 255;
179
180 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
181 ovs_fatal(0, "at least one \"in_port\" or \"table\" must be specified "
182 " on resubmit");
183 }
184 }
185
186 static void
187 parse_note(const char *arg, struct ofpbuf *ofpacts)
188 {
189 struct ofpact_note *note;
190
191 note = ofpact_put_NOTE(ofpacts);
192 while (*arg != '\0') {
193 uint8_t byte;
194 bool ok;
195
196 if (*arg == '.') {
197 arg++;
198 }
199 if (*arg == '\0') {
200 break;
201 }
202
203 byte = hexits_value(arg, 2, &ok);
204 if (!ok) {
205 ovs_fatal(0, "bad hex digit in `note' argument");
206 }
207 ofpbuf_put(ofpacts, &byte, 1);
208
209 note = ofpacts->l2;
210 note->length++;
211
212 arg += 2;
213 }
214 ofpact_update_len(ofpacts, &note->ofpact);
215 }
216
217 static void
218 parse_fin_timeout(struct ofpbuf *b, char *arg)
219 {
220 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(b);
221 char *key, *value;
222
223 while (ofputil_parse_key_value(&arg, &key, &value)) {
224 if (!strcmp(key, "idle_timeout")) {
225 oft->fin_idle_timeout = str_to_u16(value, key);
226 } else if (!strcmp(key, "hard_timeout")) {
227 oft->fin_hard_timeout = str_to_u16(value, key);
228 } else {
229 ovs_fatal(0, "invalid key '%s' in 'fin_timeout' argument", key);
230 }
231 }
232 }
233
234 static void
235 parse_controller(struct ofpbuf *b, char *arg)
236 {
237 enum ofp_packet_in_reason reason = OFPR_ACTION;
238 uint16_t controller_id = 0;
239 uint16_t max_len = UINT16_MAX;
240
241 if (!arg[0]) {
242 /* Use defaults. */
243 } else if (strspn(arg, "0123456789") == strlen(arg)) {
244 max_len = str_to_u16(arg, "max_len");
245 } else {
246 char *name, *value;
247
248 while (ofputil_parse_key_value(&arg, &name, &value)) {
249 if (!strcmp(name, "reason")) {
250 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
251 ovs_fatal(0, "unknown reason \"%s\"", value);
252 }
253 } else if (!strcmp(name, "max_len")) {
254 max_len = str_to_u16(value, "max_len");
255 } else if (!strcmp(name, "id")) {
256 controller_id = str_to_u16(value, "id");
257 } else {
258 ovs_fatal(0, "unknown key \"%s\" parsing controller action",
259 name);
260 }
261 }
262 }
263
264 if (reason == OFPR_ACTION && controller_id == 0) {
265 struct ofpact_output *output;
266
267 output = ofpact_put_OUTPUT(b);
268 output->port = OFPP_CONTROLLER;
269 output->max_len = max_len;
270 } else {
271 struct ofpact_controller *controller;
272
273 controller = ofpact_put_CONTROLLER(b);
274 controller->max_len = max_len;
275 controller->reason = reason;
276 controller->controller_id = controller_id;
277 }
278 }
279
280 static void
281 parse_noargs_dec_ttl(struct ofpbuf *b)
282 {
283 struct ofpact_cnt_ids *ids;
284 uint16_t id = 0;
285
286 ids = ofpact_put_DEC_TTL(b);
287 ofpbuf_put(b, &id, sizeof id);
288 ids = b->l2;
289 ids->n_controllers++;
290 ofpact_update_len(b, &ids->ofpact);
291 }
292
293 static void
294 parse_dec_ttl(struct ofpbuf *b, char *arg)
295 {
296 if (*arg == '\0') {
297 parse_noargs_dec_ttl(b);
298 } else {
299 struct ofpact_cnt_ids *ids;
300 char *cntr;
301
302 ids = ofpact_put_DEC_TTL(b);
303 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
304 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
305 cntr = strtok_r(NULL, ", ", &arg)) {
306 uint16_t id = atoi(cntr);
307
308 ofpbuf_put(b, &id, sizeof id);
309 ids = b->l2;
310 ids->n_controllers++;
311 }
312 if (!ids->n_controllers) {
313 ovs_fatal(0, "dec_ttl_cnt_ids: expected at least one controller "
314 "id.");
315 }
316 ofpact_update_len(b, &ids->ofpact);
317 }
318 }
319
320 static void
321 parse_set_mpls_ttl(struct ofpbuf *b, const char *arg)
322 {
323 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(b);
324
325 if (*arg == '\0') {
326 ovs_fatal(0, "parse_set_mpls_ttl: expected ttl.");
327 }
328
329 mpls_ttl->ttl = atoi(arg);
330 }
331
332 static void
333 set_field_parse(const char *arg, struct ofpbuf *ofpacts)
334 {
335 char *orig = xstrdup(arg);
336 struct ofpact_reg_load *load = ofpact_put_REG_LOAD(ofpacts);
337 char *value;
338 char *delim;
339 char *key;
340 const struct mf_field *mf;
341 const char *error;
342 union mf_value mf_value;
343
344 value = orig;
345 delim = strstr(orig, "->");
346 if (!delim) {
347 ovs_fatal(0, "%s: missing `->'", orig);
348 }
349 if (strlen(delim) <= strlen("->")) {
350 ovs_fatal(0, "%s: missing field name following `->'", orig);
351 }
352
353 key = delim + strlen("->");
354 mf = mf_from_name(key);
355 if (!mf) {
356 ovs_fatal(0, "%s is not valid oxm field name", key);
357 }
358 if (!mf->writable) {
359 ovs_fatal(0, "%s is not allowed to set", key);
360 }
361
362 delim[0] = '\0';
363 error = mf_parse_value(mf, value, &mf_value);
364 if (error) {
365 ovs_fatal(0, "%s", error);
366 }
367 if (!mf_is_value_valid(mf, &mf_value)) {
368 ovs_fatal(0, "%s is not valid valid for field %s", value, key);
369 }
370 ofpact_set_field_init(load, mf, &mf_value);
371 free(orig);
372 }
373
374 static void
375 parse_metadata(struct ofpbuf *b, char *arg)
376 {
377 struct ofpact_metadata *om;
378 char *mask = strchr(arg, '/');
379
380 om = ofpact_put_WRITE_METADATA(b);
381
382 if (mask) {
383 *mask = '\0';
384 om->mask = htonll(str_to_u64(mask + 1));
385 } else {
386 om->mask = htonll(UINT64_MAX);
387 }
388
389 om->metadata = htonll(str_to_u64(arg));
390 }
391
392 static void
393 parse_sample(struct ofpbuf *b, char *arg)
394 {
395 struct ofpact_sample *os = ofpact_put_SAMPLE(b);
396 char *key, *value;
397
398 while (ofputil_parse_key_value(&arg, &key, &value)) {
399 if (!strcmp(key, "probability")) {
400 os->probability = str_to_u16(value, "probability");
401 if (os->probability == 0) {
402 ovs_fatal(0, "invalid probability value \"%s\"", value);
403 }
404 } else if (!strcmp(key, "collector_set_id")) {
405 os->collector_set_id = str_to_u32(value);
406 } else if (!strcmp(key, "obs_domain_id")) {
407 os->obs_domain_id = str_to_u32(value);
408 } else if (!strcmp(key, "obs_point_id")) {
409 os->obs_point_id = str_to_u32(value);
410 } else {
411 ovs_fatal(0, "invalid key \"%s\" in \"sample\" argument",
412 key);
413 }
414 }
415 if (os->probability == 0) {
416 ovs_fatal(0, "non-zero \"probability\" must be specified on sample");
417 }
418 }
419
420 static void
421 parse_named_action(enum ofputil_action_code code, const struct flow *flow,
422 char *arg, struct ofpbuf *ofpacts)
423 {
424 struct ofpact_tunnel *tunnel;
425 uint16_t vid;
426 uint16_t ethertype;
427 ovs_be32 ip;
428 uint8_t pcp;
429 uint8_t tos;
430
431 switch (code) {
432 case OFPUTIL_ACTION_INVALID:
433 NOT_REACHED();
434
435 case OFPUTIL_OFPAT10_OUTPUT:
436 case OFPUTIL_OFPAT11_OUTPUT:
437 parse_output(arg, ofpacts);
438 break;
439
440 case OFPUTIL_OFPAT10_SET_VLAN_VID:
441 case OFPUTIL_OFPAT11_SET_VLAN_VID:
442 vid = str_to_u32(arg);
443 if (vid & ~VLAN_VID_MASK) {
444 ovs_fatal(0, "%s: not a valid VLAN VID", arg);
445 }
446 ofpact_put_SET_VLAN_VID(ofpacts)->vlan_vid = vid;
447 break;
448
449 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
450 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
451 pcp = str_to_u32(arg);
452 if (pcp & ~7) {
453 ovs_fatal(0, "%s: not a valid VLAN PCP", arg);
454 }
455 ofpact_put_SET_VLAN_PCP(ofpacts)->vlan_pcp = pcp;
456 break;
457
458 case OFPUTIL_OFPAT12_SET_FIELD:
459 set_field_parse(arg, ofpacts);
460 break;
461
462 case OFPUTIL_OFPAT10_STRIP_VLAN:
463 case OFPUTIL_OFPAT11_POP_VLAN:
464 ofpact_put_STRIP_VLAN(ofpacts);
465 break;
466
467 case OFPUTIL_OFPAT11_PUSH_VLAN:
468 ethertype = str_to_u16(arg, "ethertype");
469 if (ethertype != ETH_TYPE_VLAN_8021Q) {
470 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
471 ovs_fatal(0, "%s: not a valid VLAN ethertype", arg);
472 }
473 ofpact_put_PUSH_VLAN(ofpacts);
474 break;
475
476 case OFPUTIL_OFPAT11_SET_QUEUE:
477 ofpact_put_SET_QUEUE(ofpacts)->queue_id = str_to_u32(arg);
478 break;
479
480
481 case OFPUTIL_OFPAT10_SET_DL_SRC:
482 case OFPUTIL_OFPAT11_SET_DL_SRC:
483 str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
484 break;
485
486 case OFPUTIL_OFPAT10_SET_DL_DST:
487 case OFPUTIL_OFPAT11_SET_DL_DST:
488 str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
489 break;
490
491 case OFPUTIL_OFPAT10_SET_NW_SRC:
492 case OFPUTIL_OFPAT11_SET_NW_SRC:
493 str_to_ip(arg, &ip);
494 ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4 = ip;
495 break;
496
497 case OFPUTIL_OFPAT10_SET_NW_DST:
498 case OFPUTIL_OFPAT11_SET_NW_DST:
499 str_to_ip(arg, &ip);
500 ofpact_put_SET_IPV4_DST(ofpacts)->ipv4 = ip;
501 break;
502
503 case OFPUTIL_OFPAT10_SET_NW_TOS:
504 case OFPUTIL_OFPAT11_SET_NW_TOS:
505 tos = str_to_u32(arg);
506 if (tos & ~IP_DSCP_MASK) {
507 ovs_fatal(0, "%s: not a valid TOS", arg);
508 }
509 ofpact_put_SET_IPV4_DSCP(ofpacts)->dscp = tos;
510 break;
511
512 case OFPUTIL_OFPAT11_DEC_NW_TTL:
513 NOT_REACHED();
514
515 case OFPUTIL_OFPAT10_SET_TP_SRC:
516 case OFPUTIL_OFPAT11_SET_TP_SRC:
517 ofpact_put_SET_L4_SRC_PORT(ofpacts)->port = str_to_u32(arg);
518 break;
519
520 case OFPUTIL_OFPAT10_SET_TP_DST:
521 case OFPUTIL_OFPAT11_SET_TP_DST:
522 ofpact_put_SET_L4_DST_PORT(ofpacts)->port = str_to_u32(arg);
523 break;
524
525 case OFPUTIL_OFPAT10_ENQUEUE:
526 parse_enqueue(arg, ofpacts);
527 break;
528
529 case OFPUTIL_NXAST_RESUBMIT:
530 parse_resubmit(arg, ofpacts);
531 break;
532
533 case OFPUTIL_NXAST_SET_TUNNEL:
534 case OFPUTIL_NXAST_SET_TUNNEL64:
535 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
536 tunnel->ofpact.compat = code;
537 tunnel->tun_id = str_to_u64(arg);
538 break;
539
540 case OFPUTIL_NXAST_WRITE_METADATA:
541 parse_metadata(ofpacts, arg);
542 break;
543
544 case OFPUTIL_NXAST_SET_QUEUE:
545 ofpact_put_SET_QUEUE(ofpacts)->queue_id = str_to_u32(arg);
546 break;
547
548 case OFPUTIL_NXAST_POP_QUEUE:
549 ofpact_put_POP_QUEUE(ofpacts);
550 break;
551
552 case OFPUTIL_NXAST_REG_MOVE:
553 nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts), arg);
554 break;
555
556 case OFPUTIL_NXAST_REG_LOAD:
557 nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts), arg);
558 break;
559
560 case OFPUTIL_NXAST_NOTE:
561 parse_note(arg, ofpacts);
562 break;
563
564 case OFPUTIL_NXAST_MULTIPATH:
565 multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
566 break;
567
568 case OFPUTIL_NXAST_BUNDLE:
569 bundle_parse(arg, ofpacts);
570 break;
571
572 case OFPUTIL_NXAST_BUNDLE_LOAD:
573 bundle_parse_load(arg, ofpacts);
574 break;
575
576 case OFPUTIL_NXAST_RESUBMIT_TABLE:
577 case OFPUTIL_NXAST_OUTPUT_REG:
578 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
579 NOT_REACHED();
580
581 case OFPUTIL_NXAST_LEARN:
582 learn_parse(arg, flow, ofpacts);
583 break;
584
585 case OFPUTIL_NXAST_EXIT:
586 ofpact_put_EXIT(ofpacts);
587 break;
588
589 case OFPUTIL_NXAST_DEC_TTL:
590 parse_dec_ttl(ofpacts, arg);
591 break;
592
593 case OFPUTIL_NXAST_SET_MPLS_TTL:
594 case OFPUTIL_OFPAT11_SET_MPLS_TTL:
595 parse_set_mpls_ttl(ofpacts, arg);
596 break;
597
598 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
599 case OFPUTIL_NXAST_DEC_MPLS_TTL:
600 ofpact_put_DEC_MPLS_TTL(ofpacts);
601 break;
602
603 case OFPUTIL_NXAST_FIN_TIMEOUT:
604 parse_fin_timeout(ofpacts, arg);
605 break;
606
607 case OFPUTIL_NXAST_CONTROLLER:
608 parse_controller(ofpacts, arg);
609 break;
610
611 case OFPUTIL_OFPAT11_PUSH_MPLS:
612 case OFPUTIL_NXAST_PUSH_MPLS:
613 ofpact_put_PUSH_MPLS(ofpacts)->ethertype =
614 htons(str_to_u16(arg, "push_mpls"));
615 break;
616
617 case OFPUTIL_OFPAT11_POP_MPLS:
618 case OFPUTIL_NXAST_POP_MPLS:
619 ofpact_put_POP_MPLS(ofpacts)->ethertype =
620 htons(str_to_u16(arg, "pop_mpls"));
621 break;
622
623 case OFPUTIL_NXAST_STACK_PUSH:
624 nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
625 break;
626 case OFPUTIL_NXAST_STACK_POP:
627 nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
628 break;
629
630 case OFPUTIL_NXAST_SAMPLE:
631 parse_sample(ofpacts, arg);
632 break;
633 }
634 }
635
636 static bool
637 str_to_ofpact__(const struct flow *flow, char *pos, char *act, char *arg,
638 struct ofpbuf *ofpacts, int n_actions)
639 {
640 int code = ofputil_action_code_from_name(act);
641 if (code >= 0) {
642 parse_named_action(code, flow, arg, ofpacts);
643 } else if (!strcasecmp(act, "drop")) {
644 if (n_actions) {
645 ovs_fatal(0, "Drop actions must not be preceded by other "
646 "actions");
647 } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
648 ovs_fatal(0, "Drop actions must not be followed by other "
649 "actions");
650 }
651 return false;
652 } else {
653 uint16_t port;
654 if (ofputil_port_from_string(act, &port)) {
655 ofpact_put_OUTPUT(ofpacts)->port = port;
656 } else {
657 ovs_fatal(0, "Unknown action: %s", act);
658 }
659 }
660
661 return true;
662 }
663
664 static void
665 str_to_ofpacts(const struct flow *flow, char *str, struct ofpbuf *ofpacts)
666 {
667 char *pos, *act, *arg;
668 enum ofperr error;
669 int n_actions;
670
671 pos = str;
672 n_actions = 0;
673 while (ofputil_parse_key_value(&pos, &act, &arg)) {
674 if (!str_to_ofpact__(flow, pos, act, arg, ofpacts, n_actions)) {
675 break;
676 }
677 n_actions++;
678 }
679
680 error = ofpacts_verify(ofpacts->data, ofpacts->size);
681 if (error) {
682 ovs_fatal(0, "Incorrect action ordering");
683 }
684
685 ofpact_pad(ofpacts);
686 }
687
688 static void
689 parse_named_instruction(enum ovs_instruction_type type,
690 char *arg, struct ofpbuf *ofpacts)
691 {
692 enum ofperr error;
693
694 switch (type) {
695 case OVSINST_OFPIT11_APPLY_ACTIONS:
696 NOT_REACHED(); /* This case is handled by str_to_inst_ofpacts() */
697 break;
698
699 case OVSINST_OFPIT11_WRITE_ACTIONS:
700 /* XXX */
701 ovs_fatal(0, "instruction write-actions is not supported yet");
702 break;
703
704 case OVSINST_OFPIT11_CLEAR_ACTIONS:
705 ofpact_put_CLEAR_ACTIONS(ofpacts);
706 break;
707
708 case OVSINST_OFPIT11_WRITE_METADATA:
709 parse_metadata(ofpacts, arg);
710 break;
711
712 case OVSINST_OFPIT11_GOTO_TABLE: {
713 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
714 char *table_s = strsep(&arg, ",");
715 if (!table_s || !table_s[0]) {
716 ovs_fatal(0, "instruction goto-table needs table id");
717 }
718 ogt->table_id = str_to_table_id(table_s);
719 break;
720 }
721 }
722
723 /* If write_metadata is specified as an action AND an instruction, ofpacts
724 could be invalid. */
725 error = ofpacts_verify(ofpacts->data, ofpacts->size);
726 if (error) {
727 ovs_fatal(0, "Incorrect instruction ordering");
728 }
729 }
730
731 static void
732 str_to_inst_ofpacts(const struct flow *flow, char *str, struct ofpbuf *ofpacts)
733 {
734 char *pos, *inst, *arg;
735 int type;
736 const char *prev_inst = NULL;
737 int prev_type = -1;
738 int n_actions = 0;
739
740 pos = str;
741 while (ofputil_parse_key_value(&pos, &inst, &arg)) {
742 type = ofpact_instruction_type_from_name(inst);
743 if (type < 0) {
744 if (!str_to_ofpact__(flow, pos, inst, arg, ofpacts, n_actions)) {
745 break;
746 }
747
748 type = OVSINST_OFPIT11_APPLY_ACTIONS;
749 if (prev_type == type) {
750 n_actions++;
751 continue;
752 }
753 } else if (type == OVSINST_OFPIT11_APPLY_ACTIONS) {
754 ovs_fatal(0, "%s isn't supported. Just write actions then "
755 "it is interpreted as apply_actions", inst);
756 } else {
757 parse_named_instruction(type, arg, ofpacts);
758 }
759
760 if (type == prev_type) {
761 ovs_fatal(0, "instruction can be specified at most once: %s",
762 inst);
763 }
764 if (type <= prev_type) {
765 ovs_fatal(0, "Instruction %s must be specified before %s",
766 inst, prev_inst);
767 }
768
769 prev_inst = inst;
770 prev_type = type;
771 n_actions++;
772 }
773 ofpact_pad(ofpacts);
774 }
775
776 struct protocol {
777 const char *name;
778 uint16_t dl_type;
779 uint8_t nw_proto;
780 };
781
782 static bool
783 parse_protocol(const char *name, const struct protocol **p_out)
784 {
785 static const struct protocol protocols[] = {
786 { "ip", ETH_TYPE_IP, 0 },
787 { "arp", ETH_TYPE_ARP, 0 },
788 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
789 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
790 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
791 { "ipv6", ETH_TYPE_IPV6, 0 },
792 { "ip6", ETH_TYPE_IPV6, 0 },
793 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
794 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
795 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
796 { "rarp", ETH_TYPE_RARP, 0},
797 { "mpls", ETH_TYPE_MPLS, 0 },
798 { "mplsm", ETH_TYPE_MPLS_MCAST, 0 },
799 };
800 const struct protocol *p;
801
802 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
803 if (!strcmp(p->name, name)) {
804 *p_out = p;
805 return true;
806 }
807 }
808 *p_out = NULL;
809 return false;
810 }
811
812 static void
813 ofp_fatal(const char *flow, bool verbose, const char *format, ...)
814 {
815 va_list args;
816
817 if (verbose) {
818 fprintf(stderr, "%s:\n", flow);
819 }
820
821 va_start(args, format);
822 ovs_fatal_valist(0, format, args);
823 }
824
825 static void
826 parse_field(const struct mf_field *mf, const char *s, struct match *match)
827 {
828 union mf_value value, mask;
829 char *error;
830
831 error = mf_parse(mf, s, &value, &mask);
832 if (error) {
833 ovs_fatal(0, "%s", error);
834 }
835
836 mf_set(mf, &value, &mask, match);
837 }
838
839 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
840 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
841 * If 'actions' is specified, an action must be in 'string' and may be expanded
842 * or reallocated.
843 *
844 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
845 * constant for 'command'. To parse syntax for an OFPST_FLOW or
846 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
847 void
848 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
849 bool verbose)
850 {
851 enum {
852 F_OUT_PORT = 1 << 0,
853 F_ACTIONS = 1 << 1,
854 F_TIMEOUT = 1 << 3,
855 F_PRIORITY = 1 << 4,
856 F_FLAGS = 1 << 5,
857 } fields;
858 char *string = xstrdup(str_);
859 char *save_ptr = NULL;
860 char *act_str = NULL;
861 char *name;
862
863 switch (command) {
864 case -1:
865 fields = F_OUT_PORT;
866 break;
867
868 case OFPFC_ADD:
869 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
870 break;
871
872 case OFPFC_DELETE:
873 fields = F_OUT_PORT;
874 break;
875
876 case OFPFC_DELETE_STRICT:
877 fields = F_OUT_PORT | F_PRIORITY;
878 break;
879
880 case OFPFC_MODIFY:
881 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
882 break;
883
884 case OFPFC_MODIFY_STRICT:
885 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
886 break;
887
888 default:
889 NOT_REACHED();
890 }
891
892 match_init_catchall(&fm->match);
893 fm->priority = OFP_DEFAULT_PRIORITY;
894 fm->cookie = htonll(0);
895 fm->cookie_mask = htonll(0);
896 if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
897 /* For modify, by default, don't update the cookie. */
898 fm->new_cookie = htonll(UINT64_MAX);
899 } else{
900 fm->new_cookie = htonll(0);
901 }
902 fm->table_id = 0xff;
903 fm->command = command;
904 fm->idle_timeout = OFP_FLOW_PERMANENT;
905 fm->hard_timeout = OFP_FLOW_PERMANENT;
906 fm->buffer_id = UINT32_MAX;
907 fm->out_port = OFPP_ANY;
908 fm->flags = 0;
909 if (fields & F_ACTIONS) {
910 act_str = strstr(string, "action");
911 if (!act_str) {
912 ofp_fatal(str_, verbose, "must specify an action");
913 }
914 *act_str = '\0';
915
916 act_str = strchr(act_str + 1, '=');
917 if (!act_str) {
918 ofp_fatal(str_, verbose, "must specify an action");
919 }
920
921 act_str++;
922 }
923 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
924 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
925 const struct protocol *p;
926
927 if (parse_protocol(name, &p)) {
928 match_set_dl_type(&fm->match, htons(p->dl_type));
929 if (p->nw_proto) {
930 match_set_nw_proto(&fm->match, p->nw_proto);
931 }
932 } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
933 fm->flags |= OFPFF_SEND_FLOW_REM;
934 } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
935 fm->flags |= OFPFF_CHECK_OVERLAP;
936 } else if (fields & F_FLAGS && !strcmp(name, "reset_counts")) {
937 fm->flags |= OFPFF12_RESET_COUNTS;
938 } else if (fields & F_FLAGS && !strcmp(name, "no_packet_counts")) {
939 fm->flags |= OFPFF13_NO_PKT_COUNTS;
940 } else if (fields & F_FLAGS && !strcmp(name, "no_byte_counts")) {
941 fm->flags |= OFPFF13_NO_BYT_COUNTS;
942 } else {
943 char *value;
944
945 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
946 if (!value) {
947 ofp_fatal(str_, verbose, "field %s missing value", name);
948 }
949
950 if (!strcmp(name, "table")) {
951 fm->table_id = str_to_table_id(value);
952 } else if (!strcmp(name, "out_port")) {
953 if (!ofputil_port_from_string(name, &fm->out_port)) {
954 ofp_fatal(str_, verbose, "%s is not a valid OpenFlow port",
955 name);
956 }
957 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
958 fm->priority = str_to_u16(value, name);
959 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
960 fm->idle_timeout = str_to_u16(value, name);
961 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
962 fm->hard_timeout = str_to_u16(value, name);
963 } else if (!strcmp(name, "cookie")) {
964 char *mask = strchr(value, '/');
965
966 if (mask) {
967 /* A mask means we're searching for a cookie. */
968 if (command == OFPFC_ADD) {
969 ofp_fatal(str_, verbose, "flow additions cannot use "
970 "a cookie mask");
971 }
972 *mask = '\0';
973 fm->cookie = htonll(str_to_u64(value));
974 fm->cookie_mask = htonll(str_to_u64(mask+1));
975 } else {
976 /* No mask means that the cookie is being set. */
977 if (command != OFPFC_ADD && command != OFPFC_MODIFY
978 && command != OFPFC_MODIFY_STRICT) {
979 ofp_fatal(str_, verbose, "cannot set cookie");
980 }
981 fm->new_cookie = htonll(str_to_u64(value));
982 }
983 } else if (mf_from_name(name)) {
984 parse_field(mf_from_name(name), value, &fm->match);
985 } else if (!strcmp(name, "duration")
986 || !strcmp(name, "n_packets")
987 || !strcmp(name, "n_bytes")
988 || !strcmp(name, "idle_age")
989 || !strcmp(name, "hard_age")) {
990 /* Ignore these, so that users can feed the output of
991 * "ovs-ofctl dump-flows" back into commands that parse
992 * flows. */
993 } else {
994 ofp_fatal(str_, verbose, "unknown keyword %s", name);
995 }
996 }
997 }
998 if (!fm->cookie_mask && fm->new_cookie == htonll(UINT64_MAX)
999 && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
1000 /* On modifies without a mask, we are supposed to add a flow if
1001 * one does not exist. If a cookie wasn't been specified, use a
1002 * default of zero. */
1003 fm->new_cookie = htonll(0);
1004 }
1005 if (fields & F_ACTIONS) {
1006 struct ofpbuf ofpacts;
1007
1008 ofpbuf_init(&ofpacts, 32);
1009 str_to_inst_ofpacts(&fm->match.flow, act_str, &ofpacts);
1010 fm->ofpacts_len = ofpacts.size;
1011 fm->ofpacts = ofpbuf_steal_data(&ofpacts);
1012 } else {
1013 fm->ofpacts_len = 0;
1014 fm->ofpacts = NULL;
1015 }
1016
1017 free(string);
1018 }
1019
1020 /* Convert 'str_' (as described in the documentation for the "monitor" command
1021 * in the ovs-ofctl man page) into 'fmr'. */
1022 void
1023 parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
1024 const char *str_)
1025 {
1026 static uint32_t id;
1027
1028 char *string = xstrdup(str_);
1029 char *save_ptr = NULL;
1030 char *name;
1031
1032 fmr->id = id++;
1033 fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
1034 | NXFMF_OWN | NXFMF_ACTIONS);
1035 fmr->out_port = OFPP_NONE;
1036 fmr->table_id = 0xff;
1037 match_init_catchall(&fmr->match);
1038
1039 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1040 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1041 const struct protocol *p;
1042
1043 if (!strcmp(name, "!initial")) {
1044 fmr->flags &= ~NXFMF_INITIAL;
1045 } else if (!strcmp(name, "!add")) {
1046 fmr->flags &= ~NXFMF_ADD;
1047 } else if (!strcmp(name, "!delete")) {
1048 fmr->flags &= ~NXFMF_DELETE;
1049 } else if (!strcmp(name, "!modify")) {
1050 fmr->flags &= ~NXFMF_MODIFY;
1051 } else if (!strcmp(name, "!actions")) {
1052 fmr->flags &= ~NXFMF_ACTIONS;
1053 } else if (!strcmp(name, "!own")) {
1054 fmr->flags &= ~NXFMF_OWN;
1055 } else if (parse_protocol(name, &p)) {
1056 match_set_dl_type(&fmr->match, htons(p->dl_type));
1057 if (p->nw_proto) {
1058 match_set_nw_proto(&fmr->match, p->nw_proto);
1059 }
1060 } else {
1061 char *value;
1062
1063 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1064 if (!value) {
1065 ovs_fatal(0, "%s: field %s missing value", str_, name);
1066 }
1067
1068 if (!strcmp(name, "table")) {
1069 fmr->table_id = str_to_table_id(value);
1070 } else if (!strcmp(name, "out_port")) {
1071 fmr->out_port = atoi(value);
1072 } else if (mf_from_name(name)) {
1073 parse_field(mf_from_name(name), value, &fmr->match);
1074 } else {
1075 ovs_fatal(0, "%s: unknown keyword %s", str_, name);
1076 }
1077 }
1078 }
1079 free(string);
1080 }
1081
1082 /* Parses 's' as a set of OpenFlow actions and appends the actions to
1083 * 'actions'.
1084 *
1085 * Prints an error on stderr and aborts the program if 's' syntax is
1086 * invalid. */
1087 void
1088 parse_ofpacts(const char *s_, struct ofpbuf *ofpacts)
1089 {
1090 char *s = xstrdup(s_);
1091 str_to_ofpacts(NULL, s, ofpacts);
1092 free(s);
1093 }
1094
1095 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
1096 * (one of OFPFC_*) into 'fm'. */
1097 void
1098 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
1099 uint16_t command, bool verbose)
1100 {
1101 struct match match_copy;
1102
1103 parse_ofp_str(fm, command, string, verbose);
1104
1105 /* Normalize a copy of the match. This ensures that non-normalized flows
1106 * get logged but doesn't affect what gets sent to the switch, so that the
1107 * switch can do whatever it likes with the flow. */
1108 match_copy = fm->match;
1109 ofputil_normalize_match(&match_copy);
1110 }
1111
1112 void
1113 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
1114 struct ofputil_flow_mod **fms, size_t *n_fms)
1115 {
1116 size_t allocated_fms;
1117 FILE *stream;
1118 struct ds s;
1119
1120 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1121 if (stream == NULL) {
1122 ovs_fatal(errno, "%s: open", file_name);
1123 }
1124
1125 allocated_fms = *n_fms;
1126 ds_init(&s);
1127 while (!ds_get_preprocessed_line(&s, stream)) {
1128 if (*n_fms >= allocated_fms) {
1129 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
1130 }
1131 parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command, false);
1132 *n_fms += 1;
1133 }
1134 ds_destroy(&s);
1135
1136 if (stream != stdin) {
1137 fclose(stream);
1138 }
1139 }
1140
1141 void
1142 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
1143 bool aggregate, const char *string)
1144 {
1145 struct ofputil_flow_mod fm;
1146
1147 parse_ofp_str(&fm, -1, string, false);
1148 fsr->aggregate = aggregate;
1149 fsr->cookie = fm.cookie;
1150 fsr->cookie_mask = fm.cookie_mask;
1151 fsr->match = fm.match;
1152 fsr->out_port = fm.out_port;
1153 fsr->table_id = fm.table_id;
1154 }
1155
1156 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
1157 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1158 * mf_field. Fields must be specified in a natural order for satisfying
1159 * prerequisites.
1160 *
1161 * Returns NULL on success, otherwise a malloc()'d string that explains the
1162 * problem. */
1163 char *
1164 parse_ofp_exact_flow(struct flow *flow, const char *s)
1165 {
1166 char *pos, *key, *value_s;
1167 char *error = NULL;
1168 char *copy;
1169
1170 memset(flow, 0, sizeof *flow);
1171
1172 pos = copy = xstrdup(s);
1173 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
1174 const struct protocol *p;
1175 if (parse_protocol(key, &p)) {
1176 if (flow->dl_type) {
1177 error = xasprintf("%s: Ethernet type set multiple times", s);
1178 goto exit;
1179 }
1180 flow->dl_type = htons(p->dl_type);
1181
1182 if (p->nw_proto) {
1183 if (flow->nw_proto) {
1184 error = xasprintf("%s: network protocol set "
1185 "multiple times", s);
1186 goto exit;
1187 }
1188 flow->nw_proto = p->nw_proto;
1189 }
1190 } else {
1191 const struct mf_field *mf;
1192 union mf_value value;
1193 char *field_error;
1194
1195 mf = mf_from_name(key);
1196 if (!mf) {
1197 error = xasprintf("%s: unknown field %s", s, key);
1198 goto exit;
1199 }
1200
1201 if (!mf_are_prereqs_ok(mf, flow)) {
1202 error = xasprintf("%s: prerequisites not met for setting %s",
1203 s, key);
1204 goto exit;
1205 }
1206
1207 if (!mf_is_zero(mf, flow)) {
1208 error = xasprintf("%s: field %s set multiple times", s, key);
1209 goto exit;
1210 }
1211
1212 field_error = mf_parse_value(mf, value_s, &value);
1213 if (field_error) {
1214 error = xasprintf("%s: bad value for %s (%s)",
1215 s, key, field_error);
1216 free(field_error);
1217 goto exit;
1218 }
1219
1220 mf_set_flow_value(mf, &value, flow);
1221 }
1222 }
1223
1224 if (!flow->in_port) {
1225 flow->in_port = OFPP_NONE;
1226 }
1227
1228 exit:
1229 free(copy);
1230
1231 if (error) {
1232 memset(flow, 0, sizeof *flow);
1233 }
1234 return error;
1235 }