tests: Fix memory leaks in test programs.
[sliver-openvswitch.git] / utilities / ovs-ofctl.c
1 /*
2  * Copyright (c) 2008, 2009 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 #include <arpa/inet.h>
19 #include <errno.h>
20 #include <getopt.h>
21 #include <inttypes.h>
22 #include <net/if.h>
23 #include <netinet/in.h>
24 #include <signal.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31
32 #include "command-line.h"
33 #include "compiler.h"
34 #include "dirs.h"
35 #include "dpif.h"
36 #include "dynamic-string.h"
37 #include "netdev.h"
38 #include "netlink.h"
39 #include "odp-util.h"
40 #include "ofp-print.h"
41 #include "ofpbuf.h"
42 #include "openflow/nicira-ext.h"
43 #include "openflow/openflow.h"
44 #include "packets.h"
45 #include "random.h"
46 #include "socket-util.h"
47 #include "stream-ssl.h"
48 #include "timeval.h"
49 #include "util.h"
50 #include "vconn.h"
51
52 #include "vlog.h"
53 #define THIS_MODULE VLM_ofctl
54
55 #define DEFAULT_IDLE_TIMEOUT 60
56
57 #define MOD_PORT_CMD_UP      "up"
58 #define MOD_PORT_CMD_DOWN    "down"
59 #define MOD_PORT_CMD_FLOOD   "flood"
60 #define MOD_PORT_CMD_NOFLOOD "noflood"
61
62 /* Use strict matching for flow mod commands? */
63 static bool strict;
64
65 static const struct command all_commands[];
66
67 static void usage(void) NO_RETURN;
68 static void parse_options(int argc, char *argv[]);
69
70 int
71 main(int argc, char *argv[])
72 {
73     set_program_name(argv[0]);
74     time_init();
75     vlog_init();
76     parse_options(argc, argv);
77     signal(SIGPIPE, SIG_IGN);
78     run_command(argc - optind, argv + optind, all_commands);
79     return 0;
80 }
81
82 static void
83 parse_options(int argc, char *argv[])
84 {
85     enum {
86         OPT_STRICT = UCHAR_MAX + 1
87     };
88     static struct option long_options[] = {
89         {"timeout", required_argument, 0, 't'},
90         {"verbose", optional_argument, 0, 'v'},
91         {"strict", no_argument, 0, OPT_STRICT},
92         {"help", no_argument, 0, 'h'},
93         {"version", no_argument, 0, 'V'},
94         STREAM_SSL_LONG_OPTIONS
95         {0, 0, 0, 0},
96     };
97     char *short_options = long_options_to_short_options(long_options);
98
99     for (;;) {
100         unsigned long int timeout;
101         int c;
102
103         c = getopt_long(argc, argv, short_options, long_options, NULL);
104         if (c == -1) {
105             break;
106         }
107
108         switch (c) {
109         case 't':
110             timeout = strtoul(optarg, NULL, 10);
111             if (timeout <= 0) {
112                 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
113                           optarg);
114             } else {
115                 time_alarm(timeout);
116             }
117             break;
118
119         case 'h':
120             usage();
121
122         case 'V':
123             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
124             exit(EXIT_SUCCESS);
125
126         case 'v':
127             vlog_set_verbosity(optarg);
128             break;
129
130         case OPT_STRICT:
131             strict = true;
132             break;
133
134         STREAM_SSL_OPTION_HANDLERS
135
136         case '?':
137             exit(EXIT_FAILURE);
138
139         default:
140             abort();
141         }
142     }
143     free(short_options);
144 }
145
146 static void
147 usage(void)
148 {
149     printf("%s: OpenFlow switch management utility\n"
150            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
151            "\nFor OpenFlow switches:\n"
152            "  show SWITCH                 show OpenFlow information\n"
153            "  status SWITCH [KEY]         report statistics (about KEY)\n"
154            "  dump-desc SWITCH            print switch description\n"
155            "  dump-tables SWITCH          print table stats\n"
156            "  mod-port SWITCH IFACE ACT   modify port behavior\n"
157            "  dump-ports SWITCH           print port statistics\n"
158            "  dump-flows SWITCH           print all flow entries\n"
159            "  dump-flows SWITCH FLOW      print matching FLOWs\n"
160            "  dump-aggregate SWITCH       print aggregate flow statistics\n"
161            "  dump-aggregate SWITCH FLOW  print aggregate stats for FLOWs\n"
162            "  add-flow SWITCH FLOW        add flow described by FLOW\n"
163            "  add-flows SWITCH FILE       add flows from FILE\n"
164            "  mod-flows SWITCH FLOW       modify actions of matching FLOWs\n"
165            "  del-flows SWITCH [FLOW]     delete matching FLOWs\n"
166            "  monitor SWITCH MISSLEN EXP  print packets received from SWITCH\n"
167            "\nFor OpenFlow switches and controllers:\n"
168            "  probe VCONN                 probe whether VCONN is up\n"
169            "  ping VCONN [N]              latency of N-byte echos\n"
170            "  benchmark VCONN N COUNT     bandwidth of COUNT N-byte echos\n"
171            "where each SWITCH is an active OpenFlow connection method.\n",
172            program_name, program_name);
173     vconn_usage(true, false, false);
174     vlog_usage();
175     printf("\nOther options:\n"
176            "  --strict                    use strict match for flow commands\n"
177            "  -t, --timeout=SECS          give up after SECS seconds\n"
178            "  -h, --help                  display this help message\n"
179            "  -V, --version               display version information\n");
180     exit(EXIT_SUCCESS);
181 }
182
183 static void run(int retval, const char *message, ...)
184     PRINTF_FORMAT(2, 3);
185
186 static void run(int retval, const char *message, ...)
187 {
188     if (retval) {
189         va_list args;
190
191         fprintf(stderr, "%s: ", program_name);
192         va_start(args, message);
193         vfprintf(stderr, message, args);
194         va_end(args);
195         if (retval == EOF) {
196             fputs(": unexpected end of file\n", stderr);
197         } else {
198             fprintf(stderr, ": %s\n", strerror(retval));
199         }
200
201         exit(EXIT_FAILURE);
202     }
203 }
204 \f
205 /* Generic commands. */
206
207 static void
208 open_vconn_socket(const char *name, struct vconn **vconnp)
209 {
210     char *vconn_name = xasprintf("unix:%s", name);
211     VLOG_INFO("connecting to %s", vconn_name);
212     run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
213         "connecting to %s", vconn_name);
214     free(vconn_name);
215 }
216
217 static void
218 open_vconn(const char *name, struct vconn **vconnp)
219 {
220     struct dpif *dpif;
221     struct stat s;
222     char *bridge_path, *datapath_name, *datapath_type;
223
224     bridge_path = xasprintf("%s/%s.mgmt", ovs_rundir, name);
225     dp_parse_name(name, &datapath_name, &datapath_type);
226
227     if (strstr(name, ":")) {
228         run(vconn_open_block(name, OFP_VERSION, vconnp),
229             "connecting to %s", name);
230     } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
231         open_vconn_socket(name, vconnp);
232     } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
233         open_vconn_socket(bridge_path, vconnp);
234     } else if (!dpif_open(datapath_name, datapath_type, &dpif)) {
235         char dpif_name[IF_NAMESIZE + 1];
236         char *socket_name;
237
238         run(dpif_port_get_name(dpif, ODPP_LOCAL, dpif_name, sizeof dpif_name),
239             "obtaining name of %s", dpif_name);
240         dpif_close(dpif);
241         if (strcmp(dpif_name, name)) {
242             VLOG_INFO("datapath %s is named %s", name, dpif_name);
243         }
244
245         socket_name = xasprintf("%s/%s.mgmt", ovs_rundir, dpif_name);
246         if (stat(socket_name, &s)) {
247             ovs_fatal(errno, "cannot connect to %s: stat failed on %s",
248                       name, socket_name);
249         } else if (!S_ISSOCK(s.st_mode)) {
250             ovs_fatal(0, "cannot connect to %s: %s is not a socket",
251                       name, socket_name);
252         }
253
254         open_vconn_socket(socket_name, vconnp);
255         free(socket_name);
256     } else {
257         ovs_fatal(0, "%s is not a valid connection method", name);
258     }
259
260     free(datapath_name);
261     free(datapath_type);
262     free(bridge_path);
263 }
264
265 static void *
266 alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
267 {
268     struct ofp_stats_request *rq;
269     rq = make_openflow((offsetof(struct ofp_stats_request, body)
270                         + body_len), OFPT_STATS_REQUEST, bufferp);
271     rq->type = htons(type);
272     rq->flags = htons(0);
273     return rq->body;
274 }
275
276 static void
277 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
278 {
279     update_openflow_length(buffer);
280     run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
281 }
282
283 static void
284 dump_transaction(const char *vconn_name, struct ofpbuf *request)
285 {
286     struct vconn *vconn;
287     struct ofpbuf *reply;
288
289     update_openflow_length(request);
290     open_vconn(vconn_name, &vconn);
291     run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
292     ofp_print(stdout, reply->data, reply->size, 1);
293     vconn_close(vconn);
294 }
295
296 static void
297 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
298 {
299     struct ofpbuf *request;
300     make_openflow(sizeof(struct ofp_header), request_type, &request);
301     dump_transaction(vconn_name, request);
302 }
303
304 static void
305 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
306 {
307     uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
308     struct vconn *vconn;
309     bool done = false;
310
311     open_vconn(vconn_name, &vconn);
312     send_openflow_buffer(vconn, request);
313     while (!done) {
314         uint32_t recv_xid;
315         struct ofpbuf *reply;
316
317         run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
318         recv_xid = ((struct ofp_header *) reply->data)->xid;
319         if (send_xid == recv_xid) {
320             struct ofp_stats_reply *osr;
321
322             ofp_print(stdout, reply->data, reply->size, 1);
323
324             osr = ofpbuf_at(reply, 0, sizeof *osr);
325             done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
326         } else {
327             VLOG_DBG("received reply with xid %08"PRIx32" "
328                      "!= expected %08"PRIx32, recv_xid, send_xid);
329         }
330         ofpbuf_delete(reply);
331     }
332     vconn_close(vconn);
333 }
334
335 static void
336 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
337 {
338     struct ofpbuf *request;
339     alloc_stats_request(0, stats_type, &request);
340     dump_stats_transaction(vconn_name, request);
341 }
342
343 static void
344 do_show(int argc UNUSED, char *argv[])
345 {
346     dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
347     dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
348 }
349
350 static void
351 do_status(int argc, char *argv[])
352 {
353     struct nicira_header *request, *reply;
354     struct vconn *vconn;
355     struct ofpbuf *b;
356
357     request = make_openflow(sizeof *request, OFPT_VENDOR, &b);
358     request->vendor = htonl(NX_VENDOR_ID);
359     request->subtype = htonl(NXT_STATUS_REQUEST);
360     if (argc > 2) {
361         ofpbuf_put(b, argv[2], strlen(argv[2]));
362         update_openflow_length(b);
363     }
364     open_vconn(argv[1], &vconn);
365     run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
366     vconn_close(vconn);
367
368     if (b->size < sizeof *reply) {
369         ovs_fatal(0, "short reply (%zu bytes)", b->size);
370     }
371     reply = b->data;
372     if (reply->header.type != OFPT_VENDOR
373         || reply->vendor != ntohl(NX_VENDOR_ID)
374         || reply->subtype != ntohl(NXT_STATUS_REPLY)) {
375         ofp_print(stderr, b->data, b->size, 2);
376         ovs_fatal(0, "bad reply");
377     }
378
379     fwrite(reply + 1, b->size - sizeof *reply, 1, stdout);
380 }
381
382 static void
383 do_dump_desc(int argc UNUSED, char *argv[])
384 {
385     dump_trivial_stats_transaction(argv[1], OFPST_DESC);
386 }
387
388 static void
389 do_dump_tables(int argc UNUSED, char *argv[])
390 {
391     dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
392 }
393
394
395 static uint32_t
396 str_to_u32(const char *str) 
397 {
398     char *tail;
399     uint32_t value;
400
401     errno = 0;
402     value = strtoul(str, &tail, 0);
403     if (errno == EINVAL || errno == ERANGE || *tail) {
404         ovs_fatal(0, "invalid numeric format %s", str);
405     }
406     return value;
407 }
408
409 static void
410 str_to_mac(const char *str, uint8_t mac[6]) 
411 {
412     if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
413         != ETH_ADDR_SCAN_COUNT) {
414         ovs_fatal(0, "invalid mac address %s", str);
415     }
416 }
417
418 static uint32_t
419 str_to_ip(const char *str_, uint32_t *ip)
420 {
421     char *str = xstrdup(str_);
422     char *save_ptr = NULL;
423     const char *name, *netmask;
424     struct in_addr in_addr;
425     int n_wild, retval;
426
427     name = strtok_r(str, "/", &save_ptr);
428     retval = name ? lookup_ip(name, &in_addr) : EINVAL;
429     if (retval) {
430         ovs_fatal(0, "%s: could not convert to IP address", str);
431     }
432     *ip = in_addr.s_addr;
433
434     netmask = strtok_r(NULL, "/", &save_ptr);
435     if (netmask) {
436         uint8_t o[4];
437         if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
438                    &o[0], &o[1], &o[2], &o[3]) == 4) {
439             uint32_t nm = (o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3];
440             int i;
441
442             /* Find first 1-bit. */
443             for (i = 0; i < 32; i++) {
444                 if (nm & (1u << i)) {
445                     break;
446                 }
447             }
448             n_wild = i;
449
450             /* Verify that the rest of the bits are 1-bits. */
451             for (; i < 32; i++) {
452                 if (!(nm & (1u << i))) {
453                     ovs_fatal(0, "%s: %s is not a valid netmask",
454                               str, netmask);
455                 }
456             }
457         } else {
458             int prefix = atoi(netmask);
459             if (prefix <= 0 || prefix > 32) {
460                 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
461                           str);
462             }
463             n_wild = 32 - prefix;
464         }
465     } else {
466         n_wild = 0;
467     }
468
469     free(str);
470     return n_wild;
471 }
472
473 static void *
474 put_action(struct ofpbuf *b, size_t size, uint16_t type)
475 {
476     struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
477     ah->type = htons(type);
478     ah->len = htons(size);
479     return ah;
480 }
481
482 static struct ofp_action_output *
483 put_output_action(struct ofpbuf *b, uint16_t port)
484 {
485     struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
486     oao->port = htons(port);
487     return oao;
488 }
489
490 static void
491 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
492 {
493     struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
494     str_to_mac(addr, oada->dl_addr);
495 }
496
497
498 static bool
499 parse_port_name(const char *name, uint16_t *port)
500 {
501     struct pair {
502         const char *name;
503         uint16_t value;
504     };
505     static const struct pair pairs[] = {
506 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
507         DEF_PAIR(IN_PORT),
508         DEF_PAIR(TABLE),
509         DEF_PAIR(NORMAL),
510         DEF_PAIR(FLOOD),
511         DEF_PAIR(ALL),
512         DEF_PAIR(CONTROLLER),
513         DEF_PAIR(LOCAL),
514         DEF_PAIR(NONE),
515 #undef DEF_PAIR
516     };
517     static const int n_pairs = ARRAY_SIZE(pairs);
518     size_t i;
519
520     for (i = 0; i < n_pairs; i++) {
521         if (!strcasecmp(name, pairs[i].name)) {
522             *port = pairs[i].value;
523             return true;
524         }
525     }
526     return false;
527 }
528
529 static void
530 str_to_action(char *str, struct ofpbuf *b)
531 {
532     char *act, *arg;
533     char *saveptr = NULL;
534     bool drop = false;
535     int n_actions;
536
537     for (act = strtok_r(str, ", \t\r\n", &saveptr), n_actions = 0; act;
538          act = strtok_r(NULL, ", \t\r\n", &saveptr), n_actions++) 
539     {
540         uint16_t port;
541
542         if (drop) {
543             ovs_fatal(0, "Drop actions must not be followed by other actions");
544         }
545
546         /* Arguments are separated by colons */
547         arg = strchr(act, ':');
548         if (arg) {
549             *arg = '\0';
550             arg++;
551         }
552
553         if (!strcasecmp(act, "mod_vlan_vid")) {
554             struct ofp_action_vlan_vid *va;
555             va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
556             va->vlan_vid = htons(str_to_u32(arg));
557         } else if (!strcasecmp(act, "mod_vlan_pcp")) {
558             struct ofp_action_vlan_pcp *va;
559             va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
560             va->vlan_pcp = str_to_u32(arg);
561         } else if (!strcasecmp(act, "strip_vlan")) {
562             struct ofp_action_header *ah;
563             ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
564             ah->type = htons(OFPAT_STRIP_VLAN);
565         } else if (!strcasecmp(act, "mod_dl_src")) {
566             put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
567         } else if (!strcasecmp(act, "mod_dl_dst")) {
568             put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
569         } else if (!strcasecmp(act, "mod_nw_src")) {
570             struct ofp_action_nw_addr *na;
571             na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
572             str_to_ip(arg, &na->nw_addr);
573         } else if (!strcasecmp(act, "mod_nw_dst")) {
574             struct ofp_action_nw_addr *na;
575             na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
576             str_to_ip(arg, &na->nw_addr);
577         } else if (!strcasecmp(act, "mod_tp_src")) {
578             struct ofp_action_tp_port *ta;
579             ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
580             ta->tp_port = htons(str_to_u32(arg));
581         } else if (!strcasecmp(act, "mod_tp_dst")) {
582             struct ofp_action_tp_port *ta;
583             ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
584             ta->tp_port = htons(str_to_u32(arg));
585         } else if (!strcasecmp(act, "output")) {
586             put_output_action(b, str_to_u32(arg));
587         } else if (!strcasecmp(act, "drop")) {
588             /* A drop action in OpenFlow occurs by just not setting 
589              * an action. */
590             drop = true;
591             if (n_actions) {
592                 ovs_fatal(0, "Drop actions must not be preceded by other "
593                           "actions");
594             }
595         } else if (!strcasecmp(act, "CONTROLLER")) {
596             struct ofp_action_output *oao;
597             oao = put_output_action(b, OFPP_CONTROLLER);
598
599             /* Unless a numeric argument is specified, we send the whole
600              * packet to the controller. */
601             if (arg && (strspn(act, "0123456789") == strlen(act))) {
602                oao->max_len = htons(str_to_u32(arg));
603             } else {
604                 oao->max_len = htons(UINT16_MAX);
605             }
606         } else if (parse_port_name(act, &port)) {
607             put_output_action(b, port);
608         } else if (strspn(act, "0123456789") == strlen(act)) {
609             put_output_action(b, str_to_u32(act));
610         } else {
611             ovs_fatal(0, "Unknown action: %s", act);
612         }
613     }
614 }
615
616 struct protocol {
617     const char *name;
618     uint16_t dl_type;
619     uint8_t nw_proto;
620 };
621
622 static bool
623 parse_protocol(const char *name, const struct protocol **p_out)
624 {
625     static const struct protocol protocols[] = {
626         { "ip", ETH_TYPE_IP, 0 },
627         { "arp", ETH_TYPE_ARP, 0 },
628         { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
629         { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
630         { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
631     };
632     const struct protocol *p;
633
634     for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
635         if (!strcmp(p->name, name)) {
636             *p_out = p;
637             return true;
638         }
639     }
640     *p_out = NULL;
641     return false;
642 }
643
644 struct field {
645     const char *name;
646     uint32_t wildcard;
647     enum { F_U8, F_U16, F_MAC, F_IP } type;
648     size_t offset, shift;
649 };
650
651 static bool
652 parse_field(const char *name, const struct field **f_out) 
653 {
654 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
655     static const struct field fields[] = { 
656         { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port), 0 },
657         { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan), 0 },
658         { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src), 0 },
659         { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst), 0 },
660         { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type), 0 },
661         { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
662           F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
663         { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
664           F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
665         { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto), 0 },
666         { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src), 0 },
667         { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst), 0 },
668         { "icmp_type", OFPFW_ICMP_TYPE, F_U16, F_OFS(icmp_type), 0 },
669         { "icmp_code", OFPFW_ICMP_CODE, F_U16, F_OFS(icmp_code), 0 }
670     };
671     const struct field *f;
672
673     for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
674         if (!strcmp(f->name, name)) {
675             *f_out = f;
676             return true;
677         }
678     }
679     *f_out = NULL;
680     return false;
681 }
682
683 static void
684 str_to_flow(char *string, struct ofp_match *match, struct ofpbuf *actions,
685             uint8_t *table_idx, uint16_t *out_port, uint16_t *priority, 
686             uint16_t *idle_timeout, uint16_t *hard_timeout)
687 {
688     char *save_ptr = NULL;
689     char *name;
690     uint32_t wildcards;
691
692     if (table_idx) {
693         *table_idx = 0xff;
694     }
695     if (out_port) {
696         *out_port = OFPP_NONE;
697     }
698     if (priority) {
699         *priority = OFP_DEFAULT_PRIORITY;
700     }
701     if (idle_timeout) {
702         *idle_timeout = DEFAULT_IDLE_TIMEOUT;
703     }
704     if (hard_timeout) {
705         *hard_timeout = OFP_FLOW_PERMANENT;
706     }
707     if (actions) {
708         char *act_str = strstr(string, "action");
709         if (!act_str) {
710             ovs_fatal(0, "must specify an action");
711         }
712         *(act_str-1) = '\0';
713
714         act_str = strchr(act_str, '=');
715         if (!act_str) {
716             ovs_fatal(0, "must specify an action");
717         }
718
719         act_str++;
720
721         str_to_action(act_str, actions);
722     }
723     memset(match, 0, sizeof *match);
724     wildcards = OFPFW_ALL;
725     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
726          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
727         const struct protocol *p;
728
729         if (parse_protocol(name, &p)) {
730             wildcards &= ~OFPFW_DL_TYPE;
731             match->dl_type = htons(p->dl_type);
732             if (p->nw_proto) {
733                 wildcards &= ~OFPFW_NW_PROTO;
734                 match->nw_proto = p->nw_proto;
735             }
736         } else {
737             const struct field *f;
738             char *value;
739
740             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
741             if (!value) {
742                 ovs_fatal(0, "field %s missing value", name);
743             }
744         
745             if (table_idx && !strcmp(name, "table")) {
746                 *table_idx = atoi(value);
747             } else if (out_port && !strcmp(name, "out_port")) {
748                 *out_port = atoi(value);
749             } else if (priority && !strcmp(name, "priority")) {
750                 *priority = atoi(value);
751             } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
752                 *idle_timeout = atoi(value);
753             } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
754                 *hard_timeout = atoi(value);
755             } else if (parse_field(name, &f)) {
756                 void *data = (char *) match + f->offset;
757                 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
758                     wildcards |= f->wildcard;
759                 } else {
760                     wildcards &= ~f->wildcard;
761                     if (f->wildcard == OFPFW_IN_PORT
762                         && parse_port_name(value, (uint16_t *) data)) {
763                         /* Nothing to do. */
764                     } else if (f->type == F_U8) {
765                         *(uint8_t *) data = str_to_u32(value);
766                     } else if (f->type == F_U16) {
767                         *(uint16_t *) data = htons(str_to_u32(value));
768                     } else if (f->type == F_MAC) {
769                         str_to_mac(value, data);
770                     } else if (f->type == F_IP) {
771                         wildcards |= str_to_ip(value, data) << f->shift;
772                     } else {
773                         NOT_REACHED();
774                     }
775                 }
776             } else {
777                 ovs_fatal(0, "unknown keyword %s", name);
778             }
779         }
780     }
781     match->wildcards = htonl(wildcards);
782 }
783
784 static void
785 do_dump_flows(int argc, char *argv[])
786 {
787     struct ofp_flow_stats_request *req;
788     uint16_t out_port;
789     struct ofpbuf *request;
790
791     req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
792     str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
793                 &req->table_id, &out_port, NULL, NULL, NULL);
794     memset(&req->pad, 0, sizeof req->pad);
795     req->out_port = htons(out_port);
796
797     dump_stats_transaction(argv[1], request);
798 }
799
800 static void
801 do_dump_aggregate(int argc, char *argv[])
802 {
803     struct ofp_aggregate_stats_request *req;
804     struct ofpbuf *request;
805     uint16_t out_port;
806
807     req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
808     str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
809                 &req->table_id, &out_port, NULL, NULL, NULL);
810     memset(&req->pad, 0, sizeof req->pad);
811     req->out_port = htons(out_port);
812
813     dump_stats_transaction(argv[1], request);
814 }
815
816 static void
817 do_add_flow(int argc UNUSED, char *argv[])
818 {
819     struct vconn *vconn;
820     struct ofpbuf *buffer;
821     struct ofp_flow_mod *ofm;
822     uint16_t priority, idle_timeout, hard_timeout;
823     struct ofp_match match;
824
825     /* Parse and send.  str_to_flow() will expand and reallocate the data in
826      * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
827     make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
828     str_to_flow(argv[2], &match, buffer,
829                 NULL, NULL, &priority, &idle_timeout, &hard_timeout);
830     ofm = buffer->data;
831     ofm->match = match;
832     ofm->command = htons(OFPFC_ADD);
833     ofm->idle_timeout = htons(idle_timeout);
834     ofm->hard_timeout = htons(hard_timeout);
835     ofm->buffer_id = htonl(UINT32_MAX);
836     ofm->priority = htons(priority);
837     ofm->reserved = htonl(0);
838
839     open_vconn(argv[1], &vconn);
840     send_openflow_buffer(vconn, buffer);
841     vconn_close(vconn);
842 }
843
844 static void
845 do_add_flows(int argc UNUSED, char *argv[])
846 {
847     struct vconn *vconn;
848     FILE *file;
849     char line[1024];
850
851     file = fopen(argv[2], "r");
852     if (file == NULL) {
853         ovs_fatal(errno, "%s: open", argv[2]);
854     }
855
856     open_vconn(argv[1], &vconn);
857     while (fgets(line, sizeof line, file)) {
858         struct ofpbuf *buffer;
859         struct ofp_flow_mod *ofm;
860         uint16_t priority, idle_timeout, hard_timeout;
861         struct ofp_match match;
862
863         char *comment;
864
865         /* Delete comments. */
866         comment = strchr(line, '#');
867         if (comment) {
868             *comment = '\0';
869         }
870
871         /* Drop empty lines. */
872         if (line[strspn(line, " \t\n")] == '\0') {
873             continue;
874         }
875
876         /* Parse and send.  str_to_flow() will expand and reallocate the data
877          * in 'buffer', so we can't keep pointers to across the str_to_flow()
878          * call. */
879         ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
880         str_to_flow(line, &match, buffer,
881                     NULL, NULL, &priority, &idle_timeout, &hard_timeout);
882         ofm = buffer->data;
883         ofm->match = match;
884         ofm->command = htons(OFPFC_ADD);
885         ofm->idle_timeout = htons(idle_timeout);
886         ofm->hard_timeout = htons(hard_timeout);
887         ofm->buffer_id = htonl(UINT32_MAX);
888         ofm->priority = htons(priority);
889         ofm->reserved = htonl(0);
890
891         send_openflow_buffer(vconn, buffer);
892     }
893     vconn_close(vconn);
894     fclose(file);
895 }
896
897 static void
898 do_mod_flows(int argc UNUSED, char *argv[])
899 {
900     uint16_t priority, idle_timeout, hard_timeout;
901     struct vconn *vconn;
902     struct ofpbuf *buffer;
903     struct ofp_flow_mod *ofm;
904     struct ofp_match match;
905
906     /* Parse and send.  str_to_flow() will expand and reallocate the data in
907      * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
908     make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
909     str_to_flow(argv[2], &match, buffer,
910                 NULL, NULL, &priority, &idle_timeout, &hard_timeout);
911     ofm = buffer->data;
912     ofm->match = match;
913     if (strict) {
914         ofm->command = htons(OFPFC_MODIFY_STRICT);
915     } else {
916         ofm->command = htons(OFPFC_MODIFY);
917     }
918     ofm->idle_timeout = htons(idle_timeout);
919     ofm->hard_timeout = htons(hard_timeout);
920     ofm->buffer_id = htonl(UINT32_MAX);
921     ofm->priority = htons(priority);
922     ofm->reserved = htonl(0);
923
924     open_vconn(argv[1], &vconn);
925     send_openflow_buffer(vconn, buffer);
926     vconn_close(vconn);
927 }
928
929 static void do_del_flows(int argc, char *argv[])
930 {
931     struct vconn *vconn;
932     uint16_t priority;
933     uint16_t out_port;
934     struct ofpbuf *buffer;
935     struct ofp_flow_mod *ofm;
936
937     /* Parse and send. */
938     ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
939     str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL, 
940                 &out_port, &priority, NULL, NULL);
941     if (strict) {
942         ofm->command = htons(OFPFC_DELETE_STRICT);
943     } else {
944         ofm->command = htons(OFPFC_DELETE);
945     }
946     ofm->idle_timeout = htons(0);
947     ofm->hard_timeout = htons(0);
948     ofm->buffer_id = htonl(UINT32_MAX);
949     ofm->out_port = htons(out_port);
950     ofm->priority = htons(priority);
951     ofm->reserved = htonl(0);
952
953     open_vconn(argv[1], &vconn);
954     send_openflow_buffer(vconn, buffer);
955     vconn_close(vconn);
956 }
957
958 static void
959 do_monitor(int argc UNUSED, char *argv[])
960 {
961     struct vconn *vconn;
962
963     open_vconn(argv[1], &vconn);
964     if (argc > 2) {
965         int miss_send_len = atoi(argv[2]);
966         int send_flow_exp = argc > 3 ? atoi(argv[3]) : 0;
967         struct ofp_switch_config *osc;
968         struct ofpbuf *buf;
969
970         osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
971         osc->flags = htons(send_flow_exp ? OFPC_SEND_FLOW_EXP : 0);
972         osc->miss_send_len = htons(miss_send_len);
973         send_openflow_buffer(vconn, buf);
974     }
975     for (;;) {
976         struct ofpbuf *b;
977         run(vconn_recv_block(vconn, &b), "vconn_recv");
978         ofp_print(stderr, b->data, b->size, 2);
979         ofpbuf_delete(b);
980     }
981 }
982
983 static void
984 do_dump_ports(int argc UNUSED, char *argv[])
985 {
986     dump_trivial_stats_transaction(argv[1], OFPST_PORT);
987 }
988
989 static void
990 do_probe(int argc UNUSED, char *argv[])
991 {
992     struct ofpbuf *request;
993     struct vconn *vconn;
994     struct ofpbuf *reply;
995
996     make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
997     open_vconn(argv[1], &vconn);
998     run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
999     if (reply->size != sizeof(struct ofp_header)) {
1000         ovs_fatal(0, "reply does not match request");
1001     }
1002     ofpbuf_delete(reply);
1003     vconn_close(vconn);
1004 }
1005
1006 static void
1007 do_mod_port(int argc UNUSED, char *argv[])
1008 {
1009     struct ofpbuf *request, *reply;
1010     struct ofp_switch_features *osf;
1011     struct ofp_port_mod *opm;
1012     struct vconn *vconn;
1013     char *endptr;
1014     int n_ports;
1015     int port_idx;
1016     int port_no;
1017     
1018
1019     /* Check if the argument is a port index.  Otherwise, treat it as
1020      * the port name. */
1021     port_no = strtol(argv[2], &endptr, 10);
1022     if (port_no == 0 && endptr == argv[2]) {
1023         port_no = -1;
1024     }
1025
1026     /* Send a "Features Request" to get the information we need in order 
1027      * to modify the port. */
1028     make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
1029     open_vconn(argv[1], &vconn);
1030     run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1031
1032     osf = reply->data;
1033     n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1034
1035     for (port_idx = 0; port_idx < n_ports; port_idx++) {
1036         if (port_no != -1) {
1037             /* Check argument as a port index */
1038             if (osf->ports[port_idx].port_no == htons(port_no)) {
1039                 break;
1040             }
1041         } else {
1042             /* Check argument as an interface name */
1043             if (!strncmp((char *)osf->ports[port_idx].name, argv[2], 
1044                         sizeof osf->ports[0].name)) {
1045                 break;
1046             }
1047
1048         }
1049     }
1050     if (port_idx == n_ports) {
1051         ovs_fatal(0, "couldn't find monitored port: %s", argv[2]);
1052     }
1053
1054     opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1055     opm->port_no = osf->ports[port_idx].port_no;
1056     memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
1057     opm->config = htonl(0);
1058     opm->mask = htonl(0);
1059     opm->advertise = htonl(0);
1060
1061     printf("modifying port: %s\n", osf->ports[port_idx].name);
1062
1063     if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
1064         opm->mask |= htonl(OFPPC_PORT_DOWN);
1065     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN, 
1066                 sizeof MOD_PORT_CMD_DOWN)) {
1067         opm->mask |= htonl(OFPPC_PORT_DOWN);
1068         opm->config |= htonl(OFPPC_PORT_DOWN);
1069     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD, 
1070                 sizeof MOD_PORT_CMD_FLOOD)) {
1071         opm->mask |= htonl(OFPPC_NO_FLOOD);
1072     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD, 
1073                 sizeof MOD_PORT_CMD_NOFLOOD)) {
1074         opm->mask |= htonl(OFPPC_NO_FLOOD);
1075         opm->config |= htonl(OFPPC_NO_FLOOD);
1076     } else {
1077         ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1078     }
1079
1080     send_openflow_buffer(vconn, request);
1081
1082     ofpbuf_delete(reply);
1083     vconn_close(vconn);
1084 }
1085
1086 static void
1087 do_ping(int argc, char *argv[])
1088 {
1089     size_t max_payload = 65535 - sizeof(struct ofp_header);
1090     unsigned int payload;
1091     struct vconn *vconn;
1092     int i;
1093
1094     payload = argc > 2 ? atoi(argv[2]) : 64;
1095     if (payload > max_payload) {
1096         ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1097     }
1098
1099     open_vconn(argv[1], &vconn);
1100     for (i = 0; i < 10; i++) {
1101         struct timeval start, end;
1102         struct ofpbuf *request, *reply;
1103         struct ofp_header *rq_hdr, *rpy_hdr;
1104
1105         rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1106                                OFPT_ECHO_REQUEST, &request);
1107         random_bytes(rq_hdr + 1, payload);
1108
1109         gettimeofday(&start, NULL);
1110         run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1111         gettimeofday(&end, NULL);
1112
1113         rpy_hdr = reply->data;
1114         if (reply->size != request->size
1115             || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1116             || rpy_hdr->xid != rq_hdr->xid
1117             || rpy_hdr->type != OFPT_ECHO_REPLY) {
1118             printf("Reply does not match request.  Request:\n");
1119             ofp_print(stdout, request, request->size, 2);
1120             printf("Reply:\n");
1121             ofp_print(stdout, reply, reply->size, 2);
1122         }
1123         printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1124                reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1125                    (1000*(double)(end.tv_sec - start.tv_sec))
1126                    + (.001*(end.tv_usec - start.tv_usec)));
1127         ofpbuf_delete(request);
1128         ofpbuf_delete(reply);
1129     }
1130     vconn_close(vconn);
1131 }
1132
1133 static void
1134 do_benchmark(int argc UNUSED, char *argv[])
1135 {
1136     size_t max_payload = 65535 - sizeof(struct ofp_header);
1137     struct timeval start, end;
1138     unsigned int payload_size, message_size;
1139     struct vconn *vconn;
1140     double duration;
1141     int count;
1142     int i;
1143
1144     payload_size = atoi(argv[2]);
1145     if (payload_size > max_payload) {
1146         ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1147     }
1148     message_size = sizeof(struct ofp_header) + payload_size;
1149
1150     count = atoi(argv[3]);
1151
1152     printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1153            count, message_size, count * message_size);
1154
1155     open_vconn(argv[1], &vconn);
1156     gettimeofday(&start, NULL);
1157     for (i = 0; i < count; i++) {
1158         struct ofpbuf *request, *reply;
1159         struct ofp_header *rq_hdr;
1160
1161         rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1162         memset(rq_hdr + 1, 0, payload_size);
1163         run(vconn_transact(vconn, request, &reply), "transact");
1164         ofpbuf_delete(reply);
1165     }
1166     gettimeofday(&end, NULL);
1167     vconn_close(vconn);
1168
1169     duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1170                 + (.001*(end.tv_usec - start.tv_usec)));
1171     printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1172            duration, count / (duration / 1000.0),
1173            count * message_size / (duration / 1000.0));
1174 }
1175
1176 static void
1177 do_help(int argc UNUSED, char *argv[] UNUSED)
1178 {
1179     usage();
1180 }
1181
1182 static const struct command all_commands[] = {
1183     { "show", 1, 1, do_show },
1184     { "status", 1, 2, do_status },
1185     { "monitor", 1, 3, do_monitor },
1186     { "dump-desc", 1, 1, do_dump_desc },
1187     { "dump-tables", 1, 1, do_dump_tables },
1188     { "dump-flows", 1, 2, do_dump_flows },
1189     { "dump-aggregate", 1, 2, do_dump_aggregate },
1190     { "add-flow", 2, 2, do_add_flow },
1191     { "add-flows", 2, 2, do_add_flows },
1192     { "mod-flows", 2, 2, do_mod_flows },
1193     { "del-flows", 1, 2, do_del_flows },
1194     { "dump-ports", 1, 1, do_dump_ports },
1195     { "mod-port", 3, 3, do_mod_port },
1196     { "probe", 1, 1, do_probe },
1197     { "ping", 1, 2, do_ping },
1198     { "benchmark", 3, 3, do_benchmark },
1199     { "help", 0, INT_MAX, do_help },
1200     { NULL, 0, 0, NULL },
1201 };