-
Notifications
You must be signed in to change notification settings - Fork 2
/
ovs-ofctl.c
3818 lines (3291 loc) · 117 KB
/
ovs-ofctl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <inttypes.h>
#include <sys/socket.h>
#include <net/if.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "byte-order.h"
#include "classifier.h"
#include "command-line.h"
#include "daemon.h"
#include "compiler.h"
#include "dirs.h"
#include "dynamic-string.h"
#include "fatal-signal.h"
#include "nx-match.h"
#include "odp-util.h"
#include "ofp-actions.h"
#include "ofp-errors.h"
#include "ofp-msgs.h"
#include "ofp-parse.h"
#include "ofp-print.h"
#include "ofp-util.h"
#include "ofp-version-opt.h"
#include "ofpbuf.h"
#include "ofproto/ofproto.h"
#include "openflow/nicira-ext.h"
#include "openflow/openflow.h"
#include "dp-packet.h"
#include "packets.h"
#include "pcap-file.h"
#include "poll-loop.h"
#include "random.h"
#include "stream-ssl.h"
#include "socket-util.h"
#include "timeval.h"
#include "unixctl.h"
#include "util.h"
#include "openvswitch/vconn.h"
#include "openvswitch/vlog.h"
#include "meta-flow.h"
#include "sort.h"
VLOG_DEFINE_THIS_MODULE(ofctl);
/* --bundle: Use OpenFlow 1.4 bundle for making the flow table change atomic.
* NOTE: Also the flow mod will use OpenFlow 1.4, so the semantics may be
* different (see the comment in parse_options() for details).
*/
static bool bundle = false;
/* --strict: Use strict matching for flow mod commands? Additionally governs
* use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
*/
static bool strict;
/* --readd: If true, on replace-flows, re-add even flows that have not changed
* (to reset flow counters). */
static bool readd;
/* -F, --flow-format: Allowed protocols. By default, any protocol is
* allowed. */
static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY;
/* -P, --packet-in-format: Packet IN format to use in monitor and snoop
* commands. Either one of NXPIF_* to force a particular packet_in format, or
* -1 to let ovs-ofctl choose the default. */
static int preferred_packet_in_format = -1;
/* -m, --more: Additional verbosity for ofp-print functions. */
static int verbosity;
/* --timestamp: Print a timestamp before each received packet on "monitor" and
* "snoop" command? */
static bool timestamp;
/* --unixctl-path: Path to use for unixctl server, for "monitor" and "snoop"
commands. */
static char *unixctl_path;
/* --sort, --rsort: Sort order. */
enum sort_order { SORT_ASC, SORT_DESC };
struct sort_criterion {
const struct mf_field *field; /* NULL means to sort by priority. */
enum sort_order order;
};
static struct sort_criterion *criteria;
static size_t n_criteria, allocated_criteria;
static const struct ovs_cmdl_command *get_all_commands(void);
OVS_NO_RETURN static void usage(void);
static void parse_options(int argc, char *argv[]);
static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid,
struct ofpbuf **replyp,
struct ofputil_flow_stats *,
struct ofpbuf *ofpacts);
int
main(int argc, char *argv[])
{
struct ovs_cmdl_context ctx = { .argc = 0, };
set_program_name(argv[0]);
service_start(&argc, &argv);
parse_options(argc, argv);
fatal_ignore_sigpipe();
ctx.argc = argc - optind;
ctx.argv = argv + optind;
daemon_become_new_user(false);
ovs_cmdl_run_command(&ctx, get_all_commands());
return 0;
}
static void
add_sort_criterion(enum sort_order order, const char *field)
{
struct sort_criterion *sc;
if (n_criteria >= allocated_criteria) {
criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria);
}
sc = &criteria[n_criteria++];
if (!field || !strcasecmp(field, "priority")) {
sc->field = NULL;
} else {
sc->field = mf_from_name(field);
if (!sc->field) {
ovs_fatal(0, "%s: unknown field name", field);
}
}
sc->order = order;
}
static void
parse_options(int argc, char *argv[])
{
enum {
OPT_STRICT = UCHAR_MAX + 1,
OPT_READD,
OPT_TIMESTAMP,
OPT_SORT,
OPT_RSORT,
OPT_UNIXCTL,
OPT_BUNDLE,
DAEMON_OPTION_ENUMS,
OFP_VERSION_OPTION_ENUMS,
VLOG_OPTION_ENUMS
};
static const struct option long_options[] = {
{"timeout", required_argument, NULL, 't'},
{"strict", no_argument, NULL, OPT_STRICT},
{"readd", no_argument, NULL, OPT_READD},
{"flow-format", required_argument, NULL, 'F'},
{"packet-in-format", required_argument, NULL, 'P'},
{"more", no_argument, NULL, 'm'},
{"timestamp", no_argument, NULL, OPT_TIMESTAMP},
{"sort", optional_argument, NULL, OPT_SORT},
{"rsort", optional_argument, NULL, OPT_RSORT},
{"unixctl", required_argument, NULL, OPT_UNIXCTL},
{"help", no_argument, NULL, 'h'},
{"option", no_argument, NULL, 'o'},
{"bundle", no_argument, NULL, OPT_BUNDLE},
DAEMON_LONG_OPTIONS,
OFP_VERSION_LONG_OPTIONS,
VLOG_LONG_OPTIONS,
STREAM_SSL_LONG_OPTIONS,
{NULL, 0, NULL, 0},
};
char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
uint32_t versions;
enum ofputil_protocol version_protocols;
/* For now, ovs-ofctl only enables OpenFlow 1.0 by default. This is
* because ovs-ofctl implements command such as "add-flow" as raw OpenFlow
* requests, but those requests have subtly different semantics in
* different OpenFlow versions. For example:
*
* - In OpenFlow 1.0, a "mod-flow" operation that does not find any
* existing flow to modify adds a new flow.
*
* - In OpenFlow 1.1, a "mod-flow" operation that does not find any
* existing flow to modify adds a new flow, but only if the mod-flow
* did not match on the flow cookie.
*
* - In OpenFlow 1.2 and a later, a "mod-flow" operation never adds a
* new flow.
*/
set_allowed_ofp_versions("OpenFlow10");
for (;;) {
unsigned long int timeout;
int c;
c = getopt_long(argc, argv, short_options, long_options, NULL);
if (c == -1) {
break;
}
switch (c) {
case 't':
timeout = strtoul(optarg, NULL, 10);
if (timeout <= 0) {
ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
optarg);
} else {
time_alarm(timeout);
}
break;
case 'F':
allowed_protocols = ofputil_protocols_from_string(optarg);
if (!allowed_protocols) {
ovs_fatal(0, "%s: invalid flow format(s)", optarg);
}
break;
case 'P':
preferred_packet_in_format =
ofputil_packet_in_format_from_string(optarg);
if (preferred_packet_in_format < 0) {
ovs_fatal(0, "unknown packet-in format `%s'", optarg);
}
break;
case 'm':
verbosity++;
break;
case 'h':
usage();
case 'o':
ovs_cmdl_print_options(long_options);
exit(EXIT_SUCCESS);
case OPT_BUNDLE:
bundle = true;
break;
case OPT_STRICT:
strict = true;
break;
case OPT_READD:
readd = true;
break;
case OPT_TIMESTAMP:
timestamp = true;
break;
case OPT_SORT:
add_sort_criterion(SORT_ASC, optarg);
break;
case OPT_RSORT:
add_sort_criterion(SORT_DESC, optarg);
break;
case OPT_UNIXCTL:
unixctl_path = optarg;
break;
DAEMON_OPTION_HANDLERS
OFP_VERSION_OPTION_HANDLERS
VLOG_OPTION_HANDLERS
STREAM_SSL_OPTION_HANDLERS
case '?':
exit(EXIT_FAILURE);
default:
abort();
}
}
if (n_criteria) {
/* Always do a final sort pass based on priority. */
add_sort_criterion(SORT_DESC, "priority");
}
free(short_options);
/* Implicit OpenFlow 1.4 with the '--bundle' option. */
if (bundle) {
/* Add implicit allowance for OpenFlow 1.4. */
add_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
OFPUTIL_P_OF14_OXM));
/* Remove all prior versions. */
mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
OFPUTIL_P_OF14_UP));
}
versions = get_allowed_ofp_versions();
version_protocols = ofputil_protocols_from_version_bitmap(versions);
if (!(allowed_protocols & version_protocols)) {
char *protocols = ofputil_protocols_to_string(allowed_protocols);
struct ds version_s = DS_EMPTY_INITIALIZER;
ofputil_format_version_bitmap_names(&version_s, versions);
ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports "
"any of the enabled flow formats (%s). (Use -O to enable "
"additional OpenFlow versions or -F to enable additional "
"flow formats.)", ds_cstr(&version_s), protocols);
}
allowed_protocols &= version_protocols;
mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
allowed_protocols));
}
static void
usage(void)
{
printf("%s: OpenFlow switch management utility\n"
"usage: %s [OPTIONS] COMMAND [ARG...]\n"
"\nFor OpenFlow switches:\n"
" show SWITCH show OpenFlow information\n"
" dump-desc SWITCH print switch description\n"
" dump-tables SWITCH print table stats\n"
" dump-table-features SWITCH print table features\n"
" dump-table-desc SWITCH print table description (OF1.4+)\n"
" mod-port SWITCH IFACE ACT modify port behavior\n"
" mod-table SWITCH MOD modify flow table behavior\n"
" OF1.1/1.2 MOD: controller, continue, drop\n"
" OF1.4+ MOD: evict, noevict\n"
" get-frags SWITCH print fragment handling behavior\n"
" set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
" FRAG_MODE: normal, drop, reassemble, nx-match\n"
" dump-ports SWITCH [PORT] print port statistics\n"
" dump-ports-desc SWITCH [PORT] print port descriptions\n"
" dump-flows SWITCH print all flow entries\n"
" dump-flows SWITCH FLOW print matching FLOWs\n"
" dump-aggregate SWITCH print aggregate flow statistics\n"
" dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
" queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
" add-flow SWITCH FLOW add flow described by FLOW\n"
" add-flows SWITCH FILE add flows from FILE\n"
" mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
" del-flows SWITCH [FLOW] delete matching FLOWs\n"
" replace-flows SWITCH FILE replace flows with those in FILE\n"
" diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
" packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
" execute ACTIONS on PACKET\n"
" monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
" print packets received from SWITCH\n"
" snoop SWITCH snoop on SWITCH and its controller\n"
" add-group SWITCH GROUP add group described by GROUP\n"
" add-groups SWITCH FILE add group from FILE\n"
" mod-group SWITCH GROUP modify specific group\n"
" del-groups SWITCH [GROUP] delete matching GROUPs\n"
" insert-buckets SWITCH [GROUP] add buckets to GROUP\n"
" remove-buckets SWITCH [GROUP] remove buckets from GROUP\n"
" dump-group-features SWITCH print group features\n"
" dump-groups SWITCH [GROUP] print group description\n"
" dump-group-stats SWITCH [GROUP] print group statistics\n"
" queue-get-config SWITCH PORT print queue information for port\n"
" add-meter SWITCH METER add meter described by METER\n"
" mod-meter SWITCH METER modify specific METER\n"
" del-meter SWITCH METER delete METER\n"
" del-meters SWITCH delete all meters\n"
" dump-meter SWITCH METER print METER configuration\n"
" dump-meters SWITCH print all meter configuration\n"
" meter-stats SWITCH [METER] print meter statistics\n"
" meter-features SWITCH print meter features\n"
" add-geneve-map SWITCH MAP add Geneve option MAPpings\n"
" del-geneve-map SWITCH [MAP] delete Geneve option MAPpings\n"
" dump-geneve-map SWITCH print Geneve option mappings\n"
"\nFor OpenFlow switches and controllers:\n"
" probe TARGET probe whether TARGET is up\n"
" ping TARGET [N] latency of N-byte echos\n"
" benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
"SWITCH or TARGET is an active OpenFlow connection method.\n"
"\nOther commands:\n"
" ofp-parse FILE print messages read from FILE\n"
" ofp-parse-pcap PCAP print OpenFlow read from PCAP\n",
program_name, program_name);
vconn_usage(true, false, false);
daemon_usage();
ofp_version_usage();
vlog_usage();
printf("\nOther options:\n"
" --strict use strict match for flow commands\n"
" --readd replace flows that haven't changed\n"
" -F, --flow-format=FORMAT force particular flow format\n"
" -P, --packet-in-format=FRMT force particular packet in format\n"
" -m, --more be more verbose printing OpenFlow\n"
" --timestamp (monitor, snoop) print timestamps\n"
" -t, --timeout=SECS give up after SECS seconds\n"
" --sort[=field] sort in ascending order\n"
" --rsort[=field] sort in descending order\n"
" --unixctl=SOCKET set control socket name\n"
" -h, --help display this help message\n"
" -V, --version display version information\n");
exit(EXIT_SUCCESS);
}
static void
ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
const char *argv[] OVS_UNUSED, void *exiting_)
{
bool *exiting = exiting_;
*exiting = true;
unixctl_command_reply(conn, NULL);
}
static void run(int retval, const char *message, ...)
OVS_PRINTF_FORMAT(2, 3);
static void
run(int retval, const char *message, ...)
{
if (retval) {
va_list args;
va_start(args, message);
ovs_fatal_valist(retval, message, args);
}
}
/* Generic commands. */
static int
open_vconn_socket(const char *name, struct vconn **vconnp)
{
char *vconn_name = xasprintf("unix:%s", name);
int error;
error = vconn_open(vconn_name, get_allowed_ofp_versions(), DSCP_DEFAULT,
vconnp);
if (error && error != ENOENT) {
ovs_fatal(0, "%s: failed to open socket (%s)", name,
ovs_strerror(error));
}
free(vconn_name);
return error;
}
enum open_target { MGMT, SNOOP };
static enum ofputil_protocol
open_vconn__(const char *name, enum open_target target,
struct vconn **vconnp)
{
const char *suffix = target == MGMT ? "mgmt" : "snoop";
char *datapath_name, *datapath_type, *socket_name;
enum ofputil_protocol protocol;
char *bridge_path;
int ofp_version;
int error;
bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, suffix);
ofproto_parse_name(name, &datapath_name, &datapath_type);
socket_name = xasprintf("%s/%s.%s", ovs_rundir(), datapath_name, suffix);
free(datapath_name);
free(datapath_type);
if (strchr(name, ':')) {
run(vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT, vconnp),
"connecting to %s", name);
} else if (!open_vconn_socket(name, vconnp)) {
/* Fall Through. */
} else if (!open_vconn_socket(bridge_path, vconnp)) {
/* Fall Through. */
} else if (!open_vconn_socket(socket_name, vconnp)) {
/* Fall Through. */
} else {
ovs_fatal(0, "%s is not a bridge or a socket", name);
}
if (target == SNOOP) {
vconn_set_recv_any_version(*vconnp);
}
free(bridge_path);
free(socket_name);
VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
error = vconn_connect_block(*vconnp);
if (error) {
ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
ovs_strerror(error));
}
ofp_version = vconn_get_version(*vconnp);
protocol = ofputil_protocol_from_ofp_version(ofp_version);
if (!protocol) {
ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
name, ofp_version);
}
return protocol;
}
static enum ofputil_protocol
open_vconn(const char *name, struct vconn **vconnp)
{
return open_vconn__(name, MGMT, vconnp);
}
static void
send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
{
run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
}
static void
dump_transaction(struct vconn *vconn, struct ofpbuf *request)
{
struct ofpbuf *reply;
run(vconn_transact(vconn, request, &reply), "talking to %s",
vconn_get_name(vconn));
ofp_print(stdout, reply->data, reply->size, verbosity + 1);
ofpbuf_delete(reply);
}
static void
dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
{
struct ofpbuf *request;
struct vconn *vconn;
open_vconn(vconn_name, &vconn);
request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
dump_transaction(vconn, request);
vconn_close(vconn);
}
static void
dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
{
const struct ofp_header *request_oh = request->data;
ovs_be32 send_xid = request_oh->xid;
enum ofpraw request_raw;
enum ofpraw reply_raw;
bool done = false;
ofpraw_decode_partial(&request_raw, request->data, request->size);
reply_raw = ofpraw_stats_request_to_reply(request_raw,
request_oh->version);
send_openflow_buffer(vconn, request);
while (!done) {
ovs_be32 recv_xid;
struct ofpbuf *reply;
run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
recv_xid = ((struct ofp_header *) reply->data)->xid;
if (send_xid == recv_xid) {
enum ofpraw raw;
ofp_print(stdout, reply->data, reply->size, verbosity + 1);
ofpraw_decode(&raw, reply->data);
if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
done = true;
} else if (raw == reply_raw) {
done = !ofpmp_more(reply->data);
} else {
ovs_fatal(0, "received bad reply: %s",
ofp_to_string(reply->data, reply->size,
verbosity + 1));
}
} else {
VLOG_DBG("received reply with xid %08"PRIx32" "
"!= expected %08"PRIx32, recv_xid, send_xid);
}
ofpbuf_delete(reply);
}
}
static void
dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
{
struct ofpbuf *request;
struct vconn *vconn;
open_vconn(vconn_name, &vconn);
request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
dump_stats_transaction(vconn, request);
vconn_close(vconn);
}
/* Sends all of the 'requests', which should be requests that only have replies
* if an error occurs, and waits for them to succeed or fail. If an error does
* occur, prints it and exits with an error.
*
* Destroys all of the 'requests'. */
static void
transact_multiple_noreply(struct vconn *vconn, struct ovs_list *requests)
{
struct ofpbuf *reply;
run(vconn_transact_multiple_noreply(vconn, requests, &reply),
"talking to %s", vconn_get_name(vconn));
if (reply) {
ofp_print(stderr, reply->data, reply->size, verbosity + 2);
exit(1);
}
ofpbuf_delete(reply);
}
static void
bundle_error_reporter(const struct ofp_header *oh)
{
ofp_print(stderr, oh, ntohs(oh->length), verbosity + 1);
fflush(stderr);
}
static void
bundle_transact(struct vconn *vconn, struct ovs_list *requests, uint16_t flags)
{
run(vconn_bundle_transact(vconn, requests, flags, bundle_error_reporter),
"talking to %s", vconn_get_name(vconn));
}
/* Sends 'request', which should be a request that only has a reply if an error
* occurs, and waits for it to succeed or fail. If an error does occur, prints
* it and exits with an error.
*
* Destroys 'request'. */
static void
transact_noreply(struct vconn *vconn, struct ofpbuf *request)
{
struct ovs_list requests;
list_init(&requests);
list_push_back(&requests, &request->list_node);
transact_multiple_noreply(vconn, &requests);
}
static void
fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
{
struct ofp_switch_config *config;
struct ofpbuf *request;
struct ofpbuf *reply;
enum ofptype type;
request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
vconn_get_version(vconn), 0);
run(vconn_transact(vconn, request, &reply),
"talking to %s", vconn_get_name(vconn));
if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
}
config = ofpbuf_pull(reply, sizeof *config);
*config_ = *config;
ofpbuf_delete(reply);
}
static void
set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
{
struct ofpbuf *request;
request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, vconn_get_version(vconn), 0);
ofpbuf_put(request, config, sizeof *config);
transact_noreply(vconn, request);
}
static void
ofctl_show(struct ovs_cmdl_context *ctx)
{
const char *vconn_name = ctx->argv[1];
enum ofp_version version;
struct vconn *vconn;
struct ofpbuf *request;
struct ofpbuf *reply;
bool has_ports;
open_vconn(vconn_name, &vconn);
version = vconn_get_version(vconn);
request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, version, 0);
run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
has_ports = ofputil_switch_features_has_ports(reply);
ofp_print(stdout, reply->data, reply->size, verbosity + 1);
ofpbuf_delete(reply);
if (!has_ports) {
request = ofputil_encode_port_desc_stats_request(version, OFPP_ANY);
dump_stats_transaction(vconn, request);
}
dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
vconn_close(vconn);
}
static void
ofctl_dump_desc(struct ovs_cmdl_context *ctx)
{
dump_trivial_stats_transaction(ctx->argv[1], OFPRAW_OFPST_DESC_REQUEST);
}
static void
ofctl_dump_tables(struct ovs_cmdl_context *ctx)
{
dump_trivial_stats_transaction(ctx->argv[1], OFPRAW_OFPST_TABLE_REQUEST);
}
static void
ofctl_dump_table_features(struct ovs_cmdl_context *ctx)
{
struct ofpbuf *request;
struct vconn *vconn;
open_vconn(ctx->argv[1], &vconn);
request = ofputil_encode_table_features_request(vconn_get_version(vconn));
/* The following is similar to dump_trivial_stats_transaction(), but it
* maintains the previous 'ofputil_table_features' from one stats reply
* message to the next, which allows duplication to be eliminated in the
* output across messages. Otherwise the output is much larger and harder
* to read, because only 17 or so ofputil_table_features elements fit in a
* single 64 kB OpenFlow message and therefore you get a ton of repetition
* (every 17th element is printed in full instead of abbreviated). */
const struct ofp_header *request_oh = request->data;
ovs_be32 send_xid = request_oh->xid;
bool done = false;
struct ofputil_table_features prev;
int n = 0;
send_openflow_buffer(vconn, request);
while (!done) {
ovs_be32 recv_xid;
struct ofpbuf *reply;
run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
recv_xid = ((struct ofp_header *) reply->data)->xid;
if (send_xid == recv_xid) {
enum ofptype type;
enum ofperr error;
error = ofptype_decode(&type, reply->data);
if (error) {
ovs_fatal(0, "decode error: %s", ofperr_get_name(error));
} else if (type == OFPTYPE_ERROR) {
ofp_print(stdout, reply->data, reply->size, verbosity + 1);
done = true;
} else if (type == OFPTYPE_TABLE_FEATURES_STATS_REPLY) {
done = !ofpmp_more(reply->data);
for (;;) {
struct ofputil_table_features tf;
int retval;
retval = ofputil_decode_table_features(reply, &tf, true);
if (retval) {
if (retval != EOF) {
ovs_fatal(0, "decode error: %s",
ofperr_get_name(retval));
}
break;
}
struct ds s = DS_EMPTY_INITIALIZER;
ofp_print_table_features(&s, &tf, n ? &prev : NULL,
NULL, NULL);
puts(ds_cstr(&s));
ds_destroy(&s);
prev = tf;
n++;
}
} else {
ovs_fatal(0, "received bad reply: %s",
ofp_to_string(reply->data, reply->size,
verbosity + 1));
}
} else {
VLOG_DBG("received reply with xid %08"PRIx32" "
"!= expected %08"PRIx32, recv_xid, send_xid);
}
ofpbuf_delete(reply);
}
vconn_close(vconn);
}
static void
ofctl_dump_table_desc(struct ovs_cmdl_context *ctx)
{
struct ofpbuf *request;
struct vconn *vconn;
open_vconn(ctx->argv[1], &vconn);
request = ofputil_encode_table_desc_request(vconn_get_version(vconn));
if (request) {
dump_stats_transaction(vconn, request);
}
vconn_close(vconn);
}
static bool fetch_port_by_stats(struct vconn *,
const char *port_name, ofp_port_t port_no,
struct ofputil_phy_port *);
/* Uses OFPT_FEATURES_REQUEST to attempt to fetch information about the port
* named 'port_name' or numbered 'port_no' into '*pp'. Returns true if
* successful, false on failure.
*
* This is only appropriate for OpenFlow 1.0, 1.1, and 1.2, which include a
* list of ports in OFPT_FEATURES_REPLY. */
static bool
fetch_port_by_features(struct vconn *vconn,
const char *port_name, ofp_port_t port_no,
struct ofputil_phy_port *pp)
{
struct ofputil_switch_features features;
const struct ofp_header *oh;
struct ofpbuf *request, *reply;
enum ofperr error;
enum ofptype type;
struct ofpbuf b;
bool found = false;
/* Fetch the switch's ofp_switch_features. */
request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
vconn_get_version(vconn), 0);
run(vconn_transact(vconn, request, &reply),
"talking to %s", vconn_get_name(vconn));
oh = reply->data;
if (ofptype_decode(&type, reply->data)
|| type != OFPTYPE_FEATURES_REPLY) {
ovs_fatal(0, "%s: received bad features reply", vconn_get_name(vconn));
}
if (!ofputil_switch_features_has_ports(reply)) {
/* The switch features reply does not contain a complete list of ports.
* Probably, there are more ports than will fit into a single 64 kB
* OpenFlow message. Use OFPST_PORT_DESC to get a complete list of
* ports. */
ofpbuf_delete(reply);
return fetch_port_by_stats(vconn, port_name, port_no, pp);
}
error = ofputil_decode_switch_features(oh, &features, &b);
if (error) {
ovs_fatal(0, "%s: failed to decode features reply (%s)",
vconn_get_name(vconn), ofperr_to_string(error));
}
while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
if (port_no != OFPP_NONE
? port_no == pp->port_no
: !strcmp(pp->name, port_name)) {
found = true;
break;
}
}
ofpbuf_delete(reply);
return found;
}
/* Uses a OFPST_PORT_DESC request to attempt to fetch information about the
* port named 'port_name' or numbered 'port_no' into '*pp'. Returns true if
* successful, false on failure.
*
* This is most appropriate for OpenFlow 1.3 and later. Open vSwitch 1.7 and
* later also implements OFPST_PORT_DESC, as an extension, for OpenFlow 1.0,
* 1.1, and 1.2, so this can be used as a fallback in those versions when there
* are too many ports than fit in an OFPT_FEATURES_REPLY. */
static bool
fetch_port_by_stats(struct vconn *vconn,
const char *port_name, ofp_port_t port_no,
struct ofputil_phy_port *pp)
{
struct ofpbuf *request;
ovs_be32 send_xid;
bool done = false;
bool found = false;
request = ofputil_encode_port_desc_stats_request(vconn_get_version(vconn),
port_no);
send_xid = ((struct ofp_header *) request->data)->xid;
send_openflow_buffer(vconn, request);
while (!done) {
ovs_be32 recv_xid;
struct ofpbuf *reply;
run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
recv_xid = ((struct ofp_header *) reply->data)->xid;
if (send_xid == recv_xid) {
struct ofp_header *oh = reply->data;
enum ofptype type;
struct ofpbuf b;
uint16_t flags;
ofpbuf_use_const(&b, oh, ntohs(oh->length));
if (ofptype_pull(&type, &b)
|| type != OFPTYPE_PORT_DESC_STATS_REPLY) {
ovs_fatal(0, "received bad reply: %s",
ofp_to_string(reply->data, reply->size,
verbosity + 1));
}
flags = ofpmp_flags(oh);
done = !(flags & OFPSF_REPLY_MORE);
if (found) {
/* We've already found the port, but we need to drain
* the queue of any other replies for this request. */
continue;
}
while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
if (port_no != OFPP_NONE ? port_no == pp->port_no
: !strcmp(pp->name, port_name)) {
found = true;
break;
}
}
} else {
VLOG_DBG("received reply with xid %08"PRIx32" "
"!= expected %08"PRIx32, recv_xid, send_xid);
}
ofpbuf_delete(reply);
}
return found;
}
static bool
str_to_ofp(const char *s, ofp_port_t *ofp_port)
{
bool ret;
uint32_t port_;
ret = str_to_uint(s, 10, &port_);
*ofp_port = u16_to_ofp(port_);
return ret;
}
/* Opens a connection to 'vconn_name', fetches the port structure for
* 'port_name' (which may be a port name or number), and copies it into
* '*pp'. */
static void
fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
struct ofputil_phy_port *pp)
{
struct vconn *vconn;
ofp_port_t port_no;
bool found;
/* Try to interpret the argument as a port number. */
if (!str_to_ofp(port_name, &port_no)) {
port_no = OFPP_NONE;
}
/* OpenFlow 1.0, 1.1, and 1.2 put the list of ports in the
* OFPT_FEATURES_REPLY message. OpenFlow 1.3 and later versions put it
* into the OFPST_PORT_DESC reply. Try it the correct way. */
open_vconn(vconn_name, &vconn);
found = (vconn_get_version(vconn) < OFP13_VERSION
? fetch_port_by_features(vconn, port_name, port_no, pp)
: fetch_port_by_stats(vconn, port_name, port_no, pp));
vconn_close(vconn);
if (!found) {
ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
}
}
/* Returns the port number corresponding to 'port_name' (which may be a port
* name or number) within the switch 'vconn_name'. */
static ofp_port_t
str_to_port_no(const char *vconn_name, const char *port_name)