-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathofp-flow.c
2018 lines (1816 loc) · 72.1 KB
/
ofp-flow.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, 2019 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 "openvswitch/ofp-flow.h"
#include <errno.h>
#include "byte-order.h"
#include "colors.h"
#include "flow.h"
#include "nx-match.h"
#include "openvswitch/ofp-actions.h"
#include "openvswitch/ofp-group.h"
#include "openvswitch/ofp-match.h"
#include "openvswitch/ofp-msgs.h"
#include "openvswitch/ofp-parse.h"
#include "openvswitch/ofp-port.h"
#include "openvswitch/ofp-print.h"
#include "openvswitch/ofp-table.h"
#include "openvswitch/ofpbuf.h"
#include "openvswitch/vlog.h"
#include "util.h"
#include "ox-stat.h"
VLOG_DEFINE_THIS_MODULE(ofp_flow);
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
struct ofputil_flow_mod_flag {
uint16_t raw_flag;
enum ofp_version min_version, max_version;
enum ofputil_flow_mod_flags flag;
};
static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags[] = {
{ OFPFF_SEND_FLOW_REM, OFP10_VERSION, 0, OFPUTIL_FF_SEND_FLOW_REM },
{ OFPFF_CHECK_OVERLAP, OFP10_VERSION, 0, OFPUTIL_FF_CHECK_OVERLAP },
{ OFPFF10_EMERG, OFP10_VERSION, OFP10_VERSION,
OFPUTIL_FF_EMERG },
{ OFPFF12_RESET_COUNTS, OFP12_VERSION, 0, OFPUTIL_FF_RESET_COUNTS },
{ OFPFF13_NO_PKT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_PKT_COUNTS },
{ OFPFF13_NO_BYT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_BYT_COUNTS },
{ 0, 0, 0, 0 },
};
static enum ofperr
ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_,
enum ofp_flow_mod_command command,
enum ofp_version version,
enum ofputil_flow_mod_flags *flagsp)
{
uint16_t raw_flags = ntohs(raw_flags_);
const struct ofputil_flow_mod_flag *f;
*flagsp = 0;
for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
if (raw_flags & f->raw_flag
&& version >= f->min_version
&& (!f->max_version || version <= f->max_version)) {
raw_flags &= ~f->raw_flag;
*flagsp |= f->flag;
}
}
/* In OF1.0 and OF1.1, "add" always resets counters, and other commands
* never do.
*
* In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
* resets counters. */
if ((version == OFP10_VERSION || version == OFP11_VERSION)
&& command == OFPFC_ADD) {
*flagsp |= OFPUTIL_FF_RESET_COUNTS;
}
return raw_flags ? OFPERR_OFPFMFC_BAD_FLAGS : 0;
}
static ovs_be16
ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags,
enum ofp_version version)
{
const struct ofputil_flow_mod_flag *f;
uint16_t raw_flags;
raw_flags = 0;
for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
if (f->flag & flags
&& version >= f->min_version
&& (!f->max_version || version <= f->max_version)) {
raw_flags |= f->raw_flag;
}
}
return htons(raw_flags);
}
void
ofputil_flow_mod_flags_format(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 ");
}
}
/* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
* flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
* code.
*
* Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
* The caller must initialize 'ofpacts' and retains ownership of it.
* 'fm->ofpacts' will point into the 'ofpacts' buffer.
*
* On success, the caller must eventually destroy fm->match.
*
* Does not validate the flow_mod actions. The caller should do that, with
* ofpacts_check(). */
enum ofperr
ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
const struct ofp_header *oh,
enum ofputil_protocol protocol,
const struct tun_table *tun_table,
const struct vl_mff_map *vl_mff_map,
struct ofpbuf *ofpacts,
ofp_port_t max_port, uint8_t max_table)
{
ovs_be16 raw_flags;
enum ofperr error;
struct match match;
struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
enum ofpraw raw = ofpraw_pull_assert(&b);
if (raw == OFPRAW_OFPT11_FLOW_MOD) {
/* Standard OpenFlow 1.1+ flow_mod. */
const struct ofp11_flow_mod *ofm;
ofm = ofpbuf_pull(&b, sizeof *ofm);
error = ofputil_pull_ofp11_match(&b, tun_table, vl_mff_map, &match,
NULL);
if (error) {
return error;
}
/* Translate the message. */
fm->priority = ntohs(ofm->priority);
if (ofm->command == OFPFC_ADD
|| (oh->version == OFP11_VERSION
&& (ofm->command == OFPFC_MODIFY ||
ofm->command == OFPFC_MODIFY_STRICT)
&& ofm->cookie_mask == htonll(0))) {
/* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
* not match on the cookie is treated as an "add" if there is no
* match. */
fm->cookie = htonll(0);
fm->cookie_mask = htonll(0);
fm->new_cookie = ofm->cookie;
} else {
fm->cookie = ofm->cookie;
fm->cookie_mask = ofm->cookie_mask;
fm->new_cookie = OVS_BE64_MAX;
}
fm->modify_cookie = false;
fm->command = ofm->command;
/* Get table ID.
*
* OF1.1 entirely forbids table_id == OFPTT_ALL.
* OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
fm->table_id = ofm->table_id;
if (fm->table_id == OFPTT_ALL
&& (oh->version == OFP11_VERSION
|| (ofm->command != OFPFC_DELETE &&
ofm->command != OFPFC_DELETE_STRICT))) {
return OFPERR_OFPFMFC_BAD_TABLE_ID;
}
fm->idle_timeout = ntohs(ofm->idle_timeout);
fm->hard_timeout = ntohs(ofm->hard_timeout);
if (oh->version >= OFP14_VERSION && ofm->command == OFPFC_ADD) {
fm->importance = ntohs(ofm->importance);
} else {
fm->importance = 0;
}
fm->buffer_id = ntohl(ofm->buffer_id);
error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
if (error) {
return error;
}
fm->out_group = (ofm->command == OFPFC_DELETE ||
ofm->command == OFPFC_DELETE_STRICT
? ntohl(ofm->out_group)
: OFPG_ANY);
raw_flags = ofm->flags;
} else {
uint16_t command;
if (raw == OFPRAW_OFPT10_FLOW_MOD) {
/* Standard OpenFlow 1.0 flow_mod. */
const struct ofp10_flow_mod *ofm;
/* Get the ofp10_flow_mod. */
ofm = ofpbuf_pull(&b, sizeof *ofm);
/* Translate the rule. */
ofputil_match_from_ofp10_match(&ofm->match, &match);
ofputil_normalize_match(&match);
/* OpenFlow 1.0 says that exact-match rules have to have the
* highest possible priority. */
fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
? ntohs(ofm->priority)
: UINT16_MAX);
/* Translate the message. */
command = ntohs(ofm->command);
fm->cookie = htonll(0);
fm->cookie_mask = htonll(0);
fm->new_cookie = ofm->cookie;
fm->idle_timeout = ntohs(ofm->idle_timeout);
fm->hard_timeout = ntohs(ofm->hard_timeout);
fm->importance = 0;
fm->buffer_id = ntohl(ofm->buffer_id);
fm->out_port = u16_to_ofp(ntohs(ofm->out_port));
fm->out_group = OFPG_ANY;
raw_flags = ofm->flags;
} else if (raw == OFPRAW_NXT_FLOW_MOD) {
/* Nicira extended flow_mod. */
const struct nx_flow_mod *nfm;
/* Dissect the message. */
nfm = ofpbuf_pull(&b, sizeof *nfm);
error = nx_pull_match(&b, ntohs(nfm->match_len),
&match, &fm->cookie, &fm->cookie_mask,
false, tun_table, vl_mff_map);
if (error) {
return error;
}
/* Translate the message. */
command = ntohs(nfm->command);
if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
/* Flow additions may only set a new cookie, not match an
* existing cookie. */
return OFPERR_NXBRC_NXM_INVALID;
}
fm->priority = ntohs(nfm->priority);
fm->new_cookie = nfm->cookie;
fm->idle_timeout = ntohs(nfm->idle_timeout);
fm->hard_timeout = ntohs(nfm->hard_timeout);
fm->importance = 0;
fm->buffer_id = ntohl(nfm->buffer_id);
fm->out_port = u16_to_ofp(ntohs(nfm->out_port));
fm->out_group = OFPG_ANY;
raw_flags = nfm->flags;
} else {
OVS_NOT_REACHED();
}
fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
if (protocol & OFPUTIL_P_TID) {
fm->command = command & 0xff;
fm->table_id = command >> 8;
} else {
if (command > 0xff) {
VLOG_WARN_RL(&rl, "flow_mod has explicit table_id "
"but flow_mod_table_id extension is not enabled");
}
fm->command = command;
fm->table_id = 0xff;
}
}
/* Check for mismatched conntrack original direction tuple address fields
* w.r.t. the IP version of the match. */
if (((match.wc.masks.ct_nw_src || match.wc.masks.ct_nw_dst)
&& match.flow.dl_type != htons(ETH_TYPE_IP))
|| ((ipv6_addr_is_set(&match.wc.masks.ct_ipv6_src)
|| ipv6_addr_is_set(&match.wc.masks.ct_ipv6_dst))
&& match.flow.dl_type != htons(ETH_TYPE_IPV6))) {
return OFPERR_OFPBAC_MATCH_INCONSISTENT;
}
if (fm->command > OFPFC_DELETE_STRICT) {
return OFPERR_OFPFMFC_BAD_COMMAND;
}
fm->ofpacts_tlv_bitmap = 0;
error = ofpacts_pull_openflow_instructions(&b, b.size, oh->version,
vl_mff_map,
&fm->ofpacts_tlv_bitmap,
ofpacts);
if (error) {
return error;
}
fm->ofpacts = ofpacts->data;
fm->ofpacts_len = ofpacts->size;
error = ofputil_decode_flow_mod_flags(raw_flags, fm->command,
oh->version, &fm->flags);
if (error) {
return error;
}
if (fm->flags & OFPUTIL_FF_EMERG) {
/* We do not support the OpenFlow 1.0 emergency flow cache, which
* is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
*
* OpenFlow 1.0 specifies the error code to use when idle_timeout
* or hard_timeout is nonzero. Otherwise, there is no good error
* code, so just state that the flow table is full. */
return (fm->hard_timeout || fm->idle_timeout
? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
: OFPERR_OFPFMFC_TABLE_FULL);
}
struct ofpact_check_params cp = {
.match = &match,
.max_ports = max_port,
.table_id = fm->table_id,
.n_tables = max_table
};
error = ofpacts_check_consistency(fm->ofpacts, fm->ofpacts_len,
protocol, &cp);
if (!error) {
minimatch_init(&fm->match, &match);
}
return error;
}
static ovs_be16
ofputil_tid_command(const struct ofputil_flow_mod *fm,
enum ofputil_protocol protocol)
{
return htons(protocol & OFPUTIL_P_TID
? (fm->command & 0xff) | (fm->table_id << 8)
: fm->command);
}
/* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
* 'protocol' and returns the message. */
struct ofpbuf *
ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
enum ofputil_protocol protocol)
{
enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
ovs_be16 raw_flags = ofputil_encode_flow_mod_flags(fm->flags, version);
struct ofpbuf *msg;
struct match match;
minimatch_expand(&fm->match, &match);
switch (protocol) {
case OFPUTIL_P_OF11_STD:
case OFPUTIL_P_OF12_OXM:
case OFPUTIL_P_OF13_OXM:
case OFPUTIL_P_OF14_OXM:
case OFPUTIL_P_OF15_OXM: {
struct ofp11_flow_mod *ofm;
int tailroom;
tailroom = ofputil_match_typical_len(protocol) + fm->ofpacts_len;
msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, version, tailroom);
ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
if ((protocol == OFPUTIL_P_OF11_STD
&& (fm->command == OFPFC_MODIFY ||
fm->command == OFPFC_MODIFY_STRICT)
&& fm->cookie_mask == htonll(0))
|| fm->command == OFPFC_ADD) {
ofm->cookie = fm->new_cookie;
} else {
ofm->cookie = fm->cookie & fm->cookie_mask;
}
ofm->cookie_mask = fm->cookie_mask;
if (fm->table_id != OFPTT_ALL
|| (protocol != OFPUTIL_P_OF11_STD
&& (fm->command == OFPFC_DELETE ||
fm->command == OFPFC_DELETE_STRICT))) {
ofm->table_id = fm->table_id;
} else {
ofm->table_id = 0;
}
ofm->command = fm->command;
ofm->idle_timeout = htons(fm->idle_timeout);
ofm->hard_timeout = htons(fm->hard_timeout);
ofm->priority = htons(fm->priority);
ofm->buffer_id = htonl(fm->buffer_id);
ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
ofm->out_group = htonl(fm->out_group);
ofm->flags = raw_flags;
if (version >= OFP14_VERSION && fm->command == OFPFC_ADD) {
ofm->importance = htons(fm->importance);
} else {
ofm->importance = 0;
}
ofputil_put_ofp11_match(msg, &match, protocol);
ofpacts_put_openflow_instructions(fm->ofpacts, fm->ofpacts_len, msg,
version);
break;
}
case OFPUTIL_P_OF10_STD:
case OFPUTIL_P_OF10_STD_TID: {
struct ofp10_flow_mod *ofm;
msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
fm->ofpacts_len);
ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
ofputil_match_to_ofp10_match(&match, &ofm->match);
ofm->cookie = fm->new_cookie;
ofm->command = ofputil_tid_command(fm, protocol);
ofm->idle_timeout = htons(fm->idle_timeout);
ofm->hard_timeout = htons(fm->hard_timeout);
ofm->priority = htons(fm->priority);
ofm->buffer_id = htonl(fm->buffer_id);
ofm->out_port = htons(ofp_to_u16(fm->out_port));
ofm->flags = raw_flags;
ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
version);
break;
}
case OFPUTIL_P_OF10_NXM:
case OFPUTIL_P_OF10_NXM_TID: {
struct nx_flow_mod *nfm;
int match_len;
msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
NXM_TYPICAL_LEN + fm->ofpacts_len);
nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
nfm->command = ofputil_tid_command(fm, protocol);
nfm->cookie = fm->new_cookie;
match_len = nx_put_match(msg, &match, fm->cookie, fm->cookie_mask);
nfm = msg->msg;
nfm->idle_timeout = htons(fm->idle_timeout);
nfm->hard_timeout = htons(fm->hard_timeout);
nfm->priority = htons(fm->priority);
nfm->buffer_id = htonl(fm->buffer_id);
nfm->out_port = htons(ofp_to_u16(fm->out_port));
nfm->flags = raw_flags;
nfm->match_len = htons(match_len);
ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
version);
break;
}
default:
OVS_NOT_REACHED();
}
ofpmsg_update_length(msg);
return msg;
}
enum ofperr
ofputil_flow_mod_format(struct ds *s, const struct ofp_header *oh,
const struct ofputil_port_map *port_map,
const struct ofputil_table_map *table_map,
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, NULL, NULL, &ofpacts,
OFPP_MAX, 255);
if (error) {
ofpbuf_uninit(&ofpacts);
return error;
}
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
|| ofputil_table_map_get_name(table_map, fm.table_id)) {
ds_put_format(s, " table:");
ofputil_format_table(fm.table_id, table_map, s);
}
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, port_map, 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 {
struct match match;
minimatch_expand(&fm.match, &match);
match_format(&match, port_map, 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:%d ", 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, port_map, 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;
}
ofputil_flow_mod_flags_format(s, fm.flags);
ds_put_cstr(s, "actions=");
struct ofpact_format_params fp = {
.port_map = port_map,
.table_map = table_map,
.s = s,
};
ofpacts_format(fm.ofpacts, fm.ofpacts_len, &fp);
ofpbuf_uninit(&ofpacts);
minimatch_destroy(&fm.match);
return 0;
}
static enum ofperr
ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
const struct ofp10_flow_stats_request *ofsr,
bool aggregate)
{
fsr->aggregate = aggregate;
ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
fsr->out_port = u16_to_ofp(ntohs(ofsr->out_port));
fsr->out_group = OFPG_ANY;
fsr->table_id = ofsr->table_id;
fsr->cookie = fsr->cookie_mask = htonll(0);
return 0;
}
static enum ofperr
ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
struct ofpbuf *b, bool aggregate,
const struct tun_table *tun_table,
const struct vl_mff_map *vl_mff_map)
{
const struct ofp11_flow_stats_request *ofsr;
enum ofperr error;
ofsr = ofpbuf_pull(b, sizeof *ofsr);
fsr->aggregate = aggregate;
fsr->table_id = ofsr->table_id;
error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
if (error) {
return error;
}
fsr->out_group = ntohl(ofsr->out_group);
fsr->cookie = ofsr->cookie;
fsr->cookie_mask = ofsr->cookie_mask;
error = ofputil_pull_ofp11_match(b, tun_table, vl_mff_map, &fsr->match,
NULL);
if (error) {
return error;
}
return 0;
}
static enum ofperr
ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
struct ofpbuf *b, bool aggregate,
const struct tun_table *tun_table,
const struct vl_mff_map *vl_mff_map)
{
const struct nx_flow_stats_request *nfsr;
enum ofperr error;
nfsr = ofpbuf_pull(b, sizeof *nfsr);
error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
&fsr->cookie, &fsr->cookie_mask, false, tun_table,
vl_mff_map);
if (error) {
return error;
}
if (b->size) {
return OFPERR_OFPBRC_BAD_LEN;
}
fsr->aggregate = aggregate;
fsr->out_port = u16_to_ofp(ntohs(nfsr->out_port));
fsr->out_group = OFPG_ANY;
fsr->table_id = nfsr->table_id;
return 0;
}
/* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
* request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
* successful, otherwise an OpenFlow error code.
*
* 'vl_mff_map' is an optional parameter that is used to validate the length
* of variable length mf_fields in 'match'. If it is not provided, the
* default mf_fields with maximum length will be used. */
enum ofperr
ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
const struct ofp_header *oh,
const struct tun_table *tun_table,
const struct vl_mff_map *vl_mff_map)
{
struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
enum ofpraw raw = ofpraw_pull_assert(&b);
switch ((int) raw) {
case OFPRAW_OFPST10_FLOW_REQUEST:
return ofputil_decode_ofpst10_flow_request(fsr, b.data, false);
case OFPRAW_OFPST10_AGGREGATE_REQUEST:
return ofputil_decode_ofpst10_flow_request(fsr, b.data, true);
case OFPRAW_OFPST11_FLOW_REQUEST:
return ofputil_decode_ofpst11_flow_request(fsr, &b, false, tun_table,
vl_mff_map);
case OFPRAW_OFPST11_AGGREGATE_REQUEST:
return ofputil_decode_ofpst11_flow_request(fsr, &b, true, tun_table,
vl_mff_map);
case OFPRAW_OFPST15_AGGREGATE_REQUEST:
return ofputil_decode_ofpst11_flow_request(fsr, &b, true,
tun_table, vl_mff_map);
case OFPRAW_NXST_FLOW_REQUEST:
return ofputil_decode_nxst_flow_request(fsr, &b, false, tun_table,
vl_mff_map);
case OFPRAW_NXST_AGGREGATE_REQUEST:
return ofputil_decode_nxst_flow_request(fsr, &b, true, tun_table,
vl_mff_map);
default:
/* Hey, the caller lied. */
OVS_NOT_REACHED();
}
}
/* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
* OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
* 'protocol', and returns the message. */
struct ofpbuf *
ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
enum ofputil_protocol protocol)
{
struct ofpbuf *msg;
enum ofpraw raw;
switch (protocol) {
case OFPUTIL_P_OF11_STD:
case OFPUTIL_P_OF12_OXM:
case OFPUTIL_P_OF13_OXM:
case OFPUTIL_P_OF14_OXM:
case OFPUTIL_P_OF15_OXM: {
struct ofp11_flow_stats_request *ofsr;
if (protocol > OFPUTIL_P_OF14_OXM) {
raw = (fsr->aggregate
? OFPRAW_OFPST15_AGGREGATE_REQUEST
: OFPRAW_OFPST11_FLOW_REQUEST);
} else {
raw = (fsr->aggregate
? OFPRAW_OFPST11_AGGREGATE_REQUEST
: OFPRAW_OFPST11_FLOW_REQUEST);
}
msg = ofpraw_alloc(raw, ofputil_protocol_to_ofp_version(protocol),
ofputil_match_typical_len(protocol));
ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
ofsr->table_id = fsr->table_id;
ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
ofsr->out_group = htonl(fsr->out_group);
ofsr->cookie = fsr->cookie;
ofsr->cookie_mask = fsr->cookie_mask;
ofputil_put_ofp11_match(msg, &fsr->match, protocol);
break;
}
case OFPUTIL_P_OF10_STD:
case OFPUTIL_P_OF10_STD_TID: {
struct ofp10_flow_stats_request *ofsr;
raw = (fsr->aggregate
? OFPRAW_OFPST10_AGGREGATE_REQUEST
: OFPRAW_OFPST10_FLOW_REQUEST);
msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
ofsr->table_id = fsr->table_id;
ofsr->out_port = htons(ofp_to_u16(fsr->out_port));
break;
}
case OFPUTIL_P_OF10_NXM:
case OFPUTIL_P_OF10_NXM_TID: {
struct nx_flow_stats_request *nfsr;
int match_len;
raw = (fsr->aggregate
? OFPRAW_NXST_AGGREGATE_REQUEST
: OFPRAW_NXST_FLOW_REQUEST);
msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
ofpbuf_put_zeros(msg, sizeof *nfsr);
match_len = nx_put_match(msg, &fsr->match,
fsr->cookie, fsr->cookie_mask);
nfsr = msg->msg;
nfsr->out_port = htons(ofp_to_u16(fsr->out_port));
nfsr->match_len = htons(match_len);
nfsr->table_id = fsr->table_id;
break;
}
default:
OVS_NOT_REACHED();
}
return msg;
}
void
ofputil_flow_stats_request_format(struct ds *s,
const struct ofputil_flow_stats_request *fsr,
const struct ofputil_port_map *port_map,
const struct ofputil_table_map *table_map)
{
if (fsr->table_id != 0xff) {
ds_put_format(s, " table=");
ofputil_format_table(fsr->table_id, table_map, s);
}
if (fsr->out_port != OFPP_ANY) {
ds_put_cstr(s, " out_port=");
ofputil_format_port(fsr->out_port, port_map, s);
}
ds_put_char(s, ' ');
match_format(&fsr->match, port_map, s, OFP_DEFAULT_PRIORITY);
}
char * OVS_WARN_UNUSED_RESULT
parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
bool aggregate, const char *string,
const struct ofputil_port_map *port_map,
const struct ofputil_table_map *table_map,
enum ofputil_protocol *usable_protocols)
{
struct ofputil_flow_mod fm;
char *error;
error = parse_ofp_str(&fm, -1, string, port_map, table_map,
usable_protocols);
if (error) {
return error;
}
/* Special table ID support not required for stats requests. */
if (*usable_protocols & OFPUTIL_P_OF10_STD_TID) {
*usable_protocols |= OFPUTIL_P_OF10_STD;
}
if (*usable_protocols & OFPUTIL_P_OF10_NXM_TID) {
*usable_protocols |= OFPUTIL_P_OF10_NXM;
}
fsr->aggregate = aggregate;
fsr->cookie = fm.cookie;
fsr->cookie_mask = fm.cookie_mask;
minimatch_expand(&fm.match, &fsr->match);
fsr->out_port = fm.out_port;
fsr->out_group = fm.out_group;
fsr->table_id = fm.table_id;
minimatch_destroy(&fm.match);
return NULL;
}
/* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
* ofputil_flow_stats in 'fs'.
*
* Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
* OpenFlow message. Calling this function multiple times for a single 'msg'
* iterates through the replies. The caller must initially leave 'msg''s layer
* pointers null and not modify them between calls.
*
* Most switches don't send the values needed to populate fs->idle_age and
* fs->hard_age, so those members will usually be set to 0. If the switch from
* which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
* 'flow_age_extension' as true so that the contents of 'msg' determine the
* 'idle_age' and 'hard_age' members in 'fs'.
*
* Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
* reply's actions. The caller must initialize 'ofpacts' and retains ownership
* of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
*
* Returns 0 if successful, EOF if no replies were left in this 'msg',
* otherwise an OFPERR_* value. */
int
ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
struct ofpbuf *msg,
bool flow_age_extension,
struct ofpbuf *ofpacts)
{
const struct ofp_header *oh;
size_t instructions_len;
enum ofperr error;
enum ofpraw raw;
error = (msg->header ? ofpraw_decode(&raw, msg->header)
: ofpraw_pull(&raw, msg));
if (error) {
return error;
}
oh = msg->header;
if (!msg->size) {
return EOF;
} else if (raw == OFPRAW_OFPST15_FLOW_REPLY) {
const struct ofp15_flow_desc *ofd;
size_t length;
uint16_t padded_match_len;
uint16_t stat_len;
uint8_t oxs_field_set;
ofd = ofpbuf_try_pull(msg, sizeof *ofd);
if (!ofd) {
VLOG_WARN_RL(&rl, "OFPST_FLOW reply has %" PRIu32
" leftover " "bytes at end", msg->size);
return EINVAL;
}
length = ntohs(ofd->length);
if (length < sizeof *ofd) {
VLOG_WARN_RL(&rl, "OFPST_FLOW reply claims invalid "
"length %" PRIuSIZE, length);
return EINVAL;
}
if (ofputil_pull_ofp11_match(msg, NULL, NULL, &fs->match,
&padded_match_len)) {
VLOG_WARN_RL(&rl, "OFPST_FLOW reply bad match");
return EINVAL;
}
fs->priority = ntohs(ofd->priority);
fs->table_id = ofd->table_id;
fs->cookie = ofd->cookie;
fs->idle_timeout = ntohs(ofd->idle_timeout);
fs->hard_timeout = ntohs(ofd->hard_timeout);
fs->importance = ntohs(ofd->importance);
error = ofputil_decode_flow_mod_flags(ofd->flags, -1, oh->version,
&fs->flags);
if (error) {
return error;
}
struct oxs_stats oxs;
if (oxs_pull_stat(msg, &oxs, &stat_len, &oxs_field_set)) {
VLOG_WARN_RL(&rl, "OXS OFPST_FLOW reply bad stats");
return EINVAL;
}
fs->duration_sec = oxs.duration_sec;
fs->duration_nsec = oxs.duration_nsec;
fs->packet_count = oxs.packet_count;
fs->byte_count = oxs.byte_count;
fs->idle_age = oxs.idle_age == UINT32_MAX ? -1 : oxs.idle_age;
fs->hard_age = -1;
instructions_len = length - sizeof *ofd - padded_match_len - stat_len;
} else if (raw == OFPRAW_OFPST11_FLOW_REPLY
|| raw == OFPRAW_OFPST13_FLOW_REPLY) {
const struct ofp11_flow_stats *ofs;
size_t length;
uint16_t padded_match_len;
ofs = ofpbuf_try_pull(msg, sizeof *ofs);
if (!ofs) {
VLOG_WARN_RL(&rl, "OFPST_FLOW reply has %"PRIu32" leftover "
"bytes at end", msg->size);
return OFPERR_OFPBRC_BAD_LEN;
}
length = ntohs(ofs->length);
if (length < sizeof *ofs) {
VLOG_WARN_RL(&rl, "OFPST_FLOW reply claims invalid "
"length %"PRIuSIZE, length);
return OFPERR_OFPBRC_BAD_LEN;
}
error = ofputil_pull_ofp11_match(msg, NULL, NULL, &fs->match,
&padded_match_len);
if (error) {
VLOG_WARN_RL(&rl, "OFPST_FLOW reply bad match");
return error;
}
instructions_len = length - sizeof *ofs - padded_match_len;
fs->priority = ntohs(ofs->priority);
fs->table_id = ofs->table_id;
fs->duration_sec = ntohl(ofs->duration_sec);
fs->duration_nsec = ntohl(ofs->duration_nsec);
fs->idle_timeout = ntohs(ofs->idle_timeout);
fs->hard_timeout = ntohs(ofs->hard_timeout);
if (oh->version >= OFP14_VERSION) {
fs->importance = ntohs(ofs->importance);
} else {