2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <sys/socket.h>
27 #include <sys/fcntl.h>
31 #include "byte-order.h"
32 #include "classifier.h"
33 #include "command-line.h"
37 #include "dynamic-string.h"
41 #include "ofp-errors.h"
42 #include "ofp-parse.h"
43 #include "ofp-print.h"
46 #include "ofproto/ofproto.h"
47 #include "openflow/nicira-ext.h"
48 #include "openflow/openflow.h"
50 #include "poll-loop.h"
52 #include "stream-ssl.h"
59 VLOG_DEFINE_THIS_MODULE(ofctl
);
61 /* --strict: Use strict matching for flow mod commands? Additionally governs
62 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
66 /* --readd: If true, on replace-flows, re-add even flows that have not changed
67 * (to reset flow counters). */
70 /* -F, --flow-format: Allowed protocols. By default, any protocol is
72 static enum ofputil_protocol allowed_protocols
= OFPUTIL_P_ANY
;
74 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
75 * commands. Either one of NXPIF_* to force a particular packet_in format, or
76 * -1 to let ovs-ofctl choose the default. */
77 static int preferred_packet_in_format
= -1;
79 /* -m, --more: Additional verbosity for ofp-print functions. */
82 /* --timestamp: Print a timestamp before each received packet on "monitor" and
84 static bool timestamp
;
86 static const struct command all_commands
[];
88 static void usage(void) NO_RETURN
;
89 static void parse_options(int argc
, char *argv
[]);
92 main(int argc
, char *argv
[])
94 set_program_name(argv
[0]);
95 parse_options(argc
, argv
);
96 signal(SIGPIPE
, SIG_IGN
);
97 run_command(argc
- optind
, argv
+ optind
, all_commands
);
102 parse_options(int argc
, char *argv
[])
105 OPT_STRICT
= UCHAR_MAX
+ 1,
111 static struct option long_options
[] = {
112 {"timeout", required_argument
, NULL
, 't'},
113 {"strict", no_argument
, NULL
, OPT_STRICT
},
114 {"readd", no_argument
, NULL
, OPT_READD
},
115 {"flow-format", required_argument
, NULL
, 'F'},
116 {"packet-in-format", required_argument
, NULL
, 'P'},
117 {"more", no_argument
, NULL
, 'm'},
118 {"timestamp", no_argument
, NULL
, OPT_TIMESTAMP
},
119 {"help", no_argument
, NULL
, 'h'},
120 {"version", no_argument
, NULL
, 'V'},
123 STREAM_SSL_LONG_OPTIONS
,
126 char *short_options
= long_options_to_short_options(long_options
);
129 unsigned long int timeout
;
132 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
139 timeout
= strtoul(optarg
, NULL
, 10);
141 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
149 allowed_protocols
= ofputil_protocols_from_string(optarg
);
150 if (!allowed_protocols
) {
151 ovs_fatal(0, "%s: invalid flow format(s)", optarg
);
156 preferred_packet_in_format
=
157 ofputil_packet_in_format_from_string(optarg
);
158 if (preferred_packet_in_format
< 0) {
159 ovs_fatal(0, "unknown packet-in format `%s'", optarg
);
171 ovs_print_version(OFP10_VERSION
, OFP10_VERSION
);
186 DAEMON_OPTION_HANDLERS
188 STREAM_SSL_OPTION_HANDLERS
203 printf("%s: OpenFlow switch management utility\n"
204 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
205 "\nFor OpenFlow switches:\n"
206 " show SWITCH show OpenFlow information\n"
207 " dump-desc SWITCH print switch description\n"
208 " dump-tables SWITCH print table stats\n"
209 " mod-port SWITCH IFACE ACT modify port behavior\n"
210 " get-frags SWITCH print fragment handling behavior\n"
211 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
212 " dump-ports SWITCH [PORT] print port statistics\n"
213 " dump-flows SWITCH print all flow entries\n"
214 " dump-flows SWITCH FLOW print matching FLOWs\n"
215 " dump-aggregate SWITCH print aggregate flow statistics\n"
216 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
217 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
218 " add-flow SWITCH FLOW add flow described by FLOW\n"
219 " add-flows SWITCH FILE add flows from FILE\n"
220 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
221 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
222 " replace-flows SWITCH FILE replace flows with those in FILE\n"
223 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
224 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
225 " execute ACTIONS on PACKET\n"
226 " monitor SWITCH [MISSLEN] [invalid_ttl]\n"
227 " print packets received from SWITCH\n"
228 " snoop SWITCH snoop on SWITCH and its controller\n"
229 "\nFor OpenFlow switches and controllers:\n"
230 " probe TARGET probe whether TARGET is up\n"
231 " ping TARGET [N] latency of N-byte echos\n"
232 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
233 "where SWITCH or TARGET is an active OpenFlow connection method.\n",
234 program_name
, program_name
);
235 vconn_usage(true, false, false);
238 printf("\nOther options:\n"
239 " --strict use strict match for flow commands\n"
240 " --readd replace flows that haven't changed\n"
241 " -F, --flow-format=FORMAT force particular flow format\n"
242 " -P, --packet-in-format=FRMT force particular packet in format\n"
243 " -m, --more be more verbose printing OpenFlow\n"
244 " --timestamp (monitor, snoop) print timestamps\n"
245 " -t, --timeout=SECS give up after SECS seconds\n"
246 " -h, --help display this help message\n"
247 " -V, --version display version information\n");
252 ofctl_exit(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
253 const char *argv
[] OVS_UNUSED
, void *exiting_
)
255 bool *exiting
= exiting_
;
257 unixctl_command_reply(conn
, NULL
);
260 static void run(int retval
, const char *message
, ...)
263 static void run(int retval
, const char *message
, ...)
268 va_start(args
, message
);
269 ovs_fatal_valist(retval
, message
, args
);
273 /* Generic commands. */
276 open_vconn_socket(const char *name
, struct vconn
**vconnp
)
278 char *vconn_name
= xasprintf("unix:%s", name
);
279 VLOG_DBG("connecting to %s", vconn_name
);
280 run(vconn_open_block(vconn_name
, OFP10_VERSION
, vconnp
),
281 "connecting to %s", vconn_name
);
285 static enum ofputil_protocol
286 open_vconn__(const char *name
, const char *default_suffix
,
287 struct vconn
**vconnp
)
289 char *datapath_name
, *datapath_type
, *socket_name
;
290 enum ofputil_protocol protocol
;
295 bridge_path
= xasprintf("%s/%s.%s", ovs_rundir(), name
, default_suffix
);
297 ofproto_parse_name(name
, &datapath_name
, &datapath_type
);
298 socket_name
= xasprintf("%s/%s.%s",
299 ovs_rundir(), datapath_name
, default_suffix
);
303 if (strchr(name
, ':')) {
304 run(vconn_open_block(name
, OFP10_VERSION
, vconnp
),
305 "connecting to %s", name
);
306 } else if (!stat(name
, &s
) && S_ISSOCK(s
.st_mode
)) {
307 open_vconn_socket(name
, vconnp
);
308 } else if (!stat(bridge_path
, &s
) && S_ISSOCK(s
.st_mode
)) {
309 open_vconn_socket(bridge_path
, vconnp
);
310 } else if (!stat(socket_name
, &s
)) {
311 if (!S_ISSOCK(s
.st_mode
)) {
312 ovs_fatal(0, "cannot connect to %s: %s is not a socket",
315 open_vconn_socket(socket_name
, vconnp
);
317 ovs_fatal(0, "%s is not a bridge or a socket", name
);
323 ofp_version
= vconn_get_version(*vconnp
);
324 protocol
= ofputil_protocol_from_ofp_version(ofp_version
);
326 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
332 static enum ofputil_protocol
333 open_vconn(const char *name
, struct vconn
**vconnp
)
335 return open_vconn__(name
, "mgmt", vconnp
);
339 alloc_stats_request(size_t rq_len
, uint16_t type
, struct ofpbuf
**bufferp
)
341 struct ofp_stats_msg
*rq
;
343 rq
= make_openflow(rq_len
, OFPT_STATS_REQUEST
, bufferp
);
344 rq
->type
= htons(type
);
345 rq
->flags
= htons(0);
350 send_openflow_buffer(struct vconn
*vconn
, struct ofpbuf
*buffer
)
352 update_openflow_length(buffer
);
353 run(vconn_send_block(vconn
, buffer
), "failed to send packet to switch");
357 dump_transaction(const char *vconn_name
, struct ofpbuf
*request
)
360 struct ofpbuf
*reply
;
362 update_openflow_length(request
);
363 open_vconn(vconn_name
, &vconn
);
364 run(vconn_transact(vconn
, request
, &reply
), "talking to %s", vconn_name
);
365 ofp_print(stdout
, reply
->data
, reply
->size
, verbosity
+ 1);
370 dump_trivial_transaction(const char *vconn_name
, uint8_t request_type
)
372 struct ofpbuf
*request
;
373 make_openflow(sizeof(struct ofp_header
), request_type
, &request
);
374 dump_transaction(vconn_name
, request
);
378 dump_stats_transaction(const char *vconn_name
, struct ofpbuf
*request
)
380 ovs_be32 send_xid
= ((struct ofp_header
*) request
->data
)->xid
;
384 open_vconn(vconn_name
, &vconn
);
385 send_openflow_buffer(vconn
, request
);
388 struct ofpbuf
*reply
;
390 run(vconn_recv_block(vconn
, &reply
), "OpenFlow packet receive failed");
391 recv_xid
= ((struct ofp_header
*) reply
->data
)->xid
;
392 if (send_xid
== recv_xid
) {
393 struct ofp_stats_msg
*osm
;
395 ofp_print(stdout
, reply
->data
, reply
->size
, verbosity
+ 1);
397 osm
= ofpbuf_at(reply
, 0, sizeof *osm
);
398 done
= !osm
|| !(ntohs(osm
->flags
) & OFPSF_REPLY_MORE
);
400 VLOG_DBG("received reply with xid %08"PRIx32
" "
401 "!= expected %08"PRIx32
, recv_xid
, send_xid
);
403 ofpbuf_delete(reply
);
409 dump_trivial_stats_transaction(const char *vconn_name
, uint8_t stats_type
)
411 struct ofpbuf
*request
;
412 alloc_stats_request(sizeof(struct ofp_stats_msg
), stats_type
, &request
);
413 dump_stats_transaction(vconn_name
, request
);
416 /* Sends 'request', which should be a request that only has a reply if an error
417 * occurs, and waits for it to succeed or fail. If an error does occur, prints
418 * it and exits with an error.
420 * Destroys all of the 'requests'. */
422 transact_multiple_noreply(struct vconn
*vconn
, struct list
*requests
)
424 struct ofpbuf
*request
, *reply
;
426 LIST_FOR_EACH (request
, list_node
, requests
) {
427 update_openflow_length(request
);
430 run(vconn_transact_multiple_noreply(vconn
, requests
, &reply
),
431 "talking to %s", vconn_get_name(vconn
));
433 ofp_print(stderr
, reply
->data
, reply
->size
, verbosity
+ 2);
436 ofpbuf_delete(reply
);
439 /* Sends 'request', which should be a request that only has a reply if an error
440 * occurs, and waits for it to succeed or fail. If an error does occur, prints
441 * it and exits with an error.
443 * Destroys 'request'. */
445 transact_noreply(struct vconn
*vconn
, struct ofpbuf
*request
)
447 struct list requests
;
449 list_init(&requests
);
450 list_push_back(&requests
, &request
->list_node
);
451 transact_multiple_noreply(vconn
, &requests
);
455 fetch_switch_config(struct vconn
*vconn
, struct ofp_switch_config
*config_
)
457 struct ofp_switch_config
*config
;
458 struct ofp_header
*header
;
459 struct ofpbuf
*request
;
460 struct ofpbuf
*reply
;
462 make_openflow(sizeof(struct ofp_header
), OFPT_GET_CONFIG_REQUEST
,
464 run(vconn_transact(vconn
, request
, &reply
),
465 "talking to %s", vconn_get_name(vconn
));
467 header
= reply
->data
;
468 if (header
->type
!= OFPT_GET_CONFIG_REPLY
||
469 header
->length
!= htons(sizeof *config
)) {
470 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn
));
473 config
= reply
->data
;
476 ofpbuf_delete(reply
);
480 set_switch_config(struct vconn
*vconn
, struct ofp_switch_config
*config_
)
482 struct ofp_switch_config
*config
;
483 struct ofp_header save_header
;
484 struct ofpbuf
*request
;
486 config
= make_openflow(sizeof *config
, OFPT_SET_CONFIG
, &request
);
487 save_header
= config
->header
;
489 config
->header
= save_header
;
491 transact_noreply(vconn
, request
);
495 do_show(int argc OVS_UNUSED
, char *argv
[])
497 dump_trivial_transaction(argv
[1], OFPT_FEATURES_REQUEST
);
498 dump_trivial_transaction(argv
[1], OFPT_GET_CONFIG_REQUEST
);
502 do_dump_desc(int argc OVS_UNUSED
, char *argv
[])
504 dump_trivial_stats_transaction(argv
[1], OFPST_DESC
);
508 do_dump_tables(int argc OVS_UNUSED
, char *argv
[])
510 dump_trivial_stats_transaction(argv
[1], OFPST_TABLE
);
513 /* Opens a connection to 'vconn_name', fetches the ofp_phy_port structure for
514 * 'port_name' (which may be a port name or number), and copies it into
517 fetch_ofp_phy_port(const char *vconn_name
, const char *port_name
,
518 struct ofp_phy_port
*oppp
)
520 struct ofpbuf
*request
, *reply
;
521 struct ofp_switch_features
*osf
;
522 unsigned int port_no
;
527 /* Try to interpret the argument as a port number. */
528 if (!str_to_uint(port_name
, 10, &port_no
)) {
532 /* Fetch the switch's ofp_switch_features. */
533 make_openflow(sizeof(struct ofp_header
), OFPT_FEATURES_REQUEST
, &request
);
534 open_vconn(vconn_name
, &vconn
);
535 run(vconn_transact(vconn
, request
, &reply
), "talking to %s", vconn_name
);
538 if (reply
->size
< sizeof *osf
) {
539 ovs_fatal(0, "%s: received too-short features reply (only %zu bytes)",
540 vconn_name
, reply
->size
);
542 n_ports
= (reply
->size
- sizeof *osf
) / sizeof *osf
->ports
;
544 for (port_idx
= 0; port_idx
< n_ports
; port_idx
++) {
545 const struct ofp_phy_port
*opp
= &osf
->ports
[port_idx
];
547 if (port_no
!= UINT_MAX
548 ? htons(port_no
) == opp
->port_no
549 : !strncmp(opp
->name
, port_name
, sizeof opp
->name
)) {
551 ofpbuf_delete(reply
);
556 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name
, port_name
);
559 /* Returns the port number corresponding to 'port_name' (which may be a port
560 * name or number) within the switch 'vconn_name'. */
562 str_to_port_no(const char *vconn_name
, const char *port_name
)
564 unsigned int port_no
;
566 if (str_to_uint(port_name
, 10, &port_no
)) {
569 struct ofp_phy_port opp
;
571 fetch_ofp_phy_port(vconn_name
, port_name
, &opp
);
572 return ntohs(opp
.port_no
);
577 try_set_protocol(struct vconn
*vconn
, enum ofputil_protocol want
,
578 enum ofputil_protocol
*cur
)
581 struct ofpbuf
*request
, *reply
;
582 enum ofputil_protocol next
;
584 request
= ofputil_encode_set_protocol(*cur
, want
, &next
);
589 run(vconn_transact_noreply(vconn
, request
, &reply
),
590 "talking to %s", vconn_get_name(vconn
));
592 char *s
= ofp_to_string(reply
->data
, reply
->size
, 2);
593 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
594 vconn_get_name(vconn
), s
);
596 ofpbuf_delete(reply
);
604 static enum ofputil_protocol
605 set_protocol_for_flow_dump(struct vconn
*vconn
,
606 enum ofputil_protocol cur_protocol
,
607 enum ofputil_protocol usable_protocols
)
612 for (i
= 0; i
< ofputil_n_flow_dump_protocols
; i
++) {
613 enum ofputil_protocol f
= ofputil_flow_dump_protocols
[i
];
614 if (f
& usable_protocols
& allowed_protocols
615 && try_set_protocol(vconn
, f
, &cur_protocol
)) {
620 usable_s
= ofputil_protocols_to_string(usable_protocols
);
621 if (usable_protocols
& allowed_protocols
) {
622 ovs_fatal(0, "switch does not support any of the usable flow "
623 "formats (%s)", usable_s
);
625 char *allowed_s
= ofputil_protocols_to_string(allowed_protocols
);
626 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
627 "allowed flow formats (%s)", usable_s
, allowed_s
);
632 do_dump_flows__(int argc
, char *argv
[], bool aggregate
)
634 enum ofputil_protocol usable_protocols
, protocol
;
635 struct ofputil_flow_stats_request fsr
;
636 struct ofpbuf
*request
;
639 parse_ofp_flow_stats_request_str(&fsr
, aggregate
, argc
> 2 ? argv
[2] : "");
640 usable_protocols
= ofputil_flow_stats_request_usable_protocols(&fsr
);
642 protocol
= open_vconn(argv
[1], &vconn
);
643 protocol
= set_protocol_for_flow_dump(vconn
, protocol
, usable_protocols
);
644 request
= ofputil_encode_flow_stats_request(&fsr
, protocol
);
645 dump_stats_transaction(argv
[1], request
);
650 do_dump_flows(int argc
, char *argv
[])
652 return do_dump_flows__(argc
, argv
, false);
656 do_dump_aggregate(int argc
, char *argv
[])
658 return do_dump_flows__(argc
, argv
, true);
662 do_queue_stats(int argc
, char *argv
[])
664 struct ofp_queue_stats_request
*req
;
665 struct ofpbuf
*request
;
667 req
= alloc_stats_request(sizeof *req
, OFPST_QUEUE
, &request
);
669 if (argc
> 2 && argv
[2][0] && strcasecmp(argv
[2], "all")) {
670 req
->port_no
= htons(str_to_port_no(argv
[1], argv
[2]));
672 req
->port_no
= htons(OFPP_ALL
);
674 if (argc
> 3 && argv
[3][0] && strcasecmp(argv
[3], "all")) {
675 req
->queue_id
= htonl(atoi(argv
[3]));
677 req
->queue_id
= htonl(OFPQ_ALL
);
680 memset(req
->pad
, 0, sizeof req
->pad
);
682 dump_stats_transaction(argv
[1], request
);
685 static enum ofputil_protocol
686 open_vconn_for_flow_mod(const char *remote
,
687 const struct ofputil_flow_mod
*fms
, size_t n_fms
,
688 struct vconn
**vconnp
)
690 enum ofputil_protocol usable_protocols
;
691 enum ofputil_protocol cur_protocol
;
695 /* Figure out what flow formats will work. */
696 usable_protocols
= ofputil_flow_mod_usable_protocols(fms
, n_fms
);
697 if (!(usable_protocols
& allowed_protocols
)) {
698 char *allowed_s
= ofputil_protocols_to_string(allowed_protocols
);
699 usable_s
= ofputil_protocols_to_string(usable_protocols
);
700 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
701 "allowed flow formats (%s)", usable_s
, allowed_s
);
704 /* If the initial flow format is allowed and usable, keep it. */
705 cur_protocol
= open_vconn(remote
, vconnp
);
706 if (usable_protocols
& allowed_protocols
& cur_protocol
) {
710 /* Otherwise try each flow format in turn. */
711 for (i
= 0; i
< sizeof(enum ofputil_protocol
) * CHAR_BIT
; i
++) {
712 enum ofputil_protocol f
= 1 << i
;
714 if (f
!= cur_protocol
715 && f
& usable_protocols
& allowed_protocols
716 && try_set_protocol(*vconnp
, f
, &cur_protocol
)) {
721 usable_s
= ofputil_protocols_to_string(usable_protocols
);
722 ovs_fatal(0, "switch does not support any of the usable flow "
723 "formats (%s)", usable_s
);
727 do_flow_mod__(const char *remote
, struct ofputil_flow_mod
*fms
, size_t n_fms
)
729 enum ofputil_protocol protocol
;
733 protocol
= open_vconn_for_flow_mod(remote
, fms
, n_fms
, &vconn
);
735 for (i
= 0; i
< n_fms
; i
++) {
736 struct ofputil_flow_mod
*fm
= &fms
[i
];
738 transact_noreply(vconn
, ofputil_encode_flow_mod(fm
, protocol
));
745 do_flow_mod_file(int argc OVS_UNUSED
, char *argv
[], uint16_t command
)
747 struct ofputil_flow_mod
*fms
= NULL
;
750 parse_ofp_flow_mod_file(argv
[2], command
, &fms
, &n_fms
);
751 do_flow_mod__(argv
[1], fms
, n_fms
);
756 do_flow_mod(int argc
, char *argv
[], uint16_t command
)
758 if (argc
> 2 && !strcmp(argv
[2], "-")) {
759 do_flow_mod_file(argc
, argv
, command
);
761 struct ofputil_flow_mod fm
;
762 parse_ofp_flow_mod_str(&fm
, argc
> 2 ? argv
[2] : "", command
, false);
763 do_flow_mod__(argv
[1], &fm
, 1);
768 do_add_flow(int argc
, char *argv
[])
770 do_flow_mod(argc
, argv
, OFPFC_ADD
);
774 do_add_flows(int argc
, char *argv
[])
776 do_flow_mod_file(argc
, argv
, OFPFC_ADD
);
780 do_mod_flows(int argc
, char *argv
[])
782 do_flow_mod(argc
, argv
, strict
? OFPFC_MODIFY_STRICT
: OFPFC_MODIFY
);
786 do_del_flows(int argc
, char *argv
[])
788 do_flow_mod(argc
, argv
, strict
? OFPFC_DELETE_STRICT
: OFPFC_DELETE
);
792 set_packet_in_format(struct vconn
*vconn
,
793 enum nx_packet_in_format packet_in_format
)
795 struct ofpbuf
*spif
= ofputil_make_set_packet_in_format(packet_in_format
);
796 transact_noreply(vconn
, spif
);
797 VLOG_DBG("%s: using user-specified packet in format %s",
798 vconn_get_name(vconn
),
799 ofputil_packet_in_format_to_string(packet_in_format
));
803 monitor_set_invalid_ttl_to_controller(struct vconn
*vconn
)
805 struct ofp_switch_config config
;
806 enum ofp_config_flags flags
;
808 fetch_switch_config(vconn
, &config
);
809 flags
= ntohs(config
.flags
);
810 if (!(flags
& OFPC_INVALID_TTL_TO_CONTROLLER
)) {
811 /* Set the invalid ttl config. */
812 flags
|= OFPC_INVALID_TTL_TO_CONTROLLER
;
814 config
.flags
= htons(flags
);
815 set_switch_config(vconn
, &config
);
817 /* Then retrieve the configuration to see if it really took. OpenFlow
818 * doesn't define error reporting for bad modes, so this is all we can
820 fetch_switch_config(vconn
, &config
);
821 flags
= ntohs(config
.flags
);
822 if (!(flags
& OFPC_INVALID_TTL_TO_CONTROLLER
)) {
823 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
824 "switch probably doesn't support mode)");
831 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
832 * caller must free '*msgp'. On success, returns NULL. On failure, returns
833 * an error message and stores NULL in '*msgp'. */
835 openflow_from_hex(const char *hex
, struct ofpbuf
**msgp
)
837 struct ofp_header
*oh
;
840 msg
= ofpbuf_new(strlen(hex
) / 2);
843 if (ofpbuf_put_hex(msg
, hex
, NULL
)[0] != '\0') {
845 return "Trailing garbage in hex data";
848 if (msg
->size
< sizeof(struct ofp_header
)) {
850 return "Message too short for OpenFlow";
854 if (msg
->size
!= ntohs(oh
->length
)) {
856 return "Message size does not match length in OpenFlow header";
864 ofctl_send(struct unixctl_conn
*conn
, int argc
,
865 const char *argv
[], void *vconn_
)
867 struct vconn
*vconn
= vconn_
;
874 for (i
= 1; i
< argc
; i
++) {
875 const char *error_msg
;
879 error_msg
= openflow_from_hex(argv
[i
], &msg
);
881 ds_put_format(&reply
, "%s\n", error_msg
);
886 fprintf(stderr
, "send: ");
887 ofp_print(stderr
, msg
->data
, msg
->size
, verbosity
);
889 error
= vconn_send_block(vconn
, msg
);
892 ds_put_format(&reply
, "%s\n", strerror(error
));
895 ds_put_cstr(&reply
, "sent\n");
900 unixctl_command_reply(conn
, ds_cstr(&reply
));
902 unixctl_command_reply_error(conn
, ds_cstr(&reply
));
908 struct vconn
*vconn
; /* OpenFlow connection for sending barrier. */
909 struct unixctl_conn
*conn
; /* Connection waiting for barrier response. */
913 ofctl_barrier(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
914 const char *argv
[] OVS_UNUSED
, void *aux_
)
916 struct barrier_aux
*aux
= aux_
;
921 unixctl_command_reply_error(conn
, "already waiting for barrier reply");
925 msg
= ofputil_encode_barrier_request();
926 fprintf(stderr
, "send: ");
927 ofp_print(stderr
, msg
->data
, msg
->size
, verbosity
);
929 error
= vconn_send_block(aux
->vconn
, msg
);
932 unixctl_command_reply_error(conn
, strerror(error
));
939 ofctl_set_output_file(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
940 const char *argv
[], void *aux OVS_UNUSED
)
944 fd
= open(argv
[1], O_CREAT
| O_TRUNC
| O_WRONLY
, 0666);
946 unixctl_command_reply_error(conn
, strerror(errno
));
951 dup2(fd
, STDERR_FILENO
);
953 unixctl_command_reply(conn
, NULL
);
957 monitor_vconn(struct vconn
*vconn
)
959 struct barrier_aux barrier_aux
= { vconn
, NULL
};
960 struct unixctl_server
*server
;
961 bool exiting
= false;
964 daemon_save_fd(STDERR_FILENO
);
966 error
= unixctl_server_create(NULL
, &server
);
968 ovs_fatal(error
, "failed to create unixctl server");
970 unixctl_command_register("exit", "", 0, 0, ofctl_exit
, &exiting
);
971 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX
,
973 unixctl_command_register("ofctl/barrier", "", 0, 0,
974 ofctl_barrier
, &barrier_aux
);
975 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
976 ofctl_set_output_file
, NULL
);
977 daemonize_complete();
983 unixctl_server_run(server
);
988 retval
= vconn_recv(vconn
, &b
);
989 if (retval
== EAGAIN
) {
992 run(retval
, "vconn_recv");
995 time_t now
= time_wall();
998 strftime(s
, sizeof s
, "%Y-%m-%d %H:%M:%S: ", localtime(&now
));
1002 msg_type
= ((const struct ofp_header
*) b
->data
)->type
;
1003 ofp_print(stderr
, b
->data
, b
->size
, verbosity
+ 2);
1006 if (barrier_aux
.conn
&& msg_type
== OFPT_BARRIER_REPLY
) {
1007 unixctl_command_reply(barrier_aux
.conn
, NULL
);
1008 barrier_aux
.conn
= NULL
;
1017 vconn_run_wait(vconn
);
1018 vconn_recv_wait(vconn
);
1019 unixctl_server_wait(server
);
1023 unixctl_server_destroy(server
);
1027 do_monitor(int argc
, char *argv
[])
1029 struct vconn
*vconn
;
1031 open_vconn(argv
[1], &vconn
);
1033 struct ofp_switch_config config
;
1035 fetch_switch_config(vconn
, &config
);
1036 config
.miss_send_len
= htons(atoi(argv
[2]));
1037 set_switch_config(vconn
, &config
);
1040 if (!strcmp(argv
[3], "invalid_ttl")) {
1041 monitor_set_invalid_ttl_to_controller(vconn
);
1044 if (preferred_packet_in_format
>= 0) {
1045 set_packet_in_format(vconn
, preferred_packet_in_format
);
1047 struct ofpbuf
*spif
, *reply
;
1049 spif
= ofputil_make_set_packet_in_format(NXPIF_NXM
);
1050 run(vconn_transact_noreply(vconn
, spif
, &reply
),
1051 "talking to %s", vconn_get_name(vconn
));
1053 char *s
= ofp_to_string(reply
->data
, reply
->size
, 2);
1054 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1055 " replied: %s. Falling back to the switch default.",
1056 vconn_get_name(vconn
), s
);
1058 ofpbuf_delete(reply
);
1062 monitor_vconn(vconn
);
1066 do_snoop(int argc OVS_UNUSED
, char *argv
[])
1068 struct vconn
*vconn
;
1070 open_vconn__(argv
[1], "snoop", &vconn
);
1071 monitor_vconn(vconn
);
1075 do_dump_ports(int argc
, char *argv
[])
1077 struct ofp_port_stats_request
*req
;
1078 struct ofpbuf
*request
;
1081 req
= alloc_stats_request(sizeof *req
, OFPST_PORT
, &request
);
1082 port
= argc
> 2 ? str_to_port_no(argv
[1], argv
[2]) : OFPP_NONE
;
1083 req
->port_no
= htons(port
);
1084 dump_stats_transaction(argv
[1], request
);
1088 do_probe(int argc OVS_UNUSED
, char *argv
[])
1090 struct ofpbuf
*request
;
1091 struct vconn
*vconn
;
1092 struct ofpbuf
*reply
;
1094 make_openflow(sizeof(struct ofp_header
), OFPT_ECHO_REQUEST
, &request
);
1095 open_vconn(argv
[1], &vconn
);
1096 run(vconn_transact(vconn
, request
, &reply
), "talking to %s", argv
[1]);
1097 if (reply
->size
!= sizeof(struct ofp_header
)) {
1098 ovs_fatal(0, "reply does not match request");
1100 ofpbuf_delete(reply
);
1105 do_packet_out(int argc
, char *argv
[])
1107 struct ofputil_packet_out po
;
1108 struct ofpbuf actions
;
1109 struct vconn
*vconn
;
1112 ofpbuf_init(&actions
, sizeof(union ofp_action
));
1113 parse_ofp_actions(argv
[3], &actions
);
1115 po
.buffer_id
= UINT32_MAX
;
1116 po
.in_port
= (!strcasecmp(argv
[2], "none") ? OFPP_NONE
1117 : !strcasecmp(argv
[2], "local") ? OFPP_LOCAL
1118 : str_to_port_no(argv
[1], argv
[2]));
1119 po
.actions
= actions
.data
;
1120 po
.n_actions
= actions
.size
/ sizeof(union ofp_action
);
1122 open_vconn(argv
[1], &vconn
);
1123 for (i
= 4; i
< argc
; i
++) {
1124 struct ofpbuf
*packet
, *opo
;
1125 const char *error_msg
;
1127 error_msg
= eth_from_hex(argv
[i
], &packet
);
1129 ovs_fatal(0, "%s", error_msg
);
1132 po
.packet
= packet
->data
;
1133 po
.packet_len
= packet
->size
;
1134 opo
= ofputil_encode_packet_out(&po
);
1135 transact_noreply(vconn
, opo
);
1136 ofpbuf_delete(packet
);
1142 do_mod_port(int argc OVS_UNUSED
, char *argv
[])
1144 struct ofp_port_mod
*opm
;
1145 struct ofp_phy_port opp
;
1146 struct ofpbuf
*request
;
1147 struct vconn
*vconn
;
1149 fetch_ofp_phy_port(argv
[1], argv
[2], &opp
);
1151 opm
= make_openflow(sizeof(struct ofp_port_mod
), OFPT_PORT_MOD
, &request
);
1152 opm
->port_no
= opp
.port_no
;
1153 memcpy(opm
->hw_addr
, opp
.hw_addr
, sizeof opm
->hw_addr
);
1154 opm
->config
= htonl(0);
1155 opm
->mask
= htonl(0);
1156 opm
->advertise
= htonl(0);
1158 if (!strcasecmp(argv
[3], "up")) {
1159 opm
->mask
|= htonl(OFPPC_PORT_DOWN
);
1160 } else if (!strcasecmp(argv
[3], "down")) {
1161 opm
->mask
|= htonl(OFPPC_PORT_DOWN
);
1162 opm
->config
|= htonl(OFPPC_PORT_DOWN
);
1163 } else if (!strcasecmp(argv
[3], "flood")) {
1164 opm
->mask
|= htonl(OFPPC_NO_FLOOD
);
1165 } else if (!strcasecmp(argv
[3], "noflood")) {
1166 opm
->mask
|= htonl(OFPPC_NO_FLOOD
);
1167 opm
->config
|= htonl(OFPPC_NO_FLOOD
);
1168 } else if (!strcasecmp(argv
[3], "forward")) {
1169 opm
->mask
|= htonl(OFPPC_NO_FWD
);
1170 } else if (!strcasecmp(argv
[3], "noforward")) {
1171 opm
->mask
|= htonl(OFPPC_NO_FWD
);
1172 opm
->config
|= htonl(OFPPC_NO_FWD
);
1174 ovs_fatal(0, "unknown mod-port command '%s'", argv
[3]);
1177 open_vconn(argv
[1], &vconn
);
1178 transact_noreply(vconn
, request
);
1183 do_get_frags(int argc OVS_UNUSED
, char *argv
[])
1185 struct ofp_switch_config config
;
1186 struct vconn
*vconn
;
1188 open_vconn(argv
[1], &vconn
);
1189 fetch_switch_config(vconn
, &config
);
1190 puts(ofputil_frag_handling_to_string(ntohs(config
.flags
)));
1195 do_set_frags(int argc OVS_UNUSED
, char *argv
[])
1197 struct ofp_switch_config config
;
1198 enum ofp_config_flags mode
;
1199 struct vconn
*vconn
;
1202 if (!ofputil_frag_handling_from_string(argv
[2], &mode
)) {
1203 ovs_fatal(0, "%s: unknown fragment handling mode", argv
[2]);
1206 open_vconn(argv
[1], &vconn
);
1207 fetch_switch_config(vconn
, &config
);
1208 flags
= htons(mode
) | (config
.flags
& htons(~OFPC_FRAG_MASK
));
1209 if (flags
!= config
.flags
) {
1210 /* Set the configuration. */
1211 config
.flags
= flags
;
1212 set_switch_config(vconn
, &config
);
1214 /* Then retrieve the configuration to see if it really took. OpenFlow
1215 * doesn't define error reporting for bad modes, so this is all we can
1217 fetch_switch_config(vconn
, &config
);
1218 if (flags
!= config
.flags
) {
1219 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1220 "switch probably doesn't support mode \"%s\")",
1221 argv
[1], ofputil_frag_handling_to_string(mode
));
1228 do_ping(int argc
, char *argv
[])
1230 size_t max_payload
= 65535 - sizeof(struct ofp_header
);
1231 unsigned int payload
;
1232 struct vconn
*vconn
;
1235 payload
= argc
> 2 ? atoi(argv
[2]) : 64;
1236 if (payload
> max_payload
) {
1237 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload
);
1240 open_vconn(argv
[1], &vconn
);
1241 for (i
= 0; i
< 10; i
++) {
1242 struct timeval start
, end
;
1243 struct ofpbuf
*request
, *reply
;
1244 struct ofp_header
*rq_hdr
, *rpy_hdr
;
1246 rq_hdr
= make_openflow(sizeof(struct ofp_header
) + payload
,
1247 OFPT_ECHO_REQUEST
, &request
);
1248 random_bytes(rq_hdr
+ 1, payload
);
1250 xgettimeofday(&start
);
1251 run(vconn_transact(vconn
, ofpbuf_clone(request
), &reply
), "transact");
1252 xgettimeofday(&end
);
1254 rpy_hdr
= reply
->data
;
1255 if (reply
->size
!= request
->size
1256 || memcmp(rpy_hdr
+ 1, rq_hdr
+ 1, payload
)
1257 || rpy_hdr
->xid
!= rq_hdr
->xid
1258 || rpy_hdr
->type
!= OFPT_ECHO_REPLY
) {
1259 printf("Reply does not match request. Request:\n");
1260 ofp_print(stdout
, request
, request
->size
, verbosity
+ 2);
1262 ofp_print(stdout
, reply
, reply
->size
, verbosity
+ 2);
1264 printf("%zu bytes from %s: xid=%08"PRIx32
" time=%.1f ms\n",
1265 reply
->size
- sizeof *rpy_hdr
, argv
[1], ntohl(rpy_hdr
->xid
),
1266 (1000*(double)(end
.tv_sec
- start
.tv_sec
))
1267 + (.001*(end
.tv_usec
- start
.tv_usec
)));
1268 ofpbuf_delete(request
);
1269 ofpbuf_delete(reply
);
1275 do_benchmark(int argc OVS_UNUSED
, char *argv
[])
1277 size_t max_payload
= 65535 - sizeof(struct ofp_header
);
1278 struct timeval start
, end
;
1279 unsigned int payload_size
, message_size
;
1280 struct vconn
*vconn
;
1285 payload_size
= atoi(argv
[2]);
1286 if (payload_size
> max_payload
) {
1287 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload
);
1289 message_size
= sizeof(struct ofp_header
) + payload_size
;
1291 count
= atoi(argv
[3]);
1293 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1294 count
, message_size
, count
* message_size
);
1296 open_vconn(argv
[1], &vconn
);
1297 xgettimeofday(&start
);
1298 for (i
= 0; i
< count
; i
++) {
1299 struct ofpbuf
*request
, *reply
;
1300 struct ofp_header
*rq_hdr
;
1302 rq_hdr
= make_openflow(message_size
, OFPT_ECHO_REQUEST
, &request
);
1303 memset(rq_hdr
+ 1, 0, payload_size
);
1304 run(vconn_transact(vconn
, request
, &reply
), "transact");
1305 ofpbuf_delete(reply
);
1307 xgettimeofday(&end
);
1310 duration
= ((1000*(double)(end
.tv_sec
- start
.tv_sec
))
1311 + (.001*(end
.tv_usec
- start
.tv_usec
)));
1312 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1313 duration
, count
/ (duration
/ 1000.0),
1314 count
* message_size
/ (duration
/ 1000.0));
1318 do_help(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1323 /* replace-flows and diff-flows commands. */
1325 /* A flow table entry, possibly with two different versions. */
1327 struct cls_rule rule
; /* Within a "struct classifier". */
1328 struct fte_version
*versions
[2];
1331 /* One version of a Flow Table Entry. */
1332 struct fte_version
{
1334 uint16_t idle_timeout
;
1335 uint16_t hard_timeout
;
1337 union ofp_action
*actions
;
1341 /* Frees 'version' and the data that it owns. */
1343 fte_version_free(struct fte_version
*version
)
1346 free(version
->actions
);
1351 /* Returns true if 'a' and 'b' are the same, false if they differ.
1353 * Ignores differences in 'flags' because there's no way to retrieve flags from
1354 * an OpenFlow switch. We have to assume that they are the same. */
1356 fte_version_equals(const struct fte_version
*a
, const struct fte_version
*b
)
1358 return (a
->cookie
== b
->cookie
1359 && a
->idle_timeout
== b
->idle_timeout
1360 && a
->hard_timeout
== b
->hard_timeout
1361 && a
->n_actions
== b
->n_actions
1362 && !memcmp(a
->actions
, b
->actions
,
1363 a
->n_actions
* sizeof *a
->actions
));
1366 /* Prints 'version' on stdout. Expects the caller to have printed the rule
1367 * associated with the version. */
1369 fte_version_print(const struct fte_version
*version
)
1373 if (version
->cookie
!= htonll(0)) {
1374 printf(" cookie=0x%"PRIx64
, ntohll(version
->cookie
));
1376 if (version
->idle_timeout
!= OFP_FLOW_PERMANENT
) {
1377 printf(" idle_timeout=%"PRIu16
, version
->idle_timeout
);
1379 if (version
->hard_timeout
!= OFP_FLOW_PERMANENT
) {
1380 printf(" hard_timeout=%"PRIu16
, version
->hard_timeout
);
1384 ofp_print_actions(&s
, version
->actions
, version
->n_actions
);
1385 printf(" %s\n", ds_cstr(&s
));
1390 fte_from_cls_rule(const struct cls_rule
*cls_rule
)
1392 return cls_rule
? CONTAINER_OF(cls_rule
, struct fte
, rule
) : NULL
;
1395 /* Frees 'fte' and its versions. */
1397 fte_free(struct fte
*fte
)
1400 fte_version_free(fte
->versions
[0]);
1401 fte_version_free(fte
->versions
[1]);
1406 /* Frees all of the FTEs within 'cls'. */
1408 fte_free_all(struct classifier
*cls
)
1410 struct cls_cursor cursor
;
1411 struct fte
*fte
, *next
;
1413 cls_cursor_init(&cursor
, cls
, NULL
);
1414 CLS_CURSOR_FOR_EACH_SAFE (fte
, next
, rule
, &cursor
) {
1415 classifier_remove(cls
, &fte
->rule
);
1420 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
1421 * necessary. Sets 'version' as the version of that rule with the given
1422 * 'index', replacing any existing version, if any.
1424 * Takes ownership of 'version'. */
1426 fte_insert(struct classifier
*cls
, const struct cls_rule
*rule
,
1427 struct fte_version
*version
, int index
)
1429 struct fte
*old
, *fte
;
1431 fte
= xzalloc(sizeof *fte
);
1433 fte
->versions
[index
] = version
;
1435 old
= fte_from_cls_rule(classifier_replace(cls
, &fte
->rule
));
1437 fte_version_free(old
->versions
[index
]);
1438 fte
->versions
[!index
] = old
->versions
[!index
];
1443 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
1444 * with the specified 'index'. Returns the flow formats able to represent the
1445 * flows that were read. */
1446 static enum ofputil_protocol
1447 read_flows_from_file(const char *filename
, struct classifier
*cls
, int index
)
1449 enum ofputil_protocol usable_protocols
;
1453 file
= !strcmp(filename
, "-") ? stdin
: fopen(filename
, "r");
1455 ovs_fatal(errno
, "%s: open", filename
);
1459 usable_protocols
= OFPUTIL_P_ANY
;
1460 while (!ds_get_preprocessed_line(&s
, file
)) {
1461 struct fte_version
*version
;
1462 struct ofputil_flow_mod fm
;
1464 parse_ofp_str(&fm
, OFPFC_ADD
, ds_cstr(&s
), true);
1466 version
= xmalloc(sizeof *version
);
1467 version
->cookie
= fm
.cookie
;
1468 version
->idle_timeout
= fm
.idle_timeout
;
1469 version
->hard_timeout
= fm
.hard_timeout
;
1470 version
->flags
= fm
.flags
& (OFPFF_SEND_FLOW_REM
| OFPFF_EMERG
);
1471 version
->actions
= fm
.actions
;
1472 version
->n_actions
= fm
.n_actions
;
1474 usable_protocols
&= ofputil_usable_protocols(&fm
.cr
);
1476 fte_insert(cls
, &fm
.cr
, version
, index
);
1480 if (file
!= stdin
) {
1484 return usable_protocols
;
1487 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
1488 * format 'protocol', and adds them as flow table entries in 'cls' for the
1489 * version with the specified 'index'. */
1491 read_flows_from_switch(struct vconn
*vconn
,
1492 enum ofputil_protocol protocol
,
1493 struct classifier
*cls
, int index
)
1495 struct ofputil_flow_stats_request fsr
;
1496 struct ofpbuf
*request
;
1500 fsr
.aggregate
= false;
1501 cls_rule_init_catchall(&fsr
.match
, 0);
1502 fsr
.out_port
= OFPP_NONE
;
1503 fsr
.table_id
= 0xff;
1504 fsr
.cookie
= fsr
.cookie_mask
= htonll(0);
1505 request
= ofputil_encode_flow_stats_request(&fsr
, protocol
);
1506 send_xid
= ((struct ofp_header
*) request
->data
)->xid
;
1507 send_openflow_buffer(vconn
, request
);
1512 struct ofpbuf
*reply
;
1514 run(vconn_recv_block(vconn
, &reply
), "OpenFlow packet receive failed");
1515 recv_xid
= ((struct ofp_header
*) reply
->data
)->xid
;
1516 if (send_xid
== recv_xid
) {
1517 const struct ofputil_msg_type
*type
;
1518 const struct ofp_stats_msg
*osm
;
1519 enum ofputil_msg_code code
;
1521 ofputil_decode_msg_type(reply
->data
, &type
);
1522 code
= ofputil_msg_type_code(type
);
1523 if (code
!= OFPUTIL_OFPST_FLOW_REPLY
&&
1524 code
!= OFPUTIL_NXST_FLOW_REPLY
) {
1525 ovs_fatal(0, "received bad reply: %s",
1526 ofp_to_string(reply
->data
, reply
->size
,
1531 if (!(osm
->flags
& htons(OFPSF_REPLY_MORE
))) {
1536 struct fte_version
*version
;
1537 struct ofputil_flow_stats fs
;
1540 retval
= ofputil_decode_flow_stats_reply(&fs
, reply
, false);
1542 if (retval
!= EOF
) {
1543 ovs_fatal(0, "parse error in reply");
1548 version
= xmalloc(sizeof *version
);
1549 version
->cookie
= fs
.cookie
;
1550 version
->idle_timeout
= fs
.idle_timeout
;
1551 version
->hard_timeout
= fs
.hard_timeout
;
1553 version
->n_actions
= fs
.n_actions
;
1554 version
->actions
= xmemdup(fs
.actions
,
1555 fs
.n_actions
* sizeof *fs
.actions
);
1557 fte_insert(cls
, &fs
.rule
, version
, index
);
1560 VLOG_DBG("received reply with xid %08"PRIx32
" "
1561 "!= expected %08"PRIx32
, recv_xid
, send_xid
);
1563 ofpbuf_delete(reply
);
1568 fte_make_flow_mod(const struct fte
*fte
, int index
, uint16_t command
,
1569 enum ofputil_protocol protocol
, struct list
*packets
)
1571 const struct fte_version
*version
= fte
->versions
[index
];
1572 struct ofputil_flow_mod fm
;
1576 fm
.cookie
= version
->cookie
;
1578 fm
.command
= command
;
1579 fm
.idle_timeout
= version
->idle_timeout
;
1580 fm
.hard_timeout
= version
->hard_timeout
;
1581 fm
.buffer_id
= UINT32_MAX
;
1582 fm
.out_port
= OFPP_NONE
;
1583 fm
.flags
= version
->flags
;
1584 if (command
== OFPFC_ADD
|| command
== OFPFC_MODIFY
||
1585 command
== OFPFC_MODIFY_STRICT
) {
1586 fm
.actions
= version
->actions
;
1587 fm
.n_actions
= version
->n_actions
;
1593 ofm
= ofputil_encode_flow_mod(&fm
, protocol
);
1594 list_push_back(packets
, &ofm
->list_node
);
1598 do_replace_flows(int argc OVS_UNUSED
, char *argv
[])
1600 enum { FILE_IDX
= 0, SWITCH_IDX
= 1 };
1601 enum ofputil_protocol usable_protocols
, protocol
;
1602 struct cls_cursor cursor
;
1603 struct classifier cls
;
1604 struct list requests
;
1605 struct vconn
*vconn
;
1608 classifier_init(&cls
);
1609 usable_protocols
= read_flows_from_file(argv
[2], &cls
, FILE_IDX
);
1611 protocol
= open_vconn(argv
[1], &vconn
);
1612 protocol
= set_protocol_for_flow_dump(vconn
, protocol
, usable_protocols
);
1614 read_flows_from_switch(vconn
, protocol
, &cls
, SWITCH_IDX
);
1616 list_init(&requests
);
1618 /* Delete flows that exist on the switch but not in the file. */
1619 cls_cursor_init(&cursor
, &cls
, NULL
);
1620 CLS_CURSOR_FOR_EACH (fte
, rule
, &cursor
) {
1621 struct fte_version
*file_ver
= fte
->versions
[FILE_IDX
];
1622 struct fte_version
*sw_ver
= fte
->versions
[SWITCH_IDX
];
1624 if (sw_ver
&& !file_ver
) {
1625 fte_make_flow_mod(fte
, SWITCH_IDX
, OFPFC_DELETE_STRICT
,
1626 protocol
, &requests
);
1630 /* Add flows that exist in the file but not on the switch.
1631 * Update flows that exist in both places but differ. */
1632 cls_cursor_init(&cursor
, &cls
, NULL
);
1633 CLS_CURSOR_FOR_EACH (fte
, rule
, &cursor
) {
1634 struct fte_version
*file_ver
= fte
->versions
[FILE_IDX
];
1635 struct fte_version
*sw_ver
= fte
->versions
[SWITCH_IDX
];
1638 && (readd
|| !sw_ver
|| !fte_version_equals(sw_ver
, file_ver
))) {
1639 fte_make_flow_mod(fte
, FILE_IDX
, OFPFC_ADD
, protocol
, &requests
);
1642 transact_multiple_noreply(vconn
, &requests
);
1649 read_flows_from_source(const char *source
, struct classifier
*cls
, int index
)
1653 if (source
[0] == '/' || source
[0] == '.'
1654 || (!strchr(source
, ':') && !stat(source
, &s
))) {
1655 read_flows_from_file(source
, cls
, index
);
1657 enum ofputil_protocol protocol
;
1658 struct vconn
*vconn
;
1660 protocol
= open_vconn(source
, &vconn
);
1661 protocol
= set_protocol_for_flow_dump(vconn
, protocol
, OFPUTIL_P_ANY
);
1662 read_flows_from_switch(vconn
, protocol
, cls
, index
);
1668 do_diff_flows(int argc OVS_UNUSED
, char *argv
[])
1670 bool differences
= false;
1671 struct cls_cursor cursor
;
1672 struct classifier cls
;
1675 classifier_init(&cls
);
1676 read_flows_from_source(argv
[1], &cls
, 0);
1677 read_flows_from_source(argv
[2], &cls
, 1);
1679 cls_cursor_init(&cursor
, &cls
, NULL
);
1680 CLS_CURSOR_FOR_EACH (fte
, rule
, &cursor
) {
1681 struct fte_version
*a
= fte
->versions
[0];
1682 struct fte_version
*b
= fte
->versions
[1];
1684 if (!a
|| !b
|| !fte_version_equals(a
, b
)) {
1685 char *rule_s
= cls_rule_to_string(&fte
->rule
);
1687 printf("-%s", rule_s
);
1688 fte_version_print(a
);
1691 printf("+%s", rule_s
);
1692 fte_version_print(b
);
1707 /* Undocumented commands for unit testing. */
1710 do_parse_flows__(struct ofputil_flow_mod
*fms
, size_t n_fms
)
1712 enum ofputil_protocol usable_protocols
;
1713 enum ofputil_protocol protocol
= 0;
1717 usable_protocols
= ofputil_flow_mod_usable_protocols(fms
, n_fms
);
1718 usable_s
= ofputil_protocols_to_string(usable_protocols
);
1719 printf("usable protocols: %s\n", usable_s
);
1722 if (!(usable_protocols
& allowed_protocols
)) {
1723 ovs_fatal(0, "no usable protocol");
1725 for (i
= 0; i
< sizeof(enum ofputil_protocol
) * CHAR_BIT
; i
++) {
1727 if (protocol
& usable_protocols
& allowed_protocols
) {
1731 assert(IS_POW2(protocol
));
1733 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol
));
1735 for (i
= 0; i
< n_fms
; i
++) {
1736 struct ofputil_flow_mod
*fm
= &fms
[i
];
1739 msg
= ofputil_encode_flow_mod(fm
, protocol
);
1740 ofp_print(stdout
, msg
->data
, msg
->size
, verbosity
);
1747 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
1748 * it back to stdout. */
1750 do_parse_flow(int argc OVS_UNUSED
, char *argv
[])
1752 struct ofputil_flow_mod fm
;
1754 parse_ofp_flow_mod_str(&fm
, argv
[1], OFPFC_ADD
, false);
1755 do_parse_flows__(&fm
, 1);
1758 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
1759 * add-flows) and prints each of the flows back to stdout. */
1761 do_parse_flows(int argc OVS_UNUSED
, char *argv
[])
1763 struct ofputil_flow_mod
*fms
= NULL
;
1766 parse_ofp_flow_mod_file(argv
[1], OFPFC_ADD
, &fms
, &n_fms
);
1767 do_parse_flows__(fms
, n_fms
);
1771 /* "parse-nx-match": reads a series of nx_match specifications as strings from
1772 * stdin, does some internal fussing with them, and then prints them back as
1773 * strings on stdout. */
1775 do_parse_nx_match(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1780 while (!ds_get_line(&in
, stdin
)) {
1781 struct ofpbuf nx_match
;
1782 struct cls_rule rule
;
1783 ovs_be64 cookie
, cookie_mask
;
1788 /* Delete comments, skip blank lines. */
1794 if (strchr(s
, '#')) {
1795 *strchr(s
, '#') = '\0';
1797 if (s
[strspn(s
, " ")] == '\0') {
1802 /* Convert string to nx_match. */
1803 ofpbuf_init(&nx_match
, 0);
1804 match_len
= nx_match_from_string(ds_cstr(&in
), &nx_match
);
1806 /* Convert nx_match to cls_rule. */
1808 error
= nx_pull_match(&nx_match
, match_len
, 0, &rule
,
1809 &cookie
, &cookie_mask
);
1811 error
= nx_pull_match_loose(&nx_match
, match_len
, 0, &rule
,
1812 &cookie
, &cookie_mask
);
1818 /* Convert cls_rule back to nx_match. */
1819 ofpbuf_uninit(&nx_match
);
1820 ofpbuf_init(&nx_match
, 0);
1821 match_len
= nx_put_match(&nx_match
, &rule
, cookie
, cookie_mask
);
1823 /* Convert nx_match to string. */
1824 out
= nx_match_to_string(nx_match
.data
, match_len
);
1828 printf("nx_pull_match() returned error %s\n",
1829 ofperr_get_name(error
));
1832 ofpbuf_uninit(&nx_match
);
1837 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
1838 * binary data, interpreting them as an OpenFlow message, and prints the
1839 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
1841 do_ofp_print(int argc
, char *argv
[])
1843 struct ofpbuf packet
;
1845 ofpbuf_init(&packet
, strlen(argv
[1]) / 2);
1846 if (ofpbuf_put_hex(&packet
, argv
[1], NULL
)[0] != '\0') {
1847 ovs_fatal(0, "trailing garbage following hex bytes");
1849 ofp_print(stdout
, packet
.data
, packet
.size
, argc
> 2 ? atoi(argv
[2]) : 2);
1850 ofpbuf_uninit(&packet
);
1853 static const struct command all_commands
[] = {
1854 { "show", 1, 1, do_show
},
1855 { "monitor", 1, 3, do_monitor
},
1856 { "snoop", 1, 1, do_snoop
},
1857 { "dump-desc", 1, 1, do_dump_desc
},
1858 { "dump-tables", 1, 1, do_dump_tables
},
1859 { "dump-flows", 1, 2, do_dump_flows
},
1860 { "dump-aggregate", 1, 2, do_dump_aggregate
},
1861 { "queue-stats", 1, 3, do_queue_stats
},
1862 { "add-flow", 2, 2, do_add_flow
},
1863 { "add-flows", 2, 2, do_add_flows
},
1864 { "mod-flows", 2, 2, do_mod_flows
},
1865 { "del-flows", 1, 2, do_del_flows
},
1866 { "replace-flows", 2, 2, do_replace_flows
},
1867 { "diff-flows", 2, 2, do_diff_flows
},
1868 { "packet-out", 4, INT_MAX
, do_packet_out
},
1869 { "dump-ports", 1, 2, do_dump_ports
},
1870 { "mod-port", 3, 3, do_mod_port
},
1871 { "get-frags", 1, 1, do_get_frags
},
1872 { "set-frags", 2, 2, do_set_frags
},
1873 { "probe", 1, 1, do_probe
},
1874 { "ping", 1, 2, do_ping
},
1875 { "benchmark", 3, 3, do_benchmark
},
1876 { "help", 0, INT_MAX
, do_help
},
1878 /* Undocumented commands for testing. */
1879 { "parse-flow", 1, 1, do_parse_flow
},
1880 { "parse-flows", 1, 1, do_parse_flows
},
1881 { "parse-nx-match", 0, 0, do_parse_nx_match
},
1882 { "ofp-print", 1, 2, do_ofp_print
},
1884 { NULL
, 0, 0, NULL
},