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"
24 #include "byte-order.h"
25 #include "dynamic-string.h"
29 #include "openflow/openflow.h"
31 #include "socket-util.h"
35 VLOG_DEFINE_THIS_MODULE(ofp_parse);
38 str_to_u32(const char *str)
44 ovs_fatal(0, "missing required numeric argument");
48 value = strtoul(str, &tail, 0);
49 if (errno == EINVAL || errno == ERANGE || *tail) {
50 ovs_fatal(0, "invalid numeric format %s", str);
56 str_to_u64(const char *str)
62 value = strtoull(str, &tail, 0);
63 if (errno == EINVAL || errno == ERANGE || *tail) {
64 ovs_fatal(0, "invalid numeric format %s", str);
70 str_to_mac(const char *str, uint8_t mac[6])
72 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
73 != ETH_ADDR_SCAN_COUNT) {
74 ovs_fatal(0, "invalid mac address %s", str);
79 str_to_ip(const char *str_, ovs_be32 *ip, ovs_be32 *maskp)
81 char *str = xstrdup(str_);
82 char *save_ptr = NULL;
83 const char *name, *netmask;
84 struct in_addr in_addr;
88 name = strtok_r(str, "/", &save_ptr);
89 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
91 ovs_fatal(0, "%s: could not convert to IP address", str);
95 netmask = strtok_r(NULL, "/", &save_ptr);
98 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
99 &o[0], &o[1], &o[2], &o[3]) == 4) {
100 mask = htonl((o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3]);
102 int prefix = atoi(netmask);
103 if (prefix <= 0 || prefix > 32) {
104 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
106 } else if (prefix == 32) {
107 mask = htonl(UINT32_MAX);
109 mask = htonl(((1u << prefix) - 1) << (32 - prefix));
113 mask = htonl(UINT32_MAX);
120 if (mask != htonl(UINT32_MAX)) {
121 ovs_fatal(0, "%s: netmask not allowed here", str_);
129 put_action(struct ofpbuf *b, size_t size, uint16_t type)
131 struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
132 ah->type = htons(type);
133 ah->len = htons(size);
137 static struct ofp_action_output *
138 put_output_action(struct ofpbuf *b, uint16_t port)
140 struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
141 oao->port = htons(port);
146 put_enqueue_action(struct ofpbuf *b, uint16_t port, uint32_t queue)
148 struct ofp_action_enqueue *oae = put_action(b, sizeof *oae, OFPAT_ENQUEUE);
149 oae->port = htons(port);
150 oae->queue_id = htonl(queue);
154 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
156 struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
157 str_to_mac(addr, oada->dl_addr);
162 parse_port_name(const char *name, uint16_t *port)
168 static const struct pair pairs[] = {
169 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
175 DEF_PAIR(CONTROLLER),
180 static const int n_pairs = ARRAY_SIZE(pairs);
183 for (i = 0; i < n_pairs; i++) {
184 if (!strcasecmp(name, pairs[i].name)) {
185 *port = pairs[i].value;
193 str_to_action(char *str, struct ofpbuf *b)
196 char *saveptr = NULL;
200 for (act = strtok_r(str, ", \t\r\n", &saveptr), n_actions = 0; act;
201 act = strtok_r(NULL, ", \t\r\n", &saveptr), n_actions++)
206 ovs_fatal(0, "Drop actions must not be followed by other actions");
209 /* Arguments are separated by colons */
210 arg = strchr(act, ':');
216 if (!strcasecmp(act, "mod_vlan_vid")) {
217 struct ofp_action_vlan_vid *va;
218 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
219 va->vlan_vid = htons(str_to_u32(arg));
220 } else if (!strcasecmp(act, "mod_vlan_pcp")) {
221 struct ofp_action_vlan_pcp *va;
222 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
223 va->vlan_pcp = str_to_u32(arg);
224 } else if (!strcasecmp(act, "strip_vlan")) {
225 struct ofp_action_header *ah;
226 ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
227 ah->type = htons(OFPAT_STRIP_VLAN);
228 } else if (!strcasecmp(act, "mod_dl_src")) {
229 put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
230 } else if (!strcasecmp(act, "mod_dl_dst")) {
231 put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
232 } else if (!strcasecmp(act, "mod_nw_src")) {
233 struct ofp_action_nw_addr *na;
234 na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
235 str_to_ip(arg, &na->nw_addr, NULL);
236 } else if (!strcasecmp(act, "mod_nw_dst")) {
237 struct ofp_action_nw_addr *na;
238 na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
239 str_to_ip(arg, &na->nw_addr, NULL);
240 } else if (!strcasecmp(act, "mod_tp_src")) {
241 struct ofp_action_tp_port *ta;
242 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
243 ta->tp_port = htons(str_to_u32(arg));
244 } else if (!strcasecmp(act, "mod_tp_dst")) {
245 struct ofp_action_tp_port *ta;
246 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
247 ta->tp_port = htons(str_to_u32(arg));
248 } else if (!strcasecmp(act, "mod_nw_tos")) {
249 struct ofp_action_nw_tos *nt;
250 nt = put_action(b, sizeof *nt, OFPAT_SET_NW_TOS);
251 nt->nw_tos = str_to_u32(arg);
252 } else if (!strcasecmp(act, "resubmit")) {
253 struct nx_action_resubmit *nar;
254 nar = put_action(b, sizeof *nar, OFPAT_VENDOR);
255 nar->vendor = htonl(NX_VENDOR_ID);
256 nar->subtype = htons(NXAST_RESUBMIT);
257 nar->in_port = htons(str_to_u32(arg));
258 } else if (!strcasecmp(act, "set_tunnel")) {
259 struct nx_action_set_tunnel *nast;
260 nast = put_action(b, sizeof *nast, OFPAT_VENDOR);
261 nast->vendor = htonl(NX_VENDOR_ID);
262 nast->subtype = htons(NXAST_SET_TUNNEL);
263 nast->tun_id = htonl(str_to_u32(arg));
264 } else if (!strcasecmp(act, "drop_spoofed_arp")) {
265 struct nx_action_header *nah;
266 nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
267 nah->vendor = htonl(NX_VENDOR_ID);
268 nah->subtype = htons(NXAST_DROP_SPOOFED_ARP);
269 } else if (!strcasecmp(act, "set_queue")) {
270 struct nx_action_set_queue *nasq;
271 nasq = put_action(b, sizeof *nasq, OFPAT_VENDOR);
272 nasq->vendor = htonl(NX_VENDOR_ID);
273 nasq->subtype = htons(NXAST_SET_QUEUE);
274 nasq->queue_id = htonl(str_to_u32(arg));
275 } else if (!strcasecmp(act, "pop_queue")) {
276 struct nx_action_header *nah;
277 nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
278 nah->vendor = htonl(NX_VENDOR_ID);
279 nah->subtype = htons(NXAST_POP_QUEUE);
280 } else if (!strcasecmp(act, "output")) {
281 put_output_action(b, str_to_u32(arg));
282 } else if (!strcasecmp(act, "enqueue")) {
284 char *port_s = strtok_r(arg, ":q", &sp);
285 char *queue = strtok_r(NULL, "", &sp);
286 if (port_s == NULL || queue == NULL) {
287 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
289 put_enqueue_action(b, str_to_u32(port_s), str_to_u32(queue));
290 } else if (!strcasecmp(act, "drop")) {
291 /* A drop action in OpenFlow occurs by just not setting
295 ovs_fatal(0, "Drop actions must not be preceded by other "
298 } else if (!strcasecmp(act, "CONTROLLER")) {
299 struct ofp_action_output *oao;
300 oao = put_output_action(b, OFPP_CONTROLLER);
302 /* Unless a numeric argument is specified, we send the whole
303 * packet to the controller. */
304 if (arg && (strspn(arg, "0123456789") == strlen(arg))) {
305 oao->max_len = htons(str_to_u32(arg));
307 oao->max_len = htons(UINT16_MAX);
309 } else if (parse_port_name(act, &port)) {
310 put_output_action(b, port);
311 } else if (strspn(act, "0123456789") == strlen(act)) {
312 put_output_action(b, str_to_u32(act));
314 ovs_fatal(0, "Unknown action: %s", act);
326 parse_protocol(const char *name, const struct protocol **p_out)
328 static const struct protocol protocols[] = {
329 { "ip", ETH_TYPE_IP, 0 },
330 { "arp", ETH_TYPE_ARP, 0 },
331 { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
332 { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
333 { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
335 const struct protocol *p;
337 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
338 if (!strcmp(p->name, name)) {
348 FIELD(F_IN_PORT, "in_port", OFPFW_IN_PORT) \
349 FIELD(F_DL_VLAN, "dl_vlan", OFPFW_DL_VLAN) \
350 FIELD(F_DL_VLAN_PCP, "dl_vlan_pcp", OFPFW_DL_VLAN_PCP) \
351 FIELD(F_DL_SRC, "dl_src", OFPFW_DL_SRC) \
352 FIELD(F_DL_DST, "dl_dst", OFPFW_DL_DST) \
353 FIELD(F_DL_TYPE, "dl_type", OFPFW_DL_TYPE) \
354 FIELD(F_NW_SRC, "nw_src", 0) \
355 FIELD(F_NW_DST, "nw_dst", 0) \
356 FIELD(F_NW_PROTO, "nw_proto", OFPFW_NW_PROTO) \
357 FIELD(F_NW_TOS, "nw_tos", OFPFW_NW_TOS) \
358 FIELD(F_TP_SRC, "tp_src", OFPFW_TP_SRC) \
359 FIELD(F_TP_DST, "tp_dst", OFPFW_TP_DST) \
360 FIELD(F_ICMP_TYPE, "icmp_type", OFPFW_ICMP_TYPE) \
361 FIELD(F_ICMP_CODE, "icmp_code", OFPFW_ICMP_CODE)
364 #define FIELD(ENUM, NAME, WILDCARD) ENUM,
371 enum field_index index;
377 parse_field_name(const char *name, const struct field **f_out)
379 static const struct field fields[N_FIELDS] = {
380 #define FIELD(ENUM, NAME, WILDCARD) { ENUM, NAME, WILDCARD },
384 const struct field *f;
386 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
387 if (!strcmp(f->name, name)) {
397 parse_field_value(struct cls_rule *rule, enum field_index index,
400 uint8_t mac[ETH_ADDR_LEN];
406 if (!parse_port_name(value, &port_no)) {
407 port_no = atoi(value);
409 if (port_no == OFPP_LOCAL) {
410 port_no = ODPP_LOCAL;
412 cls_rule_set_in_port(rule, port_no);
416 cls_rule_set_dl_vlan(rule, htons(str_to_u32(value)));
420 cls_rule_set_dl_vlan_pcp(rule, str_to_u32(value));
424 str_to_mac(value, mac);
425 cls_rule_set_dl_src(rule, mac);
429 str_to_mac(value, mac);
430 cls_rule_set_dl_dst(rule, mac);
434 cls_rule_set_dl_type(rule, htons(str_to_u32(value)));
438 str_to_ip(value, &ip, &mask);
439 cls_rule_set_nw_src_masked(rule, ip, mask);
443 str_to_ip(value, &ip, &mask);
444 cls_rule_set_nw_dst_masked(rule, ip, mask);
448 cls_rule_set_nw_proto(rule, str_to_u32(value));
452 cls_rule_set_nw_tos(rule, str_to_u32(value));
456 cls_rule_set_tp_src(rule, htons(str_to_u32(value)));
460 cls_rule_set_tp_dst(rule, htons(str_to_u32(value)));
464 cls_rule_set_icmp_type(rule, str_to_u32(value));
468 cls_rule_set_icmp_code(rule, str_to_u32(value));
476 /* Convert 'string' (as described in the Flow Syntax section of the ovs-ofctl
477 * man page) into 'pf'. If 'actions' is specified, an action must be in
478 * 'string' and may be expanded or reallocated. */
480 parse_ofp_str(struct parsed_flow *pf, struct ofpbuf *actions, char *string)
482 char *save_ptr = NULL;
485 cls_rule_init_catchall(&pf->rule, OFP_DEFAULT_PRIORITY);
486 pf->table_idx = 0xff;
487 pf->out_port = OFPP_NONE;
488 pf->idle_timeout = OFP_FLOW_PERMANENT;
489 pf->hard_timeout = OFP_FLOW_PERMANENT;
492 char *act_str = strstr(string, "action");
494 ovs_fatal(0, "must specify an action");
498 act_str = strchr(act_str + 1, '=');
500 ovs_fatal(0, "must specify an action");
505 str_to_action(act_str, actions);
507 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
508 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
509 const struct protocol *p;
511 if (parse_protocol(name, &p)) {
512 cls_rule_set_dl_type(&pf->rule, htons(p->dl_type));
514 cls_rule_set_nw_proto(&pf->rule, p->nw_proto);
517 const struct field *f;
520 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
522 ovs_fatal(0, "field %s missing value", name);
525 if (!strcmp(name, "table")) {
526 pf->table_idx = atoi(value);
527 } else if (!strcmp(name, "out_port")) {
528 pf->out_port = atoi(value);
529 } else if (!strcmp(name, "priority")) {
530 pf->rule.priority = atoi(value);
531 } else if (!strcmp(name, "idle_timeout")) {
532 pf->idle_timeout = atoi(value);
533 } else if (!strcmp(name, "hard_timeout")) {
534 pf->hard_timeout = atoi(value);
535 } else if (!strcmp(name, "cookie")) {
536 pf->cookie = str_to_u64(value);
537 } else if (parse_field_name(name, &f)) {
538 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
540 pf->rule.wc.wildcards |= f->wildcard;
541 cls_rule_zero_wildcarded_fields(&pf->rule);
542 } else if (f->index == F_NW_SRC) {
543 cls_rule_set_nw_src_masked(&pf->rule, 0, 0);
544 } else if (f->index == F_NW_DST) {
545 cls_rule_set_nw_dst_masked(&pf->rule, 0, 0);
550 parse_field_value(&pf->rule, f->index, value);
553 ovs_fatal(0, "unknown keyword %s", name);
559 /* Parses 'string' as an OFPT_FLOW_MOD with command 'command' (one of OFPFC_*)
560 * and returns an ofpbuf that contains it. */
562 parse_ofp_flow_mod_str(char *string, uint16_t command)
564 struct parsed_flow pf;
565 struct ofpbuf *buffer;
566 struct ofp_flow_mod *ofm;
568 /* parse_ofp_str() will expand and reallocate the data in 'buffer', so we
569 * can't keep pointers to across the parse_ofp_str() call. */
570 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
571 parse_ofp_str(&pf, buffer, string);
574 flow_to_match(&pf.rule.flow, pf.rule.wc.wildcards, NXFF_OPENFLOW10,
576 ofm->command = htons(command);
577 ofm->cookie = htonll(pf.cookie);
578 ofm->idle_timeout = htons(pf.idle_timeout);
579 ofm->hard_timeout = htons(pf.hard_timeout);
580 ofm->buffer_id = htonl(UINT32_MAX);
581 ofm->out_port = htons(pf.out_port);
582 ofm->priority = htons(pf.rule.priority);
583 update_openflow_length(buffer);
588 /* Parses an OFPT_FLOW_MOD with subtype OFPFC_ADD from 'stream' and returns an
589 * ofpbuf that contains it. Returns a null pointer if end-of-file is reached
590 * before reading a flow. */
592 parse_ofp_add_flow_file(FILE *stream)
594 struct ofpbuf *b = NULL;
595 struct ds s = DS_EMPTY_INITIALIZER;
597 while (!ds_get_line(&s, stream)) {
598 char *line = ds_cstr(&s);
601 /* Delete comments. */
602 comment = strchr(line, '#');
607 /* Drop empty lines. */
608 if (line[strspn(line, " \t\n")] == '\0') {
612 b = parse_ofp_flow_mod_str(line, OFPFC_ADD);