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