2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <sys/socket.h>
27 #include <sys/fcntl.h>
31 #include "byte-order.h"
32 #include "classifier.h"
33 #include "command-line.h"
37 #include "dynamic-string.h"
40 #include "ofp-actions.h"
41 #include "ofp-errors.h"
42 #include "ofp-parse.h"
43 #include "ofp-print.h"
46 #include "ofproto/ofproto.h"
47 #include "openflow/nicira-ext.h"
48 #include "openflow/openflow.h"
50 #include "poll-loop.h"
52 #include "stream-ssl.h"
58 #include "meta-flow.h"
61 VLOG_DEFINE_THIS_MODULE(ofctl);
63 /* --strict: Use strict matching for flow mod commands? Additionally governs
64 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
68 /* --readd: If true, on replace-flows, re-add even flows that have not changed
69 * (to reset flow counters). */
72 /* -F, --flow-format: Allowed protocols. By default, any protocol is
74 static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY;
76 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
77 * commands. Either one of NXPIF_* to force a particular packet_in format, or
78 * -1 to let ovs-ofctl choose the default. */
79 static int preferred_packet_in_format = -1;
81 /* -m, --more: Additional verbosity for ofp-print functions. */
84 /* --timestamp: Print a timestamp before each received packet on "monitor" and
86 static bool timestamp;
88 /* --sort, --rsort: Sort order. */
89 enum sort_order { SORT_ASC, SORT_DESC };
90 struct sort_criterion {
91 const struct mf_field *field; /* NULL means to sort by priority. */
92 enum sort_order order;
94 static struct sort_criterion *criteria;
95 static size_t n_criteria, allocated_criteria;
97 static const struct command all_commands[];
99 static void usage(void) NO_RETURN;
100 static void parse_options(int argc, char *argv[]);
102 static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid,
103 struct ofpbuf **replyp,
104 struct ofputil_flow_stats *,
105 struct ofpbuf *ofpacts);
107 main(int argc, char *argv[])
109 set_program_name(argv[0]);
110 parse_options(argc, argv);
111 signal(SIGPIPE, SIG_IGN);
112 run_command(argc - optind, argv + optind, all_commands);
117 add_sort_criterion(enum sort_order order, const char *field)
119 struct sort_criterion *sc;
121 if (n_criteria >= allocated_criteria) {
122 criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria);
125 sc = &criteria[n_criteria++];
126 if (!field || !strcasecmp(field, "priority")) {
129 sc->field = mf_from_name(field);
131 ovs_fatal(0, "%s: unknown field name", field);
138 parse_options(int argc, char *argv[])
141 OPT_STRICT = UCHAR_MAX + 1,
149 static struct option long_options[] = {
150 {"timeout", required_argument, NULL, 't'},
151 {"strict", no_argument, NULL, OPT_STRICT},
152 {"readd", no_argument, NULL, OPT_READD},
153 {"flow-format", required_argument, NULL, 'F'},
154 {"packet-in-format", required_argument, NULL, 'P'},
155 {"more", no_argument, NULL, 'm'},
156 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
157 {"sort", optional_argument, NULL, OPT_SORT},
158 {"rsort", optional_argument, NULL, OPT_RSORT},
159 {"help", no_argument, NULL, 'h'},
160 {"version", no_argument, NULL, 'V'},
163 STREAM_SSL_LONG_OPTIONS,
166 char *short_options = long_options_to_short_options(long_options);
169 unsigned long int timeout;
172 c = getopt_long(argc, argv, short_options, long_options, NULL);
179 timeout = strtoul(optarg, NULL, 10);
181 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
189 allowed_protocols = ofputil_protocols_from_string(optarg);
190 if (!allowed_protocols) {
191 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
196 preferred_packet_in_format =
197 ofputil_packet_in_format_from_string(optarg);
198 if (preferred_packet_in_format < 0) {
199 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
211 ovs_print_version(OFP10_VERSION, OFP10_VERSION);
227 add_sort_criterion(SORT_ASC, optarg);
231 add_sort_criterion(SORT_DESC, optarg);
234 DAEMON_OPTION_HANDLERS
236 STREAM_SSL_OPTION_HANDLERS
247 /* Always do a final sort pass based on priority. */
248 add_sort_criterion(SORT_DESC, "priority");
257 printf("%s: OpenFlow switch management utility\n"
258 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
259 "\nFor OpenFlow switches:\n"
260 " show SWITCH show OpenFlow information\n"
261 " dump-desc SWITCH print switch description\n"
262 " dump-tables SWITCH print table stats\n"
263 " mod-port SWITCH IFACE ACT modify port behavior\n"
264 " get-frags SWITCH print fragment handling behavior\n"
265 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
266 " dump-ports SWITCH [PORT] print port statistics\n"
267 " dump-ports-desc SWITCH print port descriptions\n"
268 " dump-flows SWITCH print all flow entries\n"
269 " dump-flows SWITCH FLOW print matching FLOWs\n"
270 " dump-aggregate SWITCH print aggregate flow statistics\n"
271 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
272 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
273 " add-flow SWITCH FLOW add flow described by FLOW\n"
274 " add-flows SWITCH FILE add flows from FILE\n"
275 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
276 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
277 " replace-flows SWITCH FILE replace flows with those in FILE\n"
278 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
279 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
280 " execute ACTIONS on PACKET\n"
281 " monitor SWITCH [MISSLEN] [invalid_ttl]\n"
282 " print packets received from SWITCH\n"
283 " snoop SWITCH snoop on SWITCH and its controller\n"
284 "\nFor OpenFlow switches and controllers:\n"
285 " probe TARGET probe whether TARGET is up\n"
286 " ping TARGET [N] latency of N-byte echos\n"
287 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
288 "where SWITCH or TARGET is an active OpenFlow connection method.\n",
289 program_name, program_name);
290 vconn_usage(true, false, false);
293 printf("\nOther options:\n"
294 " --strict use strict match for flow commands\n"
295 " --readd replace flows that haven't changed\n"
296 " -F, --flow-format=FORMAT force particular flow format\n"
297 " -P, --packet-in-format=FRMT force particular packet in format\n"
298 " -m, --more be more verbose printing OpenFlow\n"
299 " --timestamp (monitor, snoop) print timestamps\n"
300 " -t, --timeout=SECS give up after SECS seconds\n"
301 " --sort[=field] sort in ascending order\n"
302 " --rsort[=field] sort in descending order\n"
303 " -h, --help display this help message\n"
304 " -V, --version display version information\n");
309 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
310 const char *argv[] OVS_UNUSED, void *exiting_)
312 bool *exiting = exiting_;
314 unixctl_command_reply(conn, NULL);
317 static void run(int retval, const char *message, ...)
320 static void run(int retval, const char *message, ...)
325 va_start(args, message);
326 ovs_fatal_valist(retval, message, args);
330 /* Generic commands. */
333 open_vconn_socket(const char *name, struct vconn **vconnp)
335 char *vconn_name = xasprintf("unix:%s", name);
336 VLOG_DBG("connecting to %s", vconn_name);
337 run(vconn_open_block(vconn_name, OFP10_VERSION, vconnp),
338 "connecting to %s", vconn_name);
342 static enum ofputil_protocol
343 open_vconn__(const char *name, const char *default_suffix,
344 struct vconn **vconnp)
346 char *datapath_name, *datapath_type, *socket_name;
347 enum ofputil_protocol protocol;
352 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, default_suffix);
354 ofproto_parse_name(name, &datapath_name, &datapath_type);
355 socket_name = xasprintf("%s/%s.%s",
356 ovs_rundir(), datapath_name, default_suffix);
360 if (strchr(name, ':')) {
361 run(vconn_open_block(name, OFP10_VERSION, vconnp),
362 "connecting to %s", name);
363 } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
364 open_vconn_socket(name, vconnp);
365 } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
366 open_vconn_socket(bridge_path, vconnp);
367 } else if (!stat(socket_name, &s)) {
368 if (!S_ISSOCK(s.st_mode)) {
369 ovs_fatal(0, "cannot connect to %s: %s is not a socket",
372 open_vconn_socket(socket_name, vconnp);
374 ovs_fatal(0, "%s is not a bridge or a socket", name);
380 ofp_version = vconn_get_version(*vconnp);
381 protocol = ofputil_protocol_from_ofp_version(ofp_version);
383 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
389 static enum ofputil_protocol
390 open_vconn(const char *name, struct vconn **vconnp)
392 return open_vconn__(name, "mgmt", vconnp);
396 alloc_stats_request(size_t rq_len, uint16_t type, struct ofpbuf **bufferp)
398 struct ofp_stats_msg *rq;
400 rq = make_openflow(rq_len, OFPT10_STATS_REQUEST, bufferp);
401 rq->type = htons(type);
402 rq->flags = htons(0);
407 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
409 update_openflow_length(buffer);
410 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
414 dump_transaction(const char *vconn_name, struct ofpbuf *request)
417 struct ofpbuf *reply;
419 update_openflow_length(request);
420 open_vconn(vconn_name, &vconn);
421 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
422 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
423 ofpbuf_delete(reply);
428 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
430 struct ofpbuf *request;
431 make_openflow(sizeof(struct ofp_header), request_type, &request);
432 dump_transaction(vconn_name, request);
436 dump_stats_transaction__(struct vconn *vconn, struct ofpbuf *request)
438 ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid;
439 ovs_be16 stats_type = ((struct ofp_stats_msg *) request->data)->type;
442 send_openflow_buffer(vconn, request);
445 struct ofpbuf *reply;
447 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
448 recv_xid = ((struct ofp_header *) reply->data)->xid;
449 if (send_xid == recv_xid) {
450 const struct ofp_stats_msg *osm = reply->data;
451 const struct ofp_header *oh = reply->data;
453 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
455 if (oh->type == OFPT_ERROR) {
457 } else if (oh->type == OFPT10_STATS_REPLY
458 && osm->type == stats_type) {
459 done = !(ntohs(osm->flags) & OFPSF_REPLY_MORE);
461 ovs_fatal(0, "received bad reply: %s",
462 ofp_to_string(reply->data, reply->size,
466 VLOG_DBG("received reply with xid %08"PRIx32" "
467 "!= expected %08"PRIx32, recv_xid, send_xid);
469 ofpbuf_delete(reply);
474 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
478 open_vconn(vconn_name, &vconn);
479 dump_stats_transaction__(vconn, request);
484 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
486 struct ofpbuf *request;
487 alloc_stats_request(sizeof(struct ofp_stats_msg), stats_type, &request);
488 dump_stats_transaction(vconn_name, request);
491 /* Sends 'request', which should be a request that only has a reply if an error
492 * occurs, and waits for it to succeed or fail. If an error does occur, prints
493 * it and exits with an error.
495 * Destroys all of the 'requests'. */
497 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
499 struct ofpbuf *request, *reply;
501 LIST_FOR_EACH (request, list_node, requests) {
502 update_openflow_length(request);
505 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
506 "talking to %s", vconn_get_name(vconn));
508 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
511 ofpbuf_delete(reply);
514 /* Sends 'request', which should be a request that only has a reply if an error
515 * occurs, and waits for it to succeed or fail. If an error does occur, prints
516 * it and exits with an error.
518 * Destroys 'request'. */
520 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
522 struct list requests;
524 list_init(&requests);
525 list_push_back(&requests, &request->list_node);
526 transact_multiple_noreply(vconn, &requests);
530 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
532 struct ofp_switch_config *config;
533 struct ofp_header *header;
534 struct ofpbuf *request;
535 struct ofpbuf *reply;
537 make_openflow(sizeof(struct ofp_header), OFPT_GET_CONFIG_REQUEST,
539 run(vconn_transact(vconn, request, &reply),
540 "talking to %s", vconn_get_name(vconn));
542 header = reply->data;
543 if (header->type != OFPT_GET_CONFIG_REPLY ||
544 header->length != htons(sizeof *config)) {
545 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
548 config = reply->data;
551 ofpbuf_delete(reply);
555 set_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
557 struct ofp_switch_config *config;
558 struct ofp_header save_header;
559 struct ofpbuf *request;
561 config = make_openflow(sizeof *config, OFPT_SET_CONFIG, &request);
562 save_header = config->header;
564 config->header = save_header;
566 transact_noreply(vconn, request);
570 ofctl_show(int argc OVS_UNUSED, char *argv[])
572 const char *vconn_name = argv[1];
574 struct ofpbuf *request;
575 struct ofpbuf *reply;
578 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST,
580 open_vconn(vconn_name, &vconn);
581 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
583 trunc = ofputil_switch_features_ports_trunc(reply);
584 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
586 ofpbuf_delete(reply);
590 /* The Features Reply may not contain all the ports, so send a
591 * Port Description stats request, which doesn't have size
593 dump_trivial_stats_transaction(vconn_name, OFPST_PORT_DESC);
595 dump_trivial_transaction(vconn_name, OFPT_GET_CONFIG_REQUEST);
599 ofctl_dump_desc(int argc OVS_UNUSED, char *argv[])
601 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
605 ofctl_dump_tables(int argc OVS_UNUSED, char *argv[])
607 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
611 fetch_port_by_features(const char *vconn_name,
612 const char *port_name, unsigned int port_no,
613 struct ofputil_phy_port *pp, bool *trunc)
615 struct ofputil_switch_features features;
616 const struct ofp_switch_features *osf;
617 struct ofpbuf *request, *reply;
623 /* Fetch the switch's ofp_switch_features. */
624 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
625 open_vconn(vconn_name, &vconn);
626 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
630 if (reply->size < sizeof *osf) {
631 ovs_fatal(0, "%s: received too-short features reply (only %zu bytes)",
632 vconn_name, reply->size);
636 if (ofputil_switch_features_ports_trunc(reply)) {
641 error = ofputil_decode_switch_features(osf, &features, &b);
643 ovs_fatal(0, "%s: failed to decode features reply (%s)",
644 vconn_name, ofperr_to_string(error));
647 while (!ofputil_pull_phy_port(osf->header.version, &b, pp)) {
648 if (port_no != UINT_MAX
649 ? port_no == pp->port_no
650 : !strcmp(pp->name, port_name)) {
657 ofpbuf_delete(reply);
662 fetch_port_by_stats(const char *vconn_name,
663 const char *port_name, unsigned int port_no,
664 struct ofputil_phy_port *pp)
666 struct ofpbuf *request;
673 alloc_stats_request(sizeof(struct ofp_stats_msg), OFPST_PORT_DESC,
675 send_xid = ((struct ofp_header *) request->data)->xid;
677 open_vconn(vconn_name, &vconn);
678 send_openflow_buffer(vconn, request);
681 struct ofpbuf *reply;
683 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
684 recv_xid = ((struct ofp_header *) reply->data)->xid;
685 if (send_xid == recv_xid) {
686 const struct ofputil_msg_type *type;
687 struct ofp_stats_msg *osm;
689 ofputil_decode_msg_type(reply->data, &type);
690 if (ofputil_msg_type_code(type) != OFPUTIL_OFPST_PORT_DESC_REPLY) {
691 ovs_fatal(0, "received bad reply: %s",
692 ofp_to_string(reply->data, reply->size,
696 osm = ofpbuf_at_assert(reply, 0, sizeof *osm);
697 done = !(ntohs(osm->flags) & OFPSF_REPLY_MORE);
700 /* We've already found the port, but we need to drain
701 * the queue of any other replies for this request. */
705 ofpbuf_use_const(&b, &osm->header, ntohs(osm->header.length));
706 ofpbuf_pull(&b, sizeof(struct ofp_stats_msg));
708 while (!ofputil_pull_phy_port(osm->header.version, &b, pp)) {
709 if (port_no != UINT_MAX ? port_no == pp->port_no
710 : !strcmp(pp->name, port_name)) {
716 VLOG_DBG("received reply with xid %08"PRIx32" "
717 "!= expected %08"PRIx32, recv_xid, send_xid);
719 ofpbuf_delete(reply);
727 /* Opens a connection to 'vconn_name', fetches the port structure for
728 * 'port_name' (which may be a port name or number), and copies it into
731 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
732 struct ofputil_phy_port *pp)
734 unsigned int port_no;
738 /* Try to interpret the argument as a port number. */
739 if (!str_to_uint(port_name, 10, &port_no)) {
743 /* Try to find the port based on the Features Reply. If it looks
744 * like the results may be truncated, then use the Port Description
745 * stats message introduced in OVS 1.7. */
746 found = fetch_port_by_features(vconn_name, port_name, port_no, pp,
749 found = fetch_port_by_stats(vconn_name, port_name, port_no, pp);
753 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
757 /* Returns the port number corresponding to 'port_name' (which may be a port
758 * name or number) within the switch 'vconn_name'. */
760 str_to_port_no(const char *vconn_name, const char *port_name)
762 unsigned int port_no;
764 if (str_to_uint(port_name, 10, &port_no)) {
767 struct ofputil_phy_port pp;
769 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
775 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
776 enum ofputil_protocol *cur)
779 struct ofpbuf *request, *reply;
780 enum ofputil_protocol next;
782 request = ofputil_encode_set_protocol(*cur, want, &next);
787 run(vconn_transact_noreply(vconn, request, &reply),
788 "talking to %s", vconn_get_name(vconn));
790 char *s = ofp_to_string(reply->data, reply->size, 2);
791 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
792 vconn_get_name(vconn), s);
794 ofpbuf_delete(reply);
802 static enum ofputil_protocol
803 set_protocol_for_flow_dump(struct vconn *vconn,
804 enum ofputil_protocol cur_protocol,
805 enum ofputil_protocol usable_protocols)
810 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
811 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
812 if (f & usable_protocols & allowed_protocols
813 && try_set_protocol(vconn, f, &cur_protocol)) {
818 usable_s = ofputil_protocols_to_string(usable_protocols);
819 if (usable_protocols & allowed_protocols) {
820 ovs_fatal(0, "switch does not support any of the usable flow "
821 "formats (%s)", usable_s);
823 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
824 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
825 "allowed flow formats (%s)", usable_s, allowed_s);
829 static struct vconn *
830 prepare_dump_flows(int argc, char *argv[], bool aggregate,
831 struct ofpbuf **requestp)
833 enum ofputil_protocol usable_protocols, protocol;
834 struct ofputil_flow_stats_request fsr;
837 parse_ofp_flow_stats_request_str(&fsr, aggregate, argc > 2 ? argv[2] : "");
838 usable_protocols = ofputil_flow_stats_request_usable_protocols(&fsr);
840 protocol = open_vconn(argv[1], &vconn);
841 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
842 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
847 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
849 struct ofpbuf *request;
852 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
853 dump_stats_transaction__(vconn, request);
858 compare_flows(const void *afs_, const void *bfs_)
860 const struct ofputil_flow_stats *afs = afs_;
861 const struct ofputil_flow_stats *bfs = bfs_;
862 const struct cls_rule *a = &afs->rule;
863 const struct cls_rule *b = &bfs->rule;
864 const struct sort_criterion *sc;
866 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
867 const struct mf_field *f = sc->field;
871 ret = a->priority < b->priority ? -1 : a->priority > b->priority;
875 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
876 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
878 /* Skip the test for sc->order, so that missing fields always
879 * sort to the end whether we're sorting in ascending or
880 * descending order. */
883 union mf_value aval, bval;
885 mf_get_value(f, &a->flow, &aval);
886 mf_get_value(f, &b->flow, &bval);
887 ret = memcmp(&aval, &bval, f->n_bytes);
892 return sc->order == SORT_ASC ? ret : -ret;
900 ofctl_dump_flows(int argc, char *argv[])
903 return ofctl_dump_flows__(argc, argv, false);
905 struct ofputil_flow_stats *fses;
906 size_t n_fses, allocated_fses;
907 struct ofpbuf *request;
908 struct ofpbuf ofpacts;
909 struct ofpbuf *reply;
915 vconn = prepare_dump_flows(argc, argv, false, &request);
916 send_xid = ((struct ofp_header *) request->data)->xid;
917 send_openflow_buffer(vconn, request);
920 n_fses = allocated_fses = 0;
922 ofpbuf_init(&ofpacts, 0);
924 struct ofputil_flow_stats *fs;
926 if (n_fses >= allocated_fses) {
927 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
931 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
935 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
938 ofpbuf_uninit(&ofpacts);
940 qsort(fses, n_fses, sizeof *fses, compare_flows);
943 for (i = 0; i < n_fses; i++) {
945 ofp_print_flow_stats(&s, &fses[i]);
950 for (i = 0; i < n_fses; i++) {
951 free(fses[i].ofpacts);
960 ofctl_dump_aggregate(int argc, char *argv[])
962 return ofctl_dump_flows__(argc, argv, true);
966 ofctl_queue_stats(int argc, char *argv[])
968 struct ofp_queue_stats_request *req;
969 struct ofpbuf *request;
971 req = alloc_stats_request(sizeof *req, OFPST_QUEUE, &request);
973 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
974 req->port_no = htons(str_to_port_no(argv[1], argv[2]));
976 req->port_no = htons(OFPP_ALL);
978 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
979 req->queue_id = htonl(atoi(argv[3]));
981 req->queue_id = htonl(OFPQ_ALL);
984 memset(req->pad, 0, sizeof req->pad);
986 dump_stats_transaction(argv[1], request);
989 static enum ofputil_protocol
990 open_vconn_for_flow_mod(const char *remote,
991 const struct ofputil_flow_mod *fms, size_t n_fms,
992 struct vconn **vconnp)
994 enum ofputil_protocol usable_protocols;
995 enum ofputil_protocol cur_protocol;
999 /* Figure out what flow formats will work. */
1000 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
1001 if (!(usable_protocols & allowed_protocols)) {
1002 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1003 usable_s = ofputil_protocols_to_string(usable_protocols);
1004 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1005 "allowed flow formats (%s)", usable_s, allowed_s);
1008 /* If the initial flow format is allowed and usable, keep it. */
1009 cur_protocol = open_vconn(remote, vconnp);
1010 if (usable_protocols & allowed_protocols & cur_protocol) {
1011 return cur_protocol;
1014 /* Otherwise try each flow format in turn. */
1015 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1016 enum ofputil_protocol f = 1 << i;
1018 if (f != cur_protocol
1019 && f & usable_protocols & allowed_protocols
1020 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1025 usable_s = ofputil_protocols_to_string(usable_protocols);
1026 ovs_fatal(0, "switch does not support any of the usable flow "
1027 "formats (%s)", usable_s);
1031 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1034 enum ofputil_protocol protocol;
1035 struct vconn *vconn;
1038 protocol = open_vconn_for_flow_mod(remote, fms, n_fms, &vconn);
1040 for (i = 0; i < n_fms; i++) {
1041 struct ofputil_flow_mod *fm = &fms[i];
1043 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1050 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1052 struct ofputil_flow_mod *fms = NULL;
1055 parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms);
1056 ofctl_flow_mod__(argv[1], fms, n_fms);
1061 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1063 if (argc > 2 && !strcmp(argv[2], "-")) {
1064 ofctl_flow_mod_file(argc, argv, command);
1066 struct ofputil_flow_mod fm;
1067 parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command, false);
1068 ofctl_flow_mod__(argv[1], &fm, 1);
1073 ofctl_add_flow(int argc, char *argv[])
1075 ofctl_flow_mod(argc, argv, OFPFC_ADD);
1079 ofctl_add_flows(int argc, char *argv[])
1081 ofctl_flow_mod_file(argc, argv, OFPFC_ADD);
1085 ofctl_mod_flows(int argc, char *argv[])
1087 ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1091 ofctl_del_flows(int argc, char *argv[])
1093 ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1097 set_packet_in_format(struct vconn *vconn,
1098 enum nx_packet_in_format packet_in_format)
1100 struct ofpbuf *spif = ofputil_make_set_packet_in_format(packet_in_format);
1101 transact_noreply(vconn, spif);
1102 VLOG_DBG("%s: using user-specified packet in format %s",
1103 vconn_get_name(vconn),
1104 ofputil_packet_in_format_to_string(packet_in_format));
1108 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1110 struct ofp_switch_config config;
1111 enum ofp_config_flags flags;
1113 fetch_switch_config(vconn, &config);
1114 flags = ntohs(config.flags);
1115 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1116 /* Set the invalid ttl config. */
1117 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1119 config.flags = htons(flags);
1120 set_switch_config(vconn, &config);
1122 /* Then retrieve the configuration to see if it really took. OpenFlow
1123 * doesn't define error reporting for bad modes, so this is all we can
1125 fetch_switch_config(vconn, &config);
1126 flags = ntohs(config.flags);
1127 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1128 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1129 "switch probably doesn't support mode)");
1136 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1137 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1138 * an error message and stores NULL in '*msgp'. */
1140 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1142 struct ofp_header *oh;
1145 msg = ofpbuf_new(strlen(hex) / 2);
1148 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1150 return "Trailing garbage in hex data";
1153 if (msg->size < sizeof(struct ofp_header)) {
1155 return "Message too short for OpenFlow";
1159 if (msg->size != ntohs(oh->length)) {
1161 return "Message size does not match length in OpenFlow header";
1169 ofctl_send(struct unixctl_conn *conn, int argc,
1170 const char *argv[], void *vconn_)
1172 struct vconn *vconn = vconn_;
1179 for (i = 1; i < argc; i++) {
1180 const char *error_msg;
1184 error_msg = openflow_from_hex(argv[i], &msg);
1186 ds_put_format(&reply, "%s\n", error_msg);
1191 fprintf(stderr, "send: ");
1192 ofp_print(stderr, msg->data, msg->size, verbosity);
1194 error = vconn_send_block(vconn, msg);
1197 ds_put_format(&reply, "%s\n", strerror(error));
1200 ds_put_cstr(&reply, "sent\n");
1205 unixctl_command_reply(conn, ds_cstr(&reply));
1207 unixctl_command_reply_error(conn, ds_cstr(&reply));
1212 struct barrier_aux {
1213 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1214 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1218 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1219 const char *argv[] OVS_UNUSED, void *aux_)
1221 struct barrier_aux *aux = aux_;
1226 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1230 msg = ofputil_encode_barrier_request();
1231 error = vconn_send_block(aux->vconn, msg);
1234 unixctl_command_reply_error(conn, strerror(error));
1241 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1242 const char *argv[], void *aux OVS_UNUSED)
1246 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1248 unixctl_command_reply_error(conn, strerror(errno));
1253 dup2(fd, STDERR_FILENO);
1255 unixctl_command_reply(conn, NULL);
1259 monitor_vconn(struct vconn *vconn)
1261 struct barrier_aux barrier_aux = { vconn, NULL };
1262 struct unixctl_server *server;
1263 bool exiting = false;
1266 daemon_save_fd(STDERR_FILENO);
1268 error = unixctl_server_create(NULL, &server);
1270 ovs_fatal(error, "failed to create unixctl server");
1272 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1273 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1275 unixctl_command_register("ofctl/barrier", "", 0, 0,
1276 ofctl_barrier, &barrier_aux);
1277 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1278 ofctl_set_output_file, NULL);
1279 daemonize_complete();
1285 unixctl_server_run(server);
1290 retval = vconn_recv(vconn, &b);
1291 if (retval == EAGAIN) {
1294 run(retval, "vconn_recv");
1297 time_t now = time_wall();
1300 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S: ", gmtime(&now));
1304 msg_type = ((const struct ofp_header *) b->data)->type;
1305 ofp_print(stderr, b->data, b->size, verbosity + 2);
1308 if (barrier_aux.conn && msg_type == OFPT10_BARRIER_REPLY) {
1309 unixctl_command_reply(barrier_aux.conn, NULL);
1310 barrier_aux.conn = NULL;
1319 vconn_run_wait(vconn);
1320 vconn_recv_wait(vconn);
1321 unixctl_server_wait(server);
1325 unixctl_server_destroy(server);
1329 ofctl_monitor(int argc, char *argv[])
1331 struct vconn *vconn;
1333 open_vconn(argv[1], &vconn);
1335 struct ofp_switch_config config;
1337 fetch_switch_config(vconn, &config);
1338 config.miss_send_len = htons(atoi(argv[2]));
1339 set_switch_config(vconn, &config);
1342 if (!strcmp(argv[3], "invalid_ttl")) {
1343 monitor_set_invalid_ttl_to_controller(vconn);
1346 if (preferred_packet_in_format >= 0) {
1347 set_packet_in_format(vconn, preferred_packet_in_format);
1349 struct ofpbuf *spif, *reply;
1351 spif = ofputil_make_set_packet_in_format(NXPIF_NXM);
1352 run(vconn_transact_noreply(vconn, spif, &reply),
1353 "talking to %s", vconn_get_name(vconn));
1355 char *s = ofp_to_string(reply->data, reply->size, 2);
1356 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1357 " replied: %s. Falling back to the switch default.",
1358 vconn_get_name(vconn), s);
1360 ofpbuf_delete(reply);
1364 monitor_vconn(vconn);
1368 ofctl_snoop(int argc OVS_UNUSED, char *argv[])
1370 struct vconn *vconn;
1372 open_vconn__(argv[1], "snoop", &vconn);
1373 monitor_vconn(vconn);
1377 ofctl_dump_ports(int argc, char *argv[])
1379 struct ofp_port_stats_request *req;
1380 struct ofpbuf *request;
1383 req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
1384 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
1385 req->port_no = htons(port);
1386 dump_stats_transaction(argv[1], request);
1390 ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1392 dump_trivial_stats_transaction(argv[1], OFPST_PORT_DESC);
1396 ofctl_probe(int argc OVS_UNUSED, char *argv[])
1398 struct ofpbuf *request;
1399 struct vconn *vconn;
1400 struct ofpbuf *reply;
1402 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1403 open_vconn(argv[1], &vconn);
1404 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1405 if (reply->size != sizeof(struct ofp_header)) {
1406 ovs_fatal(0, "reply does not match request");
1408 ofpbuf_delete(reply);
1413 ofctl_packet_out(int argc, char *argv[])
1415 struct ofputil_packet_out po;
1416 struct ofpbuf ofpacts;
1417 struct vconn *vconn;
1420 ofpbuf_init(&ofpacts, 64);
1421 parse_ofpacts(argv[3], &ofpacts);
1423 po.buffer_id = UINT32_MAX;
1424 po.in_port = (!strcasecmp(argv[2], "none") ? OFPP_NONE
1425 : !strcasecmp(argv[2], "local") ? OFPP_LOCAL
1426 : str_to_port_no(argv[1], argv[2]));
1427 po.ofpacts = ofpacts.data;
1428 po.ofpacts_len = ofpacts.size;
1430 open_vconn(argv[1], &vconn);
1431 for (i = 4; i < argc; i++) {
1432 struct ofpbuf *packet, *opo;
1433 const char *error_msg;
1435 error_msg = eth_from_hex(argv[i], &packet);
1437 ovs_fatal(0, "%s", error_msg);
1440 po.packet = packet->data;
1441 po.packet_len = packet->size;
1442 opo = ofputil_encode_packet_out(&po);
1443 transact_noreply(vconn, opo);
1444 ofpbuf_delete(packet);
1447 ofpbuf_uninit(&ofpacts);
1451 ofctl_mod_port(int argc OVS_UNUSED, char *argv[])
1453 struct ofp_config_flag {
1454 const char *name; /* The flag's name. */
1455 enum ofputil_port_config bit; /* Bit to turn on or off. */
1456 bool on; /* Value to set the bit to. */
1458 static const struct ofp_config_flag flags[] = {
1459 { "up", OFPUTIL_PC_PORT_DOWN, false },
1460 { "down", OFPUTIL_PC_PORT_DOWN, true },
1461 { "stp", OFPUTIL_PC_NO_STP, false },
1462 { "receive", OFPUTIL_PC_NO_RECV, false },
1463 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1464 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1465 { "forward", OFPUTIL_PC_NO_FWD, false },
1466 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1469 const struct ofp_config_flag *flag;
1470 enum ofputil_protocol protocol;
1471 struct ofputil_port_mod pm;
1472 struct ofputil_phy_port pp;
1473 struct vconn *vconn;
1474 const char *command;
1477 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1479 pm.port_no = pp.port_no;
1480 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1485 if (!strncasecmp(argv[3], "no-", 3)) {
1486 command = argv[3] + 3;
1488 } else if (!strncasecmp(argv[3], "no", 2)) {
1489 command = argv[3] + 2;
1495 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1496 if (!strcasecmp(command, flag->name)) {
1497 pm.mask = flag->bit;
1498 pm.config = flag->on ^ not ? flag->bit : 0;
1502 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1505 protocol = open_vconn(argv[1], &vconn);
1506 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1511 ofctl_get_frags(int argc OVS_UNUSED, char *argv[])
1513 struct ofp_switch_config config;
1514 struct vconn *vconn;
1516 open_vconn(argv[1], &vconn);
1517 fetch_switch_config(vconn, &config);
1518 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1523 ofctl_set_frags(int argc OVS_UNUSED, char *argv[])
1525 struct ofp_switch_config config;
1526 enum ofp_config_flags mode;
1527 struct vconn *vconn;
1530 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1531 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1534 open_vconn(argv[1], &vconn);
1535 fetch_switch_config(vconn, &config);
1536 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1537 if (flags != config.flags) {
1538 /* Set the configuration. */
1539 config.flags = flags;
1540 set_switch_config(vconn, &config);
1542 /* Then retrieve the configuration to see if it really took. OpenFlow
1543 * doesn't define error reporting for bad modes, so this is all we can
1545 fetch_switch_config(vconn, &config);
1546 if (flags != config.flags) {
1547 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1548 "switch probably doesn't support mode \"%s\")",
1549 argv[1], ofputil_frag_handling_to_string(mode));
1556 ofctl_ping(int argc, char *argv[])
1558 size_t max_payload = 65535 - sizeof(struct ofp_header);
1559 unsigned int payload;
1560 struct vconn *vconn;
1563 payload = argc > 2 ? atoi(argv[2]) : 64;
1564 if (payload > max_payload) {
1565 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1568 open_vconn(argv[1], &vconn);
1569 for (i = 0; i < 10; i++) {
1570 struct timeval start, end;
1571 struct ofpbuf *request, *reply;
1572 struct ofp_header *rq_hdr, *rpy_hdr;
1574 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1575 OFPT_ECHO_REQUEST, &request);
1576 random_bytes(rq_hdr + 1, payload);
1578 xgettimeofday(&start);
1579 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1580 xgettimeofday(&end);
1582 rpy_hdr = reply->data;
1583 if (reply->size != request->size
1584 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1585 || rpy_hdr->xid != rq_hdr->xid
1586 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1587 printf("Reply does not match request. Request:\n");
1588 ofp_print(stdout, request, request->size, verbosity + 2);
1590 ofp_print(stdout, reply, reply->size, verbosity + 2);
1592 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1593 reply->size - sizeof *rpy_hdr, argv[1], ntohl(rpy_hdr->xid),
1594 (1000*(double)(end.tv_sec - start.tv_sec))
1595 + (.001*(end.tv_usec - start.tv_usec)));
1596 ofpbuf_delete(request);
1597 ofpbuf_delete(reply);
1603 ofctl_benchmark(int argc OVS_UNUSED, char *argv[])
1605 size_t max_payload = 65535 - sizeof(struct ofp_header);
1606 struct timeval start, end;
1607 unsigned int payload_size, message_size;
1608 struct vconn *vconn;
1613 payload_size = atoi(argv[2]);
1614 if (payload_size > max_payload) {
1615 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1617 message_size = sizeof(struct ofp_header) + payload_size;
1619 count = atoi(argv[3]);
1621 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1622 count, message_size, count * message_size);
1624 open_vconn(argv[1], &vconn);
1625 xgettimeofday(&start);
1626 for (i = 0; i < count; i++) {
1627 struct ofpbuf *request, *reply;
1628 struct ofp_header *rq_hdr;
1630 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1631 memset(rq_hdr + 1, 0, payload_size);
1632 run(vconn_transact(vconn, request, &reply), "transact");
1633 ofpbuf_delete(reply);
1635 xgettimeofday(&end);
1638 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1639 + (.001*(end.tv_usec - start.tv_usec)));
1640 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1641 duration, count / (duration / 1000.0),
1642 count * message_size / (duration / 1000.0));
1646 ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1651 /* replace-flows and diff-flows commands. */
1653 /* A flow table entry, possibly with two different versions. */
1655 struct cls_rule rule; /* Within a "struct classifier". */
1656 struct fte_version *versions[2];
1659 /* One version of a Flow Table Entry. */
1660 struct fte_version {
1662 uint16_t idle_timeout;
1663 uint16_t hard_timeout;
1665 struct ofpact *ofpacts;
1669 /* Frees 'version' and the data that it owns. */
1671 fte_version_free(struct fte_version *version)
1674 free(version->ofpacts);
1679 /* Returns true if 'a' and 'b' are the same, false if they differ.
1681 * Ignores differences in 'flags' because there's no way to retrieve flags from
1682 * an OpenFlow switch. We have to assume that they are the same. */
1684 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
1686 return (a->cookie == b->cookie
1687 && a->idle_timeout == b->idle_timeout
1688 && a->hard_timeout == b->hard_timeout
1689 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
1690 b->ofpacts, b->ofpacts_len));
1693 /* Prints 'version' on stdout. Expects the caller to have printed the rule
1694 * associated with the version. */
1696 fte_version_print(const struct fte_version *version)
1700 if (version->cookie != htonll(0)) {
1701 printf(" cookie=0x%"PRIx64, ntohll(version->cookie));
1703 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
1704 printf(" idle_timeout=%"PRIu16, version->idle_timeout);
1706 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
1707 printf(" hard_timeout=%"PRIu16, version->hard_timeout);
1711 ofpacts_format(version->ofpacts, version->ofpacts_len, &s);
1712 printf(" %s\n", ds_cstr(&s));
1717 fte_from_cls_rule(const struct cls_rule *cls_rule)
1719 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
1722 /* Frees 'fte' and its versions. */
1724 fte_free(struct fte *fte)
1727 fte_version_free(fte->versions[0]);
1728 fte_version_free(fte->versions[1]);
1733 /* Frees all of the FTEs within 'cls'. */
1735 fte_free_all(struct classifier *cls)
1737 struct cls_cursor cursor;
1738 struct fte *fte, *next;
1740 cls_cursor_init(&cursor, cls, NULL);
1741 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
1742 classifier_remove(cls, &fte->rule);
1745 classifier_destroy(cls);
1748 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
1749 * necessary. Sets 'version' as the version of that rule with the given
1750 * 'index', replacing any existing version, if any.
1752 * Takes ownership of 'version'. */
1754 fte_insert(struct classifier *cls, const struct cls_rule *rule,
1755 struct fte_version *version, int index)
1757 struct fte *old, *fte;
1759 fte = xzalloc(sizeof *fte);
1761 fte->versions[index] = version;
1763 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
1765 fte_version_free(old->versions[index]);
1766 fte->versions[!index] = old->versions[!index];
1771 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
1772 * with the specified 'index'. Returns the flow formats able to represent the
1773 * flows that were read. */
1774 static enum ofputil_protocol
1775 read_flows_from_file(const char *filename, struct classifier *cls, int index)
1777 enum ofputil_protocol usable_protocols;
1781 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1783 ovs_fatal(errno, "%s: open", filename);
1787 usable_protocols = OFPUTIL_P_ANY;
1788 while (!ds_get_preprocessed_line(&s, file)) {
1789 struct fte_version *version;
1790 struct ofputil_flow_mod fm;
1792 parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), true);
1794 version = xmalloc(sizeof *version);
1795 version->cookie = fm.new_cookie;
1796 version->idle_timeout = fm.idle_timeout;
1797 version->hard_timeout = fm.hard_timeout;
1798 version->flags = fm.flags & (OFPFF_SEND_FLOW_REM | OFPFF_EMERG);
1799 version->ofpacts = fm.ofpacts;
1800 version->ofpacts_len = fm.ofpacts_len;
1802 usable_protocols &= ofputil_usable_protocols(&fm.cr);
1804 fte_insert(cls, &fm.cr, version, index);
1808 if (file != stdin) {
1812 return usable_protocols;
1816 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
1817 struct ofpbuf **replyp,
1818 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
1820 struct ofpbuf *reply = *replyp;
1826 /* Get a flow stats reply message, if we don't already have one. */
1828 const struct ofputil_msg_type *type;
1829 enum ofputil_msg_code code;
1832 run(vconn_recv_block(vconn, &reply),
1833 "OpenFlow packet receive failed");
1834 } while (((struct ofp_header *) reply->data)->xid != send_xid);
1836 ofputil_decode_msg_type(reply->data, &type);
1837 code = ofputil_msg_type_code(type);
1838 if (code != OFPUTIL_OFPST_FLOW_REPLY &&
1839 code != OFPUTIL_NXST_FLOW_REPLY) {
1840 ovs_fatal(0, "received bad reply: %s",
1841 ofp_to_string(reply->data, reply->size,
1846 /* Pull an individual flow stats reply out of the message. */
1847 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
1854 flags = ((const struct ofp_stats_msg *) reply->l2)->flags;
1855 ofpbuf_delete(reply);
1856 if (!(flags & htons(OFPSF_REPLY_MORE))) {
1863 ovs_fatal(0, "parse error in reply (%s)",
1864 ofperr_to_string(retval));
1869 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
1870 * format 'protocol', and adds them as flow table entries in 'cls' for the
1871 * version with the specified 'index'. */
1873 read_flows_from_switch(struct vconn *vconn,
1874 enum ofputil_protocol protocol,
1875 struct classifier *cls, int index)
1877 struct ofputil_flow_stats_request fsr;
1878 struct ofputil_flow_stats fs;
1879 struct ofpbuf *request;
1880 struct ofpbuf ofpacts;
1881 struct ofpbuf *reply;
1884 fsr.aggregate = false;
1885 cls_rule_init_catchall(&fsr.match, 0);
1886 fsr.out_port = OFPP_NONE;
1887 fsr.table_id = 0xff;
1888 fsr.cookie = fsr.cookie_mask = htonll(0);
1889 request = ofputil_encode_flow_stats_request(&fsr, protocol);
1890 send_xid = ((struct ofp_header *) request->data)->xid;
1891 send_openflow_buffer(vconn, request);
1894 ofpbuf_init(&ofpacts, 0);
1895 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
1896 struct fte_version *version;
1898 version = xmalloc(sizeof *version);
1899 version->cookie = fs.cookie;
1900 version->idle_timeout = fs.idle_timeout;
1901 version->hard_timeout = fs.hard_timeout;
1903 version->ofpacts_len = fs.ofpacts_len;
1904 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
1906 fte_insert(cls, &fs.rule, version, index);
1908 ofpbuf_uninit(&ofpacts);
1912 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
1913 enum ofputil_protocol protocol, struct list *packets)
1915 const struct fte_version *version = fte->versions[index];
1916 struct ofputil_flow_mod fm;
1920 fm.cookie = htonll(0);
1921 fm.cookie_mask = htonll(0);
1922 fm.new_cookie = version->cookie;
1924 fm.command = command;
1925 fm.idle_timeout = version->idle_timeout;
1926 fm.hard_timeout = version->hard_timeout;
1927 fm.buffer_id = UINT32_MAX;
1928 fm.out_port = OFPP_NONE;
1929 fm.flags = version->flags;
1930 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
1931 command == OFPFC_MODIFY_STRICT) {
1932 fm.ofpacts = version->ofpacts;
1933 fm.ofpacts_len = version->ofpacts_len;
1939 ofm = ofputil_encode_flow_mod(&fm, protocol);
1940 list_push_back(packets, &ofm->list_node);
1944 ofctl_replace_flows(int argc OVS_UNUSED, char *argv[])
1946 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
1947 enum ofputil_protocol usable_protocols, protocol;
1948 struct cls_cursor cursor;
1949 struct classifier cls;
1950 struct list requests;
1951 struct vconn *vconn;
1954 classifier_init(&cls);
1955 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
1957 protocol = open_vconn(argv[1], &vconn);
1958 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
1960 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
1962 list_init(&requests);
1964 /* Delete flows that exist on the switch but not in the file. */
1965 cls_cursor_init(&cursor, &cls, NULL);
1966 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1967 struct fte_version *file_ver = fte->versions[FILE_IDX];
1968 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1970 if (sw_ver && !file_ver) {
1971 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
1972 protocol, &requests);
1976 /* Add flows that exist in the file but not on the switch.
1977 * Update flows that exist in both places but differ. */
1978 cls_cursor_init(&cursor, &cls, NULL);
1979 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1980 struct fte_version *file_ver = fte->versions[FILE_IDX];
1981 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1984 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
1985 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
1988 transact_multiple_noreply(vconn, &requests);
1995 read_flows_from_source(const char *source, struct classifier *cls, int index)
1999 if (source[0] == '/' || source[0] == '.'
2000 || (!strchr(source, ':') && !stat(source, &s))) {
2001 read_flows_from_file(source, cls, index);
2003 enum ofputil_protocol protocol;
2004 struct vconn *vconn;
2006 protocol = open_vconn(source, &vconn);
2007 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2008 read_flows_from_switch(vconn, protocol, cls, index);
2014 ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
2016 bool differences = false;
2017 struct cls_cursor cursor;
2018 struct classifier cls;
2021 classifier_init(&cls);
2022 read_flows_from_source(argv[1], &cls, 0);
2023 read_flows_from_source(argv[2], &cls, 1);
2025 cls_cursor_init(&cursor, &cls, NULL);
2026 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2027 struct fte_version *a = fte->versions[0];
2028 struct fte_version *b = fte->versions[1];
2030 if (!a || !b || !fte_version_equals(a, b)) {
2031 char *rule_s = cls_rule_to_string(&fte->rule);
2033 printf("-%s", rule_s);
2034 fte_version_print(a);
2037 printf("+%s", rule_s);
2038 fte_version_print(b);
2053 /* Undocumented commands for unit testing. */
2056 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms)
2058 enum ofputil_protocol usable_protocols;
2059 enum ofputil_protocol protocol = 0;
2063 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
2064 usable_s = ofputil_protocols_to_string(usable_protocols);
2065 printf("usable protocols: %s\n", usable_s);
2068 if (!(usable_protocols & allowed_protocols)) {
2069 ovs_fatal(0, "no usable protocol");
2071 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
2073 if (protocol & usable_protocols & allowed_protocols) {
2077 assert(IS_POW2(protocol));
2079 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
2081 for (i = 0; i < n_fms; i++) {
2082 struct ofputil_flow_mod *fm = &fms[i];
2085 msg = ofputil_encode_flow_mod(fm, protocol);
2086 ofp_print(stdout, msg->data, msg->size, verbosity);
2093 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2094 * it back to stdout. */
2096 ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
2098 struct ofputil_flow_mod fm;
2100 parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, false);
2101 ofctl_parse_flows__(&fm, 1);
2104 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2105 * add-flows) and prints each of the flows back to stdout. */
2107 ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
2109 struct ofputil_flow_mod *fms = NULL;
2112 parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms);
2113 ofctl_parse_flows__(fms, n_fms);
2118 ofctl_parse_nxm__(bool oxm)
2123 while (!ds_get_test_line(&in, stdin)) {
2124 struct ofpbuf nx_match;
2125 struct cls_rule rule;
2126 ovs_be64 cookie, cookie_mask;
2130 /* Convert string to nx_match. */
2131 ofpbuf_init(&nx_match, 0);
2132 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
2134 /* Convert nx_match to cls_rule. */
2136 error = nx_pull_match(&nx_match, match_len, 0, &rule,
2137 &cookie, &cookie_mask);
2139 error = nx_pull_match_loose(&nx_match, match_len, 0, &rule,
2140 &cookie, &cookie_mask);
2146 /* Convert cls_rule back to nx_match. */
2147 ofpbuf_uninit(&nx_match);
2148 ofpbuf_init(&nx_match, 0);
2149 match_len = nx_put_match(&nx_match, oxm, &rule,
2150 cookie, cookie_mask);
2152 /* Convert nx_match to string. */
2153 out = nx_match_to_string(nx_match.data, match_len);
2157 printf("nx_pull_match() returned error %s\n",
2158 ofperr_get_name(error));
2161 ofpbuf_uninit(&nx_match);
2166 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2167 * stdin, does some internal fussing with them, and then prints them back as
2168 * strings on stdout. */
2170 ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2172 return ofctl_parse_nxm__(false);
2175 /* "parse-oxm": reads a series of OXM nx_match specifications as strings from
2176 * stdin, does some internal fussing with them, and then prints them back as
2177 * strings on stdout. */
2179 ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2181 return ofctl_parse_nxm__(true);
2185 print_differences(const void *a_, size_t a_len,
2186 const void *b_, size_t b_len)
2188 const uint8_t *a = a_;
2189 const uint8_t *b = b_;
2192 for (i = 0; i < MIN(a_len, b_len); i++) {
2194 printf("%2zu: %02"PRIx8" -> %02"PRIx8"\n", i, a[i], b[i]);
2197 for (i = a_len; i < b_len; i++) {
2198 printf("%2zu: (none) -> %02"PRIx8"\n", i, b[i]);
2200 for (i = b_len; i < a_len; i++) {
2201 printf("%2zu: %02"PRIx8" -> (none)\n", i, a[i]);
2205 /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications
2206 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2207 * on stdout, and then converts them back to hex bytes and prints any
2208 * differences from the input. */
2210 ofctl_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2215 while (!ds_get_preprocessed_line(&in, stdin)) {
2216 struct ofpbuf of10_out;
2217 struct ofpbuf of10_in;
2218 struct ofpbuf ofpacts;
2223 /* Parse hex bytes. */
2224 ofpbuf_init(&of10_in, 0);
2225 if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') {
2226 ovs_fatal(0, "Trailing garbage in hex data");
2229 /* Convert to ofpacts. */
2230 ofpbuf_init(&ofpacts, 0);
2231 size = of10_in.size;
2232 error = ofpacts_pull_openflow10(&of10_in, of10_in.size, &ofpacts);
2234 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2235 ofpbuf_uninit(&ofpacts);
2236 ofpbuf_uninit(&of10_in);
2239 ofpbuf_push_uninit(&of10_in, size);
2241 /* Print cls_rule. */
2243 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2247 /* Convert back to ofp10 actions and print differences from input. */
2248 ofpbuf_init(&of10_out, 0);
2249 ofpacts_put_openflow10(ofpacts.data, ofpacts.size, &of10_out);
2251 print_differences(of10_in.data, of10_in.size,
2252 of10_out.data, of10_out.size);
2255 ofpbuf_uninit(&ofpacts);
2256 ofpbuf_uninit(&of10_in);
2257 ofpbuf_uninit(&of10_out);
2262 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
2263 * bytes from stdin, converts them to cls_rules, prints them as strings on
2264 * stdout, and then converts them back to hex bytes and prints any differences
2265 * from the input. */
2267 ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2272 while (!ds_get_preprocessed_line(&in, stdin)) {
2273 struct ofpbuf match_in;
2274 struct ofp11_match match_out;
2275 struct cls_rule rule;
2278 /* Parse hex bytes. */
2279 ofpbuf_init(&match_in, 0);
2280 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2281 ovs_fatal(0, "Trailing garbage in hex data");
2283 if (match_in.size != sizeof(struct ofp11_match)) {
2284 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2285 match_in.size, sizeof(struct ofp11_match));
2288 /* Convert to cls_rule. */
2289 error = ofputil_cls_rule_from_ofp11_match(match_in.data,
2290 OFP_DEFAULT_PRIORITY, &rule);
2292 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
2293 ofpbuf_uninit(&match_in);
2297 /* Print cls_rule. */
2298 cls_rule_print(&rule);
2300 /* Convert back to ofp11_match and print differences from input. */
2301 ofputil_cls_rule_to_ofp11_match(&rule, &match_out);
2303 print_differences(match_in.data, match_in.size,
2304 &match_out, sizeof match_out);
2307 ofpbuf_uninit(&match_in);
2312 /* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications
2313 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2314 * on stdout, and then converts them back to hex bytes and prints any
2315 * differences from the input. */
2317 ofctl_parse_ofp11_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2322 while (!ds_get_preprocessed_line(&in, stdin)) {
2323 struct ofpbuf of11_out;
2324 struct ofpbuf of11_in;
2325 struct ofpbuf ofpacts;
2330 /* Parse hex bytes. */
2331 ofpbuf_init(&of11_in, 0);
2332 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
2333 ovs_fatal(0, "Trailing garbage in hex data");
2336 /* Convert to ofpacts. */
2337 ofpbuf_init(&ofpacts, 0);
2338 size = of11_in.size;
2339 error = ofpacts_pull_openflow11_actions(&of11_in, of11_in.size,
2342 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2343 ofpbuf_uninit(&ofpacts);
2344 ofpbuf_uninit(&of11_in);
2347 ofpbuf_push_uninit(&of11_in, size);
2349 /* Print cls_rule. */
2351 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2355 /* Convert back to ofp11 actions and print differences from input. */
2356 ofpbuf_init(&of11_out, 0);
2357 ofpacts_put_openflow11_actions(ofpacts.data, ofpacts.size, &of11_out);
2359 print_differences(of11_in.data, of11_in.size,
2360 of11_out.data, of11_out.size);
2363 ofpbuf_uninit(&ofpacts);
2364 ofpbuf_uninit(&of11_in);
2365 ofpbuf_uninit(&of11_out);
2370 /* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction
2371 * specifications as hex bytes from stdin, converts them to ofpacts, prints
2372 * them as strings on stdout, and then converts them back to hex bytes and
2373 * prints any differences from the input. */
2375 ofctl_parse_ofp11_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2380 while (!ds_get_preprocessed_line(&in, stdin)) {
2381 struct ofpbuf of11_out;
2382 struct ofpbuf of11_in;
2383 struct ofpbuf ofpacts;
2388 /* Parse hex bytes. */
2389 ofpbuf_init(&of11_in, 0);
2390 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
2391 ovs_fatal(0, "Trailing garbage in hex data");
2394 /* Convert to ofpacts. */
2395 ofpbuf_init(&ofpacts, 0);
2396 size = of11_in.size;
2397 error = ofpacts_pull_openflow11_instructions(&of11_in, of11_in.size,
2400 printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error));
2401 ofpbuf_uninit(&ofpacts);
2402 ofpbuf_uninit(&of11_in);
2405 ofpbuf_push_uninit(&of11_in, size);
2407 /* Print cls_rule. */
2409 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2413 /* Convert back to ofp11 instructions and print differences from
2415 ofpbuf_init(&of11_out, 0);
2416 ofpacts_put_openflow11_instructions(ofpacts.data, ofpacts.size,
2419 print_differences(of11_in.data, of11_in.size,
2420 of11_out.data, of11_out.size);
2423 ofpbuf_uninit(&ofpacts);
2424 ofpbuf_uninit(&of11_in);
2425 ofpbuf_uninit(&of11_out);
2430 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
2433 ofctl_print_error(int argc OVS_UNUSED, char *argv[])
2438 error = ofperr_from_name(argv[1]);
2440 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
2443 for (version = 0; version <= UINT8_MAX; version++) {
2444 const struct ofperr_domain *domain;
2446 domain = ofperr_domain_from_version(version);
2451 printf("%s: %d,%d\n",
2452 ofperr_domain_get_name(domain),
2453 ofperr_get_type(error, domain),
2454 ofperr_get_code(error, domain));
2458 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
2459 * binary data, interpreting them as an OpenFlow message, and prints the
2460 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
2462 ofctl_ofp_print(int argc, char *argv[])
2464 struct ofpbuf packet;
2466 ofpbuf_init(&packet, strlen(argv[1]) / 2);
2467 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
2468 ovs_fatal(0, "trailing garbage following hex bytes");
2470 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
2471 ofpbuf_uninit(&packet);
2474 static const struct command all_commands[] = {
2475 { "show", 1, 1, ofctl_show },
2476 { "monitor", 1, 3, ofctl_monitor },
2477 { "snoop", 1, 1, ofctl_snoop },
2478 { "dump-desc", 1, 1, ofctl_dump_desc },
2479 { "dump-tables", 1, 1, ofctl_dump_tables },
2480 { "dump-flows", 1, 2, ofctl_dump_flows },
2481 { "dump-aggregate", 1, 2, ofctl_dump_aggregate },
2482 { "queue-stats", 1, 3, ofctl_queue_stats },
2483 { "add-flow", 2, 2, ofctl_add_flow },
2484 { "add-flows", 2, 2, ofctl_add_flows },
2485 { "mod-flows", 2, 2, ofctl_mod_flows },
2486 { "del-flows", 1, 2, ofctl_del_flows },
2487 { "replace-flows", 2, 2, ofctl_replace_flows },
2488 { "diff-flows", 2, 2, ofctl_diff_flows },
2489 { "packet-out", 4, INT_MAX, ofctl_packet_out },
2490 { "dump-ports", 1, 2, ofctl_dump_ports },
2491 { "dump-ports-desc", 1, 1, ofctl_dump_ports_desc },
2492 { "mod-port", 3, 3, ofctl_mod_port },
2493 { "get-frags", 1, 1, ofctl_get_frags },
2494 { "set-frags", 2, 2, ofctl_set_frags },
2495 { "probe", 1, 1, ofctl_probe },
2496 { "ping", 1, 2, ofctl_ping },
2497 { "benchmark", 3, 3, ofctl_benchmark },
2498 { "help", 0, INT_MAX, ofctl_help },
2500 /* Undocumented commands for testing. */
2501 { "parse-flow", 1, 1, ofctl_parse_flow },
2502 { "parse-flows", 1, 1, ofctl_parse_flows },
2503 { "parse-nx-match", 0, 0, ofctl_parse_nxm },
2504 { "parse-nxm", 0, 0, ofctl_parse_nxm },
2505 { "parse-oxm", 0, 0, ofctl_parse_oxm },
2506 { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions },
2507 { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match },
2508 { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions },
2509 { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions },
2510 { "print-error", 1, 1, ofctl_print_error },
2511 { "ofp-print", 1, 2, ofctl_ofp_print },
2513 { NULL, 0, 0, NULL },