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_);
257 put_action(struct ofpbuf *b, size_t size, uint16_t type)
259 struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
260 ah->type = htons(type);
261 ah->len = htons(size);
265 static struct ofp_action_output *
266 put_output_action(struct ofpbuf *b, uint16_t port)
268 struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
269 oao->port = htons(port);
274 put_enqueue_action(struct ofpbuf *b, uint16_t port, uint32_t queue)
276 struct ofp_action_enqueue *oae = put_action(b, sizeof *oae, OFPAT_ENQUEUE);
277 oae->port = htons(port);
278 oae->queue_id = htonl(queue);
282 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
284 struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
285 str_to_mac(addr, oada->dl_addr);
289 parse_port_name(const char *name, uint16_t *port)
295 static const struct pair pairs[] = {
296 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
302 DEF_PAIR(CONTROLLER),
307 static const int n_pairs = ARRAY_SIZE(pairs);
310 for (i = 0; i < n_pairs; i++) {
311 if (!strcasecmp(name, pairs[i].name)) {
312 *port = pairs[i].value;
320 parse_resubmit(struct nx_action_resubmit *nar, char *arg)
322 char *in_port_s, *table_s;
326 in_port_s = strsep(&arg, ",");
327 if (in_port_s && in_port_s[0]) {
328 if (!parse_port_name(in_port_s, &in_port)) {
329 in_port = str_to_u32(in_port_s);
332 in_port = OFPP_IN_PORT;
335 table_s = strsep(&arg, ",");
336 table = table_s && table_s[0] ? str_to_u32(table_s) : 255;
338 if (in_port == OFPP_IN_PORT && table == 255) {
339 ovs_fatal(0, "at least one \"in_port\" or \"table\" must be specified "
343 nar->vendor = htonl(NX_VENDOR_ID);
344 nar->in_port = htons(in_port);
345 if (in_port != OFPP_IN_PORT && table == 255) {
346 nar->subtype = htons(NXAST_RESUBMIT);
348 nar->subtype = htons(NXAST_RESUBMIT_TABLE);
354 str_to_action(char *str, struct ofpbuf *b)
363 char empty_string[] = "";
368 pos += strspn(pos, ", \t\r\n");
374 ovs_fatal(0, "Drop actions must not be followed by other actions");
378 actlen = strcspn(pos, ":(, \t\r\n");
379 if (act[actlen] == ':') {
380 /* The argument can be separated by a colon. */
383 arg = act + actlen + 1;
384 arglen = strcspn(arg, ", \t\r\n");
385 pos = arg + arglen + (arg[arglen] != '\0');
387 } else if (act[actlen] == '(') {
388 /* The argument can be surrounded by balanced parentheses. The
389 * outermost set of parentheses is removed. */
393 arg = act + actlen + 1;
394 for (arglen = 0; level > 0; arglen++) {
395 switch (arg[arglen]) {
397 ovs_fatal(0, "unbalanced parentheses in argument to %s "
409 arg[arglen - 1] = '\0';
412 /* There might be no argument at all. */
414 pos = act + actlen + (act[actlen] != '\0');
418 if (!strcasecmp(act, "mod_vlan_vid")) {
419 struct ofp_action_vlan_vid *va;
420 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
421 va->vlan_vid = htons(str_to_u32(arg));
422 } else if (!strcasecmp(act, "mod_vlan_pcp")) {
423 struct ofp_action_vlan_pcp *va;
424 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
425 va->vlan_pcp = str_to_u32(arg);
426 } else if (!strcasecmp(act, "strip_vlan")) {
427 struct ofp_action_header *ah;
428 ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
429 ah->type = htons(OFPAT_STRIP_VLAN);
430 } else if (!strcasecmp(act, "mod_dl_src")) {
431 put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
432 } else if (!strcasecmp(act, "mod_dl_dst")) {
433 put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
434 } else if (!strcasecmp(act, "mod_nw_src")) {
435 struct ofp_action_nw_addr *na;
436 na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
437 str_to_ip(arg, &na->nw_addr, NULL);
438 } else if (!strcasecmp(act, "mod_nw_dst")) {
439 struct ofp_action_nw_addr *na;
440 na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
441 str_to_ip(arg, &na->nw_addr, NULL);
442 } else if (!strcasecmp(act, "mod_tp_src")) {
443 struct ofp_action_tp_port *ta;
444 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
445 ta->tp_port = htons(str_to_u32(arg));
446 } else if (!strcasecmp(act, "mod_tp_dst")) {
447 struct ofp_action_tp_port *ta;
448 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
449 ta->tp_port = htons(str_to_u32(arg));
450 } else if (!strcasecmp(act, "mod_nw_tos")) {
451 struct ofp_action_nw_tos *nt;
452 nt = put_action(b, sizeof *nt, OFPAT_SET_NW_TOS);
453 nt->nw_tos = str_to_u32(arg);
454 } else if (!strcasecmp(act, "resubmit")) {
455 struct nx_action_resubmit *nar;
456 nar = put_action(b, sizeof *nar, OFPAT_VENDOR);
457 parse_resubmit(nar, arg);
458 } else if (!strcasecmp(act, "set_tunnel")
459 || !strcasecmp(act, "set_tunnel64")) {
460 uint64_t tun_id = str_to_u64(arg);
461 if (!strcasecmp(act, "set_tunnel64") || tun_id > UINT32_MAX) {
462 struct nx_action_set_tunnel64 *nast64;
463 nast64 = put_action(b, sizeof *nast64, OFPAT_VENDOR);
464 nast64->vendor = htonl(NX_VENDOR_ID);
465 nast64->subtype = htons(NXAST_SET_TUNNEL64);
466 nast64->tun_id = htonll(tun_id);
468 struct nx_action_set_tunnel *nast;
469 nast = put_action(b, sizeof *nast, OFPAT_VENDOR);
470 nast->vendor = htonl(NX_VENDOR_ID);
471 nast->subtype = htons(NXAST_SET_TUNNEL);
472 nast->tun_id = htonl(tun_id);
474 } else if (!strcasecmp(act, "set_queue")) {
475 struct nx_action_set_queue *nasq;
476 nasq = put_action(b, sizeof *nasq, OFPAT_VENDOR);
477 nasq->vendor = htonl(NX_VENDOR_ID);
478 nasq->subtype = htons(NXAST_SET_QUEUE);
479 nasq->queue_id = htonl(str_to_u32(arg));
480 } else if (!strcasecmp(act, "pop_queue")) {
481 struct nx_action_header *nah;
482 nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
483 nah->vendor = htonl(NX_VENDOR_ID);
484 nah->subtype = htons(NXAST_POP_QUEUE);
485 } else if (!strcasecmp(act, "note")) {
486 size_t start_ofs = b->size;
487 struct nx_action_note *nan;
491 nan = put_action(b, sizeof *nan, OFPAT_VENDOR);
492 nan->vendor = htonl(NX_VENDOR_ID);
493 nan->subtype = htons(NXAST_NOTE);
495 b->size -= sizeof nan->note;
496 while (*arg != '\0') {
507 byte = hexits_value(arg, 2, &ok);
509 ovs_fatal(0, "bad hex digit in `note' argument");
511 ofpbuf_put(b, &byte, 1);
516 len = b->size - start_ofs;
517 remainder = len % OFP_ACTION_ALIGN;
519 ofpbuf_put_zeros(b, OFP_ACTION_ALIGN - remainder);
521 nan = (struct nx_action_note *)((char *)b->data + start_ofs);
522 nan->len = htons(b->size - start_ofs);
523 } else if (!strcasecmp(act, "move")) {
524 struct nx_action_reg_move *move;
525 move = ofpbuf_put_uninit(b, sizeof *move);
526 nxm_parse_reg_move(move, arg);
527 } else if (!strcasecmp(act, "load")) {
528 struct nx_action_reg_load *load;
529 load = ofpbuf_put_uninit(b, sizeof *load);
530 nxm_parse_reg_load(load, arg);
531 } else if (!strcasecmp(act, "multipath")) {
532 struct nx_action_multipath *nam;
533 nam = ofpbuf_put_uninit(b, sizeof *nam);
534 multipath_parse(nam, arg);
535 } else if (!strcasecmp(act, "autopath")) {
536 struct nx_action_autopath *naa;
537 naa = ofpbuf_put_uninit(b, sizeof *naa);
538 autopath_parse(naa, arg);
539 } else if (!strcasecmp(act, "bundle")) {
540 bundle_parse(b, arg);
541 } else if (!strcasecmp(act, "bundle_load")) {
542 bundle_parse_load(b, arg);
543 } else if (!strcasecmp(act, "output")) {
544 put_output_action(b, str_to_u32(arg));
545 } else if (!strcasecmp(act, "enqueue")) {
547 char *port_s = strtok_r(arg, ":q", &sp);
548 char *queue = strtok_r(NULL, "", &sp);
549 if (port_s == NULL || queue == NULL) {
550 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
552 put_enqueue_action(b, str_to_u32(port_s), str_to_u32(queue));
553 } else if (!strcasecmp(act, "drop")) {
554 /* A drop action in OpenFlow occurs by just not setting
558 ovs_fatal(0, "Drop actions must not be preceded by other "
561 } else if (!strcasecmp(act, "CONTROLLER")) {
562 struct ofp_action_output *oao;
563 oao = put_output_action(b, OFPP_CONTROLLER);
565 /* Unless a numeric argument is specified, we send the whole
566 * packet to the controller. */
567 if (arg[0] && (strspn(arg, "0123456789") == strlen(arg))) {
568 oao->max_len = htons(str_to_u32(arg));
570 oao->max_len = htons(UINT16_MAX);
572 } else if (parse_port_name(act, &port)) {
573 put_output_action(b, port);
574 } else if (strspn(act, "0123456789") == strlen(act)) {
575 put_output_action(b, str_to_u32(act));
577 ovs_fatal(0, "Unknown action: %s", act);
590 parse_protocol(const char *name, const struct protocol **p_out)
592 static const struct protocol protocols[] = {
593 { "ip", ETH_TYPE_IP, 0 },
594 { "arp", ETH_TYPE_ARP, 0 },
595 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
596 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
597 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
598 { "ipv6", ETH_TYPE_IPV6, 0 },
599 { "ip6", ETH_TYPE_IPV6, 0 },
600 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
601 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
602 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
604 const struct protocol *p;
606 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
607 if (!strcmp(p->name, name)) {
617 FIELD(F_TUN_ID, "tun_id", 0) \
618 FIELD(F_IN_PORT, "in_port", FWW_IN_PORT) \
619 FIELD(F_DL_VLAN, "dl_vlan", 0) \
620 FIELD(F_DL_VLAN_PCP, "dl_vlan_pcp", 0) \
621 FIELD(F_VLAN_TCI, "vlan_tci", 0) \
622 FIELD(F_DL_SRC, "dl_src", FWW_DL_SRC) \
623 FIELD(F_DL_DST, "dl_dst", FWW_DL_DST | FWW_ETH_MCAST) \
624 FIELD(F_DL_TYPE, "dl_type", FWW_DL_TYPE) \
625 FIELD(F_NW_SRC, "nw_src", 0) \
626 FIELD(F_NW_DST, "nw_dst", 0) \
627 FIELD(F_NW_PROTO, "nw_proto", FWW_NW_PROTO) \
628 FIELD(F_NW_TOS, "nw_tos", FWW_NW_TOS) \
629 FIELD(F_TP_SRC, "tp_src", FWW_TP_SRC) \
630 FIELD(F_TP_DST, "tp_dst", FWW_TP_DST) \
631 FIELD(F_ICMP_TYPE, "icmp_type", FWW_TP_SRC) \
632 FIELD(F_ICMP_CODE, "icmp_code", FWW_TP_DST) \
633 FIELD(F_ARP_SHA, "arp_sha", FWW_ARP_SHA) \
634 FIELD(F_ARP_THA, "arp_tha", FWW_ARP_THA) \
635 FIELD(F_IPV6_SRC, "ipv6_src", 0) \
636 FIELD(F_IPV6_DST, "ipv6_dst", 0) \
637 FIELD(F_ND_TARGET, "nd_target", FWW_ND_TARGET) \
638 FIELD(F_ND_SLL, "nd_sll", FWW_ARP_SHA) \
639 FIELD(F_ND_TLL, "nd_tll", FWW_ARP_THA)
642 #define FIELD(ENUM, NAME, WILDCARD) ENUM,
649 enum field_index index;
651 flow_wildcards_t wildcard; /* FWW_* bit. */
655 ofp_fatal(const char *flow, bool verbose, const char *format, ...)
660 fprintf(stderr, "%s:\n", flow);
663 va_start(args, format);
664 ovs_fatal_valist(0, format, args);
668 parse_field_name(const char *name, const struct field **f_out)
670 static const struct field fields[N_FIELDS] = {
671 #define FIELD(ENUM, NAME, WILDCARD) { ENUM, NAME, WILDCARD },
675 const struct field *f;
677 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
678 if (!strcmp(f->name, name)) {
688 parse_field_value(struct cls_rule *rule, enum field_index index,
691 uint8_t mac[ETH_ADDR_LEN], mac_mask[ETH_ADDR_LEN];
692 ovs_be64 tun_id, tun_mask;
694 ovs_be16 tci, tci_mask;
695 struct in6_addr ipv6, ipv6_mask;
700 str_to_tun_id(value, &tun_id, &tun_mask);
701 cls_rule_set_tun_id_masked(rule, tun_id, tun_mask);
705 if (!parse_port_name(value, &port_no)) {
706 port_no = atoi(value);
708 cls_rule_set_in_port(rule, port_no);
712 cls_rule_set_dl_vlan(rule, htons(str_to_u32(value)));
716 cls_rule_set_dl_vlan_pcp(rule, str_to_u32(value));
720 str_to_vlan_tci(value, &tci, &tci_mask);
721 cls_rule_set_dl_tci_masked(rule, tci, tci_mask);
725 str_to_mac(value, mac);
726 cls_rule_set_dl_src(rule, mac);
730 str_to_eth_dst(value, mac, mac_mask);
731 cls_rule_set_dl_dst_masked(rule, mac, mac_mask);
735 cls_rule_set_dl_type(rule, htons(str_to_u32(value)));
739 str_to_ip(value, &ip, &mask);
740 cls_rule_set_nw_src_masked(rule, ip, mask);
744 str_to_ip(value, &ip, &mask);
745 cls_rule_set_nw_dst_masked(rule, ip, mask);
749 cls_rule_set_nw_proto(rule, str_to_u32(value));
753 cls_rule_set_nw_tos(rule, str_to_u32(value));
757 cls_rule_set_tp_src(rule, htons(str_to_u32(value)));
761 cls_rule_set_tp_dst(rule, htons(str_to_u32(value)));
765 cls_rule_set_icmp_type(rule, str_to_u32(value));
769 cls_rule_set_icmp_code(rule, str_to_u32(value));
773 str_to_mac(value, mac);
774 cls_rule_set_arp_sha(rule, mac);
778 str_to_mac(value, mac);
779 cls_rule_set_arp_tha(rule, mac);
783 str_to_ipv6(value, &ipv6, &ipv6_mask);
784 cls_rule_set_ipv6_src_masked(rule, &ipv6, &ipv6_mask);
788 str_to_ipv6(value, &ipv6, &ipv6_mask);
789 cls_rule_set_ipv6_dst_masked(rule, &ipv6, &ipv6_mask);
793 str_to_ipv6(value, &ipv6, NULL);
794 cls_rule_set_nd_target(rule, ipv6);
798 str_to_mac(value, mac);
799 cls_rule_set_arp_sha(rule, mac);
803 str_to_mac(value, mac);
804 cls_rule_set_arp_tha(rule, mac);
813 parse_reg_value(struct cls_rule *rule, int reg_idx, const char *value)
815 uint32_t reg_value, reg_mask;
817 if (!strcmp(value, "ANY") || !strcmp(value, "*")) {
818 cls_rule_set_reg_masked(rule, reg_idx, 0, 0);
819 } else if (sscanf(value, "%"SCNi32"/%"SCNi32,
820 ®_value, ®_mask) == 2) {
821 cls_rule_set_reg_masked(rule, reg_idx, reg_value, reg_mask);
822 } else if (sscanf(value, "%"SCNi32, ®_value)) {
823 cls_rule_set_reg(rule, reg_idx, reg_value);
825 ovs_fatal(0, "register fields must take the form <value> "
826 "or <value>/<mask>");
830 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
831 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
832 * If 'actions' is specified, an action must be in 'string' and may be expanded
835 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
836 * constant for 'command'. To parse syntax for an OFPST_FLOW or
837 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
839 parse_ofp_str(struct flow_mod *fm, int command, const char *str_, bool verbose)
848 char *string = xstrdup(str_);
849 char *save_ptr = NULL;
858 fields = F_ACTIONS | F_COOKIE | F_TIMEOUT | F_PRIORITY;
865 case OFPFC_DELETE_STRICT:
866 fields = F_OUT_PORT | F_PRIORITY;
870 fields = F_ACTIONS | F_COOKIE;
873 case OFPFC_MODIFY_STRICT:
874 fields = F_ACTIONS | F_COOKIE | F_PRIORITY;
881 cls_rule_init_catchall(&fm->cr, OFP_DEFAULT_PRIORITY);
882 fm->cookie = htonll(0);
884 fm->command = command;
885 fm->idle_timeout = OFP_FLOW_PERMANENT;
886 fm->hard_timeout = OFP_FLOW_PERMANENT;
887 fm->buffer_id = UINT32_MAX;
888 fm->out_port = OFPP_NONE;
890 if (fields & F_ACTIONS) {
891 struct ofpbuf actions;
894 act_str = strstr(string, "action");
896 ofp_fatal(str_, verbose, "must specify an action");
900 act_str = strchr(act_str + 1, '=');
902 ofp_fatal(str_, verbose, "must specify an action");
907 ofpbuf_init(&actions, sizeof(union ofp_action));
908 str_to_action(act_str, &actions);
909 fm->actions = ofpbuf_steal_data(&actions);
910 fm->n_actions = actions.size / sizeof(union ofp_action);
915 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
916 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
917 const struct protocol *p;
919 if (parse_protocol(name, &p)) {
920 cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
922 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
925 const struct field *f;
928 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
930 ofp_fatal(str_, verbose, "field %s missing value", name);
933 if (!strcmp(name, "table")) {
934 fm->table_id = atoi(value);
935 } else if (!strcmp(name, "out_port")) {
936 fm->out_port = atoi(value);
937 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
938 fm->cr.priority = atoi(value);
939 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
940 fm->idle_timeout = atoi(value);
941 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
942 fm->hard_timeout = atoi(value);
943 } else if (fields & F_COOKIE && !strcmp(name, "cookie")) {
944 fm->cookie = htonll(str_to_u64(value));
945 } else if (parse_field_name(name, &f)) {
946 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
948 fm->cr.wc.wildcards |= f->wildcard;
949 cls_rule_zero_wildcarded_fields(&fm->cr);
950 } else if (f->index == F_NW_SRC) {
951 cls_rule_set_nw_src_masked(&fm->cr, 0, 0);
952 } else if (f->index == F_NW_DST) {
953 cls_rule_set_nw_dst_masked(&fm->cr, 0, 0);
954 } else if (f->index == F_IPV6_SRC) {
955 cls_rule_set_ipv6_src_masked(&fm->cr,
956 &in6addr_any, &in6addr_any);
957 } else if (f->index == F_IPV6_DST) {
958 cls_rule_set_ipv6_dst_masked(&fm->cr,
959 &in6addr_any, &in6addr_any);
960 } else if (f->index == F_DL_VLAN) {
961 cls_rule_set_any_vid(&fm->cr);
962 } else if (f->index == F_DL_VLAN_PCP) {
963 cls_rule_set_any_pcp(&fm->cr);
968 parse_field_value(&fm->cr, f->index, value);
970 } else if (!strncmp(name, "reg", 3)
971 && isdigit((unsigned char) name[3])) {
972 unsigned int reg_idx = atoi(name + 3);
973 if (reg_idx >= FLOW_N_REGS) {
975 fprintf(stderr, "%s:\n", str_);
977 ofp_fatal(str_, verbose, "only %d registers supported", FLOW_N_REGS);
979 parse_reg_value(&fm->cr, reg_idx, value);
980 } else if (!strcmp(name, "duration")
981 || !strcmp(name, "n_packets")
982 || !strcmp(name, "n_bytes")) {
983 /* Ignore these, so that users can feed the output of
984 * "ovs-ofctl dump-flows" back into commands that parse
987 ofp_fatal(str_, verbose, "unknown keyword %s", name);
995 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
996 * (one of OFPFC_*) and appends the parsed OpenFlow message to 'packets'.
997 * '*cur_format' should initially contain the flow format currently configured
998 * on the connection; this function will add a message to change the flow
999 * format and update '*cur_format', if this is necessary to add the parsed
1002 parse_ofp_flow_mod_str(struct list *packets, enum nx_flow_format *cur_format,
1003 bool *flow_mod_table_id, char *string, uint16_t command,
1006 enum nx_flow_format min_format, next_format;
1007 struct cls_rule rule_copy;
1008 struct ofpbuf actions;
1012 ofpbuf_init(&actions, 64);
1013 parse_ofp_str(&fm, command, string, verbose);
1015 min_format = ofputil_min_flow_format(&fm.cr);
1016 next_format = MAX(*cur_format, min_format);
1017 if (next_format != *cur_format) {
1018 struct ofpbuf *sff = ofputil_make_set_flow_format(next_format);
1019 list_push_back(packets, &sff->list_node);
1020 *cur_format = next_format;
1023 /* Normalize a copy of the rule. This ensures that non-normalized flows
1024 * get logged but doesn't affect what gets sent to the switch, so that the
1025 * switch can do whatever it likes with the flow. */
1027 ofputil_normalize_rule(&rule_copy, next_format);
1029 if (fm.table_id != 0xff && !*flow_mod_table_id) {
1030 struct ofpbuf *sff = ofputil_make_flow_mod_table_id(true);
1031 list_push_back(packets, &sff->list_node);
1032 *flow_mod_table_id = true;
1035 ofm = ofputil_encode_flow_mod(&fm, *cur_format, *flow_mod_table_id);
1036 list_push_back(packets, &ofm->list_node);
1038 ofpbuf_uninit(&actions);
1041 /* Similar to parse_ofp_flow_mod_str(), except that the string is read from
1042 * 'stream' and the command is always OFPFC_ADD. Returns false if end-of-file
1043 * is reached before reading a flow, otherwise true. */
1045 parse_ofp_flow_mod_file(struct list *packets,
1046 enum nx_flow_format *cur, bool *flow_mod_table_id,
1047 FILE *stream, uint16_t command)
1053 ok = ds_get_preprocessed_line(&s, stream) == 0;
1055 parse_ofp_flow_mod_str(packets, cur, flow_mod_table_id,
1056 ds_cstr(&s), command, true);
1064 parse_ofp_flow_stats_request_str(struct flow_stats_request *fsr,
1065 bool aggregate, char *string)
1069 parse_ofp_str(&fm, -1, string, false);
1070 fsr->aggregate = aggregate;
1072 fsr->out_port = fm.out_port;
1073 fsr->table_id = fm.table_id;