2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 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.
22 #include <sys/socket.h>
32 #include "byte-order.h"
33 #include "classifier.h"
34 #include "command-line.h"
38 #include "dynamic-string.h"
39 #include "fatal-signal.h"
42 #include "ofp-actions.h"
43 #include "ofp-errors.h"
45 #include "ofp-parse.h"
46 #include "ofp-print.h"
48 #include "ofp-version-opt.h"
50 #include "ofproto/ofproto.h"
51 #include "openflow/nicira-ext.h"
52 #include "openflow/openflow.h"
53 #include "dp-packet.h"
55 #include "pcap-file.h"
56 #include "poll-loop.h"
58 #include "stream-ssl.h"
59 #include "socket-util.h"
63 #include "openvswitch/vconn.h"
64 #include "openvswitch/vlog.h"
65 #include "meta-flow.h"
68 VLOG_DEFINE_THIS_MODULE(ofctl
);
70 /* --strict: Use strict matching for flow mod commands? Additionally governs
71 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
75 /* --readd: If true, on replace-flows, re-add even flows that have not changed
76 * (to reset flow counters). */
79 /* -F, --flow-format: Allowed protocols. By default, any protocol is
81 static enum ofputil_protocol allowed_protocols
= OFPUTIL_P_ANY
;
83 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
84 * commands. Either one of NXPIF_* to force a particular packet_in format, or
85 * -1 to let ovs-ofctl choose the default. */
86 static int preferred_packet_in_format
= -1;
88 /* -m, --more: Additional verbosity for ofp-print functions. */
91 /* --timestamp: Print a timestamp before each received packet on "monitor" and
93 static bool timestamp
;
95 /* --unixctl-path: Path to use for unixctl server, for "monitor" and "snoop"
97 static char *unixctl_path
;
99 /* --sort, --rsort: Sort order. */
100 enum sort_order
{ SORT_ASC
, SORT_DESC
};
101 struct sort_criterion
{
102 const struct mf_field
*field
; /* NULL means to sort by priority. */
103 enum sort_order order
;
105 static struct sort_criterion
*criteria
;
106 static size_t n_criteria
, allocated_criteria
;
108 static const struct ovs_cmdl_command
*get_all_commands(void);
110 OVS_NO_RETURN
static void usage(void);
111 static void parse_options(int argc
, char *argv
[]);
113 static bool recv_flow_stats_reply(struct vconn
*, ovs_be32 send_xid
,
114 struct ofpbuf
**replyp
,
115 struct ofputil_flow_stats
*,
116 struct ofpbuf
*ofpacts
);
118 main(int argc
, char *argv
[])
120 struct ovs_cmdl_context ctx
= { .argc
= 0, };
121 set_program_name(argv
[0]);
122 service_start(&argc
, &argv
);
123 parse_options(argc
, argv
);
124 fatal_ignore_sigpipe();
125 ctx
.argc
= argc
- optind
;
126 ctx
.argv
= argv
+ optind
;
127 ovs_cmdl_run_command(&ctx
, get_all_commands());
132 add_sort_criterion(enum sort_order order
, const char *field
)
134 struct sort_criterion
*sc
;
136 if (n_criteria
>= allocated_criteria
) {
137 criteria
= x2nrealloc(criteria
, &allocated_criteria
, sizeof *criteria
);
140 sc
= &criteria
[n_criteria
++];
141 if (!field
|| !strcasecmp(field
, "priority")) {
144 sc
->field
= mf_from_name(field
);
146 ovs_fatal(0, "%s: unknown field name", field
);
153 parse_options(int argc
, char *argv
[])
156 OPT_STRICT
= UCHAR_MAX
+ 1,
163 OFP_VERSION_OPTION_ENUMS
,
166 static const struct option long_options
[] = {
167 {"timeout", required_argument
, NULL
, 't'},
168 {"strict", no_argument
, NULL
, OPT_STRICT
},
169 {"readd", no_argument
, NULL
, OPT_READD
},
170 {"flow-format", required_argument
, NULL
, 'F'},
171 {"packet-in-format", required_argument
, NULL
, 'P'},
172 {"more", no_argument
, NULL
, 'm'},
173 {"timestamp", no_argument
, NULL
, OPT_TIMESTAMP
},
174 {"sort", optional_argument
, NULL
, OPT_SORT
},
175 {"rsort", optional_argument
, NULL
, OPT_RSORT
},
176 {"unixctl", required_argument
, NULL
, OPT_UNIXCTL
},
177 {"help", no_argument
, NULL
, 'h'},
178 {"option", no_argument
, NULL
, 'o'},
180 OFP_VERSION_LONG_OPTIONS
,
182 STREAM_SSL_LONG_OPTIONS
,
185 char *short_options
= ovs_cmdl_long_options_to_short_options(long_options
);
187 enum ofputil_protocol version_protocols
;
189 /* For now, ovs-ofctl only enables OpenFlow 1.0 by default. This is
190 * because ovs-ofctl implements command such as "add-flow" as raw OpenFlow
191 * requests, but those requests have subtly different semantics in
192 * different OpenFlow versions. For example:
194 * - In OpenFlow 1.0, a "mod-flow" operation that does not find any
195 * existing flow to modify adds a new flow.
197 * - In OpenFlow 1.1, a "mod-flow" operation that does not find any
198 * existing flow to modify adds a new flow, but only if the mod-flow
199 * did not match on the flow cookie.
201 * - In OpenFlow 1.2 and a later, a "mod-flow" operation never adds a
204 set_allowed_ofp_versions("OpenFlow10");
207 unsigned long int timeout
;
210 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
217 timeout
= strtoul(optarg
, NULL
, 10);
219 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
227 allowed_protocols
= ofputil_protocols_from_string(optarg
);
228 if (!allowed_protocols
) {
229 ovs_fatal(0, "%s: invalid flow format(s)", optarg
);
234 preferred_packet_in_format
=
235 ofputil_packet_in_format_from_string(optarg
);
236 if (preferred_packet_in_format
< 0) {
237 ovs_fatal(0, "unknown packet-in format `%s'", optarg
);
249 ovs_cmdl_print_options(long_options
);
265 add_sort_criterion(SORT_ASC
, optarg
);
269 add_sort_criterion(SORT_DESC
, optarg
);
273 unixctl_path
= optarg
;
276 DAEMON_OPTION_HANDLERS
277 OFP_VERSION_OPTION_HANDLERS
279 STREAM_SSL_OPTION_HANDLERS
290 /* Always do a final sort pass based on priority. */
291 add_sort_criterion(SORT_DESC
, "priority");
296 versions
= get_allowed_ofp_versions();
297 version_protocols
= ofputil_protocols_from_version_bitmap(versions
);
298 if (!(allowed_protocols
& version_protocols
)) {
299 char *protocols
= ofputil_protocols_to_string(allowed_protocols
);
300 struct ds version_s
= DS_EMPTY_INITIALIZER
;
302 ofputil_format_version_bitmap_names(&version_s
, versions
);
303 ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports "
304 "any of the enabled flow formats (%s). (Use -O to enable "
305 "additional OpenFlow versions or -F to enable additional "
306 "flow formats.)", ds_cstr(&version_s
), protocols
);
308 allowed_protocols
&= version_protocols
;
309 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
316 printf("%s: OpenFlow switch management utility\n"
317 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
318 "\nFor OpenFlow switches:\n"
319 " show SWITCH show OpenFlow information\n"
320 " dump-desc SWITCH print switch description\n"
321 " dump-tables SWITCH print table stats\n"
322 " dump-table-features SWITCH print table features\n"
323 " mod-port SWITCH IFACE ACT modify port behavior\n"
324 " mod-table SWITCH MOD modify flow table behavior\n"
325 " get-frags SWITCH print fragment handling behavior\n"
326 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
327 " dump-ports SWITCH [PORT] print port statistics\n"
328 " dump-ports-desc SWITCH [PORT] print port descriptions\n"
329 " dump-flows SWITCH print all flow entries\n"
330 " dump-flows SWITCH FLOW print matching FLOWs\n"
331 " dump-aggregate SWITCH print aggregate flow statistics\n"
332 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
333 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
334 " add-flow SWITCH FLOW add flow described by FLOW\n"
335 " add-flows SWITCH FILE add flows from FILE\n"
336 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
337 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
338 " replace-flows SWITCH FILE replace flows with those in FILE\n"
339 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
340 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
341 " execute ACTIONS on PACKET\n"
342 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
343 " print packets received from SWITCH\n"
344 " snoop SWITCH snoop on SWITCH and its controller\n"
345 " add-group SWITCH GROUP add group described by GROUP\n"
346 " add-groups SWITCH FILE add group from FILE\n"
347 " mod-group SWITCH GROUP modify specific group\n"
348 " del-groups SWITCH [GROUP] delete matching GROUPs\n"
349 " insert-buckets SWITCH [GROUP] add buckets to GROUP\n"
350 " remove-buckets SWITCH [GROUP] remove buckets from GROUP\n"
351 " dump-group-features SWITCH print group features\n"
352 " dump-groups SWITCH [GROUP] print group description\n"
353 " dump-group-stats SWITCH [GROUP] print group statistics\n"
354 " queue-get-config SWITCH PORT print queue information for port\n"
355 " add-meter SWITCH METER add meter described by METER\n"
356 " mod-meter SWITCH METER modify specific METER\n"
357 " del-meter SWITCH METER delete METER\n"
358 " del-meters SWITCH delete all meters\n"
359 " dump-meter SWITCH METER print METER configuration\n"
360 " dump-meters SWITCH print all meter configuration\n"
361 " meter-stats SWITCH [METER] print meter statistics\n"
362 " meter-features SWITCH print meter features\n"
363 "\nFor OpenFlow switches and controllers:\n"
364 " probe TARGET probe whether TARGET is up\n"
365 " ping TARGET [N] latency of N-byte echos\n"
366 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
367 "SWITCH or TARGET is an active OpenFlow connection method.\n"
368 "\nOther commands:\n"
369 " ofp-parse FILE print messages read from FILE\n"
370 " ofp-parse-pcap PCAP print OpenFlow read from PCAP\n",
371 program_name
, program_name
);
372 vconn_usage(true, false, false);
376 printf("\nOther options:\n"
377 " --strict use strict match for flow commands\n"
378 " --readd replace flows that haven't changed\n"
379 " -F, --flow-format=FORMAT force particular flow format\n"
380 " -P, --packet-in-format=FRMT force particular packet in format\n"
381 " -m, --more be more verbose printing OpenFlow\n"
382 " --timestamp (monitor, snoop) print timestamps\n"
383 " -t, --timeout=SECS give up after SECS seconds\n"
384 " --sort[=field] sort in ascending order\n"
385 " --rsort[=field] sort in descending order\n"
386 " --unixctl=SOCKET set control socket name\n"
387 " -h, --help display this help message\n"
388 " -V, --version display version information\n");
393 ofctl_exit(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
394 const char *argv
[] OVS_UNUSED
, void *exiting_
)
396 bool *exiting
= exiting_
;
398 unixctl_command_reply(conn
, NULL
);
401 static void run(int retval
, const char *message
, ...)
402 OVS_PRINTF_FORMAT(2, 3);
405 run(int retval
, const char *message
, ...)
410 va_start(args
, message
);
411 ovs_fatal_valist(retval
, message
, args
);
415 /* Generic commands. */
418 open_vconn_socket(const char *name
, struct vconn
**vconnp
)
420 char *vconn_name
= xasprintf("unix:%s", name
);
423 error
= vconn_open(vconn_name
, get_allowed_ofp_versions(), DSCP_DEFAULT
,
425 if (error
&& error
!= ENOENT
) {
426 ovs_fatal(0, "%s: failed to open socket (%s)", name
,
427 ovs_strerror(error
));
434 enum open_target
{ MGMT
, SNOOP
};
436 static enum ofputil_protocol
437 open_vconn__(const char *name
, enum open_target target
,
438 struct vconn
**vconnp
)
440 const char *suffix
= target
== MGMT
? "mgmt" : "snoop";
441 char *datapath_name
, *datapath_type
, *socket_name
;
442 enum ofputil_protocol protocol
;
447 bridge_path
= xasprintf("%s/%s.%s", ovs_rundir(), name
, suffix
);
449 ofproto_parse_name(name
, &datapath_name
, &datapath_type
);
450 socket_name
= xasprintf("%s/%s.%s", ovs_rundir(), datapath_name
, suffix
);
454 if (strchr(name
, ':')) {
455 run(vconn_open(name
, get_allowed_ofp_versions(), DSCP_DEFAULT
, vconnp
),
456 "connecting to %s", name
);
457 } else if (!open_vconn_socket(name
, vconnp
)) {
459 } else if (!open_vconn_socket(bridge_path
, vconnp
)) {
461 } else if (!open_vconn_socket(socket_name
, vconnp
)) {
464 ovs_fatal(0, "%s is not a bridge or a socket", name
);
467 if (target
== SNOOP
) {
468 vconn_set_recv_any_version(*vconnp
);
474 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp
));
475 error
= vconn_connect_block(*vconnp
);
477 ovs_fatal(0, "%s: failed to connect to socket (%s)", name
,
478 ovs_strerror(error
));
481 ofp_version
= vconn_get_version(*vconnp
);
482 protocol
= ofputil_protocol_from_ofp_version(ofp_version
);
484 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
490 static enum ofputil_protocol
491 open_vconn(const char *name
, struct vconn
**vconnp
)
493 return open_vconn__(name
, MGMT
, vconnp
);
497 send_openflow_buffer(struct vconn
*vconn
, struct ofpbuf
*buffer
)
499 ofpmsg_update_length(buffer
);
500 run(vconn_send_block(vconn
, buffer
), "failed to send packet to switch");
504 dump_transaction(struct vconn
*vconn
, struct ofpbuf
*request
)
506 struct ofpbuf
*reply
;
508 ofpmsg_update_length(request
);
509 run(vconn_transact(vconn
, request
, &reply
), "talking to %s",
510 vconn_get_name(vconn
));
511 ofp_print(stdout
, reply
->data
, reply
->size
, verbosity
+ 1);
512 ofpbuf_delete(reply
);
516 dump_trivial_transaction(const char *vconn_name
, enum ofpraw raw
)
518 struct ofpbuf
*request
;
521 open_vconn(vconn_name
, &vconn
);
522 request
= ofpraw_alloc(raw
, vconn_get_version(vconn
), 0);
523 dump_transaction(vconn
, request
);
528 dump_stats_transaction(struct vconn
*vconn
, struct ofpbuf
*request
)
530 const struct ofp_header
*request_oh
= request
->data
;
531 ovs_be32 send_xid
= request_oh
->xid
;
532 enum ofpraw request_raw
;
533 enum ofpraw reply_raw
;
536 ofpraw_decode_partial(&request_raw
, request
->data
, request
->size
);
537 reply_raw
= ofpraw_stats_request_to_reply(request_raw
,
538 request_oh
->version
);
540 send_openflow_buffer(vconn
, request
);
543 struct ofpbuf
*reply
;
545 run(vconn_recv_block(vconn
, &reply
), "OpenFlow packet receive failed");
546 recv_xid
= ((struct ofp_header
*) reply
->data
)->xid
;
547 if (send_xid
== recv_xid
) {
550 ofp_print(stdout
, reply
->data
, reply
->size
, verbosity
+ 1);
552 ofpraw_decode(&raw
, reply
->data
);
553 if (ofptype_from_ofpraw(raw
) == OFPTYPE_ERROR
) {
555 } else if (raw
== reply_raw
) {
556 done
= !ofpmp_more(reply
->data
);
558 ovs_fatal(0, "received bad reply: %s",
559 ofp_to_string(reply
->data
, reply
->size
,
563 VLOG_DBG("received reply with xid %08"PRIx32
" "
564 "!= expected %08"PRIx32
, recv_xid
, send_xid
);
566 ofpbuf_delete(reply
);
571 dump_trivial_stats_transaction(const char *vconn_name
, enum ofpraw raw
)
573 struct ofpbuf
*request
;
576 open_vconn(vconn_name
, &vconn
);
577 request
= ofpraw_alloc(raw
, vconn_get_version(vconn
), 0);
578 dump_stats_transaction(vconn
, request
);
582 /* Sends all of the 'requests', which should be requests that only have replies
583 * if an error occurs, and waits for them to succeed or fail. If an error does
584 * occur, prints it and exits with an error.
586 * Destroys all of the 'requests'. */
588 transact_multiple_noreply(struct vconn
*vconn
, struct ovs_list
*requests
)
590 struct ofpbuf
*request
, *reply
;
592 LIST_FOR_EACH (request
, list_node
, requests
) {
593 ofpmsg_update_length(request
);
596 run(vconn_transact_multiple_noreply(vconn
, requests
, &reply
),
597 "talking to %s", vconn_get_name(vconn
));
599 ofp_print(stderr
, reply
->data
, reply
->size
, verbosity
+ 2);
602 ofpbuf_delete(reply
);
605 /* Sends 'request', which should be a request that only has a reply if an error
606 * occurs, and waits for it to succeed or fail. If an error does occur, prints
607 * it and exits with an error.
609 * Destroys 'request'. */
611 transact_noreply(struct vconn
*vconn
, struct ofpbuf
*request
)
613 struct ovs_list requests
;
615 list_init(&requests
);
616 list_push_back(&requests
, &request
->list_node
);
617 transact_multiple_noreply(vconn
, &requests
);
621 fetch_switch_config(struct vconn
*vconn
, struct ofp_switch_config
*config_
)
623 struct ofp_switch_config
*config
;
624 struct ofpbuf
*request
;
625 struct ofpbuf
*reply
;
628 request
= ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST
,
629 vconn_get_version(vconn
), 0);
630 run(vconn_transact(vconn
, request
, &reply
),
631 "talking to %s", vconn_get_name(vconn
));
633 if (ofptype_pull(&type
, reply
) || type
!= OFPTYPE_GET_CONFIG_REPLY
) {
634 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn
));
637 config
= ofpbuf_pull(reply
, sizeof *config
);
640 ofpbuf_delete(reply
);
644 set_switch_config(struct vconn
*vconn
, const struct ofp_switch_config
*config
)
646 struct ofpbuf
*request
;
648 request
= ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG
, vconn_get_version(vconn
), 0);
649 ofpbuf_put(request
, config
, sizeof *config
);
651 transact_noreply(vconn
, request
);
655 ofctl_show(struct ovs_cmdl_context
*ctx
)
657 const char *vconn_name
= ctx
->argv
[1];
658 enum ofp_version version
;
660 struct ofpbuf
*request
;
661 struct ofpbuf
*reply
;
664 open_vconn(vconn_name
, &vconn
);
665 version
= vconn_get_version(vconn
);
666 request
= ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST
, version
, 0);
667 run(vconn_transact(vconn
, request
, &reply
), "talking to %s", vconn_name
);
669 has_ports
= ofputil_switch_features_has_ports(reply
);
670 ofp_print(stdout
, reply
->data
, reply
->size
, verbosity
+ 1);
671 ofpbuf_delete(reply
);
674 request
= ofputil_encode_port_desc_stats_request(version
, OFPP_ANY
);
675 dump_stats_transaction(vconn
, request
);
677 dump_trivial_transaction(vconn_name
, OFPRAW_OFPT_GET_CONFIG_REQUEST
);
682 ofctl_dump_desc(struct ovs_cmdl_context
*ctx
)
684 dump_trivial_stats_transaction(ctx
->argv
[1], OFPRAW_OFPST_DESC_REQUEST
);
688 ofctl_dump_tables(struct ovs_cmdl_context
*ctx
)
690 dump_trivial_stats_transaction(ctx
->argv
[1], OFPRAW_OFPST_TABLE_REQUEST
);
694 ofctl_dump_table_features(struct ovs_cmdl_context
*ctx
)
696 struct ofpbuf
*request
;
699 open_vconn(ctx
->argv
[1], &vconn
);
700 request
= ofputil_encode_table_features_request(vconn_get_version(vconn
));
702 dump_stats_transaction(vconn
, request
);
708 static bool fetch_port_by_stats(struct vconn
*,
709 const char *port_name
, ofp_port_t port_no
,
710 struct ofputil_phy_port
*);
712 /* Uses OFPT_FEATURES_REQUEST to attempt to fetch information about the port
713 * named 'port_name' or numbered 'port_no' into '*pp'. Returns true if
714 * successful, false on failure.
716 * This is only appropriate for OpenFlow 1.0, 1.1, and 1.2, which include a
717 * list of ports in OFPT_FEATURES_REPLY. */
719 fetch_port_by_features(struct vconn
*vconn
,
720 const char *port_name
, ofp_port_t port_no
,
721 struct ofputil_phy_port
*pp
)
723 struct ofputil_switch_features features
;
724 const struct ofp_header
*oh
;
725 struct ofpbuf
*request
, *reply
;
731 /* Fetch the switch's ofp_switch_features. */
732 request
= ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST
,
733 vconn_get_version(vconn
), 0);
734 run(vconn_transact(vconn
, request
, &reply
),
735 "talking to %s", vconn_get_name(vconn
));
738 if (ofptype_decode(&type
, reply
->data
)
739 || type
!= OFPTYPE_FEATURES_REPLY
) {
740 ovs_fatal(0, "%s: received bad features reply", vconn_get_name(vconn
));
742 if (!ofputil_switch_features_has_ports(reply
)) {
743 /* The switch features reply does not contain a complete list of ports.
744 * Probably, there are more ports than will fit into a single 64 kB
745 * OpenFlow message. Use OFPST_PORT_DESC to get a complete list of
747 ofpbuf_delete(reply
);
748 return fetch_port_by_stats(vconn
, port_name
, port_no
, pp
);
751 error
= ofputil_decode_switch_features(oh
, &features
, &b
);
753 ovs_fatal(0, "%s: failed to decode features reply (%s)",
754 vconn_get_name(vconn
), ofperr_to_string(error
));
757 while (!ofputil_pull_phy_port(oh
->version
, &b
, pp
)) {
758 if (port_no
!= OFPP_NONE
759 ? port_no
== pp
->port_no
760 : !strcmp(pp
->name
, port_name
)) {
765 ofpbuf_delete(reply
);
769 /* Uses a OFPST_PORT_DESC request to attempt to fetch information about the
770 * port named 'port_name' or numbered 'port_no' into '*pp'. Returns true if
771 * successful, false on failure.
773 * This is most appropriate for OpenFlow 1.3 and later. Open vSwitch 1.7 and
774 * later also implements OFPST_PORT_DESC, as an extension, for OpenFlow 1.0,
775 * 1.1, and 1.2, so this can be used as a fallback in those versions when there
776 * are too many ports than fit in an OFPT_FEATURES_REPLY. */
778 fetch_port_by_stats(struct vconn
*vconn
,
779 const char *port_name
, ofp_port_t port_no
,
780 struct ofputil_phy_port
*pp
)
782 struct ofpbuf
*request
;
787 request
= ofputil_encode_port_desc_stats_request(vconn_get_version(vconn
),
789 send_xid
= ((struct ofp_header
*) request
->data
)->xid
;
791 send_openflow_buffer(vconn
, request
);
794 struct ofpbuf
*reply
;
796 run(vconn_recv_block(vconn
, &reply
), "OpenFlow packet receive failed");
797 recv_xid
= ((struct ofp_header
*) reply
->data
)->xid
;
798 if (send_xid
== recv_xid
) {
799 struct ofp_header
*oh
= reply
->data
;
804 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
805 if (ofptype_pull(&type
, &b
)
806 || type
!= OFPTYPE_PORT_DESC_STATS_REPLY
) {
807 ovs_fatal(0, "received bad reply: %s",
808 ofp_to_string(reply
->data
, reply
->size
,
812 flags
= ofpmp_flags(oh
);
813 done
= !(flags
& OFPSF_REPLY_MORE
);
816 /* We've already found the port, but we need to drain
817 * the queue of any other replies for this request. */
821 while (!ofputil_pull_phy_port(oh
->version
, &b
, pp
)) {
822 if (port_no
!= OFPP_NONE
? port_no
== pp
->port_no
823 : !strcmp(pp
->name
, port_name
)) {
829 VLOG_DBG("received reply with xid %08"PRIx32
" "
830 "!= expected %08"PRIx32
, recv_xid
, send_xid
);
832 ofpbuf_delete(reply
);
839 str_to_ofp(const char *s
, ofp_port_t
*ofp_port
)
844 ret
= str_to_uint(s
, 10, &port_
);
845 *ofp_port
= u16_to_ofp(port_
);
849 /* Opens a connection to 'vconn_name', fetches the port structure for
850 * 'port_name' (which may be a port name or number), and copies it into
853 fetch_ofputil_phy_port(const char *vconn_name
, const char *port_name
,
854 struct ofputil_phy_port
*pp
)
860 /* Try to interpret the argument as a port number. */
861 if (!str_to_ofp(port_name
, &port_no
)) {
865 /* OpenFlow 1.0, 1.1, and 1.2 put the list of ports in the
866 * OFPT_FEATURES_REPLY message. OpenFlow 1.3 and later versions put it
867 * into the OFPST_PORT_DESC reply. Try it the correct way. */
868 open_vconn(vconn_name
, &vconn
);
869 found
= (vconn_get_version(vconn
) < OFP13_VERSION
870 ? fetch_port_by_features(vconn
, port_name
, port_no
, pp
)
871 : fetch_port_by_stats(vconn
, port_name
, port_no
, pp
));
875 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name
, port_name
);
879 /* Returns the port number corresponding to 'port_name' (which may be a port
880 * name or number) within the switch 'vconn_name'. */
882 str_to_port_no(const char *vconn_name
, const char *port_name
)
886 if (ofputil_port_from_string(port_name
, &port_no
)) {
889 struct ofputil_phy_port pp
;
891 fetch_ofputil_phy_port(vconn_name
, port_name
, &pp
);
897 try_set_protocol(struct vconn
*vconn
, enum ofputil_protocol want
,
898 enum ofputil_protocol
*cur
)
901 struct ofpbuf
*request
, *reply
;
902 enum ofputil_protocol next
;
904 request
= ofputil_encode_set_protocol(*cur
, want
, &next
);
909 run(vconn_transact_noreply(vconn
, request
, &reply
),
910 "talking to %s", vconn_get_name(vconn
));
912 char *s
= ofp_to_string(reply
->data
, reply
->size
, 2);
913 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
914 vconn_get_name(vconn
), s
);
916 ofpbuf_delete(reply
);
924 static enum ofputil_protocol
925 set_protocol_for_flow_dump(struct vconn
*vconn
,
926 enum ofputil_protocol cur_protocol
,
927 enum ofputil_protocol usable_protocols
)
932 for (i
= 0; i
< ofputil_n_flow_dump_protocols
; i
++) {
933 enum ofputil_protocol f
= ofputil_flow_dump_protocols
[i
];
934 if (f
& usable_protocols
& allowed_protocols
935 && try_set_protocol(vconn
, f
, &cur_protocol
)) {
940 usable_s
= ofputil_protocols_to_string(usable_protocols
);
941 if (usable_protocols
& allowed_protocols
) {
942 ovs_fatal(0, "switch does not support any of the usable flow "
943 "formats (%s)", usable_s
);
945 char *allowed_s
= ofputil_protocols_to_string(allowed_protocols
);
946 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
947 "allowed flow formats (%s)", usable_s
, allowed_s
);
951 static struct vconn
*
952 prepare_dump_flows(int argc
, char *argv
[], bool aggregate
,
953 struct ofpbuf
**requestp
)
955 enum ofputil_protocol usable_protocols
, protocol
;
956 struct ofputil_flow_stats_request fsr
;
960 error
= parse_ofp_flow_stats_request_str(&fsr
, aggregate
,
961 argc
> 2 ? argv
[2] : "",
964 ovs_fatal(0, "%s", error
);
967 protocol
= open_vconn(argv
[1], &vconn
);
968 protocol
= set_protocol_for_flow_dump(vconn
, protocol
, usable_protocols
);
969 *requestp
= ofputil_encode_flow_stats_request(&fsr
, protocol
);
974 ofctl_dump_flows__(int argc
, char *argv
[], bool aggregate
)
976 struct ofpbuf
*request
;
979 vconn
= prepare_dump_flows(argc
, argv
, aggregate
, &request
);
980 dump_stats_transaction(vconn
, request
);
985 compare_flows(const void *afs_
, const void *bfs_
)
987 const struct ofputil_flow_stats
*afs
= afs_
;
988 const struct ofputil_flow_stats
*bfs
= bfs_
;
989 const struct match
*a
= &afs
->match
;
990 const struct match
*b
= &bfs
->match
;
991 const struct sort_criterion
*sc
;
993 for (sc
= criteria
; sc
< &criteria
[n_criteria
]; sc
++) {
994 const struct mf_field
*f
= sc
->field
;
998 int a_pri
= afs
->priority
;
999 int b_pri
= bfs
->priority
;
1000 ret
= a_pri
< b_pri
? -1 : a_pri
> b_pri
;
1004 ina
= mf_are_prereqs_ok(f
, &a
->flow
) && !mf_is_all_wild(f
, &a
->wc
);
1005 inb
= mf_are_prereqs_ok(f
, &b
->flow
) && !mf_is_all_wild(f
, &b
->wc
);
1007 /* Skip the test for sc->order, so that missing fields always
1008 * sort to the end whether we're sorting in ascending or
1009 * descending order. */
1010 return ina
? -1 : 1;
1012 union mf_value aval
, bval
;
1014 mf_get_value(f
, &a
->flow
, &aval
);
1015 mf_get_value(f
, &b
->flow
, &bval
);
1016 ret
= memcmp(&aval
, &bval
, f
->n_bytes
);
1021 return sc
->order
== SORT_ASC
? ret
: -ret
;
1029 ofctl_dump_flows(struct ovs_cmdl_context
*ctx
)
1032 ofctl_dump_flows__(ctx
->argc
, ctx
->argv
, false);
1035 struct ofputil_flow_stats
*fses
;
1036 size_t n_fses
, allocated_fses
;
1037 struct ofpbuf
*request
;
1038 struct ofpbuf ofpacts
;
1039 struct ofpbuf
*reply
;
1040 struct vconn
*vconn
;
1045 vconn
= prepare_dump_flows(ctx
->argc
, ctx
->argv
, false, &request
);
1046 send_xid
= ((struct ofp_header
*) request
->data
)->xid
;
1047 send_openflow_buffer(vconn
, request
);
1050 n_fses
= allocated_fses
= 0;
1052 ofpbuf_init(&ofpacts
, 0);
1054 struct ofputil_flow_stats
*fs
;
1056 if (n_fses
>= allocated_fses
) {
1057 fses
= x2nrealloc(fses
, &allocated_fses
, sizeof *fses
);
1061 if (!recv_flow_stats_reply(vconn
, send_xid
, &reply
, fs
,
1065 fs
->ofpacts
= xmemdup(fs
->ofpacts
, fs
->ofpacts_len
);
1068 ofpbuf_uninit(&ofpacts
);
1070 qsort(fses
, n_fses
, sizeof *fses
, compare_flows
);
1073 for (i
= 0; i
< n_fses
; i
++) {
1075 ofp_print_flow_stats(&s
, &fses
[i
]);
1080 for (i
= 0; i
< n_fses
; i
++) {
1081 free(CONST_CAST(struct ofpact
*, fses
[i
].ofpacts
));
1090 ofctl_dump_aggregate(struct ovs_cmdl_context
*ctx
)
1092 ofctl_dump_flows__(ctx
->argc
, ctx
->argv
, true);
1096 ofctl_queue_stats(struct ovs_cmdl_context
*ctx
)
1098 struct ofpbuf
*request
;
1099 struct vconn
*vconn
;
1100 struct ofputil_queue_stats_request oqs
;
1102 open_vconn(ctx
->argv
[1], &vconn
);
1104 if (ctx
->argc
> 2 && ctx
->argv
[2][0] && strcasecmp(ctx
->argv
[2], "all")) {
1105 oqs
.port_no
= str_to_port_no(ctx
->argv
[1], ctx
->argv
[2]);
1107 oqs
.port_no
= OFPP_ANY
;
1109 if (ctx
->argc
> 3 && ctx
->argv
[3][0] && strcasecmp(ctx
->argv
[3], "all")) {
1110 oqs
.queue_id
= atoi(ctx
->argv
[3]);
1112 oqs
.queue_id
= OFPQ_ALL
;
1115 request
= ofputil_encode_queue_stats_request(vconn_get_version(vconn
), &oqs
);
1116 dump_stats_transaction(vconn
, request
);
1121 ofctl_queue_get_config(struct ovs_cmdl_context
*ctx
)
1123 const char *vconn_name
= ctx
->argv
[1];
1124 const char *port_name
= ctx
->argv
[2];
1125 enum ofputil_protocol protocol
;
1126 enum ofp_version version
;
1127 struct ofpbuf
*request
;
1128 struct vconn
*vconn
;
1131 port
= str_to_port_no(vconn_name
, port_name
);
1133 protocol
= open_vconn(vconn_name
, &vconn
);
1134 version
= ofputil_protocol_to_ofp_version(protocol
);
1135 request
= ofputil_encode_queue_get_config_request(version
, port
);
1136 dump_transaction(vconn
, request
);
1140 static enum ofputil_protocol
1141 open_vconn_for_flow_mod(const char *remote
, struct vconn
**vconnp
,
1142 enum ofputil_protocol usable_protocols
)
1144 enum ofputil_protocol cur_protocol
;
1148 if (!(usable_protocols
& allowed_protocols
)) {
1149 char *allowed_s
= ofputil_protocols_to_string(allowed_protocols
);
1150 usable_s
= ofputil_protocols_to_string(usable_protocols
);
1151 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1152 "allowed flow formats (%s)", usable_s
, allowed_s
);
1155 /* If the initial flow format is allowed and usable, keep it. */
1156 cur_protocol
= open_vconn(remote
, vconnp
);
1157 if (usable_protocols
& allowed_protocols
& cur_protocol
) {
1158 return cur_protocol
;
1161 /* Otherwise try each flow format in turn. */
1162 for (i
= 0; i
< sizeof(enum ofputil_protocol
) * CHAR_BIT
; i
++) {
1163 enum ofputil_protocol f
= 1 << i
;
1165 if (f
!= cur_protocol
1166 && f
& usable_protocols
& allowed_protocols
1167 && try_set_protocol(*vconnp
, f
, &cur_protocol
)) {
1172 usable_s
= ofputil_protocols_to_string(usable_protocols
);
1173 ovs_fatal(0, "switch does not support any of the usable flow "
1174 "formats (%s)", usable_s
);
1178 ofctl_flow_mod__(const char *remote
, struct ofputil_flow_mod
*fms
,
1179 size_t n_fms
, enum ofputil_protocol usable_protocols
)
1181 enum ofputil_protocol protocol
;
1182 struct vconn
*vconn
;
1185 protocol
= open_vconn_for_flow_mod(remote
, &vconn
, usable_protocols
);
1187 for (i
= 0; i
< n_fms
; i
++) {
1188 struct ofputil_flow_mod
*fm
= &fms
[i
];
1190 transact_noreply(vconn
, ofputil_encode_flow_mod(fm
, protocol
));
1191 free(CONST_CAST(struct ofpact
*, fm
->ofpacts
));
1197 ofctl_flow_mod_file(int argc OVS_UNUSED
, char *argv
[], uint16_t command
)
1199 enum ofputil_protocol usable_protocols
;
1200 struct ofputil_flow_mod
*fms
= NULL
;
1204 error
= parse_ofp_flow_mod_file(argv
[2], command
, &fms
, &n_fms
,
1207 ovs_fatal(0, "%s", error
);
1209 ofctl_flow_mod__(argv
[1], fms
, n_fms
, usable_protocols
);
1214 ofctl_flow_mod(int argc
, char *argv
[], uint16_t command
)
1216 if (argc
> 2 && !strcmp(argv
[2], "-")) {
1217 ofctl_flow_mod_file(argc
, argv
, command
);
1219 struct ofputil_flow_mod fm
;
1221 enum ofputil_protocol usable_protocols
;
1223 error
= parse_ofp_flow_mod_str(&fm
, argc
> 2 ? argv
[2] : "", command
,
1226 ovs_fatal(0, "%s", error
);
1228 ofctl_flow_mod__(argv
[1], &fm
, 1, usable_protocols
);
1233 ofctl_add_flow(struct ovs_cmdl_context
*ctx
)
1235 ofctl_flow_mod(ctx
->argc
, ctx
->argv
, OFPFC_ADD
);
1239 ofctl_add_flows(struct ovs_cmdl_context
*ctx
)
1241 ofctl_flow_mod_file(ctx
->argc
, ctx
->argv
, OFPFC_ADD
);
1245 ofctl_mod_flows(struct ovs_cmdl_context
*ctx
)
1247 ofctl_flow_mod(ctx
->argc
, ctx
->argv
, strict
? OFPFC_MODIFY_STRICT
: OFPFC_MODIFY
);
1251 ofctl_del_flows(struct ovs_cmdl_context
*ctx
)
1253 ofctl_flow_mod(ctx
->argc
, ctx
->argv
, strict
? OFPFC_DELETE_STRICT
: OFPFC_DELETE
);
1257 set_packet_in_format(struct vconn
*vconn
,
1258 enum nx_packet_in_format packet_in_format
)
1260 struct ofpbuf
*spif
;
1262 spif
= ofputil_make_set_packet_in_format(vconn_get_version(vconn
),
1264 transact_noreply(vconn
, spif
);
1265 VLOG_DBG("%s: using user-specified packet in format %s",
1266 vconn_get_name(vconn
),
1267 ofputil_packet_in_format_to_string(packet_in_format
));
1271 monitor_set_invalid_ttl_to_controller(struct vconn
*vconn
)
1273 struct ofp_switch_config config
;
1274 enum ofp_config_flags flags
;
1276 fetch_switch_config(vconn
, &config
);
1277 flags
= ntohs(config
.flags
);
1278 if (!(flags
& OFPC_INVALID_TTL_TO_CONTROLLER
)) {
1279 /* Set the invalid ttl config. */
1280 flags
|= OFPC_INVALID_TTL_TO_CONTROLLER
;
1282 config
.flags
= htons(flags
);
1283 set_switch_config(vconn
, &config
);
1285 /* Then retrieve the configuration to see if it really took. OpenFlow
1286 * doesn't define error reporting for bad modes, so this is all we can
1288 fetch_switch_config(vconn
, &config
);
1289 flags
= ntohs(config
.flags
);
1290 if (!(flags
& OFPC_INVALID_TTL_TO_CONTROLLER
)) {
1291 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1292 "switch probably doesn't support mode)");
1299 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1300 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1301 * an error message and stores NULL in '*msgp'. */
1303 openflow_from_hex(const char *hex
, struct ofpbuf
**msgp
)
1305 struct ofp_header
*oh
;
1308 msg
= ofpbuf_new(strlen(hex
) / 2);
1311 if (ofpbuf_put_hex(msg
, hex
, NULL
)[0] != '\0') {
1313 return "Trailing garbage in hex data";
1316 if (msg
->size
< sizeof(struct ofp_header
)) {
1318 return "Message too short for OpenFlow";
1322 if (msg
->size
!= ntohs(oh
->length
)) {
1324 return "Message size does not match length in OpenFlow header";
1332 ofctl_send(struct unixctl_conn
*conn
, int argc
,
1333 const char *argv
[], void *vconn_
)
1335 struct vconn
*vconn
= vconn_
;
1342 for (i
= 1; i
< argc
; i
++) {
1343 const char *error_msg
;
1347 error_msg
= openflow_from_hex(argv
[i
], &msg
);
1349 ds_put_format(&reply
, "%s\n", error_msg
);
1354 fprintf(stderr
, "send: ");
1355 ofp_print(stderr
, msg
->data
, msg
->size
, verbosity
);
1357 error
= vconn_send_block(vconn
, msg
);
1360 ds_put_format(&reply
, "%s\n", ovs_strerror(error
));
1363 ds_put_cstr(&reply
, "sent\n");
1368 unixctl_command_reply(conn
, ds_cstr(&reply
));
1370 unixctl_command_reply_error(conn
, ds_cstr(&reply
));
1375 struct barrier_aux
{
1376 struct vconn
*vconn
; /* OpenFlow connection for sending barrier. */
1377 struct unixctl_conn
*conn
; /* Connection waiting for barrier response. */
1381 ofctl_barrier(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1382 const char *argv
[] OVS_UNUSED
, void *aux_
)
1384 struct barrier_aux
*aux
= aux_
;
1389 unixctl_command_reply_error(conn
, "already waiting for barrier reply");
1393 msg
= ofputil_encode_barrier_request(vconn_get_version(aux
->vconn
));
1394 error
= vconn_send_block(aux
->vconn
, msg
);
1397 unixctl_command_reply_error(conn
, ovs_strerror(error
));
1404 ofctl_set_output_file(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1405 const char *argv
[], void *aux OVS_UNUSED
)
1409 fd
= open(argv
[1], O_CREAT
| O_TRUNC
| O_WRONLY
, 0666);
1411 unixctl_command_reply_error(conn
, ovs_strerror(errno
));
1416 dup2(fd
, STDERR_FILENO
);
1418 unixctl_command_reply(conn
, NULL
);
1422 ofctl_block(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1423 const char *argv
[] OVS_UNUSED
, void *blocked_
)
1425 bool *blocked
= blocked_
;
1429 unixctl_command_reply(conn
, NULL
);
1431 unixctl_command_reply(conn
, "already blocking");
1436 ofctl_unblock(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1437 const char *argv
[] OVS_UNUSED
, void *blocked_
)
1439 bool *blocked
= blocked_
;
1443 unixctl_command_reply(conn
, NULL
);
1445 unixctl_command_reply(conn
, "already unblocked");
1449 /* Prints to stdout all of the messages received on 'vconn'.
1451 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1452 * received on 'vconn'. */
1454 monitor_vconn(struct vconn
*vconn
, bool reply_to_echo_requests
)
1456 struct barrier_aux barrier_aux
= { vconn
, NULL
};
1457 struct unixctl_server
*server
;
1458 bool exiting
= false;
1459 bool blocked
= false;
1462 daemon_save_fd(STDERR_FILENO
);
1464 error
= unixctl_server_create(unixctl_path
, &server
);
1466 ovs_fatal(error
, "failed to create unixctl server");
1468 unixctl_command_register("exit", "", 0, 0, ofctl_exit
, &exiting
);
1469 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX
,
1471 unixctl_command_register("ofctl/barrier", "", 0, 0,
1472 ofctl_barrier
, &barrier_aux
);
1473 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1474 ofctl_set_output_file
, NULL
);
1476 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block
, &blocked
);
1477 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock
,
1480 daemonize_complete();
1486 unixctl_server_run(server
);
1491 retval
= vconn_recv(vconn
, &b
);
1492 if (retval
== EAGAIN
) {
1495 run(retval
, "vconn_recv");
1498 char *s
= xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1499 time_wall_msec(), true);
1504 ofptype_decode(&type
, b
->data
);
1505 ofp_print(stderr
, b
->data
, b
->size
, verbosity
+ 2);
1508 switch ((int) type
) {
1509 case OFPTYPE_BARRIER_REPLY
:
1510 if (barrier_aux
.conn
) {
1511 unixctl_command_reply(barrier_aux
.conn
, NULL
);
1512 barrier_aux
.conn
= NULL
;
1516 case OFPTYPE_ECHO_REQUEST
:
1517 if (reply_to_echo_requests
) {
1518 struct ofpbuf
*reply
;
1520 reply
= make_echo_reply(b
->data
);
1521 retval
= vconn_send_block(vconn
, reply
);
1523 ovs_fatal(retval
, "failed to send echo reply");
1536 vconn_run_wait(vconn
);
1538 vconn_recv_wait(vconn
);
1540 unixctl_server_wait(server
);
1544 unixctl_server_destroy(server
);
1548 ofctl_monitor(struct ovs_cmdl_context
*ctx
)
1550 struct vconn
*vconn
;
1552 enum ofputil_protocol usable_protocols
;
1554 open_vconn(ctx
->argv
[1], &vconn
);
1555 for (i
= 2; i
< ctx
->argc
; i
++) {
1556 const char *arg
= ctx
->argv
[i
];
1558 if (isdigit((unsigned char) *arg
)) {
1559 struct ofp_switch_config config
;
1561 fetch_switch_config(vconn
, &config
);
1562 config
.miss_send_len
= htons(atoi(arg
));
1563 set_switch_config(vconn
, &config
);
1564 } else if (!strcmp(arg
, "invalid_ttl")) {
1565 monitor_set_invalid_ttl_to_controller(vconn
);
1566 } else if (!strncmp(arg
, "watch:", 6)) {
1567 struct ofputil_flow_monitor_request fmr
;
1571 error
= parse_flow_monitor_request(&fmr
, arg
+ 6,
1574 ovs_fatal(0, "%s", error
);
1577 msg
= ofpbuf_new(0);
1578 ofputil_append_flow_monitor_request(&fmr
, msg
);
1579 dump_stats_transaction(vconn
, msg
);
1582 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg
);
1586 if (preferred_packet_in_format
>= 0) {
1587 set_packet_in_format(vconn
, preferred_packet_in_format
);
1589 enum ofp_version version
= vconn_get_version(vconn
);
1592 case OFP10_VERSION
: {
1593 struct ofpbuf
*spif
, *reply
;
1595 spif
= ofputil_make_set_packet_in_format(vconn_get_version(vconn
),
1597 run(vconn_transact_noreply(vconn
, spif
, &reply
),
1598 "talking to %s", vconn_get_name(vconn
));
1600 char *s
= ofp_to_string(reply
->data
, reply
->size
, 2);
1601 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1602 " replied: %s. Falling back to the switch default.",
1603 vconn_get_name(vconn
), s
);
1605 ofpbuf_delete(reply
);
1620 monitor_vconn(vconn
, true);
1624 ofctl_snoop(struct ovs_cmdl_context
*ctx
)
1626 struct vconn
*vconn
;
1628 open_vconn__(ctx
->argv
[1], SNOOP
, &vconn
);
1629 monitor_vconn(vconn
, false);
1633 ofctl_dump_ports(struct ovs_cmdl_context
*ctx
)
1635 struct ofpbuf
*request
;
1636 struct vconn
*vconn
;
1639 open_vconn(ctx
->argv
[1], &vconn
);
1640 port
= ctx
->argc
> 2 ? str_to_port_no(ctx
->argv
[1], ctx
->argv
[2]) : OFPP_ANY
;
1641 request
= ofputil_encode_dump_ports_request(vconn_get_version(vconn
), port
);
1642 dump_stats_transaction(vconn
, request
);
1647 ofctl_dump_ports_desc(struct ovs_cmdl_context
*ctx
)
1649 struct ofpbuf
*request
;
1650 struct vconn
*vconn
;
1653 open_vconn(ctx
->argv
[1], &vconn
);
1654 port
= ctx
->argc
> 2 ? str_to_port_no(ctx
->argv
[1], ctx
->argv
[2]) : OFPP_ANY
;
1655 request
= ofputil_encode_port_desc_stats_request(vconn_get_version(vconn
),
1657 dump_stats_transaction(vconn
, request
);
1662 ofctl_probe(struct ovs_cmdl_context
*ctx
)
1664 struct ofpbuf
*request
;
1665 struct vconn
*vconn
;
1666 struct ofpbuf
*reply
;
1668 open_vconn(ctx
->argv
[1], &vconn
);
1669 request
= make_echo_request(vconn_get_version(vconn
));
1670 run(vconn_transact(vconn
, request
, &reply
), "talking to %s", ctx
->argv
[1]);
1671 if (reply
->size
!= sizeof(struct ofp_header
)) {
1672 ovs_fatal(0, "reply does not match request");
1674 ofpbuf_delete(reply
);
1679 ofctl_packet_out(struct ovs_cmdl_context
*ctx
)
1681 enum ofputil_protocol protocol
;
1682 struct ofputil_packet_out po
;
1683 struct ofpbuf ofpacts
;
1684 struct vconn
*vconn
;
1687 enum ofputil_protocol usable_protocols
; /* XXX: Use in proto selection */
1689 ofpbuf_init(&ofpacts
, 64);
1690 error
= ofpacts_parse_actions(ctx
->argv
[3], &ofpacts
, &usable_protocols
);
1692 ovs_fatal(0, "%s", error
);
1695 po
.buffer_id
= UINT32_MAX
;
1696 po
.in_port
= str_to_port_no(ctx
->argv
[1], ctx
->argv
[2]);
1697 po
.ofpacts
= ofpacts
.data
;
1698 po
.ofpacts_len
= ofpacts
.size
;
1700 protocol
= open_vconn(ctx
->argv
[1], &vconn
);
1701 for (i
= 4; i
< ctx
->argc
; i
++) {
1702 struct dp_packet
*packet
;
1704 const char *error_msg
;
1706 error_msg
= eth_from_hex(ctx
->argv
[i
], &packet
);
1708 ovs_fatal(0, "%s", error_msg
);
1711 po
.packet
= dp_packet_data(packet
);
1712 po
.packet_len
= dp_packet_size(packet
);
1713 opo
= ofputil_encode_packet_out(&po
, protocol
);
1714 transact_noreply(vconn
, opo
);
1715 dp_packet_delete(packet
);
1718 ofpbuf_uninit(&ofpacts
);
1722 ofctl_mod_port(struct ovs_cmdl_context
*ctx
)
1724 struct ofp_config_flag
{
1725 const char *name
; /* The flag's name. */
1726 enum ofputil_port_config bit
; /* Bit to turn on or off. */
1727 bool on
; /* Value to set the bit to. */
1729 static const struct ofp_config_flag flags
[] = {
1730 { "up", OFPUTIL_PC_PORT_DOWN
, false },
1731 { "down", OFPUTIL_PC_PORT_DOWN
, true },
1732 { "stp", OFPUTIL_PC_NO_STP
, false },
1733 { "receive", OFPUTIL_PC_NO_RECV
, false },
1734 { "receive-stp", OFPUTIL_PC_NO_RECV_STP
, false },
1735 { "flood", OFPUTIL_PC_NO_FLOOD
, false },
1736 { "forward", OFPUTIL_PC_NO_FWD
, false },
1737 { "packet-in", OFPUTIL_PC_NO_PACKET_IN
, false },
1740 const struct ofp_config_flag
*flag
;
1741 enum ofputil_protocol protocol
;
1742 struct ofputil_port_mod pm
;
1743 struct ofputil_phy_port pp
;
1744 struct vconn
*vconn
;
1745 const char *command
;
1748 fetch_ofputil_phy_port(ctx
->argv
[1], ctx
->argv
[2], &pp
);
1750 pm
.port_no
= pp
.port_no
;
1751 memcpy(pm
.hw_addr
, pp
.hw_addr
, ETH_ADDR_LEN
);
1756 if (!strncasecmp(ctx
->argv
[3], "no-", 3)) {
1757 command
= ctx
->argv
[3] + 3;
1759 } else if (!strncasecmp(ctx
->argv
[3], "no", 2)) {
1760 command
= ctx
->argv
[3] + 2;
1763 command
= ctx
->argv
[3];
1766 for (flag
= flags
; flag
< &flags
[ARRAY_SIZE(flags
)]; flag
++) {
1767 if (!strcasecmp(command
, flag
->name
)) {
1768 pm
.mask
= flag
->bit
;
1769 pm
.config
= flag
->on
^ not ? flag
->bit
: 0;
1773 ovs_fatal(0, "unknown mod-port command '%s'", ctx
->argv
[3]);
1776 protocol
= open_vconn(ctx
->argv
[1], &vconn
);
1777 transact_noreply(vconn
, ofputil_encode_port_mod(&pm
, protocol
));
1782 ofctl_mod_table(struct ovs_cmdl_context
*ctx
)
1784 enum ofputil_protocol protocol
, usable_protocols
;
1785 struct ofputil_table_mod tm
;
1786 struct vconn
*vconn
;
1790 error
= parse_ofp_table_mod(&tm
, ctx
->argv
[2], ctx
->argv
[3], &usable_protocols
);
1792 ovs_fatal(0, "%s", error
);
1795 protocol
= open_vconn(ctx
->argv
[1], &vconn
);
1796 if (!(protocol
& usable_protocols
)) {
1797 for (i
= 0; i
< sizeof(enum ofputil_protocol
) * CHAR_BIT
; i
++) {
1798 enum ofputil_protocol f
= 1 << i
;
1800 && f
& usable_protocols
1801 && try_set_protocol(vconn
, f
, &protocol
)) {
1808 if (!(protocol
& usable_protocols
)) {
1809 char *usable_s
= ofputil_protocols_to_string(usable_protocols
);
1810 ovs_fatal(0, "Switch does not support table mod message(%s)", usable_s
);
1813 transact_noreply(vconn
, ofputil_encode_table_mod(&tm
, protocol
));
1818 ofctl_get_frags(struct ovs_cmdl_context
*ctx
)
1820 struct ofp_switch_config config
;
1821 struct vconn
*vconn
;
1823 open_vconn(ctx
->argv
[1], &vconn
);
1824 fetch_switch_config(vconn
, &config
);
1825 puts(ofputil_frag_handling_to_string(ntohs(config
.flags
)));
1830 ofctl_set_frags(struct ovs_cmdl_context
*ctx
)
1832 struct ofp_switch_config config
;
1833 enum ofp_config_flags mode
;
1834 struct vconn
*vconn
;
1837 if (!ofputil_frag_handling_from_string(ctx
->argv
[2], &mode
)) {
1838 ovs_fatal(0, "%s: unknown fragment handling mode", ctx
->argv
[2]);
1841 open_vconn(ctx
->argv
[1], &vconn
);
1842 fetch_switch_config(vconn
, &config
);
1843 flags
= htons(mode
) | (config
.flags
& htons(~OFPC_FRAG_MASK
));
1844 if (flags
!= config
.flags
) {
1845 /* Set the configuration. */
1846 config
.flags
= flags
;
1847 set_switch_config(vconn
, &config
);
1849 /* Then retrieve the configuration to see if it really took. OpenFlow
1850 * doesn't define error reporting for bad modes, so this is all we can
1852 fetch_switch_config(vconn
, &config
);
1853 if (flags
!= config
.flags
) {
1854 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1855 "switch probably doesn't support mode \"%s\")",
1856 ctx
->argv
[1], ofputil_frag_handling_to_string(mode
));
1863 ofctl_ofp_parse(struct ovs_cmdl_context
*ctx
)
1865 const char *filename
= ctx
->argv
[1];
1869 file
= !strcmp(filename
, "-") ? stdin
: fopen(filename
, "r");
1871 ovs_fatal(errno
, "%s: open", filename
);
1874 ofpbuf_init(&b
, 65536);
1876 struct ofp_header
*oh
;
1877 size_t length
, tail_len
;
1882 oh
= ofpbuf_put_uninit(&b
, sizeof *oh
);
1883 n
= fread(oh
, 1, sizeof *oh
, file
);
1886 } else if (n
< sizeof *oh
) {
1887 ovs_fatal(0, "%s: unexpected end of file mid-message", filename
);
1890 length
= ntohs(oh
->length
);
1891 if (length
< sizeof *oh
) {
1892 ovs_fatal(0, "%s: %"PRIuSIZE
"-byte message is too short for OpenFlow",
1896 tail_len
= length
- sizeof *oh
;
1897 tail
= ofpbuf_put_uninit(&b
, tail_len
);
1898 n
= fread(tail
, 1, tail_len
, file
);
1900 ovs_fatal(0, "%s: unexpected end of file mid-message", filename
);
1903 ofp_print(stdout
, b
.data
, b
.size
, verbosity
+ 2);
1907 if (file
!= stdin
) {
1913 is_openflow_port(ovs_be16 port_
, char *ports
[])
1915 uint16_t port
= ntohs(port_
);
1919 for (i
= 0; ports
[i
]; i
++) {
1920 if (port
== atoi(ports
[i
])) {
1926 return port
== OFP_PORT
|| port
== OFP_OLD_PORT
;
1931 ofctl_ofp_parse_pcap(struct ovs_cmdl_context
*ctx
)
1933 struct tcp_reader
*reader
;
1938 file
= ovs_pcap_open(ctx
->argv
[1], "rb");
1940 ovs_fatal(errno
, "%s: open failed", ctx
->argv
[1]);
1943 reader
= tcp_reader_open();
1946 struct dp_packet
*packet
;
1950 error
= ovs_pcap_read(file
, &packet
, &when
);
1954 packet
->md
= PKT_METADATA_INITIALIZER(ODPP_NONE
);
1955 flow_extract(packet
, &flow
);
1956 if (flow
.dl_type
== htons(ETH_TYPE_IP
)
1957 && flow
.nw_proto
== IPPROTO_TCP
1958 && (is_openflow_port(flow
.tp_src
, ctx
->argv
+ 2) ||
1959 is_openflow_port(flow
.tp_dst
, ctx
->argv
+ 2))) {
1960 struct dp_packet
*payload
= tcp_reader_run(reader
, &flow
, packet
);
1962 while (dp_packet_size(payload
) >= sizeof(struct ofp_header
)) {
1963 const struct ofp_header
*oh
;
1964 void *data
= dp_packet_data(payload
);
1967 /* Align OpenFlow on 8-byte boundary for safe access. */
1968 dp_packet_shift(payload
, -((intptr_t) data
& 7));
1970 oh
= dp_packet_data(payload
);
1971 length
= ntohs(oh
->length
);
1972 if (dp_packet_size(payload
) < length
) {
1982 char *s
= xastrftime_msec("%H:%M:%S.### ", when
, true);
1987 printf(IP_FMT
".%"PRIu16
" > "IP_FMT
".%"PRIu16
":\n",
1988 IP_ARGS(flow
.nw_src
), ntohs(flow
.tp_src
),
1989 IP_ARGS(flow
.nw_dst
), ntohs(flow
.tp_dst
));
1990 ofp_print(stdout
, dp_packet_data(payload
), length
, verbosity
+ 1);
1991 dp_packet_pull(payload
, length
);
1995 dp_packet_delete(packet
);
1997 tcp_reader_close(reader
);
2001 ofctl_ping(struct ovs_cmdl_context
*ctx
)
2003 size_t max_payload
= 65535 - sizeof(struct ofp_header
);
2004 unsigned int payload
;
2005 struct vconn
*vconn
;
2008 payload
= ctx
->argc
> 2 ? atoi(ctx
->argv
[2]) : 64;
2009 if (payload
> max_payload
) {
2010 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE
" bytes", max_payload
);
2013 open_vconn(ctx
->argv
[1], &vconn
);
2014 for (i
= 0; i
< 10; i
++) {
2015 struct timeval start
, end
;
2016 struct ofpbuf
*request
, *reply
;
2017 const struct ofp_header
*rpy_hdr
;
2020 request
= ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST
,
2021 vconn_get_version(vconn
), payload
);
2022 random_bytes(ofpbuf_put_uninit(request
, payload
), payload
);
2024 xgettimeofday(&start
);
2025 run(vconn_transact(vconn
, ofpbuf_clone(request
), &reply
), "transact");
2026 xgettimeofday(&end
);
2028 rpy_hdr
= reply
->data
;
2029 if (ofptype_pull(&type
, reply
)
2030 || type
!= OFPTYPE_ECHO_REPLY
2031 || reply
->size
!= payload
2032 || memcmp(request
->msg
, reply
->msg
, payload
)) {
2033 printf("Reply does not match request. Request:\n");
2034 ofp_print(stdout
, request
, request
->size
, verbosity
+ 2);
2036 ofp_print(stdout
, reply
, reply
->size
, verbosity
+ 2);
2038 printf("%"PRIu32
" bytes from %s: xid=%08"PRIx32
" time=%.1f ms\n",
2039 reply
->size
, ctx
->argv
[1], ntohl(rpy_hdr
->xid
),
2040 (1000*(double)(end
.tv_sec
- start
.tv_sec
))
2041 + (.001*(end
.tv_usec
- start
.tv_usec
)));
2042 ofpbuf_delete(request
);
2043 ofpbuf_delete(reply
);
2049 ofctl_benchmark(struct ovs_cmdl_context
*ctx
)
2051 size_t max_payload
= 65535 - sizeof(struct ofp_header
);
2052 struct timeval start
, end
;
2053 unsigned int payload_size
, message_size
;
2054 struct vconn
*vconn
;
2059 payload_size
= atoi(ctx
->argv
[2]);
2060 if (payload_size
> max_payload
) {
2061 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE
" bytes", max_payload
);
2063 message_size
= sizeof(struct ofp_header
) + payload_size
;
2065 count
= atoi(ctx
->argv
[3]);
2067 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
2068 count
, message_size
, count
* message_size
);
2070 open_vconn(ctx
->argv
[1], &vconn
);
2071 xgettimeofday(&start
);
2072 for (i
= 0; i
< count
; i
++) {
2073 struct ofpbuf
*request
, *reply
;
2075 request
= ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST
,
2076 vconn_get_version(vconn
), payload_size
);
2077 ofpbuf_put_zeros(request
, payload_size
);
2078 run(vconn_transact(vconn
, request
, &reply
), "transact");
2079 ofpbuf_delete(reply
);
2081 xgettimeofday(&end
);
2084 duration
= ((1000*(double)(end
.tv_sec
- start
.tv_sec
))
2085 + (.001*(end
.tv_usec
- start
.tv_usec
)));
2086 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
2087 duration
, count
/ (duration
/ 1000.0),
2088 count
* message_size
/ (duration
/ 1000.0));
2092 ofctl_group_mod__(const char *remote
, struct ofputil_group_mod
*gms
,
2093 size_t n_gms
, enum ofputil_protocol usable_protocols
)
2095 enum ofputil_protocol protocol
;
2096 struct ofputil_group_mod
*gm
;
2097 enum ofp_version version
;
2098 struct ofpbuf
*request
;
2100 struct vconn
*vconn
;
2103 protocol
= open_vconn_for_flow_mod(remote
, &vconn
, usable_protocols
);
2104 version
= ofputil_protocol_to_ofp_version(protocol
);
2106 for (i
= 0; i
< n_gms
; i
++) {
2108 request
= ofputil_encode_group_mod(version
, gm
);
2110 transact_noreply(vconn
, request
);
2120 ofctl_group_mod_file(int argc OVS_UNUSED
, char *argv
[], uint16_t command
)
2122 struct ofputil_group_mod
*gms
= NULL
;
2123 enum ofputil_protocol usable_protocols
;
2128 error
= parse_ofp_group_mod_file(argv
[2], command
, &gms
, &n_gms
,
2131 ovs_fatal(0, "%s", error
);
2133 ofctl_group_mod__(argv
[1], gms
, n_gms
, usable_protocols
);
2134 for (i
= 0; i
< n_gms
; i
++) {
2135 ofputil_bucket_list_destroy(&gms
[i
].buckets
);
2141 ofctl_group_mod(int argc
, char *argv
[], uint16_t command
)
2143 if (argc
> 2 && !strcmp(argv
[2], "-")) {
2144 ofctl_group_mod_file(argc
, argv
, command
);
2146 enum ofputil_protocol usable_protocols
;
2147 struct ofputil_group_mod gm
;
2150 error
= parse_ofp_group_mod_str(&gm
, command
, argc
> 2 ? argv
[2] : "",
2153 ovs_fatal(0, "%s", error
);
2155 ofctl_group_mod__(argv
[1], &gm
, 1, usable_protocols
);
2156 ofputil_bucket_list_destroy(&gm
.buckets
);
2161 ofctl_add_group(struct ovs_cmdl_context
*ctx
)
2163 ofctl_group_mod(ctx
->argc
, ctx
->argv
, OFPGC11_ADD
);
2167 ofctl_add_groups(struct ovs_cmdl_context
*ctx
)
2169 ofctl_group_mod_file(ctx
->argc
, ctx
->argv
, OFPGC11_ADD
);
2173 ofctl_mod_group(struct ovs_cmdl_context
*ctx
)
2175 ofctl_group_mod(ctx
->argc
, ctx
->argv
, OFPGC11_MODIFY
);
2179 ofctl_del_groups(struct ovs_cmdl_context
*ctx
)
2181 ofctl_group_mod(ctx
->argc
, ctx
->argv
, OFPGC11_DELETE
);
2185 ofctl_insert_bucket(struct ovs_cmdl_context
*ctx
)
2187 ofctl_group_mod(ctx
->argc
, ctx
->argv
, OFPGC15_INSERT_BUCKET
);
2191 ofctl_remove_bucket(struct ovs_cmdl_context
*ctx
)
2193 ofctl_group_mod(ctx
->argc
, ctx
->argv
, OFPGC15_REMOVE_BUCKET
);
2197 ofctl_dump_group_stats(struct ovs_cmdl_context
*ctx
)
2199 enum ofputil_protocol usable_protocols
;
2200 struct ofputil_group_mod gm
;
2201 struct ofpbuf
*request
;
2202 struct vconn
*vconn
;
2206 memset(&gm
, 0, sizeof gm
);
2208 error
= parse_ofp_group_mod_str(&gm
, OFPGC11_DELETE
,
2209 ctx
->argc
> 2 ? ctx
->argv
[2] : "",
2212 ovs_fatal(0, "%s", error
);
2215 group_id
= gm
.group_id
;
2217 open_vconn(ctx
->argv
[1], &vconn
);
2218 request
= ofputil_encode_group_stats_request(vconn_get_version(vconn
),
2221 dump_stats_transaction(vconn
, request
);
2228 ofctl_dump_group_desc(struct ovs_cmdl_context
*ctx
)
2230 struct ofpbuf
*request
;
2231 struct vconn
*vconn
;
2234 open_vconn(ctx
->argv
[1], &vconn
);
2236 if (ctx
->argc
< 3 || !ofputil_group_from_string(ctx
->argv
[2], &group_id
)) {
2237 group_id
= OFPG11_ALL
;
2240 request
= ofputil_encode_group_desc_request(vconn_get_version(vconn
),
2243 dump_stats_transaction(vconn
, request
);
2250 ofctl_dump_group_features(struct ovs_cmdl_context
*ctx
)
2252 struct ofpbuf
*request
;
2253 struct vconn
*vconn
;
2255 open_vconn(ctx
->argv
[1], &vconn
);
2256 request
= ofputil_encode_group_features_request(vconn_get_version(vconn
));
2258 dump_stats_transaction(vconn
, request
);
2265 ofctl_help(struct ovs_cmdl_context
*ctx OVS_UNUSED
)
2271 ofctl_list_commands(struct ovs_cmdl_context
*ctx OVS_UNUSED
)
2273 ovs_cmdl_print_commands(get_all_commands());
2276 /* replace-flows and diff-flows commands. */
2278 /* A flow table entry, possibly with two different versions. */
2280 struct cls_rule rule
; /* Within a "struct classifier". */
2281 struct fte_version
*versions
[2];
2284 /* One version of a Flow Table Entry. */
2285 struct fte_version
{
2287 uint16_t idle_timeout
;
2288 uint16_t hard_timeout
;
2289 uint16_t importance
;
2291 struct ofpact
*ofpacts
;
2295 /* Frees 'version' and the data that it owns. */
2297 fte_version_free(struct fte_version
*version
)
2300 free(CONST_CAST(struct ofpact
*, version
->ofpacts
));
2305 /* Returns true if 'a' and 'b' are the same, false if they differ.
2307 * Ignores differences in 'flags' because there's no way to retrieve flags from
2308 * an OpenFlow switch. We have to assume that they are the same. */
2310 fte_version_equals(const struct fte_version
*a
, const struct fte_version
*b
)
2312 return (a
->cookie
== b
->cookie
2313 && a
->idle_timeout
== b
->idle_timeout
2314 && a
->hard_timeout
== b
->hard_timeout
2315 && a
->importance
== b
->importance
2316 && ofpacts_equal(a
->ofpacts
, a
->ofpacts_len
,
2317 b
->ofpacts
, b
->ofpacts_len
));
2320 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
2321 * 'index' into 's', followed by a new-line. */
2323 fte_version_format(const struct fte
*fte
, int index
, struct ds
*s
)
2325 const struct fte_version
*version
= fte
->versions
[index
];
2332 cls_rule_format(&fte
->rule
, s
);
2333 if (version
->cookie
!= htonll(0)) {
2334 ds_put_format(s
, " cookie=0x%"PRIx64
, ntohll(version
->cookie
));
2336 if (version
->idle_timeout
!= OFP_FLOW_PERMANENT
) {
2337 ds_put_format(s
, " idle_timeout=%"PRIu16
, version
->idle_timeout
);
2339 if (version
->hard_timeout
!= OFP_FLOW_PERMANENT
) {
2340 ds_put_format(s
, " hard_timeout=%"PRIu16
, version
->hard_timeout
);
2342 if (version
->importance
!= 0) {
2343 ds_put_format(s
, " importance=%"PRIu16
, version
->importance
);
2346 ds_put_cstr(s
, " actions=");
2347 ofpacts_format(version
->ofpacts
, version
->ofpacts_len
, s
);
2349 ds_put_char(s
, '\n');
2353 fte_from_cls_rule(const struct cls_rule
*cls_rule
)
2355 return cls_rule
? CONTAINER_OF(cls_rule
, struct fte
, rule
) : NULL
;
2358 /* Frees 'fte' and its versions. */
2360 fte_free(struct fte
*fte
)
2363 fte_version_free(fte
->versions
[0]);
2364 fte_version_free(fte
->versions
[1]);
2365 cls_rule_destroy(&fte
->rule
);
2370 /* Frees all of the FTEs within 'cls'. */
2372 fte_free_all(struct classifier
*cls
)
2376 classifier_defer(cls
);
2377 CLS_FOR_EACH (fte
, rule
, cls
) {
2378 classifier_remove(cls
, &fte
->rule
);
2379 ovsrcu_postpone(fte_free
, fte
);
2381 classifier_destroy(cls
);
2384 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
2385 * necessary. Sets 'version' as the version of that rule with the given
2386 * 'index', replacing any existing version, if any.
2388 * Takes ownership of 'version'. */
2390 fte_insert(struct classifier
*cls
, const struct match
*match
,
2391 int priority
, struct fte_version
*version
, int index
)
2393 struct fte
*old
, *fte
;
2395 fte
= xzalloc(sizeof *fte
);
2396 cls_rule_init(&fte
->rule
, match
, priority
);
2397 fte
->versions
[index
] = version
;
2399 old
= fte_from_cls_rule(classifier_replace(cls
, &fte
->rule
, NULL
, 0));
2401 fte
->versions
[!index
] = old
->versions
[!index
];
2402 old
->versions
[!index
] = NULL
;
2404 ovsrcu_postpone(fte_free
, old
);
2408 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
2409 * with the specified 'index'. Returns the flow formats able to represent the
2410 * flows that were read. */
2411 static enum ofputil_protocol
2412 read_flows_from_file(const char *filename
, struct classifier
*cls
, int index
)
2414 enum ofputil_protocol usable_protocols
;
2419 file
= !strcmp(filename
, "-") ? stdin
: fopen(filename
, "r");
2421 ovs_fatal(errno
, "%s: open", filename
);
2425 usable_protocols
= OFPUTIL_P_ANY
;
2427 classifier_defer(cls
);
2428 while (!ds_get_preprocessed_line(&s
, file
, &line_number
)) {
2429 struct fte_version
*version
;
2430 struct ofputil_flow_mod fm
;
2432 enum ofputil_protocol usable
;
2434 error
= parse_ofp_str(&fm
, OFPFC_ADD
, ds_cstr(&s
), &usable
);
2436 ovs_fatal(0, "%s:%d: %s", filename
, line_number
, error
);
2438 usable_protocols
&= usable
;
2440 version
= xmalloc(sizeof *version
);
2441 version
->cookie
= fm
.new_cookie
;
2442 version
->idle_timeout
= fm
.idle_timeout
;
2443 version
->hard_timeout
= fm
.hard_timeout
;
2444 version
->importance
= fm
.importance
;
2445 version
->flags
= fm
.flags
& (OFPUTIL_FF_SEND_FLOW_REM
2446 | OFPUTIL_FF_EMERG
);
2447 version
->ofpacts
= fm
.ofpacts
;
2448 version
->ofpacts_len
= fm
.ofpacts_len
;
2450 fte_insert(cls
, &fm
.match
, fm
.priority
, version
, index
);
2452 classifier_publish(cls
);
2455 if (file
!= stdin
) {
2459 return usable_protocols
;
2463 recv_flow_stats_reply(struct vconn
*vconn
, ovs_be32 send_xid
,
2464 struct ofpbuf
**replyp
,
2465 struct ofputil_flow_stats
*fs
, struct ofpbuf
*ofpacts
)
2467 struct ofpbuf
*reply
= *replyp
;
2473 /* Get a flow stats reply message, if we don't already have one. */
2479 run(vconn_recv_block(vconn
, &reply
),
2480 "OpenFlow packet receive failed");
2481 } while (((struct ofp_header
*) reply
->data
)->xid
!= send_xid
);
2483 error
= ofptype_decode(&type
, reply
->data
);
2484 if (error
|| type
!= OFPTYPE_FLOW_STATS_REPLY
) {
2485 ovs_fatal(0, "received bad reply: %s",
2486 ofp_to_string(reply
->data
, reply
->size
,
2491 /* Pull an individual flow stats reply out of the message. */
2492 retval
= ofputil_decode_flow_stats_reply(fs
, reply
, false, ofpacts
);
2499 more
= ofpmp_more(reply
->header
);
2500 ofpbuf_delete(reply
);
2509 ovs_fatal(0, "parse error in reply (%s)",
2510 ofperr_to_string(retval
));
2515 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
2516 * format 'protocol', and adds them as flow table entries in 'cls' for the
2517 * version with the specified 'index'. */
2519 read_flows_from_switch(struct vconn
*vconn
,
2520 enum ofputil_protocol protocol
,
2521 struct classifier
*cls
, int index
)
2523 struct ofputil_flow_stats_request fsr
;
2524 struct ofputil_flow_stats fs
;
2525 struct ofpbuf
*request
;
2526 struct ofpbuf ofpacts
;
2527 struct ofpbuf
*reply
;
2530 fsr
.aggregate
= false;
2531 match_init_catchall(&fsr
.match
);
2532 fsr
.out_port
= OFPP_ANY
;
2533 fsr
.table_id
= 0xff;
2534 fsr
.cookie
= fsr
.cookie_mask
= htonll(0);
2535 request
= ofputil_encode_flow_stats_request(&fsr
, protocol
);
2536 send_xid
= ((struct ofp_header
*) request
->data
)->xid
;
2537 send_openflow_buffer(vconn
, request
);
2540 ofpbuf_init(&ofpacts
, 0);
2541 classifier_defer(cls
);
2542 while (recv_flow_stats_reply(vconn
, send_xid
, &reply
, &fs
, &ofpacts
)) {
2543 struct fte_version
*version
;
2545 version
= xmalloc(sizeof *version
);
2546 version
->cookie
= fs
.cookie
;
2547 version
->idle_timeout
= fs
.idle_timeout
;
2548 version
->hard_timeout
= fs
.hard_timeout
;
2549 version
->importance
= fs
.importance
;
2551 version
->ofpacts_len
= fs
.ofpacts_len
;
2552 version
->ofpacts
= xmemdup(fs
.ofpacts
, fs
.ofpacts_len
);
2554 fte_insert(cls
, &fs
.match
, fs
.priority
, version
, index
);
2556 classifier_publish(cls
);
2557 ofpbuf_uninit(&ofpacts
);
2561 fte_make_flow_mod(const struct fte
*fte
, int index
, uint16_t command
,
2562 enum ofputil_protocol protocol
, struct ovs_list
*packets
)
2564 const struct fte_version
*version
= fte
->versions
[index
];
2565 struct ofputil_flow_mod fm
;
2568 minimatch_expand(&fte
->rule
.match
, &fm
.match
);
2569 fm
.priority
= fte
->rule
.priority
;
2570 fm
.cookie
= htonll(0);
2571 fm
.cookie_mask
= htonll(0);
2572 fm
.new_cookie
= version
->cookie
;
2573 fm
.modify_cookie
= true;
2575 fm
.command
= command
;
2576 fm
.idle_timeout
= version
->idle_timeout
;
2577 fm
.hard_timeout
= version
->hard_timeout
;
2578 fm
.importance
= version
->importance
;
2579 fm
.buffer_id
= UINT32_MAX
;
2580 fm
.out_port
= OFPP_ANY
;
2581 fm
.flags
= version
->flags
;
2582 if (command
== OFPFC_ADD
|| command
== OFPFC_MODIFY
||
2583 command
== OFPFC_MODIFY_STRICT
) {
2584 fm
.ofpacts
= version
->ofpacts
;
2585 fm
.ofpacts_len
= version
->ofpacts_len
;
2590 fm
.delete_reason
= OFPRR_DELETE
;
2592 ofm
= ofputil_encode_flow_mod(&fm
, protocol
);
2593 list_push_back(packets
, &ofm
->list_node
);
2597 ofctl_replace_flows(struct ovs_cmdl_context
*ctx
)
2599 enum { FILE_IDX
= 0, SWITCH_IDX
= 1 };
2600 enum ofputil_protocol usable_protocols
, protocol
;
2601 struct classifier cls
;
2602 struct ovs_list requests
;
2603 struct vconn
*vconn
;
2606 classifier_init(&cls
, NULL
);
2607 usable_protocols
= read_flows_from_file(ctx
->argv
[2], &cls
, FILE_IDX
);
2609 protocol
= open_vconn(ctx
->argv
[1], &vconn
);
2610 protocol
= set_protocol_for_flow_dump(vconn
, protocol
, usable_protocols
);
2612 read_flows_from_switch(vconn
, protocol
, &cls
, SWITCH_IDX
);
2614 list_init(&requests
);
2616 /* Delete flows that exist on the switch but not in the file. */
2617 CLS_FOR_EACH (fte
, rule
, &cls
) {
2618 struct fte_version
*file_ver
= fte
->versions
[FILE_IDX
];
2619 struct fte_version
*sw_ver
= fte
->versions
[SWITCH_IDX
];
2621 if (sw_ver
&& !file_ver
) {
2622 fte_make_flow_mod(fte
, SWITCH_IDX
, OFPFC_DELETE_STRICT
,
2623 protocol
, &requests
);
2627 /* Add flows that exist in the file but not on the switch.
2628 * Update flows that exist in both places but differ. */
2629 CLS_FOR_EACH (fte
, rule
, &cls
) {
2630 struct fte_version
*file_ver
= fte
->versions
[FILE_IDX
];
2631 struct fte_version
*sw_ver
= fte
->versions
[SWITCH_IDX
];
2634 && (readd
|| !sw_ver
|| !fte_version_equals(sw_ver
, file_ver
))) {
2635 fte_make_flow_mod(fte
, FILE_IDX
, OFPFC_ADD
, protocol
, &requests
);
2638 transact_multiple_noreply(vconn
, &requests
);
2645 read_flows_from_source(const char *source
, struct classifier
*cls
, int index
)
2649 if (source
[0] == '/' || source
[0] == '.'
2650 || (!strchr(source
, ':') && !stat(source
, &s
))) {
2651 read_flows_from_file(source
, cls
, index
);
2653 enum ofputil_protocol protocol
;
2654 struct vconn
*vconn
;
2656 protocol
= open_vconn(source
, &vconn
);
2657 protocol
= set_protocol_for_flow_dump(vconn
, protocol
, OFPUTIL_P_ANY
);
2658 read_flows_from_switch(vconn
, protocol
, cls
, index
);
2664 ofctl_diff_flows(struct ovs_cmdl_context
*ctx
)
2666 bool differences
= false;
2667 struct classifier cls
;
2671 classifier_init(&cls
, NULL
);
2672 read_flows_from_source(ctx
->argv
[1], &cls
, 0);
2673 read_flows_from_source(ctx
->argv
[2], &cls
, 1);
2678 CLS_FOR_EACH (fte
, rule
, &cls
) {
2679 struct fte_version
*a
= fte
->versions
[0];
2680 struct fte_version
*b
= fte
->versions
[1];
2682 if (!a
|| !b
|| !fte_version_equals(a
, b
)) {
2683 fte_version_format(fte
, 0, &a_s
);
2684 fte_version_format(fte
, 1, &b_s
);
2685 if (strcmp(ds_cstr(&a_s
), ds_cstr(&b_s
))) {
2687 printf("-%s", ds_cstr(&a_s
));
2690 printf("+%s", ds_cstr(&b_s
));
2708 ofctl_meter_mod__(const char *bridge
, const char *str
, int command
)
2710 struct ofputil_meter_mod mm
;
2711 struct vconn
*vconn
;
2712 enum ofputil_protocol protocol
;
2713 enum ofputil_protocol usable_protocols
;
2714 enum ofp_version version
;
2718 error
= parse_ofp_meter_mod_str(&mm
, str
, command
, &usable_protocols
);
2720 ovs_fatal(0, "%s", error
);
2723 usable_protocols
= OFPUTIL_P_OF13_UP
;
2724 mm
.command
= command
;
2725 mm
.meter
.meter_id
= OFPM13_ALL
;
2728 protocol
= open_vconn_for_flow_mod(bridge
, &vconn
, usable_protocols
);
2729 version
= ofputil_protocol_to_ofp_version(protocol
);
2730 transact_noreply(vconn
, ofputil_encode_meter_mod(version
, &mm
));
2735 ofctl_meter_request__(const char *bridge
, const char *str
,
2736 enum ofputil_meter_request_type type
)
2738 struct ofputil_meter_mod mm
;
2739 struct vconn
*vconn
;
2740 enum ofputil_protocol usable_protocols
;
2741 enum ofputil_protocol protocol
;
2742 enum ofp_version version
;
2746 error
= parse_ofp_meter_mod_str(&mm
, str
, -1, &usable_protocols
);
2748 ovs_fatal(0, "%s", error
);
2751 usable_protocols
= OFPUTIL_P_OF13_UP
;
2752 mm
.meter
.meter_id
= OFPM13_ALL
;
2755 protocol
= open_vconn_for_flow_mod(bridge
, &vconn
, usable_protocols
);
2756 version
= ofputil_protocol_to_ofp_version(protocol
);
2757 transact_noreply(vconn
, ofputil_encode_meter_request(version
,
2759 mm
.meter
.meter_id
));
2765 ofctl_add_meter(struct ovs_cmdl_context
*ctx
)
2767 ofctl_meter_mod__(ctx
->argv
[1], ctx
->argv
[2], OFPMC13_ADD
);
2771 ofctl_mod_meter(struct ovs_cmdl_context
*ctx
)
2773 ofctl_meter_mod__(ctx
->argv
[1], ctx
->argv
[2], OFPMC13_MODIFY
);
2777 ofctl_del_meters(struct ovs_cmdl_context
*ctx
)
2779 ofctl_meter_mod__(ctx
->argv
[1], ctx
->argc
> 2 ? ctx
->argv
[2] : NULL
, OFPMC13_DELETE
);
2783 ofctl_dump_meters(struct ovs_cmdl_context
*ctx
)
2785 ofctl_meter_request__(ctx
->argv
[1], ctx
->argc
> 2 ? ctx
->argv
[2] : NULL
,
2786 OFPUTIL_METER_CONFIG
);
2790 ofctl_meter_stats(struct ovs_cmdl_context
*ctx
)
2792 ofctl_meter_request__(ctx
->argv
[1], ctx
->argc
> 2 ? ctx
->argv
[2] : NULL
,
2793 OFPUTIL_METER_STATS
);
2797 ofctl_meter_features(struct ovs_cmdl_context
*ctx
)
2799 ofctl_meter_request__(ctx
->argv
[1], NULL
, OFPUTIL_METER_FEATURES
);
2803 /* Undocumented commands for unit testing. */
2806 ofctl_parse_flows__(struct ofputil_flow_mod
*fms
, size_t n_fms
,
2807 enum ofputil_protocol usable_protocols
)
2809 enum ofputil_protocol protocol
= 0;
2813 usable_s
= ofputil_protocols_to_string(usable_protocols
);
2814 printf("usable protocols: %s\n", usable_s
);
2817 if (!(usable_protocols
& allowed_protocols
)) {
2818 ovs_fatal(0, "no usable protocol");
2820 for (i
= 0; i
< sizeof(enum ofputil_protocol
) * CHAR_BIT
; i
++) {
2822 if (protocol
& usable_protocols
& allowed_protocols
) {
2826 ovs_assert(is_pow2(protocol
));
2828 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol
));
2830 for (i
= 0; i
< n_fms
; i
++) {
2831 struct ofputil_flow_mod
*fm
= &fms
[i
];
2834 msg
= ofputil_encode_flow_mod(fm
, protocol
);
2835 ofp_print(stdout
, msg
->data
, msg
->size
, verbosity
);
2838 free(CONST_CAST(struct ofpact
*, fm
->ofpacts
));
2842 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2843 * it back to stdout. */
2845 ofctl_parse_flow(struct ovs_cmdl_context
*ctx
)
2847 enum ofputil_protocol usable_protocols
;
2848 struct ofputil_flow_mod fm
;
2851 error
= parse_ofp_flow_mod_str(&fm
, ctx
->argv
[1], OFPFC_ADD
, &usable_protocols
);
2853 ovs_fatal(0, "%s", error
);
2855 ofctl_parse_flows__(&fm
, 1, usable_protocols
);
2858 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2859 * add-flows) and prints each of the flows back to stdout. */
2861 ofctl_parse_flows(struct ovs_cmdl_context
*ctx
)
2863 enum ofputil_protocol usable_protocols
;
2864 struct ofputil_flow_mod
*fms
= NULL
;
2868 error
= parse_ofp_flow_mod_file(ctx
->argv
[1], OFPFC_ADD
, &fms
, &n_fms
,
2871 ovs_fatal(0, "%s", error
);
2873 ofctl_parse_flows__(fms
, n_fms
, usable_protocols
);
2878 ofctl_parse_nxm__(bool oxm
, enum ofp_version version
)
2883 while (!ds_get_test_line(&in
, stdin
)) {
2884 struct ofpbuf nx_match
;
2886 ovs_be64 cookie
, cookie_mask
;
2890 /* Convert string to nx_match. */
2891 ofpbuf_init(&nx_match
, 0);
2893 match_len
= oxm_match_from_string(ds_cstr(&in
), &nx_match
);
2895 match_len
= nx_match_from_string(ds_cstr(&in
), &nx_match
);
2898 /* Convert nx_match to match. */
2901 error
= oxm_pull_match(&nx_match
, &match
);
2903 error
= nx_pull_match(&nx_match
, match_len
, &match
,
2904 &cookie
, &cookie_mask
);
2908 error
= oxm_pull_match_loose(&nx_match
, &match
);
2910 error
= nx_pull_match_loose(&nx_match
, match_len
, &match
,
2911 &cookie
, &cookie_mask
);
2919 /* Convert match back to nx_match. */
2920 ofpbuf_uninit(&nx_match
);
2921 ofpbuf_init(&nx_match
, 0);
2923 match_len
= oxm_put_match(&nx_match
, &match
, version
);
2924 out
= oxm_match_to_string(&nx_match
, match_len
);
2926 match_len
= nx_put_match(&nx_match
, &match
,
2927 cookie
, cookie_mask
);
2928 out
= nx_match_to_string(nx_match
.data
, match_len
);
2934 if (verbosity
> 0) {
2935 ovs_hex_dump(stdout
, nx_match
.data
, nx_match
.size
, 0, false);
2938 printf("nx_pull_match() returned error %s\n",
2939 ofperr_get_name(error
));
2942 ofpbuf_uninit(&nx_match
);
2947 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2948 * stdin, does some internal fussing with them, and then prints them back as
2949 * strings on stdout. */
2951 ofctl_parse_nxm(struct ovs_cmdl_context
*ctx OVS_UNUSED
)
2953 ofctl_parse_nxm__(false, 0);
2956 /* "parse-oxm VERSION": reads a series of OXM nx_match specifications as
2957 * strings from stdin, does some internal fussing with them, and then prints
2958 * them back as strings on stdout. VERSION must specify an OpenFlow version,
2959 * e.g. "OpenFlow12". */
2961 ofctl_parse_oxm(struct ovs_cmdl_context
*ctx
)
2963 enum ofp_version version
= ofputil_version_from_string(ctx
->argv
[1]);
2964 if (version
< OFP12_VERSION
) {
2965 ovs_fatal(0, "%s: not a valid version for OXM", ctx
->argv
[1]);
2968 ofctl_parse_nxm__(true, version
);
2972 print_differences(const char *prefix
,
2973 const void *a_
, size_t a_len
,
2974 const void *b_
, size_t b_len
)
2976 const uint8_t *a
= a_
;
2977 const uint8_t *b
= b_
;
2980 for (i
= 0; i
< MIN(a_len
, b_len
); i
++) {
2982 printf("%s%2"PRIuSIZE
": %02"PRIx8
" -> %02"PRIx8
"\n",
2983 prefix
, i
, a
[i
], b
[i
]);
2986 for (i
= a_len
; i
< b_len
; i
++) {
2987 printf("%s%2"PRIuSIZE
": (none) -> %02"PRIx8
"\n", prefix
, i
, b
[i
]);
2989 for (i
= b_len
; i
< a_len
; i
++) {
2990 printf("%s%2"PRIuSIZE
": %02"PRIx8
" -> (none)\n", prefix
, i
, a
[i
]);
2995 ofctl_parse_actions__(const char *version_s
, bool instructions
)
2997 enum ofp_version version
;
3000 version
= ofputil_version_from_string(version_s
);
3002 ovs_fatal(0, "%s: not a valid OpenFlow version", version_s
);
3006 while (!ds_get_preprocessed_line(&in
, stdin
, NULL
)) {
3007 struct ofpbuf of_out
;
3008 struct ofpbuf of_in
;
3009 struct ofpbuf ofpacts
;
3010 const char *table_id
;
3016 /* Parse table_id separated with the follow-up actions by ",", if
3018 actions
= ds_cstr(&in
);
3020 if (strstr(actions
, ",")) {
3021 table_id
= strsep(&actions
, ",");
3024 /* Parse hex bytes. */
3025 ofpbuf_init(&of_in
, 0);
3026 if (ofpbuf_put_hex(&of_in
, actions
, NULL
)[0] != '\0') {
3027 ovs_fatal(0, "Trailing garbage in hex data");
3030 /* Convert to ofpacts. */
3031 ofpbuf_init(&ofpacts
, 0);
3033 error
= (instructions
3034 ? ofpacts_pull_openflow_instructions
3035 : ofpacts_pull_openflow_actions
)(
3036 &of_in
, of_in
.size
, version
, &ofpacts
);
3037 if (!error
&& instructions
) {
3038 /* Verify actions, enforce consistency. */
3039 enum ofputil_protocol protocol
;
3042 memset(&flow
, 0, sizeof flow
);
3043 protocol
= ofputil_protocols_from_ofp_version(version
);
3044 error
= ofpacts_check_consistency(ofpacts
.data
, ofpacts
.size
,
3046 table_id
? atoi(table_id
) : 0,
3050 printf("bad %s %s: %s\n\n",
3051 version_s
, instructions
? "instructions" : "actions",
3052 ofperr_get_name(error
));
3053 ofpbuf_uninit(&ofpacts
);
3054 ofpbuf_uninit(&of_in
);
3057 ofpbuf_push_uninit(&of_in
, size
);
3059 /* Print cls_rule. */
3061 ds_put_cstr(&s
, "actions=");
3062 ofpacts_format(ofpacts
.data
, ofpacts
.size
, &s
);
3066 /* Convert back to ofp10 actions and print differences from input. */
3067 ofpbuf_init(&of_out
, 0);
3069 ofpacts_put_openflow_instructions(ofpacts
.data
, ofpacts
.size
,
3072 ofpacts_put_openflow_actions(ofpacts
.data
, ofpacts
.size
,
3076 print_differences("", of_in
.data
, of_in
.size
,
3077 of_out
.data
, of_out
.size
);
3080 ofpbuf_uninit(&ofpacts
);
3081 ofpbuf_uninit(&of_in
);
3082 ofpbuf_uninit(&of_out
);
3087 /* "parse-actions VERSION": reads a series of action specifications for the
3088 * given OpenFlow VERSION as hex bytes from stdin, converts them to ofpacts,
3089 * prints them as strings on stdout, and then converts them back to hex bytes
3090 * and prints any differences from the input. */
3092 ofctl_parse_actions(struct ovs_cmdl_context
*ctx
)
3094 ofctl_parse_actions__(ctx
->argv
[1], false);
3097 /* "parse-actions VERSION": reads a series of instruction specifications for
3098 * the given OpenFlow VERSION as hex bytes from stdin, converts them to
3099 * ofpacts, prints them as strings on stdout, and then converts them back to
3100 * hex bytes and prints any differences from the input. */
3102 ofctl_parse_instructions(struct ovs_cmdl_context
*ctx
)
3104 ofctl_parse_actions__(ctx
->argv
[1], true);
3107 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
3108 * bytes from stdin, converts them to cls_rules, prints them as strings on
3109 * stdout, and then converts them back to hex bytes and prints any differences
3112 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
3113 * values are ignored in the input and will be set to zero when OVS converts
3114 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
3115 * it does the conversion to hex, to ensure that in fact they are ignored. */
3117 ofctl_parse_ofp10_match(struct ovs_cmdl_context
*ctx OVS_UNUSED
)
3124 while (!ds_get_preprocessed_line(&in
, stdin
, NULL
)) {
3125 struct ofpbuf match_in
, match_expout
;
3126 struct ofp10_match match_out
;
3127 struct ofp10_match match_normal
;
3131 /* Parse hex bytes to use for expected output. */
3133 ds_put_cstr(&expout
, ds_cstr(&in
));
3134 for (p
= ds_cstr(&expout
); *p
; p
++) {
3139 ofpbuf_init(&match_expout
, 0);
3140 if (ofpbuf_put_hex(&match_expout
, ds_cstr(&expout
), NULL
)[0] != '\0') {
3141 ovs_fatal(0, "Trailing garbage in hex data");
3143 if (match_expout
.size
!= sizeof(struct ofp10_match
)) {
3144 ovs_fatal(0, "Input is %"PRIu32
" bytes, expected %"PRIuSIZE
,
3145 match_expout
.size
, sizeof(struct ofp10_match
));
3148 /* Parse hex bytes for input. */
3149 for (p
= ds_cstr(&in
); *p
; p
++) {
3151 *p
= "0123456789abcdef"[random_uint32() & 0xf];
3154 ofpbuf_init(&match_in
, 0);
3155 if (ofpbuf_put_hex(&match_in
, ds_cstr(&in
), NULL
)[0] != '\0') {
3156 ovs_fatal(0, "Trailing garbage in hex data");
3158 if (match_in
.size
!= sizeof(struct ofp10_match
)) {
3159 ovs_fatal(0, "Input is %"PRIu32
" bytes, expected %"PRIuSIZE
,
3160 match_in
.size
, sizeof(struct ofp10_match
));
3163 /* Convert to cls_rule and print. */
3164 ofputil_match_from_ofp10_match(match_in
.data
, &match
);
3165 match_print(&match
);
3167 /* Convert back to ofp10_match and print differences from input. */
3168 ofputil_match_to_ofp10_match(&match
, &match_out
);
3169 print_differences("", match_expout
.data
, match_expout
.size
,
3170 &match_out
, sizeof match_out
);
3172 /* Normalize, then convert and compare again. */
3173 ofputil_normalize_match(&match
);
3174 ofputil_match_to_ofp10_match(&match
, &match_normal
);
3175 print_differences("normal: ", &match_out
, sizeof match_out
,
3176 &match_normal
, sizeof match_normal
);
3179 ofpbuf_uninit(&match_in
);
3180 ofpbuf_uninit(&match_expout
);
3183 ds_destroy(&expout
);
3186 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
3187 * bytes from stdin, converts them to "struct match"es, prints them as strings
3188 * on stdout, and then converts them back to hex bytes and prints any
3189 * differences from the input. */
3191 ofctl_parse_ofp11_match(struct ovs_cmdl_context
*ctx OVS_UNUSED
)
3196 while (!ds_get_preprocessed_line(&in
, stdin
, NULL
)) {
3197 struct ofpbuf match_in
;
3198 struct ofp11_match match_out
;
3202 /* Parse hex bytes. */
3203 ofpbuf_init(&match_in
, 0);
3204 if (ofpbuf_put_hex(&match_in
, ds_cstr(&in
), NULL
)[0] != '\0') {
3205 ovs_fatal(0, "Trailing garbage in hex data");
3207 if (match_in
.size
!= sizeof(struct ofp11_match
)) {
3208 ovs_fatal(0, "Input is %"PRIu32
" bytes, expected %"PRIuSIZE
,
3209 match_in
.size
, sizeof(struct ofp11_match
));
3212 /* Convert to match. */
3213 error
= ofputil_match_from_ofp11_match(match_in
.data
, &match
);
3215 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error
));
3216 ofpbuf_uninit(&match_in
);
3221 match_print(&match
);
3223 /* Convert back to ofp11_match and print differences from input. */
3224 ofputil_match_to_ofp11_match(&match
, &match_out
);
3226 print_differences("", match_in
.data
, match_in
.size
,
3227 &match_out
, sizeof match_out
);
3230 ofpbuf_uninit(&match_in
);
3235 /* "parse-pcap PCAP": read packets from PCAP and print their flows. */
3237 ofctl_parse_pcap(struct ovs_cmdl_context
*ctx
)
3241 pcap
= ovs_pcap_open(ctx
->argv
[1], "rb");
3243 ovs_fatal(errno
, "%s: open failed", ctx
->argv
[1]);
3247 struct dp_packet
*packet
;
3251 error
= ovs_pcap_read(pcap
, &packet
, NULL
);
3255 ovs_fatal(error
, "%s: read failed", ctx
->argv
[1]);
3258 packet
->md
= PKT_METADATA_INITIALIZER(ODPP_NONE
);
3259 flow_extract(packet
, &flow
);
3260 flow_print(stdout
, &flow
);
3262 dp_packet_delete(packet
);
3266 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
3267 * mask values to and from various formats and prints the results. */
3269 ofctl_check_vlan(struct ovs_cmdl_context
*ctx
)
3274 struct ofputil_flow_mod fm
;
3277 struct match nxm_match
;
3281 struct ofp10_match of10_raw
;
3282 struct match of10_match
;
3284 struct ofp11_match of11_raw
;
3285 struct match of11_match
;
3290 enum ofputil_protocol usable_protocols
; /* Unused for now. */
3292 match_init_catchall(&match
);
3293 match
.flow
.vlan_tci
= htons(strtoul(ctx
->argv
[1], NULL
, 16));
3294 match
.wc
.masks
.vlan_tci
= htons(strtoul(ctx
->argv
[2], NULL
, 16));
3296 /* Convert to and from string. */
3297 string_s
= match_to_string(&match
, OFP_DEFAULT_PRIORITY
);
3298 printf("%s -> ", string_s
);
3300 error_s
= parse_ofp_str(&fm
, -1, string_s
, &usable_protocols
);
3302 ovs_fatal(0, "%s", error_s
);
3304 printf("%04"PRIx16
"/%04"PRIx16
"\n",
3305 ntohs(fm
.match
.flow
.vlan_tci
),
3306 ntohs(fm
.match
.wc
.masks
.vlan_tci
));
3309 /* Convert to and from NXM. */
3310 ofpbuf_init(&nxm
, 0);
3311 nxm_match_len
= nx_put_match(&nxm
, &match
, htonll(0), htonll(0));
3312 nxm_s
= nx_match_to_string(nxm
.data
, nxm_match_len
);
3313 error
= nx_pull_match(&nxm
, nxm_match_len
, &nxm_match
, NULL
, NULL
);
3314 printf("NXM: %s -> ", nxm_s
);
3316 printf("%s\n", ofperr_to_string(error
));
3318 printf("%04"PRIx16
"/%04"PRIx16
"\n",
3319 ntohs(nxm_match
.flow
.vlan_tci
),
3320 ntohs(nxm_match
.wc
.masks
.vlan_tci
));
3323 ofpbuf_uninit(&nxm
);
3325 /* Convert to and from OXM. */
3326 ofpbuf_init(&nxm
, 0);
3327 nxm_match_len
= oxm_put_match(&nxm
, &match
, OFP12_VERSION
);
3328 nxm_s
= oxm_match_to_string(&nxm
, nxm_match_len
);
3329 error
= oxm_pull_match(&nxm
, &nxm_match
);
3330 printf("OXM: %s -> ", nxm_s
);
3332 printf("%s\n", ofperr_to_string(error
));
3334 uint16_t vid
= ntohs(nxm_match
.flow
.vlan_tci
) &
3335 (VLAN_VID_MASK
| VLAN_CFI
);
3336 uint16_t mask
= ntohs(nxm_match
.wc
.masks
.vlan_tci
) &
3337 (VLAN_VID_MASK
| VLAN_CFI
);
3339 printf("%04"PRIx16
"/%04"PRIx16
",", vid
, mask
);
3340 if (vid
&& vlan_tci_to_pcp(nxm_match
.wc
.masks
.vlan_tci
)) {
3341 printf("%02"PRIx8
"\n", vlan_tci_to_pcp(nxm_match
.flow
.vlan_tci
));
3347 ofpbuf_uninit(&nxm
);
3349 /* Convert to and from OpenFlow 1.0. */
3350 ofputil_match_to_ofp10_match(&match
, &of10_raw
);
3351 ofputil_match_from_ofp10_match(&of10_raw
, &of10_match
);
3352 printf("OF1.0: %04"PRIx16
"/%d,%02"PRIx8
"/%d -> %04"PRIx16
"/%04"PRIx16
"\n",
3353 ntohs(of10_raw
.dl_vlan
),
3354 (of10_raw
.wildcards
& htonl(OFPFW10_DL_VLAN
)) != 0,
3355 of10_raw
.dl_vlan_pcp
,
3356 (of10_raw
.wildcards
& htonl(OFPFW10_DL_VLAN_PCP
)) != 0,
3357 ntohs(of10_match
.flow
.vlan_tci
),
3358 ntohs(of10_match
.wc
.masks
.vlan_tci
));
3360 /* Convert to and from OpenFlow 1.1. */
3361 ofputil_match_to_ofp11_match(&match
, &of11_raw
);
3362 ofputil_match_from_ofp11_match(&of11_raw
, &of11_match
);
3363 printf("OF1.1: %04"PRIx16
"/%d,%02"PRIx8
"/%d -> %04"PRIx16
"/%04"PRIx16
"\n",
3364 ntohs(of11_raw
.dl_vlan
),
3365 (of11_raw
.wildcards
& htonl(OFPFW11_DL_VLAN
)) != 0,
3366 of11_raw
.dl_vlan_pcp
,
3367 (of11_raw
.wildcards
& htonl(OFPFW11_DL_VLAN_PCP
)) != 0,
3368 ntohs(of11_match
.flow
.vlan_tci
),
3369 ntohs(of11_match
.wc
.masks
.vlan_tci
));
3372 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
3375 ofctl_print_error(struct ovs_cmdl_context
*ctx
)
3380 error
= ofperr_from_name(ctx
->argv
[1]);
3382 ovs_fatal(0, "unknown error \"%s\"", ctx
->argv
[1]);
3385 for (version
= 0; version
<= UINT8_MAX
; version
++) {
3386 const char *name
= ofperr_domain_get_name(version
);
3388 int vendor
= ofperr_get_vendor(error
, version
);
3389 int type
= ofperr_get_type(error
, version
);
3390 int code
= ofperr_get_code(error
, version
);
3392 if (vendor
!= -1 || type
!= -1 || code
!= -1) {
3393 printf("%s: vendor %#x, type %d, code %d\n",
3394 name
, vendor
, type
, code
);
3400 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
3401 * error named ENUM and prints the error reply in hex. */
3403 ofctl_encode_error_reply(struct ovs_cmdl_context
*ctx
)
3405 const struct ofp_header
*oh
;
3406 struct ofpbuf request
, *reply
;
3409 error
= ofperr_from_name(ctx
->argv
[1]);
3411 ovs_fatal(0, "unknown error \"%s\"", ctx
->argv
[1]);
3414 ofpbuf_init(&request
, 0);
3415 if (ofpbuf_put_hex(&request
, ctx
->argv
[2], NULL
)[0] != '\0') {
3416 ovs_fatal(0, "Trailing garbage in hex data");
3418 if (request
.size
< sizeof(struct ofp_header
)) {
3419 ovs_fatal(0, "Request too short");
3423 if (request
.size
!= ntohs(oh
->length
)) {
3424 ovs_fatal(0, "Request size inconsistent");
3427 reply
= ofperr_encode_reply(error
, request
.data
);
3428 ofpbuf_uninit(&request
);
3430 ovs_hex_dump(stdout
, reply
->data
, reply
->size
, 0, false);
3431 ofpbuf_delete(reply
);
3434 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
3435 * binary data, interpreting them as an OpenFlow message, and prints the
3436 * OpenFlow message on stdout, at VERBOSITY (level 2 by default).
3438 * Alternative usage: "ofp-print [VERBOSITY] - < HEXSTRING_FILE", where
3439 * HEXSTRING_FILE contains the HEXSTRING. */
3441 ofctl_ofp_print(struct ovs_cmdl_context
*ctx
)
3443 struct ofpbuf packet
;
3450 if (!strcmp(ctx
->argv
[ctx
->argc
-1], "-")) {
3451 if (ds_get_line(&line
, stdin
)) {
3452 VLOG_FATAL("Failed to read stdin");
3455 buffer
= line
.string
;
3456 verbosity
= ctx
->argc
> 2 ? atoi(ctx
->argv
[1]) : verbosity
;
3457 } else if (ctx
->argc
> 2) {
3458 buffer
= ctx
->argv
[1];
3459 verbosity
= atoi(ctx
->argv
[2]);
3461 buffer
= ctx
->argv
[1];
3464 ofpbuf_init(&packet
, strlen(buffer
) / 2);
3465 if (ofpbuf_put_hex(&packet
, buffer
, NULL
)[0] != '\0') {
3466 ovs_fatal(0, "trailing garbage following hex bytes");
3468 ofp_print(stdout
, packet
.data
, packet
.size
, verbosity
);
3469 ofpbuf_uninit(&packet
);
3473 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
3474 * and dumps each message in hex. */
3476 ofctl_encode_hello(struct ovs_cmdl_context
*ctx
)
3478 uint32_t bitmap
= strtol(ctx
->argv
[1], NULL
, 0);
3479 struct ofpbuf
*hello
;
3481 hello
= ofputil_encode_hello(bitmap
);
3482 ovs_hex_dump(stdout
, hello
->data
, hello
->size
, 0, false);
3483 ofp_print(stdout
, hello
->data
, hello
->size
, verbosity
);
3484 ofpbuf_delete(hello
);
3487 static const struct ovs_cmdl_command all_commands
[] = {
3490 { "monitor", "switch [misslen] [invalid_ttl] [watch:[...]]",
3491 1, 3, ofctl_monitor
},
3492 { "snoop", "switch",
3493 1, 1, ofctl_snoop
},
3494 { "dump-desc", "switch",
3495 1, 1, ofctl_dump_desc
},
3496 { "dump-tables", "switch",
3497 1, 1, ofctl_dump_tables
},
3498 { "dump-table-features", "switch",
3499 1, 1, ofctl_dump_table_features
},
3500 { "dump-flows", "switch",
3501 1, 2, ofctl_dump_flows
},
3502 { "dump-aggregate", "switch",
3503 1, 2, ofctl_dump_aggregate
},
3504 { "queue-stats", "switch [port [queue]]",
3505 1, 3, ofctl_queue_stats
},
3506 { "queue-get-config", "switch port",
3507 2, 2, ofctl_queue_get_config
},
3508 { "add-flow", "switch flow",
3509 2, 2, ofctl_add_flow
},
3510 { "add-flows", "switch file",
3511 2, 2, ofctl_add_flows
},
3512 { "mod-flows", "switch flow",
3513 2, 2, ofctl_mod_flows
},
3514 { "del-flows", "switch [flow]",
3515 1, 2, ofctl_del_flows
},
3516 { "replace-flows", "switch file",
3517 2, 2, ofctl_replace_flows
},
3518 { "diff-flows", "source1 source2",
3519 2, 2, ofctl_diff_flows
},
3520 { "add-meter", "switch meter",
3521 2, 2, ofctl_add_meter
},
3522 { "mod-meter", "switch meter",
3523 2, 2, ofctl_mod_meter
},
3524 { "del-meter", "switch meter",
3525 2, 2, ofctl_del_meters
},
3526 { "del-meters", "switch",
3527 1, 1, ofctl_del_meters
},
3528 { "dump-meter", "switch meter",
3529 2, 2, ofctl_dump_meters
},
3530 { "dump-meters", "switch",
3531 1, 1, ofctl_dump_meters
},
3532 { "meter-stats", "switch [meter]",
3533 1, 2, ofctl_meter_stats
},
3534 { "meter-features", "switch",
3535 1, 1, ofctl_meter_features
},
3536 { "packet-out", "switch in_port actions packet...",
3537 4, INT_MAX
, ofctl_packet_out
},
3538 { "dump-ports", "switch [port]",
3539 1, 2, ofctl_dump_ports
},
3540 { "dump-ports-desc", "switch [port]",
3541 1, 2, ofctl_dump_ports_desc
},
3542 { "mod-port", "switch iface act",
3543 3, 3, ofctl_mod_port
},
3544 { "mod-table", "switch mod",
3545 3, 3, ofctl_mod_table
},
3546 { "get-frags", "switch",
3547 1, 1, ofctl_get_frags
},
3548 { "set-frags", "switch frag_mode",
3549 2, 2, ofctl_set_frags
},
3550 { "probe", "target",
3551 1, 1, ofctl_probe
},
3552 { "ping", "target [n]",
3554 { "benchmark", "target n count",
3555 3, 3, ofctl_benchmark
},
3557 { "ofp-parse", "file",
3558 1, 1, ofctl_ofp_parse
},
3559 { "ofp-parse-pcap", "pcap",
3560 1, INT_MAX
, ofctl_ofp_parse_pcap
},
3562 { "add-group", "switch group",
3563 1, 2, ofctl_add_group
},
3564 { "add-groups", "switch file",
3565 1, 2, ofctl_add_groups
},
3566 { "mod-group", "switch group",
3567 1, 2, ofctl_mod_group
},
3568 { "del-groups", "switch [group]",
3569 1, 2, ofctl_del_groups
},
3570 { "insert-buckets", "switch [group]",
3571 1, 2, ofctl_insert_bucket
},
3572 { "remove-buckets", "switch [group]",
3573 1, 2, ofctl_remove_bucket
},
3574 { "dump-groups", "switch [group]",
3575 1, 2, ofctl_dump_group_desc
},
3576 { "dump-group-stats", "switch [group]",
3577 1, 2, ofctl_dump_group_stats
},
3578 { "dump-group-features", "switch",
3579 1, 1, ofctl_dump_group_features
},
3580 { "help", NULL
, 0, INT_MAX
, ofctl_help
},
3581 { "list-commands", NULL
, 0, INT_MAX
, ofctl_list_commands
},
3583 /* Undocumented commands for testing. */
3584 { "parse-flow", NULL
, 1, 1, ofctl_parse_flow
},
3585 { "parse-flows", NULL
, 1, 1, ofctl_parse_flows
},
3586 { "parse-nx-match", NULL
, 0, 0, ofctl_parse_nxm
},
3587 { "parse-nxm", NULL
, 0, 0, ofctl_parse_nxm
},
3588 { "parse-oxm", NULL
, 1, 1, ofctl_parse_oxm
},
3589 { "parse-actions", NULL
, 1, 1, ofctl_parse_actions
},
3590 { "parse-instructions", NULL
, 1, 1, ofctl_parse_instructions
},
3591 { "parse-ofp10-match", NULL
, 0, 0, ofctl_parse_ofp10_match
},
3592 { "parse-ofp11-match", NULL
, 0, 0, ofctl_parse_ofp11_match
},
3593 { "parse-pcap", NULL
, 1, 1, ofctl_parse_pcap
},
3594 { "check-vlan", NULL
, 2, 2, ofctl_check_vlan
},
3595 { "print-error", NULL
, 1, 1, ofctl_print_error
},
3596 { "encode-error-reply", NULL
, 2, 2, ofctl_encode_error_reply
},
3597 { "ofp-print", NULL
, 1, 2, ofctl_ofp_print
},
3598 { "encode-hello", NULL
, 1, 1, ofctl_encode_hello
},
3600 { NULL
, NULL
, 0, 0, NULL
},
3603 static const struct ovs_cmdl_command
*get_all_commands(void)
3605 return all_commands
;