Catalli's threaded switch
[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, "drop_spoofed_arp")) {
267             struct nx_action_header *nah;
268             nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
269             nah->vendor = htonl(NX_VENDOR_ID);
270             nah->subtype = htons(NXAST_DROP_SPOOFED_ARP);
271         } else if (!strcasecmp(act, "output")) {
272             put_output_action(b, str_to_u32(arg));
273         } else if (!strcasecmp(act, "enqueue")) {
274             char *sp = NULL;
275             char *port = strtok_r(arg, ":q", &sp);
276             char *queue = strtok_r(NULL, "", &sp);
277             if (port == NULL || queue == NULL) {
278                 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
279             }
280             put_enqueue_action(b, str_to_u32(port), str_to_u32(queue));
281         } else if (!strcasecmp(act, "drop")) {
282             /* A drop action in OpenFlow occurs by just not setting
283              * an action. */
284             drop = true;
285             if (n_actions) {
286                 ovs_fatal(0, "Drop actions must not be preceded by other "
287                           "actions");
288             }
289         } else if (!strcasecmp(act, "CONTROLLER")) {
290             struct ofp_action_output *oao;
291             oao = put_output_action(b, OFPP_CONTROLLER);
292
293             /* Unless a numeric argument is specified, we send the whole
294              * packet to the controller. */
295             if (arg && (strspn(arg, "0123456789") == strlen(arg))) {
296                oao->max_len = htons(str_to_u32(arg));
297             } else {
298                 oao->max_len = htons(UINT16_MAX);
299             }
300         } else if (parse_port_name(act, &port)) {
301             put_output_action(b, port);
302         } else if (strspn(act, "0123456789") == strlen(act)) {
303             put_output_action(b, str_to_u32(act));
304         } else {
305             ovs_fatal(0, "Unknown action: %s", act);
306         }
307     }
308 }
309
310 struct protocol {
311     const char *name;
312     uint16_t dl_type;
313     uint8_t nw_proto;
314 };
315
316 static bool
317 parse_protocol(const char *name, const struct protocol **p_out)
318 {
319     static const struct protocol protocols[] = {
320         { "ip", ETH_TYPE_IP, 0 },
321         { "arp", ETH_TYPE_ARP, 0 },
322         { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
323         { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
324         { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
325     };
326     const struct protocol *p;
327
328     for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
329         if (!strcmp(p->name, name)) {
330             *p_out = p;
331             return true;
332         }
333     }
334     *p_out = NULL;
335     return false;
336 }
337
338 struct field {
339     const char *name;
340     uint32_t wildcard;
341     enum { F_U8, F_U16, F_MAC, F_IP } type;
342     size_t offset, shift;
343 };
344
345 static bool
346 parse_field(const char *name, const struct field **f_out)
347 {
348 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
349     static const struct field fields[] = {
350         { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port), 0 },
351         { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan), 0 },
352         { "dl_vlan_pcp", OFPFW_DL_VLAN_PCP, F_U8, F_OFS(dl_vlan_pcp), 0 },
353         { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src), 0 },
354         { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst), 0 },
355         { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type), 0 },
356         { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
357           F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
358         { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
359           F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
360         { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto), 0 },
361         { "nw_tos", OFPFW_NW_TOS, F_U8, F_OFS(nw_tos), 0 },
362         { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src), 0 },
363         { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst), 0 },
364         { "icmp_type", OFPFW_ICMP_TYPE, F_U16, F_OFS(icmp_type), 0 },
365         { "icmp_code", OFPFW_ICMP_CODE, F_U16, F_OFS(icmp_code), 0 }
366     };
367     const struct field *f;
368
369     for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
370         if (!strcmp(f->name, name)) {
371             *f_out = f;
372             return true;
373         }
374     }
375     *f_out = NULL;
376     return false;
377 }
378
379 /* Convert 'string' (as described in the Flow Syntax section of the
380  * ovs-ofctl man page) into 'match'.  The other arguments are optional
381  * and may be NULL if their value is not needed.  If 'actions' is
382  * specified, an action must be in 'string' and may be expanded or
383  * reallocated. */
384 void
385 parse_ofp_str(char *string, struct ofp_match *match, struct ofpbuf *actions,
386               uint8_t *table_idx, uint16_t *out_port, uint16_t *priority,
387               uint16_t *idle_timeout, uint16_t *hard_timeout,
388               uint64_t *cookie)
389 {
390     struct ofp_match normalized;
391     char *save_ptr = NULL;
392     char *name;
393     uint32_t wildcards;
394
395     if (table_idx) {
396         *table_idx = 0xff;
397     }
398     if (out_port) {
399         *out_port = OFPP_NONE;
400     }
401     if (priority) {
402         *priority = OFP_DEFAULT_PRIORITY;
403     }
404     if (idle_timeout) {
405         *idle_timeout = DEFAULT_IDLE_TIMEOUT;
406     }
407     if (hard_timeout) {
408         *hard_timeout = OFP_FLOW_PERMANENT;
409     }
410     if (cookie) {
411         *cookie = 0;
412     }
413     if (actions) {
414         char *act_str = strstr(string, "action");
415         if (!act_str) {
416             ovs_fatal(0, "must specify an action");
417         }
418         *act_str = '\0';
419
420         act_str = strchr(act_str + 1, '=');
421         if (!act_str) {
422             ovs_fatal(0, "must specify an action");
423         }
424
425         act_str++;
426
427         str_to_action(act_str, actions);
428     }
429     memset(match, 0, sizeof *match);
430     wildcards = OFPFW_ALL;
431     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
432          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
433         const struct protocol *p;
434
435         if (parse_protocol(name, &p)) {
436             wildcards &= ~OFPFW_DL_TYPE;
437             match->dl_type = htons(p->dl_type);
438             if (p->nw_proto) {
439                 wildcards &= ~OFPFW_NW_PROTO;
440                 match->nw_proto = p->nw_proto;
441             }
442         } else {
443             const struct field *f;
444             char *value;
445
446             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
447             if (!value) {
448                 ovs_fatal(0, "field %s missing value", name);
449             }
450
451             if (table_idx && !strcmp(name, "table")) {
452                 *table_idx = atoi(value);
453             } else if (out_port && !strcmp(name, "out_port")) {
454                 *out_port = atoi(value);
455             } else if (priority && !strcmp(name, "priority")) {
456                 *priority = atoi(value);
457             } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
458                 *idle_timeout = atoi(value);
459             } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
460                 *hard_timeout = atoi(value);
461             } else if (cookie && !strcmp(name, "cookie")) {
462                 *cookie = str_to_u64(value);
463             } else if (!strcmp(name, "tun_id_wild")) {
464                 wildcards |= NXFW_TUN_ID;
465             } else if (parse_field(name, &f)) {
466                 void *data = (char *) match + f->offset;
467                 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
468                     wildcards |= f->wildcard;
469                 } else {
470                     wildcards &= ~f->wildcard;
471                     if (f->wildcard == OFPFW_IN_PORT
472                         && parse_port_name(value, (uint16_t *) data)) {
473                         /* Nothing to do. */
474                     } else if (f->type == F_U8) {
475                         *(uint8_t *) data = str_to_u32(value);
476                     } else if (f->type == F_U16) {
477                         *(uint16_t *) data = htons(str_to_u32(value));
478                     } else if (f->type == F_MAC) {
479                         str_to_mac(value, data);
480                     } else if (f->type == F_IP) {
481                         wildcards |= str_to_ip(value, data) << f->shift;
482                     } else {
483                         NOT_REACHED();
484                     }
485                 }
486             } else {
487                 ovs_fatal(0, "unknown keyword %s", name);
488             }
489         }
490     }
491     match->wildcards = htonl(wildcards);
492
493     normalized = *match;
494     normalize_match(&normalized);
495     if (memcmp(match, &normalized, sizeof normalized)) {
496         char *old = ofp_match_to_literal_string(match);
497         char *new = ofp_match_to_literal_string(&normalized);
498         VLOG_WARN("The specified flow is not in normal form:");
499         VLOG_WARN(" as specified: %s", old);
500         VLOG_WARN("as normalized: %s", new);
501         free(old);
502         free(new);
503     }
504 }