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