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