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 || !strcasecmp(act, "set_tunnel64")) {
262 uint64_t tun_id = str_to_u64(arg);
263 if (!strcasecmp(act, "set_tunnel64") || tun_id > UINT32_MAX) {
264 struct nx_action_set_tunnel64 *nast64;
265 nast64 = put_action(b, sizeof *nast64, OFPAT_VENDOR);
266 nast64->vendor = htonl(NX_VENDOR_ID);
267 nast64->subtype = htons(NXAST_SET_TUNNEL64);
268 nast64->tun_id = htonll(tun_id);
270 struct nx_action_set_tunnel *nast;
271 nast = put_action(b, sizeof *nast, OFPAT_VENDOR);
272 nast->vendor = htonl(NX_VENDOR_ID);
273 nast->subtype = htons(NXAST_SET_TUNNEL);
274 nast->tun_id = htonl(tun_id);
276 } else if (!strcasecmp(act, "drop_spoofed_arp")) {
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_DROP_SPOOFED_ARP);
281 } else if (!strcasecmp(act, "set_queue")) {
282 struct nx_action_set_queue *nasq;
283 nasq = put_action(b, sizeof *nasq, OFPAT_VENDOR);
284 nasq->vendor = htonl(NX_VENDOR_ID);
285 nasq->subtype = htons(NXAST_SET_QUEUE);
286 nasq->queue_id = htonl(str_to_u32(arg));
287 } else if (!strcasecmp(act, "pop_queue")) {
288 struct nx_action_header *nah;
289 nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
290 nah->vendor = htonl(NX_VENDOR_ID);
291 nah->subtype = htons(NXAST_POP_QUEUE);
292 } else if (!strcasecmp(act, "note")) {
293 size_t start_ofs = b->size;
294 struct nx_action_note *nan;
298 nan = put_action(b, sizeof *nan, OFPAT_VENDOR);
299 nan->vendor = htonl(NX_VENDOR_ID);
300 nan->subtype = htons(NXAST_NOTE);
302 b->size -= sizeof nan->note;
303 while (arg && *arg != '\0') {
314 byte = hexits_value(arg, 2, &ok);
316 ovs_fatal(0, "bad hex digit in `note' argument");
318 ofpbuf_put(b, &byte, 1);
323 len = b->size - start_ofs;
324 remainder = len % OFP_ACTION_ALIGN;
326 ofpbuf_put_zeros(b, OFP_ACTION_ALIGN - remainder);
328 nan->len = htons(b->size - start_ofs);
329 } else if (!strcasecmp(act, "move")) {
330 struct nx_action_reg_move *move;
331 move = ofpbuf_put_uninit(b, sizeof *move);
332 nxm_parse_reg_move(move, arg);
333 } else if (!strcasecmp(act, "load")) {
334 struct nx_action_reg_load *load;
335 load = ofpbuf_put_uninit(b, sizeof *load);
336 nxm_parse_reg_load(load, arg);
337 } else if (!strcasecmp(act, "output")) {
338 put_output_action(b, str_to_u32(arg));
339 } else if (!strcasecmp(act, "enqueue")) {
341 char *port_s = strtok_r(arg, ":q", &sp);
342 char *queue = strtok_r(NULL, "", &sp);
343 if (port_s == NULL || queue == NULL) {
344 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
346 put_enqueue_action(b, str_to_u32(port_s), str_to_u32(queue));
347 } else if (!strcasecmp(act, "drop")) {
348 /* A drop action in OpenFlow occurs by just not setting
352 ovs_fatal(0, "Drop actions must not be preceded by other "
355 } else if (!strcasecmp(act, "CONTROLLER")) {
356 struct ofp_action_output *oao;
357 oao = put_output_action(b, OFPP_CONTROLLER);
359 /* Unless a numeric argument is specified, we send the whole
360 * packet to the controller. */
361 if (arg && (strspn(arg, "0123456789") == strlen(arg))) {
362 oao->max_len = htons(str_to_u32(arg));
364 oao->max_len = htons(UINT16_MAX);
366 } else if (parse_port_name(act, &port)) {
367 put_output_action(b, port);
368 } else if (strspn(act, "0123456789") == strlen(act)) {
369 put_output_action(b, str_to_u32(act));
371 ovs_fatal(0, "Unknown action: %s", act);
383 parse_protocol(const char *name, const struct protocol **p_out)
385 static const struct protocol protocols[] = {
386 { "ip", ETH_TYPE_IP, 0 },
387 { "arp", ETH_TYPE_ARP, 0 },
388 { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
389 { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
390 { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
392 const struct protocol *p;
394 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
395 if (!strcmp(p->name, name)) {
405 FIELD(F_TUN_ID, "tun_id", FWW_TUN_ID) \
406 FIELD(F_IN_PORT, "in_port", FWW_IN_PORT) \
407 FIELD(F_DL_VLAN, "dl_vlan", 0) \
408 FIELD(F_DL_VLAN_PCP, "dl_vlan_pcp", 0) \
409 FIELD(F_DL_SRC, "dl_src", FWW_DL_SRC) \
410 FIELD(F_DL_DST, "dl_dst", FWW_DL_DST) \
411 FIELD(F_DL_TYPE, "dl_type", FWW_DL_TYPE) \
412 FIELD(F_NW_SRC, "nw_src", 0) \
413 FIELD(F_NW_DST, "nw_dst", 0) \
414 FIELD(F_NW_PROTO, "nw_proto", FWW_NW_PROTO) \
415 FIELD(F_NW_TOS, "nw_tos", FWW_NW_TOS) \
416 FIELD(F_TP_SRC, "tp_src", FWW_TP_SRC) \
417 FIELD(F_TP_DST, "tp_dst", FWW_TP_DST) \
418 FIELD(F_ICMP_TYPE, "icmp_type", FWW_TP_SRC) \
419 FIELD(F_ICMP_CODE, "icmp_code", FWW_TP_DST)
422 #define FIELD(ENUM, NAME, WILDCARD) ENUM,
429 enum field_index index;
431 flow_wildcards_t wildcard; /* FWW_* bit. */
435 parse_field_name(const char *name, const struct field **f_out)
437 static const struct field fields[N_FIELDS] = {
438 #define FIELD(ENUM, NAME, WILDCARD) { ENUM, NAME, WILDCARD },
442 const struct field *f;
444 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
445 if (!strcmp(f->name, name)) {
455 parse_field_value(struct cls_rule *rule, enum field_index index,
458 uint8_t mac[ETH_ADDR_LEN];
464 cls_rule_set_tun_id(rule, htonll(str_to_u64(value)));
468 if (!parse_port_name(value, &port_no)) {
469 port_no = atoi(value);
471 if (port_no == OFPP_LOCAL) {
472 port_no = ODPP_LOCAL;
474 cls_rule_set_in_port(rule, port_no);
478 cls_rule_set_dl_vlan(rule, htons(str_to_u32(value)));
482 cls_rule_set_dl_vlan_pcp(rule, str_to_u32(value));
486 str_to_mac(value, mac);
487 cls_rule_set_dl_src(rule, mac);
491 str_to_mac(value, mac);
492 cls_rule_set_dl_dst(rule, mac);
496 cls_rule_set_dl_type(rule, htons(str_to_u32(value)));
500 str_to_ip(value, &ip, &mask);
501 cls_rule_set_nw_src_masked(rule, ip, mask);
505 str_to_ip(value, &ip, &mask);
506 cls_rule_set_nw_dst_masked(rule, ip, mask);
510 cls_rule_set_nw_proto(rule, str_to_u32(value));
514 cls_rule_set_nw_tos(rule, str_to_u32(value));
518 cls_rule_set_tp_src(rule, htons(str_to_u32(value)));
522 cls_rule_set_tp_dst(rule, htons(str_to_u32(value)));
526 cls_rule_set_icmp_type(rule, str_to_u32(value));
530 cls_rule_set_icmp_code(rule, str_to_u32(value));
539 parse_reg_value(struct cls_rule *rule, int reg_idx, const char *value)
541 uint32_t reg_value, reg_mask;
543 if (!strcmp(value, "ANY") || !strcmp(value, "*")) {
544 cls_rule_set_reg_masked(rule, reg_idx, 0, 0);
545 } else if (sscanf(value, "%"SCNi32"/%"SCNi32,
546 ®_value, ®_mask) == 2) {
547 cls_rule_set_reg_masked(rule, reg_idx, reg_value, reg_mask);
548 } else if (sscanf(value, "%"SCNi32, ®_value)) {
549 cls_rule_set_reg(rule, reg_idx, reg_value);
551 ovs_fatal(0, "register fields must take the form <value> "
552 "or <value>/<mask>");
556 /* Convert 'string' (as described in the Flow Syntax section of the ovs-ofctl
557 * man page) into 'pf'. If 'actions' is specified, an action must be in
558 * 'string' and may be expanded or reallocated. */
560 parse_ofp_str(struct flow_mod *fm, uint8_t *table_idx,
561 struct ofpbuf *actions, char *string)
563 char *save_ptr = NULL;
569 cls_rule_init_catchall(&fm->cr, OFP_DEFAULT_PRIORITY);
570 fm->cookie = htonll(0);
571 fm->command = UINT16_MAX;
572 fm->idle_timeout = OFP_FLOW_PERMANENT;
573 fm->hard_timeout = OFP_FLOW_PERMANENT;
574 fm->buffer_id = UINT32_MAX;
575 fm->out_port = OFPP_NONE;
578 char *act_str = strstr(string, "action");
580 ovs_fatal(0, "must specify an action");
584 act_str = strchr(act_str + 1, '=');
586 ovs_fatal(0, "must specify an action");
591 str_to_action(act_str, actions);
592 fm->actions = actions->data;
593 fm->n_actions = actions->size / sizeof(union ofp_action);
598 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
599 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
600 const struct protocol *p;
602 if (parse_protocol(name, &p)) {
603 cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
605 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
608 const struct field *f;
611 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
613 ovs_fatal(0, "field %s missing value", name);
616 if (table_idx && !strcmp(name, "table")) {
617 *table_idx = atoi(value);
618 } else if (!strcmp(name, "out_port")) {
619 fm->out_port = atoi(value);
620 } else if (!strcmp(name, "priority")) {
621 fm->cr.priority = atoi(value);
622 } else if (!strcmp(name, "idle_timeout")) {
623 fm->idle_timeout = atoi(value);
624 } else if (!strcmp(name, "hard_timeout")) {
625 fm->hard_timeout = atoi(value);
626 } else if (!strcmp(name, "cookie")) {
627 fm->cookie = htonll(str_to_u64(value));
628 } else if (parse_field_name(name, &f)) {
629 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
631 fm->cr.wc.wildcards |= f->wildcard;
632 cls_rule_zero_wildcarded_fields(&fm->cr);
633 } else if (f->index == F_NW_SRC) {
634 cls_rule_set_nw_src_masked(&fm->cr, 0, 0);
635 } else if (f->index == F_NW_DST) {
636 cls_rule_set_nw_dst_masked(&fm->cr, 0, 0);
637 } else if (f->index == F_DL_VLAN) {
638 cls_rule_set_any_vid(&fm->cr);
639 } else if (f->index == F_DL_VLAN_PCP) {
640 cls_rule_set_any_pcp(&fm->cr);
645 parse_field_value(&fm->cr, f->index, value);
647 } else if (!strncmp(name, "reg", 3) && isdigit(name[3])) {
648 unsigned int reg_idx = atoi(name + 3);
649 if (reg_idx >= FLOW_N_REGS) {
650 ovs_fatal(0, "only %d registers supported", FLOW_N_REGS);
652 parse_reg_value(&fm->cr, reg_idx, value);
654 ovs_fatal(0, "unknown keyword %s", name);
660 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
661 * (one of OFPFC_*) and appends the parsed OpenFlow message to 'packets'.
662 * '*cur_format' should initially contain the flow format currently configured
663 * on the connection; this function will add a message to change the flow
664 * format and update '*cur_format', if this is necessary to add the parsed
667 parse_ofp_flow_mod_str(struct list *packets, enum nx_flow_format *cur_format,
668 char *string, uint16_t command)
670 bool is_del = command == OFPFC_DELETE || command == OFPFC_DELETE_STRICT;
671 enum nx_flow_format min_format, next_format;
672 struct ofpbuf actions;
676 ofpbuf_init(&actions, 64);
677 parse_ofp_str(&fm, NULL, is_del ? NULL : &actions, string);
678 fm.command = command;
680 min_format = ofputil_min_flow_format(&fm.cr, true, fm.cookie);
681 next_format = MAX(*cur_format, min_format);
682 if (next_format != *cur_format) {
683 struct ofpbuf *sff = ofputil_make_set_flow_format(next_format);
684 list_push_back(packets, &sff->list_node);
685 *cur_format = next_format;
688 ofm = ofputil_encode_flow_mod(&fm, *cur_format);
689 list_push_back(packets, &ofm->list_node);
691 ofpbuf_uninit(&actions);
694 /* Similar to parse_ofp_flow_mod_str(), except that the string is read from
695 * 'stream' and the command is always OFPFC_ADD. Returns false if end-of-file
696 * is reached before reading a flow, otherwise true. */
698 parse_ofp_add_flow_file(struct list *packets, enum nx_flow_format *cur,
701 struct ds s = DS_EMPTY_INITIALIZER;
704 while (!ds_get_line(&s, stream)) {
705 char *line = ds_cstr(&s);
708 /* Delete comments. */
709 comment = strchr(line, '#');
714 /* Drop empty lines. */
715 if (line[strspn(line, " \t\n")] == '\0') {
719 parse_ofp_flow_mod_str(packets, cur, line, OFPFC_ADD);
729 parse_ofp_flow_stats_request_str(struct flow_stats_request *fsr,
730 bool aggregate, char *string)
735 parse_ofp_str(&fm, &table_id, NULL, string);
736 fsr->aggregate = aggregate;
738 fsr->out_port = fm.out_port;
739 fsr->table_id = table_id;