forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathofp-print.c
3456 lines (2952 loc) · 100 KB
/
ofp-print.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 "ofp-print.h"
#include <errno.h>
#include <inttypes.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include "bundle.h"
#include "byte-order.h"
#include "compiler.h"
#include "dynamic-string.h"
#include "flow.h"
#include "learn.h"
#include "multipath.h"
#include "meta-flow.h"
#include "netdev.h"
#include "nx-match.h"
#include "ofp-actions.h"
#include "ofpbuf.h"
#include "ofp-errors.h"
#include "ofp-msgs.h"
#include "ofp-util.h"
#include "openflow/openflow.h"
#include "openflow/nicira-ext.h"
#include "packets.h"
#include "dp-packet.h"
#include "type-props.h"
#include "unaligned.h"
#include "odp-util.h"
#include "util.h"
static void ofp_print_queue_name(struct ds *string, uint32_t port);
static void ofp_print_error(struct ds *, enum ofperr);
/* Returns a string that represents the contents of the Ethernet frame in the
* 'len' bytes starting at 'data'. The caller must free the returned string.*/
char *
ofp_packet_to_string(const void *data, size_t len)
{
struct ds ds = DS_EMPTY_INITIALIZER;
struct dp_packet buf;
struct flow flow;
size_t l4_size;
dp_packet_use_const(&buf, data, len);
flow_extract(&buf, &flow);
flow_format(&ds, &flow);
l4_size = dp_packet_l4_size(&buf);
if (flow.nw_proto == IPPROTO_TCP && l4_size >= TCP_HEADER_LEN) {
struct tcp_header *th = dp_packet_l4(&buf);
ds_put_format(&ds, " tcp_csum:%"PRIx16, ntohs(th->tcp_csum));
} else if (flow.nw_proto == IPPROTO_UDP && l4_size >= UDP_HEADER_LEN) {
struct udp_header *uh = dp_packet_l4(&buf);
ds_put_format(&ds, " udp_csum:%"PRIx16, ntohs(uh->udp_csum));
} else if (flow.nw_proto == IPPROTO_SCTP && l4_size >= SCTP_HEADER_LEN) {
struct sctp_header *sh = dp_packet_l4(&buf);
ds_put_format(&ds, " sctp_csum:%"PRIx32,
ntohl(get_16aligned_be32(&sh->sctp_csum)));
} else if (flow.nw_proto == IPPROTO_ICMP && l4_size >= ICMP_HEADER_LEN) {
struct icmp_header *icmph = dp_packet_l4(&buf);
ds_put_format(&ds, " icmp_csum:%"PRIx16,
ntohs(icmph->icmp_csum));
} else if (flow.nw_proto == IPPROTO_ICMPV6 && l4_size >= ICMP6_HEADER_LEN) {
struct icmp6_header *icmp6h = dp_packet_l4(&buf);
ds_put_format(&ds, " icmp6_csum:%"PRIx16,
ntohs(icmp6h->icmp6_cksum));
}
ds_put_char(&ds, '\n');
return ds_cstr(&ds);
}
static void
ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
int verbosity)
{
char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
struct ofputil_packet_in pin;
int error;
error = ofputil_decode_packet_in(&pin, oh);
if (error) {
ofp_print_error(string, error);
return;
}
if (pin.table_id) {
ds_put_format(string, " table_id=%"PRIu8, pin.table_id);
}
if (pin.cookie != OVS_BE64_MAX) {
ds_put_format(string, " cookie=0x%"PRIx64, ntohll(pin.cookie));
}
ds_put_format(string, " total_len=%"PRIuSIZE" ", pin.total_len);
match_format(&pin.flow_metadata, string, OFP_DEFAULT_PRIORITY);
ds_put_format(string, " (via %s)",
ofputil_packet_in_reason_to_string(pin.reason, reasonbuf,
sizeof reasonbuf));
ds_put_format(string, " data_len=%"PRIuSIZE, pin.packet_len);
if (pin.buffer_id == UINT32_MAX) {
ds_put_format(string, " (unbuffered)");
if (pin.total_len != pin.packet_len) {
ds_put_format(string, " (***total_len != data_len***)");
}
} else {
ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
if (pin.total_len < pin.packet_len) {
ds_put_format(string, " (***total_len < data_len***)");
}
}
ds_put_char(string, '\n');
if (verbosity > 0) {
char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
ds_put_cstr(string, packet);
free(packet);
}
if (verbosity > 2) {
ds_put_hex_dump(string, pin.packet, pin.packet_len, 0, false);
}
}
static void
ofp_print_packet_out(struct ds *string, const struct ofp_header *oh,
int verbosity)
{
struct ofputil_packet_out po;
struct ofpbuf ofpacts;
enum ofperr error;
ofpbuf_init(&ofpacts, 64);
error = ofputil_decode_packet_out(&po, oh, &ofpacts);
if (error) {
ofpbuf_uninit(&ofpacts);
ofp_print_error(string, error);
return;
}
ds_put_cstr(string, " in_port=");
ofputil_format_port(po.in_port, string);
ds_put_cstr(string, " actions=");
ofpacts_format(po.ofpacts, po.ofpacts_len, string);
if (po.buffer_id == UINT32_MAX) {
ds_put_format(string, " data_len=%"PRIuSIZE, po.packet_len);
if (verbosity > 0 && po.packet_len > 0) {
char *packet = ofp_packet_to_string(po.packet, po.packet_len);
ds_put_char(string, '\n');
ds_put_cstr(string, packet);
free(packet);
}
if (verbosity > 2) {
ds_put_hex_dump(string, po.packet, po.packet_len, 0, false);
}
} else {
ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id);
}
ofpbuf_uninit(&ofpacts);
}
/* qsort comparison function. */
static int
compare_ports(const void *a_, const void *b_)
{
const struct ofputil_phy_port *a = a_;
const struct ofputil_phy_port *b = b_;
uint16_t ap = ofp_to_u16(a->port_no);
uint16_t bp = ofp_to_u16(b->port_no);
return ap < bp ? -1 : ap > bp;
}
static void
ofp_print_bit_names(struct ds *string, uint32_t bits,
const char *(*bit_to_name)(uint32_t bit),
char separator)
{
int n = 0;
int i;
if (!bits) {
ds_put_cstr(string, "0");
return;
}
for (i = 0; i < 32; i++) {
uint32_t bit = UINT32_C(1) << i;
if (bits & bit) {
const char *name = bit_to_name(bit);
if (name) {
if (n++) {
ds_put_char(string, separator);
}
ds_put_cstr(string, name);
bits &= ~bit;
}
}
}
if (bits) {
if (n) {
ds_put_char(string, separator);
}
ds_put_format(string, "0x%"PRIx32, bits);
}
}
static const char *
netdev_feature_to_name(uint32_t bit)
{
enum netdev_features f = bit;
switch (f) {
case NETDEV_F_10MB_HD: return "10MB-HD";
case NETDEV_F_10MB_FD: return "10MB-FD";
case NETDEV_F_100MB_HD: return "100MB-HD";
case NETDEV_F_100MB_FD: return "100MB-FD";
case NETDEV_F_1GB_HD: return "1GB-HD";
case NETDEV_F_1GB_FD: return "1GB-FD";
case NETDEV_F_10GB_FD: return "10GB-FD";
case NETDEV_F_40GB_FD: return "40GB-FD";
case NETDEV_F_100GB_FD: return "100GB-FD";
case NETDEV_F_1TB_FD: return "1TB-FD";
case NETDEV_F_OTHER: return "OTHER";
case NETDEV_F_COPPER: return "COPPER";
case NETDEV_F_FIBER: return "FIBER";
case NETDEV_F_AUTONEG: return "AUTO_NEG";
case NETDEV_F_PAUSE: return "AUTO_PAUSE";
case NETDEV_F_PAUSE_ASYM: return "AUTO_PAUSE_ASYM";
}
return NULL;
}
static void
ofp_print_port_features(struct ds *string, enum netdev_features features)
{
ofp_print_bit_names(string, features, netdev_feature_to_name, ' ');
ds_put_char(string, '\n');
}
static const char *
ofputil_port_config_to_name(uint32_t bit)
{
enum ofputil_port_config pc = bit;
switch (pc) {
case OFPUTIL_PC_PORT_DOWN: return "PORT_DOWN";
case OFPUTIL_PC_NO_STP: return "NO_STP";
case OFPUTIL_PC_NO_RECV: return "NO_RECV";
case OFPUTIL_PC_NO_RECV_STP: return "NO_RECV_STP";
case OFPUTIL_PC_NO_FLOOD: return "NO_FLOOD";
case OFPUTIL_PC_NO_FWD: return "NO_FWD";
case OFPUTIL_PC_NO_PACKET_IN: return "NO_PACKET_IN";
}
return NULL;
}
static void
ofp_print_port_config(struct ds *string, enum ofputil_port_config config)
{
ofp_print_bit_names(string, config, ofputil_port_config_to_name, ' ');
ds_put_char(string, '\n');
}
static const char *
ofputil_port_state_to_name(uint32_t bit)
{
enum ofputil_port_state ps = bit;
switch (ps) {
case OFPUTIL_PS_LINK_DOWN: return "LINK_DOWN";
case OFPUTIL_PS_BLOCKED: return "BLOCKED";
case OFPUTIL_PS_LIVE: return "LIVE";
case OFPUTIL_PS_STP_LISTEN:
case OFPUTIL_PS_STP_LEARN:
case OFPUTIL_PS_STP_FORWARD:
case OFPUTIL_PS_STP_BLOCK:
/* Handled elsewhere. */
return NULL;
}
return NULL;
}
static void
ofp_print_port_state(struct ds *string, enum ofputil_port_state state)
{
enum ofputil_port_state stp_state;
/* The STP state is a 2-bit field so it doesn't fit in with the bitmask
* pattern. We have to special case it.
*
* OVS doesn't support STP, so this field will always be 0 if we are
* talking to OVS, so we'd always print STP_LISTEN in that case.
* Therefore, we don't print anything at all if the value is STP_LISTEN, to
* avoid confusing users. */
stp_state = state & OFPUTIL_PS_STP_MASK;
if (stp_state) {
ds_put_cstr(string,
(stp_state == OFPUTIL_PS_STP_LEARN ? "STP_LEARN"
: stp_state == OFPUTIL_PS_STP_FORWARD ? "STP_FORWARD"
: "STP_BLOCK"));
state &= ~OFPUTIL_PS_STP_MASK;
if (state) {
ofp_print_bit_names(string, state, ofputil_port_state_to_name,
' ');
}
} else {
ofp_print_bit_names(string, state, ofputil_port_state_to_name, ' ');
}
ds_put_char(string, '\n');
}
static void
ofp_print_phy_port(struct ds *string, const struct ofputil_phy_port *port)
{
char name[sizeof port->name];
int j;
memcpy(name, port->name, sizeof name);
for (j = 0; j < sizeof name - 1; j++) {
if (!isprint((unsigned char) name[j])) {
break;
}
}
name[j] = '\0';
ds_put_char(string, ' ');
ofputil_format_port(port->port_no, string);
ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
name, ETH_ADDR_ARGS(port->hw_addr));
ds_put_cstr(string, " config: ");
ofp_print_port_config(string, port->config);
ds_put_cstr(string, " state: ");
ofp_print_port_state(string, port->state);
if (port->curr) {
ds_put_format(string, " current: ");
ofp_print_port_features(string, port->curr);
}
if (port->advertised) {
ds_put_format(string, " advertised: ");
ofp_print_port_features(string, port->advertised);
}
if (port->supported) {
ds_put_format(string, " supported: ");
ofp_print_port_features(string, port->supported);
}
if (port->peer) {
ds_put_format(string, " peer: ");
ofp_print_port_features(string, port->peer);
}
ds_put_format(string, " speed: %"PRIu32" Mbps now, "
"%"PRIu32" Mbps max\n",
port->curr_speed / UINT32_C(1000),
port->max_speed / UINT32_C(1000));
}
/* Given a buffer 'b' that contains an array of OpenFlow ports of type
* 'ofp_version', writes a detailed description of each port into
* 'string'. */
static void
ofp_print_phy_ports(struct ds *string, uint8_t ofp_version,
struct ofpbuf *b)
{
struct ofputil_phy_port *ports;
size_t allocated_ports, n_ports;
int retval;
size_t i;
ports = NULL;
allocated_ports = 0;
for (n_ports = 0; ; n_ports++) {
if (n_ports >= allocated_ports) {
ports = x2nrealloc(ports, &allocated_ports, sizeof *ports);
}
retval = ofputil_pull_phy_port(ofp_version, b, &ports[n_ports]);
if (retval) {
break;
}
}
qsort(ports, n_ports, sizeof *ports, compare_ports);
for (i = 0; i < n_ports; i++) {
ofp_print_phy_port(string, &ports[i]);
}
free(ports);
if (retval != EOF) {
ofp_print_error(string, retval);
}
}
static const char *
ofputil_capabilities_to_name(uint32_t bit)
{
enum ofputil_capabilities capabilities = bit;
switch (capabilities) {
case OFPUTIL_C_FLOW_STATS: return "FLOW_STATS";
case OFPUTIL_C_TABLE_STATS: return "TABLE_STATS";
case OFPUTIL_C_PORT_STATS: return "PORT_STATS";
case OFPUTIL_C_IP_REASM: return "IP_REASM";
case OFPUTIL_C_QUEUE_STATS: return "QUEUE_STATS";
case OFPUTIL_C_ARP_MATCH_IP: return "ARP_MATCH_IP";
case OFPUTIL_C_STP: return "STP";
case OFPUTIL_C_GROUP_STATS: return "GROUP_STATS";
case OFPUTIL_C_PORT_BLOCKED: return "PORT_BLOCKED";
}
return NULL;
}
static void
ofp_print_switch_features(struct ds *string, const struct ofp_header *oh)
{
struct ofputil_switch_features features;
enum ofperr error;
struct ofpbuf b;
error = ofputil_decode_switch_features(oh, &features, &b);
if (error) {
ofp_print_error(string, error);
return;
}
ds_put_format(string, " dpid:%016"PRIx64"\n", features.datapath_id);
ds_put_format(string, "n_tables:%"PRIu8", n_buffers:%"PRIu32,
features.n_tables, features.n_buffers);
if (features.auxiliary_id) {
ds_put_format(string, ", auxiliary_id:%"PRIu8, features.auxiliary_id);
}
ds_put_char(string, '\n');
ds_put_cstr(string, "capabilities: ");
ofp_print_bit_names(string, features.capabilities,
ofputil_capabilities_to_name, ' ');
ds_put_char(string, '\n');
switch ((enum ofp_version)oh->version) {
case OFP10_VERSION:
ds_put_cstr(string, "actions: ");
ofpact_bitmap_format(features.ofpacts, string);
ds_put_char(string, '\n');
break;
case OFP11_VERSION:
case OFP12_VERSION:
break;
case OFP13_VERSION:
case OFP14_VERSION:
case OFP15_VERSION:
return; /* no ports in ofp13_switch_features */
default:
OVS_NOT_REACHED();
}
ofp_print_phy_ports(string, oh->version, &b);
}
static void
ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
{
enum ofp_config_flags flags;
flags = ntohs(osc->flags);
ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
flags &= ~OFPC_FRAG_MASK;
if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) {
ds_put_format(string, " invalid_ttl_to_controller");
flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER;
}
if (flags) {
ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
}
ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
}
static void print_wild(struct ds *string, const char *leader, int is_wild,
int verbosity, const char *format, ...)
OVS_PRINTF_FORMAT(5, 6);
static void print_wild(struct ds *string, const char *leader, int is_wild,
int verbosity, const char *format, ...)
{
if (is_wild && verbosity < 2) {
return;
}
ds_put_cstr(string, leader);
if (!is_wild) {
va_list args;
va_start(args, format);
ds_put_format_valist(string, format, args);
va_end(args);
} else {
ds_put_char(string, '*');
}
ds_put_char(string, ',');
}
static void
print_wild_port(struct ds *string, const char *leader, int is_wild,
int verbosity, ofp_port_t port)
{
if (is_wild && verbosity < 2) {
return;
}
ds_put_cstr(string, leader);
if (!is_wild) {
ofputil_format_port(port, string);
} else {
ds_put_char(string, '*');
}
ds_put_char(string, ',');
}
static void
print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
uint32_t wild_bits, int verbosity)
{
if (wild_bits >= 32 && verbosity < 2) {
return;
}
ds_put_cstr(string, leader);
if (wild_bits < 32) {
ds_put_format(string, IP_FMT, IP_ARGS(ip));
if (wild_bits) {
ds_put_format(string, "/%d", 32 - wild_bits);
}
} else {
ds_put_char(string, '*');
}
ds_put_char(string, ',');
}
void
ofp10_match_print(struct ds *f, const struct ofp10_match *om, int verbosity)
{
char *s = ofp10_match_to_string(om, verbosity);
ds_put_cstr(f, s);
free(s);
}
char *
ofp10_match_to_string(const struct ofp10_match *om, int verbosity)
{
struct ds f = DS_EMPTY_INITIALIZER;
uint32_t w = ntohl(om->wildcards);
bool skip_type = false;
bool skip_proto = false;
if (!(w & OFPFW10_DL_TYPE)) {
skip_type = true;
if (om->dl_type == htons(ETH_TYPE_IP)) {
if (!(w & OFPFW10_NW_PROTO)) {
skip_proto = true;
if (om->nw_proto == IPPROTO_ICMP) {
ds_put_cstr(&f, "icmp,");
} else if (om->nw_proto == IPPROTO_TCP) {
ds_put_cstr(&f, "tcp,");
} else if (om->nw_proto == IPPROTO_UDP) {
ds_put_cstr(&f, "udp,");
} else if (om->nw_proto == IPPROTO_SCTP) {
ds_put_cstr(&f, "sctp,");
} else {
ds_put_cstr(&f, "ip,");
skip_proto = false;
}
} else {
ds_put_cstr(&f, "ip,");
}
} else if (om->dl_type == htons(ETH_TYPE_ARP)) {
ds_put_cstr(&f, "arp,");
} else if (om->dl_type == htons(ETH_TYPE_RARP)){
ds_put_cstr(&f, "rarp,");
} else if (om->dl_type == htons(ETH_TYPE_MPLS)) {
ds_put_cstr(&f, "mpls,");
} else if (om->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
ds_put_cstr(&f, "mplsm,");
} else {
skip_type = false;
}
}
print_wild_port(&f, "in_port=", w & OFPFW10_IN_PORT, verbosity,
u16_to_ofp(ntohs(om->in_port)));
print_wild(&f, "dl_vlan=", w & OFPFW10_DL_VLAN, verbosity,
"%d", ntohs(om->dl_vlan));
print_wild(&f, "dl_vlan_pcp=", w & OFPFW10_DL_VLAN_PCP, verbosity,
"%d", om->dl_vlan_pcp);
print_wild(&f, "dl_src=", w & OFPFW10_DL_SRC, verbosity,
ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
print_wild(&f, "dl_dst=", w & OFPFW10_DL_DST, verbosity,
ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
if (!skip_type) {
print_wild(&f, "dl_type=", w & OFPFW10_DL_TYPE, verbosity,
"0x%04x", ntohs(om->dl_type));
}
print_ip_netmask(&f, "nw_src=", om->nw_src,
(w & OFPFW10_NW_SRC_MASK) >> OFPFW10_NW_SRC_SHIFT,
verbosity);
print_ip_netmask(&f, "nw_dst=", om->nw_dst,
(w & OFPFW10_NW_DST_MASK) >> OFPFW10_NW_DST_SHIFT,
verbosity);
if (!skip_proto) {
if (om->dl_type == htons(ETH_TYPE_ARP) ||
om->dl_type == htons(ETH_TYPE_RARP)) {
print_wild(&f, "arp_op=", w & OFPFW10_NW_PROTO, verbosity,
"%u", om->nw_proto);
} else {
print_wild(&f, "nw_proto=", w & OFPFW10_NW_PROTO, verbosity,
"%u", om->nw_proto);
}
}
print_wild(&f, "nw_tos=", w & OFPFW10_NW_TOS, verbosity,
"%u", om->nw_tos);
if (om->nw_proto == IPPROTO_ICMP) {
print_wild(&f, "icmp_type=", w & OFPFW10_ICMP_TYPE, verbosity,
"%d", ntohs(om->tp_src));
print_wild(&f, "icmp_code=", w & OFPFW10_ICMP_CODE, verbosity,
"%d", ntohs(om->tp_dst));
} else {
print_wild(&f, "tp_src=", w & OFPFW10_TP_SRC, verbosity,
"%d", ntohs(om->tp_src));
print_wild(&f, "tp_dst=", w & OFPFW10_TP_DST, verbosity,
"%d", ntohs(om->tp_dst));
}
ds_chomp(&f, ',');
return ds_cstr(&f);
}
static void
ofp_print_flow_flags(struct ds *s, enum ofputil_flow_mod_flags flags)
{
if (flags & OFPUTIL_FF_SEND_FLOW_REM) {
ds_put_cstr(s, "send_flow_rem ");
}
if (flags & OFPUTIL_FF_CHECK_OVERLAP) {
ds_put_cstr(s, "check_overlap ");
}
if (flags & OFPUTIL_FF_RESET_COUNTS) {
ds_put_cstr(s, "reset_counts ");
}
if (flags & OFPUTIL_FF_NO_PKT_COUNTS) {
ds_put_cstr(s, "no_packet_counts ");
}
if (flags & OFPUTIL_FF_NO_BYT_COUNTS) {
ds_put_cstr(s, "no_byte_counts ");
}
if (flags & OFPUTIL_FF_HIDDEN_FIELDS) {
ds_put_cstr(s, "allow_hidden_fields ");
}
if (flags & OFPUTIL_FF_NO_READONLY) {
ds_put_cstr(s, "no_readonly_table ");
}
}
static void
ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh, int verbosity)
{
struct ofputil_flow_mod fm;
struct ofpbuf ofpacts;
bool need_priority;
enum ofperr error;
enum ofpraw raw;
enum ofputil_protocol protocol;
protocol = ofputil_protocol_from_ofp_version(oh->version);
protocol = ofputil_protocol_set_tid(protocol, true);
ofpbuf_init(&ofpacts, 64);
error = ofputil_decode_flow_mod(&fm, oh, protocol, &ofpacts,
OFPP_MAX, 255);
if (error) {
ofpbuf_uninit(&ofpacts);
ofp_print_error(s, error);
return;
}
ds_put_char(s, ' ');
switch (fm.command) {
case OFPFC_ADD:
ds_put_cstr(s, "ADD");
break;
case OFPFC_MODIFY:
ds_put_cstr(s, "MOD");
break;
case OFPFC_MODIFY_STRICT:
ds_put_cstr(s, "MOD_STRICT");
break;
case OFPFC_DELETE:
ds_put_cstr(s, "DEL");
break;
case OFPFC_DELETE_STRICT:
ds_put_cstr(s, "DEL_STRICT");
break;
default:
ds_put_format(s, "cmd:%d", fm.command);
}
if (fm.table_id != 0) {
ds_put_format(s, " table:%d", fm.table_id);
}
ds_put_char(s, ' ');
ofpraw_decode(&raw, oh);
if (verbosity >= 3 && raw == OFPRAW_OFPT10_FLOW_MOD) {
const struct ofp10_flow_mod *ofm = ofpmsg_body(oh);
ofp10_match_print(s, &ofm->match, verbosity);
/* ofp_print_match() doesn't print priority. */
need_priority = true;
} else if (verbosity >= 3 && raw == OFPRAW_NXT_FLOW_MOD) {
const struct nx_flow_mod *nfm = ofpmsg_body(oh);
const void *nxm = nfm + 1;
char *nxm_s;
nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
ds_put_cstr(s, nxm_s);
free(nxm_s);
/* nx_match_to_string() doesn't print priority. */
need_priority = true;
} else {
match_format(&fm.match, s, fm.priority);
/* match_format() does print priority. */
need_priority = false;
}
if (ds_last(s) != ' ') {
ds_put_char(s, ' ');
}
if (fm.new_cookie != htonll(0) && fm.new_cookie != OVS_BE64_MAX) {
ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.new_cookie));
}
if (fm.cookie_mask != htonll(0)) {
ds_put_format(s, "cookie:0x%"PRIx64"/0x%"PRIx64" ",
ntohll(fm.cookie), ntohll(fm.cookie_mask));
}
if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
}
if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
}
if (fm.importance != 0) {
ds_put_format(s, "importance:%"PRIu16" ", fm.importance);
}
if (fm.priority != OFP_DEFAULT_PRIORITY && need_priority) {
ds_put_format(s, "pri:%"PRIu16" ", fm.priority);
}
if (fm.buffer_id != UINT32_MAX) {
ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
}
if (fm.out_port != OFPP_ANY) {
ds_put_format(s, "out_port:");
ofputil_format_port(fm.out_port, s);
ds_put_char(s, ' ');
}
if (oh->version == OFP10_VERSION || oh->version == OFP11_VERSION) {
/* Don't print the reset_counts flag for OF1.0 and OF1.1 because those
* versions don't really have such a flag and printing one is likely to
* confuse people. */
fm.flags &= ~OFPUTIL_FF_RESET_COUNTS;
}
ofp_print_flow_flags(s, fm.flags);
ds_put_cstr(s, "actions=");
ofpacts_format(fm.ofpacts, fm.ofpacts_len, s);
ofpbuf_uninit(&ofpacts);
}
static void
ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
{
ds_put_format(string, "%u", sec);
/* If there are no fractional seconds, don't print any decimals.
*
* If the fractional seconds can be expressed exactly as milliseconds,
* print 3 decimals. Open vSwitch provides millisecond precision for most
* time measurements, so printing 3 decimals every time makes it easier to
* spot real changes in flow dumps that refresh themselves quickly.
*
* If the fractional seconds are more precise than milliseconds, print the
* number of decimals needed to express them exactly.
*/
if (nsec > 0) {
unsigned int msec = nsec / 1000000;
if (msec * 1000000 == nsec) {
ds_put_format(string, ".%03u", msec);
} else {
ds_put_format(string, ".%09u", nsec);
while (string->string[string->length - 1] == '0') {
string->length--;
}
}
}
ds_put_char(string, 's');
}
/* Returns a string form of 'reason'. The return value is either a statically
* allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
* 'bufsize' should be at least OFP_FLOW_REMOVED_REASON_BUFSIZE. */
#define OFP_FLOW_REMOVED_REASON_BUFSIZE (INT_STRLEN(int) + 1)
static const char *
ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason,
char *reasonbuf, size_t bufsize)
{
switch (reason) {
case OFPRR_IDLE_TIMEOUT:
return "idle";
case OFPRR_HARD_TIMEOUT:
return "hard";
case OFPRR_DELETE:
return "delete";
case OFPRR_GROUP_DELETE:
return "group_delete";
case OFPRR_EVICTION:
return "eviction";
case OFPRR_METER_DELETE:
return "meter_delete";
case OVS_OFPRR_NONE:
default:
snprintf(reasonbuf, bufsize, "%d", (int) reason);
return reasonbuf;
}
}
static void
ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
{
char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
struct ofputil_flow_removed fr;
enum ofperr error;
error = ofputil_decode_flow_removed(&fr, oh);
if (error) {
ofp_print_error(string, error);
return;
}
ds_put_char(string, ' ');
match_format(&fr.match, string, fr.priority);
ds_put_format(string, " reason=%s",
ofp_flow_removed_reason_to_string(fr.reason, reasonbuf,
sizeof reasonbuf));
if (fr.table_id != 255) {
ds_put_format(string, " table_id=%"PRIu8, fr.table_id);
}
if (fr.cookie != htonll(0)) {
ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
}
ds_put_cstr(string, " duration");
ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
ds_put_format(string, " idle%"PRIu16, fr.idle_timeout);
if (fr.hard_timeout) {
/* The hard timeout was only added in OF1.2, so only print it if it is
* actually in use to avoid gratuitous change to the formatting. */
ds_put_format(string, " hard%"PRIu16, fr.hard_timeout);
}
ds_put_format(string, " pkts%"PRIu64" bytes%"PRIu64"\n",
fr.packet_count, fr.byte_count);
}
static void
ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
{
struct ofputil_port_mod pm;
enum ofperr error;
error = ofputil_decode_port_mod(oh, &pm, true);
if (error) {
ofp_print_error(string, error);
return;
}
ds_put_cstr(string, "port: ");
ofputil_format_port(pm.port_no, string);
ds_put_format(string, ": addr:"ETH_ADDR_FMT"\n",
ETH_ADDR_ARGS(pm.hw_addr));
ds_put_cstr(string, " config: ");
ofp_print_port_config(string, pm.config);
ds_put_cstr(string, " mask: ");
ofp_print_port_config(string, pm.mask);
ds_put_cstr(string, " advertise: ");
if (pm.advertise) {
ofp_print_port_features(string, pm.advertise);
} else {
ds_put_cstr(string, "UNCHANGED\n");
}
}
static const char *
ofputil_table_miss_to_string(enum ofputil_table_miss miss)
{
switch (miss) {
case OFPUTIL_TABLE_MISS_DEFAULT: return "default";
case OFPUTIL_TABLE_MISS_CONTROLLER: return "controller";
case OFPUTIL_TABLE_MISS_CONTINUE: return "continue";
case OFPUTIL_TABLE_MISS_DROP: return "drop";
default: return "***error***";
}
}
static const char *
ofputil_table_eviction_to_string(enum ofputil_table_eviction eviction)
{
switch (eviction) {
case OFPUTIL_TABLE_EVICTION_DEFAULT: return "default";
case OFPUTIL_TABLE_EVICTION_ON: return "on";
case OFPUTIL_TABLE_EVICTION_OFF: return "off";
default: return "***error***";
}
}
static const char *
ofputil_eviction_flag_to_string(uint32_t bit)
{
enum ofp14_table_mod_prop_eviction_flag eviction_flag = bit;
switch (eviction_flag) {
case OFPTMPEF14_OTHER: return "OTHER";
case OFPTMPEF14_IMPORTANCE: return "IMPORTANCE";
case OFPTMPEF14_LIFETIME: return "LIFETIME";
}
return NULL;
}
/* Appends to 'string' a description of the bitmap of OFPTMPEF14_* values in
* 'eviction_flags'. */
static void
ofputil_put_eviction_flags(struct ds *string, uint32_t eviction_flags)
{
if (eviction_flags != UINT32_MAX) {
ofp_print_bit_names(string, eviction_flags,
ofputil_eviction_flag_to_string, '|');
} else {
ds_put_cstr(string, "(default)");
}
}
static void
ofp_print_table_mod(struct ds *string, const struct ofp_header *oh)
{
struct ofputil_table_mod pm;
enum ofperr error;
error = ofputil_decode_table_mod(oh, &pm);
if (error) {