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"
28 #include "multipath.h"
32 #include "openflow/openflow.h"
34 #include "socket-util.h"
38 VLOG_DEFINE_THIS_MODULE(ofp_parse);
41 str_to_u32(const char *str)
47 ovs_fatal(0, "missing required numeric argument");
51 value = strtoul(str, &tail, 0);
52 if (errno == EINVAL || errno == ERANGE || *tail) {
53 ovs_fatal(0, "invalid numeric format %s", str);
59 str_to_u64(const char *str)
65 value = strtoull(str, &tail, 0);
66 if (errno == EINVAL || errno == ERANGE || *tail) {
67 ovs_fatal(0, "invalid numeric format %s", str);
73 str_to_mac(const char *str, uint8_t mac[6])
75 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
76 != ETH_ADDR_SCAN_COUNT) {
77 ovs_fatal(0, "invalid mac address %s", str);
82 str_to_ip(const char *str_, ovs_be32 *ip, ovs_be32 *maskp)
84 char *str = xstrdup(str_);
85 char *save_ptr = NULL;
86 const char *name, *netmask;
87 struct in_addr in_addr;
91 name = strtok_r(str, "/", &save_ptr);
92 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
94 ovs_fatal(0, "%s: could not convert to IP address", str);
98 netmask = strtok_r(NULL, "/", &save_ptr);
101 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
102 &o[0], &o[1], &o[2], &o[3]) == 4) {
103 mask = htonl((o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3]);
105 int prefix = atoi(netmask);
106 if (prefix <= 0 || prefix > 32) {
107 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
109 } else if (prefix == 32) {
110 mask = htonl(UINT32_MAX);
112 mask = htonl(((1u << prefix) - 1) << (32 - prefix));
116 mask = htonl(UINT32_MAX);
123 if (mask != htonl(UINT32_MAX)) {
124 ovs_fatal(0, "%s: netmask not allowed here", str_);
132 put_action(struct ofpbuf *b, size_t size, uint16_t type)
134 struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
135 ah->type = htons(type);
136 ah->len = htons(size);
140 static struct ofp_action_output *
141 put_output_action(struct ofpbuf *b, uint16_t port)
143 struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
144 oao->port = htons(port);
149 put_enqueue_action(struct ofpbuf *b, uint16_t port, uint32_t queue)
151 struct ofp_action_enqueue *oae = put_action(b, sizeof *oae, OFPAT_ENQUEUE);
152 oae->port = htons(port);
153 oae->queue_id = htonl(queue);
157 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
159 struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
160 str_to_mac(addr, oada->dl_addr);
165 parse_port_name(const char *name, uint16_t *port)
171 static const struct pair pairs[] = {
172 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
178 DEF_PAIR(CONTROLLER),
183 static const int n_pairs = ARRAY_SIZE(pairs);
186 for (i = 0; i < n_pairs; i++) {
187 if (!strcasecmp(name, pairs[i].name)) {
188 *port = pairs[i].value;
196 str_to_action(char *str, struct ofpbuf *b)
208 pos += strspn(pos, ", \t\r\n");
214 ovs_fatal(0, "Drop actions must not be followed by other actions");
218 actlen = strcspn(pos, ":(, \t\r\n");
219 if (act[actlen] == ':') {
220 /* The argument can be separated by a colon. */
223 arg = act + actlen + 1;
224 arglen = strcspn(arg, ", \t\r\n");
225 pos = arg + arglen + (arg[arglen] != '\0');
227 } else if (act[actlen] == '(') {
228 /* The argument can be surrounded by balanced parentheses. The
229 * outermost set of parentheses is removed. */
233 arg = act + actlen + 1;
234 for (arglen = 0; level > 0; arglen++) {
235 switch (arg[arglen]) {
237 ovs_fatal(0, "unbalanced parentheses in argument to %s "
249 arg[arglen - 1] = '\0';
252 /* There might be no argument at all. */
254 pos = act + actlen + (act[actlen] != '\0');
258 if (!strcasecmp(act, "mod_vlan_vid")) {
259 struct ofp_action_vlan_vid *va;
260 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
261 va->vlan_vid = htons(str_to_u32(arg));
262 } else if (!strcasecmp(act, "mod_vlan_pcp")) {
263 struct ofp_action_vlan_pcp *va;
264 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
265 va->vlan_pcp = str_to_u32(arg);
266 } else if (!strcasecmp(act, "strip_vlan")) {
267 struct ofp_action_header *ah;
268 ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
269 ah->type = htons(OFPAT_STRIP_VLAN);
270 } else if (!strcasecmp(act, "mod_dl_src")) {
271 put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
272 } else if (!strcasecmp(act, "mod_dl_dst")) {
273 put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
274 } else if (!strcasecmp(act, "mod_nw_src")) {
275 struct ofp_action_nw_addr *na;
276 na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
277 str_to_ip(arg, &na->nw_addr, NULL);
278 } else if (!strcasecmp(act, "mod_nw_dst")) {
279 struct ofp_action_nw_addr *na;
280 na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
281 str_to_ip(arg, &na->nw_addr, NULL);
282 } else if (!strcasecmp(act, "mod_tp_src")) {
283 struct ofp_action_tp_port *ta;
284 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
285 ta->tp_port = htons(str_to_u32(arg));
286 } else if (!strcasecmp(act, "mod_tp_dst")) {
287 struct ofp_action_tp_port *ta;
288 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
289 ta->tp_port = htons(str_to_u32(arg));
290 } else if (!strcasecmp(act, "mod_nw_tos")) {
291 struct ofp_action_nw_tos *nt;
292 nt = put_action(b, sizeof *nt, OFPAT_SET_NW_TOS);
293 nt->nw_tos = str_to_u32(arg);
294 } else if (!strcasecmp(act, "resubmit")) {
295 struct nx_action_resubmit *nar;
296 nar = put_action(b, sizeof *nar, OFPAT_VENDOR);
297 nar->vendor = htonl(NX_VENDOR_ID);
298 nar->subtype = htons(NXAST_RESUBMIT);
299 nar->in_port = htons(str_to_u32(arg));
300 } else if (!strcasecmp(act, "set_tunnel")
301 || !strcasecmp(act, "set_tunnel64")) {
302 uint64_t tun_id = str_to_u64(arg);
303 if (!strcasecmp(act, "set_tunnel64") || tun_id > UINT32_MAX) {
304 struct nx_action_set_tunnel64 *nast64;
305 nast64 = put_action(b, sizeof *nast64, OFPAT_VENDOR);
306 nast64->vendor = htonl(NX_VENDOR_ID);
307 nast64->subtype = htons(NXAST_SET_TUNNEL64);
308 nast64->tun_id = htonll(tun_id);
310 struct nx_action_set_tunnel *nast;
311 nast = put_action(b, sizeof *nast, OFPAT_VENDOR);
312 nast->vendor = htonl(NX_VENDOR_ID);
313 nast->subtype = htons(NXAST_SET_TUNNEL);
314 nast->tun_id = htonl(tun_id);
316 } else if (!strcasecmp(act, "drop_spoofed_arp")) {
317 struct nx_action_header *nah;
318 nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
319 nah->vendor = htonl(NX_VENDOR_ID);
320 nah->subtype = htons(NXAST_DROP_SPOOFED_ARP);
321 } else if (!strcasecmp(act, "set_queue")) {
322 struct nx_action_set_queue *nasq;
323 nasq = put_action(b, sizeof *nasq, OFPAT_VENDOR);
324 nasq->vendor = htonl(NX_VENDOR_ID);
325 nasq->subtype = htons(NXAST_SET_QUEUE);
326 nasq->queue_id = htonl(str_to_u32(arg));
327 } else if (!strcasecmp(act, "pop_queue")) {
328 struct nx_action_header *nah;
329 nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
330 nah->vendor = htonl(NX_VENDOR_ID);
331 nah->subtype = htons(NXAST_POP_QUEUE);
332 } else if (!strcasecmp(act, "note")) {
333 size_t start_ofs = b->size;
334 struct nx_action_note *nan;
338 nan = put_action(b, sizeof *nan, OFPAT_VENDOR);
339 nan->vendor = htonl(NX_VENDOR_ID);
340 nan->subtype = htons(NXAST_NOTE);
342 b->size -= sizeof nan->note;
343 while (arg && *arg != '\0') {
354 byte = hexits_value(arg, 2, &ok);
356 ovs_fatal(0, "bad hex digit in `note' argument");
358 ofpbuf_put(b, &byte, 1);
363 len = b->size - start_ofs;
364 remainder = len % OFP_ACTION_ALIGN;
366 ofpbuf_put_zeros(b, OFP_ACTION_ALIGN - remainder);
368 nan->len = htons(b->size - start_ofs);
369 } else if (!strcasecmp(act, "move")) {
370 struct nx_action_reg_move *move;
371 move = ofpbuf_put_uninit(b, sizeof *move);
372 nxm_parse_reg_move(move, arg);
373 } else if (!strcasecmp(act, "load")) {
374 struct nx_action_reg_load *load;
375 load = ofpbuf_put_uninit(b, sizeof *load);
376 nxm_parse_reg_load(load, arg);
377 } else if (!strcasecmp(act, "multipath")) {
378 struct nx_action_multipath *nam;
379 nam = ofpbuf_put_uninit(b, sizeof *nam);
380 multipath_parse(nam, arg);
381 } else if (!strcasecmp(act, "output")) {
382 put_output_action(b, str_to_u32(arg));
383 } else if (!strcasecmp(act, "enqueue")) {
385 char *port_s = strtok_r(arg, ":q", &sp);
386 char *queue = strtok_r(NULL, "", &sp);
387 if (port_s == NULL || queue == NULL) {
388 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
390 put_enqueue_action(b, str_to_u32(port_s), str_to_u32(queue));
391 } else if (!strcasecmp(act, "drop")) {
392 /* A drop action in OpenFlow occurs by just not setting
396 ovs_fatal(0, "Drop actions must not be preceded by other "
399 } else if (!strcasecmp(act, "CONTROLLER")) {
400 struct ofp_action_output *oao;
401 oao = put_output_action(b, OFPP_CONTROLLER);
403 /* Unless a numeric argument is specified, we send the whole
404 * packet to the controller. */
405 if (arg && (strspn(arg, "0123456789") == strlen(arg))) {
406 oao->max_len = htons(str_to_u32(arg));
408 oao->max_len = htons(UINT16_MAX);
410 } else if (parse_port_name(act, &port)) {
411 put_output_action(b, port);
412 } else if (strspn(act, "0123456789") == strlen(act)) {
413 put_output_action(b, str_to_u32(act));
415 ovs_fatal(0, "Unknown action: %s", act);
427 parse_protocol(const char *name, const struct protocol **p_out)
429 static const struct protocol protocols[] = {
430 { "ip", ETH_TYPE_IP, 0 },
431 { "arp", ETH_TYPE_ARP, 0 },
432 { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
433 { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
434 { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
436 const struct protocol *p;
438 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
439 if (!strcmp(p->name, name)) {
449 FIELD(F_TUN_ID, "tun_id", FWW_TUN_ID) \
450 FIELD(F_IN_PORT, "in_port", FWW_IN_PORT) \
451 FIELD(F_DL_VLAN, "dl_vlan", 0) \
452 FIELD(F_DL_VLAN_PCP, "dl_vlan_pcp", 0) \
453 FIELD(F_DL_SRC, "dl_src", FWW_DL_SRC) \
454 FIELD(F_DL_DST, "dl_dst", FWW_DL_DST) \
455 FIELD(F_DL_TYPE, "dl_type", FWW_DL_TYPE) \
456 FIELD(F_NW_SRC, "nw_src", 0) \
457 FIELD(F_NW_DST, "nw_dst", 0) \
458 FIELD(F_NW_PROTO, "nw_proto", FWW_NW_PROTO) \
459 FIELD(F_NW_TOS, "nw_tos", FWW_NW_TOS) \
460 FIELD(F_TP_SRC, "tp_src", FWW_TP_SRC) \
461 FIELD(F_TP_DST, "tp_dst", FWW_TP_DST) \
462 FIELD(F_ICMP_TYPE, "icmp_type", FWW_TP_SRC) \
463 FIELD(F_ICMP_CODE, "icmp_code", FWW_TP_DST)
466 #define FIELD(ENUM, NAME, WILDCARD) ENUM,
473 enum field_index index;
475 flow_wildcards_t wildcard; /* FWW_* bit. */
479 parse_field_name(const char *name, const struct field **f_out)
481 static const struct field fields[N_FIELDS] = {
482 #define FIELD(ENUM, NAME, WILDCARD) { ENUM, NAME, WILDCARD },
486 const struct field *f;
488 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
489 if (!strcmp(f->name, name)) {
499 parse_field_value(struct cls_rule *rule, enum field_index index,
502 uint8_t mac[ETH_ADDR_LEN];
508 cls_rule_set_tun_id(rule, htonll(str_to_u64(value)));
512 if (!parse_port_name(value, &port_no)) {
513 port_no = atoi(value);
515 if (port_no == OFPP_LOCAL) {
516 port_no = ODPP_LOCAL;
518 cls_rule_set_in_port(rule, port_no);
522 cls_rule_set_dl_vlan(rule, htons(str_to_u32(value)));
526 cls_rule_set_dl_vlan_pcp(rule, str_to_u32(value));
530 str_to_mac(value, mac);
531 cls_rule_set_dl_src(rule, mac);
535 str_to_mac(value, mac);
536 cls_rule_set_dl_dst(rule, mac);
540 cls_rule_set_dl_type(rule, htons(str_to_u32(value)));
544 str_to_ip(value, &ip, &mask);
545 cls_rule_set_nw_src_masked(rule, ip, mask);
549 str_to_ip(value, &ip, &mask);
550 cls_rule_set_nw_dst_masked(rule, ip, mask);
554 cls_rule_set_nw_proto(rule, str_to_u32(value));
558 cls_rule_set_nw_tos(rule, str_to_u32(value));
562 cls_rule_set_tp_src(rule, htons(str_to_u32(value)));
566 cls_rule_set_tp_dst(rule, htons(str_to_u32(value)));
570 cls_rule_set_icmp_type(rule, str_to_u32(value));
574 cls_rule_set_icmp_code(rule, str_to_u32(value));
583 parse_reg_value(struct cls_rule *rule, int reg_idx, const char *value)
585 uint32_t reg_value, reg_mask;
587 if (!strcmp(value, "ANY") || !strcmp(value, "*")) {
588 cls_rule_set_reg_masked(rule, reg_idx, 0, 0);
589 } else if (sscanf(value, "%"SCNi32"/%"SCNi32,
590 ®_value, ®_mask) == 2) {
591 cls_rule_set_reg_masked(rule, reg_idx, reg_value, reg_mask);
592 } else if (sscanf(value, "%"SCNi32, ®_value)) {
593 cls_rule_set_reg(rule, reg_idx, reg_value);
595 ovs_fatal(0, "register fields must take the form <value> "
596 "or <value>/<mask>");
600 /* Convert 'string' (as described in the Flow Syntax section of the ovs-ofctl
601 * man page) into 'pf'. If 'actions' is specified, an action must be in
602 * 'string' and may be expanded or reallocated. */
604 parse_ofp_str(struct flow_mod *fm, uint8_t *table_idx,
605 struct ofpbuf *actions, char *string)
607 char *save_ptr = NULL;
613 cls_rule_init_catchall(&fm->cr, OFP_DEFAULT_PRIORITY);
614 fm->cookie = htonll(0);
615 fm->command = UINT16_MAX;
616 fm->idle_timeout = OFP_FLOW_PERMANENT;
617 fm->hard_timeout = OFP_FLOW_PERMANENT;
618 fm->buffer_id = UINT32_MAX;
619 fm->out_port = OFPP_NONE;
622 char *act_str = strstr(string, "action");
624 ovs_fatal(0, "must specify an action");
628 act_str = strchr(act_str + 1, '=');
630 ovs_fatal(0, "must specify an action");
635 str_to_action(act_str, actions);
636 fm->actions = actions->data;
637 fm->n_actions = actions->size / sizeof(union ofp_action);
642 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
643 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
644 const struct protocol *p;
646 if (parse_protocol(name, &p)) {
647 cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
649 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
652 const struct field *f;
655 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
657 ovs_fatal(0, "field %s missing value", name);
660 if (table_idx && !strcmp(name, "table")) {
661 *table_idx = atoi(value);
662 } else if (!strcmp(name, "out_port")) {
663 fm->out_port = atoi(value);
664 } else if (!strcmp(name, "priority")) {
665 fm->cr.priority = atoi(value);
666 } else if (!strcmp(name, "idle_timeout")) {
667 fm->idle_timeout = atoi(value);
668 } else if (!strcmp(name, "hard_timeout")) {
669 fm->hard_timeout = atoi(value);
670 } else if (!strcmp(name, "cookie")) {
671 fm->cookie = htonll(str_to_u64(value));
672 } else if (parse_field_name(name, &f)) {
673 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
675 fm->cr.wc.wildcards |= f->wildcard;
676 cls_rule_zero_wildcarded_fields(&fm->cr);
677 } else if (f->index == F_NW_SRC) {
678 cls_rule_set_nw_src_masked(&fm->cr, 0, 0);
679 } else if (f->index == F_NW_DST) {
680 cls_rule_set_nw_dst_masked(&fm->cr, 0, 0);
681 } else if (f->index == F_DL_VLAN) {
682 cls_rule_set_any_vid(&fm->cr);
683 } else if (f->index == F_DL_VLAN_PCP) {
684 cls_rule_set_any_pcp(&fm->cr);
689 parse_field_value(&fm->cr, f->index, value);
691 } else if (!strncmp(name, "reg", 3) && isdigit(name[3])) {
692 unsigned int reg_idx = atoi(name + 3);
693 if (reg_idx >= FLOW_N_REGS) {
694 ovs_fatal(0, "only %d registers supported", FLOW_N_REGS);
696 parse_reg_value(&fm->cr, reg_idx, value);
698 ovs_fatal(0, "unknown keyword %s", name);
704 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
705 * (one of OFPFC_*) and appends the parsed OpenFlow message to 'packets'.
706 * '*cur_format' should initially contain the flow format currently configured
707 * on the connection; this function will add a message to change the flow
708 * format and update '*cur_format', if this is necessary to add the parsed
711 parse_ofp_flow_mod_str(struct list *packets, enum nx_flow_format *cur_format,
712 char *string, uint16_t command)
714 bool is_del = command == OFPFC_DELETE || command == OFPFC_DELETE_STRICT;
715 enum nx_flow_format min_format, next_format;
716 struct ofpbuf actions;
720 ofpbuf_init(&actions, 64);
721 parse_ofp_str(&fm, NULL, is_del ? NULL : &actions, string);
722 fm.command = command;
724 min_format = ofputil_min_flow_format(&fm.cr, true, fm.cookie);
725 next_format = MAX(*cur_format, min_format);
726 if (next_format != *cur_format) {
727 struct ofpbuf *sff = ofputil_make_set_flow_format(next_format);
728 list_push_back(packets, &sff->list_node);
729 *cur_format = next_format;
732 ofm = ofputil_encode_flow_mod(&fm, *cur_format);
733 list_push_back(packets, &ofm->list_node);
735 ofpbuf_uninit(&actions);
738 /* Similar to parse_ofp_flow_mod_str(), except that the string is read from
739 * 'stream' and the command is always OFPFC_ADD. Returns false if end-of-file
740 * is reached before reading a flow, otherwise true. */
742 parse_ofp_add_flow_file(struct list *packets, enum nx_flow_format *cur,
745 struct ds s = DS_EMPTY_INITIALIZER;
748 while (!ds_get_line(&s, stream)) {
749 char *line = ds_cstr(&s);
752 /* Delete comments. */
753 comment = strchr(line, '#');
758 /* Drop empty lines. */
759 if (line[strspn(line, " \t\n")] == '\0') {
763 parse_ofp_flow_mod_str(packets, cur, line, OFPFC_ADD);
773 parse_ofp_flow_stats_request_str(struct flow_stats_request *fsr,
774 bool aggregate, char *string)
779 parse_ofp_str(&fm, &table_id, NULL, string);
780 fsr->aggregate = aggregate;
782 fsr->out_port = fm.out_port;
783 fsr->table_id = table_id;