]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/ofp-parse.c
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
)
37 str_to_u32(const char *str
)
43 value
= strtoul(str
, &tail
, 0);
44 if (errno
== EINVAL
|| errno
== ERANGE
|| *tail
) {
45 ovs_fatal(0, "invalid numeric format %s", str
);
51 str_to_u64(const char *str
)
57 value
= strtoull(str
, &tail
, 0);
58 if (errno
== EINVAL
|| errno
== ERANGE
|| *tail
) {
59 ovs_fatal(0, "invalid numeric format %s", str
);
65 str_to_mac(const char *str
, uint8_t mac
[6])
67 if (sscanf(str
, ETH_ADDR_SCAN_FMT
, ETH_ADDR_SCAN_ARGS(mac
))
68 != ETH_ADDR_SCAN_COUNT
) {
69 ovs_fatal(0, "invalid mac address %s", str
);
74 str_to_ip(const char *str_
, uint32_t *ip
)
76 char *str
= xstrdup(str_
);
77 char *save_ptr
= NULL
;
78 const char *name
, *netmask
;
79 struct in_addr in_addr
;
82 name
= strtok_r(str
, "/", &save_ptr
);
83 retval
= name
? lookup_ip(name
, &in_addr
) : EINVAL
;
85 ovs_fatal(0, "%s: could not convert to IP address", str
);
89 netmask
= strtok_r(NULL
, "/", &save_ptr
);
92 if (sscanf(netmask
, "%"SCNu8
".%"SCNu8
".%"SCNu8
".%"SCNu8
,
93 &o
[0], &o
[1], &o
[2], &o
[3]) == 4) {
94 uint32_t nm
= (o
[0] << 24) | (o
[1] << 16) | (o
[2] << 8) | o
[3];
97 /* Find first 1-bit. */
98 for (i
= 0; i
< 32; i
++) {
105 /* Verify that the rest of the bits are 1-bits. */
106 for (; i
< 32; i
++) {
107 if (!(nm
& (1u << i
))) {
108 ovs_fatal(0, "%s: %s is not a valid netmask",
113 int prefix
= atoi(netmask
);
114 if (prefix
<= 0 || prefix
> 32) {
115 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
118 n_wild
= 32 - prefix
;
129 put_action(struct ofpbuf
*b
, size_t size
, uint16_t type
)
131 struct ofp_action_header
*ah
= ofpbuf_put_zeros(b
, size
);
132 ah
->type
= htons(type
);
133 ah
->len
= htons(size
);
137 static struct ofp_action_output
*
138 put_output_action(struct ofpbuf
*b
, uint16_t port
)
140 struct ofp_action_output
*oao
= put_action(b
, sizeof *oao
, OFPAT_OUTPUT
);
141 oao
->port
= htons(port
);
146 put_enqueue_action(struct ofpbuf
*b
, uint16_t port
, uint32_t queue
)
148 struct ofp_action_enqueue
*oae
= put_action(b
, sizeof *oae
, OFPAT_ENQUEUE
);
149 oae
->port
= htons(port
);
150 oae
->queue_id
= htonl(queue
);
154 put_dl_addr_action(struct ofpbuf
*b
, uint16_t type
, const char *addr
)
156 struct ofp_action_dl_addr
*oada
= put_action(b
, sizeof *oada
, type
);
157 str_to_mac(addr
, oada
->dl_addr
);
162 parse_port_name(const char *name
, uint16_t *port
)
168 static const struct pair pairs
[] = {
169 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
175 DEF_PAIR(CONTROLLER
),
180 static const int n_pairs
= ARRAY_SIZE(pairs
);
183 for (i
= 0; i
< n_pairs
; i
++) {
184 if (!strcasecmp(name
, pairs
[i
].name
)) {
185 *port
= pairs
[i
].value
;
193 str_to_action(char *str
, struct ofpbuf
*b
)
196 char *saveptr
= NULL
;
200 for (act
= strtok_r(str
, ", \t\r\n", &saveptr
), n_actions
= 0; act
;
201 act
= strtok_r(NULL
, ", \t\r\n", &saveptr
), n_actions
++)
206 ovs_fatal(0, "Drop actions must not be followed by other actions");
209 /* Arguments are separated by colons */
210 arg
= strchr(act
, ':');
216 if (!strcasecmp(act
, "mod_vlan_vid")) {
217 struct ofp_action_vlan_vid
*va
;
218 va
= put_action(b
, sizeof *va
, OFPAT_SET_VLAN_VID
);
219 va
->vlan_vid
= htons(str_to_u32(arg
));
220 } else if (!strcasecmp(act
, "mod_vlan_pcp")) {
221 struct ofp_action_vlan_pcp
*va
;
222 va
= put_action(b
, sizeof *va
, OFPAT_SET_VLAN_PCP
);
223 va
->vlan_pcp
= str_to_u32(arg
);
224 } else if (!strcasecmp(act
, "strip_vlan")) {
225 struct ofp_action_header
*ah
;
226 ah
= put_action(b
, sizeof *ah
, OFPAT_STRIP_VLAN
);
227 ah
->type
= htons(OFPAT_STRIP_VLAN
);
228 } else if (!strcasecmp(act
, "mod_dl_src")) {
229 put_dl_addr_action(b
, OFPAT_SET_DL_SRC
, arg
);
230 } else if (!strcasecmp(act
, "mod_dl_dst")) {
231 put_dl_addr_action(b
, OFPAT_SET_DL_DST
, arg
);
232 } else if (!strcasecmp(act
, "mod_nw_src")) {
233 struct ofp_action_nw_addr
*na
;
234 na
= put_action(b
, sizeof *na
, OFPAT_SET_NW_SRC
);
235 str_to_ip(arg
, &na
->nw_addr
);
236 } else if (!strcasecmp(act
, "mod_nw_dst")) {
237 struct ofp_action_nw_addr
*na
;
238 na
= put_action(b
, sizeof *na
, OFPAT_SET_NW_DST
);
239 str_to_ip(arg
, &na
->nw_addr
);
240 } else if (!strcasecmp(act
, "mod_tp_src")) {
241 struct ofp_action_tp_port
*ta
;
242 ta
= put_action(b
, sizeof *ta
, OFPAT_SET_TP_SRC
);
243 ta
->tp_port
= htons(str_to_u32(arg
));
244 } else if (!strcasecmp(act
, "mod_tp_dst")) {
245 struct ofp_action_tp_port
*ta
;
246 ta
= put_action(b
, sizeof *ta
, OFPAT_SET_TP_DST
);
247 ta
->tp_port
= htons(str_to_u32(arg
));
248 } else if (!strcasecmp(act
, "mod_nw_tos")) {
249 struct ofp_action_nw_tos
*nt
;
250 nt
= put_action(b
, sizeof *nt
, OFPAT_SET_NW_TOS
);
251 nt
->nw_tos
= str_to_u32(arg
);
252 } else if (!strcasecmp(act
, "resubmit")) {
253 struct nx_action_resubmit
*nar
;
254 nar
= put_action(b
, sizeof *nar
, OFPAT_VENDOR
);
255 nar
->vendor
= htonl(NX_VENDOR_ID
);
256 nar
->subtype
= htons(NXAST_RESUBMIT
);
257 nar
->in_port
= htons(str_to_u32(arg
));
258 } else if (!strcasecmp(act
, "set_tunnel")) {
259 struct nx_action_set_tunnel
*nast
;
260 nast
= put_action(b
, sizeof *nast
, OFPAT_VENDOR
);
261 nast
->vendor
= htonl(NX_VENDOR_ID
);
262 nast
->subtype
= htons(NXAST_SET_TUNNEL
);
263 nast
->tun_id
= htonl(str_to_u32(arg
));
264 } else if (!strcasecmp(act
, "drop_spoofed_arp")) {
265 struct nx_action_header
*nah
;
266 nah
= put_action(b
, sizeof *nah
, OFPAT_VENDOR
);
267 nah
->vendor
= htonl(NX_VENDOR_ID
);
268 nah
->subtype
= htons(NXAST_DROP_SPOOFED_ARP
);
269 } else if (!strcasecmp(act
, "output")) {
270 put_output_action(b
, str_to_u32(arg
));
271 } else if (!strcasecmp(act
, "enqueue")) {
273 char *port_s
= strtok_r(arg
, ":q", &sp
);
274 char *queue
= strtok_r(NULL
, "", &sp
);
275 if (port_s
== NULL
|| queue
== NULL
) {
276 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
278 put_enqueue_action(b
, str_to_u32(port_s
), str_to_u32(queue
));
279 } else if (!strcasecmp(act
, "drop")) {
280 /* A drop action in OpenFlow occurs by just not setting
284 ovs_fatal(0, "Drop actions must not be preceded by other "
287 } else if (!strcasecmp(act
, "CONTROLLER")) {
288 struct ofp_action_output
*oao
;
289 oao
= put_output_action(b
, OFPP_CONTROLLER
);
291 /* Unless a numeric argument is specified, we send the whole
292 * packet to the controller. */
293 if (arg
&& (strspn(arg
, "0123456789") == strlen(arg
))) {
294 oao
->max_len
= htons(str_to_u32(arg
));
296 oao
->max_len
= htons(UINT16_MAX
);
298 } else if (parse_port_name(act
, &port
)) {
299 put_output_action(b
, port
);
300 } else if (strspn(act
, "0123456789") == strlen(act
)) {
301 put_output_action(b
, str_to_u32(act
));
303 ovs_fatal(0, "Unknown action: %s", act
);
315 parse_protocol(const char *name
, const struct protocol
**p_out
)
317 static const struct protocol protocols
[] = {
318 { "ip", ETH_TYPE_IP
, 0 },
319 { "arp", ETH_TYPE_ARP
, 0 },
320 { "icmp", ETH_TYPE_IP
, IP_TYPE_ICMP
},
321 { "tcp", ETH_TYPE_IP
, IP_TYPE_TCP
},
322 { "udp", ETH_TYPE_IP
, IP_TYPE_UDP
},
324 const struct protocol
*p
;
326 for (p
= protocols
; p
< &protocols
[ARRAY_SIZE(protocols
)]; p
++) {
327 if (!strcmp(p
->name
, name
)) {
339 enum { F_U8
, F_U16
, F_MAC
, F_IP
} type
;
340 size_t offset
, shift
;
344 parse_field(const char *name
, const struct field
**f_out
)
346 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
347 static const struct field fields
[] = {
348 { "in_port", OFPFW_IN_PORT
, F_U16
, F_OFS(in_port
), 0 },
349 { "dl_vlan", OFPFW_DL_VLAN
, F_U16
, F_OFS(dl_vlan
), 0 },
350 { "dl_vlan_pcp", OFPFW_DL_VLAN_PCP
, F_U8
, F_OFS(dl_vlan_pcp
), 0 },
351 { "dl_src", OFPFW_DL_SRC
, F_MAC
, F_OFS(dl_src
), 0 },
352 { "dl_dst", OFPFW_DL_DST
, F_MAC
, F_OFS(dl_dst
), 0 },
353 { "dl_type", OFPFW_DL_TYPE
, F_U16
, F_OFS(dl_type
), 0 },
354 { "nw_src", OFPFW_NW_SRC_MASK
, F_IP
,
355 F_OFS(nw_src
), OFPFW_NW_SRC_SHIFT
},
356 { "nw_dst", OFPFW_NW_DST_MASK
, F_IP
,
357 F_OFS(nw_dst
), OFPFW_NW_DST_SHIFT
},
358 { "nw_proto", OFPFW_NW_PROTO
, F_U8
, F_OFS(nw_proto
), 0 },
359 { "nw_tos", OFPFW_NW_TOS
, F_U8
, F_OFS(nw_tos
), 0 },
360 { "tp_src", OFPFW_TP_SRC
, F_U16
, F_OFS(tp_src
), 0 },
361 { "tp_dst", OFPFW_TP_DST
, F_U16
, F_OFS(tp_dst
), 0 },
362 { "icmp_type", OFPFW_ICMP_TYPE
, F_U16
, F_OFS(icmp_type
), 0 },
363 { "icmp_code", OFPFW_ICMP_CODE
, F_U16
, F_OFS(icmp_code
), 0 }
365 const struct field
*f
;
367 for (f
= fields
; f
< &fields
[ARRAY_SIZE(fields
)]; f
++) {
368 if (!strcmp(f
->name
, name
)) {
377 /* Convert 'string' (as described in the Flow Syntax section of the
378 * ovs-ofctl man page) into 'match'. The other arguments are optional
379 * and may be NULL if their value is not needed. If 'actions' is
380 * specified, an action must be in 'string' and may be expanded or
383 parse_ofp_str(char *string
, struct ofp_match
*match
, struct ofpbuf
*actions
,
384 uint8_t *table_idx
, uint16_t *out_port
, uint16_t *priority
,
385 uint16_t *idle_timeout
, uint16_t *hard_timeout
,
388 struct ofp_match normalized
;
389 char *save_ptr
= NULL
;
397 *out_port
= OFPP_NONE
;
400 *priority
= OFP_DEFAULT_PRIORITY
;
403 *idle_timeout
= OFP_FLOW_PERMANENT
;
406 *hard_timeout
= OFP_FLOW_PERMANENT
;
412 char *act_str
= strstr(string
, "action");
414 ovs_fatal(0, "must specify an action");
418 act_str
= strchr(act_str
+ 1, '=');
420 ovs_fatal(0, "must specify an action");
425 str_to_action(act_str
, actions
);
427 memset(match
, 0, sizeof *match
);
428 wildcards
= OFPFW_ALL
;
429 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
430 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
431 const struct protocol
*p
;
433 if (parse_protocol(name
, &p
)) {
434 wildcards
&= ~OFPFW_DL_TYPE
;
435 match
->dl_type
= htons(p
->dl_type
);
437 wildcards
&= ~OFPFW_NW_PROTO
;
438 match
->nw_proto
= p
->nw_proto
;
441 const struct field
*f
;
444 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
446 ovs_fatal(0, "field %s missing value", name
);
449 if (table_idx
&& !strcmp(name
, "table")) {
450 *table_idx
= atoi(value
);
451 } else if (out_port
&& !strcmp(name
, "out_port")) {
452 *out_port
= atoi(value
);
453 } else if (priority
&& !strcmp(name
, "priority")) {
454 *priority
= atoi(value
);
455 } else if (idle_timeout
&& !strcmp(name
, "idle_timeout")) {
456 *idle_timeout
= atoi(value
);
457 } else if (hard_timeout
&& !strcmp(name
, "hard_timeout")) {
458 *hard_timeout
= atoi(value
);
459 } else if (cookie
&& !strcmp(name
, "cookie")) {
460 *cookie
= str_to_u64(value
);
461 } else if (!strcmp(name
, "tun_id_wild")) {
462 wildcards
|= NXFW_TUN_ID
;
463 } else if (parse_field(name
, &f
)) {
464 void *data
= (char *) match
+ f
->offset
;
465 if (!strcmp(value
, "*") || !strcmp(value
, "ANY")) {
466 wildcards
|= f
->wildcard
;
468 wildcards
&= ~f
->wildcard
;
469 if (f
->wildcard
== OFPFW_IN_PORT
470 && parse_port_name(value
, (uint16_t *) data
)) {
472 } else if (f
->type
== F_U8
) {
473 *(uint8_t *) data
= str_to_u32(value
);
474 } else if (f
->type
== F_U16
) {
475 *(uint16_t *) data
= htons(str_to_u32(value
));
476 } else if (f
->type
== F_MAC
) {
477 str_to_mac(value
, data
);
478 } else if (f
->type
== F_IP
) {
479 wildcards
|= str_to_ip(value
, data
) << f
->shift
;
485 ovs_fatal(0, "unknown keyword %s", name
);
489 match
->wildcards
= htonl(wildcards
);
492 normalize_match(&normalized
);
493 if (memcmp(match
, &normalized
, sizeof normalized
)) {
494 char *old
= ofp_match_to_literal_string(match
);
495 char *new = ofp_match_to_literal_string(&normalized
);
496 VLOG_WARN("The specified flow is not in normal form:");
497 VLOG_WARN(" as specified: %s", old
);
498 VLOG_WARN("as normalized: %s", new);