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