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