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"
26 #include "byte-order.h"
27 #include "dynamic-string.h"
29 #include "multipath.h"
33 #include "openflow/openflow.h"
35 #include "socket-util.h"
39 VLOG_DEFINE_THIS_MODULE(ofp_parse);
42 str_to_u32(const char *str)
48 ovs_fatal(0, "missing required numeric argument");
52 value = strtoul(str, &tail, 0);
53 if (errno == EINVAL || errno == ERANGE || *tail) {
54 ovs_fatal(0, "invalid numeric format %s", str);
60 str_to_u64(const char *str)
66 ovs_fatal(0, "missing required numeric argument");
70 value = strtoull(str, &tail, 0);
71 if (errno == EINVAL || errno == ERANGE || *tail) {
72 ovs_fatal(0, "invalid numeric format %s", str);
78 str_to_mac(const char *str, uint8_t mac[6])
80 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
81 != ETH_ADDR_SCAN_COUNT) {
82 ovs_fatal(0, "invalid mac address %s", str);
87 str_to_eth_dst(const char *str,
88 uint8_t mac[ETH_ADDR_LEN], uint8_t mask[ETH_ADDR_LEN])
90 if (sscanf(str, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
91 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))
92 == ETH_ADDR_SCAN_COUNT * 2) {
93 if (!flow_wildcards_is_dl_dst_mask_valid(mask)) {
94 ovs_fatal(0, "%s: invalid Ethernet destination mask (only "
95 "00:00:00:00:00:00, 01:00:00:00:00:00, "
96 "fe:ff:ff:ff:ff:ff, and ff:ff:ff:ff:ff:ff are allowed)",
99 } else if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
100 == ETH_ADDR_SCAN_COUNT) {
101 memset(mask, 0xff, ETH_ADDR_LEN);
103 ovs_fatal(0, "invalid mac address %s", str);
108 str_to_ip(const char *str_, ovs_be32 *ip, ovs_be32 *maskp)
110 char *str = xstrdup(str_);
111 char *save_ptr = NULL;
112 const char *name, *netmask;
113 struct in_addr in_addr;
117 name = strtok_r(str, "/", &save_ptr);
118 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
120 ovs_fatal(0, "%s: could not convert to IP address", str);
122 *ip = in_addr.s_addr;
124 netmask = strtok_r(NULL, "/", &save_ptr);
127 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
128 &o[0], &o[1], &o[2], &o[3]) == 4) {
129 mask = htonl((o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3]);
131 int prefix = atoi(netmask);
132 if (prefix <= 0 || prefix > 32) {
133 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
135 } else if (prefix == 32) {
136 mask = htonl(UINT32_MAX);
138 mask = htonl(((1u << prefix) - 1) << (32 - prefix));
142 mask = htonl(UINT32_MAX);
149 if (mask != htonl(UINT32_MAX)) {
150 ovs_fatal(0, "%s: netmask not allowed here", str_);
158 str_to_tun_id(const char *str, ovs_be64 *tun_idp, ovs_be64 *maskp)
160 uint64_t tun_id, mask;
164 tun_id = strtoull(str, &tail, 0);
165 if (errno || (*tail != '\0' && *tail != '/')) {
170 mask = strtoull(tail + 1, &tail, 0);
171 if (errno || *tail != '\0') {
178 *tun_idp = htonll(tun_id);
179 *maskp = htonll(mask);
183 ovs_fatal(0, "%s: bad syntax for tunnel id", str);
187 str_to_vlan_tci(const char *str, ovs_be16 *vlan_tcip, ovs_be16 *maskp)
189 uint16_t vlan_tci, mask;
193 vlan_tci = strtol(str, &tail, 0);
194 if (errno || (*tail != '\0' && *tail != '/')) {
199 mask = strtol(tail + 1, &tail, 0);
200 if (errno || *tail != '\0') {
207 *vlan_tcip = htons(vlan_tci);
208 *maskp = htons(mask);
212 ovs_fatal(0, "%s: bad syntax for vlan_tci", str);
216 str_to_ipv6(const char *str_, struct in6_addr *addrp, struct in6_addr *maskp)
218 char *str = xstrdup(str_);
219 char *save_ptr = NULL;
220 const char *name, *netmask;
221 struct in6_addr addr, mask;
224 name = strtok_r(str, "/", &save_ptr);
225 retval = name ? lookup_ipv6(name, &addr) : EINVAL;
227 ovs_fatal(0, "%s: could not convert to IPv6 address", str);
230 netmask = strtok_r(NULL, "/", &save_ptr);
232 int prefix = atoi(netmask);
233 if (prefix <= 0 || prefix > 128) {
234 ovs_fatal(0, "%s: network prefix bits not between 1 and 128",
237 mask = ipv6_create_mask(prefix);
240 mask = in6addr_exact;
242 *addrp = ipv6_addr_bitand(&addr, &mask);
247 if (!ipv6_mask_is_exact(&mask)) {
248 ovs_fatal(0, "%s: netmask not allowed here", str_);
256 put_action(struct ofpbuf *b, size_t size, uint16_t type)
258 struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
259 ah->type = htons(type);
260 ah->len = htons(size);
264 static struct ofp_action_output *
265 put_output_action(struct ofpbuf *b, uint16_t port)
267 struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
268 oao->port = htons(port);
273 put_enqueue_action(struct ofpbuf *b, uint16_t port, uint32_t queue)
275 struct ofp_action_enqueue *oae = put_action(b, sizeof *oae, OFPAT_ENQUEUE);
276 oae->port = htons(port);
277 oae->queue_id = htonl(queue);
281 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
283 struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
284 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 str_to_action(char *str, struct ofpbuf *b)
329 char empty_string[] = "";
334 pos += strspn(pos, ", \t\r\n");
340 ovs_fatal(0, "Drop actions must not be followed by other actions");
344 actlen = strcspn(pos, ":(, \t\r\n");
345 if (act[actlen] == ':') {
346 /* The argument can be separated by a colon. */
349 arg = act + actlen + 1;
350 arglen = strcspn(arg, ", \t\r\n");
351 pos = arg + arglen + (arg[arglen] != '\0');
353 } else if (act[actlen] == '(') {
354 /* The argument can be surrounded by balanced parentheses. The
355 * outermost set of parentheses is removed. */
359 arg = act + actlen + 1;
360 for (arglen = 0; level > 0; arglen++) {
361 switch (arg[arglen]) {
363 ovs_fatal(0, "unbalanced parentheses in argument to %s "
375 arg[arglen - 1] = '\0';
378 /* There might be no argument at all. */
380 pos = act + actlen + (act[actlen] != '\0');
384 if (!strcasecmp(act, "mod_vlan_vid")) {
385 struct ofp_action_vlan_vid *va;
386 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
387 va->vlan_vid = htons(str_to_u32(arg));
388 } else if (!strcasecmp(act, "mod_vlan_pcp")) {
389 struct ofp_action_vlan_pcp *va;
390 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
391 va->vlan_pcp = str_to_u32(arg);
392 } else if (!strcasecmp(act, "strip_vlan")) {
393 struct ofp_action_header *ah;
394 ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
395 ah->type = htons(OFPAT_STRIP_VLAN);
396 } else if (!strcasecmp(act, "mod_dl_src")) {
397 put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
398 } else if (!strcasecmp(act, "mod_dl_dst")) {
399 put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
400 } else if (!strcasecmp(act, "mod_nw_src")) {
401 struct ofp_action_nw_addr *na;
402 na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
403 str_to_ip(arg, &na->nw_addr, NULL);
404 } else if (!strcasecmp(act, "mod_nw_dst")) {
405 struct ofp_action_nw_addr *na;
406 na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
407 str_to_ip(arg, &na->nw_addr, NULL);
408 } else if (!strcasecmp(act, "mod_tp_src")) {
409 struct ofp_action_tp_port *ta;
410 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
411 ta->tp_port = htons(str_to_u32(arg));
412 } else if (!strcasecmp(act, "mod_tp_dst")) {
413 struct ofp_action_tp_port *ta;
414 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
415 ta->tp_port = htons(str_to_u32(arg));
416 } else if (!strcasecmp(act, "mod_nw_tos")) {
417 struct ofp_action_nw_tos *nt;
418 nt = put_action(b, sizeof *nt, OFPAT_SET_NW_TOS);
419 nt->nw_tos = str_to_u32(arg);
420 } else if (!strcasecmp(act, "resubmit")) {
421 struct nx_action_resubmit *nar;
422 nar = put_action(b, sizeof *nar, OFPAT_VENDOR);
423 nar->vendor = htonl(NX_VENDOR_ID);
424 nar->subtype = htons(NXAST_RESUBMIT);
425 nar->in_port = htons(str_to_u32(arg));
426 } else if (!strcasecmp(act, "set_tunnel")
427 || !strcasecmp(act, "set_tunnel64")) {
428 uint64_t tun_id = str_to_u64(arg);
429 if (!strcasecmp(act, "set_tunnel64") || tun_id > UINT32_MAX) {
430 struct nx_action_set_tunnel64 *nast64;
431 nast64 = put_action(b, sizeof *nast64, OFPAT_VENDOR);
432 nast64->vendor = htonl(NX_VENDOR_ID);
433 nast64->subtype = htons(NXAST_SET_TUNNEL64);
434 nast64->tun_id = htonll(tun_id);
436 struct nx_action_set_tunnel *nast;
437 nast = put_action(b, sizeof *nast, OFPAT_VENDOR);
438 nast->vendor = htonl(NX_VENDOR_ID);
439 nast->subtype = htons(NXAST_SET_TUNNEL);
440 nast->tun_id = htonl(tun_id);
442 } else if (!strcasecmp(act, "set_queue")) {
443 struct nx_action_set_queue *nasq;
444 nasq = put_action(b, sizeof *nasq, OFPAT_VENDOR);
445 nasq->vendor = htonl(NX_VENDOR_ID);
446 nasq->subtype = htons(NXAST_SET_QUEUE);
447 nasq->queue_id = htonl(str_to_u32(arg));
448 } else if (!strcasecmp(act, "pop_queue")) {
449 struct nx_action_header *nah;
450 nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
451 nah->vendor = htonl(NX_VENDOR_ID);
452 nah->subtype = htons(NXAST_POP_QUEUE);
453 } else if (!strcasecmp(act, "note")) {
454 size_t start_ofs = b->size;
455 struct nx_action_note *nan;
459 nan = put_action(b, sizeof *nan, OFPAT_VENDOR);
460 nan->vendor = htonl(NX_VENDOR_ID);
461 nan->subtype = htons(NXAST_NOTE);
463 b->size -= sizeof nan->note;
464 while (*arg != '\0') {
475 byte = hexits_value(arg, 2, &ok);
477 ovs_fatal(0, "bad hex digit in `note' argument");
479 ofpbuf_put(b, &byte, 1);
484 len = b->size - start_ofs;
485 remainder = len % OFP_ACTION_ALIGN;
487 ofpbuf_put_zeros(b, OFP_ACTION_ALIGN - remainder);
489 nan = (struct nx_action_note *)((char *)b->data + start_ofs);
490 nan->len = htons(b->size - start_ofs);
491 } else if (!strcasecmp(act, "move")) {
492 struct nx_action_reg_move *move;
493 move = ofpbuf_put_uninit(b, sizeof *move);
494 nxm_parse_reg_move(move, arg);
495 } else if (!strcasecmp(act, "load")) {
496 struct nx_action_reg_load *load;
497 load = ofpbuf_put_uninit(b, sizeof *load);
498 nxm_parse_reg_load(load, arg);
499 } else if (!strcasecmp(act, "multipath")) {
500 struct nx_action_multipath *nam;
501 nam = ofpbuf_put_uninit(b, sizeof *nam);
502 multipath_parse(nam, arg);
503 } else if (!strcasecmp(act, "autopath")) {
504 struct nx_action_autopath *naa;
505 naa = ofpbuf_put_uninit(b, sizeof *naa);
506 autopath_parse(naa, arg);
507 } else if (!strcasecmp(act, "output")) {
508 put_output_action(b, str_to_u32(arg));
509 } else if (!strcasecmp(act, "enqueue")) {
511 char *port_s = strtok_r(arg, ":q", &sp);
512 char *queue = strtok_r(NULL, "", &sp);
513 if (port_s == NULL || queue == NULL) {
514 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
516 put_enqueue_action(b, str_to_u32(port_s), str_to_u32(queue));
517 } else if (!strcasecmp(act, "drop")) {
518 /* A drop action in OpenFlow occurs by just not setting
522 ovs_fatal(0, "Drop actions must not be preceded by other "
525 } else if (!strcasecmp(act, "CONTROLLER")) {
526 struct ofp_action_output *oao;
527 oao = put_output_action(b, OFPP_CONTROLLER);
529 /* Unless a numeric argument is specified, we send the whole
530 * packet to the controller. */
531 if (arg[0] && (strspn(arg, "0123456789") == strlen(arg))) {
532 oao->max_len = htons(str_to_u32(arg));
534 oao->max_len = htons(UINT16_MAX);
536 } else if (parse_port_name(act, &port)) {
537 put_output_action(b, port);
538 } else if (strspn(act, "0123456789") == strlen(act)) {
539 put_output_action(b, str_to_u32(act));
541 ovs_fatal(0, "Unknown action: %s", act);
554 parse_protocol(const char *name, const struct protocol **p_out)
556 static const struct protocol protocols[] = {
557 { "ip", ETH_TYPE_IP, 0 },
558 { "arp", ETH_TYPE_ARP, 0 },
559 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
560 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
561 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
562 { "ipv6", ETH_TYPE_IPV6, 0 },
563 { "ip6", ETH_TYPE_IPV6, 0 },
564 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
565 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
566 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
568 const struct protocol *p;
570 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
571 if (!strcmp(p->name, name)) {
581 FIELD(F_TUN_ID, "tun_id", 0) \
582 FIELD(F_IN_PORT, "in_port", FWW_IN_PORT) \
583 FIELD(F_DL_VLAN, "dl_vlan", 0) \
584 FIELD(F_DL_VLAN_PCP, "dl_vlan_pcp", 0) \
585 FIELD(F_VLAN_TCI, "vlan_tci", 0) \
586 FIELD(F_DL_SRC, "dl_src", FWW_DL_SRC) \
587 FIELD(F_DL_DST, "dl_dst", FWW_DL_DST | FWW_ETH_MCAST) \
588 FIELD(F_DL_TYPE, "dl_type", FWW_DL_TYPE) \
589 FIELD(F_NW_SRC, "nw_src", 0) \
590 FIELD(F_NW_DST, "nw_dst", 0) \
591 FIELD(F_NW_PROTO, "nw_proto", FWW_NW_PROTO) \
592 FIELD(F_NW_TOS, "nw_tos", FWW_NW_TOS) \
593 FIELD(F_TP_SRC, "tp_src", FWW_TP_SRC) \
594 FIELD(F_TP_DST, "tp_dst", FWW_TP_DST) \
595 FIELD(F_ICMP_TYPE, "icmp_type", FWW_TP_SRC) \
596 FIELD(F_ICMP_CODE, "icmp_code", FWW_TP_DST) \
597 FIELD(F_ARP_SHA, "arp_sha", FWW_ARP_SHA) \
598 FIELD(F_ARP_THA, "arp_tha", FWW_ARP_THA) \
599 FIELD(F_IPV6_SRC, "ipv6_src", 0) \
600 FIELD(F_IPV6_DST, "ipv6_dst", 0) \
601 FIELD(F_ND_TARGET, "nd_target", FWW_ND_TARGET) \
602 FIELD(F_ND_SLL, "nd_sll", FWW_ARP_SHA) \
603 FIELD(F_ND_TLL, "nd_tll", FWW_ARP_THA)
606 #define FIELD(ENUM, NAME, WILDCARD) ENUM,
613 enum field_index index;
615 flow_wildcards_t wildcard; /* FWW_* bit. */
619 ofp_fatal(const char *flow, bool verbose, const char *format, ...)
624 fprintf(stderr, "%s:\n", flow);
627 va_start(args, format);
628 ovs_fatal_valist(0, format, args);
632 parse_field_name(const char *name, const struct field **f_out)
634 static const struct field fields[N_FIELDS] = {
635 #define FIELD(ENUM, NAME, WILDCARD) { ENUM, NAME, WILDCARD },
639 const struct field *f;
641 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
642 if (!strcmp(f->name, name)) {
652 parse_field_value(struct cls_rule *rule, enum field_index index,
655 uint8_t mac[ETH_ADDR_LEN], mac_mask[ETH_ADDR_LEN];
656 ovs_be64 tun_id, tun_mask;
658 ovs_be16 tci, tci_mask;
659 struct in6_addr ipv6, ipv6_mask;
664 str_to_tun_id(value, &tun_id, &tun_mask);
665 cls_rule_set_tun_id_masked(rule, tun_id, tun_mask);
669 if (!parse_port_name(value, &port_no)) {
670 port_no = atoi(value);
672 cls_rule_set_in_port(rule, port_no);
676 cls_rule_set_dl_vlan(rule, htons(str_to_u32(value)));
680 cls_rule_set_dl_vlan_pcp(rule, str_to_u32(value));
684 str_to_vlan_tci(value, &tci, &tci_mask);
685 cls_rule_set_dl_tci_masked(rule, tci, tci_mask);
689 str_to_mac(value, mac);
690 cls_rule_set_dl_src(rule, mac);
694 str_to_eth_dst(value, mac, mac_mask);
695 cls_rule_set_dl_dst_masked(rule, mac, mac_mask);
699 cls_rule_set_dl_type(rule, htons(str_to_u32(value)));
703 str_to_ip(value, &ip, &mask);
704 cls_rule_set_nw_src_masked(rule, ip, mask);
708 str_to_ip(value, &ip, &mask);
709 cls_rule_set_nw_dst_masked(rule, ip, mask);
713 cls_rule_set_nw_proto(rule, str_to_u32(value));
717 cls_rule_set_nw_tos(rule, str_to_u32(value));
721 cls_rule_set_tp_src(rule, htons(str_to_u32(value)));
725 cls_rule_set_tp_dst(rule, htons(str_to_u32(value)));
729 cls_rule_set_icmp_type(rule, str_to_u32(value));
733 cls_rule_set_icmp_code(rule, str_to_u32(value));
737 str_to_mac(value, mac);
738 cls_rule_set_arp_sha(rule, mac);
742 str_to_mac(value, mac);
743 cls_rule_set_arp_tha(rule, mac);
747 str_to_ipv6(value, &ipv6, &ipv6_mask);
748 cls_rule_set_ipv6_src_masked(rule, &ipv6, &ipv6_mask);
752 str_to_ipv6(value, &ipv6, &ipv6_mask);
753 cls_rule_set_ipv6_dst_masked(rule, &ipv6, &ipv6_mask);
757 str_to_ipv6(value, &ipv6, NULL);
758 cls_rule_set_nd_target(rule, ipv6);
762 str_to_mac(value, mac);
763 cls_rule_set_arp_sha(rule, mac);
767 str_to_mac(value, mac);
768 cls_rule_set_arp_tha(rule, mac);
777 parse_reg_value(struct cls_rule *rule, int reg_idx, const char *value)
779 uint32_t reg_value, reg_mask;
781 if (!strcmp(value, "ANY") || !strcmp(value, "*")) {
782 cls_rule_set_reg_masked(rule, reg_idx, 0, 0);
783 } else if (sscanf(value, "%"SCNi32"/%"SCNi32,
784 ®_value, ®_mask) == 2) {
785 cls_rule_set_reg_masked(rule, reg_idx, reg_value, reg_mask);
786 } else if (sscanf(value, "%"SCNi32, ®_value)) {
787 cls_rule_set_reg(rule, reg_idx, reg_value);
789 ovs_fatal(0, "register fields must take the form <value> "
790 "or <value>/<mask>");
794 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
795 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
796 * If 'actions' is specified, an action must be in 'string' and may be expanded
799 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
800 * constant for 'command'. To parse syntax for an OFPST_FLOW or
801 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
803 parse_ofp_str(struct flow_mod *fm, int command, const char *str_, bool verbose)
812 char *string = xstrdup(str_);
813 char *save_ptr = NULL;
822 fields = F_ACTIONS | F_COOKIE | F_TIMEOUT | F_PRIORITY;
829 case OFPFC_DELETE_STRICT:
830 fields = F_OUT_PORT | F_PRIORITY;
834 fields = F_ACTIONS | F_COOKIE;
837 case OFPFC_MODIFY_STRICT:
838 fields = F_ACTIONS | F_COOKIE | F_PRIORITY;
845 cls_rule_init_catchall(&fm->cr, OFP_DEFAULT_PRIORITY);
846 fm->cookie = htonll(0);
848 fm->command = command;
849 fm->idle_timeout = OFP_FLOW_PERMANENT;
850 fm->hard_timeout = OFP_FLOW_PERMANENT;
851 fm->buffer_id = UINT32_MAX;
852 fm->out_port = OFPP_NONE;
854 if (fields & F_ACTIONS) {
855 struct ofpbuf actions;
858 act_str = strstr(string, "action");
860 ofp_fatal(str_, verbose, "must specify an action");
864 act_str = strchr(act_str + 1, '=');
866 ofp_fatal(str_, verbose, "must specify an action");
871 ofpbuf_init(&actions, sizeof(union ofp_action));
872 str_to_action(act_str, &actions);
873 fm->actions = ofpbuf_steal_data(&actions);
874 fm->n_actions = actions.size / sizeof(union ofp_action);
879 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
880 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
881 const struct protocol *p;
883 if (parse_protocol(name, &p)) {
884 cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
886 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
889 const struct field *f;
892 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
894 ofp_fatal(str_, verbose, "field %s missing value", name);
897 if (!strcmp(name, "table")) {
898 fm->table_id = atoi(value);
899 } else if (!strcmp(name, "out_port")) {
900 fm->out_port = atoi(value);
901 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
902 fm->cr.priority = atoi(value);
903 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
904 fm->idle_timeout = atoi(value);
905 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
906 fm->hard_timeout = atoi(value);
907 } else if (fields & F_COOKIE && !strcmp(name, "cookie")) {
908 fm->cookie = htonll(str_to_u64(value));
909 } else if (parse_field_name(name, &f)) {
910 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
912 fm->cr.wc.wildcards |= f->wildcard;
913 cls_rule_zero_wildcarded_fields(&fm->cr);
914 } else if (f->index == F_NW_SRC) {
915 cls_rule_set_nw_src_masked(&fm->cr, 0, 0);
916 } else if (f->index == F_NW_DST) {
917 cls_rule_set_nw_dst_masked(&fm->cr, 0, 0);
918 } else if (f->index == F_IPV6_SRC) {
919 cls_rule_set_ipv6_src_masked(&fm->cr,
920 &in6addr_any, &in6addr_any);
921 } else if (f->index == F_IPV6_DST) {
922 cls_rule_set_ipv6_dst_masked(&fm->cr,
923 &in6addr_any, &in6addr_any);
924 } else if (f->index == F_DL_VLAN) {
925 cls_rule_set_any_vid(&fm->cr);
926 } else if (f->index == F_DL_VLAN_PCP) {
927 cls_rule_set_any_pcp(&fm->cr);
932 parse_field_value(&fm->cr, f->index, value);
934 } else if (!strncmp(name, "reg", 3)
935 && isdigit((unsigned char) name[3])) {
936 unsigned int reg_idx = atoi(name + 3);
937 if (reg_idx >= FLOW_N_REGS) {
939 fprintf(stderr, "%s:\n", str_);
941 ofp_fatal(str_, verbose, "only %d registers supported", FLOW_N_REGS);
943 parse_reg_value(&fm->cr, reg_idx, value);
944 } else if (!strcmp(name, "duration")
945 || !strcmp(name, "n_packets")
946 || !strcmp(name, "n_bytes")) {
947 /* Ignore these, so that users can feed the output of
948 * "ovs-ofctl dump-flows" back into commands that parse
951 ofp_fatal(str_, verbose, "unknown keyword %s", name);
959 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
960 * (one of OFPFC_*) and appends the parsed OpenFlow message to 'packets'.
961 * '*cur_format' should initially contain the flow format currently configured
962 * on the connection; this function will add a message to change the flow
963 * format and update '*cur_format', if this is necessary to add the parsed
966 parse_ofp_flow_mod_str(struct list *packets, enum nx_flow_format *cur_format,
967 bool *flow_mod_table_id, char *string, uint16_t command,
970 enum nx_flow_format min_format, next_format;
971 struct cls_rule rule_copy;
972 struct ofpbuf actions;
976 ofpbuf_init(&actions, 64);
977 parse_ofp_str(&fm, command, string, verbose);
979 min_format = ofputil_min_flow_format(&fm.cr);
980 next_format = MAX(*cur_format, min_format);
981 if (next_format != *cur_format) {
982 struct ofpbuf *sff = ofputil_make_set_flow_format(next_format);
983 list_push_back(packets, &sff->list_node);
984 *cur_format = next_format;
987 /* Normalize a copy of the rule. This ensures that non-normalized flows
988 * get logged but doesn't affect what gets sent to the switch, so that the
989 * switch can do whatever it likes with the flow. */
991 ofputil_normalize_rule(&rule_copy, next_format);
993 if (fm.table_id != 0xff && !*flow_mod_table_id) {
994 struct ofpbuf *sff = ofputil_make_flow_mod_table_id(true);
995 list_push_back(packets, &sff->list_node);
996 *flow_mod_table_id = true;
999 ofm = ofputil_encode_flow_mod(&fm, *cur_format, *flow_mod_table_id);
1000 list_push_back(packets, &ofm->list_node);
1002 ofpbuf_uninit(&actions);
1005 /* Similar to parse_ofp_flow_mod_str(), except that the string is read from
1006 * 'stream' and the command is always OFPFC_ADD. Returns false if end-of-file
1007 * is reached before reading a flow, otherwise true. */
1009 parse_ofp_flow_mod_file(struct list *packets,
1010 enum nx_flow_format *cur, bool *flow_mod_table_id,
1011 FILE *stream, uint16_t command)
1017 ok = ds_get_preprocessed_line(&s, stream) == 0;
1019 parse_ofp_flow_mod_str(packets, cur, flow_mod_table_id,
1020 ds_cstr(&s), command, true);
1028 parse_ofp_flow_stats_request_str(struct flow_stats_request *fsr,
1029 bool aggregate, char *string)
1033 parse_ofp_str(&fm, -1, string, false);
1034 fsr->aggregate = aggregate;
1036 fsr->out_port = fm.out_port;
1037 fsr->table_id = fm.table_id;