]> git.proxmox.com Git - mirror_ovs.git/blob - ofproto/ofproto-dpif-ipfix.c
ofproto-dpif-ipfix: Fix inefficent memory use in ipfix_send_template_msgs().
[mirror_ovs.git] / ofproto / ofproto-dpif-ipfix.c
1 /*
2 * Copyright (c) 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include "ofproto-dpif-ipfix.h"
19 #include <sys/time.h>
20 #include "byte-order.h"
21 #include "collectors.h"
22 #include "flow.h"
23 #include "hash.h"
24 #include "openvswitch/hmap.h"
25 #include "netdev.h"
26 #include "openvswitch/list.h"
27 #include "openvswitch/ofpbuf.h"
28 #include "ofproto.h"
29 #include "ofproto-dpif.h"
30 #include "dp-packet.h"
31 #include "packets.h"
32 #include "poll-loop.h"
33 #include "sset.h"
34 #include "util.h"
35 #include "timeval.h"
36 #include "openvswitch/vlog.h"
37
38 VLOG_DEFINE_THIS_MODULE(ipfix);
39
40 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
41 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
42
43 /* Cf. IETF RFC 5101 Section 10.3.4. */
44 #define IPFIX_DEFAULT_COLLECTOR_PORT 4739
45
46 /* Cf. IETF RFC 5881 Setion 8. */
47 #define BFD_CONTROL_DEST_PORT 3784
48 #define BFD_ECHO_DEST_PORT 3785
49
50 enum ipfix_sampled_packet_type {
51 IPFIX_SAMPLED_PKT_UNKNOWN = 0x00,
52 IPFIX_SAMPLED_PKT_IPV4_OK = 0x01,
53 IPFIX_SAMPLED_PKT_IPV6_OK = 0x02,
54 IPFIX_SAMPLED_PKT_IPV4_ERROR = 0x03,
55 IPFIX_SAMPLED_PKT_IPV6_ERROR = 0x04,
56 IPFIX_SAMPLED_PKT_OTHERS = 0x05
57 };
58
59 /* The standard layer2SegmentId (ID 351) element is included in vDS to send
60 * the VxLAN tunnel's VNI. It is 64-bit long, the most significant byte is
61 * used to indicate the type of tunnel (0x01 = VxLAN, 0x02 = GRE) and the three
62 * least significant bytes hold the value of the layer 2 overlay network
63 * segment identifier: a 24-bit VxLAN tunnel's VNI or a 24-bit GRE tunnel's
64 * TNI. This is not compatible with STT, as implemented in OVS, as
65 * its tunnel IDs is 64-bit.
66 *
67 * Two new enterprise information elements are defined which are similar to
68 * laryerSegmentId but support 64-bit IDs:
69 * tunnelType (ID 891) and tunnelKey (ID 892).
70 *
71 * The enum dpif_ipfix_tunnel_type is to declare the types supported in the
72 * tunnelType element.
73 * The number of ipfix tunnel types includes two reserverd types: 0x04 and 0x06.
74 */
75 enum dpif_ipfix_tunnel_type {
76 DPIF_IPFIX_TUNNEL_UNKNOWN = 0x00,
77 DPIF_IPFIX_TUNNEL_VXLAN = 0x01,
78 DPIF_IPFIX_TUNNEL_GRE = 0x02,
79 DPIF_IPFIX_TUNNEL_LISP = 0x03,
80 DPIF_IPFIX_TUNNEL_STT = 0x04,
81 DPIF_IPFIX_TUNNEL_GENEVE = 0x07,
82 NUM_DPIF_IPFIX_TUNNEL
83 };
84
85 typedef struct ofputil_ipfix_stats ofproto_ipfix_stats;
86
87 struct dpif_ipfix_global_stats {
88 uint64_t packet_total_count;
89 uint64_t octet_total_count;
90 uint64_t octet_total_sum_of_squares;
91 uint64_t layer2_octet_total_count;
92 uint64_t tcp_ack_total_count;
93 uint64_t tcp_fin_total_count;
94 uint64_t tcp_psh_total_count;
95 uint64_t tcp_rst_total_count;
96 uint64_t tcp_syn_total_count;
97 uint64_t tcp_urg_total_count;
98 };
99
100 struct dpif_ipfix_port {
101 struct hmap_node hmap_node; /* In struct dpif_ipfix's "tunnel_ports" hmap. */
102 struct ofport *ofport; /* To retrieve port stats. */
103 odp_port_t odp_port;
104 enum dpif_ipfix_tunnel_type tunnel_type;
105 uint8_t tunnel_key_length;
106 };
107
108 struct dpif_ipfix_exporter {
109 struct collectors *collectors;
110 uint32_t seq_number;
111 time_t last_template_set_time;
112 struct hmap cache_flow_key_map; /* ipfix_flow_cache_entry. */
113 struct ovs_list cache_flow_start_timestamp_list; /* ipfix_flow_cache_entry. */
114 uint32_t cache_active_timeout; /* In seconds. */
115 uint32_t cache_max_flows;
116 char *virtual_obs_id;
117 uint8_t virtual_obs_len;
118
119 ofproto_ipfix_stats ofproto_stats;
120 struct dpif_ipfix_global_stats ipfix_global_stats;
121 };
122
123 struct dpif_ipfix_bridge_exporter {
124 struct dpif_ipfix_exporter exporter;
125 struct ofproto_ipfix_bridge_exporter_options *options;
126 uint32_t probability;
127 };
128
129 struct dpif_ipfix_flow_exporter {
130 struct dpif_ipfix_exporter exporter;
131 struct ofproto_ipfix_flow_exporter_options *options;
132 };
133
134 struct dpif_ipfix_flow_exporter_map_node {
135 struct hmap_node node;
136 struct dpif_ipfix_flow_exporter exporter;
137 };
138
139 struct dpif_ipfix {
140 struct dpif_ipfix_bridge_exporter bridge_exporter;
141 struct hmap flow_exporter_map; /* dpif_ipfix_flow_exporter_map_node. */
142 struct hmap tunnel_ports; /* Contains "struct dpif_ipfix_port"s.
143 * It makes tunnel port lookups faster in
144 * sampling upcalls. */
145 struct ovs_refcount ref_cnt;
146 };
147
148 #define IPFIX_VERSION 0x000a
149
150 /* When using UDP, IPFIX Template Records must be re-sent regularly.
151 * The standard default interval is 10 minutes (600 seconds).
152 * Cf. IETF RFC 5101 Section 10.3.6. */
153 #define IPFIX_TEMPLATE_INTERVAL 600
154
155 /* Cf. IETF RFC 5101 Section 3.1. */
156 OVS_PACKED(
157 struct ipfix_header {
158 ovs_be16 version; /* IPFIX_VERSION. */
159 ovs_be16 length; /* Length in bytes including this header. */
160 ovs_be32 export_time; /* Seconds since the epoch. */
161 ovs_be32 seq_number; /* Message sequence number. */
162 ovs_be32 obs_domain_id; /* Observation Domain ID. */
163 });
164 BUILD_ASSERT_DECL(sizeof(struct ipfix_header) == 16);
165
166 #define IPFIX_SET_ID_TEMPLATE 2
167 #define IPFIX_SET_ID_OPTION_TEMPLATE 3
168
169 /* Cf. IETF RFC 5101 Section 3.3.2. */
170 OVS_PACKED(
171 struct ipfix_set_header {
172 ovs_be16 set_id; /* IPFIX_SET_ID_* or valid template ID for Data Sets. */
173 ovs_be16 length; /* Length of the set in bytes including header. */
174 });
175 BUILD_ASSERT_DECL(sizeof(struct ipfix_set_header) == 4);
176
177 /* Alternatives for templates at each layer. A template is defined by
178 * a combination of one value for each layer. */
179 enum ipfix_proto_l2 {
180 IPFIX_PROTO_L2_ETH = 0, /* No VLAN. */
181 IPFIX_PROTO_L2_VLAN,
182 NUM_IPFIX_PROTO_L2
183 };
184 enum ipfix_proto_l3 {
185 IPFIX_PROTO_L3_UNKNOWN = 0,
186 IPFIX_PROTO_L3_IPV4,
187 IPFIX_PROTO_L3_IPV6,
188 NUM_IPFIX_PROTO_L3
189 };
190 enum ipfix_proto_l4 {
191 IPFIX_PROTO_L4_UNKNOWN = 0,
192 IPFIX_PROTO_L4_TCP,
193 IPFIX_PROTO_L4_UDP,
194 IPFIX_PROTO_L4_SCTP,
195 IPFIX_PROTO_L4_ICMP,
196 NUM_IPFIX_PROTO_L4
197 };
198 enum ipfix_proto_tunnel {
199 IPFIX_PROTO_NOT_TUNNELED = 0,
200 IPFIX_PROTO_TUNNELED, /* Support gre, lisp and vxlan. */
201 NUM_IPFIX_PROTO_TUNNEL
202 };
203
204 /* Any Template ID > 255 is usable for Template Records. */
205 #define IPFIX_TEMPLATE_ID_MIN 256
206
207 /* Cf. IETF RFC 5101 Section 3.4.1. */
208 OVS_PACKED(
209 struct ipfix_template_record_header {
210 ovs_be16 template_id;
211 ovs_be16 field_count;
212 });
213 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_record_header) == 4);
214
215 enum ipfix_entity_id {
216 /* standard IPFIX elements */
217 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_ID_##ENUM = ID,
218 #include "ofproto/ipfix-entities.def"
219 /* non-standard IPFIX elements */
220 #define IPFIX_SET_ENTERPRISE(v) (((v) | 0x8000))
221 #define IPFIX_ENTERPRISE_ENTITY(ENUM, ID, SIZE, NAME, ENTERPRISE) \
222 IPFIX_ENTITY_ID_##ENUM = IPFIX_SET_ENTERPRISE(ID),
223 #include "ofproto/ipfix-enterprise-entities.def"
224 };
225
226 enum ipfix_entity_size {
227 /* standard IPFIX elements */
228 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_SIZE_##ENUM = SIZE,
229 #include "ofproto/ipfix-entities.def"
230 /* non-standard IPFIX elements */
231 #define IPFIX_ENTERPRISE_ENTITY(ENUM, ID, SIZE, NAME, ENTERPRISE) \
232 IPFIX_ENTITY_SIZE_##ENUM = SIZE,
233 #include "ofproto/ipfix-enterprise-entities.def"
234 };
235
236 enum ipfix_entity_enterprise {
237 /* standard IPFIX elements */
238 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_ENTERPRISE_##ENUM = 0,
239 #include "ofproto/ipfix-entities.def"
240 /* non-standard IPFIX elements */
241 #define IPFIX_ENTERPRISE_ENTITY(ENUM, ID, SIZE, NAME, ENTERPRISE) \
242 IPFIX_ENTITY_ENTERPRISE_##ENUM = ENTERPRISE,
243 #include "ofproto/ipfix-enterprise-entities.def"
244 };
245
246 OVS_PACKED(
247 struct ipfix_template_field_specifier {
248 ovs_be16 element_id; /* IPFIX_ENTITY_ID_*. */
249 ovs_be16 field_length; /* Length of the field's value, in bytes.
250 * For Variable-Length element, it should be 65535.
251 */
252 ovs_be32 enterprise; /* Enterprise number */
253 });
254 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_field_specifier) == 8);
255
256 /* Cf. IETF RFC 5102 Section 5.11.6. */
257 enum ipfix_flow_direction {
258 INGRESS_FLOW = 0x00,
259 EGRESS_FLOW = 0x01
260 };
261
262 /* Part of data record flow key for common metadata and Ethernet entities. */
263 OVS_PACKED(
264 struct ipfix_data_record_flow_key_common {
265 ovs_be32 observation_point_id; /* OBSERVATION_POINT_ID */
266 uint8_t flow_direction; /* FLOW_DIRECTION */
267 struct eth_addr source_mac_address; /* SOURCE_MAC_ADDRESS */
268 struct eth_addr destination_mac_address; /* DESTINATION_MAC_ADDRESS */
269 ovs_be16 ethernet_type; /* ETHERNET_TYPE */
270 uint8_t ethernet_header_length; /* ETHERNET_HEADER_LENGTH */
271 });
272 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_common) == 20);
273
274 /* Part of data record flow key for VLAN entities. */
275 OVS_PACKED(
276 struct ipfix_data_record_flow_key_vlan {
277 ovs_be16 vlan_id; /* VLAN_ID */
278 ovs_be16 dot1q_vlan_id; /* DOT1Q_VLAN_ID */
279 uint8_t dot1q_priority; /* DOT1Q_PRIORITY */
280 });
281 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_vlan) == 5);
282
283 /* Part of data record flow key for IP entities. */
284 /* XXX: Replace IP_TTL with MINIMUM_TTL and MAXIMUM_TTL? */
285 OVS_PACKED(
286 struct ipfix_data_record_flow_key_ip {
287 uint8_t ip_version; /* IP_VERSION */
288 uint8_t ip_ttl; /* IP_TTL */
289 uint8_t protocol_identifier; /* PROTOCOL_IDENTIFIER */
290 uint8_t ip_diff_serv_code_point; /* IP_DIFF_SERV_CODE_POINT */
291 uint8_t ip_precedence; /* IP_PRECEDENCE */
292 uint8_t ip_class_of_service; /* IP_CLASS_OF_SERVICE */
293 });
294 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ip) == 6);
295
296 /* Part of data record flow key for IPv4 entities. */
297 OVS_PACKED(
298 struct ipfix_data_record_flow_key_ipv4 {
299 ovs_be32 source_ipv4_address; /* SOURCE_IPV4_ADDRESS */
300 ovs_be32 destination_ipv4_address; /* DESTINATION_IPV4_ADDRESS */
301 });
302 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ipv4) == 8);
303
304 /* Part of data record flow key for IPv6 entities. */
305 OVS_PACKED(
306 struct ipfix_data_record_flow_key_ipv6 {
307 uint8_t source_ipv6_address[16]; /* SOURCE_IPV6_ADDRESS */
308 uint8_t destination_ipv6_address[16]; /* DESTINATION_IPV6_ADDRESS */
309 ovs_be32 flow_label_ipv6; /* FLOW_LABEL_IPV6 */
310 });
311 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ipv6) == 36);
312
313 /* Part of data record flow key for TCP/UDP/SCTP entities. */
314 OVS_PACKED(
315 struct ipfix_data_record_flow_key_transport {
316 ovs_be16 source_transport_port; /* SOURCE_TRANSPORT_PORT */
317 ovs_be16 destination_transport_port; /* DESTINATION_TRANSPORT_PORT */
318 });
319 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_transport) == 4);
320
321 /* Part of data record flow key for ICMP entities. */
322 OVS_PACKED(
323 struct ipfix_data_record_flow_key_icmp {
324 uint8_t icmp_type; /* ICMP_TYPE_IPV4 / ICMP_TYPE_IPV6 */
325 uint8_t icmp_code; /* ICMP_CODE_IPV4 / ICMP_CODE_IPV6 */
326 });
327 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_icmp) == 2);
328
329 static uint8_t tunnel_protocol[NUM_DPIF_IPFIX_TUNNEL] = {
330 0, /* reserved */
331 IPPROTO_UDP, /* DPIF_IPFIX_TUNNEL_VXLAN */
332 IPPROTO_GRE, /* DPIF_IPFIX_TUNNEL_GRE */
333 IPPROTO_UDP, /* DPIF_IPFIX_TUNNEL_LISP*/
334 IPPROTO_TCP, /* DPIF_IPFIX_TUNNEL_STT*/
335 0 , /* reserved */
336 IPPROTO_UDP, /* DPIF_IPFIX_TUNNEL_GENEVE*/
337 };
338
339 OVS_PACKED(
340 struct ipfix_data_record_flow_key_tunnel {
341 ovs_be32 tunnel_source_ipv4_address; /* TUNNEL_SOURCE_IPV4_ADDRESS */
342 ovs_be32 tunnel_destination_ipv4_address; /* TUNNEL_DESTINATION_IPV4_ADDRESS */
343 uint8_t tunnel_protocol_identifier; /* TUNNEL_PROTOCOL_IDENTIFIER */
344 ovs_be16 tunnel_source_transport_port; /* TUNNEL_SOURCE_TRANSPORT_PORT */
345 ovs_be16 tunnel_destination_transport_port; /* TUNNEL_DESTINATION_TRANSPORT_PORT */
346 uint8_t tunnel_type; /* TUNNEL_TYPE */
347 uint8_t tunnel_key_length; /* length of TUNNEL_KEY */
348 uint8_t tunnel_key[]; /* data of TUNNEL_KEY */
349 });
350 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_tunnel) == 15);
351
352 /* Cf. IETF RFC 5102 Section 5.11.3. */
353 enum ipfix_flow_end_reason {
354 IDLE_TIMEOUT = 0x01,
355 ACTIVE_TIMEOUT = 0x02,
356 END_OF_FLOW_DETECTED = 0x03,
357 FORCED_END = 0x04,
358 LACK_OF_RESOURCES = 0x05
359 };
360
361 /* Part of data record for common aggregated elements. */
362 OVS_PACKED(
363 struct ipfix_data_record_aggregated_common {
364 ovs_be32 flow_start_delta_microseconds; /* FLOW_START_DELTA_MICROSECONDS */
365 ovs_be32 flow_end_delta_microseconds; /* FLOW_END_DELTA_MICROSECONDS */
366 ovs_be64 packet_delta_count; /* PACKET_DELTA_COUNT */
367 ovs_be64 packet_total_count; /* PACKET_TOTAL_COUNT */
368 ovs_be64 layer2_octet_delta_count; /* LAYER2_OCTET_DELTA_COUNT */
369 ovs_be64 layer2_octet_total_count; /* LAYER2_OCTET_TOTAL_COUNT */
370 uint8_t flow_end_reason; /* FLOW_END_REASON */
371 });
372 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_aggregated_common) == 41);
373
374 /* Part of data record for IP aggregated elements. */
375 OVS_PACKED(
376 struct ipfix_data_record_aggregated_ip {
377 ovs_be64 octet_delta_count; /* OCTET_DELTA_COUNT */
378 ovs_be64 octet_total_count; /* OCTET_TOTAL_COUNT */
379 ovs_be64 octet_delta_sum_of_squares; /* OCTET_DELTA_SUM_OF_SQUARES */
380 ovs_be64 octet_total_sum_of_squares; /* OCTET_TOTAL_SUM_OF_SQUARES */
381 ovs_be64 minimum_ip_total_length; /* MINIMUM_IP_TOTAL_LENGTH */
382 ovs_be64 maximum_ip_total_length; /* MAXIMUM_IP_TOTAL_LENGTH */
383 });
384 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_aggregated_ip) == 48);
385
386 /* Part of data record for TCP aggregated elements. */
387 OVS_PACKED(
388 struct ipfix_data_record_aggregated_tcp {
389 ovs_be64 tcp_ack_total_count; /* TCP_ACK_TOTAL_COUNT */
390 ovs_be64 tcp_fin_total_count; /* TCP_FIN_TOTAL_COUNT */
391 ovs_be64 tcp_psh_total_count; /* TCP_PSH_TOTAL_COUNT */
392 ovs_be64 tcp_rst_total_count; /* TCP_RST_TOTAL_COUNT */
393 ovs_be64 tcp_syn_total_count; /* TCP_SYN_TOTAL_COUNT */
394 ovs_be64 tcp_urg_total_count; /* TCP_URG_TOTAL_COUNT */
395 });
396 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_aggregated_tcp) == 48);
397
398 /*
399 * Refer to RFC 7011, the length of Variable length element is 0~65535:
400 * In most case, it should be less than 255 octets:
401 * 0 1 2 3
402 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
403 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
404 * | Length (< 255)| Information Element |
405 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
406 * | ... continuing as needed |
407 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
408 *
409 * When it is greater than or equeal to 255 octets:
410 * 0 1 2 3
411 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
412 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
413 * | 255 | Length (0 to 65535) | IE |
414 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
415 * | ... continuing as needed |
416 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
417 *
418 *
419 * Now, only the virtual_obs_id whose length < 255 is implemented.
420 */
421
422 #define IPFIX_VIRTUAL_OBS_MAX_LEN 254
423
424 /*
425 * support tunnel key for:
426 * VxLAN: 24-bit VIN,
427 * GRE: 32-bit key,
428 * LISP: 24-bit instance ID
429 * STT: 64-bit key
430 */
431 #define MAX_TUNNEL_KEY_LEN 8
432
433 #define MAX_FLOW_KEY_LEN \
434 (sizeof(struct ipfix_data_record_flow_key_common) \
435 + sizeof(struct ipfix_data_record_flow_key_vlan) \
436 + sizeof(struct ipfix_data_record_flow_key_ip) \
437 + MAX(sizeof(struct ipfix_data_record_flow_key_ipv4), \
438 sizeof(struct ipfix_data_record_flow_key_ipv6)) \
439 + MAX(sizeof(struct ipfix_data_record_flow_key_icmp), \
440 sizeof(struct ipfix_data_record_flow_key_transport)) \
441 + sizeof(struct ipfix_data_record_flow_key_tunnel) \
442 + MAX_TUNNEL_KEY_LEN)
443
444 #define MAX_DATA_RECORD_LEN \
445 (MAX_FLOW_KEY_LEN \
446 + sizeof(struct ipfix_data_record_aggregated_common) \
447 + sizeof(struct ipfix_data_record_aggregated_ip) \
448 + sizeof(struct ipfix_data_record_aggregated_tcp))
449
450 /* Max length of a data set. To simplify the implementation, each
451 * data record is sent in a separate data set, so each data set
452 * contains at most one data record. */
453 #define MAX_DATA_SET_LEN \
454 (sizeof(struct ipfix_set_header) \
455 + MAX_DATA_RECORD_LEN)
456
457 /* Max length of an IPFIX message. Arbitrarily set to accommodate low
458 * MTU. */
459 #define MAX_MESSAGE_LEN 1024
460
461 /* Cache structures. */
462
463 /* Flow key. */
464 struct ipfix_flow_key {
465 uint32_t obs_domain_id;
466 uint16_t template_id;
467 size_t flow_key_msg_part_size;
468 uint64_t flow_key_msg_part[DIV_ROUND_UP(MAX_FLOW_KEY_LEN, 8)];
469 };
470
471 /* Flow cache entry. */
472 struct ipfix_flow_cache_entry {
473 struct hmap_node flow_key_map_node;
474 struct ovs_list cache_flow_start_timestamp_list_node;
475 struct ipfix_flow_key flow_key;
476 /* Common aggregated elements. */
477 uint64_t flow_start_timestamp_usec;
478 uint64_t flow_end_timestamp_usec;
479 uint64_t packet_delta_count;
480 uint64_t packet_total_count;
481 uint64_t layer2_octet_delta_count;
482 uint64_t layer2_octet_total_count;
483 uint64_t octet_delta_count;
484 uint64_t octet_total_count;
485 uint64_t octet_delta_sum_of_squares; /* 0 if not IP. */
486 uint64_t octet_total_sum_of_squares; /* 0 if not IP. */
487 uint16_t minimum_ip_total_length; /* 0 if not IP. */
488 uint16_t maximum_ip_total_length; /* 0 if not IP. */
489 uint64_t tcp_packet_delta_count;
490 uint64_t tcp_ack_total_count;
491 uint64_t tcp_fin_total_count;
492 uint64_t tcp_psh_total_count;
493 uint64_t tcp_rst_total_count;
494 uint64_t tcp_syn_total_count;
495 uint64_t tcp_urg_total_count;
496 };
497
498 static void dpif_ipfix_cache_expire(struct dpif_ipfix_exporter *, bool,
499 const uint64_t, const uint32_t);
500
501 static void get_export_time_now(uint64_t *, uint32_t *);
502
503 static void dpif_ipfix_cache_expire_now(struct dpif_ipfix_exporter *, bool);
504
505 static bool
506 ofproto_ipfix_bridge_exporter_options_equal(
507 const struct ofproto_ipfix_bridge_exporter_options *a,
508 const struct ofproto_ipfix_bridge_exporter_options *b)
509 {
510 return (a->obs_domain_id == b->obs_domain_id
511 && a->obs_point_id == b->obs_point_id
512 && a->sampling_rate == b->sampling_rate
513 && a->cache_active_timeout == b->cache_active_timeout
514 && a->cache_max_flows == b->cache_max_flows
515 && a->enable_tunnel_sampling == b->enable_tunnel_sampling
516 && a->enable_input_sampling == b->enable_input_sampling
517 && a->enable_output_sampling == b->enable_output_sampling
518 && sset_equals(&a->targets, &b->targets)
519 && nullable_string_is_equal(a->virtual_obs_id, b->virtual_obs_id));
520 }
521
522 static struct ofproto_ipfix_bridge_exporter_options *
523 ofproto_ipfix_bridge_exporter_options_clone(
524 const struct ofproto_ipfix_bridge_exporter_options *old)
525 {
526 struct ofproto_ipfix_bridge_exporter_options *new =
527 xmemdup(old, sizeof *old);
528 sset_clone(&new->targets, &old->targets);
529 new->virtual_obs_id = nullable_xstrdup(old->virtual_obs_id);
530 return new;
531 }
532
533 static void
534 ofproto_ipfix_bridge_exporter_options_destroy(
535 struct ofproto_ipfix_bridge_exporter_options *options)
536 {
537 if (options) {
538 sset_destroy(&options->targets);
539 free(options->virtual_obs_id);
540 free(options);
541 }
542 }
543
544 static bool
545 ofproto_ipfix_flow_exporter_options_equal(
546 const struct ofproto_ipfix_flow_exporter_options *a,
547 const struct ofproto_ipfix_flow_exporter_options *b)
548 {
549 return (a->collector_set_id == b->collector_set_id
550 && a->cache_active_timeout == b->cache_active_timeout
551 && a->cache_max_flows == b->cache_max_flows
552 && a->enable_tunnel_sampling == b->enable_tunnel_sampling
553 && sset_equals(&a->targets, &b->targets)
554 && nullable_string_is_equal(a->virtual_obs_id, b->virtual_obs_id));
555 }
556
557 static struct ofproto_ipfix_flow_exporter_options *
558 ofproto_ipfix_flow_exporter_options_clone(
559 const struct ofproto_ipfix_flow_exporter_options *old)
560 {
561 struct ofproto_ipfix_flow_exporter_options *new =
562 xmemdup(old, sizeof *old);
563 sset_clone(&new->targets, &old->targets);
564 new->virtual_obs_id = nullable_xstrdup(old->virtual_obs_id);
565 return new;
566 }
567
568 static void
569 ofproto_ipfix_flow_exporter_options_destroy(
570 struct ofproto_ipfix_flow_exporter_options *options)
571 {
572 if (options) {
573 sset_destroy(&options->targets);
574 free(options->virtual_obs_id);
575 free(options);
576 }
577 }
578
579 static void
580 dpif_ipfix_exporter_init(struct dpif_ipfix_exporter *exporter)
581 {
582 exporter->collectors = NULL;
583 exporter->seq_number = 1;
584 exporter->last_template_set_time = 0;
585 hmap_init(&exporter->cache_flow_key_map);
586 ovs_list_init(&exporter->cache_flow_start_timestamp_list);
587 exporter->cache_active_timeout = 0;
588 exporter->cache_max_flows = 0;
589 exporter->virtual_obs_id = NULL;
590 exporter->virtual_obs_len = 0;
591
592 memset(&exporter->ipfix_global_stats, 0,
593 sizeof(struct dpif_ipfix_global_stats));
594 }
595
596 static void
597 dpif_ipfix_exporter_clear(struct dpif_ipfix_exporter *exporter)
598 {
599 /* Flush the cache with flow end reason "forced end." */
600 dpif_ipfix_cache_expire_now(exporter, true);
601
602 collectors_destroy(exporter->collectors);
603 exporter->collectors = NULL;
604 exporter->seq_number = 1;
605 exporter->last_template_set_time = 0;
606 exporter->cache_active_timeout = 0;
607 exporter->cache_max_flows = 0;
608 free(exporter->virtual_obs_id);
609 exporter->virtual_obs_id = NULL;
610 exporter->virtual_obs_len = 0;
611
612 memset(&exporter->ipfix_global_stats, 0,
613 sizeof(struct dpif_ipfix_global_stats));
614 }
615
616 static void
617 dpif_ipfix_exporter_destroy(struct dpif_ipfix_exporter *exporter)
618 {
619 dpif_ipfix_exporter_clear(exporter);
620 hmap_destroy(&exporter->cache_flow_key_map);
621 }
622
623 static bool
624 dpif_ipfix_exporter_set_options(struct dpif_ipfix_exporter *exporter,
625 const struct sset *targets,
626 const uint32_t cache_active_timeout,
627 const uint32_t cache_max_flows,
628 const char *virtual_obs_id)
629 {
630 size_t virtual_obs_len;
631 collectors_destroy(exporter->collectors);
632 collectors_create(targets, IPFIX_DEFAULT_COLLECTOR_PORT,
633 &exporter->collectors);
634 if (exporter->collectors == NULL) {
635 VLOG_WARN_RL(&rl, "no collectors could be initialized, "
636 "IPFIX exporter disabled");
637 dpif_ipfix_exporter_clear(exporter);
638 return false;
639 }
640 exporter->cache_active_timeout = cache_active_timeout;
641 exporter->cache_max_flows = cache_max_flows;
642 virtual_obs_len = virtual_obs_id ? strlen(virtual_obs_id) : 0;
643 if (virtual_obs_len > IPFIX_VIRTUAL_OBS_MAX_LEN) {
644 VLOG_WARN_RL(&rl, "Virtual obsevation ID too long (%d bytes), "
645 "should not be longer than %d bytes.",
646 exporter->virtual_obs_len, IPFIX_VIRTUAL_OBS_MAX_LEN);
647 dpif_ipfix_exporter_clear(exporter);
648 return false;
649 }
650 exporter->virtual_obs_len = virtual_obs_len;
651 exporter->virtual_obs_id = nullable_xstrdup(virtual_obs_id);
652 return true;
653 }
654
655 static struct dpif_ipfix_port *
656 dpif_ipfix_find_port(const struct dpif_ipfix *di,
657 odp_port_t odp_port) OVS_REQUIRES(mutex)
658 {
659 struct dpif_ipfix_port *dip;
660
661 HMAP_FOR_EACH_IN_BUCKET (dip, hmap_node, hash_odp_port(odp_port),
662 &di->tunnel_ports) {
663 if (dip->odp_port == odp_port) {
664 return dip;
665 }
666 }
667 return NULL;
668 }
669
670 static void
671 dpif_ipfix_del_port(struct dpif_ipfix *di,
672 struct dpif_ipfix_port *dip)
673 OVS_REQUIRES(mutex)
674 {
675 hmap_remove(&di->tunnel_ports, &dip->hmap_node);
676 free(dip);
677 }
678
679 void
680 dpif_ipfix_add_tunnel_port(struct dpif_ipfix *di, struct ofport *ofport,
681 odp_port_t odp_port) OVS_EXCLUDED(mutex)
682 {
683 struct dpif_ipfix_port *dip;
684 const char *type;
685
686 ovs_mutex_lock(&mutex);
687 dip = dpif_ipfix_find_port(di, odp_port);
688 if (dip) {
689 dpif_ipfix_del_port(di, dip);
690 }
691
692 type = netdev_get_type(ofport->netdev);
693 if (type == NULL) {
694 goto out;
695 }
696
697 /* Add to table of tunnel ports. */
698 dip = xmalloc(sizeof *dip);
699 dip->ofport = ofport;
700 dip->odp_port = odp_port;
701 if (strcmp(type, "gre") == 0) {
702 /* 32-bit key gre */
703 dip->tunnel_type = DPIF_IPFIX_TUNNEL_GRE;
704 dip->tunnel_key_length = 4;
705 } else if (strcmp(type, "vxlan") == 0) {
706 dip->tunnel_type = DPIF_IPFIX_TUNNEL_VXLAN;
707 dip->tunnel_key_length = 3;
708 } else if (strcmp(type, "lisp") == 0) {
709 dip->tunnel_type = DPIF_IPFIX_TUNNEL_LISP;
710 dip->tunnel_key_length = 3;
711 } else if (strcmp(type, "geneve") == 0) {
712 dip->tunnel_type = DPIF_IPFIX_TUNNEL_GENEVE;
713 dip->tunnel_key_length = 3;
714 } else if (strcmp(type, "stt") == 0) {
715 dip->tunnel_type = DPIF_IPFIX_TUNNEL_STT;
716 dip->tunnel_key_length = 8;
717 } else {
718 free(dip);
719 goto out;
720 }
721 hmap_insert(&di->tunnel_ports, &dip->hmap_node, hash_odp_port(odp_port));
722
723 out:
724 ovs_mutex_unlock(&mutex);
725 }
726
727 void
728 dpif_ipfix_del_tunnel_port(struct dpif_ipfix *di, odp_port_t odp_port)
729 OVS_EXCLUDED(mutex)
730 {
731 struct dpif_ipfix_port *dip;
732 ovs_mutex_lock(&mutex);
733 dip = dpif_ipfix_find_port(di, odp_port);
734 if (dip) {
735 dpif_ipfix_del_port(di, dip);
736 }
737 ovs_mutex_unlock(&mutex);
738 }
739
740 bool
741 dpif_ipfix_get_tunnel_port(const struct dpif_ipfix *di, odp_port_t odp_port)
742 OVS_EXCLUDED(mutex)
743 {
744 struct dpif_ipfix_port *dip;
745 ovs_mutex_lock(&mutex);
746 dip = dpif_ipfix_find_port(di, odp_port);
747 ovs_mutex_unlock(&mutex);
748 return dip != NULL;
749 }
750
751 static void
752 dpif_ipfix_bridge_exporter_init(struct dpif_ipfix_bridge_exporter *exporter)
753 {
754 dpif_ipfix_exporter_init(&exporter->exporter);
755 exporter->options = NULL;
756 exporter->probability = 0;
757 }
758
759 static void
760 dpif_ipfix_bridge_exporter_clear(struct dpif_ipfix_bridge_exporter *exporter)
761 {
762 dpif_ipfix_exporter_clear(&exporter->exporter);
763 ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
764 exporter->options = NULL;
765 exporter->probability = 0;
766 }
767
768 static void
769 dpif_ipfix_bridge_exporter_destroy(struct dpif_ipfix_bridge_exporter *exporter)
770 {
771 dpif_ipfix_bridge_exporter_clear(exporter);
772 dpif_ipfix_exporter_destroy(&exporter->exporter);
773 }
774
775 static void
776 dpif_ipfix_bridge_exporter_set_options(
777 struct dpif_ipfix_bridge_exporter *exporter,
778 const struct ofproto_ipfix_bridge_exporter_options *options)
779 {
780 bool options_changed;
781
782 if (!options || sset_is_empty(&options->targets)) {
783 /* No point in doing any work if there are no targets. */
784 dpif_ipfix_bridge_exporter_clear(exporter);
785 return;
786 }
787
788 options_changed = (
789 !exporter->options
790 || !ofproto_ipfix_bridge_exporter_options_equal(
791 options, exporter->options));
792
793 /* Configure collectors if options have changed or if we're
794 * shortchanged in collectors (which indicates that opening one or
795 * more of the configured collectors failed, so that we should
796 * retry). */
797 if (options_changed
798 || collectors_count(exporter->exporter.collectors)
799 < sset_count(&options->targets)) {
800 if (!dpif_ipfix_exporter_set_options(
801 &exporter->exporter, &options->targets,
802 options->cache_active_timeout, options->cache_max_flows,
803 options->virtual_obs_id)) {
804 return;
805 }
806 }
807
808 /* Avoid reconfiguring if options didn't change. */
809 if (!options_changed) {
810 return;
811 }
812
813 ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
814 exporter->options = ofproto_ipfix_bridge_exporter_options_clone(options);
815 exporter->probability =
816 MAX(1, UINT32_MAX / exporter->options->sampling_rate);
817
818 /* Run over the cache as some entries might have expired after
819 * changing the timeouts. */
820 dpif_ipfix_cache_expire_now(&exporter->exporter, false);
821 }
822
823 static struct dpif_ipfix_flow_exporter_map_node*
824 dpif_ipfix_find_flow_exporter_map_node(
825 const struct dpif_ipfix *di, const uint32_t collector_set_id)
826 OVS_REQUIRES(mutex)
827 {
828 struct dpif_ipfix_flow_exporter_map_node *exporter_node;
829
830 HMAP_FOR_EACH_WITH_HASH (exporter_node, node,
831 hash_int(collector_set_id, 0),
832 &di->flow_exporter_map) {
833 if (exporter_node->exporter.options->collector_set_id
834 == collector_set_id) {
835 return exporter_node;
836 }
837 }
838
839 return NULL;
840 }
841
842 static void
843 dpif_ipfix_flow_exporter_init(struct dpif_ipfix_flow_exporter *exporter)
844 {
845 dpif_ipfix_exporter_init(&exporter->exporter);
846 exporter->options = NULL;
847 }
848
849 static void
850 dpif_ipfix_flow_exporter_clear(struct dpif_ipfix_flow_exporter *exporter)
851 {
852 dpif_ipfix_exporter_clear(&exporter->exporter);
853 ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
854 exporter->options = NULL;
855 }
856
857 static void
858 dpif_ipfix_flow_exporter_destroy(struct dpif_ipfix_flow_exporter *exporter)
859 {
860 dpif_ipfix_flow_exporter_clear(exporter);
861 dpif_ipfix_exporter_destroy(&exporter->exporter);
862 }
863
864 static bool
865 dpif_ipfix_flow_exporter_set_options(
866 struct dpif_ipfix_flow_exporter *exporter,
867 const struct ofproto_ipfix_flow_exporter_options *options)
868 {
869 bool options_changed;
870
871 if (sset_is_empty(&options->targets)) {
872 /* No point in doing any work if there are no targets. */
873 dpif_ipfix_flow_exporter_clear(exporter);
874 return true;
875 }
876
877 options_changed = (
878 !exporter->options
879 || !ofproto_ipfix_flow_exporter_options_equal(
880 options, exporter->options));
881
882 /* Configure collectors if options have changed or if we're
883 * shortchanged in collectors (which indicates that opening one or
884 * more of the configured collectors failed, so that we should
885 * retry). */
886 if (options_changed
887 || collectors_count(exporter->exporter.collectors)
888 < sset_count(&options->targets)) {
889 if (!dpif_ipfix_exporter_set_options(
890 &exporter->exporter, &options->targets,
891 options->cache_active_timeout, options->cache_max_flows,
892 options->virtual_obs_id)) {
893 return false;
894 }
895 }
896
897 /* Avoid reconfiguring if options didn't change. */
898 if (!options_changed) {
899 return true;
900 }
901
902 ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
903 exporter->options = ofproto_ipfix_flow_exporter_options_clone(options);
904
905 /* Run over the cache as some entries might have expired after
906 * changing the timeouts. */
907 dpif_ipfix_cache_expire_now(&exporter->exporter, false);
908
909 return true;
910 }
911
912 static void
913 remove_flow_exporter(struct dpif_ipfix *di,
914 struct dpif_ipfix_flow_exporter_map_node *node)
915 {
916 hmap_remove(&di->flow_exporter_map, &node->node);
917 dpif_ipfix_flow_exporter_destroy(&node->exporter);
918 free(node);
919 }
920
921 void
922 dpif_ipfix_set_options(
923 struct dpif_ipfix *di,
924 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
925 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
926 size_t n_flow_exporters_options) OVS_EXCLUDED(mutex)
927 {
928 int i;
929 struct ofproto_ipfix_flow_exporter_options *options;
930 struct dpif_ipfix_flow_exporter_map_node *node, *next;
931
932 ovs_mutex_lock(&mutex);
933 dpif_ipfix_bridge_exporter_set_options(&di->bridge_exporter,
934 bridge_exporter_options);
935
936 /* Add new flow exporters and update current flow exporters. */
937 options = (struct ofproto_ipfix_flow_exporter_options *)
938 flow_exporters_options;
939 for (i = 0; i < n_flow_exporters_options; i++) {
940 node = dpif_ipfix_find_flow_exporter_map_node(
941 di, options->collector_set_id);
942 if (!node) {
943 node = xzalloc(sizeof *node);
944 dpif_ipfix_flow_exporter_init(&node->exporter);
945 hmap_insert(&di->flow_exporter_map, &node->node,
946 hash_int(options->collector_set_id, 0));
947 }
948 if (!dpif_ipfix_flow_exporter_set_options(&node->exporter, options)) {
949 remove_flow_exporter(di, node);
950 }
951 options++;
952 }
953
954 /* Remove dropped flow exporters, if any needs to be removed. */
955 HMAP_FOR_EACH_SAFE (node, next, node, &di->flow_exporter_map) {
956 /* This is slow but doesn't take any extra memory, and
957 * this table is not supposed to contain many rows anyway. */
958 options = (struct ofproto_ipfix_flow_exporter_options *)
959 flow_exporters_options;
960 for (i = 0; i < n_flow_exporters_options; i++) {
961 if (node->exporter.options->collector_set_id
962 == options->collector_set_id) {
963 break;
964 }
965 options++;
966 }
967 if (i == n_flow_exporters_options) { // Not found.
968 remove_flow_exporter(di, node);
969 }
970 }
971
972 ovs_mutex_unlock(&mutex);
973 }
974
975 struct dpif_ipfix *
976 dpif_ipfix_create(void)
977 {
978 struct dpif_ipfix *di;
979 di = xzalloc(sizeof *di);
980 dpif_ipfix_bridge_exporter_init(&di->bridge_exporter);
981 hmap_init(&di->flow_exporter_map);
982 hmap_init(&di->tunnel_ports);
983 ovs_refcount_init(&di->ref_cnt);
984 return di;
985 }
986
987 struct dpif_ipfix *
988 dpif_ipfix_ref(const struct dpif_ipfix *di_)
989 {
990 struct dpif_ipfix *di = CONST_CAST(struct dpif_ipfix *, di_);
991 if (di) {
992 ovs_refcount_ref(&di->ref_cnt);
993 }
994 return di;
995 }
996
997 uint32_t
998 dpif_ipfix_get_bridge_exporter_probability(const struct dpif_ipfix *di)
999 OVS_EXCLUDED(mutex)
1000 {
1001 uint32_t ret;
1002 ovs_mutex_lock(&mutex);
1003 ret = di->bridge_exporter.probability;
1004 ovs_mutex_unlock(&mutex);
1005 return ret;
1006 }
1007
1008 bool
1009 dpif_ipfix_get_bridge_exporter_input_sampling(const struct dpif_ipfix *di)
1010 OVS_EXCLUDED(mutex)
1011 {
1012 bool ret = false;
1013 ovs_mutex_lock(&mutex);
1014 if (di->bridge_exporter.options) {
1015 ret = di->bridge_exporter.options->enable_input_sampling;
1016 }
1017 ovs_mutex_unlock(&mutex);
1018 return ret;
1019 }
1020
1021 bool
1022 dpif_ipfix_get_bridge_exporter_output_sampling(const struct dpif_ipfix *di)
1023 OVS_EXCLUDED(mutex)
1024 {
1025 bool ret = false;
1026 ovs_mutex_lock(&mutex);
1027 if (di->bridge_exporter.options) {
1028 ret = di->bridge_exporter.options->enable_output_sampling;
1029 }
1030 ovs_mutex_unlock(&mutex);
1031 return ret;
1032 }
1033
1034 bool
1035 dpif_ipfix_get_bridge_exporter_tunnel_sampling(const struct dpif_ipfix *di)
1036 OVS_EXCLUDED(mutex)
1037 {
1038 bool ret = false;
1039 ovs_mutex_lock(&mutex);
1040 if (di->bridge_exporter.options) {
1041 ret = di->bridge_exporter.options->enable_tunnel_sampling;
1042 }
1043 ovs_mutex_unlock(&mutex);
1044 return ret;
1045 }
1046
1047 bool
1048 dpif_ipfix_get_flow_exporter_tunnel_sampling(const struct dpif_ipfix *di,
1049 const uint32_t collector_set_id)
1050 OVS_EXCLUDED(mutex)
1051 {
1052 ovs_mutex_lock(&mutex);
1053 struct dpif_ipfix_flow_exporter_map_node *node
1054 = dpif_ipfix_find_flow_exporter_map_node(di, collector_set_id);
1055 bool ret = (node
1056 && node->exporter.options
1057 && node->exporter.options->enable_tunnel_sampling);
1058 ovs_mutex_unlock(&mutex);
1059
1060 return ret;
1061 }
1062
1063 static void
1064 dpif_ipfix_clear(struct dpif_ipfix *di) OVS_REQUIRES(mutex)
1065 {
1066 struct dpif_ipfix_flow_exporter_map_node *exp_node;
1067 struct dpif_ipfix_port *dip, *next;
1068
1069 dpif_ipfix_bridge_exporter_clear(&di->bridge_exporter);
1070
1071 HMAP_FOR_EACH_POP (exp_node, node, &di->flow_exporter_map) {
1072 dpif_ipfix_flow_exporter_destroy(&exp_node->exporter);
1073 free(exp_node);
1074 }
1075
1076 HMAP_FOR_EACH_SAFE (dip, next, hmap_node, &di->tunnel_ports) {
1077 dpif_ipfix_del_port(di, dip);
1078 }
1079 }
1080
1081 void
1082 dpif_ipfix_unref(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
1083 {
1084 if (di && ovs_refcount_unref_relaxed(&di->ref_cnt) == 1) {
1085 ovs_mutex_lock(&mutex);
1086 dpif_ipfix_clear(di);
1087 dpif_ipfix_bridge_exporter_destroy(&di->bridge_exporter);
1088 hmap_destroy(&di->flow_exporter_map);
1089 hmap_destroy(&di->tunnel_ports);
1090 free(di);
1091 ovs_mutex_unlock(&mutex);
1092 }
1093 }
1094
1095 static void
1096 ipfix_init_header(uint32_t export_time_sec, uint32_t seq_number,
1097 uint32_t obs_domain_id, struct dp_packet *msg)
1098 {
1099 struct ipfix_header *hdr;
1100
1101 hdr = dp_packet_put_zeros(msg, sizeof *hdr);
1102 hdr->version = htons(IPFIX_VERSION);
1103 hdr->length = htons(sizeof *hdr); /* Updated in ipfix_send_msg. */
1104 hdr->export_time = htonl(export_time_sec);
1105 hdr->seq_number = htonl(seq_number);
1106 hdr->obs_domain_id = htonl(obs_domain_id);
1107 }
1108
1109 static size_t
1110 ipfix_send_msg(const struct collectors *collectors, struct dp_packet *msg)
1111 {
1112 struct ipfix_header *hdr;
1113 size_t tx_errors;
1114
1115 /* Adjust the length in the header. */
1116 hdr = dp_packet_data(msg);
1117 hdr->length = htons(dp_packet_size(msg));
1118
1119 tx_errors = collectors_send(collectors,
1120 dp_packet_data(msg), dp_packet_size(msg));
1121 dp_packet_set_size(msg, 0);
1122
1123 return tx_errors;
1124 }
1125
1126 static uint16_t
1127 ipfix_get_template_id(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
1128 enum ipfix_proto_l4 l4, enum ipfix_proto_tunnel tunnel)
1129 {
1130 uint16_t template_id;
1131 template_id = l2;
1132 template_id = template_id * NUM_IPFIX_PROTO_L3 + l3;
1133 template_id = template_id * NUM_IPFIX_PROTO_L4 + l4;
1134 template_id = template_id * NUM_IPFIX_PROTO_TUNNEL + tunnel;
1135 return IPFIX_TEMPLATE_ID_MIN + template_id;
1136 }
1137
1138 static void
1139 ipfix_define_template_entity(enum ipfix_entity_id id,
1140 enum ipfix_entity_size size,
1141 enum ipfix_entity_enterprise enterprise,
1142 struct dp_packet *msg)
1143 {
1144 struct ipfix_template_field_specifier *field;
1145 size_t field_size;
1146
1147 if (enterprise) {
1148 field_size = sizeof *field;
1149 } else {
1150 /* No enterprise number */
1151 field_size = sizeof *field - sizeof(ovs_be32);
1152 }
1153 field = dp_packet_put_zeros(msg, field_size);
1154 field->element_id = htons(id);
1155 if (size) {
1156 field->field_length = htons(size);
1157 } else {
1158 /* RFC 5101, Section 7. Variable-Length Information Element */
1159 field->field_length = OVS_BE16_MAX;
1160 }
1161 if (enterprise) {
1162 field->enterprise = htonl(enterprise);
1163 }
1164
1165 }
1166
1167 static uint16_t
1168 ipfix_define_template_fields(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
1169 enum ipfix_proto_l4 l4, enum ipfix_proto_tunnel tunnel,
1170 bool virtual_obs_id_set,
1171 struct dp_packet *msg)
1172 {
1173 uint16_t count = 0;
1174
1175 #define DEF(ID) \
1176 { \
1177 ipfix_define_template_entity(IPFIX_ENTITY_ID_##ID, \
1178 IPFIX_ENTITY_SIZE_##ID, \
1179 IPFIX_ENTITY_ENTERPRISE_##ID, msg); \
1180 count++; \
1181 }
1182
1183 /* 1. Flow key. */
1184
1185 DEF(OBSERVATION_POINT_ID);
1186 DEF(FLOW_DIRECTION);
1187
1188 /* Common Ethernet entities. */
1189 DEF(SOURCE_MAC_ADDRESS);
1190 DEF(DESTINATION_MAC_ADDRESS);
1191 DEF(ETHERNET_TYPE);
1192 DEF(ETHERNET_HEADER_LENGTH);
1193
1194 if (l2 == IPFIX_PROTO_L2_VLAN) {
1195 DEF(VLAN_ID);
1196 DEF(DOT1Q_VLAN_ID);
1197 DEF(DOT1Q_PRIORITY);
1198 }
1199
1200 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
1201 DEF(IP_VERSION);
1202 DEF(IP_TTL);
1203 DEF(PROTOCOL_IDENTIFIER);
1204 DEF(IP_DIFF_SERV_CODE_POINT);
1205 DEF(IP_PRECEDENCE);
1206 DEF(IP_CLASS_OF_SERVICE);
1207
1208 if (l3 == IPFIX_PROTO_L3_IPV4) {
1209 DEF(SOURCE_IPV4_ADDRESS);
1210 DEF(DESTINATION_IPV4_ADDRESS);
1211 if (l4 == IPFIX_PROTO_L4_TCP
1212 || l4 == IPFIX_PROTO_L4_UDP
1213 || l4 == IPFIX_PROTO_L4_SCTP) {
1214 DEF(SOURCE_TRANSPORT_PORT);
1215 DEF(DESTINATION_TRANSPORT_PORT);
1216 } else if (l4 == IPFIX_PROTO_L4_ICMP) {
1217 DEF(ICMP_TYPE_IPV4);
1218 DEF(ICMP_CODE_IPV4);
1219 }
1220 } else { /* l3 == IPFIX_PROTO_L3_IPV6 */
1221 DEF(SOURCE_IPV6_ADDRESS);
1222 DEF(DESTINATION_IPV6_ADDRESS);
1223 DEF(FLOW_LABEL_IPV6);
1224 if (l4 == IPFIX_PROTO_L4_TCP
1225 || l4 == IPFIX_PROTO_L4_UDP
1226 || l4 == IPFIX_PROTO_L4_SCTP) {
1227 DEF(SOURCE_TRANSPORT_PORT);
1228 DEF(DESTINATION_TRANSPORT_PORT);
1229 } else if (l4 == IPFIX_PROTO_L4_ICMP) {
1230 DEF(ICMP_TYPE_IPV6);
1231 DEF(ICMP_CODE_IPV6);
1232 }
1233 }
1234 }
1235
1236 if (tunnel != IPFIX_PROTO_NOT_TUNNELED) {
1237 DEF(TUNNEL_SOURCE_IPV4_ADDRESS);
1238 DEF(TUNNEL_DESTINATION_IPV4_ADDRESS);
1239 DEF(TUNNEL_PROTOCOL_IDENTIFIER);
1240 DEF(TUNNEL_SOURCE_TRANSPORT_PORT);
1241 DEF(TUNNEL_DESTINATION_TRANSPORT_PORT);
1242 DEF(TUNNEL_TYPE);
1243 DEF(TUNNEL_KEY);
1244 }
1245
1246 /* 2. Virtual observation ID, which is not a part of flow key. */
1247 if (virtual_obs_id_set) {
1248 DEF(VIRTUAL_OBS_ID);
1249 }
1250
1251 /* 3. Flow aggregated data. */
1252
1253 DEF(FLOW_START_DELTA_MICROSECONDS);
1254 DEF(FLOW_END_DELTA_MICROSECONDS);
1255 DEF(PACKET_DELTA_COUNT);
1256 DEF(PACKET_TOTAL_COUNT);
1257 DEF(LAYER2_OCTET_DELTA_COUNT);
1258 DEF(LAYER2_OCTET_TOTAL_COUNT);
1259 DEF(FLOW_END_REASON);
1260
1261 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
1262 DEF(OCTET_DELTA_COUNT);
1263 DEF(OCTET_TOTAL_COUNT);
1264 DEF(OCTET_DELTA_SUM_OF_SQUARES);
1265 DEF(OCTET_TOTAL_SUM_OF_SQUARES);
1266 DEF(MINIMUM_IP_TOTAL_LENGTH);
1267 DEF(MAXIMUM_IP_TOTAL_LENGTH);
1268 }
1269
1270 if (l4 == IPFIX_PROTO_L4_TCP) {
1271 DEF(TCP_ACK_TOTAL_COUNT);
1272 DEF(TCP_FIN_TOTAL_COUNT);
1273 DEF(TCP_PSH_TOTAL_COUNT);
1274 DEF(TCP_RST_TOTAL_COUNT);
1275 DEF(TCP_SYN_TOTAL_COUNT);
1276 DEF(TCP_URG_TOTAL_COUNT);
1277 }
1278 #undef DEF
1279
1280 return count;
1281 }
1282
1283 static void
1284 ipfix_init_template_msg(uint32_t export_time_sec,
1285 uint32_t seq_number, uint32_t obs_domain_id,
1286 struct dp_packet *msg, size_t *set_hdr_offset)
1287 {
1288 struct ipfix_set_header *set_hdr;
1289
1290 dp_packet_clear(msg);
1291
1292 ipfix_init_header(export_time_sec, seq_number, obs_domain_id, msg);
1293 *set_hdr_offset = dp_packet_size(msg);
1294
1295 /* Add a Template Set. */
1296 set_hdr = dp_packet_put_zeros(msg, sizeof *set_hdr);
1297 set_hdr->set_id = htons(IPFIX_SET_ID_TEMPLATE);
1298 }
1299
1300 static size_t
1301 ipfix_send_template_msg(const struct collectors *collectors,
1302 struct dp_packet *msg, size_t set_hdr_offset)
1303 {
1304 struct ipfix_set_header *set_hdr;
1305 size_t tx_errors;
1306
1307 /* Send template message. */
1308 set_hdr = (struct ipfix_set_header*)
1309 ((uint8_t*)dp_packet_data(msg) + set_hdr_offset);
1310 set_hdr->length = htons(dp_packet_size(msg) - set_hdr_offset);
1311
1312 tx_errors = ipfix_send_msg(collectors, msg);
1313
1314 return tx_errors;
1315 }
1316
1317 static void
1318 ipfix_send_template_msgs(struct dpif_ipfix_exporter *exporter,
1319 uint32_t export_time_sec, uint32_t obs_domain_id)
1320 {
1321 uint64_t msg_stub[DIV_ROUND_UP(MAX_MESSAGE_LEN, 8)];
1322 struct dp_packet msg;
1323 dp_packet_use_stub(&msg, msg_stub, sizeof msg_stub);
1324
1325 size_t set_hdr_offset, tmpl_hdr_offset, error_pkts;
1326 struct ipfix_template_record_header *tmpl_hdr;
1327 uint16_t field_count;
1328 size_t tx_packets = 0;
1329 size_t tx_errors = 0;
1330 enum ipfix_proto_l2 l2;
1331 enum ipfix_proto_l3 l3;
1332 enum ipfix_proto_l4 l4;
1333 enum ipfix_proto_tunnel tunnel;
1334
1335 ipfix_init_template_msg(export_time_sec, exporter->seq_number,
1336 obs_domain_id, &msg, &set_hdr_offset);
1337 /* Define one template for each possible combination of
1338 * protocols. */
1339 for (l2 = 0; l2 < NUM_IPFIX_PROTO_L2; l2++) {
1340 for (l3 = 0; l3 < NUM_IPFIX_PROTO_L3; l3++) {
1341 for (l4 = 0; l4 < NUM_IPFIX_PROTO_L4; l4++) {
1342 if (l3 == IPFIX_PROTO_L3_UNKNOWN &&
1343 l4 != IPFIX_PROTO_L4_UNKNOWN) {
1344 continue;
1345 }
1346 for (tunnel = 0; tunnel < NUM_IPFIX_PROTO_TUNNEL; tunnel++) {
1347 /* When the size of the template packet reaches
1348 * MAX_MESSAGE_LEN(1024), send it out.
1349 * And then reinitialize the msg to construct a new
1350 * packet for the following templates.
1351 */
1352 if (dp_packet_size(&msg) >= MAX_MESSAGE_LEN) {
1353 /* Send template message. */
1354 error_pkts = ipfix_send_template_msg(exporter->collectors,
1355 &msg, set_hdr_offset);
1356 tx_errors += error_pkts;
1357 tx_packets += collectors_count(exporter->collectors) - error_pkts;
1358
1359 /* Reinitialize the template msg. */
1360 ipfix_init_template_msg(export_time_sec,
1361 exporter->seq_number,
1362 obs_domain_id, &msg,
1363 &set_hdr_offset);
1364 }
1365
1366 tmpl_hdr_offset = dp_packet_size(&msg);
1367 tmpl_hdr = dp_packet_put_zeros(&msg, sizeof *tmpl_hdr);
1368 tmpl_hdr->template_id = htons(
1369 ipfix_get_template_id(l2, l3, l4, tunnel));
1370 field_count = ipfix_define_template_fields(
1371 l2, l3, l4, tunnel, exporter->virtual_obs_id != NULL,
1372 &msg);
1373 tmpl_hdr = (struct ipfix_template_record_header*)
1374 ((uint8_t*)dp_packet_data(&msg) + tmpl_hdr_offset);
1375 tmpl_hdr->field_count = htons(field_count);
1376 }
1377 }
1378 }
1379 }
1380
1381 /* Send template message. */
1382 error_pkts = ipfix_send_template_msg(exporter->collectors, &msg, set_hdr_offset);
1383 tx_errors += error_pkts;
1384 tx_packets += collectors_count(exporter->collectors) - error_pkts;
1385
1386 exporter->ofproto_stats.tx_pkts += tx_packets;
1387 exporter->ofproto_stats.tx_errors += tx_errors;
1388
1389 /* XXX: Add Options Template Sets, at least to define a Flow Keys
1390 * Option Template. */
1391
1392 dp_packet_uninit(&msg);
1393 }
1394
1395 static inline uint32_t
1396 ipfix_hash_flow_key(const struct ipfix_flow_key *flow_key, uint32_t basis)
1397 {
1398 uint32_t hash;
1399 hash = hash_int(flow_key->obs_domain_id, basis);
1400 hash = hash_int(flow_key->template_id, hash);
1401 hash = hash_bytes(flow_key->flow_key_msg_part,
1402 flow_key->flow_key_msg_part_size, hash);
1403 return hash;
1404 }
1405
1406 static bool
1407 ipfix_flow_key_equal(const struct ipfix_flow_key *a,
1408 const struct ipfix_flow_key *b)
1409 {
1410 /* The template ID determines the flow key size, so not need to
1411 * compare it. */
1412 return (a->obs_domain_id == b->obs_domain_id
1413 && a->template_id == b->template_id
1414 && memcmp(a->flow_key_msg_part, b->flow_key_msg_part,
1415 a->flow_key_msg_part_size) == 0);
1416 }
1417
1418 static struct ipfix_flow_cache_entry*
1419 ipfix_cache_find_entry(const struct dpif_ipfix_exporter *exporter,
1420 const struct ipfix_flow_key *flow_key)
1421 {
1422 struct ipfix_flow_cache_entry *entry;
1423
1424 HMAP_FOR_EACH_WITH_HASH (entry, flow_key_map_node,
1425 ipfix_hash_flow_key(flow_key, 0),
1426 &exporter->cache_flow_key_map) {
1427 if (ipfix_flow_key_equal(&entry->flow_key, flow_key)) {
1428 return entry;
1429 }
1430 }
1431
1432 return NULL;
1433 }
1434
1435 static bool
1436 ipfix_cache_next_timeout_msec(const struct dpif_ipfix_exporter *exporter,
1437 long long int *next_timeout_msec)
1438 {
1439 struct ipfix_flow_cache_entry *entry;
1440
1441 LIST_FOR_EACH (entry, cache_flow_start_timestamp_list_node,
1442 &exporter->cache_flow_start_timestamp_list) {
1443 *next_timeout_msec = entry->flow_start_timestamp_usec / 1000LL
1444 + 1000LL * exporter->cache_active_timeout;
1445 return true;
1446 }
1447
1448 return false;
1449 }
1450
1451 static void
1452 ipfix_cache_aggregate_entries(struct ipfix_flow_cache_entry *from_entry,
1453 struct ipfix_flow_cache_entry *to_entry)
1454 {
1455 uint64_t *to_start, *to_end, *from_start, *from_end;
1456 uint16_t *to_min_len, *to_max_len, *from_min_len, *from_max_len;
1457
1458 to_start = &to_entry->flow_start_timestamp_usec;
1459 to_end = &to_entry->flow_end_timestamp_usec;
1460 from_start = &from_entry->flow_start_timestamp_usec;
1461 from_end = &from_entry->flow_end_timestamp_usec;
1462
1463 if (*to_start > *from_start) {
1464 *to_start = *from_start;
1465 }
1466 if (*to_end < *from_end) {
1467 *to_end = *from_end;
1468 }
1469
1470 to_entry->packet_delta_count += from_entry->packet_delta_count;
1471 to_entry->layer2_octet_delta_count += from_entry->layer2_octet_delta_count;
1472
1473 to_entry->packet_total_count = from_entry->packet_total_count;
1474 to_entry->layer2_octet_total_count = from_entry->layer2_octet_total_count;
1475
1476 to_entry->octet_delta_count += from_entry->octet_delta_count;
1477 to_entry->octet_delta_sum_of_squares +=
1478 from_entry->octet_delta_sum_of_squares;
1479
1480 to_entry->octet_total_count = from_entry->octet_total_count;
1481 to_entry->octet_total_sum_of_squares =
1482 from_entry->octet_total_sum_of_squares;
1483
1484 to_min_len = &to_entry->minimum_ip_total_length;
1485 to_max_len = &to_entry->maximum_ip_total_length;
1486 from_min_len = &from_entry->minimum_ip_total_length;
1487 from_max_len = &from_entry->maximum_ip_total_length;
1488
1489 if (!*to_min_len || (*from_min_len && *to_min_len > *from_min_len)) {
1490 *to_min_len = *from_min_len;
1491 }
1492 if (*to_max_len < *from_max_len) {
1493 *to_max_len = *from_max_len;
1494 }
1495
1496 to_entry->tcp_packet_delta_count += from_entry->tcp_packet_delta_count;
1497 to_entry->tcp_ack_total_count = from_entry->tcp_ack_total_count;
1498 to_entry->tcp_fin_total_count = from_entry->tcp_fin_total_count;
1499 to_entry->tcp_psh_total_count = from_entry->tcp_psh_total_count;
1500 to_entry->tcp_rst_total_count = from_entry->tcp_rst_total_count;
1501 to_entry->tcp_syn_total_count = from_entry->tcp_syn_total_count;
1502 to_entry->tcp_urg_total_count = from_entry->tcp_urg_total_count;
1503 }
1504
1505 /* Get statistics */
1506 static void
1507 ipfix_get_stats__(const struct dpif_ipfix_exporter *exporter,
1508 ofproto_ipfix_stats *stats)
1509 {
1510 memset(stats, 0xff, sizeof *stats);
1511
1512 if (!exporter) {
1513 return;
1514 }
1515
1516 *stats = exporter->ofproto_stats;
1517 }
1518
1519 static void
1520 ipfix_get_bridge_stats(const struct dpif_ipfix_bridge_exporter *exporter,
1521 ofproto_ipfix_stats *stats)
1522 {
1523 ipfix_get_stats__(&exporter->exporter, stats);
1524 }
1525
1526 static void
1527 ipfix_get_flow_stats(const struct dpif_ipfix_flow_exporter *exporter,
1528 ofproto_ipfix_stats *stats)
1529 {
1530 ipfix_get_stats__(&exporter->exporter, stats);
1531 stats->collector_set_id = exporter->options->collector_set_id;
1532 }
1533
1534 int
1535 dpif_ipfix_get_stats(const struct dpif_ipfix *di,
1536 bool bridge_ipfix,
1537 struct ovs_list *replies)
1538 OVS_EXCLUDED(mutex)
1539 {
1540 struct dpif_ipfix_flow_exporter_map_node *flow_exporter_node;
1541 struct ofputil_ipfix_stats ois;
1542
1543 ovs_mutex_lock(&mutex);
1544 if (bridge_ipfix) {
1545 if (!di->bridge_exporter.options) {
1546 ovs_mutex_unlock(&mutex);
1547 return OFPERR_NXST_NOT_CONFIGURED;
1548 }
1549
1550 ipfix_get_bridge_stats(&di->bridge_exporter, &ois);
1551 ofputil_append_ipfix_stat(replies, &ois);
1552 } else {
1553 if (hmap_count(&di->flow_exporter_map) == 0) {
1554 ovs_mutex_unlock(&mutex);
1555 return OFPERR_NXST_NOT_CONFIGURED;
1556 }
1557
1558 HMAP_FOR_EACH (flow_exporter_node, node,
1559 &di->flow_exporter_map) {
1560 ipfix_get_flow_stats(&flow_exporter_node->exporter, &ois);
1561 ofputil_append_ipfix_stat(replies, &ois);
1562 }
1563 }
1564 ovs_mutex_unlock(&mutex);
1565
1566 return 0;
1567 }
1568
1569 /* Update partial ipfix stats */
1570 static void
1571 ipfix_update_stats(struct dpif_ipfix_exporter *exporter,
1572 bool new_flow,
1573 size_t current_flows,
1574 enum ipfix_sampled_packet_type sampled_pkt_type)
1575 {
1576 if (new_flow) {
1577 exporter->ofproto_stats.total_flows++;
1578 exporter->ofproto_stats.current_flows = current_flows;
1579 }
1580 exporter->ofproto_stats.pkts++;
1581
1582 switch (sampled_pkt_type) {
1583 case IPFIX_SAMPLED_PKT_IPV4_OK:
1584 exporter->ofproto_stats.ipv4_pkts++;
1585 break;
1586 case IPFIX_SAMPLED_PKT_IPV6_OK:
1587 exporter->ofproto_stats.ipv6_pkts++;
1588 break;
1589 case IPFIX_SAMPLED_PKT_IPV4_ERROR:
1590 exporter->ofproto_stats.ipv4_error_pkts++;
1591 exporter->ofproto_stats.error_pkts++;
1592 break;
1593 case IPFIX_SAMPLED_PKT_IPV6_ERROR:
1594 exporter->ofproto_stats.ipv6_error_pkts++;
1595 exporter->ofproto_stats.error_pkts++;
1596 break;
1597 case IPFIX_SAMPLED_PKT_UNKNOWN:
1598 exporter->ofproto_stats.error_pkts++;
1599 break;
1600 case IPFIX_SAMPLED_PKT_OTHERS:
1601 default:
1602 break;
1603 }
1604 }
1605
1606 /* Add an entry into a flow cache. The entry is either aggregated into
1607 * an existing entry with the same flow key and free()d, or it is
1608 * inserted into the cache. And IPFIX stats will be updated */
1609 static void
1610 ipfix_cache_update(struct dpif_ipfix_exporter *exporter,
1611 struct ipfix_flow_cache_entry *entry,
1612 enum ipfix_sampled_packet_type sampled_pkt_type)
1613 {
1614 struct ipfix_flow_cache_entry *old_entry;
1615 size_t current_flows = 0;
1616
1617 old_entry = ipfix_cache_find_entry(exporter, &entry->flow_key);
1618
1619 if (old_entry == NULL) {
1620 hmap_insert(&exporter->cache_flow_key_map, &entry->flow_key_map_node,
1621 ipfix_hash_flow_key(&entry->flow_key, 0));
1622
1623 /* As the latest entry added into the cache, it should
1624 * logically have the highest flow_start_timestamp_usec, so
1625 * append it at the tail. */
1626 ovs_list_push_back(&exporter->cache_flow_start_timestamp_list,
1627 &entry->cache_flow_start_timestamp_list_node);
1628
1629 /* Enforce exporter->cache_max_flows limit. */
1630 current_flows = hmap_count(&exporter->cache_flow_key_map);
1631 ipfix_update_stats(exporter, true, current_flows, sampled_pkt_type);
1632 if (current_flows > exporter->cache_max_flows) {
1633 dpif_ipfix_cache_expire_now(exporter, false);
1634 }
1635 } else {
1636 ipfix_cache_aggregate_entries(entry, old_entry);
1637 free(entry);
1638 ipfix_update_stats(exporter, false, current_flows, sampled_pkt_type);
1639 }
1640 }
1641
1642 static enum ipfix_sampled_packet_type
1643 ipfix_cache_entry_init(struct ipfix_flow_cache_entry *entry,
1644 const struct dp_packet *packet, const struct flow *flow,
1645 uint64_t packet_delta_count, uint32_t obs_domain_id,
1646 uint32_t obs_point_id, odp_port_t output_odp_port,
1647 enum nx_action_sample_direction direction,
1648 const struct dpif_ipfix_port *tunnel_port,
1649 const struct flow_tnl *tunnel_key,
1650 struct dpif_ipfix_global_stats *stats)
1651 {
1652 struct ipfix_flow_key *flow_key;
1653 struct dp_packet msg;
1654 enum ipfix_proto_l2 l2;
1655 enum ipfix_proto_l3 l3;
1656 enum ipfix_proto_l4 l4;
1657 enum ipfix_proto_tunnel tunnel = IPFIX_PROTO_NOT_TUNNELED;
1658 enum ipfix_sampled_packet_type sampled_pkt_type = IPFIX_SAMPLED_PKT_UNKNOWN;
1659 uint8_t ethernet_header_length;
1660 uint16_t ethernet_total_length;
1661
1662 flow_key = &entry->flow_key;
1663 dp_packet_use_stub(&msg, flow_key->flow_key_msg_part,
1664 sizeof flow_key->flow_key_msg_part);
1665
1666 /* Choose the right template ID matching the protocols in the
1667 * sampled packet. */
1668 l2 = (flow->vlans[0].tci == 0) ? IPFIX_PROTO_L2_ETH : IPFIX_PROTO_L2_VLAN;
1669
1670 switch(ntohs(flow->dl_type)) {
1671 case ETH_TYPE_IP:
1672 l3 = IPFIX_PROTO_L3_IPV4;
1673 sampled_pkt_type = IPFIX_SAMPLED_PKT_IPV4_OK;
1674 switch(flow->nw_proto) {
1675 case IPPROTO_TCP:
1676 l4 = IPFIX_PROTO_L4_TCP;
1677 break;
1678 case IPPROTO_UDP:
1679 l4 = IPFIX_PROTO_L4_UDP;
1680 break;
1681 case IPPROTO_SCTP:
1682 l4 = IPFIX_PROTO_L4_SCTP;
1683 break;
1684 case IPPROTO_ICMP:
1685 l4 = IPFIX_PROTO_L4_ICMP;
1686 break;
1687 default:
1688 l4 = IPFIX_PROTO_L4_UNKNOWN;
1689 sampled_pkt_type = IPFIX_SAMPLED_PKT_IPV4_ERROR;
1690 }
1691 break;
1692 case ETH_TYPE_IPV6:
1693 l3 = IPFIX_PROTO_L3_IPV6;
1694 sampled_pkt_type = IPFIX_SAMPLED_PKT_IPV6_OK;
1695 switch(flow->nw_proto) {
1696 case IPPROTO_TCP:
1697 l4 = IPFIX_PROTO_L4_TCP;
1698 break;
1699 case IPPROTO_UDP:
1700 l4 = IPFIX_PROTO_L4_UDP;
1701 break;
1702 case IPPROTO_SCTP:
1703 l4 = IPFIX_PROTO_L4_SCTP;
1704 break;
1705 case IPPROTO_ICMPV6:
1706 l4 = IPFIX_PROTO_L4_ICMP;
1707 break;
1708 default:
1709 l4 = IPFIX_PROTO_L4_UNKNOWN;
1710 sampled_pkt_type = IPFIX_SAMPLED_PKT_IPV6_ERROR;
1711 }
1712 break;
1713 default:
1714 l3 = IPFIX_PROTO_L3_UNKNOWN;
1715 l4 = IPFIX_PROTO_L4_UNKNOWN;
1716 sampled_pkt_type = IPFIX_SAMPLED_PKT_OTHERS;
1717 }
1718
1719 if (tunnel_port && tunnel_key) {
1720 tunnel = IPFIX_PROTO_TUNNELED;
1721 }
1722
1723 flow_key->obs_domain_id = obs_domain_id;
1724 flow_key->template_id = ipfix_get_template_id(l2, l3, l4, tunnel);
1725
1726 /* The fields defined in the ipfix_data_record_* structs and sent
1727 * below must match exactly the templates defined in
1728 * ipfix_define_template_fields. */
1729
1730 ethernet_header_length = (l2 == IPFIX_PROTO_L2_VLAN)
1731 ? VLAN_ETH_HEADER_LEN : ETH_HEADER_LEN;
1732 ethernet_total_length = dp_packet_size(packet);
1733
1734 /* Common Ethernet entities. */
1735 {
1736 struct ipfix_data_record_flow_key_common *data_common;
1737
1738 data_common = dp_packet_put_zeros(&msg, sizeof *data_common);
1739 data_common->observation_point_id = htonl(obs_point_id);
1740 data_common->flow_direction =
1741 (direction == NX_ACTION_SAMPLE_INGRESS ? INGRESS_FLOW
1742 : direction == NX_ACTION_SAMPLE_EGRESS ? EGRESS_FLOW
1743 : output_odp_port == ODPP_NONE ? INGRESS_FLOW : EGRESS_FLOW);
1744 data_common->source_mac_address = flow->dl_src;
1745 data_common->destination_mac_address = flow->dl_dst;
1746 data_common->ethernet_type = flow->dl_type;
1747 data_common->ethernet_header_length = ethernet_header_length;
1748 }
1749
1750 if (l2 == IPFIX_PROTO_L2_VLAN) {
1751 struct ipfix_data_record_flow_key_vlan *data_vlan;
1752 uint16_t vlan_id = vlan_tci_to_vid(flow->vlans[0].tci);
1753 uint8_t priority = vlan_tci_to_pcp(flow->vlans[0].tci);
1754
1755 data_vlan = dp_packet_put_zeros(&msg, sizeof *data_vlan);
1756 data_vlan->vlan_id = htons(vlan_id);
1757 data_vlan->dot1q_vlan_id = htons(vlan_id);
1758 data_vlan->dot1q_priority = priority;
1759 }
1760
1761 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
1762 struct ipfix_data_record_flow_key_ip *data_ip;
1763
1764 data_ip = dp_packet_put_zeros(&msg, sizeof *data_ip);
1765 data_ip->ip_version = (l3 == IPFIX_PROTO_L3_IPV4) ? 4 : 6;
1766 data_ip->ip_ttl = flow->nw_ttl;
1767 data_ip->protocol_identifier = flow->nw_proto;
1768 data_ip->ip_diff_serv_code_point = flow->nw_tos >> 2;
1769 data_ip->ip_precedence = flow->nw_tos >> 5;
1770 data_ip->ip_class_of_service = flow->nw_tos;
1771
1772 if (l3 == IPFIX_PROTO_L3_IPV4) {
1773 struct ipfix_data_record_flow_key_ipv4 *data_ipv4;
1774
1775 data_ipv4 = dp_packet_put_zeros(&msg, sizeof *data_ipv4);
1776 data_ipv4->source_ipv4_address = flow->nw_src;
1777 data_ipv4->destination_ipv4_address = flow->nw_dst;
1778 } else { /* l3 == IPFIX_PROTO_L3_IPV6 */
1779 struct ipfix_data_record_flow_key_ipv6 *data_ipv6;
1780
1781 data_ipv6 = dp_packet_put_zeros(&msg, sizeof *data_ipv6);
1782 memcpy(data_ipv6->source_ipv6_address, &flow->ipv6_src,
1783 sizeof flow->ipv6_src);
1784 memcpy(data_ipv6->destination_ipv6_address, &flow->ipv6_dst,
1785 sizeof flow->ipv6_dst);
1786 data_ipv6->flow_label_ipv6 = flow->ipv6_label;
1787 }
1788 }
1789
1790 if (l4 == IPFIX_PROTO_L4_TCP
1791 || l4 == IPFIX_PROTO_L4_UDP
1792 || l4 == IPFIX_PROTO_L4_SCTP) {
1793 struct ipfix_data_record_flow_key_transport *data_transport;
1794
1795 data_transport = dp_packet_put_zeros(&msg, sizeof *data_transport);
1796 data_transport->source_transport_port = flow->tp_src;
1797 data_transport->destination_transport_port = flow->tp_dst;
1798 } else if (l4 == IPFIX_PROTO_L4_ICMP) {
1799 struct ipfix_data_record_flow_key_icmp *data_icmp;
1800
1801 data_icmp = dp_packet_put_zeros(&msg, sizeof *data_icmp);
1802 data_icmp->icmp_type = ntohs(flow->tp_src) & 0xff;
1803 data_icmp->icmp_code = ntohs(flow->tp_dst) & 0xff;
1804 }
1805
1806 if (tunnel == IPFIX_PROTO_TUNNELED) {
1807 struct ipfix_data_record_flow_key_tunnel *data_tunnel;
1808 const uint8_t *tun_id;
1809
1810 data_tunnel = dp_packet_put_zeros(&msg, sizeof *data_tunnel +
1811 tunnel_port->tunnel_key_length);
1812 data_tunnel->tunnel_source_ipv4_address = tunnel_key->ip_src;
1813 data_tunnel->tunnel_destination_ipv4_address = tunnel_key->ip_dst;
1814 /* The tunnel_protocol_identifier is from tunnel_proto array, which
1815 * contains protocol_identifiers of each tunnel type.
1816 */
1817 data_tunnel->tunnel_protocol_identifier =
1818 tunnel_protocol[tunnel_port->tunnel_type];
1819 data_tunnel->tunnel_source_transport_port = tunnel_key->tp_src;
1820 data_tunnel->tunnel_destination_transport_port = tunnel_key->tp_dst;
1821 data_tunnel->tunnel_type = tunnel_port->tunnel_type;
1822 data_tunnel->tunnel_key_length = tunnel_port->tunnel_key_length;
1823 /* tun_id is in network order, and tunnel key is in low bits. */
1824 tun_id = (const uint8_t *) &tunnel_key->tun_id;
1825 memcpy(data_tunnel->tunnel_key,
1826 &tun_id[8 - tunnel_port->tunnel_key_length],
1827 tunnel_port->tunnel_key_length);
1828 }
1829
1830 flow_key->flow_key_msg_part_size = dp_packet_size(&msg);
1831
1832 {
1833 struct timeval now;
1834 uint64_t layer2_octet_delta_count;
1835
1836 /* Calculate the total matched octet count by considering as
1837 * an approximation that all matched packets have the same
1838 * length. */
1839 layer2_octet_delta_count = packet_delta_count * ethernet_total_length;
1840
1841 xgettimeofday(&now);
1842 entry->flow_end_timestamp_usec = now.tv_usec + 1000000LL * now.tv_sec;
1843 entry->flow_start_timestamp_usec = entry->flow_end_timestamp_usec;
1844 entry->packet_delta_count = packet_delta_count;
1845 entry->layer2_octet_delta_count = layer2_octet_delta_count;
1846
1847 stats->packet_total_count += packet_delta_count;
1848 stats->layer2_octet_total_count += layer2_octet_delta_count;
1849 entry->packet_total_count = stats->packet_total_count;
1850 entry->layer2_octet_total_count = stats->layer2_octet_total_count;
1851
1852 }
1853
1854 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
1855 uint16_t ip_total_length =
1856 ethernet_total_length - ethernet_header_length;
1857 uint64_t octet_delta_count;
1858
1859 /* Calculate the total matched octet count by considering as
1860 * an approximation that all matched packets have the same
1861 * length. */
1862 octet_delta_count = packet_delta_count * ip_total_length;
1863
1864 entry->octet_delta_count = octet_delta_count;
1865 entry->octet_delta_sum_of_squares = octet_delta_count * ip_total_length;
1866 entry->minimum_ip_total_length = ip_total_length;
1867 entry->maximum_ip_total_length = ip_total_length;
1868
1869 stats->octet_total_count += octet_delta_count;
1870 stats->octet_total_sum_of_squares += entry->octet_delta_sum_of_squares;
1871
1872 } else {
1873 entry->octet_delta_sum_of_squares = 0;
1874 entry->minimum_ip_total_length = 0;
1875 entry->maximum_ip_total_length = 0;
1876 }
1877
1878 entry->octet_total_sum_of_squares = stats->octet_total_sum_of_squares;
1879 entry->octet_total_count = stats->octet_total_count;
1880
1881 if (l4 == IPFIX_PROTO_L4_TCP) {
1882 uint16_t tcp_flags = ntohs(flow->tcp_flags);
1883 entry->tcp_packet_delta_count = packet_delta_count;
1884
1885 if (tcp_flags & TCP_ACK) {
1886 stats->tcp_ack_total_count += packet_delta_count;
1887 }
1888 if (tcp_flags & TCP_FIN) {
1889 stats->tcp_fin_total_count += packet_delta_count;
1890 }
1891 if (tcp_flags & TCP_PSH) {
1892 stats->tcp_psh_total_count += packet_delta_count;
1893 }
1894 if (tcp_flags & TCP_RST) {
1895 stats->tcp_rst_total_count += packet_delta_count;
1896 }
1897 if (tcp_flags & TCP_SYN) {
1898 stats->tcp_syn_total_count += packet_delta_count;
1899 }
1900 if (tcp_flags & TCP_URG) {
1901 stats->tcp_urg_total_count += packet_delta_count;
1902 }
1903 } else {
1904 entry->tcp_packet_delta_count = 0;
1905 }
1906
1907 entry->tcp_ack_total_count = stats->tcp_ack_total_count;
1908 entry->tcp_fin_total_count = stats->tcp_fin_total_count;
1909 entry->tcp_psh_total_count = stats->tcp_psh_total_count;
1910 entry->tcp_rst_total_count = stats->tcp_rst_total_count;
1911 entry->tcp_syn_total_count = stats->tcp_syn_total_count;
1912 entry->tcp_urg_total_count = stats->tcp_urg_total_count;
1913
1914 return sampled_pkt_type;
1915 }
1916
1917 /* Send each single data record in its own data set, to simplify the
1918 * implementation by avoiding having to group record by template ID
1919 * before sending. */
1920 static void
1921 ipfix_put_data_set(uint32_t export_time_sec,
1922 struct ipfix_flow_cache_entry *entry,
1923 enum ipfix_flow_end_reason flow_end_reason,
1924 const char *virtual_obs_id,
1925 uint8_t virtual_obs_len,
1926 struct dp_packet *msg)
1927 {
1928 size_t set_hdr_offset;
1929 struct ipfix_set_header *set_hdr;
1930
1931 set_hdr_offset = dp_packet_size(msg);
1932
1933 /* Put a Data Set. */
1934 set_hdr = dp_packet_put_zeros(msg, sizeof *set_hdr);
1935 set_hdr->set_id = htons(entry->flow_key.template_id);
1936
1937 /* Copy the flow key part of the data record. */
1938
1939 dp_packet_put(msg, entry->flow_key.flow_key_msg_part,
1940 entry->flow_key.flow_key_msg_part_size);
1941
1942 /* Export virtual observation ID. */
1943 if (virtual_obs_id) {
1944 dp_packet_put(msg, &virtual_obs_len, sizeof(virtual_obs_len));
1945 dp_packet_put(msg, virtual_obs_id, virtual_obs_len);
1946 }
1947
1948 /* Put the non-key part of the data record. */
1949
1950 {
1951 struct ipfix_data_record_aggregated_common *data_aggregated_common;
1952 uint64_t export_time_usec, flow_start_delta_usec, flow_end_delta_usec;
1953
1954 /* Calculate the negative deltas relative to the export time
1955 * in seconds sent in the header, not the exact export
1956 * time. */
1957 export_time_usec = 1000000LL * export_time_sec;
1958 flow_start_delta_usec = export_time_usec
1959 - entry->flow_start_timestamp_usec;
1960 flow_end_delta_usec = export_time_usec
1961 - entry->flow_end_timestamp_usec;
1962
1963 data_aggregated_common = dp_packet_put_zeros(
1964 msg, sizeof *data_aggregated_common);
1965 data_aggregated_common->flow_start_delta_microseconds = htonl(
1966 flow_start_delta_usec);
1967 data_aggregated_common->flow_end_delta_microseconds = htonl(
1968 flow_end_delta_usec);
1969 data_aggregated_common->packet_delta_count = htonll(
1970 entry->packet_delta_count);
1971 data_aggregated_common->packet_total_count = htonll(
1972 entry->packet_total_count);
1973 data_aggregated_common->layer2_octet_delta_count = htonll(
1974 entry->layer2_octet_delta_count);
1975 data_aggregated_common->layer2_octet_total_count = htonll(
1976 entry->layer2_octet_total_count);
1977 data_aggregated_common->flow_end_reason = flow_end_reason;
1978 }
1979
1980 if (entry->octet_delta_sum_of_squares) { /* IP packet. */
1981 struct ipfix_data_record_aggregated_ip *data_aggregated_ip;
1982
1983 data_aggregated_ip = dp_packet_put_zeros(
1984 msg, sizeof *data_aggregated_ip);
1985 data_aggregated_ip->octet_delta_count = htonll(
1986 entry->octet_delta_count);
1987 data_aggregated_ip->octet_total_count = htonll(
1988 entry->octet_total_count);
1989 data_aggregated_ip->octet_delta_sum_of_squares = htonll(
1990 entry->octet_delta_sum_of_squares);
1991 data_aggregated_ip->octet_total_sum_of_squares = htonll(
1992 entry->octet_total_sum_of_squares);
1993 data_aggregated_ip->minimum_ip_total_length = htonll(
1994 entry->minimum_ip_total_length);
1995 data_aggregated_ip->maximum_ip_total_length = htonll(
1996 entry->maximum_ip_total_length);
1997 }
1998
1999 if (entry->tcp_packet_delta_count) {
2000 struct ipfix_data_record_aggregated_tcp *data_aggregated_tcp;
2001
2002 data_aggregated_tcp = dp_packet_put_zeros(
2003 msg, sizeof *data_aggregated_tcp);
2004 data_aggregated_tcp->tcp_ack_total_count = htonll(
2005 entry->tcp_ack_total_count);
2006 data_aggregated_tcp->tcp_fin_total_count = htonll(
2007 entry->tcp_fin_total_count);
2008 data_aggregated_tcp->tcp_psh_total_count = htonll(
2009 entry->tcp_psh_total_count);
2010 data_aggregated_tcp->tcp_rst_total_count = htonll(
2011 entry->tcp_rst_total_count);
2012 data_aggregated_tcp->tcp_syn_total_count = htonll(
2013 entry->tcp_syn_total_count);
2014 data_aggregated_tcp->tcp_urg_total_count = htonll(
2015 entry->tcp_urg_total_count);
2016 }
2017
2018 set_hdr = (struct ipfix_set_header*)((uint8_t*)dp_packet_data(msg) + set_hdr_offset);
2019 set_hdr->length = htons(dp_packet_size(msg) - set_hdr_offset);
2020 }
2021
2022 /* Send an IPFIX message with a single data record. */
2023 static void
2024 ipfix_send_data_msg(struct dpif_ipfix_exporter *exporter,
2025 uint32_t export_time_sec,
2026 struct ipfix_flow_cache_entry *entry,
2027 enum ipfix_flow_end_reason flow_end_reason)
2028 {
2029 uint64_t msg_stub[DIV_ROUND_UP(MAX_MESSAGE_LEN, 8)];
2030 struct dp_packet msg;
2031 size_t tx_errors;
2032
2033 dp_packet_use_stub(&msg, msg_stub, sizeof msg_stub);
2034
2035 ipfix_init_header(export_time_sec, exporter->seq_number++,
2036 entry->flow_key.obs_domain_id, &msg);
2037 ipfix_put_data_set(export_time_sec, entry, flow_end_reason,
2038 exporter->virtual_obs_id, exporter->virtual_obs_len,
2039 &msg);
2040 tx_errors = ipfix_send_msg(exporter->collectors, &msg);
2041
2042 dp_packet_uninit(&msg);
2043
2044 exporter->ofproto_stats.current_flows--;
2045 exporter->ofproto_stats.tx_pkts +=
2046 collectors_count(exporter->collectors) - tx_errors;
2047 exporter->ofproto_stats.tx_errors += tx_errors;
2048 }
2049
2050 static void
2051 dpif_ipfix_sample(struct dpif_ipfix_exporter *exporter,
2052 const struct dp_packet *packet, const struct flow *flow,
2053 uint64_t packet_delta_count, uint32_t obs_domain_id,
2054 uint32_t obs_point_id, odp_port_t output_odp_port,
2055 enum nx_action_sample_direction direction,
2056 const struct dpif_ipfix_port *tunnel_port,
2057 const struct flow_tnl *tunnel_key)
2058 {
2059 struct ipfix_flow_cache_entry *entry;
2060 enum ipfix_sampled_packet_type sampled_packet_type;
2061
2062 /* Create a flow cache entry from the sample. */
2063 entry = xmalloc(sizeof *entry);
2064 sampled_packet_type =
2065 ipfix_cache_entry_init(entry, packet,
2066 flow, packet_delta_count,
2067 obs_domain_id, obs_point_id,
2068 output_odp_port, direction,
2069 tunnel_port, tunnel_key,
2070 &exporter->ipfix_global_stats);
2071
2072 ipfix_cache_update(exporter, entry, sampled_packet_type);
2073 }
2074
2075 static bool
2076 bridge_exporter_enabled(struct dpif_ipfix *di)
2077 {
2078 return di->bridge_exporter.probability > 0;
2079 }
2080
2081 void
2082 dpif_ipfix_bridge_sample(struct dpif_ipfix *di, const struct dp_packet *packet,
2083 const struct flow *flow,
2084 odp_port_t input_odp_port, odp_port_t output_odp_port,
2085 const struct flow_tnl *output_tunnel_key)
2086 OVS_EXCLUDED(mutex)
2087 {
2088 uint64_t packet_delta_count;
2089 const struct flow_tnl *tunnel_key = NULL;
2090 struct dpif_ipfix_port * tunnel_port = NULL;
2091
2092 ovs_mutex_lock(&mutex);
2093 if (!bridge_exporter_enabled(di)) {
2094 ovs_mutex_unlock(&mutex);
2095 return;
2096 }
2097
2098 /* Skip BFD packets:
2099 * Bidirectional Forwarding Detection(BFD) packets are for monitoring
2100 * the tunnel link status and consumed by ovs itself. No need to
2101 * smaple them.
2102 * CF IETF RFC 5881, BFD control packet is the UDP packet with
2103 * destination port 3784, and BFD echo packet is the UDP packet with
2104 * destination port 3785.
2105 */
2106 if (is_ip_any(flow) &&
2107 flow->nw_proto == IPPROTO_UDP &&
2108 (flow->tp_dst == htons(BFD_CONTROL_DEST_PORT) ||
2109 flow->tp_dst == htons(BFD_ECHO_DEST_PORT))) {
2110 ovs_mutex_unlock(&mutex);
2111 return;
2112 }
2113
2114 /* Use the sampling probability as an approximation of the number
2115 * of matched packets. */
2116 packet_delta_count = UINT32_MAX / di->bridge_exporter.probability;
2117 if (di->bridge_exporter.options->enable_tunnel_sampling) {
2118 if (output_odp_port == ODPP_NONE && flow->tunnel.ip_dst) {
2119 /* Input tunnel. */
2120 tunnel_key = &flow->tunnel;
2121 tunnel_port = dpif_ipfix_find_port(di, input_odp_port);
2122 }
2123 if (output_odp_port != ODPP_NONE && output_tunnel_key) {
2124 /* Output tunnel, output_tunnel_key must be valid. */
2125 tunnel_key = output_tunnel_key;
2126 tunnel_port = dpif_ipfix_find_port(di, output_odp_port);
2127 }
2128 }
2129
2130 dpif_ipfix_sample(&di->bridge_exporter.exporter, packet, flow,
2131 packet_delta_count,
2132 di->bridge_exporter.options->obs_domain_id,
2133 di->bridge_exporter.options->obs_point_id,
2134 output_odp_port, NX_ACTION_SAMPLE_DEFAULT,
2135 tunnel_port, tunnel_key);
2136 ovs_mutex_unlock(&mutex);
2137 }
2138
2139 void
2140 dpif_ipfix_flow_sample(struct dpif_ipfix *di, const struct dp_packet *packet,
2141 const struct flow *flow,
2142 const union user_action_cookie *cookie,
2143 odp_port_t input_odp_port,
2144 const struct flow_tnl *output_tunnel_key)
2145 OVS_EXCLUDED(mutex)
2146 {
2147 struct dpif_ipfix_flow_exporter_map_node *node;
2148 const struct flow_tnl *tunnel_key = NULL;
2149 struct dpif_ipfix_port * tunnel_port = NULL;
2150 odp_port_t output_odp_port = cookie->flow_sample.output_odp_port;
2151 uint32_t collector_set_id = cookie->flow_sample.collector_set_id;
2152 uint16_t probability = cookie->flow_sample.probability;
2153
2154 /* Use the sampling probability as an approximation of the number
2155 * of matched packets. */
2156 uint64_t packet_delta_count = USHRT_MAX / probability;
2157
2158 ovs_mutex_lock(&mutex);
2159 node = dpif_ipfix_find_flow_exporter_map_node(di, collector_set_id);
2160 if (node) {
2161 if (node->exporter.options->enable_tunnel_sampling) {
2162 if (output_odp_port == ODPP_NONE && flow->tunnel.ip_dst) {
2163 /* Input tunnel. */
2164 tunnel_key = &flow->tunnel;
2165 tunnel_port = dpif_ipfix_find_port(di, input_odp_port);
2166 }
2167 if (output_odp_port != ODPP_NONE && output_tunnel_key) {
2168 /* Output tunnel, output_tunnel_key must be valid. */
2169 tunnel_key = output_tunnel_key;
2170 tunnel_port = dpif_ipfix_find_port(di, output_odp_port);
2171 }
2172 }
2173
2174 dpif_ipfix_sample(&node->exporter.exporter, packet, flow,
2175 packet_delta_count,
2176 cookie->flow_sample.obs_domain_id,
2177 cookie->flow_sample.obs_point_id,
2178 output_odp_port, cookie->flow_sample.direction,
2179 tunnel_port, tunnel_key);
2180 }
2181 ovs_mutex_unlock(&mutex);
2182 }
2183
2184 static void
2185 dpif_ipfix_cache_expire(struct dpif_ipfix_exporter *exporter,
2186 bool forced_end, const uint64_t export_time_usec,
2187 const uint32_t export_time_sec)
2188 {
2189 struct ipfix_flow_cache_entry *entry, *next_entry;
2190 uint64_t max_flow_start_timestamp_usec;
2191 bool template_msg_sent = false;
2192 enum ipfix_flow_end_reason flow_end_reason;
2193
2194 if (ovs_list_is_empty(&exporter->cache_flow_start_timestamp_list)) {
2195 return;
2196 }
2197
2198 max_flow_start_timestamp_usec = export_time_usec -
2199 1000000LL * exporter->cache_active_timeout;
2200
2201 LIST_FOR_EACH_SAFE (entry, next_entry, cache_flow_start_timestamp_list_node,
2202 &exporter->cache_flow_start_timestamp_list) {
2203 if (forced_end) {
2204 flow_end_reason = FORCED_END;
2205 } else if (entry->flow_start_timestamp_usec
2206 <= max_flow_start_timestamp_usec) {
2207 flow_end_reason = ACTIVE_TIMEOUT;
2208 } else if (hmap_count(&exporter->cache_flow_key_map)
2209 > exporter->cache_max_flows) {
2210 /* Enforce exporter->cache_max_flows. */
2211 flow_end_reason = LACK_OF_RESOURCES;
2212 } else {
2213 /* Remaining flows haven't expired yet. */
2214 break;
2215 }
2216
2217 ovs_list_remove(&entry->cache_flow_start_timestamp_list_node);
2218 hmap_remove(&exporter->cache_flow_key_map,
2219 &entry->flow_key_map_node);
2220
2221 if (!template_msg_sent
2222 && (exporter->last_template_set_time + IPFIX_TEMPLATE_INTERVAL)
2223 <= export_time_sec) {
2224 ipfix_send_template_msgs(exporter, export_time_sec,
2225 entry->flow_key.obs_domain_id);
2226 exporter->last_template_set_time = export_time_sec;
2227 template_msg_sent = true;
2228 }
2229
2230 /* XXX: Group multiple data records for the same obs domain id
2231 * into the same message. */
2232 ipfix_send_data_msg(exporter, export_time_sec, entry, flow_end_reason);
2233 free(entry);
2234 }
2235 }
2236
2237 static void
2238 get_export_time_now(uint64_t *export_time_usec, uint32_t *export_time_sec)
2239 {
2240 struct timeval export_time;
2241 xgettimeofday(&export_time);
2242
2243 *export_time_usec = export_time.tv_usec + 1000000LL * export_time.tv_sec;
2244
2245 /* The IPFIX start and end deltas are negative deltas relative to
2246 * the export time, so set the export time 1 second off to
2247 * calculate those deltas. */
2248 if (export_time.tv_usec == 0) {
2249 *export_time_sec = export_time.tv_sec;
2250 } else {
2251 *export_time_sec = export_time.tv_sec + 1;
2252 }
2253 }
2254
2255 static void
2256 dpif_ipfix_cache_expire_now(struct dpif_ipfix_exporter *exporter,
2257 bool forced_end)
2258 {
2259 uint64_t export_time_usec;
2260 uint32_t export_time_sec;
2261
2262 get_export_time_now(&export_time_usec, &export_time_sec);
2263 dpif_ipfix_cache_expire(exporter, forced_end, export_time_usec,
2264 export_time_sec);
2265 }
2266
2267 void
2268 dpif_ipfix_run(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
2269 {
2270 uint64_t export_time_usec;
2271 uint32_t export_time_sec;
2272 struct dpif_ipfix_flow_exporter_map_node *flow_exporter_node;
2273
2274 ovs_mutex_lock(&mutex);
2275 get_export_time_now(&export_time_usec, &export_time_sec);
2276 if (bridge_exporter_enabled(di)) {
2277 dpif_ipfix_cache_expire(
2278 &di->bridge_exporter.exporter, false, export_time_usec,
2279 export_time_sec);
2280 }
2281 HMAP_FOR_EACH (flow_exporter_node, node, &di->flow_exporter_map) {
2282 dpif_ipfix_cache_expire(
2283 &flow_exporter_node->exporter.exporter, false, export_time_usec,
2284 export_time_sec);
2285 }
2286 ovs_mutex_unlock(&mutex);
2287 }
2288
2289 void
2290 dpif_ipfix_wait(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
2291 {
2292 long long int next_timeout_msec = LLONG_MAX;
2293 struct dpif_ipfix_flow_exporter_map_node *flow_exporter_node;
2294
2295 ovs_mutex_lock(&mutex);
2296 if (bridge_exporter_enabled(di)) {
2297 if (ipfix_cache_next_timeout_msec(
2298 &di->bridge_exporter.exporter, &next_timeout_msec)) {
2299 poll_timer_wait_until(next_timeout_msec);
2300 }
2301 }
2302 HMAP_FOR_EACH (flow_exporter_node, node, &di->flow_exporter_map) {
2303 if (ipfix_cache_next_timeout_msec(
2304 &flow_exporter_node->exporter.exporter, &next_timeout_msec)) {
2305 poll_timer_wait_until(next_timeout_msec);
2306 }
2307 }
2308 ovs_mutex_unlock(&mutex);
2309 }