ofp-parse: Add support for tun_id.
[sliver-openvswitch.git] / lib / ofp-parse.c
1 /*
2  * Copyright (c) 2010 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 <errno.h>
22 #include <stdlib.h>
23
24 #include "byte-order.h"
25 #include "dynamic-string.h"
26 #include "netdev.h"
27 #include "ofp-util.h"
28 #include "ofpbuf.h"
29 #include "openflow/openflow.h"
30 #include "packets.h"
31 #include "socket-util.h"
32 #include "vconn.h"
33 #include "vlog.h"
34
35 VLOG_DEFINE_THIS_MODULE(ofp_parse);
36
37 static uint32_t
38 str_to_u32(const char *str)
39 {
40     char *tail;
41     uint32_t value;
42
43     if (!str) {
44         ovs_fatal(0, "missing required numeric argument");
45     }
46
47     errno = 0;
48     value = strtoul(str, &tail, 0);
49     if (errno == EINVAL || errno == ERANGE || *tail) {
50         ovs_fatal(0, "invalid numeric format %s", str);
51     }
52     return value;
53 }
54
55 static uint64_t
56 str_to_u64(const char *str)
57 {
58     char *tail;
59     uint64_t value;
60
61     errno = 0;
62     value = strtoull(str, &tail, 0);
63     if (errno == EINVAL || errno == ERANGE || *tail) {
64         ovs_fatal(0, "invalid numeric format %s", str);
65     }
66     return value;
67 }
68
69 static void
70 str_to_mac(const char *str, uint8_t mac[6])
71 {
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);
75     }
76 }
77
78 static void
79 str_to_ip(const char *str_, ovs_be32 *ip, ovs_be32 *maskp)
80 {
81     char *str = xstrdup(str_);
82     char *save_ptr = NULL;
83     const char *name, *netmask;
84     struct in_addr in_addr;
85     ovs_be32 mask;
86     int retval;
87
88     name = strtok_r(str, "/", &save_ptr);
89     retval = name ? lookup_ip(name, &in_addr) : EINVAL;
90     if (retval) {
91         ovs_fatal(0, "%s: could not convert to IP address", str);
92     }
93     *ip = in_addr.s_addr;
94
95     netmask = strtok_r(NULL, "/", &save_ptr);
96     if (netmask) {
97         uint8_t o[4];
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]);
101         } else {
102             int prefix = atoi(netmask);
103             if (prefix <= 0 || prefix > 32) {
104                 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
105                           str);
106             } else if (prefix == 32) {
107                 mask = htonl(UINT32_MAX);
108             } else {
109                 mask = htonl(((1u << prefix) - 1) << (32 - prefix));
110             }
111         }
112     } else {
113         mask = htonl(UINT32_MAX);
114     }
115     *ip &= mask;
116
117     if (maskp) {
118         *maskp = mask;
119     } else {
120         if (mask != htonl(UINT32_MAX)) {
121             ovs_fatal(0, "%s: netmask not allowed here", str_);
122         }
123     }
124
125     free(str);
126 }
127
128 static void *
129 put_action(struct ofpbuf *b, size_t size, uint16_t type)
130 {
131     struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
132     ah->type = htons(type);
133     ah->len = htons(size);
134     return ah;
135 }
136
137 static struct ofp_action_output *
138 put_output_action(struct ofpbuf *b, uint16_t port)
139 {
140     struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
141     oao->port = htons(port);
142     return oao;
143 }
144
145 static void
146 put_enqueue_action(struct ofpbuf *b, uint16_t port, uint32_t queue)
147 {
148     struct ofp_action_enqueue *oae = put_action(b, sizeof *oae, OFPAT_ENQUEUE);
149     oae->port = htons(port);
150     oae->queue_id = htonl(queue);
151 }
152
153 static void
154 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
155 {
156     struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
157     str_to_mac(addr, oada->dl_addr);
158 }
159
160
161 static bool
162 parse_port_name(const char *name, uint16_t *port)
163 {
164     struct pair {
165         const char *name;
166         uint16_t value;
167     };
168     static const struct pair pairs[] = {
169 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
170         DEF_PAIR(IN_PORT),
171         DEF_PAIR(TABLE),
172         DEF_PAIR(NORMAL),
173         DEF_PAIR(FLOOD),
174         DEF_PAIR(ALL),
175         DEF_PAIR(CONTROLLER),
176         DEF_PAIR(LOCAL),
177         DEF_PAIR(NONE),
178 #undef DEF_PAIR
179     };
180     static const int n_pairs = ARRAY_SIZE(pairs);
181     size_t i;
182
183     for (i = 0; i < n_pairs; i++) {
184         if (!strcasecmp(name, pairs[i].name)) {
185             *port = pairs[i].value;
186             return true;
187         }
188     }
189     return false;
190 }
191
192 static void
193 str_to_action(char *str, struct ofpbuf *b)
194 {
195     char *act, *arg;
196     char *saveptr = NULL;
197     bool drop = false;
198     int n_actions;
199
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++)
202     {
203         uint16_t port;
204
205         if (drop) {
206             ovs_fatal(0, "Drop actions must not be followed by other actions");
207         }
208
209         /* Arguments are separated by colons */
210         arg = strchr(act, ':');
211         if (arg) {
212             *arg = '\0';
213             arg++;
214         }
215
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, "note")) {
281             size_t start_ofs = b->size;
282             struct nx_action_note *nan;
283             int remainder;
284             size_t len;
285
286             nan = put_action(b, sizeof *nan, OFPAT_VENDOR);
287             nan->vendor = htonl(NX_VENDOR_ID);
288             nan->subtype = htons(NXAST_NOTE);
289
290             b->size -= sizeof nan->note;
291             while (arg && *arg != '\0') {
292                 uint8_t byte;
293                 bool ok;
294
295                 if (*arg == '.') {
296                     arg++;
297                 }
298                 if (*arg == '\0') {
299                     break;
300                 }
301
302                 byte = hexits_value(arg, 2, &ok);
303                 if (!ok) {
304                     ovs_fatal(0, "bad hex digit in `note' argument");
305                 }
306                 ofpbuf_put(b, &byte, 1);
307
308                 arg += 2;
309             }
310
311             len = b->size - start_ofs;
312             remainder = len % OFP_ACTION_ALIGN;
313             if (remainder) {
314                 ofpbuf_put_zeros(b, OFP_ACTION_ALIGN - remainder);
315             }
316             nan->len = htons(b->size - start_ofs);
317         } else if (!strcasecmp(act, "output")) {
318             put_output_action(b, str_to_u32(arg));
319         } else if (!strcasecmp(act, "enqueue")) {
320             char *sp = NULL;
321             char *port_s = strtok_r(arg, ":q", &sp);
322             char *queue = strtok_r(NULL, "", &sp);
323             if (port_s == NULL || queue == NULL) {
324                 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
325             }
326             put_enqueue_action(b, str_to_u32(port_s), str_to_u32(queue));
327         } else if (!strcasecmp(act, "drop")) {
328             /* A drop action in OpenFlow occurs by just not setting
329              * an action. */
330             drop = true;
331             if (n_actions) {
332                 ovs_fatal(0, "Drop actions must not be preceded by other "
333                           "actions");
334             }
335         } else if (!strcasecmp(act, "CONTROLLER")) {
336             struct ofp_action_output *oao;
337             oao = put_output_action(b, OFPP_CONTROLLER);
338
339             /* Unless a numeric argument is specified, we send the whole
340              * packet to the controller. */
341             if (arg && (strspn(arg, "0123456789") == strlen(arg))) {
342                oao->max_len = htons(str_to_u32(arg));
343             } else {
344                 oao->max_len = htons(UINT16_MAX);
345             }
346         } else if (parse_port_name(act, &port)) {
347             put_output_action(b, port);
348         } else if (strspn(act, "0123456789") == strlen(act)) {
349             put_output_action(b, str_to_u32(act));
350         } else {
351             ovs_fatal(0, "Unknown action: %s", act);
352         }
353     }
354 }
355
356 struct protocol {
357     const char *name;
358     uint16_t dl_type;
359     uint8_t nw_proto;
360 };
361
362 static bool
363 parse_protocol(const char *name, const struct protocol **p_out)
364 {
365     static const struct protocol protocols[] = {
366         { "ip", ETH_TYPE_IP, 0 },
367         { "arp", ETH_TYPE_ARP, 0 },
368         { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
369         { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
370         { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
371     };
372     const struct protocol *p;
373
374     for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
375         if (!strcmp(p->name, name)) {
376             *p_out = p;
377             return true;
378         }
379     }
380     *p_out = NULL;
381     return false;
382 }
383
384 #define FIELDS                                              \
385     FIELD(F_TUN_ID,      "tun_id",      FWW_TUN_ID)         \
386     FIELD(F_IN_PORT,     "in_port",     FWW_IN_PORT)        \
387     FIELD(F_DL_VLAN,     "dl_vlan",     0)                  \
388     FIELD(F_DL_VLAN_PCP, "dl_vlan_pcp", 0)                  \
389     FIELD(F_DL_SRC,      "dl_src",      FWW_DL_SRC)         \
390     FIELD(F_DL_DST,      "dl_dst",      FWW_DL_DST)         \
391     FIELD(F_DL_TYPE,     "dl_type",     FWW_DL_TYPE)        \
392     FIELD(F_NW_SRC,      "nw_src",      0)                  \
393     FIELD(F_NW_DST,      "nw_dst",      0)                  \
394     FIELD(F_NW_PROTO,    "nw_proto",    FWW_NW_PROTO)       \
395     FIELD(F_NW_TOS,      "nw_tos",      FWW_NW_TOS)         \
396     FIELD(F_TP_SRC,      "tp_src",      FWW_TP_SRC)         \
397     FIELD(F_TP_DST,      "tp_dst",      FWW_TP_DST)         \
398     FIELD(F_ICMP_TYPE,   "icmp_type",   FWW_TP_SRC)         \
399     FIELD(F_ICMP_CODE,   "icmp_code",   FWW_TP_DST)
400
401 enum field_index {
402 #define FIELD(ENUM, NAME, WILDCARD) ENUM,
403     FIELDS
404 #undef FIELD
405     N_FIELDS
406 };
407
408 struct field {
409     enum field_index index;
410     const char *name;
411     flow_wildcards_t wildcard;  /* FWW_* bit. */
412 };
413
414 static bool
415 parse_field_name(const char *name, const struct field **f_out)
416 {
417     static const struct field fields[N_FIELDS] = {
418 #define FIELD(ENUM, NAME, WILDCARD) { ENUM, NAME, WILDCARD },
419         FIELDS
420 #undef FIELD
421     };
422     const struct field *f;
423
424     for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
425         if (!strcmp(f->name, name)) {
426             *f_out = f;
427             return true;
428         }
429     }
430     *f_out = NULL;
431     return false;
432 }
433
434 static void
435 parse_field_value(struct cls_rule *rule, enum field_index index,
436                   const char *value)
437 {
438     uint8_t mac[ETH_ADDR_LEN];
439     ovs_be32 ip, mask;
440     uint16_t port_no;
441
442     switch (index) {
443     case F_TUN_ID:
444         cls_rule_set_tun_id(rule, htonl(str_to_u32(value)));
445         break;
446
447     case F_IN_PORT:
448         if (!parse_port_name(value, &port_no)) {
449             port_no = atoi(value);
450         }
451         if (port_no == OFPP_LOCAL) {
452             port_no = ODPP_LOCAL;
453         }
454         cls_rule_set_in_port(rule, port_no);
455         break;
456
457     case F_DL_VLAN:
458         cls_rule_set_dl_vlan(rule, htons(str_to_u32(value)));
459         break;
460
461     case F_DL_VLAN_PCP:
462         cls_rule_set_dl_vlan_pcp(rule, str_to_u32(value));
463         break;
464
465     case F_DL_SRC:
466         str_to_mac(value, mac);
467         cls_rule_set_dl_src(rule, mac);
468         break;
469
470     case F_DL_DST:
471         str_to_mac(value, mac);
472         cls_rule_set_dl_dst(rule, mac);
473         break;
474
475     case F_DL_TYPE:
476         cls_rule_set_dl_type(rule, htons(str_to_u32(value)));
477         break;
478
479     case F_NW_SRC:
480         str_to_ip(value, &ip, &mask);
481         cls_rule_set_nw_src_masked(rule, ip, mask);
482         break;
483
484     case F_NW_DST:
485         str_to_ip(value, &ip, &mask);
486         cls_rule_set_nw_dst_masked(rule, ip, mask);
487         break;
488
489     case F_NW_PROTO:
490         cls_rule_set_nw_proto(rule, str_to_u32(value));
491         break;
492
493     case F_NW_TOS:
494         cls_rule_set_nw_tos(rule, str_to_u32(value));
495         break;
496
497     case F_TP_SRC:
498         cls_rule_set_tp_src(rule, htons(str_to_u32(value)));
499         break;
500
501     case F_TP_DST:
502         cls_rule_set_tp_dst(rule, htons(str_to_u32(value)));
503         break;
504
505     case F_ICMP_TYPE:
506         cls_rule_set_icmp_type(rule, str_to_u32(value));
507         break;
508
509     case F_ICMP_CODE:
510         cls_rule_set_icmp_code(rule, str_to_u32(value));
511         break;
512
513     case N_FIELDS:
514         NOT_REACHED();
515     }
516 }
517
518 /* Convert 'string' (as described in the Flow Syntax section of the ovs-ofctl
519  * man page) into 'pf'.  If 'actions' is specified, an action must be in
520  * 'string' and may be expanded or reallocated. */
521 void
522 parse_ofp_str(struct parsed_flow *pf, struct ofpbuf *actions, char *string)
523 {
524     char *save_ptr = NULL;
525     char *name;
526
527     cls_rule_init_catchall(&pf->rule, OFP_DEFAULT_PRIORITY);
528     pf->table_idx = 0xff;
529     pf->out_port = OFPP_NONE;
530     pf->idle_timeout = OFP_FLOW_PERMANENT;
531     pf->hard_timeout = OFP_FLOW_PERMANENT;
532     pf->cookie = 0;
533     if (actions) {
534         char *act_str = strstr(string, "action");
535         if (!act_str) {
536             ovs_fatal(0, "must specify an action");
537         }
538         *act_str = '\0';
539
540         act_str = strchr(act_str + 1, '=');
541         if (!act_str) {
542             ovs_fatal(0, "must specify an action");
543         }
544
545         act_str++;
546
547         str_to_action(act_str, actions);
548     }
549     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
550          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
551         const struct protocol *p;
552
553         if (parse_protocol(name, &p)) {
554             cls_rule_set_dl_type(&pf->rule, htons(p->dl_type));
555             if (p->nw_proto) {
556                 cls_rule_set_nw_proto(&pf->rule, p->nw_proto);
557             }
558         } else {
559             const struct field *f;
560             char *value;
561
562             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
563             if (!value) {
564                 ovs_fatal(0, "field %s missing value", name);
565             }
566
567             if (!strcmp(name, "table")) {
568                 pf->table_idx = atoi(value);
569             } else if (!strcmp(name, "out_port")) {
570                 pf->out_port = atoi(value);
571             } else if (!strcmp(name, "priority")) {
572                 pf->rule.priority = atoi(value);
573             } else if (!strcmp(name, "idle_timeout")) {
574                 pf->idle_timeout = atoi(value);
575             } else if (!strcmp(name, "hard_timeout")) {
576                 pf->hard_timeout = atoi(value);
577             } else if (!strcmp(name, "cookie")) {
578                 pf->cookie = str_to_u64(value);
579             } else if (parse_field_name(name, &f)) {
580                 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
581                     if (f->wildcard) {
582                         pf->rule.wc.wildcards |= f->wildcard;
583                         cls_rule_zero_wildcarded_fields(&pf->rule);
584                     } else if (f->index == F_NW_SRC) {
585                         cls_rule_set_nw_src_masked(&pf->rule, 0, 0);
586                     } else if (f->index == F_NW_DST) {
587                         cls_rule_set_nw_dst_masked(&pf->rule, 0, 0);
588                     } else if (f->index == F_DL_VLAN) {
589                         cls_rule_set_any_vid(&pf->rule);
590                     } else if (f->index == F_DL_VLAN_PCP) {
591                         cls_rule_set_any_pcp(&pf->rule);
592                     } else {
593                         NOT_REACHED();
594                     }
595                 } else {
596                     parse_field_value(&pf->rule, f->index, value);
597                 }
598             } else {
599                 ovs_fatal(0, "unknown keyword %s", name);
600             }
601         }
602     }
603 }
604
605 /* Parses 'string' as an OFPT_FLOW_MOD with command 'command' (one of OFPFC_*)
606  * and returns an ofpbuf that contains it. */
607 struct ofpbuf *
608 parse_ofp_flow_mod_str(char *string, uint16_t command)
609 {
610     struct parsed_flow pf;
611     struct ofpbuf *buffer;
612     struct ofp_flow_mod *ofm;
613
614     /* parse_ofp_str() will expand and reallocate the data in 'buffer', so we
615      * can't keep pointers to across the parse_ofp_str() call. */
616     make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
617     parse_ofp_str(&pf, buffer, string);
618
619     ofm = buffer->data;
620     ofputil_cls_rule_to_match(&pf.rule, NXFF_OPENFLOW10, &ofm->match);
621     ofm->command = htons(command);
622     ofm->cookie = htonll(pf.cookie);
623     ofm->idle_timeout = htons(pf.idle_timeout);
624     ofm->hard_timeout = htons(pf.hard_timeout);
625     ofm->buffer_id = htonl(UINT32_MAX);
626     ofm->out_port = htons(pf.out_port);
627     ofm->priority = htons(pf.rule.priority);
628     update_openflow_length(buffer);
629
630     return buffer;
631 }
632
633 /* Parses an OFPT_FLOW_MOD with subtype OFPFC_ADD from 'stream' and returns an
634  * ofpbuf that contains it.  Returns a null pointer if end-of-file is reached
635  * before reading a flow. */
636 struct ofpbuf *
637 parse_ofp_add_flow_file(FILE *stream)
638 {
639     struct ofpbuf *b = NULL;
640     struct ds s = DS_EMPTY_INITIALIZER;
641
642     while (!ds_get_line(&s, stream)) {
643         char *line = ds_cstr(&s);
644         char *comment;
645
646         /* Delete comments. */
647         comment = strchr(line, '#');
648         if (comment) {
649             *comment = '\0';
650         }
651
652         /* Drop empty lines. */
653         if (line[strspn(line, " \t\n")] == '\0') {
654             continue;
655         }
656
657         b = parse_ofp_flow_mod_str(line, OFPFC_ADD);
658         break;
659     }
660     ds_destroy(&s);
661
662     return b;
663 }