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