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"
31 #include "openflow/openflow.h"
33 #include "socket-util.h"
37 VLOG_DEFINE_THIS_MODULE(ofp_parse);
40 str_to_u32(const char *str)
46 ovs_fatal(0, "missing required numeric argument");
50 value = strtoul(str, &tail, 0);
51 if (errno == EINVAL || errno == ERANGE || *tail) {
52 ovs_fatal(0, "invalid numeric format %s", str);
58 str_to_u64(const char *str)
64 value = strtoull(str, &tail, 0);
65 if (errno == EINVAL || errno == ERANGE || *tail) {
66 ovs_fatal(0, "invalid numeric format %s", str);
72 str_to_mac(const char *str, uint8_t mac[6])
74 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
75 != ETH_ADDR_SCAN_COUNT) {
76 ovs_fatal(0, "invalid mac address %s", str);
81 str_to_ip(const char *str_, ovs_be32 *ip, ovs_be32 *maskp)
83 char *str = xstrdup(str_);
84 char *save_ptr = NULL;
85 const char *name, *netmask;
86 struct in_addr in_addr;
90 name = strtok_r(str, "/", &save_ptr);
91 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
93 ovs_fatal(0, "%s: could not convert to IP address", str);
97 netmask = strtok_r(NULL, "/", &save_ptr);
100 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
101 &o[0], &o[1], &o[2], &o[3]) == 4) {
102 mask = htonl((o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3]);
104 int prefix = atoi(netmask);
105 if (prefix <= 0 || prefix > 32) {
106 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
108 } else if (prefix == 32) {
109 mask = htonl(UINT32_MAX);
111 mask = htonl(((1u << prefix) - 1) << (32 - prefix));
115 mask = htonl(UINT32_MAX);
122 if (mask != htonl(UINT32_MAX)) {
123 ovs_fatal(0, "%s: netmask not allowed here", str_);
131 put_action(struct ofpbuf *b, size_t size, uint16_t type)
133 struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
134 ah->type = htons(type);
135 ah->len = htons(size);
139 static struct ofp_action_output *
140 put_output_action(struct ofpbuf *b, uint16_t port)
142 struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
143 oao->port = htons(port);
148 put_enqueue_action(struct ofpbuf *b, uint16_t port, uint32_t queue)
150 struct ofp_action_enqueue *oae = put_action(b, sizeof *oae, OFPAT_ENQUEUE);
151 oae->port = htons(port);
152 oae->queue_id = htonl(queue);
156 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
158 struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
159 str_to_mac(addr, oada->dl_addr);
164 parse_port_name(const char *name, uint16_t *port)
170 static const struct pair pairs[] = {
171 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
177 DEF_PAIR(CONTROLLER),
182 static const int n_pairs = ARRAY_SIZE(pairs);
185 for (i = 0; i < n_pairs; i++) {
186 if (!strcasecmp(name, pairs[i].name)) {
187 *port = pairs[i].value;
195 str_to_action(char *str, struct ofpbuf *b)
198 char *saveptr = NULL;
202 for (act = strtok_r(str, ", \t\r\n", &saveptr), n_actions = 0; act;
203 act = strtok_r(NULL, ", \t\r\n", &saveptr), n_actions++)
208 ovs_fatal(0, "Drop actions must not be followed by other actions");
211 /* Arguments are separated by colons */
212 arg = strchr(act, ':');
218 if (!strcasecmp(act, "mod_vlan_vid")) {
219 struct ofp_action_vlan_vid *va;
220 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
221 va->vlan_vid = htons(str_to_u32(arg));
222 } else if (!strcasecmp(act, "mod_vlan_pcp")) {
223 struct ofp_action_vlan_pcp *va;
224 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
225 va->vlan_pcp = str_to_u32(arg);
226 } else if (!strcasecmp(act, "strip_vlan")) {
227 struct ofp_action_header *ah;
228 ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
229 ah->type = htons(OFPAT_STRIP_VLAN);
230 } else if (!strcasecmp(act, "mod_dl_src")) {
231 put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
232 } else if (!strcasecmp(act, "mod_dl_dst")) {
233 put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
234 } else if (!strcasecmp(act, "mod_nw_src")) {
235 struct ofp_action_nw_addr *na;
236 na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
237 str_to_ip(arg, &na->nw_addr, NULL);
238 } else if (!strcasecmp(act, "mod_nw_dst")) {
239 struct ofp_action_nw_addr *na;
240 na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
241 str_to_ip(arg, &na->nw_addr, NULL);
242 } else if (!strcasecmp(act, "mod_tp_src")) {
243 struct ofp_action_tp_port *ta;
244 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
245 ta->tp_port = htons(str_to_u32(arg));
246 } else if (!strcasecmp(act, "mod_tp_dst")) {
247 struct ofp_action_tp_port *ta;
248 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
249 ta->tp_port = htons(str_to_u32(arg));
250 } else if (!strcasecmp(act, "mod_nw_tos")) {
251 struct ofp_action_nw_tos *nt;
252 nt = put_action(b, sizeof *nt, OFPAT_SET_NW_TOS);
253 nt->nw_tos = str_to_u32(arg);
254 } else if (!strcasecmp(act, "resubmit")) {
255 struct nx_action_resubmit *nar;
256 nar = put_action(b, sizeof *nar, OFPAT_VENDOR);
257 nar->vendor = htonl(NX_VENDOR_ID);
258 nar->subtype = htons(NXAST_RESUBMIT);
259 nar->in_port = htons(str_to_u32(arg));
260 } else if (!strcasecmp(act, "set_tunnel")) {
261 struct nx_action_set_tunnel *nast;
262 nast = put_action(b, sizeof *nast, OFPAT_VENDOR);
263 nast->vendor = htonl(NX_VENDOR_ID);
264 nast->subtype = htons(NXAST_SET_TUNNEL);
265 nast->tun_id = htonl(str_to_u32(arg));
266 } else if (!strcasecmp(act, "drop_spoofed_arp")) {
267 struct nx_action_header *nah;
268 nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
269 nah->vendor = htonl(NX_VENDOR_ID);
270 nah->subtype = htons(NXAST_DROP_SPOOFED_ARP);
271 } else if (!strcasecmp(act, "set_queue")) {
272 struct nx_action_set_queue *nasq;
273 nasq = put_action(b, sizeof *nasq, OFPAT_VENDOR);
274 nasq->vendor = htonl(NX_VENDOR_ID);
275 nasq->subtype = htons(NXAST_SET_QUEUE);
276 nasq->queue_id = htonl(str_to_u32(arg));
277 } else if (!strcasecmp(act, "pop_queue")) {
278 struct nx_action_header *nah;
279 nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
280 nah->vendor = htonl(NX_VENDOR_ID);
281 nah->subtype = htons(NXAST_POP_QUEUE);
282 } else if (!strcasecmp(act, "note")) {
283 size_t start_ofs = b->size;
284 struct nx_action_note *nan;
288 nan = put_action(b, sizeof *nan, OFPAT_VENDOR);
289 nan->vendor = htonl(NX_VENDOR_ID);
290 nan->subtype = htons(NXAST_NOTE);
292 b->size -= sizeof nan->note;
293 while (arg && *arg != '\0') {
304 byte = hexits_value(arg, 2, &ok);
306 ovs_fatal(0, "bad hex digit in `note' argument");
308 ofpbuf_put(b, &byte, 1);
313 len = b->size - start_ofs;
314 remainder = len % OFP_ACTION_ALIGN;
316 ofpbuf_put_zeros(b, OFP_ACTION_ALIGN - remainder);
318 nan->len = htons(b->size - start_ofs);
319 } else if (!strcasecmp(act, "move")) {
320 struct nx_action_reg_move *move;
321 move = ofpbuf_put_uninit(b, sizeof *move);
322 nxm_parse_reg_move(move, arg);
323 } else if (!strcasecmp(act, "load")) {
324 struct nx_action_reg_load *load;
325 load = ofpbuf_put_uninit(b, sizeof *load);
326 nxm_parse_reg_load(load, arg);
327 } else if (!strcasecmp(act, "output")) {
328 put_output_action(b, str_to_u32(arg));
329 } else if (!strcasecmp(act, "enqueue")) {
331 char *port_s = strtok_r(arg, ":q", &sp);
332 char *queue = strtok_r(NULL, "", &sp);
333 if (port_s == NULL || queue == NULL) {
334 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
336 put_enqueue_action(b, str_to_u32(port_s), str_to_u32(queue));
337 } else if (!strcasecmp(act, "drop")) {
338 /* A drop action in OpenFlow occurs by just not setting
342 ovs_fatal(0, "Drop actions must not be preceded by other "
345 } else if (!strcasecmp(act, "CONTROLLER")) {
346 struct ofp_action_output *oao;
347 oao = put_output_action(b, OFPP_CONTROLLER);
349 /* Unless a numeric argument is specified, we send the whole
350 * packet to the controller. */
351 if (arg && (strspn(arg, "0123456789") == strlen(arg))) {
352 oao->max_len = htons(str_to_u32(arg));
354 oao->max_len = htons(UINT16_MAX);
356 } else if (parse_port_name(act, &port)) {
357 put_output_action(b, port);
358 } else if (strspn(act, "0123456789") == strlen(act)) {
359 put_output_action(b, str_to_u32(act));
361 ovs_fatal(0, "Unknown action: %s", act);
373 parse_protocol(const char *name, const struct protocol **p_out)
375 static const struct protocol protocols[] = {
376 { "ip", ETH_TYPE_IP, 0 },
377 { "arp", ETH_TYPE_ARP, 0 },
378 { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
379 { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
380 { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
382 const struct protocol *p;
384 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
385 if (!strcmp(p->name, name)) {
395 FIELD(F_TUN_ID, "tun_id", FWW_TUN_ID) \
396 FIELD(F_IN_PORT, "in_port", FWW_IN_PORT) \
397 FIELD(F_DL_VLAN, "dl_vlan", 0) \
398 FIELD(F_DL_VLAN_PCP, "dl_vlan_pcp", 0) \
399 FIELD(F_DL_SRC, "dl_src", FWW_DL_SRC) \
400 FIELD(F_DL_DST, "dl_dst", FWW_DL_DST) \
401 FIELD(F_DL_TYPE, "dl_type", FWW_DL_TYPE) \
402 FIELD(F_NW_SRC, "nw_src", 0) \
403 FIELD(F_NW_DST, "nw_dst", 0) \
404 FIELD(F_NW_PROTO, "nw_proto", FWW_NW_PROTO) \
405 FIELD(F_NW_TOS, "nw_tos", FWW_NW_TOS) \
406 FIELD(F_TP_SRC, "tp_src", FWW_TP_SRC) \
407 FIELD(F_TP_DST, "tp_dst", FWW_TP_DST) \
408 FIELD(F_ICMP_TYPE, "icmp_type", FWW_TP_SRC) \
409 FIELD(F_ICMP_CODE, "icmp_code", FWW_TP_DST)
412 #define FIELD(ENUM, NAME, WILDCARD) ENUM,
419 enum field_index index;
421 flow_wildcards_t wildcard; /* FWW_* bit. */
425 parse_field_name(const char *name, const struct field **f_out)
427 static const struct field fields[N_FIELDS] = {
428 #define FIELD(ENUM, NAME, WILDCARD) { ENUM, NAME, WILDCARD },
432 const struct field *f;
434 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
435 if (!strcmp(f->name, name)) {
445 parse_field_value(struct cls_rule *rule, enum field_index index,
448 uint8_t mac[ETH_ADDR_LEN];
454 cls_rule_set_tun_id(rule, htonl(str_to_u32(value)));
458 if (!parse_port_name(value, &port_no)) {
459 port_no = atoi(value);
461 if (port_no == OFPP_LOCAL) {
462 port_no = ODPP_LOCAL;
464 cls_rule_set_in_port(rule, port_no);
468 cls_rule_set_dl_vlan(rule, htons(str_to_u32(value)));
472 cls_rule_set_dl_vlan_pcp(rule, str_to_u32(value));
476 str_to_mac(value, mac);
477 cls_rule_set_dl_src(rule, mac);
481 str_to_mac(value, mac);
482 cls_rule_set_dl_dst(rule, mac);
486 cls_rule_set_dl_type(rule, htons(str_to_u32(value)));
490 str_to_ip(value, &ip, &mask);
491 cls_rule_set_nw_src_masked(rule, ip, mask);
495 str_to_ip(value, &ip, &mask);
496 cls_rule_set_nw_dst_masked(rule, ip, mask);
500 cls_rule_set_nw_proto(rule, str_to_u32(value));
504 cls_rule_set_nw_tos(rule, str_to_u32(value));
508 cls_rule_set_tp_src(rule, htons(str_to_u32(value)));
512 cls_rule_set_tp_dst(rule, htons(str_to_u32(value)));
516 cls_rule_set_icmp_type(rule, str_to_u32(value));
520 cls_rule_set_icmp_code(rule, str_to_u32(value));
529 parse_reg_value(struct cls_rule *rule, int reg_idx, const char *value)
531 uint32_t reg_value, reg_mask;
533 if (!strcmp(value, "ANY") || !strcmp(value, "*")) {
534 cls_rule_set_reg_masked(rule, reg_idx, 0, 0);
535 } else if (sscanf(value, "%"SCNi32"/%"SCNi32,
536 ®_value, ®_mask) == 2) {
537 cls_rule_set_reg_masked(rule, reg_idx, reg_value, reg_mask);
538 } else if (sscanf(value, "%"SCNi32, ®_value)) {
539 cls_rule_set_reg(rule, reg_idx, reg_value);
541 ovs_fatal(0, "register fields must take the form <value> "
542 "or <value>/<mask>");
546 /* Convert 'string' (as described in the Flow Syntax section of the ovs-ofctl
547 * man page) into 'pf'. If 'actions' is specified, an action must be in
548 * 'string' and may be expanded or reallocated. */
550 parse_ofp_str(struct flow_mod *fm, uint8_t *table_idx,
551 struct ofpbuf *actions, char *string)
553 char *save_ptr = NULL;
559 cls_rule_init_catchall(&fm->cr, OFP_DEFAULT_PRIORITY);
560 fm->cookie = htonll(0);
561 fm->command = UINT16_MAX;
562 fm->idle_timeout = OFP_FLOW_PERMANENT;
563 fm->hard_timeout = OFP_FLOW_PERMANENT;
564 fm->buffer_id = UINT32_MAX;
565 fm->out_port = OFPP_NONE;
568 char *act_str = strstr(string, "action");
570 ovs_fatal(0, "must specify an action");
574 act_str = strchr(act_str + 1, '=');
576 ovs_fatal(0, "must specify an action");
581 str_to_action(act_str, actions);
582 fm->actions = actions->data;
583 fm->n_actions = actions->size / sizeof(union ofp_action);
588 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
589 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
590 const struct protocol *p;
592 if (parse_protocol(name, &p)) {
593 cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
595 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
598 const struct field *f;
601 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
603 ovs_fatal(0, "field %s missing value", name);
606 if (table_idx && !strcmp(name, "table")) {
607 *table_idx = atoi(value);
608 } else if (!strcmp(name, "out_port")) {
609 fm->out_port = atoi(value);
610 } else if (!strcmp(name, "priority")) {
611 fm->cr.priority = atoi(value);
612 } else if (!strcmp(name, "idle_timeout")) {
613 fm->idle_timeout = atoi(value);
614 } else if (!strcmp(name, "hard_timeout")) {
615 fm->hard_timeout = atoi(value);
616 } else if (!strcmp(name, "cookie")) {
617 fm->cookie = htonll(str_to_u64(value));
618 } else if (parse_field_name(name, &f)) {
619 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
621 fm->cr.wc.wildcards |= f->wildcard;
622 cls_rule_zero_wildcarded_fields(&fm->cr);
623 } else if (f->index == F_NW_SRC) {
624 cls_rule_set_nw_src_masked(&fm->cr, 0, 0);
625 } else if (f->index == F_NW_DST) {
626 cls_rule_set_nw_dst_masked(&fm->cr, 0, 0);
627 } else if (f->index == F_DL_VLAN) {
628 cls_rule_set_any_vid(&fm->cr);
629 } else if (f->index == F_DL_VLAN_PCP) {
630 cls_rule_set_any_pcp(&fm->cr);
635 parse_field_value(&fm->cr, f->index, value);
637 } else if (!strncmp(name, "reg", 3) && isdigit(name[3])) {
638 unsigned int reg_idx = atoi(name + 3);
639 if (reg_idx >= FLOW_N_REGS) {
640 ovs_fatal(0, "only %d registers supported", FLOW_N_REGS);
642 parse_reg_value(&fm->cr, reg_idx, value);
644 ovs_fatal(0, "unknown keyword %s", name);
650 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
651 * (one of OFPFC_*) and appends the parsed OpenFlow message to 'packets'.
652 * '*cur_format' should initially contain the flow format currently configured
653 * on the connection; this function will add a message to change the flow
654 * format and update '*cur_format', if this is necessary to add the parsed
657 parse_ofp_flow_mod_str(struct list *packets, enum nx_flow_format *cur_format,
658 char *string, uint16_t command)
660 bool is_del = command == OFPFC_DELETE || command == OFPFC_DELETE_STRICT;
661 enum nx_flow_format min_format, next_format;
662 struct ofpbuf actions;
666 ofpbuf_init(&actions, 64);
667 parse_ofp_str(&fm, NULL, is_del ? NULL : &actions, string);
668 fm.command = command;
670 min_format = ofputil_min_flow_format(&fm.cr, true, fm.cookie);
671 next_format = MAX(*cur_format, min_format);
672 if (next_format != *cur_format) {
673 struct ofpbuf *sff = ofputil_make_set_flow_format(next_format);
674 list_push_back(packets, &sff->list_node);
675 *cur_format = next_format;
678 ofm = ofputil_encode_flow_mod(&fm, *cur_format);
679 list_push_back(packets, &ofm->list_node);
681 ofpbuf_uninit(&actions);
684 /* Similar to parse_ofp_flow_mod_str(), except that the string is read from
685 * 'stream' and the command is always OFPFC_ADD. Returns false if end-of-file
686 * is reached before reading a flow, otherwise true. */
688 parse_ofp_add_flow_file(struct list *packets, enum nx_flow_format *cur,
691 struct ds s = DS_EMPTY_INITIALIZER;
694 while (!ds_get_line(&s, stream)) {
695 char *line = ds_cstr(&s);
698 /* Delete comments. */
699 comment = strchr(line, '#');
704 /* Drop empty lines. */
705 if (line[strspn(line, " \t\n")] == '\0') {
709 parse_ofp_flow_mod_str(packets, cur, line, OFPFC_ADD);
719 parse_ofp_flow_stats_request_str(struct flow_stats_request *fsr,
720 bool aggregate, char *string)
725 parse_ofp_str(&fm, &table_id, NULL, string);
726 fsr->aggregate = aggregate;
728 fsr->out_port = fm.out_port;
729 fsr->table_id = table_id;