2 * Copyright (c) 2010 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"
30 #include "openflow/openflow.h"
32 #include "socket-util.h"
36 VLOG_DEFINE_THIS_MODULE(ofp_parse);
39 str_to_u32(const char *str)
45 ovs_fatal(0, "missing required numeric argument");
49 value = strtoul(str, &tail, 0);
50 if (errno == EINVAL || errno == ERANGE || *tail) {
51 ovs_fatal(0, "invalid numeric format %s", str);
57 str_to_u64(const char *str)
63 value = strtoull(str, &tail, 0);
64 if (errno == EINVAL || errno == ERANGE || *tail) {
65 ovs_fatal(0, "invalid numeric format %s", str);
71 str_to_mac(const char *str, uint8_t mac[6])
73 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
74 != ETH_ADDR_SCAN_COUNT) {
75 ovs_fatal(0, "invalid mac address %s", str);
80 str_to_ip(const char *str_, ovs_be32 *ip, ovs_be32 *maskp)
82 char *str = xstrdup(str_);
83 char *save_ptr = NULL;
84 const char *name, *netmask;
85 struct in_addr in_addr;
89 name = strtok_r(str, "/", &save_ptr);
90 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
92 ovs_fatal(0, "%s: could not convert to IP address", str);
96 netmask = strtok_r(NULL, "/", &save_ptr);
99 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
100 &o[0], &o[1], &o[2], &o[3]) == 4) {
101 mask = htonl((o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3]);
103 int prefix = atoi(netmask);
104 if (prefix <= 0 || prefix > 32) {
105 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
107 } else if (prefix == 32) {
108 mask = htonl(UINT32_MAX);
110 mask = htonl(((1u << prefix) - 1) << (32 - prefix));
114 mask = htonl(UINT32_MAX);
121 if (mask != htonl(UINT32_MAX)) {
122 ovs_fatal(0, "%s: netmask not allowed here", str_);
130 put_action(struct ofpbuf *b, size_t size, uint16_t type)
132 struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
133 ah->type = htons(type);
134 ah->len = htons(size);
138 static struct ofp_action_output *
139 put_output_action(struct ofpbuf *b, uint16_t port)
141 struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
142 oao->port = htons(port);
147 put_enqueue_action(struct ofpbuf *b, uint16_t port, uint32_t queue)
149 struct ofp_action_enqueue *oae = put_action(b, sizeof *oae, OFPAT_ENQUEUE);
150 oae->port = htons(port);
151 oae->queue_id = htonl(queue);
155 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
157 struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
158 str_to_mac(addr, oada->dl_addr);
163 parse_port_name(const char *name, uint16_t *port)
169 static const struct pair pairs[] = {
170 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
176 DEF_PAIR(CONTROLLER),
181 static const int n_pairs = ARRAY_SIZE(pairs);
184 for (i = 0; i < n_pairs; i++) {
185 if (!strcasecmp(name, pairs[i].name)) {
186 *port = pairs[i].value;
194 str_to_action(char *str, struct ofpbuf *b)
197 char *saveptr = NULL;
201 for (act = strtok_r(str, ", \t\r\n", &saveptr), n_actions = 0; act;
202 act = strtok_r(NULL, ", \t\r\n", &saveptr), n_actions++)
207 ovs_fatal(0, "Drop actions must not be followed by other actions");
210 /* Arguments are separated by colons */
211 arg = strchr(act, ':');
217 if (!strcasecmp(act, "mod_vlan_vid")) {
218 struct ofp_action_vlan_vid *va;
219 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
220 va->vlan_vid = htons(str_to_u32(arg));
221 } else if (!strcasecmp(act, "mod_vlan_pcp")) {
222 struct ofp_action_vlan_pcp *va;
223 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
224 va->vlan_pcp = str_to_u32(arg);
225 } else if (!strcasecmp(act, "strip_vlan")) {
226 struct ofp_action_header *ah;
227 ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
228 ah->type = htons(OFPAT_STRIP_VLAN);
229 } else if (!strcasecmp(act, "mod_dl_src")) {
230 put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
231 } else if (!strcasecmp(act, "mod_dl_dst")) {
232 put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
233 } else if (!strcasecmp(act, "mod_nw_src")) {
234 struct ofp_action_nw_addr *na;
235 na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
236 str_to_ip(arg, &na->nw_addr, NULL);
237 } else if (!strcasecmp(act, "mod_nw_dst")) {
238 struct ofp_action_nw_addr *na;
239 na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
240 str_to_ip(arg, &na->nw_addr, NULL);
241 } else if (!strcasecmp(act, "mod_tp_src")) {
242 struct ofp_action_tp_port *ta;
243 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
244 ta->tp_port = htons(str_to_u32(arg));
245 } else if (!strcasecmp(act, "mod_tp_dst")) {
246 struct ofp_action_tp_port *ta;
247 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
248 ta->tp_port = htons(str_to_u32(arg));
249 } else if (!strcasecmp(act, "mod_nw_tos")) {
250 struct ofp_action_nw_tos *nt;
251 nt = put_action(b, sizeof *nt, OFPAT_SET_NW_TOS);
252 nt->nw_tos = str_to_u32(arg);
253 } else if (!strcasecmp(act, "resubmit")) {
254 struct nx_action_resubmit *nar;
255 nar = put_action(b, sizeof *nar, OFPAT_VENDOR);
256 nar->vendor = htonl(NX_VENDOR_ID);
257 nar->subtype = htons(NXAST_RESUBMIT);
258 nar->in_port = htons(str_to_u32(arg));
259 } else if (!strcasecmp(act, "set_tunnel")) {
260 struct nx_action_set_tunnel *nast;
261 nast = put_action(b, sizeof *nast, OFPAT_VENDOR);
262 nast->vendor = htonl(NX_VENDOR_ID);
263 nast->subtype = htons(NXAST_SET_TUNNEL);
264 nast->tun_id = htonl(str_to_u32(arg));
265 } else if (!strcasecmp(act, "drop_spoofed_arp")) {
266 struct nx_action_header *nah;
267 nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
268 nah->vendor = htonl(NX_VENDOR_ID);
269 nah->subtype = htons(NXAST_DROP_SPOOFED_ARP);
270 } else if (!strcasecmp(act, "set_queue")) {
271 struct nx_action_set_queue *nasq;
272 nasq = put_action(b, sizeof *nasq, OFPAT_VENDOR);
273 nasq->vendor = htonl(NX_VENDOR_ID);
274 nasq->subtype = htons(NXAST_SET_QUEUE);
275 nasq->queue_id = htonl(str_to_u32(arg));
276 } else if (!strcasecmp(act, "pop_queue")) {
277 struct nx_action_header *nah;
278 nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
279 nah->vendor = htonl(NX_VENDOR_ID);
280 nah->subtype = htons(NXAST_POP_QUEUE);
281 } else if (!strcasecmp(act, "note")) {
282 size_t start_ofs = b->size;
283 struct nx_action_note *nan;
287 nan = put_action(b, sizeof *nan, OFPAT_VENDOR);
288 nan->vendor = htonl(NX_VENDOR_ID);
289 nan->subtype = htons(NXAST_NOTE);
291 b->size -= sizeof nan->note;
292 while (arg && *arg != '\0') {
303 byte = hexits_value(arg, 2, &ok);
305 ovs_fatal(0, "bad hex digit in `note' argument");
307 ofpbuf_put(b, &byte, 1);
312 len = b->size - start_ofs;
313 remainder = len % OFP_ACTION_ALIGN;
315 ofpbuf_put_zeros(b, OFP_ACTION_ALIGN - remainder);
317 nan->len = htons(b->size - start_ofs);
318 } else if (!strcasecmp(act, "output")) {
319 put_output_action(b, str_to_u32(arg));
320 } else if (!strcasecmp(act, "enqueue")) {
322 char *port_s = strtok_r(arg, ":q", &sp);
323 char *queue = strtok_r(NULL, "", &sp);
324 if (port_s == NULL || queue == NULL) {
325 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
327 put_enqueue_action(b, str_to_u32(port_s), str_to_u32(queue));
328 } else if (!strcasecmp(act, "drop")) {
329 /* A drop action in OpenFlow occurs by just not setting
333 ovs_fatal(0, "Drop actions must not be preceded by other "
336 } else if (!strcasecmp(act, "CONTROLLER")) {
337 struct ofp_action_output *oao;
338 oao = put_output_action(b, OFPP_CONTROLLER);
340 /* Unless a numeric argument is specified, we send the whole
341 * packet to the controller. */
342 if (arg && (strspn(arg, "0123456789") == strlen(arg))) {
343 oao->max_len = htons(str_to_u32(arg));
345 oao->max_len = htons(UINT16_MAX);
347 } else if (parse_port_name(act, &port)) {
348 put_output_action(b, port);
349 } else if (strspn(act, "0123456789") == strlen(act)) {
350 put_output_action(b, str_to_u32(act));
352 ovs_fatal(0, "Unknown action: %s", act);
364 parse_protocol(const char *name, const struct protocol **p_out)
366 static const struct protocol protocols[] = {
367 { "ip", ETH_TYPE_IP, 0 },
368 { "arp", ETH_TYPE_ARP, 0 },
369 { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
370 { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
371 { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
373 const struct protocol *p;
375 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
376 if (!strcmp(p->name, name)) {
386 FIELD(F_TUN_ID, "tun_id", FWW_TUN_ID) \
387 FIELD(F_IN_PORT, "in_port", FWW_IN_PORT) \
388 FIELD(F_DL_VLAN, "dl_vlan", 0) \
389 FIELD(F_DL_VLAN_PCP, "dl_vlan_pcp", 0) \
390 FIELD(F_DL_SRC, "dl_src", FWW_DL_SRC) \
391 FIELD(F_DL_DST, "dl_dst", FWW_DL_DST) \
392 FIELD(F_DL_TYPE, "dl_type", FWW_DL_TYPE) \
393 FIELD(F_NW_SRC, "nw_src", 0) \
394 FIELD(F_NW_DST, "nw_dst", 0) \
395 FIELD(F_NW_PROTO, "nw_proto", FWW_NW_PROTO) \
396 FIELD(F_NW_TOS, "nw_tos", FWW_NW_TOS) \
397 FIELD(F_TP_SRC, "tp_src", FWW_TP_SRC) \
398 FIELD(F_TP_DST, "tp_dst", FWW_TP_DST) \
399 FIELD(F_ICMP_TYPE, "icmp_type", FWW_TP_SRC) \
400 FIELD(F_ICMP_CODE, "icmp_code", FWW_TP_DST)
403 #define FIELD(ENUM, NAME, WILDCARD) ENUM,
410 enum field_index index;
412 flow_wildcards_t wildcard; /* FWW_* bit. */
416 parse_field_name(const char *name, const struct field **f_out)
418 static const struct field fields[N_FIELDS] = {
419 #define FIELD(ENUM, NAME, WILDCARD) { ENUM, NAME, WILDCARD },
423 const struct field *f;
425 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
426 if (!strcmp(f->name, name)) {
436 parse_field_value(struct cls_rule *rule, enum field_index index,
439 uint8_t mac[ETH_ADDR_LEN];
445 cls_rule_set_tun_id(rule, htonl(str_to_u32(value)));
449 if (!parse_port_name(value, &port_no)) {
450 port_no = atoi(value);
452 if (port_no == OFPP_LOCAL) {
453 port_no = ODPP_LOCAL;
455 cls_rule_set_in_port(rule, port_no);
459 cls_rule_set_dl_vlan(rule, htons(str_to_u32(value)));
463 cls_rule_set_dl_vlan_pcp(rule, str_to_u32(value));
467 str_to_mac(value, mac);
468 cls_rule_set_dl_src(rule, mac);
472 str_to_mac(value, mac);
473 cls_rule_set_dl_dst(rule, mac);
477 cls_rule_set_dl_type(rule, htons(str_to_u32(value)));
481 str_to_ip(value, &ip, &mask);
482 cls_rule_set_nw_src_masked(rule, ip, mask);
486 str_to_ip(value, &ip, &mask);
487 cls_rule_set_nw_dst_masked(rule, ip, mask);
491 cls_rule_set_nw_proto(rule, str_to_u32(value));
495 cls_rule_set_nw_tos(rule, str_to_u32(value));
499 cls_rule_set_tp_src(rule, htons(str_to_u32(value)));
503 cls_rule_set_tp_dst(rule, htons(str_to_u32(value)));
507 cls_rule_set_icmp_type(rule, str_to_u32(value));
511 cls_rule_set_icmp_code(rule, str_to_u32(value));
520 parse_reg_value(struct cls_rule *rule, int reg_idx, const char *value)
522 uint32_t reg_value, reg_mask;
524 if (!strcmp(value, "ANY") || !strcmp(value, "*")) {
525 cls_rule_set_reg_masked(rule, reg_idx, 0, 0);
526 } else if (sscanf(value, "%"SCNi32"/%"SCNi32,
527 ®_value, ®_mask) == 2) {
528 cls_rule_set_reg_masked(rule, reg_idx, reg_value, reg_mask);
529 } else if (sscanf(value, "%"SCNi32, ®_value)) {
530 cls_rule_set_reg(rule, reg_idx, reg_value);
532 ovs_fatal(0, "register fields must take the form <value> "
533 "or <value>/<mask>");
537 /* Convert 'string' (as described in the Flow Syntax section of the ovs-ofctl
538 * man page) into 'pf'. If 'actions' is specified, an action must be in
539 * 'string' and may be expanded or reallocated. */
541 parse_ofp_str(struct flow_mod *fm, uint8_t *table_idx,
542 struct ofpbuf *actions, char *string)
544 char *save_ptr = NULL;
550 cls_rule_init_catchall(&fm->cr, OFP_DEFAULT_PRIORITY);
551 fm->cookie = htonll(0);
552 fm->command = UINT16_MAX;
553 fm->idle_timeout = OFP_FLOW_PERMANENT;
554 fm->hard_timeout = OFP_FLOW_PERMANENT;
555 fm->buffer_id = UINT32_MAX;
556 fm->out_port = OFPP_NONE;
559 char *act_str = strstr(string, "action");
561 ovs_fatal(0, "must specify an action");
565 act_str = strchr(act_str + 1, '=');
567 ovs_fatal(0, "must specify an action");
572 str_to_action(act_str, actions);
573 fm->actions = actions->data;
574 fm->n_actions = actions->size / sizeof(union ofp_action);
579 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
580 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
581 const struct protocol *p;
583 if (parse_protocol(name, &p)) {
584 cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
586 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
589 const struct field *f;
592 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
594 ovs_fatal(0, "field %s missing value", name);
597 if (table_idx && !strcmp(name, "table")) {
598 *table_idx = atoi(value);
599 } else if (!strcmp(name, "out_port")) {
600 fm->out_port = atoi(value);
601 } else if (!strcmp(name, "priority")) {
602 fm->cr.priority = atoi(value);
603 } else if (!strcmp(name, "idle_timeout")) {
604 fm->idle_timeout = atoi(value);
605 } else if (!strcmp(name, "hard_timeout")) {
606 fm->hard_timeout = atoi(value);
607 } else if (!strcmp(name, "cookie")) {
608 fm->cookie = htonll(str_to_u64(value));
609 } else if (parse_field_name(name, &f)) {
610 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
612 fm->cr.wc.wildcards |= f->wildcard;
613 cls_rule_zero_wildcarded_fields(&fm->cr);
614 } else if (f->index == F_NW_SRC) {
615 cls_rule_set_nw_src_masked(&fm->cr, 0, 0);
616 } else if (f->index == F_NW_DST) {
617 cls_rule_set_nw_dst_masked(&fm->cr, 0, 0);
618 } else if (f->index == F_DL_VLAN) {
619 cls_rule_set_any_vid(&fm->cr);
620 } else if (f->index == F_DL_VLAN_PCP) {
621 cls_rule_set_any_pcp(&fm->cr);
626 parse_field_value(&fm->cr, f->index, value);
628 } else if (!strncmp(name, "reg", 3) && isdigit(name[3])) {
629 unsigned int reg_idx = atoi(name + 3);
630 if (reg_idx >= FLOW_N_REGS) {
631 ovs_fatal(0, "only %d registers supported", FLOW_N_REGS);
633 parse_reg_value(&fm->cr, reg_idx, value);
635 ovs_fatal(0, "unknown keyword %s", name);
641 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
642 * (one of OFPFC_*) and appends the parsed OpenFlow message to 'packets'.
643 * '*cur_format' should initially contain the flow format currently configured
644 * on the connection; this function will add a message to change the flow
645 * format and update '*cur_format', if this is necessary to add the parsed
648 parse_ofp_flow_mod_str(struct list *packets, enum nx_flow_format *cur_format,
649 char *string, uint16_t command)
651 enum nx_flow_format min_format, next_format;
652 struct ofpbuf actions;
656 ofpbuf_init(&actions, 64);
657 parse_ofp_str(&fm, NULL, &actions, string);
658 fm.command = command;
660 min_format = ofputil_min_flow_format(&fm.cr, true, fm.cookie);
661 next_format = MAX(*cur_format, min_format);
662 if (next_format != *cur_format) {
663 struct ofpbuf *sff = ofputil_make_set_flow_format(next_format);
664 list_push_back(packets, &sff->list_node);
665 *cur_format = next_format;
668 ofm = ofputil_encode_flow_mod(&fm, *cur_format);
669 list_push_back(packets, &ofm->list_node);
671 ofpbuf_uninit(&actions);
674 /* Similar to parse_ofp_flow_mod_str(), except that the string is read from
675 * 'stream' and the command is always OFPFC_ADD. Returns false if end-of-file
676 * is reached before reading a flow, otherwise true. */
678 parse_ofp_add_flow_file(struct list *packets, enum nx_flow_format *cur,
681 struct ds s = DS_EMPTY_INITIALIZER;
684 while (!ds_get_line(&s, stream)) {
685 char *line = ds_cstr(&s);
688 /* Delete comments. */
689 comment = strchr(line, '#');
694 /* Drop empty lines. */
695 if (line[strspn(line, " \t\n")] == '\0') {
699 parse_ofp_flow_mod_str(packets, cur, line, OFPFC_ADD);
709 parse_ofp_flow_stats_request_str(struct flow_stats_request *fsr,
710 bool aggregate, char *string)
715 parse_ofp_str(&fm, &table_id, NULL, string);
716 fsr->aggregate = aggregate;
718 fsr->out_port = fm.out_port;
719 fsr->table_id = table_id;