2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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 /* --bundle: Use OpenFlow 1.3+ bundle for making the flow table change atomic.
71 * NOTE: If OpenFlow 1.3 or higher is not selected with the '-O' option,
72 * OpenFlow 1.4 will be implicitly selected. Also the flow mod will use
73 * OpenFlow 1.4, so the semantics may be different (see the comment in
74 * parse_options() for details).
76 static bool bundle
= false;
78 /* --strict: Use strict matching for flow mod commands? Additionally governs
79 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
83 /* --readd: If true, on replace-flows, re-add even flows that have not changed
84 * (to reset flow counters). */
87 /* -F, --flow-format: Allowed protocols. By default, any protocol is
89 static enum ofputil_protocol allowed_protocols
= OFPUTIL_P_ANY
;
91 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
92 * commands. Either one of NXPIF_* to force a particular packet_in format, or
93 * -1 to let ovs-ofctl choose the default. */
94 static int preferred_packet_in_format
= -1;
96 /* -m, --more: Additional verbosity for ofp-print functions. */
99 /* --timestamp: Print a timestamp before each received packet on "monitor" and
100 * "snoop" command? */
101 static bool timestamp
;
103 /* --unixctl-path: Path to use for unixctl server, for "monitor" and "snoop"
105 static char *unixctl_path
;
107 /* --sort, --rsort: Sort order. */
108 enum sort_order
{ SORT_ASC
, SORT_DESC
};
109 struct sort_criterion
{
110 const struct mf_field
*field
; /* NULL means to sort by priority. */
111 enum sort_order order
;
113 static struct sort_criterion
*criteria
;
114 static size_t n_criteria
, allocated_criteria
;
116 static const struct ovs_cmdl_command
*get_all_commands(void);
118 OVS_NO_RETURN
static void usage(void);
119 static void parse_options(int argc
, char *argv
[]);
121 static bool recv_flow_stats_reply(struct vconn
*, ovs_be32 send_xid
,
122 struct ofpbuf
**replyp
,
123 struct ofputil_flow_stats
*,
124 struct ofpbuf
*ofpacts
);
126 main(int argc
, char *argv
[])
128 struct ovs_cmdl_context ctx
= { .argc
= 0, };
129 set_program_name(argv
[0]);
130 service_start(&argc
, &argv
);
131 parse_options(argc
, argv
);
132 fatal_ignore_sigpipe();
133 ctx
.argc
= argc
- optind
;
134 ctx
.argv
= argv
+ optind
;
136 daemon_become_new_user(false);
137 ovs_cmdl_run_command(&ctx
, get_all_commands());
142 add_sort_criterion(enum sort_order order
, const char *field
)
144 struct sort_criterion
*sc
;
146 if (n_criteria
>= allocated_criteria
) {
147 criteria
= x2nrealloc(criteria
, &allocated_criteria
, sizeof *criteria
);
150 sc
= &criteria
[n_criteria
++];
151 if (!field
|| !strcasecmp(field
, "priority")) {
154 sc
->field
= mf_from_name(field
);
156 ovs_fatal(0, "%s: unknown field name", field
);
163 parse_options(int argc
, char *argv
[])
166 OPT_STRICT
= UCHAR_MAX
+ 1,
174 OFP_VERSION_OPTION_ENUMS
,
177 static const struct option long_options
[] = {
178 {"timeout", required_argument
, NULL
, 't'},
179 {"strict", no_argument
, NULL
, OPT_STRICT
},
180 {"readd", no_argument
, NULL
, OPT_READD
},
181 {"flow-format", required_argument
, NULL
, 'F'},
182 {"packet-in-format", required_argument
, NULL
, 'P'},
183 {"more", no_argument
, NULL
, 'm'},
184 {"timestamp", no_argument
, NULL
, OPT_TIMESTAMP
},
185 {"sort", optional_argument
, NULL
, OPT_SORT
},
186 {"rsort", optional_argument
, NULL
, OPT_RSORT
},
187 {"unixctl", required_argument
, NULL
, OPT_UNIXCTL
},
188 {"help", no_argument
, NULL
, 'h'},
189 {"option", no_argument
, NULL
, 'o'},
190 {"bundle", no_argument
, NULL
, OPT_BUNDLE
},
192 OFP_VERSION_LONG_OPTIONS
,
194 STREAM_SSL_LONG_OPTIONS
,
197 char *short_options
= ovs_cmdl_long_options_to_short_options(long_options
);
199 enum ofputil_protocol version_protocols
;
201 /* For now, ovs-ofctl only enables OpenFlow 1.0 by default. This is
202 * because ovs-ofctl implements command such as "add-flow" as raw OpenFlow
203 * requests, but those requests have subtly different semantics in
204 * different OpenFlow versions. For example:
206 * - In OpenFlow 1.0, a "mod-flow" operation that does not find any
207 * existing flow to modify adds a new flow.
209 * - In OpenFlow 1.1, a "mod-flow" operation that does not find any
210 * existing flow to modify adds a new flow, but only if the mod-flow
211 * did not match on the flow cookie.
213 * - In OpenFlow 1.2 and a later, a "mod-flow" operation never adds a
216 set_allowed_ofp_versions("OpenFlow10");
219 unsigned long int timeout
;
222 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
229 timeout
= strtoul(optarg
, NULL
, 10);
231 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
239 allowed_protocols
= ofputil_protocols_from_string(optarg
);
240 if (!allowed_protocols
) {
241 ovs_fatal(0, "%s: invalid flow format(s)", optarg
);
246 preferred_packet_in_format
=
247 ofputil_packet_in_format_from_string(optarg
);
248 if (preferred_packet_in_format
< 0) {
249 ovs_fatal(0, "unknown packet-in format `%s'", optarg
);
261 ovs_cmdl_print_options(long_options
);
281 add_sort_criterion(SORT_ASC
, optarg
);
285 add_sort_criterion(SORT_DESC
, optarg
);
289 unixctl_path
= optarg
;
292 DAEMON_OPTION_HANDLERS
293 OFP_VERSION_OPTION_HANDLERS
295 STREAM_SSL_OPTION_HANDLERS
306 /* Always do a final sort pass based on priority. */
307 add_sort_criterion(SORT_DESC
, "priority");
312 /* Implicit OpenFlow 1.4 with the '--bundle' option. */
313 if (bundle
&& !(get_allowed_ofp_versions() &
314 ofputil_protocols_to_version_bitmap(OFPUTIL_P_OF13_UP
))) {
315 /* Add implicit allowance for OpenFlow 1.4. */
316 add_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
317 OFPUTIL_P_OF14_OXM
));
318 /* Remove all versions that do not support bundles. */
319 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
322 versions
= get_allowed_ofp_versions();
323 version_protocols
= ofputil_protocols_from_version_bitmap(versions
);
324 if (!(allowed_protocols
& version_protocols
)) {
325 char *protocols
= ofputil_protocols_to_string(allowed_protocols
);
326 struct ds version_s
= DS_EMPTY_INITIALIZER
;
328 ofputil_format_version_bitmap_names(&version_s
, versions
);
329 ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports "
330 "any of the enabled flow formats (%s). (Use -O to enable "
331 "additional OpenFlow versions or -F to enable additional "
332 "flow formats.)", ds_cstr(&version_s
), protocols
);
334 allowed_protocols
&= version_protocols
;
335 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
342 printf("%s: OpenFlow switch management utility\n"
343 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
344 "\nFor OpenFlow switches:\n"
345 " show SWITCH show OpenFlow information\n"
346 " dump-desc SWITCH print switch description\n"
347 " dump-tables SWITCH print table stats\n"
348 " dump-table-features SWITCH print table features\n"
349 " dump-table-desc SWITCH print table description (OF1.4+)\n"
350 " mod-port SWITCH IFACE ACT modify port behavior\n"
351 " mod-table SWITCH MOD modify flow table behavior\n"
352 " OF1.1/1.2 MOD: controller, continue, drop\n"
353 " OF1.4+ MOD: evict, noevict, vacancy:low,high, novacancy\n"
354 " get-frags SWITCH print fragment handling behavior\n"
355 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
356 " FRAG_MODE: normal, drop, reassemble, nx-match\n"
357 " dump-ports SWITCH [PORT] print port statistics\n"
358 " dump-ports-desc SWITCH [PORT] print port descriptions\n"
359 " dump-flows SWITCH print all flow entries\n"
360 " dump-flows SWITCH FLOW print matching FLOWs\n"
361 " dump-aggregate SWITCH print aggregate flow statistics\n"
362 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
363 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
364 " add-flow SWITCH FLOW add flow described by FLOW\n"
365 " add-flows SWITCH FILE add flows from FILE\n"
366 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
367 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
368 " replace-flows SWITCH FILE replace flows with those in FILE\n"
369 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
370 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
371 " execute ACTIONS on PACKET\n"
372 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
373 " print packets received from SWITCH\n"
374 " snoop SWITCH snoop on SWITCH and its controller\n"
375 " add-group SWITCH GROUP add group described by GROUP\n"
376 " add-groups SWITCH FILE add group from FILE\n"
377 " mod-group SWITCH GROUP modify specific group\n"
378 " del-groups SWITCH [GROUP] delete matching GROUPs\n"
379 " insert-buckets SWITCH [GROUP] add buckets to GROUP\n"
380 " remove-buckets SWITCH [GROUP] remove buckets from GROUP\n"
381 " dump-group-features SWITCH print group features\n"
382 " dump-groups SWITCH [GROUP] print group description\n"
383 " dump-group-stats SWITCH [GROUP] print group statistics\n"
384 " queue-get-config SWITCH [PORT] print queue config for PORT\n"
385 " add-meter SWITCH METER add meter described by METER\n"
386 " mod-meter SWITCH METER modify specific METER\n"
387 " del-meter SWITCH METER delete METER\n"
388 " del-meters SWITCH delete all meters\n"
389 " dump-meter SWITCH METER print METER configuration\n"
390 " dump-meters SWITCH print all meter configuration\n"
391 " meter-stats SWITCH [METER] print meter statistics\n"
392 " meter-features SWITCH print meter features\n"
393 " add-tlv-map SWITCH MAP add TLV option MAPpings\n"
394 " del-tlv-map SWITCH [MAP] delete TLV option MAPpings\n"
395 " dump-tlv-map SWITCH print TLV option mappings\n"
396 "\nFor OpenFlow switches and controllers:\n"
397 " probe TARGET probe whether TARGET is up\n"
398 " ping TARGET [N] latency of N-byte echos\n"
399 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
400 "SWITCH or TARGET is an active OpenFlow connection method.\n"
401 "\nOther commands:\n"
402 " ofp-parse FILE print messages read from FILE\n"
403 " ofp-parse-pcap PCAP print OpenFlow read from PCAP\n",
404 program_name
, program_name
);
405 vconn_usage(true, false, false);
409 printf("\nOther options:\n"
410 " --strict use strict match for flow commands\n"
411 " --readd replace flows that haven't changed\n"
412 " -F, --flow-format=FORMAT force particular flow format\n"
413 " -P, --packet-in-format=FRMT force particular packet in format\n"
414 " -m, --more be more verbose printing OpenFlow\n"
415 " --timestamp (monitor, snoop) print timestamps\n"
416 " -t, --timeout=SECS give up after SECS seconds\n"
417 " --sort[=field] sort in ascending order\n"
418 " --rsort[=field] sort in descending order\n"
419 " --unixctl=SOCKET set control socket name\n"
420 " -h, --help display this help message\n"
421 " -V, --version display version information\n");
426 ofctl_exit(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
427 const char *argv
[] OVS_UNUSED
, void *exiting_
)
429 bool *exiting
= exiting_
;
431 unixctl_command_reply(conn
, NULL
);
434 static void run(int retval
, const char *message
, ...)
435 OVS_PRINTF_FORMAT(2, 3);
438 run(int retval
, const char *message
, ...)
443 va_start(args
, message
);
444 ovs_fatal_valist(retval
, message
, args
);
448 /* Generic commands. */
451 open_vconn_socket(const char *name
, struct vconn
**vconnp
)
453 char *vconn_name
= xasprintf("unix:%s", name
);
456 error
= vconn_open(vconn_name
, get_allowed_ofp_versions(), DSCP_DEFAULT
,
458 if (error
&& error
!= ENOENT
) {
459 ovs_fatal(0, "%s: failed to open socket (%s)", name
,
460 ovs_strerror(error
));
467 enum open_target
{ MGMT
, SNOOP
};
469 static enum ofputil_protocol
470 open_vconn__(const char *name
, enum open_target target
,
471 struct vconn
**vconnp
)
473 const char *suffix
= target
== MGMT
? "mgmt" : "snoop";
474 char *datapath_name
, *datapath_type
, *socket_name
;
475 enum ofputil_protocol protocol
;
480 bridge_path
= xasprintf("%s/%s.%s", ovs_rundir(), name
, suffix
);
482 ofproto_parse_name(name
, &datapath_name
, &datapath_type
);
483 socket_name
= xasprintf("%s/%s.%s", ovs_rundir(), datapath_name
, suffix
);
487 if (strchr(name
, ':')) {
488 run(vconn_open(name
, get_allowed_ofp_versions(), DSCP_DEFAULT
, vconnp
),
489 "connecting to %s", name
);
490 } else if (!open_vconn_socket(name
, vconnp
)) {
492 } else if (!open_vconn_socket(bridge_path
, vconnp
)) {
494 } else if (!open_vconn_socket(socket_name
, vconnp
)) {
497 ovs_fatal(0, "%s is not a bridge or a socket", name
);
500 if (target
== SNOOP
) {
501 vconn_set_recv_any_version(*vconnp
);
507 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp
));
508 error
= vconn_connect_block(*vconnp
);
510 ovs_fatal(0, "%s: failed to connect to socket (%s)", name
,
511 ovs_strerror(error
));
514 ofp_version
= vconn_get_version(*vconnp
);
515 protocol
= ofputil_protocol_from_ofp_version(ofp_version
);
517 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
523 static enum ofputil_protocol
524 open_vconn(const char *name
, struct vconn
**vconnp
)
526 return open_vconn__(name
, MGMT
, vconnp
);
530 send_openflow_buffer(struct vconn
*vconn
, struct ofpbuf
*buffer
)
532 run(vconn_send_block(vconn
, buffer
), "failed to send packet to switch");
536 dump_transaction(struct vconn
*vconn
, struct ofpbuf
*request
)
538 const struct ofp_header
*oh
= request
->data
;
539 if (ofpmsg_is_stat_request(oh
)) {
540 ovs_be32 send_xid
= oh
->xid
;
541 enum ofpraw request_raw
;
542 enum ofpraw reply_raw
;
545 ofpraw_decode_partial(&request_raw
, request
->data
, request
->size
);
546 reply_raw
= ofpraw_stats_request_to_reply(request_raw
, oh
->version
);
548 send_openflow_buffer(vconn
, request
);
551 struct ofpbuf
*reply
;
553 run(vconn_recv_block(vconn
, &reply
),
554 "OpenFlow packet receive failed");
555 recv_xid
= ((struct ofp_header
*) reply
->data
)->xid
;
556 if (send_xid
== recv_xid
) {
559 ofp_print(stdout
, reply
->data
, reply
->size
, verbosity
+ 1);
561 ofpraw_decode(&raw
, reply
->data
);
562 if (ofptype_from_ofpraw(raw
) == OFPTYPE_ERROR
) {
564 } else if (raw
== reply_raw
) {
565 done
= !ofpmp_more(reply
->data
);
567 ovs_fatal(0, "received bad reply: %s",
568 ofp_to_string(reply
->data
, reply
->size
,
572 VLOG_DBG("received reply with xid %08"PRIx32
" "
573 "!= expected %08"PRIx32
, recv_xid
, send_xid
);
575 ofpbuf_delete(reply
);
578 struct ofpbuf
*reply
;
580 run(vconn_transact(vconn
, request
, &reply
), "talking to %s",
581 vconn_get_name(vconn
));
582 ofp_print(stdout
, reply
->data
, reply
->size
, verbosity
+ 1);
583 ofpbuf_delete(reply
);
588 dump_trivial_transaction(const char *vconn_name
, enum ofpraw raw
)
590 struct ofpbuf
*request
;
593 open_vconn(vconn_name
, &vconn
);
594 request
= ofpraw_alloc(raw
, vconn_get_version(vconn
), 0);
595 dump_transaction(vconn
, request
);
599 /* Sends all of the 'requests', which should be requests that only have replies
600 * if an error occurs, and waits for them to succeed or fail. If an error does
601 * occur, prints it and exits with an error.
603 * Destroys all of the 'requests'. */
605 transact_multiple_noreply(struct vconn
*vconn
, struct ovs_list
*requests
)
607 struct ofpbuf
*reply
;
609 run(vconn_transact_multiple_noreply(vconn
, requests
, &reply
),
610 "talking to %s", vconn_get_name(vconn
));
612 ofp_print(stderr
, reply
->data
, reply
->size
, verbosity
+ 2);
615 ofpbuf_delete(reply
);
619 bundle_error_reporter(const struct ofp_header
*oh
)
621 ofp_print(stderr
, oh
, ntohs(oh
->length
), verbosity
+ 1);
626 bundle_transact(struct vconn
*vconn
, struct ovs_list
*requests
, uint16_t flags
)
628 run(vconn_bundle_transact(vconn
, requests
, flags
, bundle_error_reporter
),
629 "talking to %s", vconn_get_name(vconn
));
632 /* Sends 'request', which should be a request that only has a reply if an error
633 * occurs, and waits for it to succeed or fail. If an error does occur, prints
634 * it and exits with an error.
636 * Destroys 'request'. */
638 transact_noreply(struct vconn
*vconn
, struct ofpbuf
*request
)
640 struct ovs_list requests
;
642 list_init(&requests
);
643 list_push_back(&requests
, &request
->list_node
);
644 transact_multiple_noreply(vconn
, &requests
);
648 fetch_switch_config(struct vconn
*vconn
, struct ofputil_switch_config
*config
)
650 struct ofpbuf
*request
;
651 struct ofpbuf
*reply
;
654 request
= ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST
,
655 vconn_get_version(vconn
), 0);
656 run(vconn_transact(vconn
, request
, &reply
),
657 "talking to %s", vconn_get_name(vconn
));
659 if (ofptype_decode(&type
, reply
->data
)
660 || type
!= OFPTYPE_GET_CONFIG_REPLY
) {
661 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn
));
663 ofputil_decode_get_config_reply(reply
->data
, config
);
664 ofpbuf_delete(reply
);
668 set_switch_config(struct vconn
*vconn
,
669 const struct ofputil_switch_config
*config
)
671 enum ofp_version version
= vconn_get_version(vconn
);
672 transact_noreply(vconn
, ofputil_encode_set_config(config
, version
));
676 ofctl_show(struct ovs_cmdl_context
*ctx
)
678 const char *vconn_name
= ctx
->argv
[1];
679 enum ofp_version version
;
681 struct ofpbuf
*request
;
682 struct ofpbuf
*reply
;
685 open_vconn(vconn_name
, &vconn
);
686 version
= vconn_get_version(vconn
);
687 request
= ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST
, version
, 0);
688 run(vconn_transact(vconn
, request
, &reply
), "talking to %s", vconn_name
);
690 has_ports
= ofputil_switch_features_has_ports(reply
);
691 ofp_print(stdout
, reply
->data
, reply
->size
, verbosity
+ 1);
692 ofpbuf_delete(reply
);
695 request
= ofputil_encode_port_desc_stats_request(version
, OFPP_ANY
);
696 dump_transaction(vconn
, request
);
698 dump_trivial_transaction(vconn_name
, OFPRAW_OFPT_GET_CONFIG_REQUEST
);
703 ofctl_dump_desc(struct ovs_cmdl_context
*ctx
)
705 dump_trivial_transaction(ctx
->argv
[1], OFPRAW_OFPST_DESC_REQUEST
);
709 ofctl_dump_tables(struct ovs_cmdl_context
*ctx
)
711 dump_trivial_transaction(ctx
->argv
[1], OFPRAW_OFPST_TABLE_REQUEST
);
715 ofctl_dump_table_features(struct ovs_cmdl_context
*ctx
)
717 struct ofpbuf
*request
;
720 open_vconn(ctx
->argv
[1], &vconn
);
721 request
= ofputil_encode_table_features_request(vconn_get_version(vconn
));
723 /* The following is similar to dump_trivial_transaction(), but it
724 * maintains the previous 'ofputil_table_features' from one stats reply
725 * message to the next, which allows duplication to be eliminated in the
726 * output across messages. Otherwise the output is much larger and harder
727 * to read, because only 17 or so ofputil_table_features elements fit in a
728 * single 64 kB OpenFlow message and therefore you get a ton of repetition
729 * (every 17th element is printed in full instead of abbreviated). */
731 const struct ofp_header
*request_oh
= request
->data
;
732 ovs_be32 send_xid
= request_oh
->xid
;
735 struct ofputil_table_features prev
;
738 send_openflow_buffer(vconn
, request
);
741 struct ofpbuf
*reply
;
743 run(vconn_recv_block(vconn
, &reply
), "OpenFlow packet receive failed");
744 recv_xid
= ((struct ofp_header
*) reply
->data
)->xid
;
745 if (send_xid
== recv_xid
) {
748 error
= ofptype_decode(&type
, reply
->data
);
750 ovs_fatal(0, "decode error: %s", ofperr_get_name(error
));
751 } else if (type
== OFPTYPE_ERROR
) {
752 ofp_print(stdout
, reply
->data
, reply
->size
, verbosity
+ 1);
754 } else if (type
== OFPTYPE_TABLE_FEATURES_STATS_REPLY
) {
755 done
= !ofpmp_more(reply
->data
);
757 struct ofputil_table_features tf
;
760 retval
= ofputil_decode_table_features(reply
, &tf
, true);
763 ovs_fatal(0, "decode error: %s",
764 ofperr_get_name(retval
));
769 struct ds s
= DS_EMPTY_INITIALIZER
;
770 ofp_print_table_features(&s
, &tf
, n
? &prev
: NULL
,
779 ovs_fatal(0, "received bad reply: %s",
780 ofp_to_string(reply
->data
, reply
->size
,
784 VLOG_DBG("received reply with xid %08"PRIx32
" "
785 "!= expected %08"PRIx32
, recv_xid
, send_xid
);
787 ofpbuf_delete(reply
);
794 ofctl_dump_table_desc(struct ovs_cmdl_context
*ctx
)
796 struct ofpbuf
*request
;
799 open_vconn(ctx
->argv
[1], &vconn
);
800 request
= ofputil_encode_table_desc_request(vconn_get_version(vconn
));
802 dump_transaction(vconn
, request
);
810 str_to_ofp(const char *s
, ofp_port_t
*ofp_port
)
815 ret
= str_to_uint(s
, 10, &port_
);
816 *ofp_port
= u16_to_ofp(port_
);
820 struct port_iterator
{
823 enum { PI_FEATURES
, PI_PORT_DESC
} variant
;
824 struct ofpbuf
*reply
;
830 port_iterator_fetch_port_desc(struct port_iterator
*pi
)
832 pi
->variant
= PI_PORT_DESC
;
835 struct ofpbuf
*rq
= ofputil_encode_port_desc_stats_request(
836 vconn_get_version(pi
->vconn
), OFPP_ANY
);
837 pi
->send_xid
= ((struct ofp_header
*) rq
->data
)->xid
;
838 send_openflow_buffer(pi
->vconn
, rq
);
842 port_iterator_fetch_features(struct port_iterator
*pi
)
844 pi
->variant
= PI_FEATURES
;
846 /* Fetch the switch's ofp_switch_features. */
847 enum ofp_version version
= vconn_get_version(pi
->vconn
);
848 struct ofpbuf
*rq
= ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST
, version
, 0);
849 run(vconn_transact(pi
->vconn
, rq
, &pi
->reply
),
850 "talking to %s", vconn_get_name(pi
->vconn
));
853 if (ofptype_decode(&type
, pi
->reply
->data
)
854 || type
!= OFPTYPE_FEATURES_REPLY
) {
855 ovs_fatal(0, "%s: received bad features reply",
856 vconn_get_name(pi
->vconn
));
858 if (!ofputil_switch_features_has_ports(pi
->reply
)) {
859 /* The switch features reply does not contain a complete list of ports.
860 * Probably, there are more ports than will fit into a single 64 kB
861 * OpenFlow message. Use OFPST_PORT_DESC to get a complete list of
863 ofpbuf_delete(pi
->reply
);
865 port_iterator_fetch_port_desc(pi
);
869 struct ofputil_switch_features features
;
870 enum ofperr error
= ofputil_pull_switch_features(pi
->reply
, &features
);
872 ovs_fatal(0, "%s: failed to decode features reply (%s)",
873 vconn_get_name(pi
->vconn
), ofperr_to_string(error
));
877 /* Initializes 'pi' to prepare for iterating through all of the ports on the
878 * OpenFlow switch to which 'vconn' is connected.
880 * During iteration, the client should not make other use of 'vconn', because
881 * that can cause other messages to be interleaved with the replies used by the
882 * iterator and thus some ports may be missed or a hang can occur. */
884 port_iterator_init(struct port_iterator
*pi
, struct vconn
*vconn
)
886 memset(pi
, 0, sizeof *pi
);
888 if (vconn_get_version(vconn
) < OFP13_VERSION
) {
889 port_iterator_fetch_features(pi
);
891 port_iterator_fetch_port_desc(pi
);
895 /* Obtains the next port from 'pi'. On success, initializes '*pp' with the
896 * port's details and returns true, otherwise (if all the ports have already
897 * been seen), returns false. */
899 port_iterator_next(struct port_iterator
*pi
, struct ofputil_phy_port
*pp
)
903 int retval
= ofputil_pull_phy_port(vconn_get_version(pi
->vconn
),
907 } else if (retval
!= EOF
) {
908 ovs_fatal(0, "received bad reply: %s",
909 ofp_to_string(pi
->reply
->data
, pi
->reply
->size
,
914 if (pi
->variant
== PI_FEATURES
|| !pi
->more
) {
920 ofpbuf_delete(pi
->reply
);
921 run(vconn_recv_block(pi
->vconn
, &pi
->reply
),
922 "OpenFlow receive failed");
923 recv_xid
= ((struct ofp_header
*) pi
->reply
->data
)->xid
;
924 } while (pi
->send_xid
!= recv_xid
);
926 struct ofp_header
*oh
= pi
->reply
->data
;
928 if (ofptype_pull(&type
, pi
->reply
)
929 || type
!= OFPTYPE_PORT_DESC_STATS_REPLY
) {
930 ovs_fatal(0, "received bad reply: %s",
931 ofp_to_string(pi
->reply
->data
, pi
->reply
->size
,
935 pi
->more
= (ofpmp_flags(oh
) & OFPSF_REPLY_MORE
) != 0;
939 /* Destroys iterator 'pi'. */
941 port_iterator_destroy(struct port_iterator
*pi
)
944 while (pi
->variant
== PI_PORT_DESC
&& pi
->more
) {
945 /* Drain vconn's queue of any other replies for this request. */
946 struct ofputil_phy_port pp
;
947 port_iterator_next(pi
, &pp
);
950 ofpbuf_delete(pi
->reply
);
954 /* Opens a connection to 'vconn_name', fetches the port structure for
955 * 'port_name' (which may be a port name or number), and copies it into
958 fetch_ofputil_phy_port(const char *vconn_name
, const char *port_name
,
959 struct ofputil_phy_port
*pp
)
965 /* Try to interpret the argument as a port number. */
966 if (!str_to_ofp(port_name
, &port_no
)) {
970 /* OpenFlow 1.0, 1.1, and 1.2 put the list of ports in the
971 * OFPT_FEATURES_REPLY message. OpenFlow 1.3 and later versions put it
972 * into the OFPST_PORT_DESC reply. Try it the correct way. */
973 open_vconn(vconn_name
, &vconn
);
974 struct port_iterator pi
;
975 for (port_iterator_init(&pi
, vconn
); port_iterator_next(&pi
, pp
); ) {
976 if (port_no
!= OFPP_NONE
977 ? port_no
== pp
->port_no
978 : !strcmp(pp
->name
, port_name
)) {
983 port_iterator_destroy(&pi
);
987 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name
, port_name
);
991 /* Returns the port number corresponding to 'port_name' (which may be a port
992 * name or number) within the switch 'vconn_name'. */
994 str_to_port_no(const char *vconn_name
, const char *port_name
)
998 if (ofputil_port_from_string(port_name
, &port_no
)) {
1001 struct ofputil_phy_port pp
;
1003 fetch_ofputil_phy_port(vconn_name
, port_name
, &pp
);
1009 try_set_protocol(struct vconn
*vconn
, enum ofputil_protocol want
,
1010 enum ofputil_protocol
*cur
)
1013 struct ofpbuf
*request
, *reply
;
1014 enum ofputil_protocol next
;
1016 request
= ofputil_encode_set_protocol(*cur
, want
, &next
);
1018 return *cur
== want
;
1021 run(vconn_transact_noreply(vconn
, request
, &reply
),
1022 "talking to %s", vconn_get_name(vconn
));
1024 char *s
= ofp_to_string(reply
->data
, reply
->size
, 2);
1025 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
1026 vconn_get_name(vconn
), s
);
1028 ofpbuf_delete(reply
);
1036 static enum ofputil_protocol
1037 set_protocol_for_flow_dump(struct vconn
*vconn
,
1038 enum ofputil_protocol cur_protocol
,
1039 enum ofputil_protocol usable_protocols
)
1044 for (i
= 0; i
< ofputil_n_flow_dump_protocols
; i
++) {
1045 enum ofputil_protocol f
= ofputil_flow_dump_protocols
[i
];
1046 if (f
& usable_protocols
& allowed_protocols
1047 && try_set_protocol(vconn
, f
, &cur_protocol
)) {
1052 usable_s
= ofputil_protocols_to_string(usable_protocols
);
1053 if (usable_protocols
& allowed_protocols
) {
1054 ovs_fatal(0, "switch does not support any of the usable flow "
1055 "formats (%s)", usable_s
);
1057 char *allowed_s
= ofputil_protocols_to_string(allowed_protocols
);
1058 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1059 "allowed flow formats (%s)", usable_s
, allowed_s
);
1063 static struct vconn
*
1064 prepare_dump_flows(int argc
, char *argv
[], bool aggregate
,
1065 struct ofpbuf
**requestp
)
1067 enum ofputil_protocol usable_protocols
, protocol
;
1068 struct ofputil_flow_stats_request fsr
;
1069 struct vconn
*vconn
;
1072 error
= parse_ofp_flow_stats_request_str(&fsr
, aggregate
,
1073 argc
> 2 ? argv
[2] : "",
1076 ovs_fatal(0, "%s", error
);
1079 protocol
= open_vconn(argv
[1], &vconn
);
1080 protocol
= set_protocol_for_flow_dump(vconn
, protocol
, usable_protocols
);
1081 *requestp
= ofputil_encode_flow_stats_request(&fsr
, protocol
);
1086 ofctl_dump_flows__(int argc
, char *argv
[], bool aggregate
)
1088 struct ofpbuf
*request
;
1089 struct vconn
*vconn
;
1091 vconn
= prepare_dump_flows(argc
, argv
, aggregate
, &request
);
1092 dump_transaction(vconn
, request
);
1097 compare_flows(const void *afs_
, const void *bfs_
)
1099 const struct ofputil_flow_stats
*afs
= afs_
;
1100 const struct ofputil_flow_stats
*bfs
= bfs_
;
1101 const struct match
*a
= &afs
->match
;
1102 const struct match
*b
= &bfs
->match
;
1103 const struct sort_criterion
*sc
;
1105 for (sc
= criteria
; sc
< &criteria
[n_criteria
]; sc
++) {
1106 const struct mf_field
*f
= sc
->field
;
1110 int a_pri
= afs
->priority
;
1111 int b_pri
= bfs
->priority
;
1112 ret
= a_pri
< b_pri
? -1 : a_pri
> b_pri
;
1116 ina
= mf_are_prereqs_ok(f
, &a
->flow
) && !mf_is_all_wild(f
, &a
->wc
);
1117 inb
= mf_are_prereqs_ok(f
, &b
->flow
) && !mf_is_all_wild(f
, &b
->wc
);
1119 /* Skip the test for sc->order, so that missing fields always
1120 * sort to the end whether we're sorting in ascending or
1121 * descending order. */
1122 return ina
? -1 : 1;
1124 union mf_value aval
, bval
;
1126 mf_get_value(f
, &a
->flow
, &aval
);
1127 mf_get_value(f
, &b
->flow
, &bval
);
1128 ret
= memcmp(&aval
, &bval
, f
->n_bytes
);
1133 return sc
->order
== SORT_ASC
? ret
: -ret
;
1141 ofctl_dump_flows(struct ovs_cmdl_context
*ctx
)
1144 ofctl_dump_flows__(ctx
->argc
, ctx
->argv
, false);
1147 struct ofputil_flow_stats
*fses
;
1148 size_t n_fses
, allocated_fses
;
1149 struct ofpbuf
*request
;
1150 struct ofpbuf ofpacts
;
1151 struct ofpbuf
*reply
;
1152 struct vconn
*vconn
;
1157 vconn
= prepare_dump_flows(ctx
->argc
, ctx
->argv
, false, &request
);
1158 send_xid
= ((struct ofp_header
*) request
->data
)->xid
;
1159 send_openflow_buffer(vconn
, request
);
1162 n_fses
= allocated_fses
= 0;
1164 ofpbuf_init(&ofpacts
, 0);
1166 struct ofputil_flow_stats
*fs
;
1168 if (n_fses
>= allocated_fses
) {
1169 fses
= x2nrealloc(fses
, &allocated_fses
, sizeof *fses
);
1173 if (!recv_flow_stats_reply(vconn
, send_xid
, &reply
, fs
,
1177 fs
->ofpacts
= xmemdup(fs
->ofpacts
, fs
->ofpacts_len
);
1180 ofpbuf_uninit(&ofpacts
);
1182 qsort(fses
, n_fses
, sizeof *fses
, compare_flows
);
1185 for (i
= 0; i
< n_fses
; i
++) {
1187 ofp_print_flow_stats(&s
, &fses
[i
]);
1192 for (i
= 0; i
< n_fses
; i
++) {
1193 free(CONST_CAST(struct ofpact
*, fses
[i
].ofpacts
));
1202 ofctl_dump_aggregate(struct ovs_cmdl_context
*ctx
)
1204 ofctl_dump_flows__(ctx
->argc
, ctx
->argv
, true);
1208 ofctl_queue_stats(struct ovs_cmdl_context
*ctx
)
1210 struct ofpbuf
*request
;
1211 struct vconn
*vconn
;
1212 struct ofputil_queue_stats_request oqs
;
1214 open_vconn(ctx
->argv
[1], &vconn
);
1216 if (ctx
->argc
> 2 && ctx
->argv
[2][0] && strcasecmp(ctx
->argv
[2], "all")) {
1217 oqs
.port_no
= str_to_port_no(ctx
->argv
[1], ctx
->argv
[2]);
1219 oqs
.port_no
= OFPP_ANY
;
1221 if (ctx
->argc
> 3 && ctx
->argv
[3][0] && strcasecmp(ctx
->argv
[3], "all")) {
1222 oqs
.queue_id
= atoi(ctx
->argv
[3]);
1224 oqs
.queue_id
= OFPQ_ALL
;
1227 request
= ofputil_encode_queue_stats_request(vconn_get_version(vconn
), &oqs
);
1228 dump_transaction(vconn
, request
);
1233 ofctl_queue_get_config(struct ovs_cmdl_context
*ctx
)
1235 const char *vconn_name
= ctx
->argv
[1];
1236 const char *port_name
= ctx
->argc
> 2 ? ctx
->argv
[2] : "any";
1237 ofp_port_t port
= str_to_port_no(vconn_name
, port_name
);
1238 const char *queue_name
= ctx
->argc
> 3 ? ctx
->argv
[3] : "all";
1239 uint32_t queue
= (!strcasecmp(queue_name
, "all")
1241 : atoi(queue_name
));
1242 struct vconn
*vconn
;
1244 enum ofputil_protocol protocol
= open_vconn(vconn_name
, &vconn
);
1245 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
1246 if (port
== OFPP_ANY
&& version
== OFP10_VERSION
) {
1247 /* The user requested all queues on all ports. OpenFlow 1.0 only
1248 * supports getting queues for an individual port, so to implement the
1249 * user's request we have to get a list of all the ports.
1251 * We use a second vconn to avoid having to accumulate a list of all of
1253 struct vconn
*vconn2
;
1254 enum ofputil_protocol protocol2
= open_vconn(vconn_name
, &vconn2
);
1255 enum ofp_version version2
= ofputil_protocol_to_ofp_version(protocol2
);
1257 struct port_iterator pi
;
1258 struct ofputil_phy_port pp
;
1259 for (port_iterator_init(&pi
, vconn
); port_iterator_next(&pi
, &pp
); ) {
1260 if (ofp_to_u16(pp
.port_no
) < ofp_to_u16(OFPP_MAX
)) {
1261 dump_transaction(vconn2
,
1262 ofputil_encode_queue_get_config_request(
1263 version2
, pp
.port_no
, queue
));
1266 port_iterator_destroy(&pi
);
1267 vconn_close(vconn2
);
1269 dump_transaction(vconn
, ofputil_encode_queue_get_config_request(
1270 version
, port
, queue
));
1275 static enum ofputil_protocol
1276 open_vconn_for_flow_mod(const char *remote
, struct vconn
**vconnp
,
1277 enum ofputil_protocol usable_protocols
)
1279 enum ofputil_protocol cur_protocol
;
1283 if (!(usable_protocols
& allowed_protocols
)) {
1284 char *allowed_s
= ofputil_protocols_to_string(allowed_protocols
);
1285 usable_s
= ofputil_protocols_to_string(usable_protocols
);
1286 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1287 "allowed flow formats (%s)", usable_s
, allowed_s
);
1290 /* If the initial flow format is allowed and usable, keep it. */
1291 cur_protocol
= open_vconn(remote
, vconnp
);
1292 if (usable_protocols
& allowed_protocols
& cur_protocol
) {
1293 return cur_protocol
;
1296 /* Otherwise try each flow format in turn. */
1297 for (i
= 0; i
< sizeof(enum ofputil_protocol
) * CHAR_BIT
; i
++) {
1298 enum ofputil_protocol f
= 1 << i
;
1300 if (f
!= cur_protocol
1301 && f
& usable_protocols
& allowed_protocols
1302 && try_set_protocol(*vconnp
, f
, &cur_protocol
)) {
1307 usable_s
= ofputil_protocols_to_string(usable_protocols
);
1308 ovs_fatal(0, "switch does not support any of the usable flow "
1309 "formats (%s)", usable_s
);
1313 bundle_flow_mod__(const char *remote
, struct ofputil_flow_mod
*fms
,
1314 size_t n_fms
, enum ofputil_protocol usable_protocols
)
1316 enum ofputil_protocol protocol
;
1317 struct vconn
*vconn
;
1318 struct ovs_list requests
;
1321 list_init(&requests
);
1323 /* Bundles need OpenFlow 1.3+. */
1324 usable_protocols
&= OFPUTIL_P_OF13_UP
;
1325 protocol
= open_vconn_for_flow_mod(remote
, &vconn
, usable_protocols
);
1327 for (i
= 0; i
< n_fms
; i
++) {
1328 struct ofputil_flow_mod
*fm
= &fms
[i
];
1329 struct ofpbuf
*request
= ofputil_encode_flow_mod(fm
, protocol
);
1331 list_push_back(&requests
, &request
->list_node
);
1332 free(CONST_CAST(struct ofpact
*, fm
->ofpacts
));
1335 bundle_transact(vconn
, &requests
, OFPBF_ORDERED
| OFPBF_ATOMIC
);
1336 ofpbuf_list_delete(&requests
);
1341 ofctl_flow_mod__(const char *remote
, struct ofputil_flow_mod
*fms
,
1342 size_t n_fms
, enum ofputil_protocol usable_protocols
)
1344 enum ofputil_protocol protocol
;
1345 struct vconn
*vconn
;
1349 bundle_flow_mod__(remote
, fms
, n_fms
, usable_protocols
);
1353 protocol
= open_vconn_for_flow_mod(remote
, &vconn
, usable_protocols
);
1355 for (i
= 0; i
< n_fms
; i
++) {
1356 struct ofputil_flow_mod
*fm
= &fms
[i
];
1358 transact_noreply(vconn
, ofputil_encode_flow_mod(fm
, protocol
));
1359 free(CONST_CAST(struct ofpact
*, fm
->ofpacts
));
1365 ofctl_flow_mod_file(int argc OVS_UNUSED
, char *argv
[], int command
)
1367 enum ofputil_protocol usable_protocols
;
1368 struct ofputil_flow_mod
*fms
= NULL
;
1372 if (command
== OFPFC_ADD
) {
1373 /* Allow the file to specify a mix of commands. If none specified at
1374 * the beginning of any given line, then the default is OFPFC_ADD, so
1375 * this is backwards compatible. */
1378 error
= parse_ofp_flow_mod_file(argv
[2], command
, &fms
, &n_fms
,
1381 ovs_fatal(0, "%s", error
);
1383 ofctl_flow_mod__(argv
[1], fms
, n_fms
, usable_protocols
);
1388 ofctl_flow_mod(int argc
, char *argv
[], uint16_t command
)
1390 if (argc
> 2 && !strcmp(argv
[2], "-")) {
1391 ofctl_flow_mod_file(argc
, argv
, command
);
1393 struct ofputil_flow_mod fm
;
1395 enum ofputil_protocol usable_protocols
;
1397 error
= parse_ofp_flow_mod_str(&fm
, argc
> 2 ? argv
[2] : "", command
,
1400 ovs_fatal(0, "%s", error
);
1402 ofctl_flow_mod__(argv
[1], &fm
, 1, usable_protocols
);
1407 ofctl_add_flow(struct ovs_cmdl_context
*ctx
)
1409 ofctl_flow_mod(ctx
->argc
, ctx
->argv
, OFPFC_ADD
);
1413 ofctl_add_flows(struct ovs_cmdl_context
*ctx
)
1415 ofctl_flow_mod_file(ctx
->argc
, ctx
->argv
, OFPFC_ADD
);
1419 ofctl_mod_flows(struct ovs_cmdl_context
*ctx
)
1421 ofctl_flow_mod(ctx
->argc
, ctx
->argv
, strict
? OFPFC_MODIFY_STRICT
: OFPFC_MODIFY
);
1425 ofctl_del_flows(struct ovs_cmdl_context
*ctx
)
1427 ofctl_flow_mod(ctx
->argc
, ctx
->argv
, strict
? OFPFC_DELETE_STRICT
: OFPFC_DELETE
);
1431 set_packet_in_format(struct vconn
*vconn
,
1432 enum nx_packet_in_format packet_in_format
,
1435 struct ofpbuf
*spif
;
1437 spif
= ofputil_make_set_packet_in_format(vconn_get_version(vconn
),
1440 transact_noreply(vconn
, spif
);
1442 struct ofpbuf
*reply
;
1444 run(vconn_transact_noreply(vconn
, spif
, &reply
),
1445 "talking to %s", vconn_get_name(vconn
));
1447 char *s
= ofp_to_string(reply
->data
, reply
->size
, 2);
1448 VLOG_DBG("%s: failed to set packet in format to nx_packet_in, "
1449 "controller replied: %s.",
1450 vconn_get_name(vconn
), s
);
1452 ofpbuf_delete(reply
);
1456 VLOG_DBG("%s: using user-specified packet in format %s",
1457 vconn_get_name(vconn
),
1458 ofputil_packet_in_format_to_string(packet_in_format
));
1465 monitor_set_invalid_ttl_to_controller(struct vconn
*vconn
)
1467 struct ofputil_switch_config config
;
1469 fetch_switch_config(vconn
, &config
);
1470 if (!config
.invalid_ttl_to_controller
) {
1471 config
.invalid_ttl_to_controller
= 1;
1472 set_switch_config(vconn
, &config
);
1474 /* Then retrieve the configuration to see if it really took. OpenFlow
1475 * has ill-defined error reporting for bad flags, so this is about the
1476 * best we can do. */
1477 fetch_switch_config(vconn
, &config
);
1478 if (!config
.invalid_ttl_to_controller
) {
1479 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1480 "switch probably doesn't support this flag)");
1486 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1487 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1488 * an error message and stores NULL in '*msgp'. */
1490 openflow_from_hex(const char *hex
, struct ofpbuf
**msgp
)
1492 struct ofp_header
*oh
;
1495 msg
= ofpbuf_new(strlen(hex
) / 2);
1498 if (ofpbuf_put_hex(msg
, hex
, NULL
)[0] != '\0') {
1500 return "Trailing garbage in hex data";
1503 if (msg
->size
< sizeof(struct ofp_header
)) {
1505 return "Message too short for OpenFlow";
1509 if (msg
->size
!= ntohs(oh
->length
)) {
1511 return "Message size does not match length in OpenFlow header";
1519 ofctl_send(struct unixctl_conn
*conn
, int argc
,
1520 const char *argv
[], void *vconn_
)
1522 struct vconn
*vconn
= vconn_
;
1529 for (i
= 1; i
< argc
; i
++) {
1530 const char *error_msg
;
1534 error_msg
= openflow_from_hex(argv
[i
], &msg
);
1536 ds_put_format(&reply
, "%s\n", error_msg
);
1541 fprintf(stderr
, "send: ");
1542 ofp_print(stderr
, msg
->data
, msg
->size
, verbosity
);
1544 error
= vconn_send_block(vconn
, msg
);
1547 ds_put_format(&reply
, "%s\n", ovs_strerror(error
));
1550 ds_put_cstr(&reply
, "sent\n");
1555 unixctl_command_reply(conn
, ds_cstr(&reply
));
1557 unixctl_command_reply_error(conn
, ds_cstr(&reply
));
1562 struct barrier_aux
{
1563 struct vconn
*vconn
; /* OpenFlow connection for sending barrier. */
1564 struct unixctl_conn
*conn
; /* Connection waiting for barrier response. */
1568 ofctl_barrier(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1569 const char *argv
[] OVS_UNUSED
, void *aux_
)
1571 struct barrier_aux
*aux
= aux_
;
1576 unixctl_command_reply_error(conn
, "already waiting for barrier reply");
1580 msg
= ofputil_encode_barrier_request(vconn_get_version(aux
->vconn
));
1581 error
= vconn_send_block(aux
->vconn
, msg
);
1584 unixctl_command_reply_error(conn
, ovs_strerror(error
));
1591 ofctl_set_output_file(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1592 const char *argv
[], void *aux OVS_UNUSED
)
1596 fd
= open(argv
[1], O_CREAT
| O_TRUNC
| O_WRONLY
, 0666);
1598 unixctl_command_reply_error(conn
, ovs_strerror(errno
));
1603 dup2(fd
, STDERR_FILENO
);
1605 unixctl_command_reply(conn
, NULL
);
1609 ofctl_block(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1610 const char *argv
[] OVS_UNUSED
, void *blocked_
)
1612 bool *blocked
= blocked_
;
1616 unixctl_command_reply(conn
, NULL
);
1618 unixctl_command_reply(conn
, "already blocking");
1623 ofctl_unblock(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1624 const char *argv
[] OVS_UNUSED
, void *blocked_
)
1626 bool *blocked
= blocked_
;
1630 unixctl_command_reply(conn
, NULL
);
1632 unixctl_command_reply(conn
, "already unblocked");
1636 /* Prints to stdout all of the messages received on 'vconn'.
1638 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1639 * received on 'vconn'.
1641 * If 'resume_continuations' is true, sends an NXT_RESUME in reply to any
1642 * NXT_PACKET_IN2 that includes a continuation. */
1644 monitor_vconn(struct vconn
*vconn
, bool reply_to_echo_requests
,
1645 bool resume_continuations
)
1647 struct barrier_aux barrier_aux
= { vconn
, NULL
};
1648 struct unixctl_server
*server
;
1649 bool exiting
= false;
1650 bool blocked
= false;
1653 daemon_save_fd(STDERR_FILENO
);
1654 daemonize_start(false);
1655 error
= unixctl_server_create(unixctl_path
, &server
);
1657 ovs_fatal(error
, "failed to create unixctl server");
1659 unixctl_command_register("exit", "", 0, 0, ofctl_exit
, &exiting
);
1660 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX
,
1662 unixctl_command_register("ofctl/barrier", "", 0, 0,
1663 ofctl_barrier
, &barrier_aux
);
1664 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1665 ofctl_set_output_file
, NULL
);
1667 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block
, &blocked
);
1668 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock
,
1671 daemonize_complete();
1673 enum ofp_version version
= vconn_get_version(vconn
);
1674 enum ofputil_protocol protocol
1675 = ofputil_protocol_from_ofp_version(version
);
1681 unixctl_server_run(server
);
1686 retval
= vconn_recv(vconn
, &b
);
1687 if (retval
== EAGAIN
) {
1690 run(retval
, "vconn_recv");
1693 char *s
= xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1694 time_wall_msec(), true);
1699 ofptype_decode(&type
, b
->data
);
1700 ofp_print(stderr
, b
->data
, b
->size
, verbosity
+ 2);
1703 switch ((int) type
) {
1704 case OFPTYPE_BARRIER_REPLY
:
1705 if (barrier_aux
.conn
) {
1706 unixctl_command_reply(barrier_aux
.conn
, NULL
);
1707 barrier_aux
.conn
= NULL
;
1711 case OFPTYPE_ECHO_REQUEST
:
1712 if (reply_to_echo_requests
) {
1713 struct ofpbuf
*reply
;
1715 reply
= make_echo_reply(b
->data
);
1716 retval
= vconn_send_block(vconn
, reply
);
1718 ovs_fatal(retval
, "failed to send echo reply");
1723 case OFPTYPE_PACKET_IN
:
1724 if (resume_continuations
) {
1725 struct ofputil_packet_in pin
;
1726 struct ofpbuf continuation
;
1728 error
= ofputil_decode_packet_in(b
->data
, true, &pin
,
1732 fprintf(stderr
, "decoding packet-in failed: %s",
1733 ofperr_to_string(error
));
1734 } else if (continuation
.size
) {
1735 struct ofpbuf
*reply
;
1737 reply
= ofputil_encode_resume(&pin
, &continuation
,
1740 fprintf(stderr
, "send: ");
1741 ofp_print(stderr
, reply
->data
, reply
->size
,
1745 retval
= vconn_send_block(vconn
, reply
);
1747 ovs_fatal(retval
, "failed to send NXT_RESUME");
1761 vconn_run_wait(vconn
);
1763 vconn_recv_wait(vconn
);
1765 unixctl_server_wait(server
);
1769 unixctl_server_destroy(server
);
1773 ofctl_monitor(struct ovs_cmdl_context
*ctx
)
1775 struct vconn
*vconn
;
1777 enum ofputil_protocol usable_protocols
;
1779 /* If the user wants the invalid_ttl_to_controller feature, limit the
1780 * OpenFlow versions to those that support that feature. (Support in
1781 * OpenFlow 1.0 is an Open vSwitch extension.) */
1782 for (i
= 2; i
< ctx
->argc
; i
++) {
1783 if (!strcmp(ctx
->argv
[i
], "invalid_ttl")) {
1784 uint32_t usable_versions
= ((1u << OFP10_VERSION
) |
1785 (1u << OFP11_VERSION
) |
1786 (1u << OFP12_VERSION
));
1787 uint32_t allowed_versions
= get_allowed_ofp_versions();
1788 if (!(allowed_versions
& usable_versions
)) {
1789 struct ds versions
= DS_EMPTY_INITIALIZER
;
1790 ofputil_format_version_bitmap_names(&versions
,
1792 ovs_fatal(0, "invalid_ttl requires one of the OpenFlow "
1793 "versions %s but none is enabled (use -O)",
1794 ds_cstr(&versions
));
1796 mask_allowed_ofp_versions(usable_versions
);
1801 open_vconn(ctx
->argv
[1], &vconn
);
1802 bool resume_continuations
= false;
1803 for (i
= 2; i
< ctx
->argc
; i
++) {
1804 const char *arg
= ctx
->argv
[i
];
1806 if (isdigit((unsigned char) *arg
)) {
1807 struct ofputil_switch_config config
;
1809 fetch_switch_config(vconn
, &config
);
1810 config
.miss_send_len
= atoi(arg
);
1811 set_switch_config(vconn
, &config
);
1812 } else if (!strcmp(arg
, "invalid_ttl")) {
1813 monitor_set_invalid_ttl_to_controller(vconn
);
1814 } else if (!strncmp(arg
, "watch:", 6)) {
1815 struct ofputil_flow_monitor_request fmr
;
1819 error
= parse_flow_monitor_request(&fmr
, arg
+ 6,
1822 ovs_fatal(0, "%s", error
);
1825 msg
= ofpbuf_new(0);
1826 ofputil_append_flow_monitor_request(&fmr
, msg
);
1827 dump_transaction(vconn
, msg
);
1829 } else if (!strcmp(arg
, "resume")) {
1830 /* This option is intentionally undocumented because it is meant
1831 * only for testing. */
1832 resume_continuations
= true;
1834 /* Set miss_send_len to ensure that we get packet-ins. */
1835 struct ofputil_switch_config config
;
1836 fetch_switch_config(vconn
, &config
);
1837 config
.miss_send_len
= UINT16_MAX
;
1838 set_switch_config(vconn
, &config
);
1840 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg
);
1844 if (preferred_packet_in_format
>= 0) {
1845 /* A particular packet-in format was requested, so we must set it. */
1846 set_packet_in_format(vconn
, preferred_packet_in_format
, true);
1848 /* Otherwise, we always prefer NXT_PACKET_IN2. */
1849 if (!set_packet_in_format(vconn
, NXPIF_NXT_PACKET_IN2
, false)) {
1850 /* We can't get NXT_PACKET_IN2. For OpenFlow 1.0 only, request
1851 * NXT_PACKET_IN. (Before 2.6, Open vSwitch will accept a request
1852 * for NXT_PACKET_IN with OF1.1+, but even after that it still
1853 * sends packet-ins in the OpenFlow native format.) */
1854 if (vconn_get_version(vconn
) == OFP10_VERSION
) {
1855 set_packet_in_format(vconn
, NXPIF_NXT_PACKET_IN
, false);
1860 monitor_vconn(vconn
, true, resume_continuations
);
1864 ofctl_snoop(struct ovs_cmdl_context
*ctx
)
1866 struct vconn
*vconn
;
1868 open_vconn__(ctx
->argv
[1], SNOOP
, &vconn
);
1869 monitor_vconn(vconn
, false, false);
1873 ofctl_dump_ports(struct ovs_cmdl_context
*ctx
)
1875 struct ofpbuf
*request
;
1876 struct vconn
*vconn
;
1879 open_vconn(ctx
->argv
[1], &vconn
);
1880 port
= ctx
->argc
> 2 ? str_to_port_no(ctx
->argv
[1], ctx
->argv
[2]) : OFPP_ANY
;
1881 request
= ofputil_encode_dump_ports_request(vconn_get_version(vconn
), port
);
1882 dump_transaction(vconn
, request
);
1887 ofctl_dump_ports_desc(struct ovs_cmdl_context
*ctx
)
1889 struct ofpbuf
*request
;
1890 struct vconn
*vconn
;
1893 open_vconn(ctx
->argv
[1], &vconn
);
1894 port
= ctx
->argc
> 2 ? str_to_port_no(ctx
->argv
[1], ctx
->argv
[2]) : OFPP_ANY
;
1895 request
= ofputil_encode_port_desc_stats_request(vconn_get_version(vconn
),
1897 dump_transaction(vconn
, request
);
1902 ofctl_probe(struct ovs_cmdl_context
*ctx
)
1904 struct ofpbuf
*request
;
1905 struct vconn
*vconn
;
1906 struct ofpbuf
*reply
;
1908 open_vconn(ctx
->argv
[1], &vconn
);
1909 request
= make_echo_request(vconn_get_version(vconn
));
1910 run(vconn_transact(vconn
, request
, &reply
), "talking to %s", ctx
->argv
[1]);
1911 if (reply
->size
!= sizeof(struct ofp_header
)) {
1912 ovs_fatal(0, "reply does not match request");
1914 ofpbuf_delete(reply
);
1919 ofctl_packet_out(struct ovs_cmdl_context
*ctx
)
1921 enum ofputil_protocol protocol
;
1922 struct ofputil_packet_out po
;
1923 struct ofpbuf ofpacts
;
1924 struct vconn
*vconn
;
1927 enum ofputil_protocol usable_protocols
; /* XXX: Use in proto selection */
1929 ofpbuf_init(&ofpacts
, 64);
1930 error
= ofpacts_parse_actions(ctx
->argv
[3], &ofpacts
, &usable_protocols
);
1932 ovs_fatal(0, "%s", error
);
1935 po
.buffer_id
= UINT32_MAX
;
1936 po
.in_port
= str_to_port_no(ctx
->argv
[1], ctx
->argv
[2]);
1937 po
.ofpacts
= ofpacts
.data
;
1938 po
.ofpacts_len
= ofpacts
.size
;
1940 protocol
= open_vconn(ctx
->argv
[1], &vconn
);
1941 for (i
= 4; i
< ctx
->argc
; i
++) {
1942 struct dp_packet
*packet
;
1944 const char *error_msg
;
1946 error_msg
= eth_from_hex(ctx
->argv
[i
], &packet
);
1948 ovs_fatal(0, "%s", error_msg
);
1951 po
.packet
= dp_packet_data(packet
);
1952 po
.packet_len
= dp_packet_size(packet
);
1953 opo
= ofputil_encode_packet_out(&po
, protocol
);
1954 transact_noreply(vconn
, opo
);
1955 dp_packet_delete(packet
);
1958 ofpbuf_uninit(&ofpacts
);
1962 ofctl_mod_port(struct ovs_cmdl_context
*ctx
)
1964 struct ofp_config_flag
{
1965 const char *name
; /* The flag's name. */
1966 enum ofputil_port_config bit
; /* Bit to turn on or off. */
1967 bool on
; /* Value to set the bit to. */
1969 static const struct ofp_config_flag flags
[] = {
1970 { "up", OFPUTIL_PC_PORT_DOWN
, false },
1971 { "down", OFPUTIL_PC_PORT_DOWN
, true },
1972 { "stp", OFPUTIL_PC_NO_STP
, false },
1973 { "receive", OFPUTIL_PC_NO_RECV
, false },
1974 { "receive-stp", OFPUTIL_PC_NO_RECV_STP
, false },
1975 { "flood", OFPUTIL_PC_NO_FLOOD
, false },
1976 { "forward", OFPUTIL_PC_NO_FWD
, false },
1977 { "packet-in", OFPUTIL_PC_NO_PACKET_IN
, false },
1980 const struct ofp_config_flag
*flag
;
1981 enum ofputil_protocol protocol
;
1982 struct ofputil_port_mod pm
;
1983 struct ofputil_phy_port pp
;
1984 struct vconn
*vconn
;
1985 const char *command
;
1988 fetch_ofputil_phy_port(ctx
->argv
[1], ctx
->argv
[2], &pp
);
1990 pm
.port_no
= pp
.port_no
;
1991 pm
.hw_addr
= pp
.hw_addr
;
1996 if (!strncasecmp(ctx
->argv
[3], "no-", 3)) {
1997 command
= ctx
->argv
[3] + 3;
1999 } else if (!strncasecmp(ctx
->argv
[3], "no", 2)) {
2000 command
= ctx
->argv
[3] + 2;
2003 command
= ctx
->argv
[3];
2006 for (flag
= flags
; flag
< &flags
[ARRAY_SIZE(flags
)]; flag
++) {
2007 if (!strcasecmp(command
, flag
->name
)) {
2008 pm
.mask
= flag
->bit
;
2009 pm
.config
= flag
->on
^ not ? flag
->bit
: 0;
2013 ovs_fatal(0, "unknown mod-port command '%s'", ctx
->argv
[3]);
2016 protocol
= open_vconn(ctx
->argv
[1], &vconn
);
2017 transact_noreply(vconn
, ofputil_encode_port_mod(&pm
, protocol
));
2021 /* This function uses OFPMP14_TABLE_DESC request to get the current
2022 * table configuration from switch. The function then modifies
2023 * only that table-config property, which has been requested. */
2025 fetch_table_desc(struct vconn
*vconn
, struct ofputil_table_mod
*tm
,
2026 struct ofputil_table_desc
*td
)
2028 struct ofpbuf
*request
;
2033 request
= ofputil_encode_table_desc_request(vconn_get_version(vconn
));
2034 send_xid
= ((struct ofp_header
*) request
->data
)->xid
;
2035 send_openflow_buffer(vconn
, request
);
2038 struct ofpbuf
*reply
;
2040 run(vconn_recv_block(vconn
, &reply
), "OpenFlow packet receive failed");
2041 recv_xid
= ((struct ofp_header
*) reply
->data
)->xid
;
2042 if (send_xid
== recv_xid
) {
2043 struct ofp_header
*oh
= reply
->data
;
2044 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
2047 if (ofptype_pull(&type
, &b
)
2048 || type
!= OFPTYPE_TABLE_DESC_REPLY
) {
2049 ovs_fatal(0, "received bad reply: %s",
2050 ofp_to_string(reply
->data
, reply
->size
,
2053 uint16_t flags
= ofpmp_flags(oh
);
2054 done
= !(flags
& OFPSF_REPLY_MORE
);
2056 /* We've already found the table desc consisting of current
2057 * table configuration, but we need to drain the queue of
2058 * any other replies for this request. */
2061 while (!ofputil_decode_table_desc(&b
, td
, oh
->version
)) {
2062 if (td
->table_id
== tm
->table_id
) {
2068 VLOG_DBG("received reply with xid %08"PRIx32
" "
2069 "!= expected %08"PRIx32
, recv_xid
, send_xid
);
2071 ofpbuf_delete(reply
);
2073 if (tm
->eviction
!= OFPUTIL_TABLE_EVICTION_DEFAULT
) {
2074 tm
->vacancy
= td
->vacancy
;
2075 tm
->table_vacancy
.vacancy_down
= td
->table_vacancy
.vacancy_down
;
2076 tm
->table_vacancy
.vacancy_up
= td
->table_vacancy
.vacancy_up
;
2077 } else if (tm
->vacancy
!= OFPUTIL_TABLE_VACANCY_DEFAULT
) {
2078 tm
->eviction
= td
->eviction
;
2079 tm
->eviction_flags
= td
->eviction_flags
;
2084 ofctl_mod_table(struct ovs_cmdl_context
*ctx
)
2086 uint32_t usable_versions
;
2087 struct ofputil_table_mod tm
;
2088 struct vconn
*vconn
;
2092 error
= parse_ofp_table_mod(&tm
, ctx
->argv
[2], ctx
->argv
[3],
2095 ovs_fatal(0, "%s", error
);
2098 uint32_t allowed_versions
= get_allowed_ofp_versions();
2099 if (!(allowed_versions
& usable_versions
)) {
2100 struct ds versions
= DS_EMPTY_INITIALIZER
;
2101 ofputil_format_version_bitmap_names(&versions
, usable_versions
);
2102 ovs_fatal(0, "table_mod '%s' requires one of the OpenFlow "
2104 ctx
->argv
[3], ds_cstr(&versions
));
2106 mask_allowed_ofp_versions(usable_versions
);
2107 enum ofputil_protocol protocol
= open_vconn(ctx
->argv
[1], &vconn
);
2109 /* For OpenFlow 1.4+, ovs-ofctl mod-table should not affect table-config
2110 * properties that the user didn't ask to change, so it is necessary to
2111 * restore the current configuration of table-config parameters using
2112 * OFPMP14_TABLE_DESC request. */
2113 if ((allowed_versions
& (1u << OFP14_VERSION
)) ||
2114 (allowed_versions
& (1u << OFP15_VERSION
))) {
2115 struct ofputil_table_desc td
;
2117 if (tm
.table_id
== OFPTT_ALL
) {
2118 for (i
= 0; i
< OFPTT_MAX
; i
++) {
2120 fetch_table_desc(vconn
, &tm
, &td
);
2121 transact_noreply(vconn
,
2122 ofputil_encode_table_mod(&tm
, protocol
));
2125 fetch_table_desc(vconn
, &tm
, &td
);
2126 transact_noreply(vconn
, ofputil_encode_table_mod(&tm
, protocol
));
2129 transact_noreply(vconn
, ofputil_encode_table_mod(&tm
, protocol
));
2135 ofctl_get_frags(struct ovs_cmdl_context
*ctx
)
2137 struct ofputil_switch_config config
;
2138 struct vconn
*vconn
;
2140 open_vconn(ctx
->argv
[1], &vconn
);
2141 fetch_switch_config(vconn
, &config
);
2142 puts(ofputil_frag_handling_to_string(config
.frag
));
2147 ofctl_set_frags(struct ovs_cmdl_context
*ctx
)
2149 struct ofputil_switch_config config
;
2150 enum ofputil_frag_handling frag
;
2151 struct vconn
*vconn
;
2153 if (!ofputil_frag_handling_from_string(ctx
->argv
[2], &frag
)) {
2154 ovs_fatal(0, "%s: unknown fragment handling mode", ctx
->argv
[2]);
2157 open_vconn(ctx
->argv
[1], &vconn
);
2158 fetch_switch_config(vconn
, &config
);
2159 if (frag
!= config
.frag
) {
2160 /* Set the configuration. */
2162 set_switch_config(vconn
, &config
);
2164 /* Then retrieve the configuration to see if it really took. OpenFlow
2165 * has ill-defined error reporting for bad flags, so this is about the
2166 * best we can do. */
2167 fetch_switch_config(vconn
, &config
);
2168 if (frag
!= config
.frag
) {
2169 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
2170 "switch probably doesn't support mode \"%s\")",
2171 ctx
->argv
[1], ofputil_frag_handling_to_string(frag
));
2178 ofctl_ofp_parse(struct ovs_cmdl_context
*ctx
)
2180 const char *filename
= ctx
->argv
[1];
2184 file
= !strcmp(filename
, "-") ? stdin
: fopen(filename
, "r");
2186 ovs_fatal(errno
, "%s: open", filename
);
2189 ofpbuf_init(&b
, 65536);
2191 struct ofp_header
*oh
;
2192 size_t length
, tail_len
;
2197 oh
= ofpbuf_put_uninit(&b
, sizeof *oh
);
2198 n
= fread(oh
, 1, sizeof *oh
, file
);
2201 } else if (n
< sizeof *oh
) {
2202 ovs_fatal(0, "%s: unexpected end of file mid-message", filename
);
2205 length
= ntohs(oh
->length
);
2206 if (length
< sizeof *oh
) {
2207 ovs_fatal(0, "%s: %"PRIuSIZE
"-byte message is too short for OpenFlow",
2211 tail_len
= length
- sizeof *oh
;
2212 tail
= ofpbuf_put_uninit(&b
, tail_len
);
2213 n
= fread(tail
, 1, tail_len
, file
);
2215 ovs_fatal(0, "%s: unexpected end of file mid-message", filename
);
2218 ofp_print(stdout
, b
.data
, b
.size
, verbosity
+ 2);
2222 if (file
!= stdin
) {
2228 is_openflow_port(ovs_be16 port_
, char *ports
[])
2230 uint16_t port
= ntohs(port_
);
2234 for (i
= 0; ports
[i
]; i
++) {
2235 if (port
== atoi(ports
[i
])) {
2241 return port
== OFP_PORT
|| port
== OFP_OLD_PORT
;
2246 ofctl_ofp_parse_pcap(struct ovs_cmdl_context
*ctx
)
2248 struct tcp_reader
*reader
;
2253 file
= ovs_pcap_open(ctx
->argv
[1], "rb");
2255 ovs_fatal(errno
, "%s: open failed", ctx
->argv
[1]);
2258 reader
= tcp_reader_open();
2261 struct dp_packet
*packet
;
2265 error
= ovs_pcap_read(file
, &packet
, &when
);
2269 pkt_metadata_init(&packet
->md
, ODPP_NONE
);
2270 flow_extract(packet
, &flow
);
2271 if (flow
.dl_type
== htons(ETH_TYPE_IP
)
2272 && flow
.nw_proto
== IPPROTO_TCP
2273 && (is_openflow_port(flow
.tp_src
, ctx
->argv
+ 2) ||
2274 is_openflow_port(flow
.tp_dst
, ctx
->argv
+ 2))) {
2275 struct dp_packet
*payload
= tcp_reader_run(reader
, &flow
, packet
);
2277 while (dp_packet_size(payload
) >= sizeof(struct ofp_header
)) {
2278 const struct ofp_header
*oh
;
2279 void *data
= dp_packet_data(payload
);
2282 /* Align OpenFlow on 8-byte boundary for safe access. */
2283 dp_packet_shift(payload
, -((intptr_t) data
& 7));
2285 oh
= dp_packet_data(payload
);
2286 length
= ntohs(oh
->length
);
2287 if (dp_packet_size(payload
) < length
) {
2297 char *s
= xastrftime_msec("%H:%M:%S.### ", when
, true);
2302 printf(IP_FMT
".%"PRIu16
" > "IP_FMT
".%"PRIu16
":\n",
2303 IP_ARGS(flow
.nw_src
), ntohs(flow
.tp_src
),
2304 IP_ARGS(flow
.nw_dst
), ntohs(flow
.tp_dst
));
2305 ofp_print(stdout
, dp_packet_data(payload
), length
, verbosity
+ 1);
2306 dp_packet_pull(payload
, length
);
2310 dp_packet_delete(packet
);
2312 tcp_reader_close(reader
);
2316 ofctl_ping(struct ovs_cmdl_context
*ctx
)
2318 size_t max_payload
= 65535 - sizeof(struct ofp_header
);
2319 unsigned int payload
;
2320 struct vconn
*vconn
;
2323 payload
= ctx
->argc
> 2 ? atoi(ctx
->argv
[2]) : 64;
2324 if (payload
> max_payload
) {
2325 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE
" bytes", max_payload
);
2328 open_vconn(ctx
->argv
[1], &vconn
);
2329 for (i
= 0; i
< 10; i
++) {
2330 struct timeval start
, end
;
2331 struct ofpbuf
*request
, *reply
;
2332 const struct ofp_header
*rpy_hdr
;
2335 request
= ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST
,
2336 vconn_get_version(vconn
), payload
);
2337 random_bytes(ofpbuf_put_uninit(request
, payload
), payload
);
2339 xgettimeofday(&start
);
2340 run(vconn_transact(vconn
, ofpbuf_clone(request
), &reply
), "transact");
2341 xgettimeofday(&end
);
2343 rpy_hdr
= reply
->data
;
2344 if (ofptype_pull(&type
, reply
)
2345 || type
!= OFPTYPE_ECHO_REPLY
2346 || reply
->size
!= payload
2347 || memcmp(request
->msg
, reply
->msg
, payload
)) {
2348 printf("Reply does not match request. Request:\n");
2349 ofp_print(stdout
, request
, request
->size
, verbosity
+ 2);
2351 ofp_print(stdout
, reply
, reply
->size
, verbosity
+ 2);
2353 printf("%"PRIu32
" bytes from %s: xid=%08"PRIx32
" time=%.1f ms\n",
2354 reply
->size
, ctx
->argv
[1], ntohl(rpy_hdr
->xid
),
2355 (1000*(double)(end
.tv_sec
- start
.tv_sec
))
2356 + (.001*(end
.tv_usec
- start
.tv_usec
)));
2357 ofpbuf_delete(request
);
2358 ofpbuf_delete(reply
);
2364 ofctl_benchmark(struct ovs_cmdl_context
*ctx
)
2366 size_t max_payload
= 65535 - sizeof(struct ofp_header
);
2367 struct timeval start
, end
;
2368 unsigned int payload_size
, message_size
;
2369 struct vconn
*vconn
;
2374 payload_size
= atoi(ctx
->argv
[2]);
2375 if (payload_size
> max_payload
) {
2376 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE
" bytes", max_payload
);
2378 message_size
= sizeof(struct ofp_header
) + payload_size
;
2380 count
= atoi(ctx
->argv
[3]);
2382 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
2383 count
, message_size
, count
* message_size
);
2385 open_vconn(ctx
->argv
[1], &vconn
);
2386 xgettimeofday(&start
);
2387 for (i
= 0; i
< count
; i
++) {
2388 struct ofpbuf
*request
, *reply
;
2390 request
= ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST
,
2391 vconn_get_version(vconn
), payload_size
);
2392 ofpbuf_put_zeros(request
, payload_size
);
2393 run(vconn_transact(vconn
, request
, &reply
), "transact");
2394 ofpbuf_delete(reply
);
2396 xgettimeofday(&end
);
2399 duration
= ((1000*(double)(end
.tv_sec
- start
.tv_sec
))
2400 + (.001*(end
.tv_usec
- start
.tv_usec
)));
2401 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
2402 duration
, count
/ (duration
/ 1000.0),
2403 count
* message_size
/ (duration
/ 1000.0));
2407 ofctl_group_mod__(const char *remote
, struct ofputil_group_mod
*gms
,
2408 size_t n_gms
, enum ofputil_protocol usable_protocols
)
2410 enum ofputil_protocol protocol
;
2411 struct ofputil_group_mod
*gm
;
2412 enum ofp_version version
;
2413 struct ofpbuf
*request
;
2415 struct vconn
*vconn
;
2418 protocol
= open_vconn_for_flow_mod(remote
, &vconn
, usable_protocols
);
2419 version
= ofputil_protocol_to_ofp_version(protocol
);
2421 for (i
= 0; i
< n_gms
; i
++) {
2423 request
= ofputil_encode_group_mod(version
, gm
);
2425 transact_noreply(vconn
, request
);
2435 ofctl_group_mod_file(int argc OVS_UNUSED
, char *argv
[], uint16_t command
)
2437 struct ofputil_group_mod
*gms
= NULL
;
2438 enum ofputil_protocol usable_protocols
;
2443 error
= parse_ofp_group_mod_file(argv
[2], command
, &gms
, &n_gms
,
2446 ovs_fatal(0, "%s", error
);
2448 ofctl_group_mod__(argv
[1], gms
, n_gms
, usable_protocols
);
2449 for (i
= 0; i
< n_gms
; i
++) {
2450 ofputil_bucket_list_destroy(&gms
[i
].buckets
);
2456 ofctl_group_mod(int argc
, char *argv
[], uint16_t command
)
2458 if (argc
> 2 && !strcmp(argv
[2], "-")) {
2459 ofctl_group_mod_file(argc
, argv
, command
);
2461 enum ofputil_protocol usable_protocols
;
2462 struct ofputil_group_mod gm
;
2465 error
= parse_ofp_group_mod_str(&gm
, command
, argc
> 2 ? argv
[2] : "",
2468 ovs_fatal(0, "%s", error
);
2470 ofctl_group_mod__(argv
[1], &gm
, 1, usable_protocols
);
2471 ofputil_bucket_list_destroy(&gm
.buckets
);
2476 ofctl_add_group(struct ovs_cmdl_context
*ctx
)
2478 ofctl_group_mod(ctx
->argc
, ctx
->argv
, OFPGC11_ADD
);
2482 ofctl_add_groups(struct ovs_cmdl_context
*ctx
)
2484 ofctl_group_mod_file(ctx
->argc
, ctx
->argv
, OFPGC11_ADD
);
2488 ofctl_mod_group(struct ovs_cmdl_context
*ctx
)
2490 ofctl_group_mod(ctx
->argc
, ctx
->argv
, OFPGC11_MODIFY
);
2494 ofctl_del_groups(struct ovs_cmdl_context
*ctx
)
2496 ofctl_group_mod(ctx
->argc
, ctx
->argv
, OFPGC11_DELETE
);
2500 ofctl_insert_bucket(struct ovs_cmdl_context
*ctx
)
2502 ofctl_group_mod(ctx
->argc
, ctx
->argv
, OFPGC15_INSERT_BUCKET
);
2506 ofctl_remove_bucket(struct ovs_cmdl_context
*ctx
)
2508 ofctl_group_mod(ctx
->argc
, ctx
->argv
, OFPGC15_REMOVE_BUCKET
);
2512 ofctl_dump_group_stats(struct ovs_cmdl_context
*ctx
)
2514 enum ofputil_protocol usable_protocols
;
2515 struct ofputil_group_mod gm
;
2516 struct ofpbuf
*request
;
2517 struct vconn
*vconn
;
2521 memset(&gm
, 0, sizeof gm
);
2523 error
= parse_ofp_group_mod_str(&gm
, OFPGC11_DELETE
,
2524 ctx
->argc
> 2 ? ctx
->argv
[2] : "",
2527 ovs_fatal(0, "%s", error
);
2530 group_id
= gm
.group_id
;
2532 open_vconn(ctx
->argv
[1], &vconn
);
2533 request
= ofputil_encode_group_stats_request(vconn_get_version(vconn
),
2536 dump_transaction(vconn
, request
);
2543 ofctl_dump_group_desc(struct ovs_cmdl_context
*ctx
)
2545 struct ofpbuf
*request
;
2546 struct vconn
*vconn
;
2549 open_vconn(ctx
->argv
[1], &vconn
);
2551 if (ctx
->argc
< 3 || !ofputil_group_from_string(ctx
->argv
[2], &group_id
)) {
2552 group_id
= OFPG_ALL
;
2555 request
= ofputil_encode_group_desc_request(vconn_get_version(vconn
),
2558 dump_transaction(vconn
, request
);
2565 ofctl_dump_group_features(struct ovs_cmdl_context
*ctx
)
2567 struct ofpbuf
*request
;
2568 struct vconn
*vconn
;
2570 open_vconn(ctx
->argv
[1], &vconn
);
2571 request
= ofputil_encode_group_features_request(vconn_get_version(vconn
));
2573 dump_transaction(vconn
, request
);
2580 ofctl_tlv_mod(struct ovs_cmdl_context
*ctx
, uint16_t command
)
2582 enum ofputil_protocol usable_protocols
;
2583 enum ofputil_protocol protocol
;
2584 struct ofputil_tlv_table_mod ttm
;
2586 enum ofp_version version
;
2587 struct ofpbuf
*request
;
2588 struct vconn
*vconn
;
2590 error
= parse_ofp_tlv_table_mod_str(&ttm
, command
, ctx
->argc
> 2 ?
2594 ovs_fatal(0, "%s", error
);
2597 protocol
= open_vconn_for_flow_mod(ctx
->argv
[1], &vconn
, usable_protocols
);
2598 version
= ofputil_protocol_to_ofp_version(protocol
);
2600 request
= ofputil_encode_tlv_table_mod(version
, &ttm
);
2602 transact_noreply(vconn
, request
);
2606 ofputil_uninit_tlv_table(&ttm
.mappings
);
2610 ofctl_add_tlv_map(struct ovs_cmdl_context
*ctx
)
2612 ofctl_tlv_mod(ctx
, NXTTMC_ADD
);
2616 ofctl_del_tlv_map(struct ovs_cmdl_context
*ctx
)
2618 ofctl_tlv_mod(ctx
, ctx
->argc
> 2 ? NXTTMC_DELETE
: NXTTMC_CLEAR
);
2622 ofctl_dump_tlv_map(struct ovs_cmdl_context
*ctx
)
2624 dump_trivial_transaction(ctx
->argv
[1], OFPRAW_NXT_TLV_TABLE_REQUEST
);
2628 ofctl_help(struct ovs_cmdl_context
*ctx OVS_UNUSED
)
2634 ofctl_list_commands(struct ovs_cmdl_context
*ctx OVS_UNUSED
)
2636 ovs_cmdl_print_commands(get_all_commands());
2639 /* replace-flows and diff-flows commands. */
2641 struct flow_tables
{
2642 struct classifier tables
[OFPTT_MAX
+ 1];
2645 #define FOR_EACH_TABLE(CLS, TABLES) \
2646 for ((CLS) = (TABLES)->tables; \
2647 (CLS) < &(TABLES)->tables[ARRAY_SIZE((TABLES)->tables)]; \
2651 flow_tables_init(struct flow_tables
*tables
)
2653 struct classifier
*cls
;
2655 FOR_EACH_TABLE (cls
, tables
) {
2656 classifier_init(cls
, NULL
);
2661 flow_tables_defer(struct flow_tables
*tables
)
2663 struct classifier
*cls
;
2665 FOR_EACH_TABLE (cls
, tables
) {
2666 classifier_defer(cls
);
2671 flow_tables_publish(struct flow_tables
*tables
)
2673 struct classifier
*cls
;
2675 FOR_EACH_TABLE (cls
, tables
) {
2676 classifier_publish(cls
);
2680 /* A flow table entry, possibly with two different versions. */
2682 struct cls_rule rule
; /* Within a "struct classifier". */
2683 struct fte_version
*versions
[2];
2686 /* One version of a Flow Table Entry. */
2687 struct fte_version
{
2689 uint16_t idle_timeout
;
2690 uint16_t hard_timeout
;
2691 uint16_t importance
;
2693 struct ofpact
*ofpacts
;
2698 /* Frees 'version' and the data that it owns. */
2700 fte_version_free(struct fte_version
*version
)
2703 free(CONST_CAST(struct ofpact
*, version
->ofpacts
));
2708 /* Returns true if 'a' and 'b' are the same, false if they differ.
2710 * Ignores differences in 'flags' because there's no way to retrieve flags from
2711 * an OpenFlow switch. We have to assume that they are the same. */
2713 fte_version_equals(const struct fte_version
*a
, const struct fte_version
*b
)
2715 return (a
->cookie
== b
->cookie
2716 && a
->idle_timeout
== b
->idle_timeout
2717 && a
->hard_timeout
== b
->hard_timeout
2718 && a
->importance
== b
->importance
2719 && a
->table_id
== b
->table_id
2720 && ofpacts_equal(a
->ofpacts
, a
->ofpacts_len
,
2721 b
->ofpacts
, b
->ofpacts_len
));
2724 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
2725 * 'index' into 's', followed by a new-line. */
2727 fte_version_format(const struct fte
*fte
, int index
, struct ds
*s
)
2729 const struct fte_version
*version
= fte
->versions
[index
];
2736 if (version
->table_id
) {
2737 ds_put_format(s
, "table=%"PRIu8
" ", version
->table_id
);
2739 cls_rule_format(&fte
->rule
, s
);
2740 if (version
->cookie
!= htonll(0)) {
2741 ds_put_format(s
, " cookie=0x%"PRIx64
, ntohll(version
->cookie
));
2743 if (version
->idle_timeout
!= OFP_FLOW_PERMANENT
) {
2744 ds_put_format(s
, " idle_timeout=%"PRIu16
, version
->idle_timeout
);
2746 if (version
->hard_timeout
!= OFP_FLOW_PERMANENT
) {
2747 ds_put_format(s
, " hard_timeout=%"PRIu16
, version
->hard_timeout
);
2749 if (version
->importance
!= 0) {
2750 ds_put_format(s
, " importance=%"PRIu16
, version
->importance
);
2753 ds_put_cstr(s
, " actions=");
2754 ofpacts_format(version
->ofpacts
, version
->ofpacts_len
, s
);
2756 ds_put_char(s
, '\n');
2760 fte_from_cls_rule(const struct cls_rule
*cls_rule
)
2762 return cls_rule
? CONTAINER_OF(cls_rule
, struct fte
, rule
) : NULL
;
2765 /* Frees 'fte' and its versions. */
2767 fte_free(struct fte
*fte
)
2770 fte_version_free(fte
->versions
[0]);
2771 fte_version_free(fte
->versions
[1]);
2772 cls_rule_destroy(&fte
->rule
);
2777 /* Frees all of the FTEs within 'tables'. */
2779 fte_free_all(struct flow_tables
*tables
)
2781 struct classifier
*cls
;
2783 FOR_EACH_TABLE (cls
, tables
) {
2786 classifier_defer(cls
);
2787 CLS_FOR_EACH (fte
, rule
, cls
) {
2788 classifier_remove(cls
, &fte
->rule
);
2789 ovsrcu_postpone(fte_free
, fte
);
2791 classifier_destroy(cls
);
2795 /* Searches 'tables' for an FTE matching 'rule', inserting a new one if
2796 * necessary. Sets 'version' as the version of that rule with the given
2797 * 'index', replacing any existing version, if any.
2799 * Takes ownership of 'version'. */
2801 fte_insert(struct flow_tables
*tables
, const struct match
*match
,
2802 int priority
, struct fte_version
*version
, int index
)
2804 struct classifier
*cls
= &tables
->tables
[version
->table_id
];
2805 struct fte
*old
, *fte
;
2807 fte
= xzalloc(sizeof *fte
);
2808 cls_rule_init(&fte
->rule
, match
, priority
);
2809 fte
->versions
[index
] = version
;
2811 old
= fte_from_cls_rule(classifier_replace(cls
, &fte
->rule
,
2812 CLS_MIN_VERSION
, NULL
, 0));
2814 fte
->versions
[!index
] = old
->versions
[!index
];
2815 old
->versions
[!index
] = NULL
;
2817 ovsrcu_postpone(fte_free
, old
);
2821 /* Reads the flows in 'filename' as flow table entries in 'tables' for the
2822 * version with the specified 'index'. Returns the flow formats able to
2823 * represent the flows that were read. */
2824 static enum ofputil_protocol
2825 read_flows_from_file(const char *filename
, struct flow_tables
*tables
,
2828 enum ofputil_protocol usable_protocols
;
2833 file
= !strcmp(filename
, "-") ? stdin
: fopen(filename
, "r");
2835 ovs_fatal(errno
, "%s: open", filename
);
2839 usable_protocols
= OFPUTIL_P_ANY
;
2841 flow_tables_defer(tables
);
2842 while (!ds_get_preprocessed_line(&s
, file
, &line_number
)) {
2843 struct fte_version
*version
;
2844 struct ofputil_flow_mod fm
;
2846 enum ofputil_protocol usable
;
2848 error
= parse_ofp_str(&fm
, OFPFC_ADD
, ds_cstr(&s
), &usable
);
2850 ovs_fatal(0, "%s:%d: %s", filename
, line_number
, error
);
2852 usable_protocols
&= usable
;
2854 version
= xmalloc(sizeof *version
);
2855 version
->cookie
= fm
.new_cookie
;
2856 version
->idle_timeout
= fm
.idle_timeout
;
2857 version
->hard_timeout
= fm
.hard_timeout
;
2858 version
->importance
= fm
.importance
;
2859 version
->flags
= fm
.flags
& (OFPUTIL_FF_SEND_FLOW_REM
2860 | OFPUTIL_FF_EMERG
);
2861 version
->ofpacts
= fm
.ofpacts
;
2862 version
->ofpacts_len
= fm
.ofpacts_len
;
2863 version
->table_id
= fm
.table_id
!= OFPTT_ALL
? fm
.table_id
: 0;
2865 fte_insert(tables
, &fm
.match
, fm
.priority
, version
, index
);
2867 flow_tables_publish(tables
);
2870 if (file
!= stdin
) {
2874 return usable_protocols
;
2878 recv_flow_stats_reply(struct vconn
*vconn
, ovs_be32 send_xid
,
2879 struct ofpbuf
**replyp
,
2880 struct ofputil_flow_stats
*fs
, struct ofpbuf
*ofpacts
)
2882 struct ofpbuf
*reply
= *replyp
;
2888 /* Get a flow stats reply message, if we don't already have one. */
2894 run(vconn_recv_block(vconn
, &reply
),
2895 "OpenFlow packet receive failed");
2896 } while (((struct ofp_header
*) reply
->data
)->xid
!= send_xid
);
2898 error
= ofptype_decode(&type
, reply
->data
);
2899 if (error
|| type
!= OFPTYPE_FLOW_STATS_REPLY
) {
2900 ovs_fatal(0, "received bad reply: %s",
2901 ofp_to_string(reply
->data
, reply
->size
,
2906 /* Pull an individual flow stats reply out of the message. */
2907 retval
= ofputil_decode_flow_stats_reply(fs
, reply
, false, ofpacts
);
2914 more
= ofpmp_more(reply
->header
);
2915 ofpbuf_delete(reply
);
2924 ovs_fatal(0, "parse error in reply (%s)",
2925 ofperr_to_string(retval
));
2930 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
2931 * format 'protocol', and adds them as flow table entries in 'tables' for the
2932 * version with the specified 'index'. */
2934 read_flows_from_switch(struct vconn
*vconn
,
2935 enum ofputil_protocol protocol
,
2936 struct flow_tables
*tables
, int index
)
2938 struct ofputil_flow_stats_request fsr
;
2939 struct ofputil_flow_stats fs
;
2940 struct ofpbuf
*request
;
2941 struct ofpbuf ofpacts
;
2942 struct ofpbuf
*reply
;
2945 fsr
.aggregate
= false;
2946 match_init_catchall(&fsr
.match
);
2947 fsr
.out_port
= OFPP_ANY
;
2948 fsr
.out_group
= OFPG_ANY
;
2949 fsr
.table_id
= 0xff;
2950 fsr
.cookie
= fsr
.cookie_mask
= htonll(0);
2951 request
= ofputil_encode_flow_stats_request(&fsr
, protocol
);
2952 send_xid
= ((struct ofp_header
*) request
->data
)->xid
;
2953 send_openflow_buffer(vconn
, request
);
2956 ofpbuf_init(&ofpacts
, 0);
2957 flow_tables_defer(tables
);
2958 while (recv_flow_stats_reply(vconn
, send_xid
, &reply
, &fs
, &ofpacts
)) {
2959 struct fte_version
*version
;
2961 version
= xmalloc(sizeof *version
);
2962 version
->cookie
= fs
.cookie
;
2963 version
->idle_timeout
= fs
.idle_timeout
;
2964 version
->hard_timeout
= fs
.hard_timeout
;
2965 version
->importance
= fs
.importance
;
2967 version
->ofpacts_len
= fs
.ofpacts_len
;
2968 version
->ofpacts
= xmemdup(fs
.ofpacts
, fs
.ofpacts_len
);
2969 version
->table_id
= fs
.table_id
;
2971 fte_insert(tables
, &fs
.match
, fs
.priority
, version
, index
);
2973 flow_tables_publish(tables
);
2974 ofpbuf_uninit(&ofpacts
);
2978 fte_make_flow_mod(const struct fte
*fte
, int index
, uint16_t command
,
2979 enum ofputil_protocol protocol
, struct ovs_list
*packets
)
2981 const struct fte_version
*version
= fte
->versions
[index
];
2984 struct ofputil_flow_mod fm
= {
2985 .priority
= fte
->rule
.priority
,
2986 .new_cookie
= version
->cookie
,
2987 .modify_cookie
= true,
2988 .table_id
= version
->table_id
,
2990 .idle_timeout
= version
->idle_timeout
,
2991 .hard_timeout
= version
->hard_timeout
,
2992 .importance
= version
->importance
,
2993 .buffer_id
= UINT32_MAX
,
2994 .out_port
= OFPP_ANY
,
2995 .out_group
= OFPG_ANY
,
2996 .flags
= version
->flags
,
2997 .delete_reason
= OFPRR_DELETE
,
2999 minimatch_expand(&fte
->rule
.match
, &fm
.match
);
3000 if (command
== OFPFC_ADD
|| command
== OFPFC_MODIFY
||
3001 command
== OFPFC_MODIFY_STRICT
) {
3002 fm
.ofpacts
= version
->ofpacts
;
3003 fm
.ofpacts_len
= version
->ofpacts_len
;
3009 ofm
= ofputil_encode_flow_mod(&fm
, protocol
);
3010 list_push_back(packets
, &ofm
->list_node
);
3014 ofctl_replace_flows(struct ovs_cmdl_context
*ctx
)
3016 enum { FILE_IDX
= 0, SWITCH_IDX
= 1 };
3017 enum ofputil_protocol usable_protocols
, protocol
;
3018 struct flow_tables tables
;
3019 struct classifier
*cls
;
3020 struct ovs_list requests
;
3021 struct vconn
*vconn
;
3024 flow_tables_init(&tables
);
3025 usable_protocols
= read_flows_from_file(ctx
->argv
[2], &tables
, FILE_IDX
);
3027 protocol
= open_vconn(ctx
->argv
[1], &vconn
);
3028 protocol
= set_protocol_for_flow_dump(vconn
, protocol
, usable_protocols
);
3030 read_flows_from_switch(vconn
, protocol
, &tables
, SWITCH_IDX
);
3032 list_init(&requests
);
3034 FOR_EACH_TABLE (cls
, &tables
) {
3035 /* Delete flows that exist on the switch but not in the file. */
3036 CLS_FOR_EACH (fte
, rule
, cls
) {
3037 struct fte_version
*file_ver
= fte
->versions
[FILE_IDX
];
3038 struct fte_version
*sw_ver
= fte
->versions
[SWITCH_IDX
];
3040 if (sw_ver
&& !file_ver
) {
3041 fte_make_flow_mod(fte
, SWITCH_IDX
, OFPFC_DELETE_STRICT
,
3042 protocol
, &requests
);
3046 /* Add flows that exist in the file but not on the switch.
3047 * Update flows that exist in both places but differ. */
3048 CLS_FOR_EACH (fte
, rule
, cls
) {
3049 struct fte_version
*file_ver
= fte
->versions
[FILE_IDX
];
3050 struct fte_version
*sw_ver
= fte
->versions
[SWITCH_IDX
];
3053 (readd
|| !sw_ver
|| !fte_version_equals(sw_ver
, file_ver
))) {
3054 fte_make_flow_mod(fte
, FILE_IDX
, OFPFC_ADD
, protocol
,
3060 bundle_transact(vconn
, &requests
, OFPBF_ORDERED
| OFPBF_ATOMIC
);
3062 transact_multiple_noreply(vconn
, &requests
);
3065 ofpbuf_list_delete(&requests
);
3068 fte_free_all(&tables
);
3072 read_flows_from_source(const char *source
, struct flow_tables
*tables
,
3077 if (source
[0] == '/' || source
[0] == '.'
3078 || (!strchr(source
, ':') && !stat(source
, &s
))) {
3079 read_flows_from_file(source
, tables
, index
);
3081 enum ofputil_protocol protocol
;
3082 struct vconn
*vconn
;
3084 protocol
= open_vconn(source
, &vconn
);
3085 protocol
= set_protocol_for_flow_dump(vconn
, protocol
, OFPUTIL_P_ANY
);
3086 read_flows_from_switch(vconn
, protocol
, tables
, index
);
3092 ofctl_diff_flows(struct ovs_cmdl_context
*ctx
)
3094 bool differences
= false;
3095 struct flow_tables tables
;
3096 struct classifier
*cls
;
3100 flow_tables_init(&tables
);
3101 read_flows_from_source(ctx
->argv
[1], &tables
, 0);
3102 read_flows_from_source(ctx
->argv
[2], &tables
, 1);
3107 FOR_EACH_TABLE (cls
, &tables
) {
3108 CLS_FOR_EACH (fte
, rule
, cls
) {
3109 struct fte_version
*a
= fte
->versions
[0];
3110 struct fte_version
*b
= fte
->versions
[1];
3112 if (!a
|| !b
|| !fte_version_equals(a
, b
)) {
3113 fte_version_format(fte
, 0, &a_s
);
3114 fte_version_format(fte
, 1, &b_s
);
3115 if (strcmp(ds_cstr(&a_s
), ds_cstr(&b_s
))) {
3117 printf("-%s", ds_cstr(&a_s
));
3120 printf("+%s", ds_cstr(&b_s
));
3131 fte_free_all(&tables
);
3139 ofctl_meter_mod__(const char *bridge
, const char *str
, int command
)
3141 struct ofputil_meter_mod mm
;
3142 struct vconn
*vconn
;
3143 enum ofputil_protocol protocol
;
3144 enum ofputil_protocol usable_protocols
;
3145 enum ofp_version version
;
3149 error
= parse_ofp_meter_mod_str(&mm
, str
, command
, &usable_protocols
);
3151 ovs_fatal(0, "%s", error
);
3154 usable_protocols
= OFPUTIL_P_OF13_UP
;
3155 mm
.command
= command
;
3156 mm
.meter
.meter_id
= OFPM13_ALL
;
3159 protocol
= open_vconn_for_flow_mod(bridge
, &vconn
, usable_protocols
);
3160 version
= ofputil_protocol_to_ofp_version(protocol
);
3161 transact_noreply(vconn
, ofputil_encode_meter_mod(version
, &mm
));
3166 ofctl_meter_request__(const char *bridge
, const char *str
,
3167 enum ofputil_meter_request_type type
)
3169 struct ofputil_meter_mod mm
;
3170 struct vconn
*vconn
;
3171 enum ofputil_protocol usable_protocols
;
3172 enum ofputil_protocol protocol
;
3173 enum ofp_version version
;
3177 error
= parse_ofp_meter_mod_str(&mm
, str
, -1, &usable_protocols
);
3179 ovs_fatal(0, "%s", error
);
3182 usable_protocols
= OFPUTIL_P_OF13_UP
;
3183 mm
.meter
.meter_id
= OFPM13_ALL
;
3186 protocol
= open_vconn_for_flow_mod(bridge
, &vconn
, usable_protocols
);
3187 version
= ofputil_protocol_to_ofp_version(protocol
);
3188 dump_transaction(vconn
, ofputil_encode_meter_request(version
, type
,
3189 mm
.meter
.meter_id
));
3195 ofctl_add_meter(struct ovs_cmdl_context
*ctx
)
3197 ofctl_meter_mod__(ctx
->argv
[1], ctx
->argv
[2], OFPMC13_ADD
);
3201 ofctl_mod_meter(struct ovs_cmdl_context
*ctx
)
3203 ofctl_meter_mod__(ctx
->argv
[1], ctx
->argv
[2], OFPMC13_MODIFY
);
3207 ofctl_del_meters(struct ovs_cmdl_context
*ctx
)
3209 ofctl_meter_mod__(ctx
->argv
[1], ctx
->argc
> 2 ? ctx
->argv
[2] : NULL
, OFPMC13_DELETE
);
3213 ofctl_dump_meters(struct ovs_cmdl_context
*ctx
)
3215 ofctl_meter_request__(ctx
->argv
[1], ctx
->argc
> 2 ? ctx
->argv
[2] : NULL
,
3216 OFPUTIL_METER_CONFIG
);
3220 ofctl_meter_stats(struct ovs_cmdl_context
*ctx
)
3222 ofctl_meter_request__(ctx
->argv
[1], ctx
->argc
> 2 ? ctx
->argv
[2] : NULL
,
3223 OFPUTIL_METER_STATS
);
3227 ofctl_meter_features(struct ovs_cmdl_context
*ctx
)
3229 ofctl_meter_request__(ctx
->argv
[1], NULL
, OFPUTIL_METER_FEATURES
);
3233 /* Undocumented commands for unit testing. */
3236 ofctl_parse_flows__(struct ofputil_flow_mod
*fms
, size_t n_fms
,
3237 enum ofputil_protocol usable_protocols
)
3239 enum ofputil_protocol protocol
= 0;
3243 usable_s
= ofputil_protocols_to_string(usable_protocols
);
3244 printf("usable protocols: %s\n", usable_s
);
3247 if (!(usable_protocols
& allowed_protocols
)) {
3248 ovs_fatal(0, "no usable protocol");
3250 for (i
= 0; i
< sizeof(enum ofputil_protocol
) * CHAR_BIT
; i
++) {
3252 if (protocol
& usable_protocols
& allowed_protocols
) {
3256 ovs_assert(is_pow2(protocol
));
3258 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol
));
3260 for (i
= 0; i
< n_fms
; i
++) {
3261 struct ofputil_flow_mod
*fm
= &fms
[i
];
3264 msg
= ofputil_encode_flow_mod(fm
, protocol
);
3265 ofp_print(stdout
, msg
->data
, msg
->size
, verbosity
);
3268 free(CONST_CAST(struct ofpact
*, fm
->ofpacts
));
3272 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
3273 * it back to stdout. */
3275 ofctl_parse_flow(struct ovs_cmdl_context
*ctx
)
3277 enum ofputil_protocol usable_protocols
;
3278 struct ofputil_flow_mod fm
;
3281 error
= parse_ofp_flow_mod_str(&fm
, ctx
->argv
[1], OFPFC_ADD
, &usable_protocols
);
3283 ovs_fatal(0, "%s", error
);
3285 ofctl_parse_flows__(&fm
, 1, usable_protocols
);
3288 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
3289 * add-flows) and prints each of the flows back to stdout. */
3291 ofctl_parse_flows(struct ovs_cmdl_context
*ctx
)
3293 enum ofputil_protocol usable_protocols
;
3294 struct ofputil_flow_mod
*fms
= NULL
;
3298 error
= parse_ofp_flow_mod_file(ctx
->argv
[1], OFPFC_ADD
, &fms
, &n_fms
,
3301 ovs_fatal(0, "%s", error
);
3303 ofctl_parse_flows__(fms
, n_fms
, usable_protocols
);
3308 ofctl_parse_nxm__(bool oxm
, enum ofp_version version
)
3313 while (!ds_get_test_line(&in
, stdin
)) {
3314 struct ofpbuf nx_match
;
3316 ovs_be64 cookie
, cookie_mask
;
3320 /* Convert string to nx_match. */
3321 ofpbuf_init(&nx_match
, 0);
3323 match_len
= oxm_match_from_string(ds_cstr(&in
), &nx_match
);
3325 match_len
= nx_match_from_string(ds_cstr(&in
), &nx_match
);
3328 /* Convert nx_match to match. */
3331 error
= oxm_pull_match(&nx_match
, &match
);
3333 error
= nx_pull_match(&nx_match
, match_len
, &match
,
3334 &cookie
, &cookie_mask
);
3338 error
= oxm_pull_match_loose(&nx_match
, &match
);
3340 error
= nx_pull_match_loose(&nx_match
, match_len
, &match
,
3341 &cookie
, &cookie_mask
);
3349 /* Convert match back to nx_match. */
3350 ofpbuf_uninit(&nx_match
);
3351 ofpbuf_init(&nx_match
, 0);
3353 match_len
= oxm_put_match(&nx_match
, &match
, version
);
3354 out
= oxm_match_to_string(&nx_match
, match_len
);
3356 match_len
= nx_put_match(&nx_match
, &match
,
3357 cookie
, cookie_mask
);
3358 out
= nx_match_to_string(nx_match
.data
, match_len
);
3364 if (verbosity
> 0) {
3365 ovs_hex_dump(stdout
, nx_match
.data
, nx_match
.size
, 0, false);
3368 printf("nx_pull_match() returned error %s\n",
3369 ofperr_get_name(error
));
3372 ofpbuf_uninit(&nx_match
);
3377 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
3378 * stdin, does some internal fussing with them, and then prints them back as
3379 * strings on stdout. */
3381 ofctl_parse_nxm(struct ovs_cmdl_context
*ctx OVS_UNUSED
)
3383 ofctl_parse_nxm__(false, 0);
3386 /* "parse-oxm VERSION": reads a series of OXM nx_match specifications as
3387 * strings from stdin, does some internal fussing with them, and then prints
3388 * them back as strings on stdout. VERSION must specify an OpenFlow version,
3389 * e.g. "OpenFlow12". */
3391 ofctl_parse_oxm(struct ovs_cmdl_context
*ctx
)
3393 enum ofp_version version
= ofputil_version_from_string(ctx
->argv
[1]);
3394 if (version
< OFP12_VERSION
) {
3395 ovs_fatal(0, "%s: not a valid version for OXM", ctx
->argv
[1]);
3398 ofctl_parse_nxm__(true, version
);
3402 print_differences(const char *prefix
,
3403 const void *a_
, size_t a_len
,
3404 const void *b_
, size_t b_len
)
3406 const uint8_t *a
= a_
;
3407 const uint8_t *b
= b_
;
3410 for (i
= 0; i
< MIN(a_len
, b_len
); i
++) {
3412 printf("%s%2"PRIuSIZE
": %02"PRIx8
" -> %02"PRIx8
"\n",
3413 prefix
, i
, a
[i
], b
[i
]);
3416 for (i
= a_len
; i
< b_len
; i
++) {
3417 printf("%s%2"PRIuSIZE
": (none) -> %02"PRIx8
"\n", prefix
, i
, b
[i
]);
3419 for (i
= b_len
; i
< a_len
; i
++) {
3420 printf("%s%2"PRIuSIZE
": %02"PRIx8
" -> (none)\n", prefix
, i
, a
[i
]);
3425 ofctl_parse_actions__(const char *version_s
, bool instructions
)
3427 enum ofp_version version
;
3430 version
= ofputil_version_from_string(version_s
);
3432 ovs_fatal(0, "%s: not a valid OpenFlow version", version_s
);
3436 while (!ds_get_preprocessed_line(&in
, stdin
, NULL
)) {
3437 struct ofpbuf of_out
;
3438 struct ofpbuf of_in
;
3439 struct ofpbuf ofpacts
;
3440 const char *table_id
;
3446 /* Parse table_id separated with the follow-up actions by ",", if
3448 actions
= ds_cstr(&in
);
3450 if (strstr(actions
, ",")) {
3451 table_id
= strsep(&actions
, ",");
3454 /* Parse hex bytes. */
3455 ofpbuf_init(&of_in
, 0);
3456 if (ofpbuf_put_hex(&of_in
, actions
, NULL
)[0] != '\0') {
3457 ovs_fatal(0, "Trailing garbage in hex data");
3460 /* Convert to ofpacts. */
3461 ofpbuf_init(&ofpacts
, 0);
3463 error
= (instructions
3464 ? ofpacts_pull_openflow_instructions
3465 : ofpacts_pull_openflow_actions
)(
3466 &of_in
, of_in
.size
, version
, &ofpacts
);
3467 if (!error
&& instructions
) {
3468 /* Verify actions, enforce consistency. */
3469 enum ofputil_protocol protocol
;
3472 memset(&flow
, 0, sizeof flow
);
3473 protocol
= ofputil_protocols_from_ofp_version(version
);
3474 error
= ofpacts_check_consistency(ofpacts
.data
, ofpacts
.size
,
3476 table_id
? atoi(table_id
) : 0,
3477 OFPTT_MAX
+ 1, protocol
);
3480 printf("bad %s %s: %s\n\n",
3481 version_s
, instructions
? "instructions" : "actions",
3482 ofperr_get_name(error
));
3483 ofpbuf_uninit(&ofpacts
);
3484 ofpbuf_uninit(&of_in
);
3487 ofpbuf_push_uninit(&of_in
, size
);
3489 /* Print cls_rule. */
3491 ds_put_cstr(&s
, "actions=");
3492 ofpacts_format(ofpacts
.data
, ofpacts
.size
, &s
);
3496 /* Convert back to ofp10 actions and print differences from input. */
3497 ofpbuf_init(&of_out
, 0);
3499 ofpacts_put_openflow_instructions(ofpacts
.data
, ofpacts
.size
,
3502 ofpacts_put_openflow_actions(ofpacts
.data
, ofpacts
.size
,
3506 print_differences("", of_in
.data
, of_in
.size
,
3507 of_out
.data
, of_out
.size
);
3510 ofpbuf_uninit(&ofpacts
);
3511 ofpbuf_uninit(&of_in
);
3512 ofpbuf_uninit(&of_out
);
3517 /* "parse-actions VERSION": reads a series of action specifications for the
3518 * given OpenFlow VERSION as hex bytes from stdin, converts them to ofpacts,
3519 * prints them as strings on stdout, and then converts them back to hex bytes
3520 * and prints any differences from the input. */
3522 ofctl_parse_actions(struct ovs_cmdl_context
*ctx
)
3524 ofctl_parse_actions__(ctx
->argv
[1], false);
3527 /* "parse-actions VERSION": reads a series of instruction specifications for
3528 * the given OpenFlow VERSION as hex bytes from stdin, converts them to
3529 * ofpacts, prints them as strings on stdout, and then converts them back to
3530 * hex bytes and prints any differences from the input. */
3532 ofctl_parse_instructions(struct ovs_cmdl_context
*ctx
)
3534 ofctl_parse_actions__(ctx
->argv
[1], true);
3537 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
3538 * bytes from stdin, converts them to cls_rules, prints them as strings on
3539 * stdout, and then converts them back to hex bytes and prints any differences
3542 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
3543 * values are ignored in the input and will be set to zero when OVS converts
3544 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
3545 * it does the conversion to hex, to ensure that in fact they are ignored. */
3547 ofctl_parse_ofp10_match(struct ovs_cmdl_context
*ctx OVS_UNUSED
)
3554 while (!ds_get_preprocessed_line(&in
, stdin
, NULL
)) {
3555 struct ofpbuf match_in
, match_expout
;
3556 struct ofp10_match match_out
;
3557 struct ofp10_match match_normal
;
3561 /* Parse hex bytes to use for expected output. */
3563 ds_put_cstr(&expout
, ds_cstr(&in
));
3564 for (p
= ds_cstr(&expout
); *p
; p
++) {
3569 ofpbuf_init(&match_expout
, 0);
3570 if (ofpbuf_put_hex(&match_expout
, ds_cstr(&expout
), NULL
)[0] != '\0') {
3571 ovs_fatal(0, "Trailing garbage in hex data");
3573 if (match_expout
.size
!= sizeof(struct ofp10_match
)) {
3574 ovs_fatal(0, "Input is %"PRIu32
" bytes, expected %"PRIuSIZE
,
3575 match_expout
.size
, sizeof(struct ofp10_match
));
3578 /* Parse hex bytes for input. */
3579 for (p
= ds_cstr(&in
); *p
; p
++) {
3581 *p
= "0123456789abcdef"[random_uint32() & 0xf];
3584 ofpbuf_init(&match_in
, 0);
3585 if (ofpbuf_put_hex(&match_in
, ds_cstr(&in
), NULL
)[0] != '\0') {
3586 ovs_fatal(0, "Trailing garbage in hex data");
3588 if (match_in
.size
!= sizeof(struct ofp10_match
)) {
3589 ovs_fatal(0, "Input is %"PRIu32
" bytes, expected %"PRIuSIZE
,
3590 match_in
.size
, sizeof(struct ofp10_match
));
3593 /* Convert to cls_rule and print. */
3594 ofputil_match_from_ofp10_match(match_in
.data
, &match
);
3595 match_print(&match
);
3597 /* Convert back to ofp10_match and print differences from input. */
3598 ofputil_match_to_ofp10_match(&match
, &match_out
);
3599 print_differences("", match_expout
.data
, match_expout
.size
,
3600 &match_out
, sizeof match_out
);
3602 /* Normalize, then convert and compare again. */
3603 ofputil_normalize_match(&match
);
3604 ofputil_match_to_ofp10_match(&match
, &match_normal
);
3605 print_differences("normal: ", &match_out
, sizeof match_out
,
3606 &match_normal
, sizeof match_normal
);
3609 ofpbuf_uninit(&match_in
);
3610 ofpbuf_uninit(&match_expout
);
3613 ds_destroy(&expout
);
3616 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
3617 * bytes from stdin, converts them to "struct match"es, prints them as strings
3618 * on stdout, and then converts them back to hex bytes and prints any
3619 * differences from the input. */
3621 ofctl_parse_ofp11_match(struct ovs_cmdl_context
*ctx OVS_UNUSED
)
3626 while (!ds_get_preprocessed_line(&in
, stdin
, NULL
)) {
3627 struct ofpbuf match_in
;
3628 struct ofp11_match match_out
;
3632 /* Parse hex bytes. */
3633 ofpbuf_init(&match_in
, 0);
3634 if (ofpbuf_put_hex(&match_in
, ds_cstr(&in
), NULL
)[0] != '\0') {
3635 ovs_fatal(0, "Trailing garbage in hex data");
3637 if (match_in
.size
!= sizeof(struct ofp11_match
)) {
3638 ovs_fatal(0, "Input is %"PRIu32
" bytes, expected %"PRIuSIZE
,
3639 match_in
.size
, sizeof(struct ofp11_match
));
3642 /* Convert to match. */
3643 error
= ofputil_match_from_ofp11_match(match_in
.data
, &match
);
3645 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error
));
3646 ofpbuf_uninit(&match_in
);
3651 match_print(&match
);
3653 /* Convert back to ofp11_match and print differences from input. */
3654 ofputil_match_to_ofp11_match(&match
, &match_out
);
3656 print_differences("", match_in
.data
, match_in
.size
,
3657 &match_out
, sizeof match_out
);
3660 ofpbuf_uninit(&match_in
);
3665 /* "parse-pcap PCAP...": read packets from each PCAP file and print their
3668 ofctl_parse_pcap(struct ovs_cmdl_context
*ctx
)
3671 for (int i
= 1; i
< ctx
->argc
; i
++) {
3672 const char *filename
= ctx
->argv
[i
];
3673 FILE *pcap
= ovs_pcap_open(filename
, "rb");
3676 ovs_error(error
, "%s: open failed", filename
);
3681 struct dp_packet
*packet
;
3685 retval
= ovs_pcap_read(pcap
, &packet
, NULL
);
3686 if (retval
== EOF
) {
3688 } else if (retval
) {
3690 ovs_error(error
, "%s: read failed", filename
);
3693 pkt_metadata_init(&packet
->md
, ODPP_NONE
);
3694 flow_extract(packet
, &flow
);
3695 flow_print(stdout
, &flow
);
3697 dp_packet_delete(packet
);
3704 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
3705 * mask values to and from various formats and prints the results. */
3707 ofctl_check_vlan(struct ovs_cmdl_context
*ctx
)
3712 struct ofputil_flow_mod fm
;
3715 struct match nxm_match
;
3719 struct ofp10_match of10_raw
;
3720 struct match of10_match
;
3722 struct ofp11_match of11_raw
;
3723 struct match of11_match
;
3728 enum ofputil_protocol usable_protocols
; /* Unused for now. */
3730 match_init_catchall(&match
);
3731 match
.flow
.vlan_tci
= htons(strtoul(ctx
->argv
[1], NULL
, 16));
3732 match
.wc
.masks
.vlan_tci
= htons(strtoul(ctx
->argv
[2], NULL
, 16));
3734 /* Convert to and from string. */
3735 string_s
= match_to_string(&match
, OFP_DEFAULT_PRIORITY
);
3736 printf("%s -> ", string_s
);
3738 error_s
= parse_ofp_str(&fm
, -1, string_s
, &usable_protocols
);
3740 ovs_fatal(0, "%s", error_s
);
3742 printf("%04"PRIx16
"/%04"PRIx16
"\n",
3743 ntohs(fm
.match
.flow
.vlan_tci
),
3744 ntohs(fm
.match
.wc
.masks
.vlan_tci
));
3747 /* Convert to and from NXM. */
3748 ofpbuf_init(&nxm
, 0);
3749 nxm_match_len
= nx_put_match(&nxm
, &match
, htonll(0), htonll(0));
3750 nxm_s
= nx_match_to_string(nxm
.data
, nxm_match_len
);
3751 error
= nx_pull_match(&nxm
, nxm_match_len
, &nxm_match
, NULL
, NULL
);
3752 printf("NXM: %s -> ", nxm_s
);
3754 printf("%s\n", ofperr_to_string(error
));
3756 printf("%04"PRIx16
"/%04"PRIx16
"\n",
3757 ntohs(nxm_match
.flow
.vlan_tci
),
3758 ntohs(nxm_match
.wc
.masks
.vlan_tci
));
3761 ofpbuf_uninit(&nxm
);
3763 /* Convert to and from OXM. */
3764 ofpbuf_init(&nxm
, 0);
3765 nxm_match_len
= oxm_put_match(&nxm
, &match
, OFP12_VERSION
);
3766 nxm_s
= oxm_match_to_string(&nxm
, nxm_match_len
);
3767 error
= oxm_pull_match(&nxm
, &nxm_match
);
3768 printf("OXM: %s -> ", nxm_s
);
3770 printf("%s\n", ofperr_to_string(error
));
3772 uint16_t vid
= ntohs(nxm_match
.flow
.vlan_tci
) &
3773 (VLAN_VID_MASK
| VLAN_CFI
);
3774 uint16_t mask
= ntohs(nxm_match
.wc
.masks
.vlan_tci
) &
3775 (VLAN_VID_MASK
| VLAN_CFI
);
3777 printf("%04"PRIx16
"/%04"PRIx16
",", vid
, mask
);
3778 if (vid
&& vlan_tci_to_pcp(nxm_match
.wc
.masks
.vlan_tci
)) {
3779 printf("%02"PRIx8
"\n", vlan_tci_to_pcp(nxm_match
.flow
.vlan_tci
));
3785 ofpbuf_uninit(&nxm
);
3787 /* Convert to and from OpenFlow 1.0. */
3788 ofputil_match_to_ofp10_match(&match
, &of10_raw
);
3789 ofputil_match_from_ofp10_match(&of10_raw
, &of10_match
);
3790 printf("OF1.0: %04"PRIx16
"/%d,%02"PRIx8
"/%d -> %04"PRIx16
"/%04"PRIx16
"\n",
3791 ntohs(of10_raw
.dl_vlan
),
3792 (of10_raw
.wildcards
& htonl(OFPFW10_DL_VLAN
)) != 0,
3793 of10_raw
.dl_vlan_pcp
,
3794 (of10_raw
.wildcards
& htonl(OFPFW10_DL_VLAN_PCP
)) != 0,
3795 ntohs(of10_match
.flow
.vlan_tci
),
3796 ntohs(of10_match
.wc
.masks
.vlan_tci
));
3798 /* Convert to and from OpenFlow 1.1. */
3799 ofputil_match_to_ofp11_match(&match
, &of11_raw
);
3800 ofputil_match_from_ofp11_match(&of11_raw
, &of11_match
);
3801 printf("OF1.1: %04"PRIx16
"/%d,%02"PRIx8
"/%d -> %04"PRIx16
"/%04"PRIx16
"\n",
3802 ntohs(of11_raw
.dl_vlan
),
3803 (of11_raw
.wildcards
& htonl(OFPFW11_DL_VLAN
)) != 0,
3804 of11_raw
.dl_vlan_pcp
,
3805 (of11_raw
.wildcards
& htonl(OFPFW11_DL_VLAN_PCP
)) != 0,
3806 ntohs(of11_match
.flow
.vlan_tci
),
3807 ntohs(of11_match
.wc
.masks
.vlan_tci
));
3810 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
3813 ofctl_print_error(struct ovs_cmdl_context
*ctx
)
3818 error
= ofperr_from_name(ctx
->argv
[1]);
3820 ovs_fatal(0, "unknown error \"%s\"", ctx
->argv
[1]);
3823 for (version
= 0; version
<= UINT8_MAX
; version
++) {
3824 const char *name
= ofperr_domain_get_name(version
);
3826 int vendor
= ofperr_get_vendor(error
, version
);
3827 int type
= ofperr_get_type(error
, version
);
3828 int code
= ofperr_get_code(error
, version
);
3830 if (vendor
!= -1 || type
!= -1 || code
!= -1) {
3831 printf("%s: vendor %#x, type %d, code %d\n",
3832 name
, vendor
, type
, code
);
3838 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
3839 * error named ENUM and prints the error reply in hex. */
3841 ofctl_encode_error_reply(struct ovs_cmdl_context
*ctx
)
3843 const struct ofp_header
*oh
;
3844 struct ofpbuf request
, *reply
;
3847 error
= ofperr_from_name(ctx
->argv
[1]);
3849 ovs_fatal(0, "unknown error \"%s\"", ctx
->argv
[1]);
3852 ofpbuf_init(&request
, 0);
3853 if (ofpbuf_put_hex(&request
, ctx
->argv
[2], NULL
)[0] != '\0') {
3854 ovs_fatal(0, "Trailing garbage in hex data");
3856 if (request
.size
< sizeof(struct ofp_header
)) {
3857 ovs_fatal(0, "Request too short");
3861 if (request
.size
!= ntohs(oh
->length
)) {
3862 ovs_fatal(0, "Request size inconsistent");
3865 reply
= ofperr_encode_reply(error
, request
.data
);
3866 ofpbuf_uninit(&request
);
3868 ovs_hex_dump(stdout
, reply
->data
, reply
->size
, 0, false);
3869 ofpbuf_delete(reply
);
3872 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
3873 * binary data, interpreting them as an OpenFlow message, and prints the
3874 * OpenFlow message on stdout, at VERBOSITY (level 2 by default).
3876 * Alternative usage: "ofp-print [VERBOSITY] - < HEXSTRING_FILE", where
3877 * HEXSTRING_FILE contains the HEXSTRING. */
3879 ofctl_ofp_print(struct ovs_cmdl_context
*ctx
)
3881 struct ofpbuf packet
;
3888 if (!strcmp(ctx
->argv
[ctx
->argc
-1], "-")) {
3889 if (ds_get_line(&line
, stdin
)) {
3890 VLOG_FATAL("Failed to read stdin");
3893 buffer
= line
.string
;
3894 verbosity
= ctx
->argc
> 2 ? atoi(ctx
->argv
[1]) : verbosity
;
3895 } else if (ctx
->argc
> 2) {
3896 buffer
= ctx
->argv
[1];
3897 verbosity
= atoi(ctx
->argv
[2]);
3899 buffer
= ctx
->argv
[1];
3902 ofpbuf_init(&packet
, strlen(buffer
) / 2);
3903 if (ofpbuf_put_hex(&packet
, buffer
, NULL
)[0] != '\0') {
3904 ovs_fatal(0, "trailing garbage following hex bytes");
3906 ofp_print(stdout
, packet
.data
, packet
.size
, verbosity
);
3907 ofpbuf_uninit(&packet
);
3911 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
3912 * and dumps each message in hex. */
3914 ofctl_encode_hello(struct ovs_cmdl_context
*ctx
)
3916 uint32_t bitmap
= strtol(ctx
->argv
[1], NULL
, 0);
3917 struct ofpbuf
*hello
;
3919 hello
= ofputil_encode_hello(bitmap
);
3920 ovs_hex_dump(stdout
, hello
->data
, hello
->size
, 0, false);
3921 ofp_print(stdout
, hello
->data
, hello
->size
, verbosity
);
3922 ofpbuf_delete(hello
);
3925 static const struct ovs_cmdl_command all_commands
[] = {
3928 { "monitor", "switch [misslen] [invalid_ttl] [watch:[...]]",
3929 1, 3, ofctl_monitor
},
3930 { "snoop", "switch",
3931 1, 1, ofctl_snoop
},
3932 { "dump-desc", "switch",
3933 1, 1, ofctl_dump_desc
},
3934 { "dump-tables", "switch",
3935 1, 1, ofctl_dump_tables
},
3936 { "dump-table-features", "switch",
3937 1, 1, ofctl_dump_table_features
},
3938 { "dump-table-desc", "switch",
3939 1, 1, ofctl_dump_table_desc
},
3940 { "dump-flows", "switch",
3941 1, 2, ofctl_dump_flows
},
3942 { "dump-aggregate", "switch",
3943 1, 2, ofctl_dump_aggregate
},
3944 { "queue-stats", "switch [port [queue]]",
3945 1, 3, ofctl_queue_stats
},
3946 { "queue-get-config", "switch [port [queue]]",
3947 1, 3, ofctl_queue_get_config
},
3948 { "add-flow", "switch flow",
3949 2, 2, ofctl_add_flow
},
3950 { "add-flows", "switch file",
3951 2, 2, ofctl_add_flows
},
3952 { "mod-flows", "switch flow",
3953 2, 2, ofctl_mod_flows
},
3954 { "del-flows", "switch [flow]",
3955 1, 2, ofctl_del_flows
},
3956 { "replace-flows", "switch file",
3957 2, 2, ofctl_replace_flows
},
3958 { "diff-flows", "source1 source2",
3959 2, 2, ofctl_diff_flows
},
3960 { "add-meter", "switch meter",
3961 2, 2, ofctl_add_meter
},
3962 { "mod-meter", "switch meter",
3963 2, 2, ofctl_mod_meter
},
3964 { "del-meter", "switch meter",
3965 2, 2, ofctl_del_meters
},
3966 { "del-meters", "switch",
3967 1, 1, ofctl_del_meters
},
3968 { "dump-meter", "switch meter",
3969 2, 2, ofctl_dump_meters
},
3970 { "dump-meters", "switch",
3971 1, 1, ofctl_dump_meters
},
3972 { "meter-stats", "switch [meter]",
3973 1, 2, ofctl_meter_stats
},
3974 { "meter-features", "switch",
3975 1, 1, ofctl_meter_features
},
3976 { "packet-out", "switch in_port actions packet...",
3977 4, INT_MAX
, ofctl_packet_out
},
3978 { "dump-ports", "switch [port]",
3979 1, 2, ofctl_dump_ports
},
3980 { "dump-ports-desc", "switch [port]",
3981 1, 2, ofctl_dump_ports_desc
},
3982 { "mod-port", "switch iface act",
3983 3, 3, ofctl_mod_port
},
3984 { "mod-table", "switch mod",
3985 3, 3, ofctl_mod_table
},
3986 { "get-frags", "switch",
3987 1, 1, ofctl_get_frags
},
3988 { "set-frags", "switch frag_mode",
3989 2, 2, ofctl_set_frags
},
3990 { "probe", "target",
3991 1, 1, ofctl_probe
},
3992 { "ping", "target [n]",
3994 { "benchmark", "target n count",
3995 3, 3, ofctl_benchmark
},
3997 { "ofp-parse", "file",
3998 1, 1, ofctl_ofp_parse
},
3999 { "ofp-parse-pcap", "pcap",
4000 1, INT_MAX
, ofctl_ofp_parse_pcap
},
4002 { "add-group", "switch group",
4003 1, 2, ofctl_add_group
},
4004 { "add-groups", "switch file",
4005 1, 2, ofctl_add_groups
},
4006 { "mod-group", "switch group",
4007 1, 2, ofctl_mod_group
},
4008 { "del-groups", "switch [group]",
4009 1, 2, ofctl_del_groups
},
4010 { "insert-buckets", "switch [group]",
4011 1, 2, ofctl_insert_bucket
},
4012 { "remove-buckets", "switch [group]",
4013 1, 2, ofctl_remove_bucket
},
4014 { "dump-groups", "switch [group]",
4015 1, 2, ofctl_dump_group_desc
},
4016 { "dump-group-stats", "switch [group]",
4017 1, 2, ofctl_dump_group_stats
},
4018 { "dump-group-features", "switch",
4019 1, 1, ofctl_dump_group_features
},
4020 { "add-tlv-map", "switch map",
4021 2, 2, ofctl_add_tlv_map
},
4022 { "del-tlv-map", "switch [map]",
4023 1, 2, ofctl_del_tlv_map
},
4024 { "dump-tlv-map", "switch",
4025 1, 1, ofctl_dump_tlv_map
},
4026 { "help", NULL
, 0, INT_MAX
, ofctl_help
},
4027 { "list-commands", NULL
, 0, INT_MAX
, ofctl_list_commands
},
4029 /* Undocumented commands for testing. */
4030 { "parse-flow", NULL
, 1, 1, ofctl_parse_flow
},
4031 { "parse-flows", NULL
, 1, 1, ofctl_parse_flows
},
4032 { "parse-nx-match", NULL
, 0, 0, ofctl_parse_nxm
},
4033 { "parse-nxm", NULL
, 0, 0, ofctl_parse_nxm
},
4034 { "parse-oxm", NULL
, 1, 1, ofctl_parse_oxm
},
4035 { "parse-actions", NULL
, 1, 1, ofctl_parse_actions
},
4036 { "parse-instructions", NULL
, 1, 1, ofctl_parse_instructions
},
4037 { "parse-ofp10-match", NULL
, 0, 0, ofctl_parse_ofp10_match
},
4038 { "parse-ofp11-match", NULL
, 0, 0, ofctl_parse_ofp11_match
},
4039 { "parse-pcap", NULL
, 1, INT_MAX
, ofctl_parse_pcap
},
4040 { "check-vlan", NULL
, 2, 2, ofctl_check_vlan
},
4041 { "print-error", NULL
, 1, 1, ofctl_print_error
},
4042 { "encode-error-reply", NULL
, 2, 2, ofctl_encode_error_reply
},
4043 { "ofp-print", NULL
, 1, 2, ofctl_ofp_print
},
4044 { "encode-hello", NULL
, 1, 1, ofctl_encode_hello
},
4046 { NULL
, NULL
, 0, 0, NULL
},
4049 static const struct ovs_cmdl_command
*get_all_commands(void)
4051 return all_commands
;