2 * Copyright (c) 2010, 2011 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"
25 #include "byte-order.h"
26 #include "dynamic-string.h"
28 #include "multipath.h"
32 #include "openflow/openflow.h"
34 #include "socket-util.h"
38 VLOG_DEFINE_THIS_MODULE(ofp_parse
);
41 str_to_u32(const char *str
)
47 ovs_fatal(0, "missing required numeric argument");
51 value
= strtoul(str
, &tail
, 0);
52 if (errno
== EINVAL
|| errno
== ERANGE
|| *tail
) {
53 ovs_fatal(0, "invalid numeric format %s", str
);
59 str_to_u64(const char *str
)
65 ovs_fatal(0, "missing required numeric argument");
69 value
= strtoull(str
, &tail
, 0);
70 if (errno
== EINVAL
|| errno
== ERANGE
|| *tail
) {
71 ovs_fatal(0, "invalid numeric format %s", str
);
77 str_to_mac(const char *str
, uint8_t mac
[6])
79 if (sscanf(str
, ETH_ADDR_SCAN_FMT
, ETH_ADDR_SCAN_ARGS(mac
))
80 != ETH_ADDR_SCAN_COUNT
) {
81 ovs_fatal(0, "invalid mac address %s", str
);
86 str_to_ip(const char *str_
, ovs_be32
*ip
, ovs_be32
*maskp
)
88 char *str
= xstrdup(str_
);
89 char *save_ptr
= NULL
;
90 const char *name
, *netmask
;
91 struct in_addr in_addr
;
95 name
= strtok_r(str
, "/", &save_ptr
);
96 retval
= name
? lookup_ip(name
, &in_addr
) : EINVAL
;
98 ovs_fatal(0, "%s: could not convert to IP address", str
);
100 *ip
= in_addr
.s_addr
;
102 netmask
= strtok_r(NULL
, "/", &save_ptr
);
105 if (sscanf(netmask
, "%"SCNu8
".%"SCNu8
".%"SCNu8
".%"SCNu8
,
106 &o
[0], &o
[1], &o
[2], &o
[3]) == 4) {
107 mask
= htonl((o
[0] << 24) | (o
[1] << 16) | (o
[2] << 8) | o
[3]);
109 int prefix
= atoi(netmask
);
110 if (prefix
<= 0 || prefix
> 32) {
111 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
113 } else if (prefix
== 32) {
114 mask
= htonl(UINT32_MAX
);
116 mask
= htonl(((1u << prefix
) - 1) << (32 - prefix
));
120 mask
= htonl(UINT32_MAX
);
127 if (mask
!= htonl(UINT32_MAX
)) {
128 ovs_fatal(0, "%s: netmask not allowed here", str_
);
136 str_to_tun_id(const char *str
, ovs_be64
*tun_idp
, ovs_be64
*maskp
)
138 uint64_t tun_id
, mask
;
142 tun_id
= strtoull(str
, &tail
, 0);
143 if (errno
|| (*tail
!= '\0' && *tail
!= '/')) {
148 mask
= strtoull(tail
+ 1, &tail
, 0);
149 if (errno
|| *tail
!= '\0') {
156 *tun_idp
= htonll(tun_id
);
157 *maskp
= htonll(mask
);
161 ovs_fatal(0, "%s: bad syntax for tunnel id", str
);
165 str_to_ipv6(const char *str_
, struct in6_addr
*addrp
, struct in6_addr
*maskp
)
167 char *str
= xstrdup(str_
);
168 char *save_ptr
= NULL
;
169 const char *name
, *netmask
;
170 struct in6_addr addr
, mask
;
173 name
= strtok_r(str
, "/", &save_ptr
);
174 retval
= name
? lookup_ipv6(name
, &addr
) : EINVAL
;
176 ovs_fatal(0, "%s: could not convert to IPv6 address", str
);
179 netmask
= strtok_r(NULL
, "/", &save_ptr
);
181 int prefix
= atoi(netmask
);
182 if (prefix
<= 0 || prefix
> 128) {
183 ovs_fatal(0, "%s: network prefix bits not between 1 and 128",
186 mask
= ipv6_create_mask(prefix
);
189 mask
= in6addr_exact
;
191 *addrp
= ipv6_addr_bitand(&addr
, &mask
);
196 if (!ipv6_mask_is_exact(&mask
)) {
197 ovs_fatal(0, "%s: netmask not allowed here", str_
);
205 put_action(struct ofpbuf
*b
, size_t size
, uint16_t type
)
207 struct ofp_action_header
*ah
= ofpbuf_put_zeros(b
, size
);
208 ah
->type
= htons(type
);
209 ah
->len
= htons(size
);
213 static struct ofp_action_output
*
214 put_output_action(struct ofpbuf
*b
, uint16_t port
)
216 struct ofp_action_output
*oao
= put_action(b
, sizeof *oao
, OFPAT_OUTPUT
);
217 oao
->port
= htons(port
);
222 put_enqueue_action(struct ofpbuf
*b
, uint16_t port
, uint32_t queue
)
224 struct ofp_action_enqueue
*oae
= put_action(b
, sizeof *oae
, OFPAT_ENQUEUE
);
225 oae
->port
= htons(port
);
226 oae
->queue_id
= htonl(queue
);
230 put_dl_addr_action(struct ofpbuf
*b
, uint16_t type
, const char *addr
)
232 struct ofp_action_dl_addr
*oada
= put_action(b
, sizeof *oada
, type
);
233 str_to_mac(addr
, oada
->dl_addr
);
238 parse_port_name(const char *name
, uint16_t *port
)
244 static const struct pair pairs
[] = {
245 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
251 DEF_PAIR(CONTROLLER
),
256 static const int n_pairs
= ARRAY_SIZE(pairs
);
259 for (i
= 0; i
< n_pairs
; i
++) {
260 if (!strcasecmp(name
, pairs
[i
].name
)) {
261 *port
= pairs
[i
].value
;
269 str_to_action(char *str
, struct ofpbuf
*b
)
278 char empty_string
[] = "";
283 pos
+= strspn(pos
, ", \t\r\n");
289 ovs_fatal(0, "Drop actions must not be followed by other actions");
293 actlen
= strcspn(pos
, ":(, \t\r\n");
294 if (act
[actlen
] == ':') {
295 /* The argument can be separated by a colon. */
298 arg
= act
+ actlen
+ 1;
299 arglen
= strcspn(arg
, ", \t\r\n");
300 pos
= arg
+ arglen
+ (arg
[arglen
] != '\0');
302 } else if (act
[actlen
] == '(') {
303 /* The argument can be surrounded by balanced parentheses. The
304 * outermost set of parentheses is removed. */
308 arg
= act
+ actlen
+ 1;
309 for (arglen
= 0; level
> 0; arglen
++) {
310 switch (arg
[arglen
]) {
312 ovs_fatal(0, "unbalanced parentheses in argument to %s "
324 arg
[arglen
- 1] = '\0';
327 /* There might be no argument at all. */
329 pos
= act
+ actlen
+ (act
[actlen
] != '\0');
333 if (!strcasecmp(act
, "mod_vlan_vid")) {
334 struct ofp_action_vlan_vid
*va
;
335 va
= put_action(b
, sizeof *va
, OFPAT_SET_VLAN_VID
);
336 va
->vlan_vid
= htons(str_to_u32(arg
));
337 } else if (!strcasecmp(act
, "mod_vlan_pcp")) {
338 struct ofp_action_vlan_pcp
*va
;
339 va
= put_action(b
, sizeof *va
, OFPAT_SET_VLAN_PCP
);
340 va
->vlan_pcp
= str_to_u32(arg
);
341 } else if (!strcasecmp(act
, "strip_vlan")) {
342 struct ofp_action_header
*ah
;
343 ah
= put_action(b
, sizeof *ah
, OFPAT_STRIP_VLAN
);
344 ah
->type
= htons(OFPAT_STRIP_VLAN
);
345 } else if (!strcasecmp(act
, "mod_dl_src")) {
346 put_dl_addr_action(b
, OFPAT_SET_DL_SRC
, arg
);
347 } else if (!strcasecmp(act
, "mod_dl_dst")) {
348 put_dl_addr_action(b
, OFPAT_SET_DL_DST
, arg
);
349 } else if (!strcasecmp(act
, "mod_nw_src")) {
350 struct ofp_action_nw_addr
*na
;
351 na
= put_action(b
, sizeof *na
, OFPAT_SET_NW_SRC
);
352 str_to_ip(arg
, &na
->nw_addr
, NULL
);
353 } else if (!strcasecmp(act
, "mod_nw_dst")) {
354 struct ofp_action_nw_addr
*na
;
355 na
= put_action(b
, sizeof *na
, OFPAT_SET_NW_DST
);
356 str_to_ip(arg
, &na
->nw_addr
, NULL
);
357 } else if (!strcasecmp(act
, "mod_tp_src")) {
358 struct ofp_action_tp_port
*ta
;
359 ta
= put_action(b
, sizeof *ta
, OFPAT_SET_TP_SRC
);
360 ta
->tp_port
= htons(str_to_u32(arg
));
361 } else if (!strcasecmp(act
, "mod_tp_dst")) {
362 struct ofp_action_tp_port
*ta
;
363 ta
= put_action(b
, sizeof *ta
, OFPAT_SET_TP_DST
);
364 ta
->tp_port
= htons(str_to_u32(arg
));
365 } else if (!strcasecmp(act
, "mod_nw_tos")) {
366 struct ofp_action_nw_tos
*nt
;
367 nt
= put_action(b
, sizeof *nt
, OFPAT_SET_NW_TOS
);
368 nt
->nw_tos
= str_to_u32(arg
);
369 } else if (!strcasecmp(act
, "resubmit")) {
370 struct nx_action_resubmit
*nar
;
371 nar
= put_action(b
, sizeof *nar
, OFPAT_VENDOR
);
372 nar
->vendor
= htonl(NX_VENDOR_ID
);
373 nar
->subtype
= htons(NXAST_RESUBMIT
);
374 nar
->in_port
= htons(str_to_u32(arg
));
375 } else if (!strcasecmp(act
, "set_tunnel")
376 || !strcasecmp(act
, "set_tunnel64")) {
377 uint64_t tun_id
= str_to_u64(arg
);
378 if (!strcasecmp(act
, "set_tunnel64") || tun_id
> UINT32_MAX
) {
379 struct nx_action_set_tunnel64
*nast64
;
380 nast64
= put_action(b
, sizeof *nast64
, OFPAT_VENDOR
);
381 nast64
->vendor
= htonl(NX_VENDOR_ID
);
382 nast64
->subtype
= htons(NXAST_SET_TUNNEL64
);
383 nast64
->tun_id
= htonll(tun_id
);
385 struct nx_action_set_tunnel
*nast
;
386 nast
= put_action(b
, sizeof *nast
, OFPAT_VENDOR
);
387 nast
->vendor
= htonl(NX_VENDOR_ID
);
388 nast
->subtype
= htons(NXAST_SET_TUNNEL
);
389 nast
->tun_id
= htonl(tun_id
);
391 } else if (!strcasecmp(act
, "drop_spoofed_arp")) {
392 struct nx_action_header
*nah
;
393 nah
= put_action(b
, sizeof *nah
, OFPAT_VENDOR
);
394 nah
->vendor
= htonl(NX_VENDOR_ID
);
395 nah
->subtype
= htons(NXAST_DROP_SPOOFED_ARP
);
396 } else if (!strcasecmp(act
, "set_queue")) {
397 struct nx_action_set_queue
*nasq
;
398 nasq
= put_action(b
, sizeof *nasq
, OFPAT_VENDOR
);
399 nasq
->vendor
= htonl(NX_VENDOR_ID
);
400 nasq
->subtype
= htons(NXAST_SET_QUEUE
);
401 nasq
->queue_id
= htonl(str_to_u32(arg
));
402 } else if (!strcasecmp(act
, "pop_queue")) {
403 struct nx_action_header
*nah
;
404 nah
= put_action(b
, sizeof *nah
, OFPAT_VENDOR
);
405 nah
->vendor
= htonl(NX_VENDOR_ID
);
406 nah
->subtype
= htons(NXAST_POP_QUEUE
);
407 } else if (!strcasecmp(act
, "note")) {
408 size_t start_ofs
= b
->size
;
409 struct nx_action_note
*nan
;
413 nan
= put_action(b
, sizeof *nan
, OFPAT_VENDOR
);
414 nan
->vendor
= htonl(NX_VENDOR_ID
);
415 nan
->subtype
= htons(NXAST_NOTE
);
417 b
->size
-= sizeof nan
->note
;
418 while (*arg
!= '\0') {
429 byte
= hexits_value(arg
, 2, &ok
);
431 ovs_fatal(0, "bad hex digit in `note' argument");
433 ofpbuf_put(b
, &byte
, 1);
438 len
= b
->size
- start_ofs
;
439 remainder
= len
% OFP_ACTION_ALIGN
;
441 ofpbuf_put_zeros(b
, OFP_ACTION_ALIGN
- remainder
);
443 nan
->len
= htons(b
->size
- start_ofs
);
444 } else if (!strcasecmp(act
, "move")) {
445 struct nx_action_reg_move
*move
;
446 move
= ofpbuf_put_uninit(b
, sizeof *move
);
447 nxm_parse_reg_move(move
, arg
);
448 } else if (!strcasecmp(act
, "load")) {
449 struct nx_action_reg_load
*load
;
450 load
= ofpbuf_put_uninit(b
, sizeof *load
);
451 nxm_parse_reg_load(load
, arg
);
452 } else if (!strcasecmp(act
, "multipath")) {
453 struct nx_action_multipath
*nam
;
454 nam
= ofpbuf_put_uninit(b
, sizeof *nam
);
455 multipath_parse(nam
, arg
);
456 } else if (!strcasecmp(act
, "output")) {
457 put_output_action(b
, str_to_u32(arg
));
458 } else if (!strcasecmp(act
, "enqueue")) {
460 char *port_s
= strtok_r(arg
, ":q", &sp
);
461 char *queue
= strtok_r(NULL
, "", &sp
);
462 if (port_s
== NULL
|| queue
== NULL
) {
463 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
465 put_enqueue_action(b
, str_to_u32(port_s
), str_to_u32(queue
));
466 } else if (!strcasecmp(act
, "drop")) {
467 /* A drop action in OpenFlow occurs by just not setting
471 ovs_fatal(0, "Drop actions must not be preceded by other "
474 } else if (!strcasecmp(act
, "CONTROLLER")) {
475 struct ofp_action_output
*oao
;
476 oao
= put_output_action(b
, OFPP_CONTROLLER
);
478 /* Unless a numeric argument is specified, we send the whole
479 * packet to the controller. */
480 if (arg
[0] && (strspn(arg
, "0123456789") == strlen(arg
))) {
481 oao
->max_len
= htons(str_to_u32(arg
));
483 oao
->max_len
= htons(UINT16_MAX
);
485 } else if (parse_port_name(act
, &port
)) {
486 put_output_action(b
, port
);
487 } else if (strspn(act
, "0123456789") == strlen(act
)) {
488 put_output_action(b
, str_to_u32(act
));
490 ovs_fatal(0, "Unknown action: %s", act
);
503 parse_protocol(const char *name
, const struct protocol
**p_out
)
505 static const struct protocol protocols
[] = {
506 { "ip", ETH_TYPE_IP
, 0 },
507 { "arp", ETH_TYPE_ARP
, 0 },
508 { "icmp", ETH_TYPE_IP
, IPPROTO_ICMP
},
509 { "tcp", ETH_TYPE_IP
, IPPROTO_TCP
},
510 { "udp", ETH_TYPE_IP
, IPPROTO_UDP
},
511 { "ipv6", ETH_TYPE_IPV6
, 0 },
512 { "ip6", ETH_TYPE_IPV6
, 0 },
513 { "icmp6", ETH_TYPE_IPV6
, IPPROTO_ICMPV6
},
514 { "tcp6", ETH_TYPE_IPV6
, IPPROTO_TCP
},
515 { "udp6", ETH_TYPE_IPV6
, IPPROTO_UDP
},
517 const struct protocol
*p
;
519 for (p
= protocols
; p
< &protocols
[ARRAY_SIZE(protocols
)]; p
++) {
520 if (!strcmp(p
->name
, name
)) {
530 FIELD(F_TUN_ID, "tun_id", 0) \
531 FIELD(F_IN_PORT, "in_port", FWW_IN_PORT) \
532 FIELD(F_DL_VLAN, "dl_vlan", 0) \
533 FIELD(F_DL_VLAN_PCP, "dl_vlan_pcp", 0) \
534 FIELD(F_DL_SRC, "dl_src", FWW_DL_SRC) \
535 FIELD(F_DL_DST, "dl_dst", FWW_DL_DST) \
536 FIELD(F_DL_TYPE, "dl_type", FWW_DL_TYPE) \
537 FIELD(F_NW_SRC, "nw_src", 0) \
538 FIELD(F_NW_DST, "nw_dst", 0) \
539 FIELD(F_NW_PROTO, "nw_proto", FWW_NW_PROTO) \
540 FIELD(F_NW_TOS, "nw_tos", FWW_NW_TOS) \
541 FIELD(F_TP_SRC, "tp_src", FWW_TP_SRC) \
542 FIELD(F_TP_DST, "tp_dst", FWW_TP_DST) \
543 FIELD(F_ICMP_TYPE, "icmp_type", FWW_TP_SRC) \
544 FIELD(F_ICMP_CODE, "icmp_code", FWW_TP_DST) \
545 FIELD(F_ARP_SHA, "arp_sha", FWW_ARP_SHA) \
546 FIELD(F_ARP_THA, "arp_tha", FWW_ARP_THA) \
547 FIELD(F_IPV6_SRC, "ipv6_src", 0) \
548 FIELD(F_IPV6_DST, "ipv6_dst", 0) \
549 FIELD(F_ND_TARGET, "nd_target", FWW_ND_TARGET) \
550 FIELD(F_ND_SLL, "nd_sll", FWW_ARP_SHA) \
551 FIELD(F_ND_TLL, "nd_tll", FWW_ARP_THA)
554 #define FIELD(ENUM, NAME, WILDCARD) ENUM,
561 enum field_index index
;
563 flow_wildcards_t wildcard
; /* FWW_* bit. */
567 parse_field_name(const char *name
, const struct field
**f_out
)
569 static const struct field fields
[N_FIELDS
] = {
570 #define FIELD(ENUM, NAME, WILDCARD) { ENUM, NAME, WILDCARD },
574 const struct field
*f
;
576 for (f
= fields
; f
< &fields
[ARRAY_SIZE(fields
)]; f
++) {
577 if (!strcmp(f
->name
, name
)) {
587 parse_field_value(struct cls_rule
*rule
, enum field_index index
,
590 uint8_t mac
[ETH_ADDR_LEN
];
591 ovs_be64 tun_id
, tun_mask
;
593 struct in6_addr ipv6
, ipv6_mask
;
598 str_to_tun_id(value
, &tun_id
, &tun_mask
);
599 cls_rule_set_tun_id_masked(rule
, tun_id
, tun_mask
);
603 if (!parse_port_name(value
, &port_no
)) {
604 port_no
= atoi(value
);
606 if (port_no
== OFPP_LOCAL
) {
607 port_no
= ODPP_LOCAL
;
609 cls_rule_set_in_port(rule
, port_no
);
613 cls_rule_set_dl_vlan(rule
, htons(str_to_u32(value
)));
617 cls_rule_set_dl_vlan_pcp(rule
, str_to_u32(value
));
621 str_to_mac(value
, mac
);
622 cls_rule_set_dl_src(rule
, mac
);
626 str_to_mac(value
, mac
);
627 cls_rule_set_dl_dst(rule
, mac
);
631 cls_rule_set_dl_type(rule
, htons(str_to_u32(value
)));
635 str_to_ip(value
, &ip
, &mask
);
636 cls_rule_set_nw_src_masked(rule
, ip
, mask
);
640 str_to_ip(value
, &ip
, &mask
);
641 cls_rule_set_nw_dst_masked(rule
, ip
, mask
);
645 cls_rule_set_nw_proto(rule
, str_to_u32(value
));
649 cls_rule_set_nw_tos(rule
, str_to_u32(value
));
653 cls_rule_set_tp_src(rule
, htons(str_to_u32(value
)));
657 cls_rule_set_tp_dst(rule
, htons(str_to_u32(value
)));
661 cls_rule_set_icmp_type(rule
, str_to_u32(value
));
665 cls_rule_set_icmp_code(rule
, str_to_u32(value
));
669 str_to_mac(value
, mac
);
670 cls_rule_set_arp_sha(rule
, mac
);
674 str_to_mac(value
, mac
);
675 cls_rule_set_arp_tha(rule
, mac
);
679 str_to_ipv6(value
, &ipv6
, &ipv6_mask
);
680 cls_rule_set_ipv6_src_masked(rule
, &ipv6
, &ipv6_mask
);
684 str_to_ipv6(value
, &ipv6
, &ipv6_mask
);
685 cls_rule_set_ipv6_dst_masked(rule
, &ipv6
, &ipv6_mask
);
689 str_to_ipv6(value
, &ipv6
, NULL
);
690 cls_rule_set_nd_target(rule
, ipv6
);
694 str_to_mac(value
, mac
);
695 cls_rule_set_arp_sha(rule
, mac
);
699 str_to_mac(value
, mac
);
700 cls_rule_set_arp_tha(rule
, mac
);
709 parse_reg_value(struct cls_rule
*rule
, int reg_idx
, const char *value
)
711 uint32_t reg_value
, reg_mask
;
713 if (!strcmp(value
, "ANY") || !strcmp(value
, "*")) {
714 cls_rule_set_reg_masked(rule
, reg_idx
, 0, 0);
715 } else if (sscanf(value
, "%"SCNi32
"/%"SCNi32
,
716 ®_value
, ®_mask
) == 2) {
717 cls_rule_set_reg_masked(rule
, reg_idx
, reg_value
, reg_mask
);
718 } else if (sscanf(value
, "%"SCNi32
, ®_value
)) {
719 cls_rule_set_reg(rule
, reg_idx
, reg_value
);
721 ovs_fatal(0, "register fields must take the form <value> "
722 "or <value>/<mask>");
726 /* Convert 'string' (as described in the Flow Syntax section of the ovs-ofctl
727 * man page) into 'pf'. If 'actions' is specified, an action must be in
728 * 'string' and may be expanded or reallocated. */
730 parse_ofp_str(struct flow_mod
*fm
, uint8_t *table_idx
,
731 struct ofpbuf
*actions
, char *string
)
733 char *save_ptr
= NULL
;
739 cls_rule_init_catchall(&fm
->cr
, OFP_DEFAULT_PRIORITY
);
740 fm
->cookie
= htonll(0);
741 fm
->command
= UINT16_MAX
;
742 fm
->idle_timeout
= OFP_FLOW_PERMANENT
;
743 fm
->hard_timeout
= OFP_FLOW_PERMANENT
;
744 fm
->buffer_id
= UINT32_MAX
;
745 fm
->out_port
= OFPP_NONE
;
748 char *act_str
= strstr(string
, "action");
750 ovs_fatal(0, "must specify an action");
754 act_str
= strchr(act_str
+ 1, '=');
756 ovs_fatal(0, "must specify an action");
761 str_to_action(act_str
, actions
);
762 fm
->actions
= actions
->data
;
763 fm
->n_actions
= actions
->size
/ sizeof(union ofp_action
);
768 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
769 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
770 const struct protocol
*p
;
772 if (parse_protocol(name
, &p
)) {
773 cls_rule_set_dl_type(&fm
->cr
, htons(p
->dl_type
));
775 cls_rule_set_nw_proto(&fm
->cr
, p
->nw_proto
);
778 const struct field
*f
;
781 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
783 ovs_fatal(0, "field %s missing value", name
);
786 if (table_idx
&& !strcmp(name
, "table")) {
787 *table_idx
= atoi(value
);
788 } else if (!strcmp(name
, "out_port")) {
789 fm
->out_port
= atoi(value
);
790 } else if (!strcmp(name
, "priority")) {
791 fm
->cr
.priority
= atoi(value
);
792 } else if (!strcmp(name
, "idle_timeout")) {
793 fm
->idle_timeout
= atoi(value
);
794 } else if (!strcmp(name
, "hard_timeout")) {
795 fm
->hard_timeout
= atoi(value
);
796 } else if (!strcmp(name
, "cookie")) {
797 fm
->cookie
= htonll(str_to_u64(value
));
798 } else if (parse_field_name(name
, &f
)) {
799 if (!strcmp(value
, "*") || !strcmp(value
, "ANY")) {
801 fm
->cr
.wc
.wildcards
|= f
->wildcard
;
802 cls_rule_zero_wildcarded_fields(&fm
->cr
);
803 } else if (f
->index
== F_NW_SRC
) {
804 cls_rule_set_nw_src_masked(&fm
->cr
, 0, 0);
805 } else if (f
->index
== F_NW_DST
) {
806 cls_rule_set_nw_dst_masked(&fm
->cr
, 0, 0);
807 } else if (f
->index
== F_IPV6_SRC
) {
808 cls_rule_set_ipv6_src_masked(&fm
->cr
,
809 &in6addr_any
, &in6addr_any
);
810 } else if (f
->index
== F_IPV6_DST
) {
811 cls_rule_set_ipv6_dst_masked(&fm
->cr
,
812 &in6addr_any
, &in6addr_any
);
813 } else if (f
->index
== F_DL_VLAN
) {
814 cls_rule_set_any_vid(&fm
->cr
);
815 } else if (f
->index
== F_DL_VLAN_PCP
) {
816 cls_rule_set_any_pcp(&fm
->cr
);
821 parse_field_value(&fm
->cr
, f
->index
, value
);
823 } else if (!strncmp(name
, "reg", 3) && isdigit(name
[3])) {
824 unsigned int reg_idx
= atoi(name
+ 3);
825 if (reg_idx
>= FLOW_N_REGS
) {
826 ovs_fatal(0, "only %d registers supported", FLOW_N_REGS
);
828 parse_reg_value(&fm
->cr
, reg_idx
, value
);
830 ovs_fatal(0, "unknown keyword %s", name
);
836 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
837 * (one of OFPFC_*) and appends the parsed OpenFlow message to 'packets'.
838 * '*cur_format' should initially contain the flow format currently configured
839 * on the connection; this function will add a message to change the flow
840 * format and update '*cur_format', if this is necessary to add the parsed
843 parse_ofp_flow_mod_str(struct list
*packets
, enum nx_flow_format
*cur_format
,
844 char *string
, uint16_t command
)
846 bool is_del
= command
== OFPFC_DELETE
|| command
== OFPFC_DELETE_STRICT
;
847 enum nx_flow_format min_format
, next_format
;
848 struct ofpbuf actions
;
852 ofpbuf_init(&actions
, 64);
853 parse_ofp_str(&fm
, NULL
, is_del
? NULL
: &actions
, string
);
854 fm
.command
= command
;
856 min_format
= ofputil_min_flow_format(&fm
.cr
, true, fm
.cookie
);
857 next_format
= MAX(*cur_format
, min_format
);
858 if (next_format
!= *cur_format
) {
859 struct ofpbuf
*sff
= ofputil_make_set_flow_format(next_format
);
860 list_push_back(packets
, &sff
->list_node
);
861 *cur_format
= next_format
;
864 ofm
= ofputil_encode_flow_mod(&fm
, *cur_format
);
865 list_push_back(packets
, &ofm
->list_node
);
867 ofpbuf_uninit(&actions
);
870 /* Similar to parse_ofp_flow_mod_str(), except that the string is read from
871 * 'stream' and the command is always OFPFC_ADD. Returns false if end-of-file
872 * is reached before reading a flow, otherwise true. */
874 parse_ofp_flow_mod_file(struct list
*packets
, enum nx_flow_format
*cur
,
875 FILE *stream
, uint16_t command
)
881 ok
= ds_get_preprocessed_line(&s
, stream
) == 0;
883 parse_ofp_flow_mod_str(packets
, cur
, ds_cstr(&s
), command
);
891 parse_ofp_flow_stats_request_str(struct flow_stats_request
*fsr
,
892 bool aggregate
, char *string
)
897 parse_ofp_str(&fm
, &table_id
, NULL
, string
);
898 fsr
->aggregate
= aggregate
;
900 fsr
->out_port
= fm
.out_port
;
901 fsr
->table_id
= table_id
;