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