ovs-ofctl: Fix del-flows command parsing bugs.
[sliver-openvswitch.git] / lib / ofp-parse.c
1 /*
2  * Copyright (c) 2010 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 "byte-order.h"
26 #include "dynamic-string.h"
27 #include "netdev.h"
28 #include "ofp-util.h"
29 #include "ofpbuf.h"
30 #include "openflow/openflow.h"
31 #include "packets.h"
32 #include "socket-util.h"
33 #include "vconn.h"
34 #include "vlog.h"
35
36 VLOG_DEFINE_THIS_MODULE(ofp_parse);
37
38 static uint32_t
39 str_to_u32(const char *str)
40 {
41     char *tail;
42     uint32_t value;
43
44     if (!str) {
45         ovs_fatal(0, "missing required numeric argument");
46     }
47
48     errno = 0;
49     value = strtoul(str, &tail, 0);
50     if (errno == EINVAL || errno == ERANGE || *tail) {
51         ovs_fatal(0, "invalid numeric format %s", str);
52     }
53     return value;
54 }
55
56 static uint64_t
57 str_to_u64(const char *str)
58 {
59     char *tail;
60     uint64_t value;
61
62     errno = 0;
63     value = strtoull(str, &tail, 0);
64     if (errno == EINVAL || errno == ERANGE || *tail) {
65         ovs_fatal(0, "invalid numeric format %s", str);
66     }
67     return value;
68 }
69
70 static void
71 str_to_mac(const char *str, uint8_t mac[6])
72 {
73     if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
74         != ETH_ADDR_SCAN_COUNT) {
75         ovs_fatal(0, "invalid mac address %s", str);
76     }
77 }
78
79 static void
80 str_to_ip(const char *str_, ovs_be32 *ip, ovs_be32 *maskp)
81 {
82     char *str = xstrdup(str_);
83     char *save_ptr = NULL;
84     const char *name, *netmask;
85     struct in_addr in_addr;
86     ovs_be32 mask;
87     int retval;
88
89     name = strtok_r(str, "/", &save_ptr);
90     retval = name ? lookup_ip(name, &in_addr) : EINVAL;
91     if (retval) {
92         ovs_fatal(0, "%s: could not convert to IP address", str);
93     }
94     *ip = in_addr.s_addr;
95
96     netmask = strtok_r(NULL, "/", &save_ptr);
97     if (netmask) {
98         uint8_t o[4];
99         if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
100                    &o[0], &o[1], &o[2], &o[3]) == 4) {
101             mask = htonl((o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3]);
102         } else {
103             int prefix = atoi(netmask);
104             if (prefix <= 0 || prefix > 32) {
105                 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
106                           str);
107             } else if (prefix == 32) {
108                 mask = htonl(UINT32_MAX);
109             } else {
110                 mask = htonl(((1u << prefix) - 1) << (32 - prefix));
111             }
112         }
113     } else {
114         mask = htonl(UINT32_MAX);
115     }
116     *ip &= mask;
117
118     if (maskp) {
119         *maskp = mask;
120     } else {
121         if (mask != htonl(UINT32_MAX)) {
122             ovs_fatal(0, "%s: netmask not allowed here", str_);
123         }
124     }
125
126     free(str);
127 }
128
129 static void *
130 put_action(struct ofpbuf *b, size_t size, uint16_t type)
131 {
132     struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
133     ah->type = htons(type);
134     ah->len = htons(size);
135     return ah;
136 }
137
138 static struct ofp_action_output *
139 put_output_action(struct ofpbuf *b, uint16_t port)
140 {
141     struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
142     oao->port = htons(port);
143     return oao;
144 }
145
146 static void
147 put_enqueue_action(struct ofpbuf *b, uint16_t port, uint32_t queue)
148 {
149     struct ofp_action_enqueue *oae = put_action(b, sizeof *oae, OFPAT_ENQUEUE);
150     oae->port = htons(port);
151     oae->queue_id = htonl(queue);
152 }
153
154 static void
155 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
156 {
157     struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
158     str_to_mac(addr, oada->dl_addr);
159 }
160
161
162 static bool
163 parse_port_name(const char *name, uint16_t *port)
164 {
165     struct pair {
166         const char *name;
167         uint16_t value;
168     };
169     static const struct pair pairs[] = {
170 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
171         DEF_PAIR(IN_PORT),
172         DEF_PAIR(TABLE),
173         DEF_PAIR(NORMAL),
174         DEF_PAIR(FLOOD),
175         DEF_PAIR(ALL),
176         DEF_PAIR(CONTROLLER),
177         DEF_PAIR(LOCAL),
178         DEF_PAIR(NONE),
179 #undef DEF_PAIR
180     };
181     static const int n_pairs = ARRAY_SIZE(pairs);
182     size_t i;
183
184     for (i = 0; i < n_pairs; i++) {
185         if (!strcasecmp(name, pairs[i].name)) {
186             *port = pairs[i].value;
187             return true;
188         }
189     }
190     return false;
191 }
192
193 static void
194 str_to_action(char *str, struct ofpbuf *b)
195 {
196     char *act, *arg;
197     char *saveptr = NULL;
198     bool drop = false;
199     int n_actions;
200
201     for (act = strtok_r(str, ", \t\r\n", &saveptr), n_actions = 0; act;
202          act = strtok_r(NULL, ", \t\r\n", &saveptr), n_actions++)
203     {
204         uint16_t port;
205
206         if (drop) {
207             ovs_fatal(0, "Drop actions must not be followed by other actions");
208         }
209
210         /* Arguments are separated by colons */
211         arg = strchr(act, ':');
212         if (arg) {
213             *arg = '\0';
214             arg++;
215         }
216
217         if (!strcasecmp(act, "mod_vlan_vid")) {
218             struct ofp_action_vlan_vid *va;
219             va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
220             va->vlan_vid = htons(str_to_u32(arg));
221         } else if (!strcasecmp(act, "mod_vlan_pcp")) {
222             struct ofp_action_vlan_pcp *va;
223             va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
224             va->vlan_pcp = str_to_u32(arg);
225         } else if (!strcasecmp(act, "strip_vlan")) {
226             struct ofp_action_header *ah;
227             ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
228             ah->type = htons(OFPAT_STRIP_VLAN);
229         } else if (!strcasecmp(act, "mod_dl_src")) {
230             put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
231         } else if (!strcasecmp(act, "mod_dl_dst")) {
232             put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
233         } else if (!strcasecmp(act, "mod_nw_src")) {
234             struct ofp_action_nw_addr *na;
235             na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
236             str_to_ip(arg, &na->nw_addr, NULL);
237         } else if (!strcasecmp(act, "mod_nw_dst")) {
238             struct ofp_action_nw_addr *na;
239             na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
240             str_to_ip(arg, &na->nw_addr, NULL);
241         } else if (!strcasecmp(act, "mod_tp_src")) {
242             struct ofp_action_tp_port *ta;
243             ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
244             ta->tp_port = htons(str_to_u32(arg));
245         } else if (!strcasecmp(act, "mod_tp_dst")) {
246             struct ofp_action_tp_port *ta;
247             ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
248             ta->tp_port = htons(str_to_u32(arg));
249         } else if (!strcasecmp(act, "mod_nw_tos")) {
250             struct ofp_action_nw_tos *nt;
251             nt = put_action(b, sizeof *nt, OFPAT_SET_NW_TOS);
252             nt->nw_tos = str_to_u32(arg);
253         } else if (!strcasecmp(act, "resubmit")) {
254             struct nx_action_resubmit *nar;
255             nar = put_action(b, sizeof *nar, OFPAT_VENDOR);
256             nar->vendor = htonl(NX_VENDOR_ID);
257             nar->subtype = htons(NXAST_RESUBMIT);
258             nar->in_port = htons(str_to_u32(arg));
259         } else if (!strcasecmp(act, "set_tunnel")) {
260             struct nx_action_set_tunnel *nast;
261             nast = put_action(b, sizeof *nast, OFPAT_VENDOR);
262             nast->vendor = htonl(NX_VENDOR_ID);
263             nast->subtype = htons(NXAST_SET_TUNNEL);
264             nast->tun_id = htonl(str_to_u32(arg));
265         } else if (!strcasecmp(act, "drop_spoofed_arp")) {
266             struct nx_action_header *nah;
267             nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
268             nah->vendor = htonl(NX_VENDOR_ID);
269             nah->subtype = htons(NXAST_DROP_SPOOFED_ARP);
270         } else if (!strcasecmp(act, "set_queue")) {
271             struct nx_action_set_queue *nasq;
272             nasq = put_action(b, sizeof *nasq, OFPAT_VENDOR);
273             nasq->vendor = htonl(NX_VENDOR_ID);
274             nasq->subtype = htons(NXAST_SET_QUEUE);
275             nasq->queue_id = htonl(str_to_u32(arg));
276         } else if (!strcasecmp(act, "pop_queue")) {
277             struct nx_action_header *nah;
278             nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
279             nah->vendor = htonl(NX_VENDOR_ID);
280             nah->subtype = htons(NXAST_POP_QUEUE);
281         } else if (!strcasecmp(act, "note")) {
282             size_t start_ofs = b->size;
283             struct nx_action_note *nan;
284             int remainder;
285             size_t len;
286
287             nan = put_action(b, sizeof *nan, OFPAT_VENDOR);
288             nan->vendor = htonl(NX_VENDOR_ID);
289             nan->subtype = htons(NXAST_NOTE);
290
291             b->size -= sizeof nan->note;
292             while (arg && *arg != '\0') {
293                 uint8_t byte;
294                 bool ok;
295
296                 if (*arg == '.') {
297                     arg++;
298                 }
299                 if (*arg == '\0') {
300                     break;
301                 }
302
303                 byte = hexits_value(arg, 2, &ok);
304                 if (!ok) {
305                     ovs_fatal(0, "bad hex digit in `note' argument");
306                 }
307                 ofpbuf_put(b, &byte, 1);
308
309                 arg += 2;
310             }
311
312             len = b->size - start_ofs;
313             remainder = len % OFP_ACTION_ALIGN;
314             if (remainder) {
315                 ofpbuf_put_zeros(b, OFP_ACTION_ALIGN - remainder);
316             }
317             nan->len = htons(b->size - start_ofs);
318         } else if (!strcasecmp(act, "output")) {
319             put_output_action(b, str_to_u32(arg));
320         } else if (!strcasecmp(act, "enqueue")) {
321             char *sp = NULL;
322             char *port_s = strtok_r(arg, ":q", &sp);
323             char *queue = strtok_r(NULL, "", &sp);
324             if (port_s == NULL || queue == NULL) {
325                 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
326             }
327             put_enqueue_action(b, str_to_u32(port_s), str_to_u32(queue));
328         } else if (!strcasecmp(act, "drop")) {
329             /* A drop action in OpenFlow occurs by just not setting
330              * an action. */
331             drop = true;
332             if (n_actions) {
333                 ovs_fatal(0, "Drop actions must not be preceded by other "
334                           "actions");
335             }
336         } else if (!strcasecmp(act, "CONTROLLER")) {
337             struct ofp_action_output *oao;
338             oao = put_output_action(b, OFPP_CONTROLLER);
339
340             /* Unless a numeric argument is specified, we send the whole
341              * packet to the controller. */
342             if (arg && (strspn(arg, "0123456789") == strlen(arg))) {
343                oao->max_len = htons(str_to_u32(arg));
344             } else {
345                 oao->max_len = htons(UINT16_MAX);
346             }
347         } else if (parse_port_name(act, &port)) {
348             put_output_action(b, port);
349         } else if (strspn(act, "0123456789") == strlen(act)) {
350             put_output_action(b, str_to_u32(act));
351         } else {
352             ovs_fatal(0, "Unknown action: %s", act);
353         }
354     }
355 }
356
357 struct protocol {
358     const char *name;
359     uint16_t dl_type;
360     uint8_t nw_proto;
361 };
362
363 static bool
364 parse_protocol(const char *name, const struct protocol **p_out)
365 {
366     static const struct protocol protocols[] = {
367         { "ip", ETH_TYPE_IP, 0 },
368         { "arp", ETH_TYPE_ARP, 0 },
369         { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
370         { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
371         { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
372     };
373     const struct protocol *p;
374
375     for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
376         if (!strcmp(p->name, name)) {
377             *p_out = p;
378             return true;
379         }
380     }
381     *p_out = NULL;
382     return false;
383 }
384
385 #define FIELDS                                              \
386     FIELD(F_TUN_ID,      "tun_id",      FWW_TUN_ID)         \
387     FIELD(F_IN_PORT,     "in_port",     FWW_IN_PORT)        \
388     FIELD(F_DL_VLAN,     "dl_vlan",     0)                  \
389     FIELD(F_DL_VLAN_PCP, "dl_vlan_pcp", 0)                  \
390     FIELD(F_DL_SRC,      "dl_src",      FWW_DL_SRC)         \
391     FIELD(F_DL_DST,      "dl_dst",      FWW_DL_DST)         \
392     FIELD(F_DL_TYPE,     "dl_type",     FWW_DL_TYPE)        \
393     FIELD(F_NW_SRC,      "nw_src",      0)                  \
394     FIELD(F_NW_DST,      "nw_dst",      0)                  \
395     FIELD(F_NW_PROTO,    "nw_proto",    FWW_NW_PROTO)       \
396     FIELD(F_NW_TOS,      "nw_tos",      FWW_NW_TOS)         \
397     FIELD(F_TP_SRC,      "tp_src",      FWW_TP_SRC)         \
398     FIELD(F_TP_DST,      "tp_dst",      FWW_TP_DST)         \
399     FIELD(F_ICMP_TYPE,   "icmp_type",   FWW_TP_SRC)         \
400     FIELD(F_ICMP_CODE,   "icmp_code",   FWW_TP_DST)
401
402 enum field_index {
403 #define FIELD(ENUM, NAME, WILDCARD) ENUM,
404     FIELDS
405 #undef FIELD
406     N_FIELDS
407 };
408
409 struct field {
410     enum field_index index;
411     const char *name;
412     flow_wildcards_t wildcard;  /* FWW_* bit. */
413 };
414
415 static bool
416 parse_field_name(const char *name, const struct field **f_out)
417 {
418     static const struct field fields[N_FIELDS] = {
419 #define FIELD(ENUM, NAME, WILDCARD) { ENUM, NAME, WILDCARD },
420         FIELDS
421 #undef FIELD
422     };
423     const struct field *f;
424
425     for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
426         if (!strcmp(f->name, name)) {
427             *f_out = f;
428             return true;
429         }
430     }
431     *f_out = NULL;
432     return false;
433 }
434
435 static void
436 parse_field_value(struct cls_rule *rule, enum field_index index,
437                   const char *value)
438 {
439     uint8_t mac[ETH_ADDR_LEN];
440     ovs_be32 ip, mask;
441     uint16_t port_no;
442
443     switch (index) {
444     case F_TUN_ID:
445         cls_rule_set_tun_id(rule, htonl(str_to_u32(value)));
446         break;
447
448     case F_IN_PORT:
449         if (!parse_port_name(value, &port_no)) {
450             port_no = atoi(value);
451         }
452         if (port_no == OFPP_LOCAL) {
453             port_no = ODPP_LOCAL;
454         }
455         cls_rule_set_in_port(rule, port_no);
456         break;
457
458     case F_DL_VLAN:
459         cls_rule_set_dl_vlan(rule, htons(str_to_u32(value)));
460         break;
461
462     case F_DL_VLAN_PCP:
463         cls_rule_set_dl_vlan_pcp(rule, str_to_u32(value));
464         break;
465
466     case F_DL_SRC:
467         str_to_mac(value, mac);
468         cls_rule_set_dl_src(rule, mac);
469         break;
470
471     case F_DL_DST:
472         str_to_mac(value, mac);
473         cls_rule_set_dl_dst(rule, mac);
474         break;
475
476     case F_DL_TYPE:
477         cls_rule_set_dl_type(rule, htons(str_to_u32(value)));
478         break;
479
480     case F_NW_SRC:
481         str_to_ip(value, &ip, &mask);
482         cls_rule_set_nw_src_masked(rule, ip, mask);
483         break;
484
485     case F_NW_DST:
486         str_to_ip(value, &ip, &mask);
487         cls_rule_set_nw_dst_masked(rule, ip, mask);
488         break;
489
490     case F_NW_PROTO:
491         cls_rule_set_nw_proto(rule, str_to_u32(value));
492         break;
493
494     case F_NW_TOS:
495         cls_rule_set_nw_tos(rule, str_to_u32(value));
496         break;
497
498     case F_TP_SRC:
499         cls_rule_set_tp_src(rule, htons(str_to_u32(value)));
500         break;
501
502     case F_TP_DST:
503         cls_rule_set_tp_dst(rule, htons(str_to_u32(value)));
504         break;
505
506     case F_ICMP_TYPE:
507         cls_rule_set_icmp_type(rule, str_to_u32(value));
508         break;
509
510     case F_ICMP_CODE:
511         cls_rule_set_icmp_code(rule, str_to_u32(value));
512         break;
513
514     case N_FIELDS:
515         NOT_REACHED();
516     }
517 }
518
519 static void
520 parse_reg_value(struct cls_rule *rule, int reg_idx, const char *value)
521 {
522     uint32_t reg_value, reg_mask;
523
524     if (!strcmp(value, "ANY") || !strcmp(value, "*")) {
525         cls_rule_set_reg_masked(rule, reg_idx, 0, 0);
526     } else if (sscanf(value, "%"SCNi32"/%"SCNi32,
527                       &reg_value, &reg_mask) == 2) {
528         cls_rule_set_reg_masked(rule, reg_idx, reg_value, reg_mask);
529     } else if (sscanf(value, "%"SCNi32, &reg_value)) {
530         cls_rule_set_reg(rule, reg_idx, reg_value);
531     } else {
532         ovs_fatal(0, "register fields must take the form <value> "
533                   "or <value>/<mask>");
534     }
535 }
536
537 /* Convert 'string' (as described in the Flow Syntax section of the ovs-ofctl
538  * man page) into 'pf'.  If 'actions' is specified, an action must be in
539  * 'string' and may be expanded or reallocated. */
540 static void
541 parse_ofp_str(struct flow_mod *fm, uint8_t *table_idx,
542               struct ofpbuf *actions, char *string)
543 {
544     char *save_ptr = NULL;
545     char *name;
546
547     if (table_idx) {
548         *table_idx = 0xff;
549     }
550     cls_rule_init_catchall(&fm->cr, OFP_DEFAULT_PRIORITY);
551     fm->cookie = htonll(0);
552     fm->command = UINT16_MAX;
553     fm->idle_timeout = OFP_FLOW_PERMANENT;
554     fm->hard_timeout = OFP_FLOW_PERMANENT;
555     fm->buffer_id = UINT32_MAX;
556     fm->out_port = OFPP_NONE;
557     fm->flags = 0;
558     if (actions) {
559         char *act_str = strstr(string, "action");
560         if (!act_str) {
561             ovs_fatal(0, "must specify an action");
562         }
563         *act_str = '\0';
564
565         act_str = strchr(act_str + 1, '=');
566         if (!act_str) {
567             ovs_fatal(0, "must specify an action");
568         }
569
570         act_str++;
571
572         str_to_action(act_str, actions);
573         fm->actions = actions->data;
574         fm->n_actions = actions->size / sizeof(union ofp_action);
575     } else {
576         fm->actions = NULL;
577         fm->n_actions = 0;
578     }
579     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
580          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
581         const struct protocol *p;
582
583         if (parse_protocol(name, &p)) {
584             cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
585             if (p->nw_proto) {
586                 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
587             }
588         } else {
589             const struct field *f;
590             char *value;
591
592             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
593             if (!value) {
594                 ovs_fatal(0, "field %s missing value", name);
595             }
596
597             if (table_idx && !strcmp(name, "table")) {
598                 *table_idx = atoi(value);
599             } else if (!strcmp(name, "out_port")) {
600                 fm->out_port = atoi(value);
601             } else if (!strcmp(name, "priority")) {
602                 fm->cr.priority = atoi(value);
603             } else if (!strcmp(name, "idle_timeout")) {
604                 fm->idle_timeout = atoi(value);
605             } else if (!strcmp(name, "hard_timeout")) {
606                 fm->hard_timeout = atoi(value);
607             } else if (!strcmp(name, "cookie")) {
608                 fm->cookie = htonll(str_to_u64(value));
609             } else if (parse_field_name(name, &f)) {
610                 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
611                     if (f->wildcard) {
612                         fm->cr.wc.wildcards |= f->wildcard;
613                         cls_rule_zero_wildcarded_fields(&fm->cr);
614                     } else if (f->index == F_NW_SRC) {
615                         cls_rule_set_nw_src_masked(&fm->cr, 0, 0);
616                     } else if (f->index == F_NW_DST) {
617                         cls_rule_set_nw_dst_masked(&fm->cr, 0, 0);
618                     } else if (f->index == F_DL_VLAN) {
619                         cls_rule_set_any_vid(&fm->cr);
620                     } else if (f->index == F_DL_VLAN_PCP) {
621                         cls_rule_set_any_pcp(&fm->cr);
622                     } else {
623                         NOT_REACHED();
624                     }
625                 } else {
626                     parse_field_value(&fm->cr, f->index, value);
627                 }
628             } else if (!strncmp(name, "reg", 3) && isdigit(name[3])) {
629                 unsigned int reg_idx = atoi(name + 3);
630                 if (reg_idx >= FLOW_N_REGS) {
631                     ovs_fatal(0, "only %d registers supported", FLOW_N_REGS);
632                 }
633                 parse_reg_value(&fm->cr, reg_idx, value);
634             } else {
635                 ovs_fatal(0, "unknown keyword %s", name);
636             }
637         }
638     }
639 }
640
641 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
642  * (one of OFPFC_*) and appends the parsed OpenFlow message to 'packets'.
643  * '*cur_format' should initially contain the flow format currently configured
644  * on the connection; this function will add a message to change the flow
645  * format and update '*cur_format', if this is necessary to add the parsed
646  * flow. */
647 void
648 parse_ofp_flow_mod_str(struct list *packets, enum nx_flow_format *cur_format,
649                        char *string, uint16_t command)
650 {
651     bool is_del = command == OFPFC_DELETE || command == OFPFC_DELETE_STRICT;
652     enum nx_flow_format min_format, next_format;
653     struct ofpbuf actions;
654     struct ofpbuf *ofm;
655     struct flow_mod fm;
656
657     ofpbuf_init(&actions, 64);
658     parse_ofp_str(&fm, NULL, is_del ? NULL : &actions, string);
659     fm.command = command;
660
661     min_format = ofputil_min_flow_format(&fm.cr, true, fm.cookie);
662     next_format = MAX(*cur_format, min_format);
663     if (next_format != *cur_format) {
664         struct ofpbuf *sff = ofputil_make_set_flow_format(next_format);
665         list_push_back(packets, &sff->list_node);
666         *cur_format = next_format;
667     }
668
669     ofm = ofputil_encode_flow_mod(&fm, *cur_format);
670     list_push_back(packets, &ofm->list_node);
671
672     ofpbuf_uninit(&actions);
673 }
674
675 /* Similar to parse_ofp_flow_mod_str(), except that the string is read from
676  * 'stream' and the command is always OFPFC_ADD.  Returns false if end-of-file
677  * is reached before reading a flow, otherwise true. */
678 bool
679 parse_ofp_add_flow_file(struct list *packets, enum nx_flow_format *cur,
680                         FILE *stream)
681 {
682     struct ds s = DS_EMPTY_INITIALIZER;
683     bool ok = false;
684
685     while (!ds_get_line(&s, stream)) {
686         char *line = ds_cstr(&s);
687         char *comment;
688
689         /* Delete comments. */
690         comment = strchr(line, '#');
691         if (comment) {
692             *comment = '\0';
693         }
694
695         /* Drop empty lines. */
696         if (line[strspn(line, " \t\n")] == '\0') {
697             continue;
698         }
699
700         parse_ofp_flow_mod_str(packets, cur, line, OFPFC_ADD);
701         ok = true;
702         break;
703     }
704     ds_destroy(&s);
705
706     return ok;
707 }
708
709 void
710 parse_ofp_flow_stats_request_str(struct flow_stats_request *fsr,
711                                  bool aggregate, char *string)
712 {
713     struct flow_mod fm;
714     uint8_t table_id;
715
716     parse_ofp_str(&fm, &table_id, NULL, string);
717     fsr->aggregate = aggregate;
718     fsr->match = fm.cr;
719     fsr->out_port = fm.out_port;
720     fsr->table_id = table_id;
721 }
722