forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ovs-ofctl.c
4500 lines (3887 loc) · 142 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-2017 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 "colors.h"
#include "compiler.h"
#include "dirs.h"
#include "dp-packet.h"
#include "fatal-signal.h"
#include "nx-match.h"
#include "odp-util.h"
#include "ofp-version-opt.h"
#include "ofproto/ofproto.h"
#include "openflow/nicira-ext.h"
#include "openflow/openflow.h"
#include "openvswitch/dynamic-string.h"
#include "openvswitch/meta-flow.h"
#include "openvswitch/ofp-actions.h"
#include "openvswitch/ofp-errors.h"
#include "openvswitch/ofp-msgs.h"
#include "openvswitch/ofp-print.h"
#include "openvswitch/ofp-util.h"
#include "openvswitch/ofp-parse.h"
#include "openvswitch/ofpbuf.h"
#include "openvswitch/vconn.h"
#include "openvswitch/vlog.h"
#include "packets.h"
#include "pcap-file.h"
#include "poll-loop.h"
#include "random.h"
#include "sort.h"
#include "stream-ssl.h"
#include "socket-util.h"
#include "timeval.h"
#include "unixctl.h"
#include "util.h"
VLOG_DEFINE_THIS_MODULE(ofctl);
/* --bundle: Use OpenFlow 1.3+ bundle for making the flow table change atomic.
* NOTE: If OpenFlow 1.3 or higher is not selected with the '-O' option,
* OpenFlow 1.4 will be implicitly selected. 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;
/* --color: Use color markers. */
static bool enable_color;
/* --read-only: Do not execute read only commands. */
static bool read_only;
/* --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;
/* --may-create: If true, the mod-group command creates a group that does not
* yet exist; otherwise, such a command has no effect. */
static bool may_create;
/* --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[]);
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);
if (read_only) {
ovs_cmdl_run_command_read_only(&ctx, get_all_commands());
} else {
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,
OPT_COLOR,
OPT_MAY_CREATE,
OPT_READ_ONLY,
DAEMON_OPTION_ENUMS,
OFP_VERSION_OPTION_ENUMS,
VLOG_OPTION_ENUMS,
SSL_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},
{"color", optional_argument, NULL, OPT_COLOR},
{"may-create", no_argument, NULL, OPT_MAY_CREATE},
{"read-only", no_argument, NULL, OPT_READ_ONLY},
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_READ_ONLY:
read_only = 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;
case OPT_COLOR:
if (optarg) {
if (!strcasecmp(optarg, "always")
|| !strcasecmp(optarg, "yes")
|| !strcasecmp(optarg, "force")) {
enable_color = true;
} else if (!strcasecmp(optarg, "never")
|| !strcasecmp(optarg, "no")
|| !strcasecmp(optarg, "none")) {
enable_color = false;
} else if (!strcasecmp(optarg, "auto")
|| !strcasecmp(optarg, "tty")
|| !strcasecmp(optarg, "if-tty")) {
/* Determine whether we need colors, i.e. whether standard
* output is a tty. */
enable_color = is_stdout_a_tty();
} else {
ovs_fatal(0, "incorrect value `%s' for --color", optarg);
}
} else {
enable_color = is_stdout_a_tty();
}
break;
case OPT_MAY_CREATE:
may_create = true;
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 && !(get_allowed_ofp_versions() &
ofputil_protocols_to_version_bitmap(OFPUTIL_P_OF13_UP))) {
/* Add implicit allowance for OpenFlow 1.4. */
add_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
OFPUTIL_P_OF14_OXM));
/* Remove all versions that do not support bundles. */
mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
OFPUTIL_P_OF13_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));
colors_init(enable_color);
}
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, vacancy:low,high, novacancy\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"
" [--may-create] 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 config 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-tlv-map SWITCH MAP add TLV option MAPpings\n"
" del-tlv-map SWITCH [MAP] delete TLV option MAPpings\n"
" dump-tlv-map SWITCH print TLV option mappings\n"
" dump-ipfix-bridge SWITCH print ipfix stats of bridge\n"
" dump-ipfix-flow SWITCH print flow ipfix of a bridge\n"
" ct-flush-zone SWITCH ZONE flush conntrack entries in ZONE\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"
" --read-only do not execute read/write 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"
" --color[=always|never|auto] control use of color in output\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)
{
const struct ofp_header *oh = request->data;
if (ofpmsg_is_stat_request(oh)) {
ovs_be32 send_xid = 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, 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);
}
} else {
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);
}
/* 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);
}
/* Frees the error messages as they are printed. */
static void
bundle_print_errors(struct ovs_list *errors, struct ovs_list *requests)
{
struct vconn_bundle_error *error, *next;
struct ofpbuf *bmsg;
INIT_CONTAINER(bmsg, requests, list_node);
LIST_FOR_EACH_SAFE (error, next, list_node, errors) {
enum ofperr ofperr;
struct ofpbuf payload;
ofperr = ofperr_decode_msg(&error->ofp_msg, &payload);
if (!ofperr) {
fprintf(stderr, "***decode error***");
} else {
/* Default to the likely truncated message. */
const struct ofp_header *ofp_msg = payload.data;
size_t msg_len = payload.size;
/* Find the failing message from the requests list to be able to
* dump the whole message. We assume the errors are returned in
* the same order as in which the messages are sent to get O(n)
* rather than O(n^2) processing here. If this heuristics fails we
* may print the truncated hexdumps instead. */
LIST_FOR_EACH_CONTINUE (bmsg, list_node, requests) {
const struct ofp_header *oh = bmsg->data;
if (oh->xid == error->ofp_msg.xid) {
ofp_msg = oh;
msg_len = bmsg->size;
break;
}
}
fprintf(stderr, "Error %s for: ", ofperr_get_name(ofperr));
ofp_print(stderr, ofp_msg, msg_len, verbosity + 1);
}
ofpbuf_uninit(&payload);
free(error);
}
fflush(stderr);
}
static void
bundle_transact(struct vconn *vconn, struct ovs_list *requests, uint16_t flags)
{
struct ovs_list errors;
int retval = vconn_bundle_transact(vconn, requests, flags, &errors);
bundle_print_errors(&errors, requests);
if (retval) {
ovs_fatal(retval, "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;
ovs_list_init(&requests);
ovs_list_push_back(&requests, &request->list_node);
transact_multiple_noreply(vconn, &requests);
}
static void
fetch_switch_config(struct vconn *vconn, struct ofputil_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_decode(&type, reply->data)
|| type != OFPTYPE_GET_CONFIG_REPLY) {
ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
}
ofputil_decode_get_config_reply(reply->data, config);
ofpbuf_delete(reply);
}
static void
set_switch_config(struct vconn *vconn,
const struct ofputil_switch_config *config)
{
enum ofp_version version = vconn_get_version(vconn);
transact_noreply(vconn, ofputil_encode_set_config(config, version));
}
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_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_transaction(ctx->argv[1], OFPRAW_OFPST_DESC_REQUEST);
}
static void
ofctl_dump_tables(struct ovs_cmdl_context *ctx)
{
dump_trivial_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_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_transaction(vconn, request);
}
vconn_close(vconn);
}
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;
}
struct port_iterator {
struct vconn *vconn;
enum { PI_FEATURES, PI_PORT_DESC } variant;
struct ofpbuf *reply;
ovs_be32 send_xid;
bool more;
};
static void
port_iterator_fetch_port_desc(struct port_iterator *pi)
{
pi->variant = PI_PORT_DESC;
pi->more = true;
struct ofpbuf *rq = ofputil_encode_port_desc_stats_request(
vconn_get_version(pi->vconn), OFPP_ANY);
pi->send_xid = ((struct ofp_header *) rq->data)->xid;
send_openflow_buffer(pi->vconn, rq);
}
static void
port_iterator_fetch_features(struct port_iterator *pi)
{
pi->variant = PI_FEATURES;
/* Fetch the switch's ofp_switch_features. */
enum ofp_version version = vconn_get_version(pi->vconn);
struct ofpbuf *rq = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, version, 0);
run(vconn_transact(pi->vconn, rq, &pi->reply),
"talking to %s", vconn_get_name(pi->vconn));
enum ofptype type;
if (ofptype_decode(&type, pi->reply->data)
|| type != OFPTYPE_FEATURES_REPLY) {
ovs_fatal(0, "%s: received bad features reply",
vconn_get_name(pi->vconn));
}
if (!ofputil_switch_features_has_ports(pi->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(pi->reply);
pi->reply = NULL;
port_iterator_fetch_port_desc(pi);
return;
}
struct ofputil_switch_features features;
enum ofperr error = ofputil_pull_switch_features(pi->reply, &features);
if (error) {
ovs_fatal(0, "%s: failed to decode features reply (%s)",
vconn_get_name(pi->vconn), ofperr_to_string(error));
}
}
/* Initializes 'pi' to prepare for iterating through all of the ports on the
* OpenFlow switch to which 'vconn' is connected.
*
* During iteration, the client should not make other use of 'vconn', because
* that can cause other messages to be interleaved with the replies used by the
* iterator and thus some ports may be missed or a hang can occur. */
static void
port_iterator_init(struct port_iterator *pi, struct vconn *vconn)
{
memset(pi, 0, sizeof *pi);
pi->vconn = vconn;
if (vconn_get_version(vconn) < OFP13_VERSION) {
port_iterator_fetch_features(pi);
} else {
port_iterator_fetch_port_desc(pi);
}
}
/* Obtains the next port from 'pi'. On success, initializes '*pp' with the
* port's details and returns true, otherwise (if all the ports have already
* been seen), returns false. */
static bool
port_iterator_next(struct port_iterator *pi, struct ofputil_phy_port *pp)
{