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