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"
27 #include "byte-order.h"
28 #include "dynamic-string.h"
30 #include "multipath.h"
34 #include "openflow/openflow.h"
36 #include "socket-util.h"
40 VLOG_DEFINE_THIS_MODULE(ofp_parse);
43 str_to_u32(const char *str)
49 ovs_fatal(0, "missing required numeric argument");
53 value = strtoul(str, &tail, 0);
54 if (errno == EINVAL || errno == ERANGE || *tail) {
55 ovs_fatal(0, "invalid numeric format %s", str);
61 str_to_u64(const char *str)
67 ovs_fatal(0, "missing required numeric argument");
71 value = strtoull(str, &tail, 0);
72 if (errno == EINVAL || errno == ERANGE || *tail) {
73 ovs_fatal(0, "invalid numeric format %s", str);
79 str_to_mac(const char *str, uint8_t mac[6])
81 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
82 != ETH_ADDR_SCAN_COUNT) {
83 ovs_fatal(0, "invalid mac address %s", str);
88 str_to_eth_dst(const char *str,
89 uint8_t mac[ETH_ADDR_LEN], uint8_t mask[ETH_ADDR_LEN])
91 if (sscanf(str, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
92 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))
93 == ETH_ADDR_SCAN_COUNT * 2) {
94 if (!flow_wildcards_is_dl_dst_mask_valid(mask)) {
95 ovs_fatal(0, "%s: invalid Ethernet destination mask (only "
96 "00:00:00:00:00:00, 01:00:00:00:00:00, "
97 "fe:ff:ff:ff:ff:ff, and ff:ff:ff:ff:ff:ff are allowed)",
100 } else if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
101 == ETH_ADDR_SCAN_COUNT) {
102 memset(mask, 0xff, ETH_ADDR_LEN);
104 ovs_fatal(0, "invalid mac address %s", str);
109 str_to_ip(const char *str_, ovs_be32 *ip, ovs_be32 *maskp)
111 char *str = xstrdup(str_);
112 char *save_ptr = NULL;
113 const char *name, *netmask;
114 struct in_addr in_addr;
118 name = strtok_r(str, "/", &save_ptr);
119 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
121 ovs_fatal(0, "%s: could not convert to IP address", str);
123 *ip = in_addr.s_addr;
125 netmask = strtok_r(NULL, "/", &save_ptr);
128 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
129 &o[0], &o[1], &o[2], &o[3]) == 4) {
130 mask = htonl((o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3]);
132 int prefix = atoi(netmask);
133 if (prefix <= 0 || prefix > 32) {
134 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
136 } else if (prefix == 32) {
137 mask = htonl(UINT32_MAX);
139 mask = htonl(((1u << prefix) - 1) << (32 - prefix));
143 mask = htonl(UINT32_MAX);
150 if (mask != htonl(UINT32_MAX)) {
151 ovs_fatal(0, "%s: netmask not allowed here", str_);
159 str_to_tun_id(const char *str, ovs_be64 *tun_idp, ovs_be64 *maskp)
161 uint64_t tun_id, mask;
165 tun_id = strtoull(str, &tail, 0);
166 if (errno || (*tail != '\0' && *tail != '/')) {
171 mask = strtoull(tail + 1, &tail, 0);
172 if (errno || *tail != '\0') {
179 *tun_idp = htonll(tun_id);
180 *maskp = htonll(mask);
184 ovs_fatal(0, "%s: bad syntax for tunnel id", str);
188 str_to_vlan_tci(const char *str, ovs_be16 *vlan_tcip, ovs_be16 *maskp)
190 uint16_t vlan_tci, mask;
194 vlan_tci = strtol(str, &tail, 0);
195 if (errno || (*tail != '\0' && *tail != '/')) {
200 mask = strtol(tail + 1, &tail, 0);
201 if (errno || *tail != '\0') {
208 *vlan_tcip = htons(vlan_tci);
209 *maskp = htons(mask);
213 ovs_fatal(0, "%s: bad syntax for vlan_tci", str);
217 str_to_ipv6(const char *str_, struct in6_addr *addrp, struct in6_addr *maskp)
219 char *str = xstrdup(str_);
220 char *save_ptr = NULL;
221 const char *name, *netmask;
222 struct in6_addr addr, mask;
225 name = strtok_r(str, "/", &save_ptr);
226 retval = name ? lookup_ipv6(name, &addr) : EINVAL;
228 ovs_fatal(0, "%s: could not convert to IPv6 address", str);
231 netmask = strtok_r(NULL, "/", &save_ptr);
233 int prefix = atoi(netmask);
234 if (prefix <= 0 || prefix > 128) {
235 ovs_fatal(0, "%s: network prefix bits not between 1 and 128",
238 mask = ipv6_create_mask(prefix);
241 mask = in6addr_exact;
243 *addrp = ipv6_addr_bitand(&addr, &mask);
248 if (!ipv6_mask_is_exact(&mask)) {
249 ovs_fatal(0, "%s: netmask not allowed here", str_);
256 static struct ofp_action_output *
257 put_output_action(struct ofpbuf *b, uint16_t port)
259 struct ofp_action_output *oao;
261 oao = ofputil_put_OFPAT_OUTPUT(b);
262 oao->port = htons(port);
267 parse_enqueue(struct ofpbuf *b, char *arg)
270 char *port = strtok_r(arg, ":q", &sp);
271 char *queue = strtok_r(NULL, "", &sp);
272 struct ofp_action_enqueue *oae;
274 if (port == NULL || queue == NULL) {
275 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
278 oae = ofputil_put_OFPAT_ENQUEUE(b);
279 oae->port = htons(str_to_u32(port));
280 oae->queue_id = htonl(str_to_u32(queue));
284 parse_output(struct ofpbuf *b, char *arg)
286 if (strchr(arg, '[')) {
287 struct nx_action_output_reg *naor;
291 nxm_parse_field_bits(arg, &src, &ofs, &n_bits);
293 naor = ofputil_put_NXAST_OUTPUT_REG(b);
294 naor->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
295 naor->src = htonl(src);
296 naor->max_len = htons(UINT16_MAX);
298 put_output_action(b, str_to_u32(arg));
303 parse_resubmit(struct ofpbuf *b, char *arg)
305 struct nx_action_resubmit *nar;
306 char *in_port_s, *table_s;
310 in_port_s = strsep(&arg, ",");
311 if (in_port_s && in_port_s[0]) {
312 if (!ofputil_port_from_string(in_port_s, &in_port)) {
313 in_port = str_to_u32(in_port_s);
316 in_port = OFPP_IN_PORT;
319 table_s = strsep(&arg, ",");
320 table = table_s && table_s[0] ? str_to_u32(table_s) : 255;
322 if (in_port == OFPP_IN_PORT && table == 255) {
323 ovs_fatal(0, "at least one \"in_port\" or \"table\" must be specified "
327 if (in_port != OFPP_IN_PORT && table == 255) {
328 nar = ofputil_put_NXAST_RESUBMIT(b);
330 nar = ofputil_put_NXAST_RESUBMIT_TABLE(b);
333 nar->in_port = htons(in_port);
337 parse_set_tunnel(struct ofpbuf *b, const char *arg)
339 uint64_t tun_id = str_to_u64(arg);
340 if (tun_id > UINT32_MAX) {
341 ofputil_put_NXAST_SET_TUNNEL64(b)->tun_id = htonll(tun_id);
343 ofputil_put_NXAST_SET_TUNNEL(b)->tun_id = htonl(tun_id);
348 parse_note(struct ofpbuf *b, const char *arg)
350 size_t start_ofs = b->size;
351 struct nx_action_note *nan;
355 nan = ofputil_put_NXAST_NOTE(b);
357 b->size -= sizeof nan->note;
358 while (*arg != '\0') {
369 byte = hexits_value(arg, 2, &ok);
371 ovs_fatal(0, "bad hex digit in `note' argument");
373 ofpbuf_put(b, &byte, 1);
378 len = b->size - start_ofs;
379 remainder = len % OFP_ACTION_ALIGN;
381 ofpbuf_put_zeros(b, OFP_ACTION_ALIGN - remainder);
383 nan = (struct nx_action_note *)((char *)b->data + start_ofs);
384 nan->len = htons(b->size - start_ofs);
388 parse_named_action(enum ofputil_action_code code, struct ofpbuf *b, char *arg)
390 struct ofp_action_dl_addr *oada;
391 struct ofp_action_vlan_pcp *oavp;
392 struct ofp_action_vlan_vid *oavv;
393 struct ofp_action_nw_addr *oana;
394 struct ofp_action_tp_port *oata;
397 case OFPUTIL_OFPAT_OUTPUT:
398 parse_output(b, arg);
401 case OFPUTIL_OFPAT_SET_VLAN_VID:
402 oavv = ofputil_put_OFPAT_SET_VLAN_VID(b);
403 oavv->vlan_vid = htons(str_to_u32(arg));
406 case OFPUTIL_OFPAT_SET_VLAN_PCP:
407 oavp = ofputil_put_OFPAT_SET_VLAN_PCP(b);
408 oavp->vlan_pcp = str_to_u32(arg);
411 case OFPUTIL_OFPAT_STRIP_VLAN:
412 ofputil_put_OFPAT_STRIP_VLAN(b);
415 case OFPUTIL_OFPAT_SET_DL_SRC:
416 case OFPUTIL_OFPAT_SET_DL_DST:
417 oada = ofputil_put_action(code, b);
418 str_to_mac(arg, oada->dl_addr);
421 case OFPUTIL_OFPAT_SET_NW_SRC:
422 case OFPUTIL_OFPAT_SET_NW_DST:
423 oana = ofputil_put_action(code, b);
424 str_to_ip(arg, &oana->nw_addr, NULL);
427 case OFPUTIL_OFPAT_SET_NW_TOS:
428 ofputil_put_OFPAT_SET_NW_TOS(b)->nw_tos = str_to_u32(arg);
431 case OFPUTIL_OFPAT_SET_TP_SRC:
432 case OFPUTIL_OFPAT_SET_TP_DST:
433 oata = ofputil_put_action(code, b);
434 oata->tp_port = htons(str_to_u32(arg));
437 case OFPUTIL_OFPAT_ENQUEUE:
438 parse_enqueue(b, arg);
441 case OFPUTIL_NXAST_RESUBMIT:
442 parse_resubmit(b, arg);
445 case OFPUTIL_NXAST_SET_TUNNEL:
446 parse_set_tunnel(b, arg);
449 case OFPUTIL_NXAST_SET_QUEUE:
450 ofputil_put_NXAST_SET_QUEUE(b)->queue_id = htonl(str_to_u32(arg));
453 case OFPUTIL_NXAST_POP_QUEUE:
454 ofputil_put_NXAST_POP_QUEUE(b);
457 case OFPUTIL_NXAST_REG_MOVE:
458 nxm_parse_reg_move(ofputil_put_NXAST_REG_MOVE(b), arg);
461 case OFPUTIL_NXAST_REG_LOAD:
462 nxm_parse_reg_load(ofputil_put_NXAST_REG_LOAD(b), arg);
465 case OFPUTIL_NXAST_NOTE:
469 case OFPUTIL_NXAST_SET_TUNNEL64:
470 ofputil_put_NXAST_SET_TUNNEL64(b)->tun_id = htonll(str_to_u64(arg));
473 case OFPUTIL_NXAST_MULTIPATH:
474 multipath_parse(ofputil_put_NXAST_MULTIPATH(b), arg);
477 case OFPUTIL_NXAST_AUTOPATH:
478 autopath_parse(ofputil_put_NXAST_AUTOPATH(b), arg);
481 case OFPUTIL_NXAST_BUNDLE:
482 bundle_parse(b, arg);
485 case OFPUTIL_NXAST_BUNDLE_LOAD:
486 bundle_parse_load(b, arg);
489 case OFPUTIL_NXAST_RESUBMIT_TABLE:
490 case OFPUTIL_NXAST_OUTPUT_REG:
496 str_to_action(char *str, struct ofpbuf *b)
498 char *pos, *act, *arg;
503 while (ofputil_parse_key_value(&pos, &act, &arg)) {
507 code = ofputil_action_code_from_name(act);
509 parse_named_action(code, b, arg);
510 } else if (!strcasecmp(act, "drop")) {
511 /* A drop action in OpenFlow occurs by just not setting
514 ovs_fatal(0, "Drop actions must not be preceded by other "
516 } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
517 ovs_fatal(0, "Drop actions must not be followed by other "
521 } else if (!strcasecmp(act, "CONTROLLER")) {
522 struct ofp_action_output *oao;
523 oao = put_output_action(b, OFPP_CONTROLLER);
525 /* Unless a numeric argument is specified, we send the whole
526 * packet to the controller. */
527 if (arg[0] && (strspn(arg, "0123456789") == strlen(arg))) {
528 oao->max_len = htons(str_to_u32(arg));
530 oao->max_len = htons(UINT16_MAX);
532 } else if (ofputil_port_from_string(act, &port)) {
533 put_output_action(b, port);
535 ovs_fatal(0, "Unknown action: %s", act);
548 parse_protocol(const char *name, const struct protocol **p_out)
550 static const struct protocol protocols[] = {
551 { "ip", ETH_TYPE_IP, 0 },
552 { "arp", ETH_TYPE_ARP, 0 },
553 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
554 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
555 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
556 { "ipv6", ETH_TYPE_IPV6, 0 },
557 { "ip6", ETH_TYPE_IPV6, 0 },
558 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
559 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
560 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
562 const struct protocol *p;
564 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
565 if (!strcmp(p->name, name)) {
574 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 1);
576 FIELD(F_TUN_ID, "tun_id", 0) \
577 FIELD(F_IN_PORT, "in_port", FWW_IN_PORT) \
578 FIELD(F_DL_VLAN, "dl_vlan", 0) \
579 FIELD(F_DL_VLAN_PCP, "dl_vlan_pcp", 0) \
580 FIELD(F_VLAN_TCI, "vlan_tci", 0) \
581 FIELD(F_DL_SRC, "dl_src", FWW_DL_SRC) \
582 FIELD(F_DL_DST, "dl_dst", FWW_DL_DST | FWW_ETH_MCAST) \
583 FIELD(F_DL_TYPE, "dl_type", FWW_DL_TYPE) \
584 FIELD(F_NW_SRC, "nw_src", 0) \
585 FIELD(F_NW_DST, "nw_dst", 0) \
586 FIELD(F_NW_PROTO, "nw_proto", FWW_NW_PROTO) \
587 FIELD(F_NW_TOS, "nw_tos", FWW_NW_TOS) \
588 FIELD(F_TP_SRC, "tp_src", FWW_TP_SRC) \
589 FIELD(F_TP_DST, "tp_dst", FWW_TP_DST) \
590 FIELD(F_ICMP_TYPE, "icmp_type", FWW_TP_SRC) \
591 FIELD(F_ICMP_CODE, "icmp_code", FWW_TP_DST) \
592 FIELD(F_ARP_SHA, "arp_sha", FWW_ARP_SHA) \
593 FIELD(F_ARP_THA, "arp_tha", FWW_ARP_THA) \
594 FIELD(F_IPV6_SRC, "ipv6_src", 0) \
595 FIELD(F_IPV6_DST, "ipv6_dst", 0) \
596 FIELD(F_ND_TARGET, "nd_target", FWW_ND_TARGET) \
597 FIELD(F_ND_SLL, "nd_sll", FWW_ARP_SHA) \
598 FIELD(F_ND_TLL, "nd_tll", FWW_ARP_THA)
601 #define FIELD(ENUM, NAME, WILDCARD) ENUM,
608 enum field_index index;
610 flow_wildcards_t wildcard; /* FWW_* bit. */
614 ofp_fatal(const char *flow, bool verbose, const char *format, ...)
619 fprintf(stderr, "%s:\n", flow);
622 va_start(args, format);
623 ovs_fatal_valist(0, format, args);
627 parse_field_name(const char *name, const struct field **f_out)
629 static const struct field fields[N_FIELDS] = {
630 #define FIELD(ENUM, NAME, WILDCARD) { ENUM, NAME, WILDCARD },
634 const struct field *f;
636 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
637 if (!strcmp(f->name, name)) {
647 parse_field_value(struct cls_rule *rule, enum field_index index,
650 uint8_t mac[ETH_ADDR_LEN], mac_mask[ETH_ADDR_LEN];
651 ovs_be64 tun_id, tun_mask;
653 ovs_be16 tci, tci_mask;
654 struct in6_addr ipv6, ipv6_mask;
659 str_to_tun_id(value, &tun_id, &tun_mask);
660 cls_rule_set_tun_id_masked(rule, tun_id, tun_mask);
664 if (!ofputil_port_from_string(value, &port_no)) {
665 port_no = atoi(value);
667 cls_rule_set_in_port(rule, port_no);
671 cls_rule_set_dl_vlan(rule, htons(str_to_u32(value)));
675 cls_rule_set_dl_vlan_pcp(rule, str_to_u32(value));
679 str_to_vlan_tci(value, &tci, &tci_mask);
680 cls_rule_set_dl_tci_masked(rule, tci, tci_mask);
684 str_to_mac(value, mac);
685 cls_rule_set_dl_src(rule, mac);
689 str_to_eth_dst(value, mac, mac_mask);
690 cls_rule_set_dl_dst_masked(rule, mac, mac_mask);
694 cls_rule_set_dl_type(rule, htons(str_to_u32(value)));
698 str_to_ip(value, &ip, &mask);
699 cls_rule_set_nw_src_masked(rule, ip, mask);
703 str_to_ip(value, &ip, &mask);
704 cls_rule_set_nw_dst_masked(rule, ip, mask);
708 cls_rule_set_nw_proto(rule, str_to_u32(value));
712 cls_rule_set_nw_tos(rule, str_to_u32(value));
716 cls_rule_set_tp_src(rule, htons(str_to_u32(value)));
720 cls_rule_set_tp_dst(rule, htons(str_to_u32(value)));
724 cls_rule_set_icmp_type(rule, str_to_u32(value));
728 cls_rule_set_icmp_code(rule, str_to_u32(value));
732 str_to_mac(value, mac);
733 cls_rule_set_arp_sha(rule, mac);
737 str_to_mac(value, mac);
738 cls_rule_set_arp_tha(rule, mac);
742 str_to_ipv6(value, &ipv6, &ipv6_mask);
743 cls_rule_set_ipv6_src_masked(rule, &ipv6, &ipv6_mask);
747 str_to_ipv6(value, &ipv6, &ipv6_mask);
748 cls_rule_set_ipv6_dst_masked(rule, &ipv6, &ipv6_mask);
752 str_to_ipv6(value, &ipv6, NULL);
753 cls_rule_set_nd_target(rule, &ipv6);
757 str_to_mac(value, mac);
758 cls_rule_set_arp_sha(rule, mac);
762 str_to_mac(value, mac);
763 cls_rule_set_arp_tha(rule, mac);
772 parse_reg_value(struct cls_rule *rule, int reg_idx, const char *value)
774 /* This uses an oversized destination field (64 bits when 32 bits would do)
775 * because some sscanf() implementations truncate the range of %i
776 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
777 * value of 0x7fff. The other alternatives are to allow only a single
778 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
780 unsigned long long int reg_value, reg_mask;
782 if (!strcmp(value, "ANY") || !strcmp(value, "*")) {
783 cls_rule_set_reg_masked(rule, reg_idx, 0, 0);
784 } else if (sscanf(value, "%lli/%lli",
785 ®_value, ®_mask) == 2) {
786 cls_rule_set_reg_masked(rule, reg_idx, reg_value, reg_mask);
787 } else if (sscanf(value, "%lli", ®_value)) {
788 cls_rule_set_reg(rule, reg_idx, reg_value);
790 ovs_fatal(0, "register fields must take the form <value> "
791 "or <value>/<mask>");
795 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
796 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
797 * If 'actions' is specified, an action must be in 'string' and may be expanded
800 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
801 * constant for 'command'. To parse syntax for an OFPST_FLOW or
802 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
804 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
814 char *string = xstrdup(str_);
815 char *save_ptr = NULL;
824 fields = F_ACTIONS | F_COOKIE | F_TIMEOUT | F_PRIORITY;
831 case OFPFC_DELETE_STRICT:
832 fields = F_OUT_PORT | F_PRIORITY;
836 fields = F_ACTIONS | F_COOKIE;
839 case OFPFC_MODIFY_STRICT:
840 fields = F_ACTIONS | F_COOKIE | F_PRIORITY;
847 cls_rule_init_catchall(&fm->cr, OFP_DEFAULT_PRIORITY);
848 fm->cookie = htonll(0);
850 fm->command = command;
851 fm->idle_timeout = OFP_FLOW_PERMANENT;
852 fm->hard_timeout = OFP_FLOW_PERMANENT;
853 fm->buffer_id = UINT32_MAX;
854 fm->out_port = OFPP_NONE;
856 if (fields & F_ACTIONS) {
857 struct ofpbuf actions;
860 act_str = strstr(string, "action");
862 ofp_fatal(str_, verbose, "must specify an action");
866 act_str = strchr(act_str + 1, '=');
868 ofp_fatal(str_, verbose, "must specify an action");
873 ofpbuf_init(&actions, sizeof(union ofp_action));
874 str_to_action(act_str, &actions);
875 fm->actions = ofpbuf_steal_data(&actions);
876 fm->n_actions = actions.size / sizeof(union ofp_action);
881 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
882 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
883 const struct protocol *p;
885 if (parse_protocol(name, &p)) {
886 cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
888 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
891 const struct field *f;
894 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
896 ofp_fatal(str_, verbose, "field %s missing value", name);
899 if (!strcmp(name, "table")) {
900 fm->table_id = atoi(value);
901 } else if (!strcmp(name, "out_port")) {
902 fm->out_port = atoi(value);
903 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
904 fm->cr.priority = atoi(value);
905 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
906 fm->idle_timeout = atoi(value);
907 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
908 fm->hard_timeout = atoi(value);
909 } else if (fields & F_COOKIE && !strcmp(name, "cookie")) {
910 fm->cookie = htonll(str_to_u64(value));
911 } else if (parse_field_name(name, &f)) {
912 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
914 fm->cr.wc.wildcards |= f->wildcard;
915 cls_rule_zero_wildcarded_fields(&fm->cr);
916 } else if (f->index == F_NW_SRC) {
917 cls_rule_set_nw_src_masked(&fm->cr, 0, 0);
918 } else if (f->index == F_NW_DST) {
919 cls_rule_set_nw_dst_masked(&fm->cr, 0, 0);
920 } else if (f->index == F_IPV6_SRC) {
921 cls_rule_set_ipv6_src_masked(&fm->cr,
922 &in6addr_any, &in6addr_any);
923 } else if (f->index == F_IPV6_DST) {
924 cls_rule_set_ipv6_dst_masked(&fm->cr,
925 &in6addr_any, &in6addr_any);
926 } else if (f->index == F_DL_VLAN) {
927 cls_rule_set_any_vid(&fm->cr);
928 } else if (f->index == F_DL_VLAN_PCP) {
929 cls_rule_set_any_pcp(&fm->cr);
934 parse_field_value(&fm->cr, f->index, value);
936 } else if (!strncmp(name, "reg", 3)
937 && isdigit((unsigned char) name[3])) {
938 unsigned int reg_idx = atoi(name + 3);
939 if (reg_idx >= FLOW_N_REGS) {
941 fprintf(stderr, "%s:\n", str_);
943 ofp_fatal(str_, verbose, "only %d registers supported", FLOW_N_REGS);
945 parse_reg_value(&fm->cr, reg_idx, value);
946 } else if (!strcmp(name, "duration")
947 || !strcmp(name, "n_packets")
948 || !strcmp(name, "n_bytes")) {
949 /* Ignore these, so that users can feed the output of
950 * "ovs-ofctl dump-flows" back into commands that parse
953 ofp_fatal(str_, verbose, "unknown keyword %s", name);
961 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
962 * (one of OFPFC_*) and appends the parsed OpenFlow message to 'packets'.
963 * '*cur_format' should initially contain the flow format currently configured
964 * on the connection; this function will add a message to change the flow
965 * format and update '*cur_format', if this is necessary to add the parsed
968 parse_ofp_flow_mod_str(struct list *packets, enum nx_flow_format *cur_format,
969 bool *flow_mod_table_id, char *string, uint16_t command,
972 enum nx_flow_format min_format, next_format;
973 struct cls_rule rule_copy;
974 struct ofpbuf actions;
976 struct ofputil_flow_mod fm;
978 ofpbuf_init(&actions, 64);
979 parse_ofp_str(&fm, command, string, verbose);
981 min_format = ofputil_min_flow_format(&fm.cr);
982 next_format = MAX(*cur_format, min_format);
983 if (next_format != *cur_format) {
984 struct ofpbuf *sff = ofputil_make_set_flow_format(next_format);
985 list_push_back(packets, &sff->list_node);
986 *cur_format = next_format;
989 /* Normalize a copy of the rule. This ensures that non-normalized flows
990 * get logged but doesn't affect what gets sent to the switch, so that the
991 * switch can do whatever it likes with the flow. */
993 ofputil_normalize_rule(&rule_copy, next_format);
995 if (fm.table_id != 0xff && !*flow_mod_table_id) {
996 struct ofpbuf *sff = ofputil_make_flow_mod_table_id(true);
997 list_push_back(packets, &sff->list_node);
998 *flow_mod_table_id = true;
1001 ofm = ofputil_encode_flow_mod(&fm, *cur_format, *flow_mod_table_id);
1002 list_push_back(packets, &ofm->list_node);
1004 ofpbuf_uninit(&actions);
1007 /* Similar to parse_ofp_flow_mod_str(), except that the string is read from
1008 * 'stream' and the command is always OFPFC_ADD. Returns false if end-of-file
1009 * is reached before reading a flow, otherwise true. */
1011 parse_ofp_flow_mod_file(struct list *packets,
1012 enum nx_flow_format *cur, bool *flow_mod_table_id,
1013 FILE *stream, uint16_t command)
1019 ok = ds_get_preprocessed_line(&s, stream) == 0;
1021 parse_ofp_flow_mod_str(packets, cur, flow_mod_table_id,
1022 ds_cstr(&s), command, true);
1030 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
1031 bool aggregate, char *string)
1033 struct ofputil_flow_mod fm;
1035 parse_ofp_str(&fm, -1, string, false);
1036 fsr->aggregate = aggregate;
1038 fsr->out_port = fm.out_port;
1039 fsr->table_id = fm.table_id;