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"
27 #include "openflow/openflow.h"
29 #include "socket-util.h"
34 VLOG_DEFINE_THIS_MODULE(ofp_parse)
36 #define DEFAULT_IDLE_TIMEOUT 60
39 str_to_u32(const char *str)
45 value = strtoul(str, &tail, 0);
46 if (errno == EINVAL || errno == ERANGE || *tail) {
47 ovs_fatal(0, "invalid numeric format %s", str);
53 str_to_u64(const char *str)
59 value = strtoull(str, &tail, 0);
60 if (errno == EINVAL || errno == ERANGE || *tail) {
61 ovs_fatal(0, "invalid numeric format %s", str);
67 str_to_mac(const char *str, uint8_t mac[6])
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);
76 str_to_ip(const char *str_, uint32_t *ip)
78 char *str = xstrdup(str_);
79 char *save_ptr = NULL;
80 const char *name, *netmask;
81 struct in_addr in_addr;
84 name = strtok_r(str, "/", &save_ptr);
85 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
87 ovs_fatal(0, "%s: could not convert to IP address", str);
91 netmask = strtok_r(NULL, "/", &save_ptr);
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];
99 /* Find first 1-bit. */
100 for (i = 0; i < 32; i++) {
101 if (nm & (1u << i)) {
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",
115 int prefix = atoi(netmask);
116 if (prefix <= 0 || prefix > 32) {
117 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
120 n_wild = 32 - prefix;
131 put_action(struct ofpbuf *b, size_t size, uint16_t type)
133 struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
134 ah->type = htons(type);
135 ah->len = htons(size);
139 static struct ofp_action_output *
140 put_output_action(struct ofpbuf *b, uint16_t port)
142 struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
143 oao->port = htons(port);
148 put_enqueue_action(struct ofpbuf *b, uint16_t port, uint32_t queue)
150 struct ofp_action_enqueue *oae = put_action(b, sizeof *oae, OFPAT_ENQUEUE);
151 oae->port = htons(port);
152 oae->queue_id = htonl(queue);
156 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
158 struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
159 str_to_mac(addr, oada->dl_addr);
164 parse_port_name(const char *name, uint16_t *port)
170 static const struct pair pairs[] = {
171 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
177 DEF_PAIR(CONTROLLER),
182 static const int n_pairs = ARRAY_SIZE(pairs);
185 for (i = 0; i < n_pairs; i++) {
186 if (!strcasecmp(name, pairs[i].name)) {
187 *port = pairs[i].value;
195 str_to_action(char *str, struct ofpbuf *b)
198 char *saveptr = NULL;
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++)
208 ovs_fatal(0, "Drop actions must not be followed by other actions");
211 /* Arguments are separated by colons */
212 arg = strchr(act, ':');
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")) {
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\"");
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
281 ovs_fatal(0, "Drop actions must not be preceded by other "
284 } else if (!strcasecmp(act, "CONTROLLER")) {
285 struct ofp_action_output *oao;
286 oao = put_output_action(b, OFPP_CONTROLLER);
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));
293 oao->max_len = htons(UINT16_MAX);
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));
300 ovs_fatal(0, "Unknown action: %s", act);
312 parse_protocol(const char *name, const struct protocol **p_out)
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 },
321 const struct protocol *p;
323 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
324 if (!strcmp(p->name, name)) {
336 enum { F_U8, F_U16, F_MAC, F_IP } type;
337 size_t offset, shift;
341 parse_field(const char *name, const struct field **f_out)
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 }
362 const struct field *f;
364 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
365 if (!strcmp(f->name, name)) {
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
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,
385 struct ofp_match normalized;
386 char *save_ptr = NULL;
394 *out_port = OFPP_NONE;
397 *priority = OFP_DEFAULT_PRIORITY;
400 *idle_timeout = DEFAULT_IDLE_TIMEOUT;
403 *hard_timeout = OFP_FLOW_PERMANENT;
409 char *act_str = strstr(string, "action");
411 ovs_fatal(0, "must specify an action");
415 act_str = strchr(act_str + 1, '=');
417 ovs_fatal(0, "must specify an action");
422 str_to_action(act_str, actions);
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;
430 if (parse_protocol(name, &p)) {
431 wildcards &= ~OFPFW_DL_TYPE;
432 match->dl_type = htons(p->dl_type);
434 wildcards &= ~OFPFW_NW_PROTO;
435 match->nw_proto = p->nw_proto;
438 const struct field *f;
441 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
443 ovs_fatal(0, "field %s missing value", name);
446 if (table_idx && !strcmp(name, "table")) {
447 *table_idx = atoi(value);
448 if (*table_idx > 31) {
449 ovs_fatal(0, "table %s is invalid, "
450 "must be between 0 and 31", value);
452 } else if (out_port && !strcmp(name, "out_port")) {
453 *out_port = atoi(value);
454 } else if (priority && !strcmp(name, "priority")) {
455 *priority = atoi(value);
456 } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
457 *idle_timeout = atoi(value);
458 } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
459 *hard_timeout = atoi(value);
460 } else if (cookie && !strcmp(name, "cookie")) {
461 *cookie = str_to_u64(value);
462 } else if (!strcmp(name, "tun_id_wild")) {
463 wildcards |= NXFW_TUN_ID;
464 } else if (parse_field(name, &f)) {
465 void *data = (char *) match + f->offset;
466 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
467 wildcards |= f->wildcard;
469 wildcards &= ~f->wildcard;
470 if (f->wildcard == OFPFW_IN_PORT
471 && parse_port_name(value, (uint16_t *) data)) {
473 } else if (f->type == F_U8) {
474 *(uint8_t *) data = str_to_u32(value);
475 } else if (f->type == F_U16) {
476 *(uint16_t *) data = htons(str_to_u32(value));
477 } else if (f->type == F_MAC) {
478 str_to_mac(value, data);
479 } else if (f->type == F_IP) {
480 wildcards |= str_to_ip(value, data) << f->shift;
486 ovs_fatal(0, "unknown keyword %s", name);
490 match->wildcards = htonl(wildcards);
493 normalize_match(&normalized);
494 if (memcmp(match, &normalized, sizeof normalized)) {
495 char *old = ofp_match_to_literal_string(match);
496 char *new = ofp_match_to_literal_string(&normalized);
497 VLOG_WARN("The specified flow is not in normal form:");
498 VLOG_WARN(" as specified: %s", old);
499 VLOG_WARN("as normalized: %s", new);