forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ofproto-dpif-ipfix.c
1890 lines (1651 loc) · 64.4 KB
/
ofproto-dpif-ipfix.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) 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 "ofproto-dpif-ipfix.h"
#include <sys/time.h>
#include "byte-order.h"
#include "collectors.h"
#include "flow.h"
#include "hash.h"
#include "hmap.h"
#include "list.h"
#include "ofpbuf.h"
#include "ofproto.h"
#include "ofproto-dpif.h"
#include "dp-packet.h"
#include "packets.h"
#include "poll-loop.h"
#include "sset.h"
#include "util.h"
#include "timeval.h"
#include "util.h"
#include "openvswitch/vlog.h"
VLOG_DEFINE_THIS_MODULE(ipfix);
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
/* Cf. IETF RFC 5101 Section 10.3.4. */
#define IPFIX_DEFAULT_COLLECTOR_PORT 4739
/* Cf. IETF RFC 5881 Setion 8. */
#define BFD_CONTROL_DEST_PORT 3784
#define BFD_ECHO_DEST_PORT 3785
/* The standard layer2SegmentId (ID 351) element is included in vDS to send
* the VxLAN tunnel's VNI. It is 64-bit long, the most significant byte is
* used to indicate the type of tunnel (0x01 = VxLAN, 0x02 = GRE) and the three
* least significant bytes hold the value of the layer 2 overlay network
* segment identifier: a 24-bit VxLAN tunnel's VNI or a 24-bit GRE tunnel's
* TNI. This is not compatible with STT, as implemented in OVS, as
* its tunnel IDs is 64-bit.
*
* Two new enterprise information elements are defined which are similar to
* laryerSegmentId but support 64-bit IDs:
* tunnelType (ID 891) and tunnelKey (ID 892).
*
* The enum dpif_ipfix_tunnel_type is to declare the types supported in the
* tunnelType element.
* The number of ipfix tunnel types includes two reserverd types: 0x04 and 0x06.
*/
enum dpif_ipfix_tunnel_type {
DPIF_IPFIX_TUNNEL_UNKNOWN = 0x00,
DPIF_IPFIX_TUNNEL_VXLAN = 0x01,
DPIF_IPFIX_TUNNEL_GRE = 0x02,
DPIF_IPFIX_TUNNEL_LISP = 0x03,
DPIF_IPFIX_TUNNEL_STT = 0x04,
DPIF_IPFIX_TUNNEL_IPSEC_GRE = 0x05,
DPIF_IPFIX_TUNNEL_GENEVE = 0x07,
NUM_DPIF_IPFIX_TUNNEL
};
struct dpif_ipfix_port {
struct hmap_node hmap_node; /* In struct dpif_ipfix's "tunnel_ports" hmap. */
struct ofport *ofport; /* To retrieve port stats. */
odp_port_t odp_port;
enum dpif_ipfix_tunnel_type tunnel_type;
uint8_t tunnel_key_length;
};
struct dpif_ipfix_exporter {
struct collectors *collectors;
uint32_t seq_number;
time_t last_template_set_time;
struct hmap cache_flow_key_map; /* ipfix_flow_cache_entry. */
struct ovs_list cache_flow_start_timestamp_list; /* ipfix_flow_cache_entry. */
uint32_t cache_active_timeout; /* In seconds. */
uint32_t cache_max_flows;
};
struct dpif_ipfix_bridge_exporter {
struct dpif_ipfix_exporter exporter;
struct ofproto_ipfix_bridge_exporter_options *options;
uint32_t probability;
};
struct dpif_ipfix_flow_exporter {
struct dpif_ipfix_exporter exporter;
struct ofproto_ipfix_flow_exporter_options *options;
};
struct dpif_ipfix_flow_exporter_map_node {
struct hmap_node node;
struct dpif_ipfix_flow_exporter exporter;
};
struct dpif_ipfix {
struct dpif_ipfix_bridge_exporter bridge_exporter;
struct hmap flow_exporter_map; /* dpif_ipfix_flow_exporter_map_node. */
struct hmap tunnel_ports; /* Contains "struct dpif_ipfix_port"s.
* It makes tunnel port lookups faster in
* sampling upcalls. */
struct ovs_refcount ref_cnt;
};
#define IPFIX_VERSION 0x000a
/* When using UDP, IPFIX Template Records must be re-sent regularly.
* The standard default interval is 10 minutes (600 seconds).
* Cf. IETF RFC 5101 Section 10.3.6. */
#define IPFIX_TEMPLATE_INTERVAL 600
/* Cf. IETF RFC 5101 Section 3.1. */
OVS_PACKED(
struct ipfix_header {
ovs_be16 version; /* IPFIX_VERSION. */
ovs_be16 length; /* Length in bytes including this header. */
ovs_be32 export_time; /* Seconds since the epoch. */
ovs_be32 seq_number; /* Message sequence number. */
ovs_be32 obs_domain_id; /* Observation Domain ID. */
});
BUILD_ASSERT_DECL(sizeof(struct ipfix_header) == 16);
#define IPFIX_SET_ID_TEMPLATE 2
#define IPFIX_SET_ID_OPTION_TEMPLATE 3
/* Cf. IETF RFC 5101 Section 3.3.2. */
OVS_PACKED(
struct ipfix_set_header {
ovs_be16 set_id; /* IPFIX_SET_ID_* or valid template ID for Data Sets. */
ovs_be16 length; /* Length of the set in bytes including header. */
});
BUILD_ASSERT_DECL(sizeof(struct ipfix_set_header) == 4);
/* Alternatives for templates at each layer. A template is defined by
* a combination of one value for each layer. */
enum ipfix_proto_l2 {
IPFIX_PROTO_L2_ETH = 0, /* No VLAN. */
IPFIX_PROTO_L2_VLAN,
NUM_IPFIX_PROTO_L2
};
enum ipfix_proto_l3 {
IPFIX_PROTO_L3_UNKNOWN = 0,
IPFIX_PROTO_L3_IPV4,
IPFIX_PROTO_L3_IPV6,
NUM_IPFIX_PROTO_L3
};
enum ipfix_proto_l4 {
IPFIX_PROTO_L4_UNKNOWN = 0,
IPFIX_PROTO_L4_TCP_UDP_SCTP,
IPFIX_PROTO_L4_ICMP,
NUM_IPFIX_PROTO_L4
};
enum ipfix_proto_tunnel {
IPFIX_PROTO_NOT_TUNNELED = 0,
IPFIX_PROTO_TUNNELED, /* Support gre, lisp and vxlan. */
NUM_IPFIX_PROTO_TUNNEL
};
/* Any Template ID > 255 is usable for Template Records. */
#define IPFIX_TEMPLATE_ID_MIN 256
/* Cf. IETF RFC 5101 Section 3.4.1. */
OVS_PACKED(
struct ipfix_template_record_header {
ovs_be16 template_id;
ovs_be16 field_count;
});
BUILD_ASSERT_DECL(sizeof(struct ipfix_template_record_header) == 4);
enum ipfix_entity_id {
/* standard IPFIX elements */
#define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_ID_##ENUM = ID,
#include "ofproto/ipfix-entities.def"
/* non-standard IPFIX elements */
#define IPFIX_SET_ENTERPRISE(v) (((v) | 0x8000))
#define IPFIX_ENTERPRISE_ENTITY(ENUM, ID, SIZE, NAME, ENTERPRISE) \
IPFIX_ENTITY_ID_##ENUM = IPFIX_SET_ENTERPRISE(ID),
#include "ofproto/ipfix-enterprise-entities.def"
};
enum ipfix_entity_size {
/* standard IPFIX elements */
#define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_SIZE_##ENUM = SIZE,
#include "ofproto/ipfix-entities.def"
/* non-standard IPFIX elements */
#define IPFIX_ENTERPRISE_ENTITY(ENUM, ID, SIZE, NAME, ENTERPRISE) \
IPFIX_ENTITY_SIZE_##ENUM = SIZE,
#include "ofproto/ipfix-enterprise-entities.def"
};
enum ipfix_entity_enterprise {
/* standard IPFIX elements */
#define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_ENTERPRISE_##ENUM = 0,
#include "ofproto/ipfix-entities.def"
/* non-standard IPFIX elements */
#define IPFIX_ENTERPRISE_ENTITY(ENUM, ID, SIZE, NAME, ENTERPRISE) \
IPFIX_ENTITY_ENTERPRISE_##ENUM = ENTERPRISE,
#include "ofproto/ipfix-enterprise-entities.def"
};
OVS_PACKED(
struct ipfix_template_field_specifier {
ovs_be16 element_id; /* IPFIX_ENTITY_ID_*. */
ovs_be16 field_length; /* Length of the field's value, in bytes.
* For Variable-Length element, it should be 65535.
*/
ovs_be32 enterprise; /* Enterprise number */
});
BUILD_ASSERT_DECL(sizeof(struct ipfix_template_field_specifier) == 8);
/* Cf. IETF RFC 5102 Section 5.11.6. */
enum ipfix_flow_direction {
INGRESS_FLOW = 0x00,
EGRESS_FLOW = 0x01
};
/* Part of data record flow key for common metadata and Ethernet entities. */
OVS_PACKED(
struct ipfix_data_record_flow_key_common {
ovs_be32 observation_point_id; /* OBSERVATION_POINT_ID */
uint8_t flow_direction; /* FLOW_DIRECTION */
struct eth_addr source_mac_address; /* SOURCE_MAC_ADDRESS */
struct eth_addr destination_mac_address; /* DESTINATION_MAC_ADDRESS */
ovs_be16 ethernet_type; /* ETHERNET_TYPE */
uint8_t ethernet_header_length; /* ETHERNET_HEADER_LENGTH */
});
BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_common) == 20);
/* Part of data record flow key for VLAN entities. */
OVS_PACKED(
struct ipfix_data_record_flow_key_vlan {
ovs_be16 vlan_id; /* VLAN_ID */
ovs_be16 dot1q_vlan_id; /* DOT1Q_VLAN_ID */
uint8_t dot1q_priority; /* DOT1Q_PRIORITY */
});
BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_vlan) == 5);
/* Part of data record flow key for IP entities. */
/* XXX: Replace IP_TTL with MINIMUM_TTL and MAXIMUM_TTL? */
OVS_PACKED(
struct ipfix_data_record_flow_key_ip {
uint8_t ip_version; /* IP_VERSION */
uint8_t ip_ttl; /* IP_TTL */
uint8_t protocol_identifier; /* PROTOCOL_IDENTIFIER */
uint8_t ip_diff_serv_code_point; /* IP_DIFF_SERV_CODE_POINT */
uint8_t ip_precedence; /* IP_PRECEDENCE */
uint8_t ip_class_of_service; /* IP_CLASS_OF_SERVICE */
});
BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ip) == 6);
/* Part of data record flow key for IPv4 entities. */
OVS_PACKED(
struct ipfix_data_record_flow_key_ipv4 {
ovs_be32 source_ipv4_address; /* SOURCE_IPV4_ADDRESS */
ovs_be32 destination_ipv4_address; /* DESTINATION_IPV4_ADDRESS */
});
BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ipv4) == 8);
/* Part of data record flow key for IPv6 entities. */
OVS_PACKED(
struct ipfix_data_record_flow_key_ipv6 {
uint8_t source_ipv6_address[16]; /* SOURCE_IPV6_ADDRESS */
uint8_t destination_ipv6_address[16]; /* DESTINATION_IPV6_ADDRESS */
ovs_be32 flow_label_ipv6; /* FLOW_LABEL_IPV6 */
});
BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ipv6) == 36);
/* Part of data record flow key for TCP/UDP/SCTP entities. */
OVS_PACKED(
struct ipfix_data_record_flow_key_transport {
ovs_be16 source_transport_port; /* SOURCE_TRANSPORT_PORT */
ovs_be16 destination_transport_port; /* DESTINATION_TRANSPORT_PORT */
});
BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_transport) == 4);
/* Part of data record flow key for ICMP entities. */
OVS_PACKED(
struct ipfix_data_record_flow_key_icmp {
uint8_t icmp_type; /* ICMP_TYPE_IPV4 / ICMP_TYPE_IPV6 */
uint8_t icmp_code; /* ICMP_CODE_IPV4 / ICMP_CODE_IPV6 */
});
BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_icmp) == 2);
/* For the tunnel type that is on the top of IPSec, the protocol identifier
* of the upper tunnel type is used.
*/
static uint8_t tunnel_protocol[NUM_DPIF_IPFIX_TUNNEL] = {
0, /* reserved */
IPPROTO_UDP, /* DPIF_IPFIX_TUNNEL_VXLAN */
IPPROTO_GRE, /* DPIF_IPFIX_TUNNEL_GRE */
IPPROTO_UDP, /* DPIF_IPFIX_TUNNEL_LISP*/
IPPROTO_TCP, /* DPIF_IPFIX_TUNNEL_STT*/
IPPROTO_GRE, /* DPIF_IPFIX_TUNNEL_IPSEC_GRE */
0 , /* reserved */
IPPROTO_UDP, /* DPIF_IPFIX_TUNNEL_GENEVE*/
};
OVS_PACKED(
struct ipfix_data_record_flow_key_tunnel {
ovs_be32 tunnel_source_ipv4_address; /* TUNNEL_SOURCE_IPV4_ADDRESS */
ovs_be32 tunnel_destination_ipv4_address; /* TUNNEL_DESTINATION_IPV4_ADDRESS */
uint8_t tunnel_protocol_identifier; /* TUNNEL_PROTOCOL_IDENTIFIER */
ovs_be16 tunnel_source_transport_port; /* TUNNEL_SOURCE_TRANSPORT_PORT */
ovs_be16 tunnel_destination_transport_port; /* TUNNEL_DESTINATION_TRANSPORT_PORT */
uint8_t tunnel_type; /* TUNNEL_TYPE */
uint8_t tunnel_key_length; /* length of TUNNEL_KEY */
uint8_t tunnel_key[]; /* data of TUNNEL_KEY */
});
BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_tunnel) == 15);
/* Cf. IETF RFC 5102 Section 5.11.3. */
enum ipfix_flow_end_reason {
IDLE_TIMEOUT = 0x01,
ACTIVE_TIMEOUT = 0x02,
END_OF_FLOW_DETECTED = 0x03,
FORCED_END = 0x04,
LACK_OF_RESOURCES = 0x05
};
/* Part of data record for common aggregated elements. */
OVS_PACKED(
struct ipfix_data_record_aggregated_common {
ovs_be32 flow_start_delta_microseconds; /* FLOW_START_DELTA_MICROSECONDS */
ovs_be32 flow_end_delta_microseconds; /* FLOW_END_DELTA_MICROSECONDS */
ovs_be64 packet_delta_count; /* PACKET_DELTA_COUNT */
ovs_be64 layer2_octet_delta_count; /* LAYER2_OCTET_DELTA_COUNT */
uint8_t flow_end_reason; /* FLOW_END_REASON */
});
BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_aggregated_common) == 25);
/* Part of data record for IP aggregated elements. */
OVS_PACKED(
struct ipfix_data_record_aggregated_ip {
ovs_be64 octet_delta_count; /* OCTET_DELTA_COUNT */
ovs_be64 octet_delta_sum_of_squares; /* OCTET_DELTA_SUM_OF_SQUARES */
ovs_be64 minimum_ip_total_length; /* MINIMUM_IP_TOTAL_LENGTH */
ovs_be64 maximum_ip_total_length; /* MAXIMUM_IP_TOTAL_LENGTH */
});
BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_aggregated_ip) == 32);
/*
* support tunnel key for:
* VxLAN: 24-bit VIN,
* GRE: 32-bit key,
* LISP: 24-bit instance ID
* STT: 64-bit key
*/
#define MAX_TUNNEL_KEY_LEN 8
#define MAX_FLOW_KEY_LEN \
(sizeof(struct ipfix_data_record_flow_key_common) \
+ sizeof(struct ipfix_data_record_flow_key_vlan) \
+ sizeof(struct ipfix_data_record_flow_key_ip) \
+ MAX(sizeof(struct ipfix_data_record_flow_key_ipv4), \
sizeof(struct ipfix_data_record_flow_key_ipv6)) \
+ MAX(sizeof(struct ipfix_data_record_flow_key_icmp), \
sizeof(struct ipfix_data_record_flow_key_transport)) \
+ sizeof(struct ipfix_data_record_flow_key_tunnel) \
+ MAX_TUNNEL_KEY_LEN)
#define MAX_DATA_RECORD_LEN \
(MAX_FLOW_KEY_LEN \
+ sizeof(struct ipfix_data_record_aggregated_common) \
+ sizeof(struct ipfix_data_record_aggregated_ip))
/* Max length of a data set. To simplify the implementation, each
* data record is sent in a separate data set, so each data set
* contains at most one data record. */
#define MAX_DATA_SET_LEN \
(sizeof(struct ipfix_set_header) \
+ MAX_DATA_RECORD_LEN)
/* Max length of an IPFIX message. Arbitrarily set to accommodate low
* MTU. */
#define MAX_MESSAGE_LEN 1024
/* Cache structures. */
/* Flow key. */
struct ipfix_flow_key {
uint32_t obs_domain_id;
uint16_t template_id;
size_t flow_key_msg_part_size;
uint64_t flow_key_msg_part[DIV_ROUND_UP(MAX_FLOW_KEY_LEN, 8)];
};
/* Flow cache entry. */
struct ipfix_flow_cache_entry {
struct hmap_node flow_key_map_node;
struct ovs_list cache_flow_start_timestamp_list_node;
struct ipfix_flow_key flow_key;
/* Common aggregated elements. */
uint64_t flow_start_timestamp_usec;
uint64_t flow_end_timestamp_usec;
uint64_t packet_delta_count;
uint64_t layer2_octet_delta_count;
uint64_t octet_delta_count;
uint64_t octet_delta_sum_of_squares; /* 0 if not IP. */
uint16_t minimum_ip_total_length; /* 0 if not IP. */
uint16_t maximum_ip_total_length; /* 0 if not IP. */
};
static void dpif_ipfix_cache_expire(struct dpif_ipfix_exporter *, bool,
const uint64_t, const uint32_t);
static void get_export_time_now(uint64_t *, uint32_t *);
static void dpif_ipfix_cache_expire_now(struct dpif_ipfix_exporter *, bool);
static bool
ofproto_ipfix_bridge_exporter_options_equal(
const struct ofproto_ipfix_bridge_exporter_options *a,
const struct ofproto_ipfix_bridge_exporter_options *b)
{
return (a->obs_domain_id == b->obs_domain_id
&& a->obs_point_id == b->obs_point_id
&& a->sampling_rate == b->sampling_rate
&& a->cache_active_timeout == b->cache_active_timeout
&& a->cache_max_flows == b->cache_max_flows
&& a->enable_tunnel_sampling == b->enable_tunnel_sampling
&& a->enable_input_sampling == b->enable_input_sampling
&& a->enable_output_sampling == b->enable_output_sampling
&& sset_equals(&a->targets, &b->targets));
}
static struct ofproto_ipfix_bridge_exporter_options *
ofproto_ipfix_bridge_exporter_options_clone(
const struct ofproto_ipfix_bridge_exporter_options *old)
{
struct ofproto_ipfix_bridge_exporter_options *new =
xmemdup(old, sizeof *old);
sset_clone(&new->targets, &old->targets);
return new;
}
static void
ofproto_ipfix_bridge_exporter_options_destroy(
struct ofproto_ipfix_bridge_exporter_options *options)
{
if (options) {
sset_destroy(&options->targets);
free(options);
}
}
static bool
ofproto_ipfix_flow_exporter_options_equal(
const struct ofproto_ipfix_flow_exporter_options *a,
const struct ofproto_ipfix_flow_exporter_options *b)
{
return (a->collector_set_id == b->collector_set_id
&& a->cache_active_timeout == b->cache_active_timeout
&& a->cache_max_flows == b->cache_max_flows
&& sset_equals(&a->targets, &b->targets));
}
static struct ofproto_ipfix_flow_exporter_options *
ofproto_ipfix_flow_exporter_options_clone(
const struct ofproto_ipfix_flow_exporter_options *old)
{
struct ofproto_ipfix_flow_exporter_options *new =
xmemdup(old, sizeof *old);
sset_clone(&new->targets, &old->targets);
return new;
}
static void
ofproto_ipfix_flow_exporter_options_destroy(
struct ofproto_ipfix_flow_exporter_options *options)
{
if (options) {
sset_destroy(&options->targets);
free(options);
}
}
static void
dpif_ipfix_exporter_init(struct dpif_ipfix_exporter *exporter)
{
exporter->collectors = NULL;
exporter->seq_number = 1;
exporter->last_template_set_time = TIME_MIN;
hmap_init(&exporter->cache_flow_key_map);
list_init(&exporter->cache_flow_start_timestamp_list);
exporter->cache_active_timeout = 0;
exporter->cache_max_flows = 0;
}
static void
dpif_ipfix_exporter_clear(struct dpif_ipfix_exporter *exporter)
{
/* Flush the cache with flow end reason "forced end." */
dpif_ipfix_cache_expire_now(exporter, true);
collectors_destroy(exporter->collectors);
exporter->collectors = NULL;
exporter->seq_number = 1;
exporter->last_template_set_time = TIME_MIN;
exporter->cache_active_timeout = 0;
exporter->cache_max_flows = 0;
}
static void
dpif_ipfix_exporter_destroy(struct dpif_ipfix_exporter *exporter)
{
dpif_ipfix_exporter_clear(exporter);
hmap_destroy(&exporter->cache_flow_key_map);
}
static bool
dpif_ipfix_exporter_set_options(struct dpif_ipfix_exporter *exporter,
const struct sset *targets,
const uint32_t cache_active_timeout,
const uint32_t cache_max_flows)
{
collectors_destroy(exporter->collectors);
collectors_create(targets, IPFIX_DEFAULT_COLLECTOR_PORT,
&exporter->collectors);
if (exporter->collectors == NULL) {
VLOG_WARN_RL(&rl, "no collectors could be initialized, "
"IPFIX exporter disabled");
dpif_ipfix_exporter_clear(exporter);
return false;
}
exporter->cache_active_timeout = cache_active_timeout;
exporter->cache_max_flows = cache_max_flows;
return true;
}
static struct dpif_ipfix_port *
dpif_ipfix_find_port(const struct dpif_ipfix *di,
odp_port_t odp_port) OVS_REQUIRES(mutex)
{
struct dpif_ipfix_port *dip;
HMAP_FOR_EACH_IN_BUCKET (dip, hmap_node, hash_odp_port(odp_port),
&di->tunnel_ports) {
if (dip->odp_port == odp_port) {
return dip;
}
}
return NULL;
}
static void
dpif_ipfix_del_port(struct dpif_ipfix *di,
struct dpif_ipfix_port *dip)
OVS_REQUIRES(mutex)
{
hmap_remove(&di->tunnel_ports, &dip->hmap_node);
free(dip);
}
void
dpif_ipfix_add_tunnel_port(struct dpif_ipfix *di, struct ofport *ofport,
odp_port_t odp_port) OVS_EXCLUDED(mutex)
{
struct dpif_ipfix_port *dip;
const char *type;
ovs_mutex_lock(&mutex);
dip = dpif_ipfix_find_port(di, odp_port);
if (dip) {
dpif_ipfix_del_port(di, dip);
}
type = netdev_get_type(ofport->netdev);
if (type == NULL) {
goto out;
}
/* Add to table of tunnel ports. */
dip = xmalloc(sizeof *dip);
dip->ofport = ofport;
dip->odp_port = odp_port;
if (strcmp(type, "gre") == 0) {
/* 32-bit key gre */
dip->tunnel_type = DPIF_IPFIX_TUNNEL_GRE;
dip->tunnel_key_length = 4;
} else if (strcmp(type, "ipsec_gre") == 0) {
/* 32-bit key ipsec_gre */
dip->tunnel_type = DPIF_IPFIX_TUNNEL_IPSEC_GRE;
dip->tunnel_key_length = 4;
} else if (strcmp(type, "vxlan") == 0) {
dip->tunnel_type = DPIF_IPFIX_TUNNEL_VXLAN;
dip->tunnel_key_length = 3;
} else if (strcmp(type, "lisp") == 0) {
dip->tunnel_type = DPIF_IPFIX_TUNNEL_LISP;
dip->tunnel_key_length = 3;
} else if (strcmp(type, "geneve") == 0) {
dip->tunnel_type = DPIF_IPFIX_TUNNEL_GENEVE;
dip->tunnel_key_length = 3;
} else if (strcmp(type, "stt") == 0) {
dip->tunnel_type = DPIF_IPFIX_TUNNEL_STT;
dip->tunnel_key_length = 8;
} else {
free(dip);
goto out;
}
hmap_insert(&di->tunnel_ports, &dip->hmap_node, hash_odp_port(odp_port));
out:
ovs_mutex_unlock(&mutex);
}
void
dpif_ipfix_del_tunnel_port(struct dpif_ipfix *di, odp_port_t odp_port)
OVS_EXCLUDED(mutex)
{
struct dpif_ipfix_port *dip;
ovs_mutex_lock(&mutex);
dip = dpif_ipfix_find_port(di, odp_port);
if (dip) {
dpif_ipfix_del_port(di, dip);
}
ovs_mutex_unlock(&mutex);
}
bool
dpif_ipfix_get_tunnel_port(const struct dpif_ipfix *di, odp_port_t odp_port)
OVS_EXCLUDED(mutex)
{
struct dpif_ipfix_port *dip;
ovs_mutex_lock(&mutex);
dip = dpif_ipfix_find_port(di, odp_port);
ovs_mutex_unlock(&mutex);
return dip != NULL;
}
static void
dpif_ipfix_bridge_exporter_init(struct dpif_ipfix_bridge_exporter *exporter)
{
dpif_ipfix_exporter_init(&exporter->exporter);
exporter->options = NULL;
exporter->probability = 0;
}
static void
dpif_ipfix_bridge_exporter_clear(struct dpif_ipfix_bridge_exporter *exporter)
{
dpif_ipfix_exporter_clear(&exporter->exporter);
ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
exporter->options = NULL;
exporter->probability = 0;
}
static void
dpif_ipfix_bridge_exporter_destroy(struct dpif_ipfix_bridge_exporter *exporter)
{
dpif_ipfix_bridge_exporter_clear(exporter);
dpif_ipfix_exporter_destroy(&exporter->exporter);
}
static void
dpif_ipfix_bridge_exporter_set_options(
struct dpif_ipfix_bridge_exporter *exporter,
const struct ofproto_ipfix_bridge_exporter_options *options)
{
bool options_changed;
if (!options || sset_is_empty(&options->targets)) {
/* No point in doing any work if there are no targets. */
dpif_ipfix_bridge_exporter_clear(exporter);
return;
}
options_changed = (
!exporter->options
|| !ofproto_ipfix_bridge_exporter_options_equal(
options, exporter->options));
/* Configure collectors if options have changed or if we're
* shortchanged in collectors (which indicates that opening one or
* more of the configured collectors failed, so that we should
* retry). */
if (options_changed
|| collectors_count(exporter->exporter.collectors)
< sset_count(&options->targets)) {
if (!dpif_ipfix_exporter_set_options(
&exporter->exporter, &options->targets,
options->cache_active_timeout, options->cache_max_flows)) {
return;
}
}
/* Avoid reconfiguring if options didn't change. */
if (!options_changed) {
return;
}
ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
exporter->options = ofproto_ipfix_bridge_exporter_options_clone(options);
exporter->probability =
MAX(1, UINT32_MAX / exporter->options->sampling_rate);
/* Run over the cache as some entries might have expired after
* changing the timeouts. */
dpif_ipfix_cache_expire_now(&exporter->exporter, false);
}
static struct dpif_ipfix_flow_exporter_map_node*
dpif_ipfix_find_flow_exporter_map_node(
const struct dpif_ipfix *di, const uint32_t collector_set_id)
OVS_REQUIRES(mutex)
{
struct dpif_ipfix_flow_exporter_map_node *exporter_node;
HMAP_FOR_EACH_WITH_HASH (exporter_node, node,
hash_int(collector_set_id, 0),
&di->flow_exporter_map) {
if (exporter_node->exporter.options->collector_set_id
== collector_set_id) {
return exporter_node;
}
}
return NULL;
}
static void
dpif_ipfix_flow_exporter_init(struct dpif_ipfix_flow_exporter *exporter)
{
dpif_ipfix_exporter_init(&exporter->exporter);
exporter->options = NULL;
}
static void
dpif_ipfix_flow_exporter_clear(struct dpif_ipfix_flow_exporter *exporter)
{
dpif_ipfix_exporter_clear(&exporter->exporter);
ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
exporter->options = NULL;
}
static void
dpif_ipfix_flow_exporter_destroy(struct dpif_ipfix_flow_exporter *exporter)
{
dpif_ipfix_flow_exporter_clear(exporter);
dpif_ipfix_exporter_destroy(&exporter->exporter);
}
static bool
dpif_ipfix_flow_exporter_set_options(
struct dpif_ipfix_flow_exporter *exporter,
const struct ofproto_ipfix_flow_exporter_options *options)
{
bool options_changed;
if (sset_is_empty(&options->targets)) {
/* No point in doing any work if there are no targets. */
dpif_ipfix_flow_exporter_clear(exporter);
return true;
}
options_changed = (
!exporter->options
|| !ofproto_ipfix_flow_exporter_options_equal(
options, exporter->options));
/* Configure collectors if options have changed or if we're
* shortchanged in collectors (which indicates that opening one or
* more of the configured collectors failed, so that we should
* retry). */
if (options_changed
|| collectors_count(exporter->exporter.collectors)
< sset_count(&options->targets)) {
if (!dpif_ipfix_exporter_set_options(
&exporter->exporter, &options->targets,
options->cache_active_timeout, options->cache_max_flows)) {
return false;
}
}
/* Avoid reconfiguring if options didn't change. */
if (!options_changed) {
return true;
}
ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
exporter->options = ofproto_ipfix_flow_exporter_options_clone(options);
/* Run over the cache as some entries might have expired after
* changing the timeouts. */
dpif_ipfix_cache_expire_now(&exporter->exporter, false);
return true;
}
void
dpif_ipfix_set_options(
struct dpif_ipfix *di,
const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
size_t n_flow_exporters_options) OVS_EXCLUDED(mutex)
{
int i;
struct ofproto_ipfix_flow_exporter_options *options;
struct dpif_ipfix_flow_exporter_map_node *node, *next;
size_t n_broken_flow_exporters_options = 0;
ovs_mutex_lock(&mutex);
dpif_ipfix_bridge_exporter_set_options(&di->bridge_exporter,
bridge_exporter_options);
/* Add new flow exporters and update current flow exporters. */
options = (struct ofproto_ipfix_flow_exporter_options *)
flow_exporters_options;
for (i = 0; i < n_flow_exporters_options; i++) {
node = dpif_ipfix_find_flow_exporter_map_node(
di, options->collector_set_id);
if (!node) {
node = xzalloc(sizeof *node);
dpif_ipfix_flow_exporter_init(&node->exporter);
hmap_insert(&di->flow_exporter_map, &node->node,
hash_int(options->collector_set_id, 0));
}
if (!dpif_ipfix_flow_exporter_set_options(&node->exporter, options)) {
n_broken_flow_exporters_options++;
}
options++;
}
ovs_assert(hmap_count(&di->flow_exporter_map) >=
(n_flow_exporters_options - n_broken_flow_exporters_options));
/* Remove dropped flow exporters, if any needs to be removed. */
if (hmap_count(&di->flow_exporter_map) > n_flow_exporters_options) {
HMAP_FOR_EACH_SAFE (node, next, node, &di->flow_exporter_map) {
/* This is slow but doesn't take any extra memory, and
* this table is not supposed to contain many rows anyway. */
options = (struct ofproto_ipfix_flow_exporter_options *)
flow_exporters_options;
for (i = 0; i < n_flow_exporters_options; i++) {
if (node->exporter.options->collector_set_id
== options->collector_set_id) {
break;
}
options++;
}
if (i == n_flow_exporters_options) { // Not found.
hmap_remove(&di->flow_exporter_map, &node->node);
dpif_ipfix_flow_exporter_destroy(&node->exporter);
free(node);
}
}
}
ovs_assert(hmap_count(&di->flow_exporter_map) ==
(n_flow_exporters_options - n_broken_flow_exporters_options));
ovs_mutex_unlock(&mutex);
}
struct dpif_ipfix *
dpif_ipfix_create(void)
{
struct dpif_ipfix *di;
di = xzalloc(sizeof *di);
dpif_ipfix_bridge_exporter_init(&di->bridge_exporter);
hmap_init(&di->flow_exporter_map);
hmap_init(&di->tunnel_ports);
ovs_refcount_init(&di->ref_cnt);
return di;
}
struct dpif_ipfix *
dpif_ipfix_ref(const struct dpif_ipfix *di_)
{
struct dpif_ipfix *di = CONST_CAST(struct dpif_ipfix *, di_);
if (di) {
ovs_refcount_ref(&di->ref_cnt);
}
return di;
}
uint32_t
dpif_ipfix_get_bridge_exporter_probability(const struct dpif_ipfix *di)
OVS_EXCLUDED(mutex)
{
uint32_t ret;
ovs_mutex_lock(&mutex);
ret = di->bridge_exporter.probability;
ovs_mutex_unlock(&mutex);
return ret;
}
bool
dpif_ipfix_get_bridge_exporter_input_sampling(const struct dpif_ipfix *di)
OVS_EXCLUDED(mutex)
{
bool ret = true;
ovs_mutex_lock(&mutex);
if (di->bridge_exporter.options) {
ret = di->bridge_exporter.options->enable_input_sampling;
}
ovs_mutex_unlock(&mutex);
return ret;
}
bool
dpif_ipfix_get_bridge_exporter_output_sampling(const struct dpif_ipfix *di)
OVS_EXCLUDED(mutex)
{
bool ret = true;
ovs_mutex_lock(&mutex);
if (di->bridge_exporter.options) {
ret = di->bridge_exporter.options->enable_output_sampling;
}
ovs_mutex_unlock(&mutex);
return ret;
}
bool
dpif_ipfix_get_bridge_exporter_tunnel_sampling(const struct dpif_ipfix *di)
OVS_EXCLUDED(mutex)
{
bool ret = false;
ovs_mutex_lock(&mutex);
if (di->bridge_exporter.options) {
ret = di->bridge_exporter.options->enable_tunnel_sampling;
}
ovs_mutex_unlock(&mutex);
return ret;
}
static void
dpif_ipfix_clear(struct dpif_ipfix *di) OVS_REQUIRES(mutex)
{
struct dpif_ipfix_flow_exporter_map_node *exp_node, *exp_next;
struct dpif_ipfix_port *dip, *next;
dpif_ipfix_bridge_exporter_clear(&di->bridge_exporter);
HMAP_FOR_EACH_SAFE (exp_node, exp_next, node, &di->flow_exporter_map) {
hmap_remove(&di->flow_exporter_map, &exp_node->node);
dpif_ipfix_flow_exporter_destroy(&exp_node->exporter);
free(exp_node);
}
HMAP_FOR_EACH_SAFE (dip, next, hmap_node, &di->tunnel_ports) {
dpif_ipfix_del_port(di, dip);
}
}
void
dpif_ipfix_unref(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
{
if (di && ovs_refcount_unref_relaxed(&di->ref_cnt) == 1) {
ovs_mutex_lock(&mutex);
dpif_ipfix_clear(di);
dpif_ipfix_bridge_exporter_destroy(&di->bridge_exporter);
hmap_destroy(&di->flow_exporter_map);
hmap_destroy(&di->tunnel_ports);
free(di);
ovs_mutex_unlock(&mutex);
}
}
static void
ipfix_init_header(uint32_t export_time_sec, uint32_t seq_number,
uint32_t obs_domain_id, struct dp_packet *msg)
{
struct ipfix_header *hdr;
hdr = dp_packet_put_zeros(msg, sizeof *hdr);
hdr->version = htons(IPFIX_VERSION);
hdr->length = htons(sizeof *hdr); /* Updated in ipfix_send_msg. */
hdr->export_time = htonl(export_time_sec);
hdr->seq_number = htonl(seq_number);
hdr->obs_domain_id = htonl(obs_domain_id);
}
static void
ipfix_send_msg(const struct collectors *collectors, struct dp_packet *msg)
{
struct ipfix_header *hdr;
/* Adjust the length in the header. */
hdr = dp_packet_data(msg);
hdr->length = htons(dp_packet_size(msg));
collectors_send(collectors, dp_packet_data(msg), dp_packet_size(msg));
dp_packet_set_size(msg, 0);
}
static uint16_t