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