2 * Copyright (c) 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "ofp-parse.h"
24 #include "dynamic-string.h"
28 #include "openflow/openflow.h"
30 #include "socket-util.h"
35 VLOG_DEFINE_THIS_MODULE(ofp_parse)
38 str_to_u32(const char *str)
44 value = strtoul(str, &tail, 0);
45 if (errno == EINVAL || errno == ERANGE || *tail) {
46 ovs_fatal(0, "invalid numeric format %s", str);
52 str_to_u64(const char *str)
58 value = strtoull(str, &tail, 0);
59 if (errno == EINVAL || errno == ERANGE || *tail) {
60 ovs_fatal(0, "invalid numeric format %s", str);
66 str_to_mac(const char *str, uint8_t mac[6])
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);
75 str_to_ip(const char *str_, uint32_t *ip)
77 char *str = xstrdup(str_);
78 char *save_ptr = NULL;
79 const char *name, *netmask;
80 struct in_addr in_addr;
83 name = strtok_r(str, "/", &save_ptr);
84 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
86 ovs_fatal(0, "%s: could not convert to IP address", str);
90 netmask = strtok_r(NULL, "/", &save_ptr);
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];
98 /* Find first 1-bit. */
99 for (i = 0; i < 32; i++) {
100 if (nm & (1u << i)) {
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",
114 int prefix = atoi(netmask);
115 if (prefix <= 0 || prefix > 32) {
116 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
119 n_wild = 32 - prefix;
130 put_action(struct ofpbuf *b, size_t size, uint16_t type)
132 struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
133 ah->type = htons(type);
134 ah->len = htons(size);
138 static struct ofp_action_output *
139 put_output_action(struct ofpbuf *b, uint16_t port)
141 struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
142 oao->port = htons(port);
147 put_enqueue_action(struct ofpbuf *b, uint16_t port, uint32_t queue)
149 struct ofp_action_enqueue *oae = put_action(b, sizeof *oae, OFPAT_ENQUEUE);
150 oae->port = htons(port);
151 oae->queue_id = htonl(queue);
155 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
157 struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
158 str_to_mac(addr, oada->dl_addr);
163 parse_port_name(const char *name, uint16_t *port)
169 static const struct pair pairs[] = {
170 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
176 DEF_PAIR(CONTROLLER),
181 static const int n_pairs = ARRAY_SIZE(pairs);
184 for (i = 0; i < n_pairs; i++) {
185 if (!strcasecmp(name, pairs[i].name)) {
186 *port = pairs[i].value;
194 str_to_action(char *str, struct ofpbuf *b)
197 char *saveptr = NULL;
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++)
207 ovs_fatal(0, "Drop actions must not be followed by other actions");
210 /* Arguments are separated by colons */
211 arg = strchr(act, ':');
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")) {
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\"");
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
296 ovs_fatal(0, "Drop actions must not be preceded by other "
299 } else if (!strcasecmp(act, "CONTROLLER")) {
300 struct ofp_action_output *oao;
301 oao = put_output_action(b, OFPP_CONTROLLER);
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));
308 oao->max_len = htons(UINT16_MAX);
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));
315 ovs_fatal(0, "Unknown action: %s", act);
327 parse_protocol(const char *name, const struct protocol **p_out)
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 },
336 const struct protocol *p;
338 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
339 if (!strcmp(p->name, name)) {
351 enum { F_U8, F_U16, F_MAC, F_IP } type;
352 size_t offset, shift;
356 parse_field(const char *name, const struct field **f_out)
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 }
377 const struct field *f;
379 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
380 if (!strcmp(f->name, name)) {
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
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,
400 struct ofp_match normalized;
401 char *save_ptr = NULL;
409 *out_port = OFPP_NONE;
412 *priority = OFP_DEFAULT_PRIORITY;
415 *idle_timeout = OFP_FLOW_PERMANENT;
418 *hard_timeout = OFP_FLOW_PERMANENT;
424 char *act_str = strstr(string, "action");
426 ovs_fatal(0, "must specify an action");
430 act_str = strchr(act_str + 1, '=');
432 ovs_fatal(0, "must specify an action");
437 str_to_action(act_str, actions);
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;
445 if (parse_protocol(name, &p)) {
446 wildcards &= ~OFPFW_DL_TYPE;
447 match->dl_type = htons(p->dl_type);
449 wildcards &= ~OFPFW_NW_PROTO;
450 match->nw_proto = p->nw_proto;
453 const struct field *f;
456 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
458 ovs_fatal(0, "field %s missing value", name);
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;
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;
499 ovs_fatal(0, "unknown keyword %s", name);
503 match->wildcards = htonl(wildcards);
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);
518 /* Parses 'string' as a OFPT_FLOW_MOD with subtype OFPFC_ADD and returns an
519 * ofpbuf that contains it. */
521 parse_ofp_add_flow_str(char *string)
523 struct ofpbuf *buffer;
524 struct ofp_flow_mod *ofm;
525 uint16_t priority, idle_timeout, hard_timeout;
527 struct ofp_match match;
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,
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);
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. */
552 parse_ofp_add_flow_file(FILE *stream)
554 struct ofpbuf *b = NULL;
555 struct ds s = DS_EMPTY_INITIALIZER;
557 while (!ds_get_line(&s, stream)) {
558 char *line = ds_cstr(&s);
561 /* Delete comments. */
562 comment = strchr(line, '#');
567 /* Drop empty lines. */
568 if (line[strspn(line, " \t\n")] == '\0') {
572 b = parse_ofp_add_flow_str(line);