Make the string parameters const for do_flow_mod__()
[sliver-openvswitch.git] / lib / ofp-parse.c
1 /*
2  * Copyright (c) 2010, 2011, 2012 Nicira Networks.
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 "netdev.h"
32 #include "multipath.h"
33 #include "nx-match.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 uint8_t
45 str_to_table_id(const char *str)
46 {
47     int table_id;
48
49     if (!str_to_int(str, 10, &table_id) || table_id < 0 || table_id > 255) {
50         ovs_fatal(0, "invalid table \"%s\"", str);
51     }
52     return table_id;
53 }
54
55 static uint16_t
56 str_to_u16(const char *str, const char *name)
57 {
58     int value;
59
60     if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) {
61         ovs_fatal(0, "invalid %s \"%s\"", name, str);
62     }
63     return value;
64 }
65
66 static uint32_t
67 str_to_u32(const char *str)
68 {
69     char *tail;
70     uint32_t value;
71
72     if (!str[0]) {
73         ovs_fatal(0, "missing required numeric argument");
74     }
75
76     errno = 0;
77     value = strtoul(str, &tail, 0);
78     if (errno == EINVAL || errno == ERANGE || *tail) {
79         ovs_fatal(0, "invalid numeric format %s", str);
80     }
81     return value;
82 }
83
84 static uint64_t
85 str_to_u64(const char *str)
86 {
87     char *tail;
88     uint64_t value;
89
90     if (!str[0]) {
91         ovs_fatal(0, "missing required numeric argument");
92     }
93
94     errno = 0;
95     value = strtoull(str, &tail, 0);
96     if (errno == EINVAL || errno == ERANGE || *tail) {
97         ovs_fatal(0, "invalid numeric format %s", str);
98     }
99     return value;
100 }
101
102 static void
103 str_to_mac(const char *str, uint8_t mac[6])
104 {
105     if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
106         != ETH_ADDR_SCAN_COUNT) {
107         ovs_fatal(0, "invalid mac address %s", str);
108     }
109 }
110
111 static void
112 str_to_ip(const char *str, ovs_be32 *ip)
113 {
114     struct in_addr in_addr;
115
116     if (lookup_ip(str, &in_addr)) {
117         ovs_fatal(0, "%s: could not convert to IP address", str);
118     }
119     *ip = in_addr.s_addr;
120 }
121
122 static struct ofp_action_output *
123 put_output_action(struct ofpbuf *b, uint16_t port)
124 {
125     struct ofp_action_output *oao;
126
127     oao = ofputil_put_OFPAT_OUTPUT(b);
128     oao->port = htons(port);
129     return oao;
130 }
131
132 static void
133 parse_enqueue(struct ofpbuf *b, char *arg)
134 {
135     char *sp = NULL;
136     char *port = strtok_r(arg, ":q", &sp);
137     char *queue = strtok_r(NULL, "", &sp);
138     struct ofp_action_enqueue *oae;
139
140     if (port == NULL || queue == NULL) {
141         ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
142     }
143
144     oae = ofputil_put_OFPAT_ENQUEUE(b);
145     oae->port = htons(str_to_u32(port));
146     oae->queue_id = htonl(str_to_u32(queue));
147 }
148
149 static void
150 parse_output(struct ofpbuf *b, char *arg)
151 {
152     if (strchr(arg, '[')) {
153         struct nx_action_output_reg *naor;
154         struct mf_subfield src;
155
156         mf_parse_subfield(&src, arg);
157
158         naor = ofputil_put_NXAST_OUTPUT_REG(b);
159         naor->ofs_nbits = nxm_encode_ofs_nbits(src.ofs, src.n_bits);
160         naor->src = htonl(src.field->nxm_header);
161         naor->max_len = htons(UINT16_MAX);
162     } else {
163         put_output_action(b, str_to_u32(arg));
164     }
165 }
166
167 static void
168 parse_resubmit(struct ofpbuf *b, char *arg)
169 {
170     struct nx_action_resubmit *nar;
171     char *in_port_s, *table_s;
172     uint16_t in_port;
173     uint8_t table;
174
175     in_port_s = strsep(&arg, ",");
176     if (in_port_s && in_port_s[0]) {
177         if (!ofputil_port_from_string(in_port_s, &in_port)) {
178             in_port = str_to_u32(in_port_s);
179         }
180     } else {
181         in_port = OFPP_IN_PORT;
182     }
183
184     table_s = strsep(&arg, ",");
185     table = table_s && table_s[0] ? str_to_u32(table_s) : 255;
186
187     if (in_port == OFPP_IN_PORT && table == 255) {
188         ovs_fatal(0, "at least one \"in_port\" or \"table\" must be specified "
189                   " on resubmit");
190     }
191
192     if (in_port != OFPP_IN_PORT && table == 255) {
193         nar = ofputil_put_NXAST_RESUBMIT(b);
194     } else {
195         nar = ofputil_put_NXAST_RESUBMIT_TABLE(b);
196         nar->table = table;
197     }
198     nar->in_port = htons(in_port);
199 }
200
201 static void
202 parse_set_tunnel(struct ofpbuf *b, const char *arg)
203 {
204     uint64_t tun_id = str_to_u64(arg);
205     if (tun_id > UINT32_MAX) {
206         ofputil_put_NXAST_SET_TUNNEL64(b)->tun_id = htonll(tun_id);
207     } else {
208         ofputil_put_NXAST_SET_TUNNEL(b)->tun_id = htonl(tun_id);
209     }
210 }
211
212 static void
213 parse_note(struct ofpbuf *b, const char *arg)
214 {
215     size_t start_ofs = b->size;
216     struct nx_action_note *nan;
217     int remainder;
218     size_t len;
219
220     nan = ofputil_put_NXAST_NOTE(b);
221
222     b->size -= sizeof nan->note;
223     while (*arg != '\0') {
224         uint8_t byte;
225         bool ok;
226
227         if (*arg == '.') {
228             arg++;
229         }
230         if (*arg == '\0') {
231             break;
232         }
233
234         byte = hexits_value(arg, 2, &ok);
235         if (!ok) {
236             ovs_fatal(0, "bad hex digit in `note' argument");
237         }
238         ofpbuf_put(b, &byte, 1);
239
240         arg += 2;
241     }
242
243     len = b->size - start_ofs;
244     remainder = len % OFP_ACTION_ALIGN;
245     if (remainder) {
246         ofpbuf_put_zeros(b, OFP_ACTION_ALIGN - remainder);
247     }
248     nan = (struct nx_action_note *)((char *)b->data + start_ofs);
249     nan->len = htons(b->size - start_ofs);
250 }
251
252 static void
253 parse_fin_timeout(struct ofpbuf *b, char *arg)
254 {
255     struct nx_action_fin_timeout *naft;
256     char *key, *value;
257
258     naft = ofputil_put_NXAST_FIN_TIMEOUT(b);
259     while (ofputil_parse_key_value(&arg, &key, &value)) {
260         if (!strcmp(key, "idle_timeout")) {
261             naft->fin_idle_timeout = htons(str_to_u16(value, key));
262         } else if (!strcmp(key, "hard_timeout")) {
263             naft->fin_hard_timeout = htons(str_to_u16(value, key));
264         } else {
265             ovs_fatal(0, "invalid key '%s' in 'fin_timeout' argument", key);
266         }
267     }
268 }
269
270 static void
271 parse_named_action(enum ofputil_action_code code, const struct flow *flow,
272                    struct ofpbuf *b, char *arg)
273 {
274     struct ofp_action_dl_addr *oada;
275     struct ofp_action_vlan_pcp *oavp;
276     struct ofp_action_vlan_vid *oavv;
277     struct ofp_action_nw_addr *oana;
278     struct ofp_action_tp_port *oata;
279
280     switch (code) {
281     case OFPUTIL_OFPAT_OUTPUT:
282         parse_output(b, arg);
283         break;
284
285     case OFPUTIL_OFPAT_SET_VLAN_VID:
286         oavv = ofputil_put_OFPAT_SET_VLAN_VID(b);
287         oavv->vlan_vid = htons(str_to_u32(arg));
288         break;
289
290     case OFPUTIL_OFPAT_SET_VLAN_PCP:
291         oavp = ofputil_put_OFPAT_SET_VLAN_PCP(b);
292         oavp->vlan_pcp = str_to_u32(arg);
293         break;
294
295     case OFPUTIL_OFPAT_STRIP_VLAN:
296         ofputil_put_OFPAT_STRIP_VLAN(b);
297         break;
298
299     case OFPUTIL_OFPAT_SET_DL_SRC:
300     case OFPUTIL_OFPAT_SET_DL_DST:
301         oada = ofputil_put_action(code, b);
302         str_to_mac(arg, oada->dl_addr);
303         break;
304
305     case OFPUTIL_OFPAT_SET_NW_SRC:
306     case OFPUTIL_OFPAT_SET_NW_DST:
307         oana = ofputil_put_action(code, b);
308         str_to_ip(arg, &oana->nw_addr);
309         break;
310
311     case OFPUTIL_OFPAT_SET_NW_TOS:
312         ofputil_put_OFPAT_SET_NW_TOS(b)->nw_tos = str_to_u32(arg);
313         break;
314
315     case OFPUTIL_OFPAT_SET_TP_SRC:
316     case OFPUTIL_OFPAT_SET_TP_DST:
317         oata = ofputil_put_action(code, b);
318         oata->tp_port = htons(str_to_u32(arg));
319         break;
320
321     case OFPUTIL_OFPAT_ENQUEUE:
322         parse_enqueue(b, arg);
323         break;
324
325     case OFPUTIL_NXAST_RESUBMIT:
326         parse_resubmit(b, arg);
327         break;
328
329     case OFPUTIL_NXAST_SET_TUNNEL:
330         parse_set_tunnel(b, arg);
331         break;
332
333     case OFPUTIL_NXAST_SET_QUEUE:
334         ofputil_put_NXAST_SET_QUEUE(b)->queue_id = htonl(str_to_u32(arg));
335         break;
336
337     case OFPUTIL_NXAST_POP_QUEUE:
338         ofputil_put_NXAST_POP_QUEUE(b);
339         break;
340
341     case OFPUTIL_NXAST_REG_MOVE:
342         nxm_parse_reg_move(ofputil_put_NXAST_REG_MOVE(b), arg);
343         break;
344
345     case OFPUTIL_NXAST_REG_LOAD:
346         nxm_parse_reg_load(ofputil_put_NXAST_REG_LOAD(b), arg);
347         break;
348
349     case OFPUTIL_NXAST_NOTE:
350         parse_note(b, arg);
351         break;
352
353     case OFPUTIL_NXAST_SET_TUNNEL64:
354         ofputil_put_NXAST_SET_TUNNEL64(b)->tun_id = htonll(str_to_u64(arg));
355         break;
356
357     case OFPUTIL_NXAST_MULTIPATH:
358         multipath_parse(ofputil_put_NXAST_MULTIPATH(b), arg);
359         break;
360
361     case OFPUTIL_NXAST_AUTOPATH:
362         autopath_parse(ofputil_put_NXAST_AUTOPATH(b), arg);
363         break;
364
365     case OFPUTIL_NXAST_BUNDLE:
366         bundle_parse(b, arg);
367         break;
368
369     case OFPUTIL_NXAST_BUNDLE_LOAD:
370         bundle_parse_load(b, arg);
371         break;
372
373     case OFPUTIL_NXAST_RESUBMIT_TABLE:
374     case OFPUTIL_NXAST_OUTPUT_REG:
375         NOT_REACHED();
376
377     case OFPUTIL_NXAST_LEARN:
378         learn_parse(b, arg, flow);
379         break;
380
381     case OFPUTIL_NXAST_EXIT:
382         ofputil_put_NXAST_EXIT(b);
383         break;
384
385     case OFPUTIL_NXAST_DEC_TTL:
386         ofputil_put_NXAST_DEC_TTL(b);
387         break;
388
389     case OFPUTIL_NXAST_FIN_TIMEOUT:
390         parse_fin_timeout(b, arg);
391         break;
392     }
393 }
394
395 static void
396 str_to_action(const struct flow *flow, char *str, struct ofpbuf *b)
397 {
398     char *pos, *act, *arg;
399     int n_actions;
400
401     pos = str;
402     n_actions = 0;
403     while (ofputil_parse_key_value(&pos, &act, &arg)) {
404         uint16_t port;
405         int code;
406
407         code = ofputil_action_code_from_name(act);
408         if (code >= 0) {
409             parse_named_action(code, flow, b, arg);
410         } else if (!strcasecmp(act, "drop")) {
411             /* A drop action in OpenFlow occurs by just not setting
412              * an action. */
413             if (n_actions) {
414                 ovs_fatal(0, "Drop actions must not be preceded by other "
415                           "actions");
416             } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
417                 ovs_fatal(0, "Drop actions must not be followed by other "
418                           "actions");
419             }
420             break;
421         } else if (!strcasecmp(act, "CONTROLLER")) {
422             struct ofp_action_output *oao;
423             oao = put_output_action(b, OFPP_CONTROLLER);
424
425             /* Unless a numeric argument is specified, we send the whole
426              * packet to the controller. */
427             if (arg[0] && (strspn(arg, "0123456789") == strlen(arg))) {
428                oao->max_len = htons(str_to_u32(arg));
429             } else {
430                 oao->max_len = htons(UINT16_MAX);
431             }
432         } else if (ofputil_port_from_string(act, &port)) {
433             put_output_action(b, port);
434         } else {
435             ovs_fatal(0, "Unknown action: %s", act);
436         }
437         n_actions++;
438     }
439 }
440
441 struct protocol {
442     const char *name;
443     uint16_t dl_type;
444     uint8_t nw_proto;
445 };
446
447 static bool
448 parse_protocol(const char *name, const struct protocol **p_out)
449 {
450     static const struct protocol protocols[] = {
451         { "ip", ETH_TYPE_IP, 0 },
452         { "arp", ETH_TYPE_ARP, 0 },
453         { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
454         { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
455         { "udp", ETH_TYPE_IP, IPPROTO_UDP },
456         { "ipv6", ETH_TYPE_IPV6, 0 },
457         { "ip6", ETH_TYPE_IPV6, 0 },
458         { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
459         { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
460         { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
461     };
462     const struct protocol *p;
463
464     for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
465         if (!strcmp(p->name, name)) {
466             *p_out = p;
467             return true;
468         }
469     }
470     *p_out = NULL;
471     return false;
472 }
473
474 static void
475 ofp_fatal(const char *flow, bool verbose, const char *format, ...)
476 {
477     va_list args;
478
479     if (verbose) {
480         fprintf(stderr, "%s:\n", flow);
481     }
482
483     va_start(args, format);
484     ovs_fatal_valist(0, format, args);
485 }
486
487 static void
488 parse_field(const struct mf_field *mf, const char *s, struct cls_rule *rule)
489 {
490     union mf_value value, mask;
491     char *error;
492
493     error = mf_parse(mf, s, &value, &mask);
494     if (error) {
495         ovs_fatal(0, "%s", error);
496     }
497
498     mf_set(mf, &value, &mask, rule);
499 }
500
501 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
502  * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
503  * If 'actions' is specified, an action must be in 'string' and may be expanded
504  * or reallocated.
505  *
506  * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
507  * constant for 'command'.  To parse syntax for an OFPST_FLOW or
508  * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
509 void
510 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
511               bool verbose)
512 {
513     enum {
514         F_OUT_PORT = 1 << 0,
515         F_ACTIONS = 1 << 1,
516         F_TIMEOUT = 1 << 3,
517         F_PRIORITY = 1 << 4,
518         F_FLAGS = 1 << 5,
519     } fields;
520     char *string = xstrdup(str_);
521     char *save_ptr = NULL;
522     char *act_str = NULL;
523     char *name;
524
525     switch (command) {
526     case -1:
527         fields = F_OUT_PORT;
528         break;
529
530     case OFPFC_ADD:
531         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
532         break;
533
534     case OFPFC_DELETE:
535         fields = F_OUT_PORT;
536         break;
537
538     case OFPFC_DELETE_STRICT:
539         fields = F_OUT_PORT | F_PRIORITY;
540         break;
541
542     case OFPFC_MODIFY:
543         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
544         break;
545
546     case OFPFC_MODIFY_STRICT:
547         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
548         break;
549
550     default:
551         NOT_REACHED();
552     }
553
554     cls_rule_init_catchall(&fm->cr, OFP_DEFAULT_PRIORITY);
555     fm->cookie = htonll(0);
556     fm->cookie_mask = htonll(0);
557     fm->table_id = 0xff;
558     fm->command = command;
559     fm->idle_timeout = OFP_FLOW_PERMANENT;
560     fm->hard_timeout = OFP_FLOW_PERMANENT;
561     fm->buffer_id = UINT32_MAX;
562     fm->out_port = OFPP_NONE;
563     fm->flags = 0;
564     if (fields & F_ACTIONS) {
565         act_str = strstr(string, "action");
566         if (!act_str) {
567             ofp_fatal(str_, verbose, "must specify an action");
568         }
569         *act_str = '\0';
570
571         act_str = strchr(act_str + 1, '=');
572         if (!act_str) {
573             ofp_fatal(str_, verbose, "must specify an action");
574         }
575
576         act_str++;
577     }
578     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
579          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
580         const struct protocol *p;
581
582         if (parse_protocol(name, &p)) {
583             cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
584             if (p->nw_proto) {
585                 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
586             }
587         } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
588             fm->flags |= OFPFF_SEND_FLOW_REM;
589         } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
590             fm->flags |= OFPFF_CHECK_OVERLAP;
591         } else {
592             char *value;
593
594             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
595             if (!value) {
596                 ofp_fatal(str_, verbose, "field %s missing value", name);
597             }
598
599             if (!strcmp(name, "table")) {
600                 fm->table_id = str_to_table_id(value);
601             } else if (!strcmp(name, "out_port")) {
602                 fm->out_port = atoi(value);
603             } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
604                 fm->cr.priority = str_to_u16(value, name);
605             } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
606                 fm->idle_timeout = str_to_u16(value, name);
607             } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
608                 fm->hard_timeout = str_to_u16(value, name);
609             } else if (!strcmp(name, "cookie")) {
610                 char *mask = strchr(value, '/');
611                 if (mask) {
612                     if (command == OFPFC_ADD) {
613                         ofp_fatal(str_, verbose, "flow additions cannot use "
614                                   "a cookie mask");
615                     }
616                     *mask = '\0';
617                     fm->cookie_mask = htonll(str_to_u64(mask+1));
618                 } else {
619                     fm->cookie_mask = htonll(UINT64_MAX);
620                 }
621                 fm->cookie = htonll(str_to_u64(value));
622             } else if (mf_from_name(name)) {
623                 parse_field(mf_from_name(name), value, &fm->cr);
624             } else if (!strcmp(name, "duration")
625                        || !strcmp(name, "n_packets")
626                        || !strcmp(name, "n_bytes")) {
627                 /* Ignore these, so that users can feed the output of
628                  * "ovs-ofctl dump-flows" back into commands that parse
629                  * flows. */
630             } else {
631                 ofp_fatal(str_, verbose, "unknown keyword %s", name);
632             }
633         }
634     }
635     if (fields & F_ACTIONS) {
636         struct ofpbuf actions;
637
638         ofpbuf_init(&actions, sizeof(union ofp_action));
639         str_to_action(&fm->cr.flow, act_str, &actions);
640         fm->actions = ofpbuf_steal_data(&actions);
641         fm->n_actions = actions.size / sizeof(union ofp_action);
642     } else {
643         fm->actions = NULL;
644         fm->n_actions = 0;
645     }
646
647     free(string);
648 }
649
650 /* Parses 's' as a set of OpenFlow actions and appends the actions to
651  * 'actions'.
652  *
653  * Prints an error on stderr and aborts the program if 's' syntax is
654  * invalid. */
655 void
656 parse_ofp_actions(const char *s_, struct ofpbuf *actions)
657 {
658     char *s = xstrdup(s_);
659     str_to_action(NULL, s, actions);
660     free(s);
661 }
662
663 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
664  * (one of OFPFC_*) and appends the parsed OpenFlow message to 'packets'.
665  * '*cur_format' should initially contain the flow format currently configured
666  * on the connection; this function will add a message to change the flow
667  * format and update '*cur_format', if this is necessary to add the parsed
668  * flow. */
669 void
670 parse_ofp_flow_mod_str(struct list *packets, enum nx_flow_format *cur_format,
671                        bool *flow_mod_table_id, const char *string,
672                        uint16_t command, bool verbose)
673 {
674     enum nx_flow_format min_format, next_format;
675     struct cls_rule rule_copy;
676     struct ofpbuf *ofm;
677     struct ofputil_flow_mod fm;
678
679     parse_ofp_str(&fm, command, string, verbose);
680
681     min_format = ofputil_min_flow_format(&fm.cr);
682     if (command != OFPFC_ADD && fm.cookie_mask != htonll(0)) {
683         min_format = NXFF_NXM;
684     }
685     next_format = MAX(*cur_format, min_format);
686     if (next_format != *cur_format) {
687         struct ofpbuf *sff = ofputil_make_set_flow_format(next_format);
688         list_push_back(packets, &sff->list_node);
689         *cur_format = next_format;
690     }
691
692     /* Normalize a copy of the rule.  This ensures that non-normalized flows
693      * get logged but doesn't affect what gets sent to the switch, so that the
694      * switch can do whatever it likes with the flow. */
695     rule_copy = fm.cr;
696     ofputil_normalize_rule(&rule_copy, next_format);
697
698     if (fm.table_id != 0xff && !*flow_mod_table_id) {
699         struct ofpbuf *sff = ofputil_make_flow_mod_table_id(true);
700         list_push_back(packets, &sff->list_node);
701         *flow_mod_table_id = true;
702     }
703
704     ofm = ofputil_encode_flow_mod(&fm, *cur_format, *flow_mod_table_id);
705     list_push_back(packets, &ofm->list_node);
706 }
707
708 /* Similar to parse_ofp_flow_mod_str(), except that the string is read from
709  * 'stream' and the command is always OFPFC_ADD.  Returns false if end-of-file
710  * is reached before reading a flow, otherwise true. */
711 bool
712 parse_ofp_flow_mod_file(struct list *packets,
713                         enum nx_flow_format *cur, bool *flow_mod_table_id,
714                         FILE *stream, uint16_t command)
715 {
716     struct ds s;
717     bool ok;
718
719     ds_init(&s);
720     ok = ds_get_preprocessed_line(&s, stream) == 0;
721     if (ok) {
722         parse_ofp_flow_mod_str(packets, cur, flow_mod_table_id,
723                                ds_cstr(&s), command, true);
724     }
725     ds_destroy(&s);
726
727     return ok;
728 }
729
730 void
731 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
732                                  bool aggregate, const char *string)
733 {
734     struct ofputil_flow_mod fm;
735
736     parse_ofp_str(&fm, -1, string, false);
737     fsr->aggregate = aggregate;
738     fsr->cookie = fm.cookie;
739     fsr->cookie_mask = fm.cookie_mask;
740     fsr->match = fm.cr;
741     fsr->out_port = fm.out_port;
742     fsr->table_id = fm.table_id;
743 }