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