learning-switch: Add ability to define default flows
[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_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
149 {
150     struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
151     str_to_mac(addr, oada->dl_addr);
152 }
153
154
155 static bool
156 parse_port_name(const char *name, uint16_t *port)
157 {
158     struct pair {
159         const char *name;
160         uint16_t value;
161     };
162     static const struct pair pairs[] = {
163 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
164         DEF_PAIR(IN_PORT),
165         DEF_PAIR(TABLE),
166         DEF_PAIR(NORMAL),
167         DEF_PAIR(FLOOD),
168         DEF_PAIR(ALL),
169         DEF_PAIR(CONTROLLER),
170         DEF_PAIR(LOCAL),
171         DEF_PAIR(NONE),
172 #undef DEF_PAIR
173     };
174     static const int n_pairs = ARRAY_SIZE(pairs);
175     size_t i;
176
177     for (i = 0; i < n_pairs; i++) {
178         if (!strcasecmp(name, pairs[i].name)) {
179             *port = pairs[i].value;
180             return true;
181         }
182     }
183     return false;
184 }
185
186 static void
187 str_to_action(char *str, struct ofpbuf *b)
188 {
189     char *act, *arg;
190     char *saveptr = NULL;
191     bool drop = false;
192     int n_actions;
193
194     for (act = strtok_r(str, ", \t\r\n", &saveptr), n_actions = 0; act;
195          act = strtok_r(NULL, ", \t\r\n", &saveptr), n_actions++)
196     {
197         uint16_t port;
198
199         if (drop) {
200             ovs_fatal(0, "Drop actions must not be followed by other actions");
201         }
202
203         /* Arguments are separated by colons */
204         arg = strchr(act, ':');
205         if (arg) {
206             *arg = '\0';
207             arg++;
208         }
209
210         if (!strcasecmp(act, "mod_vlan_vid")) {
211             struct ofp_action_vlan_vid *va;
212             va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
213             va->vlan_vid = htons(str_to_u32(arg));
214         } else if (!strcasecmp(act, "mod_vlan_pcp")) {
215             struct ofp_action_vlan_pcp *va;
216             va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
217             va->vlan_pcp = str_to_u32(arg);
218         } else if (!strcasecmp(act, "strip_vlan")) {
219             struct ofp_action_header *ah;
220             ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
221             ah->type = htons(OFPAT_STRIP_VLAN);
222         } else if (!strcasecmp(act, "mod_dl_src")) {
223             put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
224         } else if (!strcasecmp(act, "mod_dl_dst")) {
225             put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
226         } else if (!strcasecmp(act, "mod_nw_src")) {
227             struct ofp_action_nw_addr *na;
228             na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
229             str_to_ip(arg, &na->nw_addr);
230         } else if (!strcasecmp(act, "mod_nw_dst")) {
231             struct ofp_action_nw_addr *na;
232             na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
233             str_to_ip(arg, &na->nw_addr);
234         } else if (!strcasecmp(act, "mod_tp_src")) {
235             struct ofp_action_tp_port *ta;
236             ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
237             ta->tp_port = htons(str_to_u32(arg));
238         } else if (!strcasecmp(act, "mod_tp_dst")) {
239             struct ofp_action_tp_port *ta;
240             ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
241             ta->tp_port = htons(str_to_u32(arg));
242         } else if (!strcasecmp(act, "mod_nw_tos")) {
243             struct ofp_action_nw_tos *nt;
244             nt = put_action(b, sizeof *nt, OFPAT_SET_NW_TOS);
245             nt->nw_tos = str_to_u32(arg);
246         } else if (!strcasecmp(act, "resubmit")) {
247             struct nx_action_resubmit *nar;
248             nar = put_action(b, sizeof *nar, OFPAT_VENDOR);
249             nar->vendor = htonl(NX_VENDOR_ID);
250             nar->subtype = htons(NXAST_RESUBMIT);
251             nar->in_port = htons(str_to_u32(arg));
252         } else if (!strcasecmp(act, "set_tunnel")) {
253             struct nx_action_set_tunnel *nast;
254             nast = put_action(b, sizeof *nast, OFPAT_VENDOR);
255             nast->vendor = htonl(NX_VENDOR_ID);
256             nast->subtype = htons(NXAST_SET_TUNNEL);
257             nast->tun_id = htonl(str_to_u32(arg));
258         } else if (!strcasecmp(act, "output")) {
259             put_output_action(b, str_to_u32(arg));
260         } else if (!strcasecmp(act, "drop")) {
261             /* A drop action in OpenFlow occurs by just not setting
262              * an action. */
263             drop = true;
264             if (n_actions) {
265                 ovs_fatal(0, "Drop actions must not be preceded by other "
266                           "actions");
267             }
268         } else if (!strcasecmp(act, "CONTROLLER")) {
269             struct ofp_action_output *oao;
270             oao = put_output_action(b, OFPP_CONTROLLER);
271
272             /* Unless a numeric argument is specified, we send the whole
273              * packet to the controller. */
274             if (arg && (strspn(arg, "0123456789") == strlen(arg))) {
275                oao->max_len = htons(str_to_u32(arg));
276             } else {
277                 oao->max_len = htons(UINT16_MAX);
278             }
279         } else if (parse_port_name(act, &port)) {
280             put_output_action(b, port);
281         } else if (strspn(act, "0123456789") == strlen(act)) {
282             put_output_action(b, str_to_u32(act));
283         } else {
284             ovs_fatal(0, "Unknown action: %s", act);
285         }
286     }
287 }
288
289 struct protocol {
290     const char *name;
291     uint16_t dl_type;
292     uint8_t nw_proto;
293 };
294
295 static bool
296 parse_protocol(const char *name, const struct protocol **p_out)
297 {
298     static const struct protocol protocols[] = {
299         { "ip", ETH_TYPE_IP, 0 },
300         { "arp", ETH_TYPE_ARP, 0 },
301         { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
302         { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
303         { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
304     };
305     const struct protocol *p;
306
307     for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
308         if (!strcmp(p->name, name)) {
309             *p_out = p;
310             return true;
311         }
312     }
313     *p_out = NULL;
314     return false;
315 }
316
317 struct field {
318     const char *name;
319     uint32_t wildcard;
320     enum { F_U8, F_U16, F_MAC, F_IP } type;
321     size_t offset, shift;
322 };
323
324 static bool
325 parse_field(const char *name, const struct field **f_out)
326 {
327 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
328     static const struct field fields[] = {
329         { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port), 0 },
330         { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan), 0 },
331         { "dl_vlan_pcp", OFPFW_DL_VLAN_PCP, F_U8, F_OFS(dl_vlan_pcp), 0 },
332         { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src), 0 },
333         { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst), 0 },
334         { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type), 0 },
335         { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
336           F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
337         { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
338           F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
339         { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto), 0 },
340         { "nw_tos", OFPFW_NW_TOS, F_U8, F_OFS(nw_tos), 0 },
341         { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src), 0 },
342         { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst), 0 },
343         { "icmp_type", OFPFW_ICMP_TYPE, F_U16, F_OFS(icmp_type), 0 },
344         { "icmp_code", OFPFW_ICMP_CODE, F_U16, F_OFS(icmp_code), 0 }
345     };
346     const struct field *f;
347
348     for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
349         if (!strcmp(f->name, name)) {
350             *f_out = f;
351             return true;
352         }
353     }
354     *f_out = NULL;
355     return false;
356 }
357
358 /* Convert 'string' (as described in the Flow Syntax section of the
359  * ovs-ofctl man page) into 'match'.  The other arguments are optional
360  * and may be NULL if their value is not needed.  If 'actions' is
361  * specified, an action must be in 'string' and may be expanded or
362  * reallocated. */
363 void
364 parse_ofp_str(char *string, struct ofp_match *match, struct ofpbuf *actions,
365               uint8_t *table_idx, uint16_t *out_port, uint16_t *priority,
366               uint16_t *idle_timeout, uint16_t *hard_timeout,
367               uint64_t *cookie)
368 {
369     struct ofp_match normalized;
370     char *save_ptr = NULL;
371     char *name;
372     uint32_t wildcards;
373
374     if (table_idx) {
375         *table_idx = 0xff;
376     }
377     if (out_port) {
378         *out_port = OFPP_NONE;
379     }
380     if (priority) {
381         *priority = OFP_DEFAULT_PRIORITY;
382     }
383     if (idle_timeout) {
384         *idle_timeout = DEFAULT_IDLE_TIMEOUT;
385     }
386     if (hard_timeout) {
387         *hard_timeout = OFP_FLOW_PERMANENT;
388     }
389     if (cookie) {
390         *cookie = 0;
391     }
392     if (actions) {
393         char *act_str = strstr(string, "action");
394         if (!act_str) {
395             ovs_fatal(0, "must specify an action");
396         }
397         *act_str = '\0';
398
399         act_str = strchr(act_str + 1, '=');
400         if (!act_str) {
401             ovs_fatal(0, "must specify an action");
402         }
403
404         act_str++;
405
406         str_to_action(act_str, actions);
407     }
408     memset(match, 0, sizeof *match);
409     wildcards = OFPFW_ALL;
410     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
411          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
412         const struct protocol *p;
413
414         if (parse_protocol(name, &p)) {
415             wildcards &= ~OFPFW_DL_TYPE;
416             match->dl_type = htons(p->dl_type);
417             if (p->nw_proto) {
418                 wildcards &= ~OFPFW_NW_PROTO;
419                 match->nw_proto = p->nw_proto;
420             }
421         } else {
422             const struct field *f;
423             char *value;
424
425             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
426             if (!value) {
427                 ovs_fatal(0, "field %s missing value", name);
428             }
429
430             if (table_idx && !strcmp(name, "table")) {
431                 *table_idx = atoi(value);
432             } else if (out_port && !strcmp(name, "out_port")) {
433                 *out_port = atoi(value);
434             } else if (priority && !strcmp(name, "priority")) {
435                 *priority = atoi(value);
436             } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
437                 *idle_timeout = atoi(value);
438             } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
439                 *hard_timeout = atoi(value);
440             } else if (cookie && !strcmp(name, "cookie")) {
441                 *cookie = str_to_u64(value);
442             } else if (!strcmp(name, "tun_id_wild")) {
443                 wildcards |= NXFW_TUN_ID;
444             } else if (parse_field(name, &f)) {
445                 void *data = (char *) match + f->offset;
446                 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
447                     wildcards |= f->wildcard;
448                 } else {
449                     wildcards &= ~f->wildcard;
450                     if (f->wildcard == OFPFW_IN_PORT
451                         && parse_port_name(value, (uint16_t *) data)) {
452                         /* Nothing to do. */
453                     } else if (f->type == F_U8) {
454                         *(uint8_t *) data = str_to_u32(value);
455                     } else if (f->type == F_U16) {
456                         *(uint16_t *) data = htons(str_to_u32(value));
457                     } else if (f->type == F_MAC) {
458                         str_to_mac(value, data);
459                     } else if (f->type == F_IP) {
460                         wildcards |= str_to_ip(value, data) << f->shift;
461                     } else {
462                         NOT_REACHED();
463                     }
464                 }
465             } else {
466                 ovs_fatal(0, "unknown keyword %s", name);
467             }
468         }
469     }
470     match->wildcards = htonl(wildcards);
471
472     normalized = *match;
473     normalize_match(&normalized);
474     if (memcmp(match, &normalized, sizeof normalized)) {
475         char *old = ofp_match_to_literal_string(match);
476         char *new = ofp_match_to_literal_string(&normalized);
477         VLOG_WARN("The specified flow is not in normal form:");
478         VLOG_WARN(" as specified: %s", old);
479         VLOG_WARN("as normalized: %s", new);
480         free(old);
481         free(new);
482     }
483 }