Expand tunnel IDs from 32 to 64 bits.
[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                    || !strcasecmp(act, "set_tunnel64")) {
262             uint64_t tun_id = str_to_u64(arg);
263             if (!strcasecmp(act, "set_tunnel64") || tun_id > UINT32_MAX) {
264                 struct nx_action_set_tunnel64 *nast64;
265                 nast64 = put_action(b, sizeof *nast64, OFPAT_VENDOR);
266                 nast64->vendor = htonl(NX_VENDOR_ID);
267                 nast64->subtype = htons(NXAST_SET_TUNNEL64);
268                 nast64->tun_id = htonll(tun_id);
269             } else {
270                 struct nx_action_set_tunnel *nast;
271                 nast = put_action(b, sizeof *nast, OFPAT_VENDOR);
272                 nast->vendor = htonl(NX_VENDOR_ID);
273                 nast->subtype = htons(NXAST_SET_TUNNEL);
274                 nast->tun_id = htonl(tun_id);
275             }
276         } else if (!strcasecmp(act, "drop_spoofed_arp")) {
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_DROP_SPOOFED_ARP);
281         } else if (!strcasecmp(act, "set_queue")) {
282             struct nx_action_set_queue *nasq;
283             nasq = put_action(b, sizeof *nasq, OFPAT_VENDOR);
284             nasq->vendor = htonl(NX_VENDOR_ID);
285             nasq->subtype = htons(NXAST_SET_QUEUE);
286             nasq->queue_id = htonl(str_to_u32(arg));
287         } else if (!strcasecmp(act, "pop_queue")) {
288             struct nx_action_header *nah;
289             nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
290             nah->vendor = htonl(NX_VENDOR_ID);
291             nah->subtype = htons(NXAST_POP_QUEUE);
292         } else if (!strcasecmp(act, "note")) {
293             size_t start_ofs = b->size;
294             struct nx_action_note *nan;
295             int remainder;
296             size_t len;
297
298             nan = put_action(b, sizeof *nan, OFPAT_VENDOR);
299             nan->vendor = htonl(NX_VENDOR_ID);
300             nan->subtype = htons(NXAST_NOTE);
301
302             b->size -= sizeof nan->note;
303             while (arg && *arg != '\0') {
304                 uint8_t byte;
305                 bool ok;
306
307                 if (*arg == '.') {
308                     arg++;
309                 }
310                 if (*arg == '\0') {
311                     break;
312                 }
313
314                 byte = hexits_value(arg, 2, &ok);
315                 if (!ok) {
316                     ovs_fatal(0, "bad hex digit in `note' argument");
317                 }
318                 ofpbuf_put(b, &byte, 1);
319
320                 arg += 2;
321             }
322
323             len = b->size - start_ofs;
324             remainder = len % OFP_ACTION_ALIGN;
325             if (remainder) {
326                 ofpbuf_put_zeros(b, OFP_ACTION_ALIGN - remainder);
327             }
328             nan->len = htons(b->size - start_ofs);
329         } else if (!strcasecmp(act, "move")) {
330             struct nx_action_reg_move *move;
331             move = ofpbuf_put_uninit(b, sizeof *move);
332             nxm_parse_reg_move(move, arg);
333         } else if (!strcasecmp(act, "load")) {
334             struct nx_action_reg_load *load;
335             load = ofpbuf_put_uninit(b, sizeof *load);
336             nxm_parse_reg_load(load, arg);
337         } else if (!strcasecmp(act, "output")) {
338             put_output_action(b, str_to_u32(arg));
339         } else if (!strcasecmp(act, "enqueue")) {
340             char *sp = NULL;
341             char *port_s = strtok_r(arg, ":q", &sp);
342             char *queue = strtok_r(NULL, "", &sp);
343             if (port_s == NULL || queue == NULL) {
344                 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
345             }
346             put_enqueue_action(b, str_to_u32(port_s), str_to_u32(queue));
347         } else if (!strcasecmp(act, "drop")) {
348             /* A drop action in OpenFlow occurs by just not setting
349              * an action. */
350             drop = true;
351             if (n_actions) {
352                 ovs_fatal(0, "Drop actions must not be preceded by other "
353                           "actions");
354             }
355         } else if (!strcasecmp(act, "CONTROLLER")) {
356             struct ofp_action_output *oao;
357             oao = put_output_action(b, OFPP_CONTROLLER);
358
359             /* Unless a numeric argument is specified, we send the whole
360              * packet to the controller. */
361             if (arg && (strspn(arg, "0123456789") == strlen(arg))) {
362                oao->max_len = htons(str_to_u32(arg));
363             } else {
364                 oao->max_len = htons(UINT16_MAX);
365             }
366         } else if (parse_port_name(act, &port)) {
367             put_output_action(b, port);
368         } else if (strspn(act, "0123456789") == strlen(act)) {
369             put_output_action(b, str_to_u32(act));
370         } else {
371             ovs_fatal(0, "Unknown action: %s", act);
372         }
373     }
374 }
375
376 struct protocol {
377     const char *name;
378     uint16_t dl_type;
379     uint8_t nw_proto;
380 };
381
382 static bool
383 parse_protocol(const char *name, const struct protocol **p_out)
384 {
385     static const struct protocol protocols[] = {
386         { "ip", ETH_TYPE_IP, 0 },
387         { "arp", ETH_TYPE_ARP, 0 },
388         { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
389         { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
390         { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
391     };
392     const struct protocol *p;
393
394     for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
395         if (!strcmp(p->name, name)) {
396             *p_out = p;
397             return true;
398         }
399     }
400     *p_out = NULL;
401     return false;
402 }
403
404 #define FIELDS                                              \
405     FIELD(F_TUN_ID,      "tun_id",      FWW_TUN_ID)         \
406     FIELD(F_IN_PORT,     "in_port",     FWW_IN_PORT)        \
407     FIELD(F_DL_VLAN,     "dl_vlan",     0)                  \
408     FIELD(F_DL_VLAN_PCP, "dl_vlan_pcp", 0)                  \
409     FIELD(F_DL_SRC,      "dl_src",      FWW_DL_SRC)         \
410     FIELD(F_DL_DST,      "dl_dst",      FWW_DL_DST)         \
411     FIELD(F_DL_TYPE,     "dl_type",     FWW_DL_TYPE)        \
412     FIELD(F_NW_SRC,      "nw_src",      0)                  \
413     FIELD(F_NW_DST,      "nw_dst",      0)                  \
414     FIELD(F_NW_PROTO,    "nw_proto",    FWW_NW_PROTO)       \
415     FIELD(F_NW_TOS,      "nw_tos",      FWW_NW_TOS)         \
416     FIELD(F_TP_SRC,      "tp_src",      FWW_TP_SRC)         \
417     FIELD(F_TP_DST,      "tp_dst",      FWW_TP_DST)         \
418     FIELD(F_ICMP_TYPE,   "icmp_type",   FWW_TP_SRC)         \
419     FIELD(F_ICMP_CODE,   "icmp_code",   FWW_TP_DST)
420
421 enum field_index {
422 #define FIELD(ENUM, NAME, WILDCARD) ENUM,
423     FIELDS
424 #undef FIELD
425     N_FIELDS
426 };
427
428 struct field {
429     enum field_index index;
430     const char *name;
431     flow_wildcards_t wildcard;  /* FWW_* bit. */
432 };
433
434 static bool
435 parse_field_name(const char *name, const struct field **f_out)
436 {
437     static const struct field fields[N_FIELDS] = {
438 #define FIELD(ENUM, NAME, WILDCARD) { ENUM, NAME, WILDCARD },
439         FIELDS
440 #undef FIELD
441     };
442     const struct field *f;
443
444     for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
445         if (!strcmp(f->name, name)) {
446             *f_out = f;
447             return true;
448         }
449     }
450     *f_out = NULL;
451     return false;
452 }
453
454 static void
455 parse_field_value(struct cls_rule *rule, enum field_index index,
456                   const char *value)
457 {
458     uint8_t mac[ETH_ADDR_LEN];
459     ovs_be32 ip, mask;
460     uint16_t port_no;
461
462     switch (index) {
463     case F_TUN_ID:
464         cls_rule_set_tun_id(rule, htonll(str_to_u64(value)));
465         break;
466
467     case F_IN_PORT:
468         if (!parse_port_name(value, &port_no)) {
469             port_no = atoi(value);
470         }
471         if (port_no == OFPP_LOCAL) {
472             port_no = ODPP_LOCAL;
473         }
474         cls_rule_set_in_port(rule, port_no);
475         break;
476
477     case F_DL_VLAN:
478         cls_rule_set_dl_vlan(rule, htons(str_to_u32(value)));
479         break;
480
481     case F_DL_VLAN_PCP:
482         cls_rule_set_dl_vlan_pcp(rule, str_to_u32(value));
483         break;
484
485     case F_DL_SRC:
486         str_to_mac(value, mac);
487         cls_rule_set_dl_src(rule, mac);
488         break;
489
490     case F_DL_DST:
491         str_to_mac(value, mac);
492         cls_rule_set_dl_dst(rule, mac);
493         break;
494
495     case F_DL_TYPE:
496         cls_rule_set_dl_type(rule, htons(str_to_u32(value)));
497         break;
498
499     case F_NW_SRC:
500         str_to_ip(value, &ip, &mask);
501         cls_rule_set_nw_src_masked(rule, ip, mask);
502         break;
503
504     case F_NW_DST:
505         str_to_ip(value, &ip, &mask);
506         cls_rule_set_nw_dst_masked(rule, ip, mask);
507         break;
508
509     case F_NW_PROTO:
510         cls_rule_set_nw_proto(rule, str_to_u32(value));
511         break;
512
513     case F_NW_TOS:
514         cls_rule_set_nw_tos(rule, str_to_u32(value));
515         break;
516
517     case F_TP_SRC:
518         cls_rule_set_tp_src(rule, htons(str_to_u32(value)));
519         break;
520
521     case F_TP_DST:
522         cls_rule_set_tp_dst(rule, htons(str_to_u32(value)));
523         break;
524
525     case F_ICMP_TYPE:
526         cls_rule_set_icmp_type(rule, str_to_u32(value));
527         break;
528
529     case F_ICMP_CODE:
530         cls_rule_set_icmp_code(rule, str_to_u32(value));
531         break;
532
533     case N_FIELDS:
534         NOT_REACHED();
535     }
536 }
537
538 static void
539 parse_reg_value(struct cls_rule *rule, int reg_idx, const char *value)
540 {
541     uint32_t reg_value, reg_mask;
542
543     if (!strcmp(value, "ANY") || !strcmp(value, "*")) {
544         cls_rule_set_reg_masked(rule, reg_idx, 0, 0);
545     } else if (sscanf(value, "%"SCNi32"/%"SCNi32,
546                       &reg_value, &reg_mask) == 2) {
547         cls_rule_set_reg_masked(rule, reg_idx, reg_value, reg_mask);
548     } else if (sscanf(value, "%"SCNi32, &reg_value)) {
549         cls_rule_set_reg(rule, reg_idx, reg_value);
550     } else {
551         ovs_fatal(0, "register fields must take the form <value> "
552                   "or <value>/<mask>");
553     }
554 }
555
556 /* Convert 'string' (as described in the Flow Syntax section of the ovs-ofctl
557  * man page) into 'pf'.  If 'actions' is specified, an action must be in
558  * 'string' and may be expanded or reallocated. */
559 static void
560 parse_ofp_str(struct flow_mod *fm, uint8_t *table_idx,
561               struct ofpbuf *actions, char *string)
562 {
563     char *save_ptr = NULL;
564     char *name;
565
566     if (table_idx) {
567         *table_idx = 0xff;
568     }
569     cls_rule_init_catchall(&fm->cr, OFP_DEFAULT_PRIORITY);
570     fm->cookie = htonll(0);
571     fm->command = UINT16_MAX;
572     fm->idle_timeout = OFP_FLOW_PERMANENT;
573     fm->hard_timeout = OFP_FLOW_PERMANENT;
574     fm->buffer_id = UINT32_MAX;
575     fm->out_port = OFPP_NONE;
576     fm->flags = 0;
577     if (actions) {
578         char *act_str = strstr(string, "action");
579         if (!act_str) {
580             ovs_fatal(0, "must specify an action");
581         }
582         *act_str = '\0';
583
584         act_str = strchr(act_str + 1, '=');
585         if (!act_str) {
586             ovs_fatal(0, "must specify an action");
587         }
588
589         act_str++;
590
591         str_to_action(act_str, actions);
592         fm->actions = actions->data;
593         fm->n_actions = actions->size / sizeof(union ofp_action);
594     } else {
595         fm->actions = NULL;
596         fm->n_actions = 0;
597     }
598     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
599          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
600         const struct protocol *p;
601
602         if (parse_protocol(name, &p)) {
603             cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
604             if (p->nw_proto) {
605                 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
606             }
607         } else {
608             const struct field *f;
609             char *value;
610
611             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
612             if (!value) {
613                 ovs_fatal(0, "field %s missing value", name);
614             }
615
616             if (table_idx && !strcmp(name, "table")) {
617                 *table_idx = atoi(value);
618             } else if (!strcmp(name, "out_port")) {
619                 fm->out_port = atoi(value);
620             } else if (!strcmp(name, "priority")) {
621                 fm->cr.priority = atoi(value);
622             } else if (!strcmp(name, "idle_timeout")) {
623                 fm->idle_timeout = atoi(value);
624             } else if (!strcmp(name, "hard_timeout")) {
625                 fm->hard_timeout = atoi(value);
626             } else if (!strcmp(name, "cookie")) {
627                 fm->cookie = htonll(str_to_u64(value));
628             } else if (parse_field_name(name, &f)) {
629                 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
630                     if (f->wildcard) {
631                         fm->cr.wc.wildcards |= f->wildcard;
632                         cls_rule_zero_wildcarded_fields(&fm->cr);
633                     } else if (f->index == F_NW_SRC) {
634                         cls_rule_set_nw_src_masked(&fm->cr, 0, 0);
635                     } else if (f->index == F_NW_DST) {
636                         cls_rule_set_nw_dst_masked(&fm->cr, 0, 0);
637                     } else if (f->index == F_DL_VLAN) {
638                         cls_rule_set_any_vid(&fm->cr);
639                     } else if (f->index == F_DL_VLAN_PCP) {
640                         cls_rule_set_any_pcp(&fm->cr);
641                     } else {
642                         NOT_REACHED();
643                     }
644                 } else {
645                     parse_field_value(&fm->cr, f->index, value);
646                 }
647             } else if (!strncmp(name, "reg", 3) && isdigit(name[3])) {
648                 unsigned int reg_idx = atoi(name + 3);
649                 if (reg_idx >= FLOW_N_REGS) {
650                     ovs_fatal(0, "only %d registers supported", FLOW_N_REGS);
651                 }
652                 parse_reg_value(&fm->cr, reg_idx, value);
653             } else {
654                 ovs_fatal(0, "unknown keyword %s", name);
655             }
656         }
657     }
658 }
659
660 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
661  * (one of OFPFC_*) and appends the parsed OpenFlow message to 'packets'.
662  * '*cur_format' should initially contain the flow format currently configured
663  * on the connection; this function will add a message to change the flow
664  * format and update '*cur_format', if this is necessary to add the parsed
665  * flow. */
666 void
667 parse_ofp_flow_mod_str(struct list *packets, enum nx_flow_format *cur_format,
668                        char *string, uint16_t command)
669 {
670     bool is_del = command == OFPFC_DELETE || command == OFPFC_DELETE_STRICT;
671     enum nx_flow_format min_format, next_format;
672     struct ofpbuf actions;
673     struct ofpbuf *ofm;
674     struct flow_mod fm;
675
676     ofpbuf_init(&actions, 64);
677     parse_ofp_str(&fm, NULL, is_del ? NULL : &actions, string);
678     fm.command = command;
679
680     min_format = ofputil_min_flow_format(&fm.cr, true, fm.cookie);
681     next_format = MAX(*cur_format, min_format);
682     if (next_format != *cur_format) {
683         struct ofpbuf *sff = ofputil_make_set_flow_format(next_format);
684         list_push_back(packets, &sff->list_node);
685         *cur_format = next_format;
686     }
687
688     ofm = ofputil_encode_flow_mod(&fm, *cur_format);
689     list_push_back(packets, &ofm->list_node);
690
691     ofpbuf_uninit(&actions);
692 }
693
694 /* Similar to parse_ofp_flow_mod_str(), except that the string is read from
695  * 'stream' and the command is always OFPFC_ADD.  Returns false if end-of-file
696  * is reached before reading a flow, otherwise true. */
697 bool
698 parse_ofp_add_flow_file(struct list *packets, enum nx_flow_format *cur,
699                         FILE *stream)
700 {
701     struct ds s = DS_EMPTY_INITIALIZER;
702     bool ok = false;
703
704     while (!ds_get_line(&s, stream)) {
705         char *line = ds_cstr(&s);
706         char *comment;
707
708         /* Delete comments. */
709         comment = strchr(line, '#');
710         if (comment) {
711             *comment = '\0';
712         }
713
714         /* Drop empty lines. */
715         if (line[strspn(line, " \t\n")] == '\0') {
716             continue;
717         }
718
719         parse_ofp_flow_mod_str(packets, cur, line, OFPFC_ADD);
720         ok = true;
721         break;
722     }
723     ds_destroy(&s);
724
725     return ok;
726 }
727
728 void
729 parse_ofp_flow_stats_request_str(struct flow_stats_request *fsr,
730                                  bool aggregate, char *string)
731 {
732     struct flow_mod fm;
733     uint8_t table_id;
734
735     parse_ofp_str(&fm, &table_id, NULL, string);
736     fsr->aggregate = aggregate;
737     fsr->match = fm.cr;
738     fsr->out_port = fm.out_port;
739     fsr->table_id = table_id;
740 }
741