ovs-ofctl, ovs-controller: Disable flow idle timeout by default.
[sliver-openvswitch.git] / lib / ofp-parse.c
1 /*
2  * Copyright (c) 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "ofp-parse.h"
20
21 #include <errno.h>
22 #include <stdlib.h>
23
24 #include "netdev.h"
25 #include "ofp-util.h"
26 #include "ofpbuf.h"
27 #include "openflow/openflow.h"
28 #include "packets.h"
29 #include "socket-util.h"
30 #include "vconn.h"
31 #include "vlog.h"
32
33
34 VLOG_DEFINE_THIS_MODULE(ofp_parse)
35
36 static uint32_t
37 str_to_u32(const char *str)
38 {
39     char *tail;
40     uint32_t value;
41
42     errno = 0;
43     value = strtoul(str, &tail, 0);
44     if (errno == EINVAL || errno == ERANGE || *tail) {
45         ovs_fatal(0, "invalid numeric format %s", str);
46     }
47     return value;
48 }
49
50 static uint64_t
51 str_to_u64(const char *str)
52 {
53     char *tail;
54     uint64_t value;
55
56     errno = 0;
57     value = strtoull(str, &tail, 0);
58     if (errno == EINVAL || errno == ERANGE || *tail) {
59         ovs_fatal(0, "invalid numeric format %s", str);
60     }
61     return value;
62 }
63
64 static void
65 str_to_mac(const char *str, uint8_t mac[6])
66 {
67     if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
68         != ETH_ADDR_SCAN_COUNT) {
69         ovs_fatal(0, "invalid mac address %s", str);
70     }
71 }
72
73 static uint32_t
74 str_to_ip(const char *str_, uint32_t *ip)
75 {
76     char *str = xstrdup(str_);
77     char *save_ptr = NULL;
78     const char *name, *netmask;
79     struct in_addr in_addr;
80     int n_wild, retval;
81
82     name = strtok_r(str, "/", &save_ptr);
83     retval = name ? lookup_ip(name, &in_addr) : EINVAL;
84     if (retval) {
85         ovs_fatal(0, "%s: could not convert to IP address", str);
86     }
87     *ip = in_addr.s_addr;
88
89     netmask = strtok_r(NULL, "/", &save_ptr);
90     if (netmask) {
91         uint8_t o[4];
92         if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
93                    &o[0], &o[1], &o[2], &o[3]) == 4) {
94             uint32_t nm = (o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3];
95             int i;
96
97             /* Find first 1-bit. */
98             for (i = 0; i < 32; i++) {
99                 if (nm & (1u << i)) {
100                     break;
101                 }
102             }
103             n_wild = i;
104
105             /* Verify that the rest of the bits are 1-bits. */
106             for (; i < 32; i++) {
107                 if (!(nm & (1u << i))) {
108                     ovs_fatal(0, "%s: %s is not a valid netmask",
109                               str, netmask);
110                 }
111             }
112         } else {
113             int prefix = atoi(netmask);
114             if (prefix <= 0 || prefix > 32) {
115                 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
116                           str);
117             }
118             n_wild = 32 - prefix;
119         }
120     } else {
121         n_wild = 0;
122     }
123
124     free(str);
125     return n_wild;
126 }
127
128 static void *
129 put_action(struct ofpbuf *b, size_t size, uint16_t type)
130 {
131     struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
132     ah->type = htons(type);
133     ah->len = htons(size);
134     return ah;
135 }
136
137 static struct ofp_action_output *
138 put_output_action(struct ofpbuf *b, uint16_t port)
139 {
140     struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
141     oao->port = htons(port);
142     return oao;
143 }
144
145 static void
146 put_enqueue_action(struct ofpbuf *b, uint16_t port, uint32_t queue)
147 {
148     struct ofp_action_enqueue *oae = put_action(b, sizeof *oae, OFPAT_ENQUEUE);
149     oae->port = htons(port);
150     oae->queue_id = htonl(queue);
151 }
152
153 static void
154 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
155 {
156     struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
157     str_to_mac(addr, oada->dl_addr);
158 }
159
160
161 static bool
162 parse_port_name(const char *name, uint16_t *port)
163 {
164     struct pair {
165         const char *name;
166         uint16_t value;
167     };
168     static const struct pair pairs[] = {
169 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
170         DEF_PAIR(IN_PORT),
171         DEF_PAIR(TABLE),
172         DEF_PAIR(NORMAL),
173         DEF_PAIR(FLOOD),
174         DEF_PAIR(ALL),
175         DEF_PAIR(CONTROLLER),
176         DEF_PAIR(LOCAL),
177         DEF_PAIR(NONE),
178 #undef DEF_PAIR
179     };
180     static const int n_pairs = ARRAY_SIZE(pairs);
181     size_t i;
182
183     for (i = 0; i < n_pairs; i++) {
184         if (!strcasecmp(name, pairs[i].name)) {
185             *port = pairs[i].value;
186             return true;
187         }
188     }
189     return false;
190 }
191
192 static void
193 str_to_action(char *str, struct ofpbuf *b)
194 {
195     char *act, *arg;
196     char *saveptr = NULL;
197     bool drop = false;
198     int n_actions;
199
200     for (act = strtok_r(str, ", \t\r\n", &saveptr), n_actions = 0; act;
201          act = strtok_r(NULL, ", \t\r\n", &saveptr), n_actions++)
202     {
203         uint16_t port;
204
205         if (drop) {
206             ovs_fatal(0, "Drop actions must not be followed by other actions");
207         }
208
209         /* Arguments are separated by colons */
210         arg = strchr(act, ':');
211         if (arg) {
212             *arg = '\0';
213             arg++;
214         }
215
216         if (!strcasecmp(act, "mod_vlan_vid")) {
217             struct ofp_action_vlan_vid *va;
218             va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
219             va->vlan_vid = htons(str_to_u32(arg));
220         } else if (!strcasecmp(act, "mod_vlan_pcp")) {
221             struct ofp_action_vlan_pcp *va;
222             va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
223             va->vlan_pcp = str_to_u32(arg);
224         } else if (!strcasecmp(act, "strip_vlan")) {
225             struct ofp_action_header *ah;
226             ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
227             ah->type = htons(OFPAT_STRIP_VLAN);
228         } else if (!strcasecmp(act, "mod_dl_src")) {
229             put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
230         } else if (!strcasecmp(act, "mod_dl_dst")) {
231             put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
232         } else if (!strcasecmp(act, "mod_nw_src")) {
233             struct ofp_action_nw_addr *na;
234             na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
235             str_to_ip(arg, &na->nw_addr);
236         } else if (!strcasecmp(act, "mod_nw_dst")) {
237             struct ofp_action_nw_addr *na;
238             na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
239             str_to_ip(arg, &na->nw_addr);
240         } else if (!strcasecmp(act, "mod_tp_src")) {
241             struct ofp_action_tp_port *ta;
242             ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
243             ta->tp_port = htons(str_to_u32(arg));
244         } else if (!strcasecmp(act, "mod_tp_dst")) {
245             struct ofp_action_tp_port *ta;
246             ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
247             ta->tp_port = htons(str_to_u32(arg));
248         } else if (!strcasecmp(act, "mod_nw_tos")) {
249             struct ofp_action_nw_tos *nt;
250             nt = put_action(b, sizeof *nt, OFPAT_SET_NW_TOS);
251             nt->nw_tos = str_to_u32(arg);
252         } else if (!strcasecmp(act, "resubmit")) {
253             struct nx_action_resubmit *nar;
254             nar = put_action(b, sizeof *nar, OFPAT_VENDOR);
255             nar->vendor = htonl(NX_VENDOR_ID);
256             nar->subtype = htons(NXAST_RESUBMIT);
257             nar->in_port = htons(str_to_u32(arg));
258         } else if (!strcasecmp(act, "set_tunnel")) {
259             struct nx_action_set_tunnel *nast;
260             nast = put_action(b, sizeof *nast, OFPAT_VENDOR);
261             nast->vendor = htonl(NX_VENDOR_ID);
262             nast->subtype = htons(NXAST_SET_TUNNEL);
263             nast->tun_id = htonl(str_to_u32(arg));
264         } else if (!strcasecmp(act, "drop_spoofed_arp")) {
265             struct nx_action_header *nah;
266             nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
267             nah->vendor = htonl(NX_VENDOR_ID);
268             nah->subtype = htons(NXAST_DROP_SPOOFED_ARP);
269         } else if (!strcasecmp(act, "output")) {
270             put_output_action(b, str_to_u32(arg));
271         } else if (!strcasecmp(act, "enqueue")) {
272             char *sp = NULL;
273             char *port = strtok_r(arg, ":q", &sp);
274             char *queue = strtok_r(NULL, "", &sp);
275             if (port == NULL || queue == NULL) {
276                 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
277             }
278             put_enqueue_action(b, str_to_u32(port), str_to_u32(queue));
279         } else if (!strcasecmp(act, "drop")) {
280             /* A drop action in OpenFlow occurs by just not setting
281              * an action. */
282             drop = true;
283             if (n_actions) {
284                 ovs_fatal(0, "Drop actions must not be preceded by other "
285                           "actions");
286             }
287         } else if (!strcasecmp(act, "CONTROLLER")) {
288             struct ofp_action_output *oao;
289             oao = put_output_action(b, OFPP_CONTROLLER);
290
291             /* Unless a numeric argument is specified, we send the whole
292              * packet to the controller. */
293             if (arg && (strspn(arg, "0123456789") == strlen(arg))) {
294                oao->max_len = htons(str_to_u32(arg));
295             } else {
296                 oao->max_len = htons(UINT16_MAX);
297             }
298         } else if (parse_port_name(act, &port)) {
299             put_output_action(b, port);
300         } else if (strspn(act, "0123456789") == strlen(act)) {
301             put_output_action(b, str_to_u32(act));
302         } else {
303             ovs_fatal(0, "Unknown action: %s", act);
304         }
305     }
306 }
307
308 struct protocol {
309     const char *name;
310     uint16_t dl_type;
311     uint8_t nw_proto;
312 };
313
314 static bool
315 parse_protocol(const char *name, const struct protocol **p_out)
316 {
317     static const struct protocol protocols[] = {
318         { "ip", ETH_TYPE_IP, 0 },
319         { "arp", ETH_TYPE_ARP, 0 },
320         { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
321         { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
322         { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
323     };
324     const struct protocol *p;
325
326     for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
327         if (!strcmp(p->name, name)) {
328             *p_out = p;
329             return true;
330         }
331     }
332     *p_out = NULL;
333     return false;
334 }
335
336 struct field {
337     const char *name;
338     uint32_t wildcard;
339     enum { F_U8, F_U16, F_MAC, F_IP } type;
340     size_t offset, shift;
341 };
342
343 static bool
344 parse_field(const char *name, const struct field **f_out)
345 {
346 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
347     static const struct field fields[] = {
348         { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port), 0 },
349         { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan), 0 },
350         { "dl_vlan_pcp", OFPFW_DL_VLAN_PCP, F_U8, F_OFS(dl_vlan_pcp), 0 },
351         { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src), 0 },
352         { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst), 0 },
353         { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type), 0 },
354         { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
355           F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
356         { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
357           F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
358         { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto), 0 },
359         { "nw_tos", OFPFW_NW_TOS, F_U8, F_OFS(nw_tos), 0 },
360         { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src), 0 },
361         { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst), 0 },
362         { "icmp_type", OFPFW_ICMP_TYPE, F_U16, F_OFS(icmp_type), 0 },
363         { "icmp_code", OFPFW_ICMP_CODE, F_U16, F_OFS(icmp_code), 0 }
364     };
365     const struct field *f;
366
367     for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
368         if (!strcmp(f->name, name)) {
369             *f_out = f;
370             return true;
371         }
372     }
373     *f_out = NULL;
374     return false;
375 }
376
377 /* Convert 'string' (as described in the Flow Syntax section of the
378  * ovs-ofctl man page) into 'match'.  The other arguments are optional
379  * and may be NULL if their value is not needed.  If 'actions' is
380  * specified, an action must be in 'string' and may be expanded or
381  * reallocated. */
382 void
383 parse_ofp_str(char *string, struct ofp_match *match, struct ofpbuf *actions,
384               uint8_t *table_idx, uint16_t *out_port, uint16_t *priority,
385               uint16_t *idle_timeout, uint16_t *hard_timeout,
386               uint64_t *cookie)
387 {
388     struct ofp_match normalized;
389     char *save_ptr = NULL;
390     char *name;
391     uint32_t wildcards;
392
393     if (table_idx) {
394         *table_idx = 0xff;
395     }
396     if (out_port) {
397         *out_port = OFPP_NONE;
398     }
399     if (priority) {
400         *priority = OFP_DEFAULT_PRIORITY;
401     }
402     if (idle_timeout) {
403         *idle_timeout = OFP_FLOW_PERMANENT;
404     }
405     if (hard_timeout) {
406         *hard_timeout = OFP_FLOW_PERMANENT;
407     }
408     if (cookie) {
409         *cookie = 0;
410     }
411     if (actions) {
412         char *act_str = strstr(string, "action");
413         if (!act_str) {
414             ovs_fatal(0, "must specify an action");
415         }
416         *act_str = '\0';
417
418         act_str = strchr(act_str + 1, '=');
419         if (!act_str) {
420             ovs_fatal(0, "must specify an action");
421         }
422
423         act_str++;
424
425         str_to_action(act_str, actions);
426     }
427     memset(match, 0, sizeof *match);
428     wildcards = OFPFW_ALL;
429     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
430          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
431         const struct protocol *p;
432
433         if (parse_protocol(name, &p)) {
434             wildcards &= ~OFPFW_DL_TYPE;
435             match->dl_type = htons(p->dl_type);
436             if (p->nw_proto) {
437                 wildcards &= ~OFPFW_NW_PROTO;
438                 match->nw_proto = p->nw_proto;
439             }
440         } else {
441             const struct field *f;
442             char *value;
443
444             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
445             if (!value) {
446                 ovs_fatal(0, "field %s missing value", name);
447             }
448
449             if (table_idx && !strcmp(name, "table")) {
450                 *table_idx = atoi(value);
451             } else if (out_port && !strcmp(name, "out_port")) {
452                 *out_port = atoi(value);
453             } else if (priority && !strcmp(name, "priority")) {
454                 *priority = atoi(value);
455             } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
456                 *idle_timeout = atoi(value);
457             } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
458                 *hard_timeout = atoi(value);
459             } else if (cookie && !strcmp(name, "cookie")) {
460                 *cookie = str_to_u64(value);
461             } else if (!strcmp(name, "tun_id_wild")) {
462                 wildcards |= NXFW_TUN_ID;
463             } else if (parse_field(name, &f)) {
464                 void *data = (char *) match + f->offset;
465                 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
466                     wildcards |= f->wildcard;
467                 } else {
468                     wildcards &= ~f->wildcard;
469                     if (f->wildcard == OFPFW_IN_PORT
470                         && parse_port_name(value, (uint16_t *) data)) {
471                         /* Nothing to do. */
472                     } else if (f->type == F_U8) {
473                         *(uint8_t *) data = str_to_u32(value);
474                     } else if (f->type == F_U16) {
475                         *(uint16_t *) data = htons(str_to_u32(value));
476                     } else if (f->type == F_MAC) {
477                         str_to_mac(value, data);
478                     } else if (f->type == F_IP) {
479                         wildcards |= str_to_ip(value, data) << f->shift;
480                     } else {
481                         NOT_REACHED();
482                     }
483                 }
484             } else {
485                 ovs_fatal(0, "unknown keyword %s", name);
486             }
487         }
488     }
489     match->wildcards = htonl(wildcards);
490
491     normalized = *match;
492     normalize_match(&normalized);
493     if (memcmp(match, &normalized, sizeof normalized)) {
494         char *old = ofp_match_to_literal_string(match);
495         char *new = ofp_match_to_literal_string(&normalized);
496         VLOG_WARN("The specified flow is not in normal form:");
497         VLOG_WARN(" as specified: %s", old);
498         VLOG_WARN("as normalized: %s", new);
499         free(old);
500         free(new);
501     }
502 }