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"
29 #include "meta-flow.h"
31 #include "multipath.h"
35 #include "openflow/openflow.h"
37 #include "socket-util.h"
41 VLOG_DEFINE_THIS_MODULE(ofp_parse);
44 str_to_u32(const char *str)
50 ovs_fatal(0, "missing required numeric argument");
54 value = strtoul(str, &tail, 0);
55 if (errno == EINVAL || errno == ERANGE || *tail) {
56 ovs_fatal(0, "invalid numeric format %s", str);
62 str_to_u64(const char *str)
68 ovs_fatal(0, "missing required numeric argument");
72 value = strtoull(str, &tail, 0);
73 if (errno == EINVAL || errno == ERANGE || *tail) {
74 ovs_fatal(0, "invalid numeric format %s", str);
80 str_to_mac(const char *str, uint8_t mac[6])
82 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
83 != ETH_ADDR_SCAN_COUNT) {
84 ovs_fatal(0, "invalid mac address %s", str);
89 str_to_ip(const char *str, ovs_be32 *ip)
91 struct in_addr in_addr;
93 if (lookup_ip(str, &in_addr)) {
94 ovs_fatal(0, "%s: could not convert to IP address", str);
99 static struct ofp_action_output *
100 put_output_action(struct ofpbuf *b, uint16_t port)
102 struct ofp_action_output *oao;
104 oao = ofputil_put_OFPAT_OUTPUT(b);
105 oao->port = htons(port);
110 parse_enqueue(struct ofpbuf *b, char *arg)
113 char *port = strtok_r(arg, ":q", &sp);
114 char *queue = strtok_r(NULL, "", &sp);
115 struct ofp_action_enqueue *oae;
117 if (port == NULL || queue == NULL) {
118 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
121 oae = ofputil_put_OFPAT_ENQUEUE(b);
122 oae->port = htons(str_to_u32(port));
123 oae->queue_id = htonl(str_to_u32(queue));
127 parse_output(struct ofpbuf *b, char *arg)
129 if (strchr(arg, '[')) {
130 struct nx_action_output_reg *naor;
134 nxm_parse_field_bits(arg, &src, &ofs, &n_bits);
136 naor = ofputil_put_NXAST_OUTPUT_REG(b);
137 naor->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
138 naor->src = htonl(src);
139 naor->max_len = htons(UINT16_MAX);
141 put_output_action(b, str_to_u32(arg));
146 parse_resubmit(struct ofpbuf *b, char *arg)
148 struct nx_action_resubmit *nar;
149 char *in_port_s, *table_s;
153 in_port_s = strsep(&arg, ",");
154 if (in_port_s && in_port_s[0]) {
155 if (!ofputil_port_from_string(in_port_s, &in_port)) {
156 in_port = str_to_u32(in_port_s);
159 in_port = OFPP_IN_PORT;
162 table_s = strsep(&arg, ",");
163 table = table_s && table_s[0] ? str_to_u32(table_s) : 255;
165 if (in_port == OFPP_IN_PORT && table == 255) {
166 ovs_fatal(0, "at least one \"in_port\" or \"table\" must be specified "
170 if (in_port != OFPP_IN_PORT && table == 255) {
171 nar = ofputil_put_NXAST_RESUBMIT(b);
173 nar = ofputil_put_NXAST_RESUBMIT_TABLE(b);
176 nar->in_port = htons(in_port);
180 parse_set_tunnel(struct ofpbuf *b, const char *arg)
182 uint64_t tun_id = str_to_u64(arg);
183 if (tun_id > UINT32_MAX) {
184 ofputil_put_NXAST_SET_TUNNEL64(b)->tun_id = htonll(tun_id);
186 ofputil_put_NXAST_SET_TUNNEL(b)->tun_id = htonl(tun_id);
191 parse_note(struct ofpbuf *b, const char *arg)
193 size_t start_ofs = b->size;
194 struct nx_action_note *nan;
198 nan = ofputil_put_NXAST_NOTE(b);
200 b->size -= sizeof nan->note;
201 while (*arg != '\0') {
212 byte = hexits_value(arg, 2, &ok);
214 ovs_fatal(0, "bad hex digit in `note' argument");
216 ofpbuf_put(b, &byte, 1);
221 len = b->size - start_ofs;
222 remainder = len % OFP_ACTION_ALIGN;
224 ofpbuf_put_zeros(b, OFP_ACTION_ALIGN - remainder);
226 nan = (struct nx_action_note *)((char *)b->data + start_ofs);
227 nan->len = htons(b->size - start_ofs);
231 parse_named_action(enum ofputil_action_code code, struct ofpbuf *b, char *arg)
233 struct ofp_action_dl_addr *oada;
234 struct ofp_action_vlan_pcp *oavp;
235 struct ofp_action_vlan_vid *oavv;
236 struct ofp_action_nw_addr *oana;
237 struct ofp_action_tp_port *oata;
240 case OFPUTIL_OFPAT_OUTPUT:
241 parse_output(b, arg);
244 case OFPUTIL_OFPAT_SET_VLAN_VID:
245 oavv = ofputil_put_OFPAT_SET_VLAN_VID(b);
246 oavv->vlan_vid = htons(str_to_u32(arg));
249 case OFPUTIL_OFPAT_SET_VLAN_PCP:
250 oavp = ofputil_put_OFPAT_SET_VLAN_PCP(b);
251 oavp->vlan_pcp = str_to_u32(arg);
254 case OFPUTIL_OFPAT_STRIP_VLAN:
255 ofputil_put_OFPAT_STRIP_VLAN(b);
258 case OFPUTIL_OFPAT_SET_DL_SRC:
259 case OFPUTIL_OFPAT_SET_DL_DST:
260 oada = ofputil_put_action(code, b);
261 str_to_mac(arg, oada->dl_addr);
264 case OFPUTIL_OFPAT_SET_NW_SRC:
265 case OFPUTIL_OFPAT_SET_NW_DST:
266 oana = ofputil_put_action(code, b);
267 str_to_ip(arg, &oana->nw_addr);
270 case OFPUTIL_OFPAT_SET_NW_TOS:
271 ofputil_put_OFPAT_SET_NW_TOS(b)->nw_tos = str_to_u32(arg);
274 case OFPUTIL_OFPAT_SET_TP_SRC:
275 case OFPUTIL_OFPAT_SET_TP_DST:
276 oata = ofputil_put_action(code, b);
277 oata->tp_port = htons(str_to_u32(arg));
280 case OFPUTIL_OFPAT_ENQUEUE:
281 parse_enqueue(b, arg);
284 case OFPUTIL_NXAST_RESUBMIT:
285 parse_resubmit(b, arg);
288 case OFPUTIL_NXAST_SET_TUNNEL:
289 parse_set_tunnel(b, arg);
292 case OFPUTIL_NXAST_SET_QUEUE:
293 ofputil_put_NXAST_SET_QUEUE(b)->queue_id = htonl(str_to_u32(arg));
296 case OFPUTIL_NXAST_POP_QUEUE:
297 ofputil_put_NXAST_POP_QUEUE(b);
300 case OFPUTIL_NXAST_REG_MOVE:
301 nxm_parse_reg_move(ofputil_put_NXAST_REG_MOVE(b), arg);
304 case OFPUTIL_NXAST_REG_LOAD:
305 nxm_parse_reg_load(ofputil_put_NXAST_REG_LOAD(b), arg);
308 case OFPUTIL_NXAST_NOTE:
312 case OFPUTIL_NXAST_SET_TUNNEL64:
313 ofputil_put_NXAST_SET_TUNNEL64(b)->tun_id = htonll(str_to_u64(arg));
316 case OFPUTIL_NXAST_MULTIPATH:
317 multipath_parse(ofputil_put_NXAST_MULTIPATH(b), arg);
320 case OFPUTIL_NXAST_AUTOPATH:
321 autopath_parse(ofputil_put_NXAST_AUTOPATH(b), arg);
324 case OFPUTIL_NXAST_BUNDLE:
325 bundle_parse(b, arg);
328 case OFPUTIL_NXAST_BUNDLE_LOAD:
329 bundle_parse_load(b, arg);
332 case OFPUTIL_NXAST_RESUBMIT_TABLE:
333 case OFPUTIL_NXAST_OUTPUT_REG:
339 str_to_action(char *str, struct ofpbuf *b)
341 char *pos, *act, *arg;
346 while (ofputil_parse_key_value(&pos, &act, &arg)) {
350 code = ofputil_action_code_from_name(act);
352 parse_named_action(code, b, arg);
353 } else if (!strcasecmp(act, "drop")) {
354 /* A drop action in OpenFlow occurs by just not setting
357 ovs_fatal(0, "Drop actions must not be preceded by other "
359 } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
360 ovs_fatal(0, "Drop actions must not be followed by other "
364 } else if (!strcasecmp(act, "CONTROLLER")) {
365 struct ofp_action_output *oao;
366 oao = put_output_action(b, OFPP_CONTROLLER);
368 /* Unless a numeric argument is specified, we send the whole
369 * packet to the controller. */
370 if (arg[0] && (strspn(arg, "0123456789") == strlen(arg))) {
371 oao->max_len = htons(str_to_u32(arg));
373 oao->max_len = htons(UINT16_MAX);
375 } else if (ofputil_port_from_string(act, &port)) {
376 put_output_action(b, port);
378 ovs_fatal(0, "Unknown action: %s", act);
391 parse_protocol(const char *name, const struct protocol **p_out)
393 static const struct protocol protocols[] = {
394 { "ip", ETH_TYPE_IP, 0 },
395 { "arp", ETH_TYPE_ARP, 0 },
396 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
397 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
398 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
399 { "ipv6", ETH_TYPE_IPV6, 0 },
400 { "ip6", ETH_TYPE_IPV6, 0 },
401 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
402 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
403 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
405 const struct protocol *p;
407 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
408 if (!strcmp(p->name, name)) {
418 ofp_fatal(const char *flow, bool verbose, const char *format, ...)
423 fprintf(stderr, "%s:\n", flow);
426 va_start(args, format);
427 ovs_fatal_valist(0, format, args);
431 parse_field(const struct mf_field *mf, const char *s, struct cls_rule *rule)
433 union mf_value value, mask;
436 error = mf_parse(mf, s, &value, &mask);
438 ovs_fatal(0, "%s", error);
441 mf_set(mf, &value, &mask, rule);
444 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
445 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
446 * If 'actions' is specified, an action must be in 'string' and may be expanded
449 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
450 * constant for 'command'. To parse syntax for an OFPST_FLOW or
451 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
453 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
463 char *string = xstrdup(str_);
464 char *save_ptr = NULL;
473 fields = F_ACTIONS | F_COOKIE | F_TIMEOUT | F_PRIORITY;
480 case OFPFC_DELETE_STRICT:
481 fields = F_OUT_PORT | F_PRIORITY;
485 fields = F_ACTIONS | F_COOKIE;
488 case OFPFC_MODIFY_STRICT:
489 fields = F_ACTIONS | F_COOKIE | F_PRIORITY;
496 cls_rule_init_catchall(&fm->cr, OFP_DEFAULT_PRIORITY);
497 fm->cookie = htonll(0);
499 fm->command = command;
500 fm->idle_timeout = OFP_FLOW_PERMANENT;
501 fm->hard_timeout = OFP_FLOW_PERMANENT;
502 fm->buffer_id = UINT32_MAX;
503 fm->out_port = OFPP_NONE;
505 if (fields & F_ACTIONS) {
506 struct ofpbuf actions;
509 act_str = strstr(string, "action");
511 ofp_fatal(str_, verbose, "must specify an action");
515 act_str = strchr(act_str + 1, '=');
517 ofp_fatal(str_, verbose, "must specify an action");
522 ofpbuf_init(&actions, sizeof(union ofp_action));
523 str_to_action(act_str, &actions);
524 fm->actions = ofpbuf_steal_data(&actions);
525 fm->n_actions = actions.size / sizeof(union ofp_action);
530 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
531 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
532 const struct protocol *p;
534 if (parse_protocol(name, &p)) {
535 cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
537 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
542 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
544 ofp_fatal(str_, verbose, "field %s missing value", name);
547 if (!strcmp(name, "table")) {
548 fm->table_id = atoi(value);
549 } else if (!strcmp(name, "out_port")) {
550 fm->out_port = atoi(value);
551 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
552 fm->cr.priority = atoi(value);
553 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
554 fm->idle_timeout = atoi(value);
555 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
556 fm->hard_timeout = atoi(value);
557 } else if (fields & F_COOKIE && !strcmp(name, "cookie")) {
558 fm->cookie = htonll(str_to_u64(value));
559 } else if (mf_from_name(name)) {
560 parse_field(mf_from_name(name), value, &fm->cr);
561 } else if (!strcmp(name, "duration")
562 || !strcmp(name, "n_packets")
563 || !strcmp(name, "n_bytes")) {
564 /* Ignore these, so that users can feed the output of
565 * "ovs-ofctl dump-flows" back into commands that parse
568 ofp_fatal(str_, verbose, "unknown keyword %s", name);
576 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
577 * (one of OFPFC_*) and appends the parsed OpenFlow message to 'packets'.
578 * '*cur_format' should initially contain the flow format currently configured
579 * on the connection; this function will add a message to change the flow
580 * format and update '*cur_format', if this is necessary to add the parsed
583 parse_ofp_flow_mod_str(struct list *packets, enum nx_flow_format *cur_format,
584 bool *flow_mod_table_id, char *string, uint16_t command,
587 enum nx_flow_format min_format, next_format;
588 struct cls_rule rule_copy;
589 struct ofpbuf actions;
591 struct ofputil_flow_mod fm;
593 ofpbuf_init(&actions, 64);
594 parse_ofp_str(&fm, command, string, verbose);
596 min_format = ofputil_min_flow_format(&fm.cr);
597 next_format = MAX(*cur_format, min_format);
598 if (next_format != *cur_format) {
599 struct ofpbuf *sff = ofputil_make_set_flow_format(next_format);
600 list_push_back(packets, &sff->list_node);
601 *cur_format = next_format;
604 /* Normalize a copy of the rule. This ensures that non-normalized flows
605 * get logged but doesn't affect what gets sent to the switch, so that the
606 * switch can do whatever it likes with the flow. */
608 ofputil_normalize_rule(&rule_copy, next_format);
610 if (fm.table_id != 0xff && !*flow_mod_table_id) {
611 struct ofpbuf *sff = ofputil_make_flow_mod_table_id(true);
612 list_push_back(packets, &sff->list_node);
613 *flow_mod_table_id = true;
616 ofm = ofputil_encode_flow_mod(&fm, *cur_format, *flow_mod_table_id);
617 list_push_back(packets, &ofm->list_node);
619 ofpbuf_uninit(&actions);
622 /* Similar to parse_ofp_flow_mod_str(), except that the string is read from
623 * 'stream' and the command is always OFPFC_ADD. Returns false if end-of-file
624 * is reached before reading a flow, otherwise true. */
626 parse_ofp_flow_mod_file(struct list *packets,
627 enum nx_flow_format *cur, bool *flow_mod_table_id,
628 FILE *stream, uint16_t command)
634 ok = ds_get_preprocessed_line(&s, stream) == 0;
636 parse_ofp_flow_mod_str(packets, cur, flow_mod_table_id,
637 ds_cstr(&s), command, true);
645 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
646 bool aggregate, char *string)
648 struct ofputil_flow_mod fm;
650 parse_ofp_str(&fm, -1, string, false);
651 fsr->aggregate = aggregate;
653 fsr->out_port = fm.out_port;
654 fsr->table_id = fm.table_id;