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