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