]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ofp-parse.c
ofp-parse: Generalize parse_ofp_add_flow_file() to parse_ofp_flow_mod_file().
[mirror_ovs.git] / lib / ofp-parse.c
1 /*
2 * Copyright (c) 2010, 2011 Nicira Networks.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18
19 #include "ofp-parse.h"
20
21 #include <ctype.h>
22 #include <errno.h>
23 #include <stdlib.h>
24
25 #include "byte-order.h"
26 #include "dynamic-string.h"
27 #include "netdev.h"
28 #include "multipath.h"
29 #include "nx-match.h"
30 #include "ofp-util.h"
31 #include "ofpbuf.h"
32 #include "openflow/openflow.h"
33 #include "packets.h"
34 #include "socket-util.h"
35 #include "vconn.h"
36 #include "vlog.h"
37
38 VLOG_DEFINE_THIS_MODULE(ofp_parse);
39
40 static uint32_t
41 str_to_u32(const char *str)
42 {
43 char *tail;
44 uint32_t value;
45
46 if (!str[0]) {
47 ovs_fatal(0, "missing required numeric argument");
48 }
49
50 errno = 0;
51 value = strtoul(str, &tail, 0);
52 if (errno == EINVAL || errno == ERANGE || *tail) {
53 ovs_fatal(0, "invalid numeric format %s", str);
54 }
55 return value;
56 }
57
58 static uint64_t
59 str_to_u64(const char *str)
60 {
61 char *tail;
62 uint64_t value;
63
64 if (!str[0]) {
65 ovs_fatal(0, "missing required numeric argument");
66 }
67
68 errno = 0;
69 value = strtoull(str, &tail, 0);
70 if (errno == EINVAL || errno == ERANGE || *tail) {
71 ovs_fatal(0, "invalid numeric format %s", str);
72 }
73 return value;
74 }
75
76 static void
77 str_to_mac(const char *str, uint8_t mac[6])
78 {
79 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
80 != ETH_ADDR_SCAN_COUNT) {
81 ovs_fatal(0, "invalid mac address %s", str);
82 }
83 }
84
85 static void
86 str_to_ip(const char *str_, ovs_be32 *ip, ovs_be32 *maskp)
87 {
88 char *str = xstrdup(str_);
89 char *save_ptr = NULL;
90 const char *name, *netmask;
91 struct in_addr in_addr;
92 ovs_be32 mask;
93 int retval;
94
95 name = strtok_r(str, "/", &save_ptr);
96 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
97 if (retval) {
98 ovs_fatal(0, "%s: could not convert to IP address", str);
99 }
100 *ip = in_addr.s_addr;
101
102 netmask = strtok_r(NULL, "/", &save_ptr);
103 if (netmask) {
104 uint8_t o[4];
105 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
106 &o[0], &o[1], &o[2], &o[3]) == 4) {
107 mask = htonl((o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3]);
108 } else {
109 int prefix = atoi(netmask);
110 if (prefix <= 0 || prefix > 32) {
111 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
112 str);
113 } else if (prefix == 32) {
114 mask = htonl(UINT32_MAX);
115 } else {
116 mask = htonl(((1u << prefix) - 1) << (32 - prefix));
117 }
118 }
119 } else {
120 mask = htonl(UINT32_MAX);
121 }
122 *ip &= mask;
123
124 if (maskp) {
125 *maskp = mask;
126 } else {
127 if (mask != htonl(UINT32_MAX)) {
128 ovs_fatal(0, "%s: netmask not allowed here", str_);
129 }
130 }
131
132 free(str);
133 }
134
135 static void
136 str_to_tun_id(const char *str, ovs_be64 *tun_idp, ovs_be64 *maskp)
137 {
138 uint64_t tun_id, mask;
139 char *tail;
140
141 errno = 0;
142 tun_id = strtoull(str, &tail, 0);
143 if (errno || (*tail != '\0' && *tail != '/')) {
144 goto error;
145 }
146
147 if (*tail == '/') {
148 mask = strtoull(tail + 1, &tail, 0);
149 if (errno || *tail != '\0') {
150 goto error;
151 }
152 } else {
153 mask = UINT64_MAX;
154 }
155
156 *tun_idp = htonll(tun_id);
157 *maskp = htonll(mask);
158 return;
159
160 error:
161 ovs_fatal(0, "%s: bad syntax for tunnel id", str);
162 }
163
164 static void
165 str_to_ipv6(const char *str_, struct in6_addr *addrp, struct in6_addr *maskp)
166 {
167 char *str = xstrdup(str_);
168 char *save_ptr = NULL;
169 const char *name, *netmask;
170 struct in6_addr addr, mask;
171 int retval;
172
173 name = strtok_r(str, "/", &save_ptr);
174 retval = name ? lookup_ipv6(name, &addr) : EINVAL;
175 if (retval) {
176 ovs_fatal(0, "%s: could not convert to IPv6 address", str);
177 }
178
179 netmask = strtok_r(NULL, "/", &save_ptr);
180 if (netmask) {
181 int prefix = atoi(netmask);
182 if (prefix <= 0 || prefix > 128) {
183 ovs_fatal(0, "%s: network prefix bits not between 1 and 128",
184 str);
185 } else {
186 mask = ipv6_create_mask(prefix);
187 }
188 } else {
189 mask = in6addr_exact;
190 }
191 *addrp = ipv6_addr_bitand(&addr, &mask);
192
193 if (maskp) {
194 *maskp = mask;
195 } else {
196 if (!ipv6_mask_is_exact(&mask)) {
197 ovs_fatal(0, "%s: netmask not allowed here", str_);
198 }
199 }
200
201 free(str);
202 }
203
204 static void *
205 put_action(struct ofpbuf *b, size_t size, uint16_t type)
206 {
207 struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
208 ah->type = htons(type);
209 ah->len = htons(size);
210 return ah;
211 }
212
213 static struct ofp_action_output *
214 put_output_action(struct ofpbuf *b, uint16_t port)
215 {
216 struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
217 oao->port = htons(port);
218 return oao;
219 }
220
221 static void
222 put_enqueue_action(struct ofpbuf *b, uint16_t port, uint32_t queue)
223 {
224 struct ofp_action_enqueue *oae = put_action(b, sizeof *oae, OFPAT_ENQUEUE);
225 oae->port = htons(port);
226 oae->queue_id = htonl(queue);
227 }
228
229 static void
230 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
231 {
232 struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
233 str_to_mac(addr, oada->dl_addr);
234 }
235
236
237 static bool
238 parse_port_name(const char *name, uint16_t *port)
239 {
240 struct pair {
241 const char *name;
242 uint16_t value;
243 };
244 static const struct pair pairs[] = {
245 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
246 DEF_PAIR(IN_PORT),
247 DEF_PAIR(TABLE),
248 DEF_PAIR(NORMAL),
249 DEF_PAIR(FLOOD),
250 DEF_PAIR(ALL),
251 DEF_PAIR(CONTROLLER),
252 DEF_PAIR(LOCAL),
253 DEF_PAIR(NONE),
254 #undef DEF_PAIR
255 };
256 static const int n_pairs = ARRAY_SIZE(pairs);
257 size_t i;
258
259 for (i = 0; i < n_pairs; i++) {
260 if (!strcasecmp(name, pairs[i].name)) {
261 *port = pairs[i].value;
262 return true;
263 }
264 }
265 return false;
266 }
267
268 static void
269 str_to_action(char *str, struct ofpbuf *b)
270 {
271 bool drop = false;
272 int n_actions;
273 char *pos;
274
275 pos = str;
276 n_actions = 0;
277 for (;;) {
278 char empty_string[] = "";
279 char *act, *arg;
280 size_t actlen;
281 uint16_t port;
282
283 pos += strspn(pos, ", \t\r\n");
284 if (*pos == '\0') {
285 break;
286 }
287
288 if (drop) {
289 ovs_fatal(0, "Drop actions must not be followed by other actions");
290 }
291
292 act = pos;
293 actlen = strcspn(pos, ":(, \t\r\n");
294 if (act[actlen] == ':') {
295 /* The argument can be separated by a colon. */
296 size_t arglen;
297
298 arg = act + actlen + 1;
299 arglen = strcspn(arg, ", \t\r\n");
300 pos = arg + arglen + (arg[arglen] != '\0');
301 arg[arglen] = '\0';
302 } else if (act[actlen] == '(') {
303 /* The argument can be surrounded by balanced parentheses. The
304 * outermost set of parentheses is removed. */
305 int level = 1;
306 size_t arglen;
307
308 arg = act + actlen + 1;
309 for (arglen = 0; level > 0; arglen++) {
310 switch (arg[arglen]) {
311 case '\0':
312 ovs_fatal(0, "unbalanced parentheses in argument to %s "
313 "action", act);
314
315 case '(':
316 level++;
317 break;
318
319 case ')':
320 level--;
321 break;
322 }
323 }
324 arg[arglen - 1] = '\0';
325 pos = arg + arglen;
326 } else {
327 /* There might be no argument at all. */
328 arg = empty_string;
329 pos = act + actlen + (act[actlen] != '\0');
330 }
331 act[actlen] = '\0';
332
333 if (!strcasecmp(act, "mod_vlan_vid")) {
334 struct ofp_action_vlan_vid *va;
335 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
336 va->vlan_vid = htons(str_to_u32(arg));
337 } else if (!strcasecmp(act, "mod_vlan_pcp")) {
338 struct ofp_action_vlan_pcp *va;
339 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
340 va->vlan_pcp = str_to_u32(arg);
341 } else if (!strcasecmp(act, "strip_vlan")) {
342 struct ofp_action_header *ah;
343 ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
344 ah->type = htons(OFPAT_STRIP_VLAN);
345 } else if (!strcasecmp(act, "mod_dl_src")) {
346 put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
347 } else if (!strcasecmp(act, "mod_dl_dst")) {
348 put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
349 } else if (!strcasecmp(act, "mod_nw_src")) {
350 struct ofp_action_nw_addr *na;
351 na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
352 str_to_ip(arg, &na->nw_addr, NULL);
353 } else if (!strcasecmp(act, "mod_nw_dst")) {
354 struct ofp_action_nw_addr *na;
355 na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
356 str_to_ip(arg, &na->nw_addr, NULL);
357 } else if (!strcasecmp(act, "mod_tp_src")) {
358 struct ofp_action_tp_port *ta;
359 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
360 ta->tp_port = htons(str_to_u32(arg));
361 } else if (!strcasecmp(act, "mod_tp_dst")) {
362 struct ofp_action_tp_port *ta;
363 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
364 ta->tp_port = htons(str_to_u32(arg));
365 } else if (!strcasecmp(act, "mod_nw_tos")) {
366 struct ofp_action_nw_tos *nt;
367 nt = put_action(b, sizeof *nt, OFPAT_SET_NW_TOS);
368 nt->nw_tos = str_to_u32(arg);
369 } else if (!strcasecmp(act, "resubmit")) {
370 struct nx_action_resubmit *nar;
371 nar = put_action(b, sizeof *nar, OFPAT_VENDOR);
372 nar->vendor = htonl(NX_VENDOR_ID);
373 nar->subtype = htons(NXAST_RESUBMIT);
374 nar->in_port = htons(str_to_u32(arg));
375 } else if (!strcasecmp(act, "set_tunnel")
376 || !strcasecmp(act, "set_tunnel64")) {
377 uint64_t tun_id = str_to_u64(arg);
378 if (!strcasecmp(act, "set_tunnel64") || tun_id > UINT32_MAX) {
379 struct nx_action_set_tunnel64 *nast64;
380 nast64 = put_action(b, sizeof *nast64, OFPAT_VENDOR);
381 nast64->vendor = htonl(NX_VENDOR_ID);
382 nast64->subtype = htons(NXAST_SET_TUNNEL64);
383 nast64->tun_id = htonll(tun_id);
384 } else {
385 struct nx_action_set_tunnel *nast;
386 nast = put_action(b, sizeof *nast, OFPAT_VENDOR);
387 nast->vendor = htonl(NX_VENDOR_ID);
388 nast->subtype = htons(NXAST_SET_TUNNEL);
389 nast->tun_id = htonl(tun_id);
390 }
391 } else if (!strcasecmp(act, "drop_spoofed_arp")) {
392 struct nx_action_header *nah;
393 nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
394 nah->vendor = htonl(NX_VENDOR_ID);
395 nah->subtype = htons(NXAST_DROP_SPOOFED_ARP);
396 } else if (!strcasecmp(act, "set_queue")) {
397 struct nx_action_set_queue *nasq;
398 nasq = put_action(b, sizeof *nasq, OFPAT_VENDOR);
399 nasq->vendor = htonl(NX_VENDOR_ID);
400 nasq->subtype = htons(NXAST_SET_QUEUE);
401 nasq->queue_id = htonl(str_to_u32(arg));
402 } else if (!strcasecmp(act, "pop_queue")) {
403 struct nx_action_header *nah;
404 nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
405 nah->vendor = htonl(NX_VENDOR_ID);
406 nah->subtype = htons(NXAST_POP_QUEUE);
407 } else if (!strcasecmp(act, "note")) {
408 size_t start_ofs = b->size;
409 struct nx_action_note *nan;
410 int remainder;
411 size_t len;
412
413 nan = put_action(b, sizeof *nan, OFPAT_VENDOR);
414 nan->vendor = htonl(NX_VENDOR_ID);
415 nan->subtype = htons(NXAST_NOTE);
416
417 b->size -= sizeof nan->note;
418 while (*arg != '\0') {
419 uint8_t byte;
420 bool ok;
421
422 if (*arg == '.') {
423 arg++;
424 }
425 if (*arg == '\0') {
426 break;
427 }
428
429 byte = hexits_value(arg, 2, &ok);
430 if (!ok) {
431 ovs_fatal(0, "bad hex digit in `note' argument");
432 }
433 ofpbuf_put(b, &byte, 1);
434
435 arg += 2;
436 }
437
438 len = b->size - start_ofs;
439 remainder = len % OFP_ACTION_ALIGN;
440 if (remainder) {
441 ofpbuf_put_zeros(b, OFP_ACTION_ALIGN - remainder);
442 }
443 nan->len = htons(b->size - start_ofs);
444 } else if (!strcasecmp(act, "move")) {
445 struct nx_action_reg_move *move;
446 move = ofpbuf_put_uninit(b, sizeof *move);
447 nxm_parse_reg_move(move, arg);
448 } else if (!strcasecmp(act, "load")) {
449 struct nx_action_reg_load *load;
450 load = ofpbuf_put_uninit(b, sizeof *load);
451 nxm_parse_reg_load(load, arg);
452 } else if (!strcasecmp(act, "multipath")) {
453 struct nx_action_multipath *nam;
454 nam = ofpbuf_put_uninit(b, sizeof *nam);
455 multipath_parse(nam, arg);
456 } else if (!strcasecmp(act, "output")) {
457 put_output_action(b, str_to_u32(arg));
458 } else if (!strcasecmp(act, "enqueue")) {
459 char *sp = NULL;
460 char *port_s = strtok_r(arg, ":q", &sp);
461 char *queue = strtok_r(NULL, "", &sp);
462 if (port_s == NULL || queue == NULL) {
463 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
464 }
465 put_enqueue_action(b, str_to_u32(port_s), str_to_u32(queue));
466 } else if (!strcasecmp(act, "drop")) {
467 /* A drop action in OpenFlow occurs by just not setting
468 * an action. */
469 drop = true;
470 if (n_actions) {
471 ovs_fatal(0, "Drop actions must not be preceded by other "
472 "actions");
473 }
474 } else if (!strcasecmp(act, "CONTROLLER")) {
475 struct ofp_action_output *oao;
476 oao = put_output_action(b, OFPP_CONTROLLER);
477
478 /* Unless a numeric argument is specified, we send the whole
479 * packet to the controller. */
480 if (arg[0] && (strspn(arg, "0123456789") == strlen(arg))) {
481 oao->max_len = htons(str_to_u32(arg));
482 } else {
483 oao->max_len = htons(UINT16_MAX);
484 }
485 } else if (parse_port_name(act, &port)) {
486 put_output_action(b, port);
487 } else if (strspn(act, "0123456789") == strlen(act)) {
488 put_output_action(b, str_to_u32(act));
489 } else {
490 ovs_fatal(0, "Unknown action: %s", act);
491 }
492 n_actions++;
493 }
494 }
495
496 struct protocol {
497 const char *name;
498 uint16_t dl_type;
499 uint8_t nw_proto;
500 };
501
502 static bool
503 parse_protocol(const char *name, const struct protocol **p_out)
504 {
505 static const struct protocol protocols[] = {
506 { "ip", ETH_TYPE_IP, 0 },
507 { "arp", ETH_TYPE_ARP, 0 },
508 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
509 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
510 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
511 { "ipv6", ETH_TYPE_IPV6, 0 },
512 { "ip6", ETH_TYPE_IPV6, 0 },
513 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
514 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
515 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
516 };
517 const struct protocol *p;
518
519 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
520 if (!strcmp(p->name, name)) {
521 *p_out = p;
522 return true;
523 }
524 }
525 *p_out = NULL;
526 return false;
527 }
528
529 #define FIELDS \
530 FIELD(F_TUN_ID, "tun_id", 0) \
531 FIELD(F_IN_PORT, "in_port", FWW_IN_PORT) \
532 FIELD(F_DL_VLAN, "dl_vlan", 0) \
533 FIELD(F_DL_VLAN_PCP, "dl_vlan_pcp", 0) \
534 FIELD(F_DL_SRC, "dl_src", FWW_DL_SRC) \
535 FIELD(F_DL_DST, "dl_dst", FWW_DL_DST) \
536 FIELD(F_DL_TYPE, "dl_type", FWW_DL_TYPE) \
537 FIELD(F_NW_SRC, "nw_src", 0) \
538 FIELD(F_NW_DST, "nw_dst", 0) \
539 FIELD(F_NW_PROTO, "nw_proto", FWW_NW_PROTO) \
540 FIELD(F_NW_TOS, "nw_tos", FWW_NW_TOS) \
541 FIELD(F_TP_SRC, "tp_src", FWW_TP_SRC) \
542 FIELD(F_TP_DST, "tp_dst", FWW_TP_DST) \
543 FIELD(F_ICMP_TYPE, "icmp_type", FWW_TP_SRC) \
544 FIELD(F_ICMP_CODE, "icmp_code", FWW_TP_DST) \
545 FIELD(F_ARP_SHA, "arp_sha", FWW_ARP_SHA) \
546 FIELD(F_ARP_THA, "arp_tha", FWW_ARP_THA) \
547 FIELD(F_IPV6_SRC, "ipv6_src", 0) \
548 FIELD(F_IPV6_DST, "ipv6_dst", 0) \
549 FIELD(F_ND_TARGET, "nd_target", FWW_ND_TARGET) \
550 FIELD(F_ND_SLL, "nd_sll", FWW_ARP_SHA) \
551 FIELD(F_ND_TLL, "nd_tll", FWW_ARP_THA)
552
553 enum field_index {
554 #define FIELD(ENUM, NAME, WILDCARD) ENUM,
555 FIELDS
556 #undef FIELD
557 N_FIELDS
558 };
559
560 struct field {
561 enum field_index index;
562 const char *name;
563 flow_wildcards_t wildcard; /* FWW_* bit. */
564 };
565
566 static bool
567 parse_field_name(const char *name, const struct field **f_out)
568 {
569 static const struct field fields[N_FIELDS] = {
570 #define FIELD(ENUM, NAME, WILDCARD) { ENUM, NAME, WILDCARD },
571 FIELDS
572 #undef FIELD
573 };
574 const struct field *f;
575
576 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
577 if (!strcmp(f->name, name)) {
578 *f_out = f;
579 return true;
580 }
581 }
582 *f_out = NULL;
583 return false;
584 }
585
586 static void
587 parse_field_value(struct cls_rule *rule, enum field_index index,
588 const char *value)
589 {
590 uint8_t mac[ETH_ADDR_LEN];
591 ovs_be64 tun_id, tun_mask;
592 ovs_be32 ip, mask;
593 struct in6_addr ipv6, ipv6_mask;
594 uint16_t port_no;
595
596 switch (index) {
597 case F_TUN_ID:
598 str_to_tun_id(value, &tun_id, &tun_mask);
599 cls_rule_set_tun_id_masked(rule, tun_id, tun_mask);
600 break;
601
602 case F_IN_PORT:
603 if (!parse_port_name(value, &port_no)) {
604 port_no = atoi(value);
605 }
606 if (port_no == OFPP_LOCAL) {
607 port_no = ODPP_LOCAL;
608 }
609 cls_rule_set_in_port(rule, port_no);
610 break;
611
612 case F_DL_VLAN:
613 cls_rule_set_dl_vlan(rule, htons(str_to_u32(value)));
614 break;
615
616 case F_DL_VLAN_PCP:
617 cls_rule_set_dl_vlan_pcp(rule, str_to_u32(value));
618 break;
619
620 case F_DL_SRC:
621 str_to_mac(value, mac);
622 cls_rule_set_dl_src(rule, mac);
623 break;
624
625 case F_DL_DST:
626 str_to_mac(value, mac);
627 cls_rule_set_dl_dst(rule, mac);
628 break;
629
630 case F_DL_TYPE:
631 cls_rule_set_dl_type(rule, htons(str_to_u32(value)));
632 break;
633
634 case F_NW_SRC:
635 str_to_ip(value, &ip, &mask);
636 cls_rule_set_nw_src_masked(rule, ip, mask);
637 break;
638
639 case F_NW_DST:
640 str_to_ip(value, &ip, &mask);
641 cls_rule_set_nw_dst_masked(rule, ip, mask);
642 break;
643
644 case F_NW_PROTO:
645 cls_rule_set_nw_proto(rule, str_to_u32(value));
646 break;
647
648 case F_NW_TOS:
649 cls_rule_set_nw_tos(rule, str_to_u32(value));
650 break;
651
652 case F_TP_SRC:
653 cls_rule_set_tp_src(rule, htons(str_to_u32(value)));
654 break;
655
656 case F_TP_DST:
657 cls_rule_set_tp_dst(rule, htons(str_to_u32(value)));
658 break;
659
660 case F_ICMP_TYPE:
661 cls_rule_set_icmp_type(rule, str_to_u32(value));
662 break;
663
664 case F_ICMP_CODE:
665 cls_rule_set_icmp_code(rule, str_to_u32(value));
666 break;
667
668 case F_ARP_SHA:
669 str_to_mac(value, mac);
670 cls_rule_set_arp_sha(rule, mac);
671 break;
672
673 case F_ARP_THA:
674 str_to_mac(value, mac);
675 cls_rule_set_arp_tha(rule, mac);
676 break;
677
678 case F_IPV6_SRC:
679 str_to_ipv6(value, &ipv6, &ipv6_mask);
680 cls_rule_set_ipv6_src_masked(rule, &ipv6, &ipv6_mask);
681 break;
682
683 case F_IPV6_DST:
684 str_to_ipv6(value, &ipv6, &ipv6_mask);
685 cls_rule_set_ipv6_dst_masked(rule, &ipv6, &ipv6_mask);
686 break;
687
688 case F_ND_TARGET:
689 str_to_ipv6(value, &ipv6, NULL);
690 cls_rule_set_nd_target(rule, ipv6);
691 break;
692
693 case F_ND_SLL:
694 str_to_mac(value, mac);
695 cls_rule_set_arp_sha(rule, mac);
696 break;
697
698 case F_ND_TLL:
699 str_to_mac(value, mac);
700 cls_rule_set_arp_tha(rule, mac);
701 break;
702
703 case N_FIELDS:
704 NOT_REACHED();
705 }
706 }
707
708 static void
709 parse_reg_value(struct cls_rule *rule, int reg_idx, const char *value)
710 {
711 uint32_t reg_value, reg_mask;
712
713 if (!strcmp(value, "ANY") || !strcmp(value, "*")) {
714 cls_rule_set_reg_masked(rule, reg_idx, 0, 0);
715 } else if (sscanf(value, "%"SCNi32"/%"SCNi32,
716 &reg_value, &reg_mask) == 2) {
717 cls_rule_set_reg_masked(rule, reg_idx, reg_value, reg_mask);
718 } else if (sscanf(value, "%"SCNi32, &reg_value)) {
719 cls_rule_set_reg(rule, reg_idx, reg_value);
720 } else {
721 ovs_fatal(0, "register fields must take the form <value> "
722 "or <value>/<mask>");
723 }
724 }
725
726 /* Convert 'string' (as described in the Flow Syntax section of the ovs-ofctl
727 * man page) into 'pf'. If 'actions' is specified, an action must be in
728 * 'string' and may be expanded or reallocated. */
729 void
730 parse_ofp_str(struct flow_mod *fm, uint8_t *table_idx,
731 struct ofpbuf *actions, char *string)
732 {
733 char *save_ptr = NULL;
734 char *name;
735
736 if (table_idx) {
737 *table_idx = 0xff;
738 }
739 cls_rule_init_catchall(&fm->cr, OFP_DEFAULT_PRIORITY);
740 fm->cookie = htonll(0);
741 fm->command = UINT16_MAX;
742 fm->idle_timeout = OFP_FLOW_PERMANENT;
743 fm->hard_timeout = OFP_FLOW_PERMANENT;
744 fm->buffer_id = UINT32_MAX;
745 fm->out_port = OFPP_NONE;
746 fm->flags = 0;
747 if (actions) {
748 char *act_str = strstr(string, "action");
749 if (!act_str) {
750 ovs_fatal(0, "must specify an action");
751 }
752 *act_str = '\0';
753
754 act_str = strchr(act_str + 1, '=');
755 if (!act_str) {
756 ovs_fatal(0, "must specify an action");
757 }
758
759 act_str++;
760
761 str_to_action(act_str, actions);
762 fm->actions = actions->data;
763 fm->n_actions = actions->size / sizeof(union ofp_action);
764 } else {
765 fm->actions = NULL;
766 fm->n_actions = 0;
767 }
768 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
769 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
770 const struct protocol *p;
771
772 if (parse_protocol(name, &p)) {
773 cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
774 if (p->nw_proto) {
775 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
776 }
777 } else {
778 const struct field *f;
779 char *value;
780
781 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
782 if (!value) {
783 ovs_fatal(0, "field %s missing value", name);
784 }
785
786 if (table_idx && !strcmp(name, "table")) {
787 *table_idx = atoi(value);
788 } else if (!strcmp(name, "out_port")) {
789 fm->out_port = atoi(value);
790 } else if (!strcmp(name, "priority")) {
791 fm->cr.priority = atoi(value);
792 } else if (!strcmp(name, "idle_timeout")) {
793 fm->idle_timeout = atoi(value);
794 } else if (!strcmp(name, "hard_timeout")) {
795 fm->hard_timeout = atoi(value);
796 } else if (!strcmp(name, "cookie")) {
797 fm->cookie = htonll(str_to_u64(value));
798 } else if (parse_field_name(name, &f)) {
799 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
800 if (f->wildcard) {
801 fm->cr.wc.wildcards |= f->wildcard;
802 cls_rule_zero_wildcarded_fields(&fm->cr);
803 } else if (f->index == F_NW_SRC) {
804 cls_rule_set_nw_src_masked(&fm->cr, 0, 0);
805 } else if (f->index == F_NW_DST) {
806 cls_rule_set_nw_dst_masked(&fm->cr, 0, 0);
807 } else if (f->index == F_IPV6_SRC) {
808 cls_rule_set_ipv6_src_masked(&fm->cr,
809 &in6addr_any, &in6addr_any);
810 } else if (f->index == F_IPV6_DST) {
811 cls_rule_set_ipv6_dst_masked(&fm->cr,
812 &in6addr_any, &in6addr_any);
813 } else if (f->index == F_DL_VLAN) {
814 cls_rule_set_any_vid(&fm->cr);
815 } else if (f->index == F_DL_VLAN_PCP) {
816 cls_rule_set_any_pcp(&fm->cr);
817 } else {
818 NOT_REACHED();
819 }
820 } else {
821 parse_field_value(&fm->cr, f->index, value);
822 }
823 } else if (!strncmp(name, "reg", 3) && isdigit(name[3])) {
824 unsigned int reg_idx = atoi(name + 3);
825 if (reg_idx >= FLOW_N_REGS) {
826 ovs_fatal(0, "only %d registers supported", FLOW_N_REGS);
827 }
828 parse_reg_value(&fm->cr, reg_idx, value);
829 } else {
830 ovs_fatal(0, "unknown keyword %s", name);
831 }
832 }
833 }
834 }
835
836 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
837 * (one of OFPFC_*) and appends the parsed OpenFlow message to 'packets'.
838 * '*cur_format' should initially contain the flow format currently configured
839 * on the connection; this function will add a message to change the flow
840 * format and update '*cur_format', if this is necessary to add the parsed
841 * flow. */
842 void
843 parse_ofp_flow_mod_str(struct list *packets, enum nx_flow_format *cur_format,
844 char *string, uint16_t command)
845 {
846 bool is_del = command == OFPFC_DELETE || command == OFPFC_DELETE_STRICT;
847 enum nx_flow_format min_format, next_format;
848 struct ofpbuf actions;
849 struct ofpbuf *ofm;
850 struct flow_mod fm;
851
852 ofpbuf_init(&actions, 64);
853 parse_ofp_str(&fm, NULL, is_del ? NULL : &actions, string);
854 fm.command = command;
855
856 min_format = ofputil_min_flow_format(&fm.cr, true, fm.cookie);
857 next_format = MAX(*cur_format, min_format);
858 if (next_format != *cur_format) {
859 struct ofpbuf *sff = ofputil_make_set_flow_format(next_format);
860 list_push_back(packets, &sff->list_node);
861 *cur_format = next_format;
862 }
863
864 ofm = ofputil_encode_flow_mod(&fm, *cur_format);
865 list_push_back(packets, &ofm->list_node);
866
867 ofpbuf_uninit(&actions);
868 }
869
870 /* Similar to parse_ofp_flow_mod_str(), except that the string is read from
871 * 'stream' and the command is always OFPFC_ADD. Returns false if end-of-file
872 * is reached before reading a flow, otherwise true. */
873 bool
874 parse_ofp_flow_mod_file(struct list *packets, enum nx_flow_format *cur,
875 FILE *stream, uint16_t command)
876 {
877 struct ds s;
878 bool ok;
879
880 ds_init(&s);
881 ok = ds_get_preprocessed_line(&s, stream) == 0;
882 if (ok) {
883 parse_ofp_flow_mod_str(packets, cur, ds_cstr(&s), command);
884 }
885 ds_destroy(&s);
886
887 return ok;
888 }
889
890 void
891 parse_ofp_flow_stats_request_str(struct flow_stats_request *fsr,
892 bool aggregate, char *string)
893 {
894 struct flow_mod fm;
895 uint8_t table_id;
896
897 parse_ofp_str(&fm, &table_id, NULL, string);
898 fsr->aggregate = aggregate;
899 fsr->match = fm.cr;
900 fsr->out_port = fm.out_port;
901 fsr->table_id = table_id;
902 }