]> git.proxmox.com Git - mirror_ovs.git/blame - ofproto/ofproto-dpif-ipfix.c
ofp-util: Remove prototypes for unimplemented functions.
[mirror_ovs.git] / ofproto / ofproto-dpif-ipfix.c
CommitLineData
29089a54 1/*
642c824b 2 * Copyright (c) 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
29089a54
RL
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"
978427a5 19#include <sys/time.h>
29089a54
RL
20#include "byte-order.h"
21#include "collectors.h"
22#include "flow.h"
23#include "hash.h"
ee89ea7b 24#include "openvswitch/hmap.h"
f4248336 25#include "netdev.h"
b19bab5b 26#include "openvswitch/list.h"
64c96779 27#include "openvswitch/ofpbuf.h"
29089a54 28#include "ofproto.h"
8b7ea2d4 29#include "ofproto-dpif.h"
cf62fa4c 30#include "dp-packet.h"
29089a54 31#include "packets.h"
fd016ae3 32#include "openvswitch/poll-loop.h"
29089a54
RL
33#include "sset.h"
34#include "util.h"
35#include "timeval.h"
e6211adc 36#include "openvswitch/vlog.h"
29089a54
RL
37
38VLOG_DEFINE_THIS_MODULE(ipfix);
39
40static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
626ace7b 41static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
29089a54 42
d564c4bb
PS
43/* This variable represents a number of exporters that have been created
44 * throughout OvS lifecycle. It's used to identify Exporting Process. Since
45 * it's NOT decreased when exporter is destroyed, it will eventually overflow.
46 * Considering the maximum value it can hold and the fact that Exporting
47 * Process may be re-started with a different ID, this shouldn't be a problem.
48 */
49static uint32_t exporter_total_count;
50
29089a54
RL
51/* Cf. IETF RFC 5101 Section 10.3.4. */
52#define IPFIX_DEFAULT_COLLECTOR_PORT 4739
53
47946ac5
WZ
54/* Cf. IETF RFC 5881 Setion 8. */
55#define BFD_CONTROL_DEST_PORT 3784
56#define BFD_ECHO_DEST_PORT 3785
57
fb8f22c1
BY
58enum ipfix_sampled_packet_type {
59 IPFIX_SAMPLED_PKT_UNKNOWN = 0x00,
60 IPFIX_SAMPLED_PKT_IPV4_OK = 0x01,
61 IPFIX_SAMPLED_PKT_IPV6_OK = 0x02,
62 IPFIX_SAMPLED_PKT_IPV4_ERROR = 0x03,
63 IPFIX_SAMPLED_PKT_IPV6_ERROR = 0x04,
64 IPFIX_SAMPLED_PKT_OTHERS = 0x05
65};
66
8b7ea2d4
WZ
67/* The standard layer2SegmentId (ID 351) element is included in vDS to send
68 * the VxLAN tunnel's VNI. It is 64-bit long, the most significant byte is
69 * used to indicate the type of tunnel (0x01 = VxLAN, 0x02 = GRE) and the three
70 * least significant bytes hold the value of the layer 2 overlay network
71 * segment identifier: a 24-bit VxLAN tunnel's VNI or a 24-bit GRE tunnel's
99e7b077
PS
72 * TNI. This is not compatible with STT, as implemented in OVS, as
73 * its tunnel IDs is 64-bit.
8b7ea2d4
WZ
74 *
75 * Two new enterprise information elements are defined which are similar to
76 * laryerSegmentId but support 64-bit IDs:
77 * tunnelType (ID 891) and tunnelKey (ID 892).
78 *
79 * The enum dpif_ipfix_tunnel_type is to declare the types supported in the
80 * tunnelType element.
81 * The number of ipfix tunnel types includes two reserverd types: 0x04 and 0x06.
82 */
83enum dpif_ipfix_tunnel_type {
84 DPIF_IPFIX_TUNNEL_UNKNOWN = 0x00,
85 DPIF_IPFIX_TUNNEL_VXLAN = 0x01,
86 DPIF_IPFIX_TUNNEL_GRE = 0x02,
87 DPIF_IPFIX_TUNNEL_LISP = 0x03,
4237026e 88 DPIF_IPFIX_TUNNEL_STT = 0x04,
8b7ea2d4
WZ
89 DPIF_IPFIX_TUNNEL_GENEVE = 0x07,
90 NUM_DPIF_IPFIX_TUNNEL
91};
92
fb8f22c1
BY
93typedef struct ofputil_ipfix_stats ofproto_ipfix_stats;
94
fe520682 95struct dpif_ipfix_global_stats {
564230b6
PS
96 uint64_t dropped_packet_total_count;
97 uint64_t dropped_octet_total_count;
fe520682 98 uint64_t packet_total_count;
99 uint64_t octet_total_count;
100 uint64_t octet_total_sum_of_squares;
101 uint64_t layer2_octet_total_count;
0b4f83e2
PS
102 uint64_t tcp_ack_total_count;
103 uint64_t tcp_fin_total_count;
104 uint64_t tcp_psh_total_count;
105 uint64_t tcp_rst_total_count;
106 uint64_t tcp_syn_total_count;
107 uint64_t tcp_urg_total_count;
ebfe9a21
PS
108 uint64_t post_mcast_packet_total_count;
109 uint64_t post_mcast_octet_total_count;
110 uint64_t in_ucast_packet_total_count;
111 uint64_t in_mcast_packet_total_count;
112 uint64_t in_bcast_packet_total_count;
113 uint64_t out_ucast_packet_total_count;
114 uint64_t out_bcast_packet_total_count;
fe520682 115};
116
8b7ea2d4 117struct dpif_ipfix_port {
cd32509e 118 struct hmap_node hmap_node; /* In struct dpif_ipfix's "ports" hmap. */
8b7ea2d4
WZ
119 struct ofport *ofport; /* To retrieve port stats. */
120 odp_port_t odp_port;
121 enum dpif_ipfix_tunnel_type tunnel_type;
122 uint8_t tunnel_key_length;
cd32509e 123 uint32_t ifindex;
8b7ea2d4
WZ
124};
125
29089a54 126struct dpif_ipfix_exporter {
d564c4bb 127 uint32_t exporter_id; /* Exporting Process identifier */
29089a54
RL
128 struct collectors *collectors;
129 uint32_t seq_number;
130 time_t last_template_set_time;
978427a5 131 struct hmap cache_flow_key_map; /* ipfix_flow_cache_entry. */
ca6ba700 132 struct ovs_list cache_flow_start_timestamp_list; /* ipfix_flow_cache_entry. */
978427a5
RL
133 uint32_t cache_active_timeout; /* In seconds. */
134 uint32_t cache_max_flows;
c97320eb
WZ
135 char *virtual_obs_id;
136 uint8_t virtual_obs_len;
fb8f22c1 137
fe520682 138 ofproto_ipfix_stats ofproto_stats;
139 struct dpif_ipfix_global_stats ipfix_global_stats;
29089a54
RL
140};
141
142struct dpif_ipfix_bridge_exporter {
143 struct dpif_ipfix_exporter exporter;
144 struct ofproto_ipfix_bridge_exporter_options *options;
145 uint32_t probability;
146};
147
148struct dpif_ipfix_flow_exporter {
149 struct dpif_ipfix_exporter exporter;
150 struct ofproto_ipfix_flow_exporter_options *options;
151};
152
153struct dpif_ipfix_flow_exporter_map_node {
154 struct hmap_node node;
155 struct dpif_ipfix_flow_exporter exporter;
156};
157
158struct dpif_ipfix {
159 struct dpif_ipfix_bridge_exporter bridge_exporter;
978427a5 160 struct hmap flow_exporter_map; /* dpif_ipfix_flow_exporter_map_node. */
cd32509e
MW
161 struct hmap ports; /* Contains "struct dpif_ipfix_port"s.
162 * It makes port lookups faster in sampling
163 * upcalls. */
37bec3d3 164 struct ovs_refcount ref_cnt;
29089a54
RL
165};
166
167#define IPFIX_VERSION 0x000a
168
169/* When using UDP, IPFIX Template Records must be re-sent regularly.
170 * The standard default interval is 10 minutes (600 seconds).
171 * Cf. IETF RFC 5101 Section 10.3.6. */
172#define IPFIX_TEMPLATE_INTERVAL 600
173
174/* Cf. IETF RFC 5101 Section 3.1. */
13b6bae6 175OVS_PACKED(
29089a54
RL
176struct ipfix_header {
177 ovs_be16 version; /* IPFIX_VERSION. */
178 ovs_be16 length; /* Length in bytes including this header. */
179 ovs_be32 export_time; /* Seconds since the epoch. */
180 ovs_be32 seq_number; /* Message sequence number. */
181 ovs_be32 obs_domain_id; /* Observation Domain ID. */
13b6bae6 182});
29089a54
RL
183BUILD_ASSERT_DECL(sizeof(struct ipfix_header) == 16);
184
185#define IPFIX_SET_ID_TEMPLATE 2
186#define IPFIX_SET_ID_OPTION_TEMPLATE 3
187
d564c4bb
PS
188enum ipfix_options_template {
189 IPFIX_OPTIONS_TEMPLATE_EXPORTER_STATS = 0,
190 NUM_IPFIX_OPTIONS_TEMPLATE
191};
192
29089a54 193/* Cf. IETF RFC 5101 Section 3.3.2. */
13b6bae6 194OVS_PACKED(
29089a54
RL
195struct ipfix_set_header {
196 ovs_be16 set_id; /* IPFIX_SET_ID_* or valid template ID for Data Sets. */
197 ovs_be16 length; /* Length of the set in bytes including header. */
13b6bae6 198});
29089a54
RL
199BUILD_ASSERT_DECL(sizeof(struct ipfix_set_header) == 4);
200
201/* Alternatives for templates at each layer. A template is defined by
202 * a combination of one value for each layer. */
203enum ipfix_proto_l2 {
204 IPFIX_PROTO_L2_ETH = 0, /* No VLAN. */
205 IPFIX_PROTO_L2_VLAN,
206 NUM_IPFIX_PROTO_L2
207};
208enum ipfix_proto_l3 {
209 IPFIX_PROTO_L3_UNKNOWN = 0,
210 IPFIX_PROTO_L3_IPV4,
211 IPFIX_PROTO_L3_IPV6,
212 NUM_IPFIX_PROTO_L3
213};
214enum ipfix_proto_l4 {
215 IPFIX_PROTO_L4_UNKNOWN = 0,
0b4f83e2
PS
216 IPFIX_PROTO_L4_TCP,
217 IPFIX_PROTO_L4_UDP,
218 IPFIX_PROTO_L4_SCTP,
f51e8ccb 219 IPFIX_PROTO_L4_ICMP,
29089a54
RL
220 NUM_IPFIX_PROTO_L4
221};
8b7ea2d4
WZ
222enum ipfix_proto_tunnel {
223 IPFIX_PROTO_NOT_TUNNELED = 0,
224 IPFIX_PROTO_TUNNELED, /* Support gre, lisp and vxlan. */
225 NUM_IPFIX_PROTO_TUNNEL
226};
29089a54
RL
227
228/* Any Template ID > 255 is usable for Template Records. */
229#define IPFIX_TEMPLATE_ID_MIN 256
230
231/* Cf. IETF RFC 5101 Section 3.4.1. */
13b6bae6 232OVS_PACKED(
29089a54
RL
233struct ipfix_template_record_header {
234 ovs_be16 template_id;
235 ovs_be16 field_count;
13b6bae6 236});
29089a54
RL
237BUILD_ASSERT_DECL(sizeof(struct ipfix_template_record_header) == 4);
238
d564c4bb
PS
239/* Cf. IETF RFC 5101 Section 3.4.2.2. */
240OVS_PACKED(
241struct ipfix_options_template_record_header {
242 ovs_be16 template_id; /* Template ID of Data Set is within 256-65535
243 range. */
244 ovs_be16 field_count; /* Number of all fields in this Options
245 * Template Record, including the Scope
246 * Fields. */
247 ovs_be16 scope_field_count; /* Number of scope fields. The number MUST BE
248 * greater than 0. */
249});
250BUILD_ASSERT_DECL(sizeof(struct ipfix_options_template_record_header) == 6);
251
252
29089a54 253enum ipfix_entity_id {
8b7ea2d4 254/* standard IPFIX elements */
29089a54
RL
255#define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_ID_##ENUM = ID,
256#include "ofproto/ipfix-entities.def"
8b7ea2d4
WZ
257/* non-standard IPFIX elements */
258#define IPFIX_SET_ENTERPRISE(v) (((v) | 0x8000))
259#define IPFIX_ENTERPRISE_ENTITY(ENUM, ID, SIZE, NAME, ENTERPRISE) \
260 IPFIX_ENTITY_ID_##ENUM = IPFIX_SET_ENTERPRISE(ID),
261#include "ofproto/ipfix-enterprise-entities.def"
29089a54
RL
262};
263
264enum ipfix_entity_size {
8b7ea2d4 265/* standard IPFIX elements */
29089a54
RL
266#define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_SIZE_##ENUM = SIZE,
267#include "ofproto/ipfix-entities.def"
8b7ea2d4
WZ
268/* non-standard IPFIX elements */
269#define IPFIX_ENTERPRISE_ENTITY(ENUM, ID, SIZE, NAME, ENTERPRISE) \
270 IPFIX_ENTITY_SIZE_##ENUM = SIZE,
271#include "ofproto/ipfix-enterprise-entities.def"
272};
273
274enum ipfix_entity_enterprise {
275/* standard IPFIX elements */
276#define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_ENTERPRISE_##ENUM = 0,
277#include "ofproto/ipfix-entities.def"
278/* non-standard IPFIX elements */
279#define IPFIX_ENTERPRISE_ENTITY(ENUM, ID, SIZE, NAME, ENTERPRISE) \
280 IPFIX_ENTITY_ENTERPRISE_##ENUM = ENTERPRISE,
281#include "ofproto/ipfix-enterprise-entities.def"
29089a54
RL
282};
283
13b6bae6 284OVS_PACKED(
29089a54
RL
285struct ipfix_template_field_specifier {
286 ovs_be16 element_id; /* IPFIX_ENTITY_ID_*. */
8b7ea2d4
WZ
287 ovs_be16 field_length; /* Length of the field's value, in bytes.
288 * For Variable-Length element, it should be 65535.
289 */
290 ovs_be32 enterprise; /* Enterprise number */
13b6bae6 291});
8b7ea2d4
WZ
292BUILD_ASSERT_DECL(sizeof(struct ipfix_template_field_specifier) == 8);
293
294/* Cf. IETF RFC 5102 Section 5.11.6. */
295enum ipfix_flow_direction {
296 INGRESS_FLOW = 0x00,
cd32509e
MW
297 EGRESS_FLOW = 0x01,
298 NUM_IPFIX_FLOW_DIRECTION
8b7ea2d4 299};
29089a54 300
978427a5 301/* Part of data record flow key for common metadata and Ethernet entities. */
13b6bae6 302OVS_PACKED(
978427a5 303struct ipfix_data_record_flow_key_common {
29089a54 304 ovs_be32 observation_point_id; /* OBSERVATION_POINT_ID */
8b7ea2d4 305 uint8_t flow_direction; /* FLOW_DIRECTION */
74ff3298
JR
306 struct eth_addr source_mac_address; /* SOURCE_MAC_ADDRESS */
307 struct eth_addr destination_mac_address; /* DESTINATION_MAC_ADDRESS */
29089a54 308 ovs_be16 ethernet_type; /* ETHERNET_TYPE */
29089a54 309 uint8_t ethernet_header_length; /* ETHERNET_HEADER_LENGTH */
13b6bae6 310});
8b7ea2d4 311BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_common) == 20);
29089a54 312
cd32509e
MW
313/* Part of data record flow key for interface information. Since some of the
314 * elements have variable length, members of this structure should be appended
315 * to the 'struct dp_packet' one by one. */
316struct ipfix_data_record_flow_key_iface {
317 ovs_be32 if_index; /* (INGRESS | EGRESS)_INTERFACE */
318 ovs_be32 if_type; /* (INGRESS | EGRESS)_INTERFACE_TYPE */
319 uint8_t if_name_len; /* Variable length element: INTERFACE_NAME */
320 char *if_name;
321 uint8_t if_descr_len; /* Variable length element: INTERFACE_DESCRIPTION */
322 char *if_descr;
323};
324
978427a5 325/* Part of data record flow key for VLAN entities. */
13b6bae6 326OVS_PACKED(
978427a5 327struct ipfix_data_record_flow_key_vlan {
29089a54
RL
328 ovs_be16 vlan_id; /* VLAN_ID */
329 ovs_be16 dot1q_vlan_id; /* DOT1Q_VLAN_ID */
330 uint8_t dot1q_priority; /* DOT1Q_PRIORITY */
13b6bae6 331});
978427a5 332BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_vlan) == 5);
29089a54 333
978427a5
RL
334/* Part of data record flow key for IP entities. */
335/* XXX: Replace IP_TTL with MINIMUM_TTL and MAXIMUM_TTL? */
13b6bae6 336OVS_PACKED(
978427a5 337struct ipfix_data_record_flow_key_ip {
29089a54
RL
338 uint8_t ip_version; /* IP_VERSION */
339 uint8_t ip_ttl; /* IP_TTL */
340 uint8_t protocol_identifier; /* PROTOCOL_IDENTIFIER */
341 uint8_t ip_diff_serv_code_point; /* IP_DIFF_SERV_CODE_POINT */
342 uint8_t ip_precedence; /* IP_PRECEDENCE */
343 uint8_t ip_class_of_service; /* IP_CLASS_OF_SERVICE */
13b6bae6 344});
978427a5 345BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ip) == 6);
29089a54 346
978427a5 347/* Part of data record flow key for IPv4 entities. */
13b6bae6 348OVS_PACKED(
978427a5 349struct ipfix_data_record_flow_key_ipv4 {
29089a54
RL
350 ovs_be32 source_ipv4_address; /* SOURCE_IPV4_ADDRESS */
351 ovs_be32 destination_ipv4_address; /* DESTINATION_IPV4_ADDRESS */
13b6bae6 352});
978427a5 353BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ipv4) == 8);
29089a54 354
978427a5 355/* Part of data record flow key for IPv6 entities. */
13b6bae6 356OVS_PACKED(
978427a5 357struct ipfix_data_record_flow_key_ipv6 {
29089a54
RL
358 uint8_t source_ipv6_address[16]; /* SOURCE_IPV6_ADDRESS */
359 uint8_t destination_ipv6_address[16]; /* DESTINATION_IPV6_ADDRESS */
360 ovs_be32 flow_label_ipv6; /* FLOW_LABEL_IPV6 */
13b6bae6 361});
978427a5 362BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ipv6) == 36);
29089a54 363
f51e8ccb 364/* Part of data record flow key for TCP/UDP/SCTP entities. */
13b6bae6 365OVS_PACKED(
f51e8ccb 366struct ipfix_data_record_flow_key_transport {
29089a54
RL
367 ovs_be16 source_transport_port; /* SOURCE_TRANSPORT_PORT */
368 ovs_be16 destination_transport_port; /* DESTINATION_TRANSPORT_PORT */
13b6bae6 369});
f51e8ccb
RL
370BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_transport) == 4);
371
372/* Part of data record flow key for ICMP entities. */
373OVS_PACKED(
374struct ipfix_data_record_flow_key_icmp {
375 uint8_t icmp_type; /* ICMP_TYPE_IPV4 / ICMP_TYPE_IPV6 */
376 uint8_t icmp_code; /* ICMP_CODE_IPV4 / ICMP_CODE_IPV6 */
377});
378BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_icmp) == 2);
978427a5 379
8b7ea2d4
WZ
380static uint8_t tunnel_protocol[NUM_DPIF_IPFIX_TUNNEL] = {
381 0, /* reserved */
382 IPPROTO_UDP, /* DPIF_IPFIX_TUNNEL_VXLAN */
383 IPPROTO_GRE, /* DPIF_IPFIX_TUNNEL_GRE */
384 IPPROTO_UDP, /* DPIF_IPFIX_TUNNEL_LISP*/
4237026e 385 IPPROTO_TCP, /* DPIF_IPFIX_TUNNEL_STT*/
8b7ea2d4
WZ
386 0 , /* reserved */
387 IPPROTO_UDP, /* DPIF_IPFIX_TUNNEL_GENEVE*/
388};
389
390OVS_PACKED(
391struct ipfix_data_record_flow_key_tunnel {
392 ovs_be32 tunnel_source_ipv4_address; /* TUNNEL_SOURCE_IPV4_ADDRESS */
393 ovs_be32 tunnel_destination_ipv4_address; /* TUNNEL_DESTINATION_IPV4_ADDRESS */
394 uint8_t tunnel_protocol_identifier; /* TUNNEL_PROTOCOL_IDENTIFIER */
395 ovs_be16 tunnel_source_transport_port; /* TUNNEL_SOURCE_TRANSPORT_PORT */
396 ovs_be16 tunnel_destination_transport_port; /* TUNNEL_DESTINATION_TRANSPORT_PORT */
397 uint8_t tunnel_type; /* TUNNEL_TYPE */
398 uint8_t tunnel_key_length; /* length of TUNNEL_KEY */
399 uint8_t tunnel_key[]; /* data of TUNNEL_KEY */
400});
401BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_tunnel) == 15);
402
978427a5
RL
403/* Cf. IETF RFC 5102 Section 5.11.3. */
404enum ipfix_flow_end_reason {
405 IDLE_TIMEOUT = 0x01,
406 ACTIVE_TIMEOUT = 0x02,
407 END_OF_FLOW_DETECTED = 0x03,
408 FORCED_END = 0x04,
409 LACK_OF_RESOURCES = 0x05
410};
411
d564c4bb
PS
412/* Exporting Process Reliability Statistics data record. */
413OVS_PACKED(
414struct ipfix_data_record_exporter_stats {
415 /* Scope Fields */
416 ovs_be32 exporting_process_id; /* EXPORTING_PROCESS_ID */
417
418 /* Fields */
419 ovs_be64 not_sent_packet_total_count; /* NOT_SENT_PACKET_TOTAL_COUNT */
420});
421BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_exporter_stats) == 12);
422
978427a5
RL
423/* Part of data record for common aggregated elements. */
424OVS_PACKED(
425struct ipfix_data_record_aggregated_common {
426 ovs_be32 flow_start_delta_microseconds; /* FLOW_START_DELTA_MICROSECONDS */
427 ovs_be32 flow_end_delta_microseconds; /* FLOW_END_DELTA_MICROSECONDS */
564230b6
PS
428 ovs_be64 dropped_packet_delta_count; /* DROPPED_PACKET_DELTA_COUNT */
429 ovs_be64 dropped_packet_total_count; /* DROPPED_PACKET_TOTAL_COUNT */
978427a5 430 ovs_be64 packet_delta_count; /* PACKET_DELTA_COUNT */
0b4f83e2 431 ovs_be64 packet_total_count; /* PACKET_TOTAL_COUNT */
ebfe9a21
PS
432 /* INGRESS_UNICAST_PACKET_TOTAL_COUNT */
433 ovs_be64 in_ucast_packet_total_count;
434 /* INGRESS_MULTICAST_PACKET_TOTAL_COUNT */
435 ovs_be64 in_mcast_packet_total_count;
436 /* INGRESS_BROADCAST_PACKET_TOTAL_COUNT */
437 ovs_be64 in_bcast_packet_total_count;
438 /* EGRESS_UNICAST_PACKET_TOTAL_COUNT */
439 ovs_be64 out_ucast_packet_total_count;
440 /* EGRESS_BROADCAST_PACKET_TOTAL_COUNT */
441 ovs_be64 out_bcast_packet_total_count;
442 ovs_be64 post_mcast_packet_delta_count; /* POST_MCAST_PACKET_DELTA_COUNT */
443 ovs_be64 post_mcast_packet_total_count; /* POST_MCAST_PACKET_TOTAL_COUNT */
978427a5 444 ovs_be64 layer2_octet_delta_count; /* LAYER2_OCTET_DELTA_COUNT */
0b4f83e2 445 ovs_be64 layer2_octet_total_count; /* LAYER2_OCTET_TOTAL_COUNT */
978427a5
RL
446 uint8_t flow_end_reason; /* FLOW_END_REASON */
447});
564230b6 448BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_aggregated_common) == 113);
978427a5
RL
449
450/* Part of data record for IP aggregated elements. */
451OVS_PACKED(
452struct ipfix_data_record_aggregated_ip {
564230b6
PS
453 ovs_be64 dropped_octet_delta_count; /* DROPPED_OCTET_DELTA_COUNT */
454 ovs_be64 dropped_octet_total_count; /* DROPPED_OCTET_TOTAL_COUNT */
f51e8ccb 455 ovs_be64 octet_delta_count; /* OCTET_DELTA_COUNT */
fe520682 456 ovs_be64 octet_total_count; /* OCTET_TOTAL_COUNT */
978427a5 457 ovs_be64 octet_delta_sum_of_squares; /* OCTET_DELTA_SUM_OF_SQUARES */
fe520682 458 ovs_be64 octet_total_sum_of_squares; /* OCTET_TOTAL_SUM_OF_SQUARES */
978427a5
RL
459 ovs_be64 minimum_ip_total_length; /* MINIMUM_IP_TOTAL_LENGTH */
460 ovs_be64 maximum_ip_total_length; /* MAXIMUM_IP_TOTAL_LENGTH */
ebfe9a21
PS
461 ovs_be64 post_mcast_octet_delta_count; /* POST_MCAST_OCTET_DELTA_COUNT */
462 ovs_be64 post_mcast_octet_total_count; /* POST_MCAST_OCTET_TOTAL_COUNT */
978427a5 463});
564230b6 464BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_aggregated_ip) == 80);
978427a5 465
0b4f83e2
PS
466/* Part of data record for TCP aggregated elements. */
467OVS_PACKED(
468struct ipfix_data_record_aggregated_tcp {
469 ovs_be64 tcp_ack_total_count; /* TCP_ACK_TOTAL_COUNT */
470 ovs_be64 tcp_fin_total_count; /* TCP_FIN_TOTAL_COUNT */
471 ovs_be64 tcp_psh_total_count; /* TCP_PSH_TOTAL_COUNT */
472 ovs_be64 tcp_rst_total_count; /* TCP_RST_TOTAL_COUNT */
473 ovs_be64 tcp_syn_total_count; /* TCP_SYN_TOTAL_COUNT */
474 ovs_be64 tcp_urg_total_count; /* TCP_URG_TOTAL_COUNT */
475});
476BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_aggregated_tcp) == 48);
477
c97320eb
WZ
478/*
479 * Refer to RFC 7011, the length of Variable length element is 0~65535:
480 * In most case, it should be less than 255 octets:
481 * 0 1 2 3
482 * 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
483 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
484 * | Length (< 255)| Information Element |
485 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
486 * | ... continuing as needed |
487 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
488 *
489 * When it is greater than or equeal to 255 octets:
490 * 0 1 2 3
491 * 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
492 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
493 * | 255 | Length (0 to 65535) | IE |
494 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
495 * | ... continuing as needed |
496 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
497 *
498 *
499 * Now, only the virtual_obs_id whose length < 255 is implemented.
500 */
501
502#define IPFIX_VIRTUAL_OBS_MAX_LEN 254
503
8b7ea2d4
WZ
504/*
505 * support tunnel key for:
506 * VxLAN: 24-bit VIN,
99e7b077 507 * GRE: 32-bit key,
8b7ea2d4 508 * LISP: 24-bit instance ID
4237026e 509 * STT: 64-bit key
8b7ea2d4
WZ
510 */
511#define MAX_TUNNEL_KEY_LEN 8
512
f51e8ccb
RL
513#define MAX_FLOW_KEY_LEN \
514 (sizeof(struct ipfix_data_record_flow_key_common) \
515 + sizeof(struct ipfix_data_record_flow_key_vlan) \
516 + sizeof(struct ipfix_data_record_flow_key_ip) \
517 + MAX(sizeof(struct ipfix_data_record_flow_key_ipv4), \
518 sizeof(struct ipfix_data_record_flow_key_ipv6)) \
519 + MAX(sizeof(struct ipfix_data_record_flow_key_icmp), \
8b7ea2d4
WZ
520 sizeof(struct ipfix_data_record_flow_key_transport)) \
521 + sizeof(struct ipfix_data_record_flow_key_tunnel) \
522 + MAX_TUNNEL_KEY_LEN)
978427a5
RL
523
524#define MAX_DATA_RECORD_LEN \
525 (MAX_FLOW_KEY_LEN \
526 + sizeof(struct ipfix_data_record_aggregated_common) \
0b4f83e2
PS
527 + sizeof(struct ipfix_data_record_aggregated_ip) \
528 + sizeof(struct ipfix_data_record_aggregated_tcp))
978427a5 529
d564c4bb
PS
530#define MAX_OPTIONS_DATA_RECORD_LEN \
531 (sizeof(struct ipfix_data_record_exporter_stats))
532
533
978427a5
RL
534/* Max length of a data set. To simplify the implementation, each
535 * data record is sent in a separate data set, so each data set
536 * contains at most one data record. */
d564c4bb
PS
537#define MAX_DATA_SET_LEN \
538 (sizeof(struct ipfix_set_header) \
539 + MAX(MAX_DATA_RECORD_LEN, \
540 MAX_OPTIONS_DATA_RECORD_LEN))
978427a5 541
8b7ea2d4 542/* Max length of an IPFIX message. Arbitrarily set to accommodate low
978427a5
RL
543 * MTU. */
544#define MAX_MESSAGE_LEN 1024
545
546/* Cache structures. */
547
548/* Flow key. */
549struct ipfix_flow_key {
550 uint32_t obs_domain_id;
551 uint16_t template_id;
552 size_t flow_key_msg_part_size;
553 uint64_t flow_key_msg_part[DIV_ROUND_UP(MAX_FLOW_KEY_LEN, 8)];
554};
555
556/* Flow cache entry. */
557struct ipfix_flow_cache_entry {
558 struct hmap_node flow_key_map_node;
ca6ba700 559 struct ovs_list cache_flow_start_timestamp_list_node;
978427a5
RL
560 struct ipfix_flow_key flow_key;
561 /* Common aggregated elements. */
562 uint64_t flow_start_timestamp_usec;
563 uint64_t flow_end_timestamp_usec;
564230b6
PS
564 uint64_t dropped_packet_delta_count;
565 uint64_t dropped_packet_total_count;
978427a5 566 uint64_t packet_delta_count;
fe520682 567 uint64_t packet_total_count;
ebfe9a21
PS
568 uint64_t in_ucast_packet_total_count;
569 uint64_t in_mcast_packet_total_count;
570 uint64_t in_bcast_packet_total_count;
571 uint64_t out_ucast_packet_total_count;
572 uint64_t out_bcast_packet_total_count;
573 uint64_t post_mcast_packet_total_count;
574 uint64_t post_mcast_packet_delta_count;
575 uint64_t post_mcast_octet_total_count;
576 uint64_t post_mcast_octet_delta_count;
978427a5 577 uint64_t layer2_octet_delta_count;
fe520682 578 uint64_t layer2_octet_total_count;
564230b6
PS
579 uint64_t dropped_octet_delta_count;
580 uint64_t dropped_octet_total_count;
f51e8ccb 581 uint64_t octet_delta_count;
fe520682 582 uint64_t octet_total_count;
978427a5 583 uint64_t octet_delta_sum_of_squares; /* 0 if not IP. */
fe520682 584 uint64_t octet_total_sum_of_squares; /* 0 if not IP. */
978427a5
RL
585 uint16_t minimum_ip_total_length; /* 0 if not IP. */
586 uint16_t maximum_ip_total_length; /* 0 if not IP. */
0b4f83e2
PS
587 uint64_t tcp_packet_delta_count;
588 uint64_t tcp_ack_total_count;
589 uint64_t tcp_fin_total_count;
590 uint64_t tcp_psh_total_count;
591 uint64_t tcp_rst_total_count;
592 uint64_t tcp_syn_total_count;
593 uint64_t tcp_urg_total_count;
978427a5
RL
594};
595
596static void dpif_ipfix_cache_expire(struct dpif_ipfix_exporter *, bool,
597 const uint64_t, const uint32_t);
598
599static void get_export_time_now(uint64_t *, uint32_t *);
600
601static void dpif_ipfix_cache_expire_now(struct dpif_ipfix_exporter *, bool);
29089a54
RL
602
603static bool
604ofproto_ipfix_bridge_exporter_options_equal(
605 const struct ofproto_ipfix_bridge_exporter_options *a,
606 const struct ofproto_ipfix_bridge_exporter_options *b)
607{
608 return (a->obs_domain_id == b->obs_domain_id
609 && a->obs_point_id == b->obs_point_id
610 && a->sampling_rate == b->sampling_rate
978427a5
RL
611 && a->cache_active_timeout == b->cache_active_timeout
612 && a->cache_max_flows == b->cache_max_flows
8b7ea2d4
WZ
613 && a->enable_tunnel_sampling == b->enable_tunnel_sampling
614 && a->enable_input_sampling == b->enable_input_sampling
615 && a->enable_output_sampling == b->enable_output_sampling
c97320eb
WZ
616 && sset_equals(&a->targets, &b->targets)
617 && nullable_string_is_equal(a->virtual_obs_id, b->virtual_obs_id));
29089a54
RL
618}
619
620static struct ofproto_ipfix_bridge_exporter_options *
621ofproto_ipfix_bridge_exporter_options_clone(
622 const struct ofproto_ipfix_bridge_exporter_options *old)
623{
624 struct ofproto_ipfix_bridge_exporter_options *new =
625 xmemdup(old, sizeof *old);
626 sset_clone(&new->targets, &old->targets);
c97320eb 627 new->virtual_obs_id = nullable_xstrdup(old->virtual_obs_id);
29089a54
RL
628 return new;
629}
630
631static void
632ofproto_ipfix_bridge_exporter_options_destroy(
633 struct ofproto_ipfix_bridge_exporter_options *options)
634{
635 if (options) {
636 sset_destroy(&options->targets);
c97320eb 637 free(options->virtual_obs_id);
29089a54
RL
638 free(options);
639 }
640}
641
642static bool
643ofproto_ipfix_flow_exporter_options_equal(
644 const struct ofproto_ipfix_flow_exporter_options *a,
645 const struct ofproto_ipfix_flow_exporter_options *b)
646{
647 return (a->collector_set_id == b->collector_set_id
978427a5
RL
648 && a->cache_active_timeout == b->cache_active_timeout
649 && a->cache_max_flows == b->cache_max_flows
f69f713b 650 && a->enable_tunnel_sampling == b->enable_tunnel_sampling
c97320eb
WZ
651 && sset_equals(&a->targets, &b->targets)
652 && nullable_string_is_equal(a->virtual_obs_id, b->virtual_obs_id));
29089a54
RL
653}
654
655static struct ofproto_ipfix_flow_exporter_options *
656ofproto_ipfix_flow_exporter_options_clone(
657 const struct ofproto_ipfix_flow_exporter_options *old)
658{
659 struct ofproto_ipfix_flow_exporter_options *new =
660 xmemdup(old, sizeof *old);
661 sset_clone(&new->targets, &old->targets);
c97320eb 662 new->virtual_obs_id = nullable_xstrdup(old->virtual_obs_id);
29089a54
RL
663 return new;
664}
665
666static void
667ofproto_ipfix_flow_exporter_options_destroy(
668 struct ofproto_ipfix_flow_exporter_options *options)
669{
670 if (options) {
671 sset_destroy(&options->targets);
c97320eb 672 free(options->virtual_obs_id);
29089a54
RL
673 free(options);
674 }
675}
676
978427a5
RL
677static void
678dpif_ipfix_exporter_init(struct dpif_ipfix_exporter *exporter)
679{
d564c4bb 680 exporter->exporter_id = ++exporter_total_count;
978427a5
RL
681 exporter->collectors = NULL;
682 exporter->seq_number = 1;
d1b97f99 683 exporter->last_template_set_time = 0;
978427a5 684 hmap_init(&exporter->cache_flow_key_map);
417e7e66 685 ovs_list_init(&exporter->cache_flow_start_timestamp_list);
978427a5
RL
686 exporter->cache_active_timeout = 0;
687 exporter->cache_max_flows = 0;
c97320eb
WZ
688 exporter->virtual_obs_id = NULL;
689 exporter->virtual_obs_len = 0;
fe520682 690
691 memset(&exporter->ipfix_global_stats, 0,
692 sizeof(struct dpif_ipfix_global_stats));
978427a5
RL
693}
694
29089a54
RL
695static void
696dpif_ipfix_exporter_clear(struct dpif_ipfix_exporter *exporter)
697{
978427a5
RL
698 /* Flush the cache with flow end reason "forced end." */
699 dpif_ipfix_cache_expire_now(exporter, true);
700
29089a54 701 collectors_destroy(exporter->collectors);
d564c4bb 702 exporter->exporter_id = 0;
29089a54
RL
703 exporter->collectors = NULL;
704 exporter->seq_number = 1;
d1b97f99 705 exporter->last_template_set_time = 0;
978427a5
RL
706 exporter->cache_active_timeout = 0;
707 exporter->cache_max_flows = 0;
c97320eb
WZ
708 free(exporter->virtual_obs_id);
709 exporter->virtual_obs_id = NULL;
710 exporter->virtual_obs_len = 0;
fe520682 711
712 memset(&exporter->ipfix_global_stats, 0,
713 sizeof(struct dpif_ipfix_global_stats));
978427a5
RL
714}
715
716static void
717dpif_ipfix_exporter_destroy(struct dpif_ipfix_exporter *exporter)
718{
719 dpif_ipfix_exporter_clear(exporter);
720 hmap_destroy(&exporter->cache_flow_key_map);
29089a54
RL
721}
722
723static bool
724dpif_ipfix_exporter_set_options(struct dpif_ipfix_exporter *exporter,
978427a5
RL
725 const struct sset *targets,
726 const uint32_t cache_active_timeout,
c97320eb
WZ
727 const uint32_t cache_max_flows,
728 const char *virtual_obs_id)
29089a54 729{
c97320eb 730 size_t virtual_obs_len;
29089a54
RL
731 collectors_destroy(exporter->collectors);
732 collectors_create(targets, IPFIX_DEFAULT_COLLECTOR_PORT,
733 &exporter->collectors);
734 if (exporter->collectors == NULL) {
735 VLOG_WARN_RL(&rl, "no collectors could be initialized, "
736 "IPFIX exporter disabled");
737 dpif_ipfix_exporter_clear(exporter);
738 return false;
739 }
978427a5
RL
740 exporter->cache_active_timeout = cache_active_timeout;
741 exporter->cache_max_flows = cache_max_flows;
c97320eb
WZ
742 virtual_obs_len = virtual_obs_id ? strlen(virtual_obs_id) : 0;
743 if (virtual_obs_len > IPFIX_VIRTUAL_OBS_MAX_LEN) {
744 VLOG_WARN_RL(&rl, "Virtual obsevation ID too long (%d bytes), "
745 "should not be longer than %d bytes.",
746 exporter->virtual_obs_len, IPFIX_VIRTUAL_OBS_MAX_LEN);
747 dpif_ipfix_exporter_clear(exporter);
748 return false;
749 }
750 exporter->virtual_obs_len = virtual_obs_len;
751 exporter->virtual_obs_id = nullable_xstrdup(virtual_obs_id);
29089a54
RL
752 return true;
753}
754
8b7ea2d4
WZ
755static struct dpif_ipfix_port *
756dpif_ipfix_find_port(const struct dpif_ipfix *di,
757 odp_port_t odp_port) OVS_REQUIRES(mutex)
758{
759 struct dpif_ipfix_port *dip;
760
761 HMAP_FOR_EACH_IN_BUCKET (dip, hmap_node, hash_odp_port(odp_port),
cd32509e 762 &di->ports) {
8b7ea2d4
WZ
763 if (dip->odp_port == odp_port) {
764 return dip;
765 }
766 }
767 return NULL;
768}
769
770static void
cd32509e 771dpif_ipfix_del_port__(struct dpif_ipfix *di,
8b7ea2d4
WZ
772 struct dpif_ipfix_port *dip)
773 OVS_REQUIRES(mutex)
774{
cd32509e 775 hmap_remove(&di->ports, &dip->hmap_node);
8b7ea2d4
WZ
776 free(dip);
777}
778
cd32509e
MW
779static enum dpif_ipfix_tunnel_type
780dpif_ipfix_tunnel_type(const struct ofport *ofport)
781{
782 const char *type = netdev_get_type(ofport->netdev);
783
784 if (type == NULL) {
785 return DPIF_IPFIX_TUNNEL_UNKNOWN;
786 }
787 if (strcmp(type, "gre") == 0) {
788 return DPIF_IPFIX_TUNNEL_GRE;
789 } else if (strcmp(type, "vxlan") == 0) {
790 return DPIF_IPFIX_TUNNEL_VXLAN;
791 } else if (strcmp(type, "lisp") == 0) {
792 return DPIF_IPFIX_TUNNEL_LISP;
793 } else if (strcmp(type, "geneve") == 0) {
794 return DPIF_IPFIX_TUNNEL_GENEVE;
795 } else if (strcmp(type, "stt") == 0) {
796 return DPIF_IPFIX_TUNNEL_STT;
797 }
798
799 return DPIF_IPFIX_TUNNEL_UNKNOWN;
800}
801
802static uint8_t
803dpif_ipfix_tunnel_key_length(enum dpif_ipfix_tunnel_type tunnel_type)
804{
805
806 switch (tunnel_type) {
807 case DPIF_IPFIX_TUNNEL_GRE:
808 /* 32-bit key gre */
809 return 4;
810 case DPIF_IPFIX_TUNNEL_VXLAN:
811 case DPIF_IPFIX_TUNNEL_LISP:
812 case DPIF_IPFIX_TUNNEL_GENEVE:
813 return 3;
814 case DPIF_IPFIX_TUNNEL_STT:
815 return 8;
816 case DPIF_IPFIX_TUNNEL_UNKNOWN:
817 case NUM_DPIF_IPFIX_TUNNEL:
818 default:
819 return 0;
820 }
821}
822
8b7ea2d4 823void
cd32509e
MW
824dpif_ipfix_add_port(struct dpif_ipfix *di, struct ofport *ofport,
825 odp_port_t odp_port) OVS_EXCLUDED(mutex)
8b7ea2d4
WZ
826{
827 struct dpif_ipfix_port *dip;
cd32509e 828 int64_t ifindex;
8b7ea2d4
WZ
829
830 ovs_mutex_lock(&mutex);
831 dip = dpif_ipfix_find_port(di, odp_port);
832 if (dip) {
cd32509e 833 dpif_ipfix_del_port__(di, dip);
8b7ea2d4
WZ
834 }
835
cd32509e
MW
836 ifindex = netdev_get_ifindex(ofport->netdev);
837 if (ifindex < 0) {
838 ifindex = 0;
8b7ea2d4
WZ
839 }
840
cd32509e 841 /* Add to table of ports. */
8b7ea2d4
WZ
842 dip = xmalloc(sizeof *dip);
843 dip->ofport = ofport;
844 dip->odp_port = odp_port;
cd32509e
MW
845 dip->tunnel_type = dpif_ipfix_tunnel_type(ofport);
846 dip->tunnel_key_length = dpif_ipfix_tunnel_key_length(dip->tunnel_type);
847 dip->ifindex = ifindex;
848 hmap_insert(&di->ports, &dip->hmap_node, hash_odp_port(odp_port));
8b7ea2d4 849
8b7ea2d4
WZ
850 ovs_mutex_unlock(&mutex);
851}
852
853void
cd32509e 854dpif_ipfix_del_port(struct dpif_ipfix *di, odp_port_t odp_port)
8b7ea2d4
WZ
855 OVS_EXCLUDED(mutex)
856{
857 struct dpif_ipfix_port *dip;
858 ovs_mutex_lock(&mutex);
859 dip = dpif_ipfix_find_port(di, odp_port);
860 if (dip) {
cd32509e 861 dpif_ipfix_del_port__(di, dip);
8b7ea2d4
WZ
862 }
863 ovs_mutex_unlock(&mutex);
864}
865
cd32509e
MW
866static struct dpif_ipfix_port *
867dpif_ipfix_find_tunnel_port(const struct dpif_ipfix *di, odp_port_t odp_port)
868 OVS_REQUIRES(mutex)
869{
870 struct dpif_ipfix_port *dip = dpif_ipfix_find_port(di, odp_port);
871 return (dip && dip->tunnel_type != DPIF_IPFIX_TUNNEL_UNKNOWN) ? dip : NULL;
872}
873
8b7ea2d4 874bool
cd32509e 875dpif_ipfix_is_tunnel_port(const struct dpif_ipfix *di, odp_port_t odp_port)
8b7ea2d4
WZ
876 OVS_EXCLUDED(mutex)
877{
878 struct dpif_ipfix_port *dip;
879 ovs_mutex_lock(&mutex);
cd32509e 880 dip = dpif_ipfix_find_tunnel_port(di, odp_port);
8b7ea2d4
WZ
881 ovs_mutex_unlock(&mutex);
882 return dip != NULL;
883}
884
978427a5
RL
885static void
886dpif_ipfix_bridge_exporter_init(struct dpif_ipfix_bridge_exporter *exporter)
887{
888 dpif_ipfix_exporter_init(&exporter->exporter);
889 exporter->options = NULL;
890 exporter->probability = 0;
891}
892
29089a54
RL
893static void
894dpif_ipfix_bridge_exporter_clear(struct dpif_ipfix_bridge_exporter *exporter)
895{
896 dpif_ipfix_exporter_clear(&exporter->exporter);
897 ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
898 exporter->options = NULL;
899 exporter->probability = 0;
900}
901
978427a5
RL
902static void
903dpif_ipfix_bridge_exporter_destroy(struct dpif_ipfix_bridge_exporter *exporter)
904{
905 dpif_ipfix_bridge_exporter_clear(exporter);
906 dpif_ipfix_exporter_destroy(&exporter->exporter);
907}
908
29089a54
RL
909static void
910dpif_ipfix_bridge_exporter_set_options(
911 struct dpif_ipfix_bridge_exporter *exporter,
912 const struct ofproto_ipfix_bridge_exporter_options *options)
913{
914 bool options_changed;
915
916 if (!options || sset_is_empty(&options->targets)) {
917 /* No point in doing any work if there are no targets. */
918 dpif_ipfix_bridge_exporter_clear(exporter);
919 return;
920 }
921
922 options_changed = (
923 !exporter->options
924 || !ofproto_ipfix_bridge_exporter_options_equal(
925 options, exporter->options));
926
927 /* Configure collectors if options have changed or if we're
928 * shortchanged in collectors (which indicates that opening one or
929 * more of the configured collectors failed, so that we should
930 * retry). */
931 if (options_changed
932 || collectors_count(exporter->exporter.collectors)
933 < sset_count(&options->targets)) {
978427a5
RL
934 if (!dpif_ipfix_exporter_set_options(
935 &exporter->exporter, &options->targets,
c97320eb
WZ
936 options->cache_active_timeout, options->cache_max_flows,
937 options->virtual_obs_id)) {
29089a54
RL
938 return;
939 }
940 }
941
942 /* Avoid reconfiguring if options didn't change. */
943 if (!options_changed) {
944 return;
945 }
946
947 ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
948 exporter->options = ofproto_ipfix_bridge_exporter_options_clone(options);
949 exporter->probability =
950 MAX(1, UINT32_MAX / exporter->options->sampling_rate);
978427a5
RL
951
952 /* Run over the cache as some entries might have expired after
953 * changing the timeouts. */
954 dpif_ipfix_cache_expire_now(&exporter->exporter, false);
29089a54
RL
955}
956
957static struct dpif_ipfix_flow_exporter_map_node*
958dpif_ipfix_find_flow_exporter_map_node(
959 const struct dpif_ipfix *di, const uint32_t collector_set_id)
978427a5 960 OVS_REQUIRES(mutex)
29089a54
RL
961{
962 struct dpif_ipfix_flow_exporter_map_node *exporter_node;
963
964 HMAP_FOR_EACH_WITH_HASH (exporter_node, node,
965 hash_int(collector_set_id, 0),
966 &di->flow_exporter_map) {
967 if (exporter_node->exporter.options->collector_set_id
968 == collector_set_id) {
969 return exporter_node;
970 }
971 }
972
973 return NULL;
974}
975
978427a5
RL
976static void
977dpif_ipfix_flow_exporter_init(struct dpif_ipfix_flow_exporter *exporter)
978{
979 dpif_ipfix_exporter_init(&exporter->exporter);
980 exporter->options = NULL;
981}
982
29089a54
RL
983static void
984dpif_ipfix_flow_exporter_clear(struct dpif_ipfix_flow_exporter *exporter)
985{
986 dpif_ipfix_exporter_clear(&exporter->exporter);
987 ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
988 exporter->options = NULL;
989}
990
978427a5
RL
991static void
992dpif_ipfix_flow_exporter_destroy(struct dpif_ipfix_flow_exporter *exporter)
993{
994 dpif_ipfix_flow_exporter_clear(exporter);
995 dpif_ipfix_exporter_destroy(&exporter->exporter);
996}
997
29089a54
RL
998static bool
999dpif_ipfix_flow_exporter_set_options(
1000 struct dpif_ipfix_flow_exporter *exporter,
1001 const struct ofproto_ipfix_flow_exporter_options *options)
1002{
1003 bool options_changed;
1004
1005 if (sset_is_empty(&options->targets)) {
1006 /* No point in doing any work if there are no targets. */
1007 dpif_ipfix_flow_exporter_clear(exporter);
1008 return true;
1009 }
1010
1011 options_changed = (
1012 !exporter->options
1013 || !ofproto_ipfix_flow_exporter_options_equal(
1014 options, exporter->options));
1015
1016 /* Configure collectors if options have changed or if we're
1017 * shortchanged in collectors (which indicates that opening one or
1018 * more of the configured collectors failed, so that we should
1019 * retry). */
1020 if (options_changed
1021 || collectors_count(exporter->exporter.collectors)
1022 < sset_count(&options->targets)) {
978427a5
RL
1023 if (!dpif_ipfix_exporter_set_options(
1024 &exporter->exporter, &options->targets,
c97320eb
WZ
1025 options->cache_active_timeout, options->cache_max_flows,
1026 options->virtual_obs_id)) {
29089a54
RL
1027 return false;
1028 }
1029 }
1030
1031 /* Avoid reconfiguring if options didn't change. */
1032 if (!options_changed) {
1033 return true;
1034 }
1035
1036 ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
1037 exporter->options = ofproto_ipfix_flow_exporter_options_clone(options);
1038
978427a5
RL
1039 /* Run over the cache as some entries might have expired after
1040 * changing the timeouts. */
1041 dpif_ipfix_cache_expire_now(&exporter->exporter, false);
1042
29089a54
RL
1043 return true;
1044}
1045
cbef684c
BP
1046static void
1047remove_flow_exporter(struct dpif_ipfix *di,
1048 struct dpif_ipfix_flow_exporter_map_node *node)
1049{
1050 hmap_remove(&di->flow_exporter_map, &node->node);
1051 dpif_ipfix_flow_exporter_destroy(&node->exporter);
1052 free(node);
1053}
1054
29089a54
RL
1055void
1056dpif_ipfix_set_options(
1057 struct dpif_ipfix *di,
1058 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1059 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
626ace7b 1060 size_t n_flow_exporters_options) OVS_EXCLUDED(mutex)
29089a54
RL
1061{
1062 int i;
1063 struct ofproto_ipfix_flow_exporter_options *options;
1064 struct dpif_ipfix_flow_exporter_map_node *node, *next;
29089a54 1065
626ace7b 1066 ovs_mutex_lock(&mutex);
29089a54
RL
1067 dpif_ipfix_bridge_exporter_set_options(&di->bridge_exporter,
1068 bridge_exporter_options);
1069
1070 /* Add new flow exporters and update current flow exporters. */
1071 options = (struct ofproto_ipfix_flow_exporter_options *)
1072 flow_exporters_options;
1073 for (i = 0; i < n_flow_exporters_options; i++) {
1074 node = dpif_ipfix_find_flow_exporter_map_node(
1075 di, options->collector_set_id);
1076 if (!node) {
1077 node = xzalloc(sizeof *node);
978427a5 1078 dpif_ipfix_flow_exporter_init(&node->exporter);
29089a54
RL
1079 hmap_insert(&di->flow_exporter_map, &node->node,
1080 hash_int(options->collector_set_id, 0));
1081 }
1082 if (!dpif_ipfix_flow_exporter_set_options(&node->exporter, options)) {
cbef684c 1083 remove_flow_exporter(di, node);
29089a54
RL
1084 }
1085 options++;
1086 }
1087
29089a54 1088 /* Remove dropped flow exporters, if any needs to be removed. */
cbef684c
BP
1089 HMAP_FOR_EACH_SAFE (node, next, node, &di->flow_exporter_map) {
1090 /* This is slow but doesn't take any extra memory, and
1091 * this table is not supposed to contain many rows anyway. */
1092 options = (struct ofproto_ipfix_flow_exporter_options *)
1093 flow_exporters_options;
1094 for (i = 0; i < n_flow_exporters_options; i++) {
1095 if (node->exporter.options->collector_set_id
1096 == options->collector_set_id) {
1097 break;
29089a54 1098 }
cbef684c
BP
1099 options++;
1100 }
1101 if (i == n_flow_exporters_options) { // Not found.
1102 remove_flow_exporter(di, node);
29089a54
RL
1103 }
1104 }
1105
626ace7b 1106 ovs_mutex_unlock(&mutex);
29089a54
RL
1107}
1108
1109struct dpif_ipfix *
1110dpif_ipfix_create(void)
1111{
1112 struct dpif_ipfix *di;
1113 di = xzalloc(sizeof *di);
978427a5 1114 dpif_ipfix_bridge_exporter_init(&di->bridge_exporter);
29089a54 1115 hmap_init(&di->flow_exporter_map);
cd32509e 1116 hmap_init(&di->ports);
37bec3d3 1117 ovs_refcount_init(&di->ref_cnt);
d857c8aa
EJ
1118 return di;
1119}
1120
1121struct dpif_ipfix *
1122dpif_ipfix_ref(const struct dpif_ipfix *di_)
1123{
1124 struct dpif_ipfix *di = CONST_CAST(struct dpif_ipfix *, di_);
1125 if (di) {
37bec3d3 1126 ovs_refcount_ref(&di->ref_cnt);
d857c8aa 1127 }
29089a54
RL
1128 return di;
1129}
1130
1131uint32_t
1132dpif_ipfix_get_bridge_exporter_probability(const struct dpif_ipfix *di)
626ace7b 1133 OVS_EXCLUDED(mutex)
29089a54 1134{
626ace7b
EJ
1135 uint32_t ret;
1136 ovs_mutex_lock(&mutex);
1137 ret = di->bridge_exporter.probability;
1138 ovs_mutex_unlock(&mutex);
1139 return ret;
29089a54
RL
1140}
1141
8b7ea2d4
WZ
1142bool
1143dpif_ipfix_get_bridge_exporter_input_sampling(const struct dpif_ipfix *di)
1144 OVS_EXCLUDED(mutex)
1145{
f7a8b4d4 1146 bool ret = false;
8b7ea2d4
WZ
1147 ovs_mutex_lock(&mutex);
1148 if (di->bridge_exporter.options) {
1149 ret = di->bridge_exporter.options->enable_input_sampling;
1150 }
1151 ovs_mutex_unlock(&mutex);
1152 return ret;
1153}
1154
1155bool
1156dpif_ipfix_get_bridge_exporter_output_sampling(const struct dpif_ipfix *di)
1157 OVS_EXCLUDED(mutex)
1158{
f7a8b4d4 1159 bool ret = false;
8b7ea2d4
WZ
1160 ovs_mutex_lock(&mutex);
1161 if (di->bridge_exporter.options) {
1162 ret = di->bridge_exporter.options->enable_output_sampling;
1163 }
1164 ovs_mutex_unlock(&mutex);
1165 return ret;
1166}
1167
1168bool
1169dpif_ipfix_get_bridge_exporter_tunnel_sampling(const struct dpif_ipfix *di)
1170 OVS_EXCLUDED(mutex)
1171{
1172 bool ret = false;
1173 ovs_mutex_lock(&mutex);
1174 if (di->bridge_exporter.options) {
1175 ret = di->bridge_exporter.options->enable_tunnel_sampling;
1176 }
1177 ovs_mutex_unlock(&mutex);
1178 return ret;
1179}
1180
f69f713b
BY
1181bool
1182dpif_ipfix_get_flow_exporter_tunnel_sampling(const struct dpif_ipfix *di,
1183 const uint32_t collector_set_id)
1184 OVS_EXCLUDED(mutex)
1185{
1186 ovs_mutex_lock(&mutex);
1187 struct dpif_ipfix_flow_exporter_map_node *node
1188 = dpif_ipfix_find_flow_exporter_map_node(di, collector_set_id);
1189 bool ret = (node
1190 && node->exporter.options
1191 && node->exporter.options->enable_tunnel_sampling);
1192 ovs_mutex_unlock(&mutex);
1193
1194 return ret;
1195}
1196
29089a54 1197static void
bd3950dd 1198dpif_ipfix_clear(struct dpif_ipfix *di) OVS_REQUIRES(mutex)
29089a54 1199{
4ec3d7c7 1200 struct dpif_ipfix_flow_exporter_map_node *exp_node;
8b7ea2d4 1201 struct dpif_ipfix_port *dip, *next;
29089a54
RL
1202
1203 dpif_ipfix_bridge_exporter_clear(&di->bridge_exporter);
1204
4ec3d7c7 1205 HMAP_FOR_EACH_POP (exp_node, node, &di->flow_exporter_map) {
978427a5
RL
1206 dpif_ipfix_flow_exporter_destroy(&exp_node->exporter);
1207 free(exp_node);
29089a54 1208 }
8b7ea2d4 1209
cd32509e
MW
1210 HMAP_FOR_EACH_SAFE (dip, next, hmap_node, &di->ports) {
1211 dpif_ipfix_del_port__(di, dip);
8b7ea2d4 1212 }
29089a54
RL
1213}
1214
1215void
626ace7b 1216dpif_ipfix_unref(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
29089a54 1217{
24f83812 1218 if (di && ovs_refcount_unref_relaxed(&di->ref_cnt) == 1) {
626ace7b 1219 ovs_mutex_lock(&mutex);
29089a54 1220 dpif_ipfix_clear(di);
978427a5 1221 dpif_ipfix_bridge_exporter_destroy(&di->bridge_exporter);
29089a54 1222 hmap_destroy(&di->flow_exporter_map);
cd32509e 1223 hmap_destroy(&di->ports);
29089a54 1224 free(di);
626ace7b 1225 ovs_mutex_unlock(&mutex);
29089a54
RL
1226 }
1227}
1228
1229static void
978427a5 1230ipfix_init_header(uint32_t export_time_sec, uint32_t seq_number,
cf62fa4c 1231 uint32_t obs_domain_id, struct dp_packet *msg)
29089a54
RL
1232{
1233 struct ipfix_header *hdr;
1234
cf62fa4c 1235 hdr = dp_packet_put_zeros(msg, sizeof *hdr);
29089a54
RL
1236 hdr->version = htons(IPFIX_VERSION);
1237 hdr->length = htons(sizeof *hdr); /* Updated in ipfix_send_msg. */
978427a5 1238 hdr->export_time = htonl(export_time_sec);
29089a54
RL
1239 hdr->seq_number = htonl(seq_number);
1240 hdr->obs_domain_id = htonl(obs_domain_id);
1241}
1242
fb8f22c1 1243static size_t
cf62fa4c 1244ipfix_send_msg(const struct collectors *collectors, struct dp_packet *msg)
29089a54
RL
1245{
1246 struct ipfix_header *hdr;
fb8f22c1 1247 size_t tx_errors;
29089a54
RL
1248
1249 /* Adjust the length in the header. */
cf62fa4c
PS
1250 hdr = dp_packet_data(msg);
1251 hdr->length = htons(dp_packet_size(msg));
29089a54 1252
fb8f22c1
BY
1253 tx_errors = collectors_send(collectors,
1254 dp_packet_data(msg), dp_packet_size(msg));
cf62fa4c 1255 dp_packet_set_size(msg, 0);
fb8f22c1
BY
1256
1257 return tx_errors;
29089a54
RL
1258}
1259
1260static uint16_t
1261ipfix_get_template_id(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
cd32509e
MW
1262 enum ipfix_proto_l4 l4, enum ipfix_proto_tunnel tunnel,
1263 enum ipfix_flow_direction flow_direction)
29089a54
RL
1264{
1265 uint16_t template_id;
1266 template_id = l2;
1267 template_id = template_id * NUM_IPFIX_PROTO_L3 + l3;
1268 template_id = template_id * NUM_IPFIX_PROTO_L4 + l4;
8b7ea2d4 1269 template_id = template_id * NUM_IPFIX_PROTO_TUNNEL + tunnel;
cd32509e 1270 template_id = template_id * NUM_IPFIX_FLOW_DIRECTION + flow_direction;
29089a54
RL
1271 return IPFIX_TEMPLATE_ID_MIN + template_id;
1272}
1273
d564c4bb
PS
1274static uint16_t
1275ipfix_get_options_template_id(enum ipfix_options_template opt_tmpl_type)
1276{
1277 /* Check what is the maximum possible Template ID for Template Record and
1278 * use it as a base number for Template ID in Options Template Record. */
1279 uint16_t max_tmpl_id = ipfix_get_template_id(NUM_IPFIX_PROTO_L2,
1280 NUM_IPFIX_PROTO_L3,
1281 NUM_IPFIX_PROTO_L4,
cd32509e
MW
1282 NUM_IPFIX_PROTO_TUNNEL,
1283 NUM_IPFIX_FLOW_DIRECTION);
d564c4bb
PS
1284
1285 return max_tmpl_id + opt_tmpl_type;
1286}
1287
1288
29089a54
RL
1289static void
1290ipfix_define_template_entity(enum ipfix_entity_id id,
8b7ea2d4
WZ
1291 enum ipfix_entity_size size,
1292 enum ipfix_entity_enterprise enterprise,
cf62fa4c 1293 struct dp_packet *msg)
29089a54
RL
1294{
1295 struct ipfix_template_field_specifier *field;
8b7ea2d4 1296 size_t field_size;
29089a54 1297
8b7ea2d4
WZ
1298 if (enterprise) {
1299 field_size = sizeof *field;
1300 } else {
1301 /* No enterprise number */
1302 field_size = sizeof *field - sizeof(ovs_be32);
1303 }
cf62fa4c 1304 field = dp_packet_put_zeros(msg, field_size);
29089a54 1305 field->element_id = htons(id);
8b7ea2d4
WZ
1306 if (size) {
1307 field->field_length = htons(size);
1308 } else {
1309 /* RFC 5101, Section 7. Variable-Length Information Element */
1310 field->field_length = OVS_BE16_MAX;
1311 }
1312 if (enterprise) {
1313 field->enterprise = htonl(enterprise);
1314 }
1315
29089a54
RL
1316}
1317
d564c4bb
PS
1318#define DEF(ID) \
1319{ \
1320 ipfix_define_template_entity(IPFIX_ENTITY_ID_##ID, \
1321 IPFIX_ENTITY_SIZE_##ID, \
1322 IPFIX_ENTITY_ENTERPRISE_##ID, msg); \
1323 count++; \
1324}
1325
1326/* Defines The Exporting Process Reliability Statistics Options Template
1327 * fields, including scope fields. Updates 'scope_field_count' and
1328 * 'field_count' in Options Template Record Header. */
1329static uint16_t
1330ipfix_def_exporter_options_template_fields(size_t opt_tmpl_hdr_offset,
1331 struct dp_packet *msg)
1332{
1333 uint16_t count = 0;
1334 struct ipfix_options_template_record_header *opt_tmpl_hdr;
1335
1336 /* 1. Scope Fields Specifiers */
1337 DEF(EXPORTING_PROCESS_ID);
1338
1339 /* Update 'scope_field_count' in options template header. */
1340 opt_tmpl_hdr = (struct ipfix_options_template_record_header *)
1341 ((uint8_t *)dp_packet_data(msg) + opt_tmpl_hdr_offset);
1342 opt_tmpl_hdr->scope_field_count = htons(count);
1343
1344 /* 2. Fields Specifiers */
1345 DEF(NOT_SENT_PACKET_TOTAL_COUNT);
1346
1347 /* Update 'field_count' in options template header. */
09a21284 1348 opt_tmpl_hdr = (struct ipfix_options_template_record_header *)
d564c4bb
PS
1349 ((uint8_t *)dp_packet_data(msg) + opt_tmpl_hdr_offset);
1350 opt_tmpl_hdr->field_count = htons(count);
1351
1352 return count;
1353}
1354
1355static uint16_t
1356ipfix_def_options_template_fields(enum ipfix_options_template opt_tmpl_type,
1357 size_t opt_tmpl_hdr_offset,
1358 struct dp_packet *msg)
1359{
1360 switch (opt_tmpl_type) {
1361 case IPFIX_OPTIONS_TEMPLATE_EXPORTER_STATS:
1362 return ipfix_def_exporter_options_template_fields(opt_tmpl_hdr_offset,
1363 msg);
1364 break;
1365 case NUM_IPFIX_OPTIONS_TEMPLATE:
1366 default:
1367 OVS_NOT_REACHED();
d564c4bb
PS
1368 }
1369
1370 return 0;
1371}
1372
1373/* Defines fields in Template Record. Updates 'field_count' in Template Record
1374 * Header. */
29089a54
RL
1375static uint16_t
1376ipfix_define_template_fields(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
8b7ea2d4 1377 enum ipfix_proto_l4 l4, enum ipfix_proto_tunnel tunnel,
cd32509e
MW
1378 enum ipfix_flow_direction flow_direction,
1379 bool virtual_obs_id_set,
1380 size_t tmpl_hdr_offset,
cf62fa4c 1381 struct dp_packet *msg)
29089a54 1382{
29089a54 1383
d564c4bb
PS
1384 struct ipfix_template_record_header *tmpl_hdr;
1385 uint16_t count = 0;
29089a54 1386
978427a5
RL
1387 /* 1. Flow key. */
1388
29089a54 1389 DEF(OBSERVATION_POINT_ID);
8b7ea2d4 1390 DEF(FLOW_DIRECTION);
29089a54
RL
1391
1392 /* Common Ethernet entities. */
1393 DEF(SOURCE_MAC_ADDRESS);
1394 DEF(DESTINATION_MAC_ADDRESS);
1395 DEF(ETHERNET_TYPE);
29089a54
RL
1396 DEF(ETHERNET_HEADER_LENGTH);
1397
cd32509e
MW
1398 /* Interface Information Elements */
1399 DEF(INGRESS_INTERFACE);
1400 DEF(INGRESS_INTERFACE_TYPE);
1401 DEF(INTERFACE_NAME);
1402 DEF(INTERFACE_DESCRIPTION);
1403
1404 if (flow_direction == EGRESS_FLOW) {
1405 DEF(EGRESS_INTERFACE);
1406 DEF(EGRESS_INTERFACE_TYPE);
1407 DEF(INTERFACE_NAME);
1408 DEF(INTERFACE_DESCRIPTION);
1409 }
1410
29089a54
RL
1411 if (l2 == IPFIX_PROTO_L2_VLAN) {
1412 DEF(VLAN_ID);
1413 DEF(DOT1Q_VLAN_ID);
1414 DEF(DOT1Q_PRIORITY);
1415 }
1416
1417 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
1418 DEF(IP_VERSION);
1419 DEF(IP_TTL);
1420 DEF(PROTOCOL_IDENTIFIER);
1421 DEF(IP_DIFF_SERV_CODE_POINT);
1422 DEF(IP_PRECEDENCE);
1423 DEF(IP_CLASS_OF_SERVICE);
1424
1425 if (l3 == IPFIX_PROTO_L3_IPV4) {
1426 DEF(SOURCE_IPV4_ADDRESS);
1427 DEF(DESTINATION_IPV4_ADDRESS);
0b4f83e2
PS
1428 if (l4 == IPFIX_PROTO_L4_TCP
1429 || l4 == IPFIX_PROTO_L4_UDP
1430 || l4 == IPFIX_PROTO_L4_SCTP) {
f51e8ccb
RL
1431 DEF(SOURCE_TRANSPORT_PORT);
1432 DEF(DESTINATION_TRANSPORT_PORT);
1433 } else if (l4 == IPFIX_PROTO_L4_ICMP) {
1434 DEF(ICMP_TYPE_IPV4);
1435 DEF(ICMP_CODE_IPV4);
1436 }
29089a54
RL
1437 } else { /* l3 == IPFIX_PROTO_L3_IPV6 */
1438 DEF(SOURCE_IPV6_ADDRESS);
1439 DEF(DESTINATION_IPV6_ADDRESS);
1440 DEF(FLOW_LABEL_IPV6);
0b4f83e2
PS
1441 if (l4 == IPFIX_PROTO_L4_TCP
1442 || l4 == IPFIX_PROTO_L4_UDP
1443 || l4 == IPFIX_PROTO_L4_SCTP) {
f51e8ccb
RL
1444 DEF(SOURCE_TRANSPORT_PORT);
1445 DEF(DESTINATION_TRANSPORT_PORT);
1446 } else if (l4 == IPFIX_PROTO_L4_ICMP) {
1447 DEF(ICMP_TYPE_IPV6);
1448 DEF(ICMP_CODE_IPV6);
1449 }
29089a54
RL
1450 }
1451 }
1452
8b7ea2d4
WZ
1453 if (tunnel != IPFIX_PROTO_NOT_TUNNELED) {
1454 DEF(TUNNEL_SOURCE_IPV4_ADDRESS);
1455 DEF(TUNNEL_DESTINATION_IPV4_ADDRESS);
1456 DEF(TUNNEL_PROTOCOL_IDENTIFIER);
1457 DEF(TUNNEL_SOURCE_TRANSPORT_PORT);
1458 DEF(TUNNEL_DESTINATION_TRANSPORT_PORT);
1459 DEF(TUNNEL_TYPE);
1460 DEF(TUNNEL_KEY);
1461 }
1462
c97320eb
WZ
1463 /* 2. Virtual observation ID, which is not a part of flow key. */
1464 if (virtual_obs_id_set) {
1465 DEF(VIRTUAL_OBS_ID);
1466 }
1467
1468 /* 3. Flow aggregated data. */
978427a5
RL
1469
1470 DEF(FLOW_START_DELTA_MICROSECONDS);
1471 DEF(FLOW_END_DELTA_MICROSECONDS);
564230b6
PS
1472 DEF(DROPPED_PACKET_DELTA_COUNT);
1473 DEF(DROPPED_PACKET_TOTAL_COUNT);
978427a5 1474 DEF(PACKET_DELTA_COUNT);
fe520682 1475 DEF(PACKET_TOTAL_COUNT);
ebfe9a21
PS
1476 DEF(INGRESS_UNICAST_PACKET_TOTAL_COUNT);
1477 DEF(INGRESS_MULTICAST_PACKET_TOTAL_COUNT);
1478 DEF(INGRESS_BROADCAST_PACKET_TOTAL_COUNT);
1479 DEF(EGRESS_UNICAST_PACKET_TOTAL_COUNT);
1480 DEF(EGRESS_BROADCAST_PACKET_TOTAL_COUNT);
1481 DEF(POST_MCAST_PACKET_DELTA_COUNT);
1482 DEF(POST_MCAST_PACKET_TOTAL_COUNT);
978427a5 1483 DEF(LAYER2_OCTET_DELTA_COUNT);
fe520682 1484 DEF(LAYER2_OCTET_TOTAL_COUNT);
978427a5
RL
1485 DEF(FLOW_END_REASON);
1486
1487 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
564230b6
PS
1488 DEF(DROPPED_OCTET_DELTA_COUNT);
1489 DEF(DROPPED_OCTET_TOTAL_COUNT);
f51e8ccb 1490 DEF(OCTET_DELTA_COUNT);
fe520682 1491 DEF(OCTET_TOTAL_COUNT);
978427a5 1492 DEF(OCTET_DELTA_SUM_OF_SQUARES);
fe520682 1493 DEF(OCTET_TOTAL_SUM_OF_SQUARES);
978427a5
RL
1494 DEF(MINIMUM_IP_TOTAL_LENGTH);
1495 DEF(MAXIMUM_IP_TOTAL_LENGTH);
ebfe9a21
PS
1496 DEF(POST_MCAST_OCTET_DELTA_COUNT);
1497 DEF(POST_MCAST_OCTET_TOTAL_COUNT);
978427a5 1498 }
0b4f83e2
PS
1499
1500 if (l4 == IPFIX_PROTO_L4_TCP) {
1501 DEF(TCP_ACK_TOTAL_COUNT);
1502 DEF(TCP_FIN_TOTAL_COUNT);
1503 DEF(TCP_PSH_TOTAL_COUNT);
1504 DEF(TCP_RST_TOTAL_COUNT);
1505 DEF(TCP_SYN_TOTAL_COUNT);
1506 DEF(TCP_URG_TOTAL_COUNT);
1507 }
d564c4bb
PS
1508
1509 /* Update 'field_count' in template header. */
1510 tmpl_hdr = (struct ipfix_template_record_header *)
1511 ((uint8_t *)dp_packet_data(msg) + tmpl_hdr_offset);
1512 tmpl_hdr->field_count = htons(count);
29089a54
RL
1513
1514 return count;
1515}
1516
d564c4bb
PS
1517#undef DEF
1518
29089a54 1519static void
642c824b 1520ipfix_init_template_msg(uint32_t export_time_sec,
8b7ea2d4 1521 uint32_t seq_number, uint32_t obs_domain_id,
d564c4bb
PS
1522 uint16_t set_id, struct dp_packet *msg,
1523 size_t *set_hdr_offset)
8b7ea2d4
WZ
1524{
1525 struct ipfix_set_header *set_hdr;
1526
642c824b 1527 dp_packet_clear(msg);
8b7ea2d4
WZ
1528
1529 ipfix_init_header(export_time_sec, seq_number, obs_domain_id, msg);
cf62fa4c 1530 *set_hdr_offset = dp_packet_size(msg);
8b7ea2d4 1531
d564c4bb 1532 /* Add a Set Header. */
cf62fa4c 1533 set_hdr = dp_packet_put_zeros(msg, sizeof *set_hdr);
d564c4bb 1534 set_hdr->set_id = htons(set_id);
8b7ea2d4
WZ
1535}
1536
fb8f22c1 1537static size_t
8b7ea2d4 1538ipfix_send_template_msg(const struct collectors *collectors,
cf62fa4c 1539 struct dp_packet *msg, size_t set_hdr_offset)
8b7ea2d4
WZ
1540{
1541 struct ipfix_set_header *set_hdr;
fb8f22c1 1542 size_t tx_errors;
8b7ea2d4
WZ
1543
1544 /* Send template message. */
1545 set_hdr = (struct ipfix_set_header*)
cf62fa4c
PS
1546 ((uint8_t*)dp_packet_data(msg) + set_hdr_offset);
1547 set_hdr->length = htons(dp_packet_size(msg) - set_hdr_offset);
8b7ea2d4 1548
fb8f22c1 1549 tx_errors = ipfix_send_msg(collectors, msg);
8b7ea2d4 1550
fb8f22c1 1551 return tx_errors;
8b7ea2d4
WZ
1552}
1553
d564c4bb
PS
1554static void
1555ipfix_add_options_template_record(enum ipfix_options_template opt_tmpl_type,
1556 struct dp_packet *msg)
1557{
1558 struct ipfix_options_template_record_header *opt_tmpl_hdr;
1559 size_t opt_tmpl_hdr_offset;
1560
1561 opt_tmpl_hdr_offset = dp_packet_size(msg);
1562 opt_tmpl_hdr = dp_packet_put_zeros(msg, sizeof *opt_tmpl_hdr);
1563 opt_tmpl_hdr->template_id =
1564 htons(ipfix_get_options_template_id(opt_tmpl_type));
1565 ipfix_def_options_template_fields(opt_tmpl_type, opt_tmpl_hdr_offset, msg);
1566}
1567
1568static void
1569ipfix_send_options_template_msgs(struct dpif_ipfix_exporter *exporter,
1570 uint32_t export_time_sec,
1571 uint32_t obs_domain_id,
1572 struct dp_packet *msg)
1573{
1574 size_t set_hdr_offset;
1575 size_t tx_packets = 0;
1576 size_t tx_errors = 0, error_pkts;
1577 enum ipfix_options_template opt_tmpl_type;
1578
1579 ipfix_init_template_msg(export_time_sec, exporter->seq_number,
1580 obs_domain_id, IPFIX_SET_ID_OPTION_TEMPLATE, msg,
1581 &set_hdr_offset);
1582
1583 for (opt_tmpl_type = 0; opt_tmpl_type < NUM_IPFIX_OPTIONS_TEMPLATE;
1584 ++opt_tmpl_type) {
1585 if (dp_packet_size(msg) >= MAX_MESSAGE_LEN) {
1586 /* Send template message. */
1587 error_pkts = ipfix_send_template_msg(exporter->collectors, msg,
1588 set_hdr_offset);
1589 tx_errors += error_pkts;
1590 tx_packets += collectors_count(exporter->collectors) - error_pkts;
1591
1592 /* Reinitialize the template msg. */
1593 ipfix_init_template_msg(export_time_sec, exporter->seq_number,
1594 obs_domain_id,
1595 IPFIX_SET_ID_OPTION_TEMPLATE,
1596 msg,
1597 &set_hdr_offset);
1598 }
1599
1600 ipfix_add_options_template_record(opt_tmpl_type, msg);
1601 }
1602
1603 error_pkts = ipfix_send_template_msg(exporter->collectors, msg,
1604 set_hdr_offset);
1605 tx_errors += error_pkts;
1606 tx_packets += collectors_count(exporter->collectors) - error_pkts;
1607 exporter->ofproto_stats.tx_pkts += tx_packets;
1608 exporter->ofproto_stats.tx_errors += tx_errors;
1609}
1610
cd32509e
MW
1611static void
1612ipfix_add_template_record(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
1613 enum ipfix_proto_l4 l4,
1614 enum ipfix_proto_tunnel tunnel,
1615 enum ipfix_flow_direction flow_direction,
1616 bool virtual_obs_id_set,
1617 struct dp_packet *msg)
1618{
1619 struct ipfix_template_record_header *tmpl_hdr;
1620 size_t tmpl_hdr_offset = dp_packet_size(msg);
1621
1622 tmpl_hdr = dp_packet_put_zeros(msg, sizeof *tmpl_hdr);
1623 tmpl_hdr->template_id =
1624 htons(ipfix_get_template_id(l2, l3, l4, tunnel, flow_direction));
1625 ipfix_define_template_fields(l2, l3, l4, tunnel, flow_direction,
1626 virtual_obs_id_set, tmpl_hdr_offset, msg);
1627}
1628
8b7ea2d4
WZ
1629static void
1630ipfix_send_template_msgs(struct dpif_ipfix_exporter *exporter,
1631 uint32_t export_time_sec, uint32_t obs_domain_id)
29089a54 1632{
978427a5 1633 uint64_t msg_stub[DIV_ROUND_UP(MAX_MESSAGE_LEN, 8)];
cf62fa4c 1634 struct dp_packet msg;
642c824b
BP
1635 dp_packet_use_stub(&msg, msg_stub, sizeof msg_stub);
1636
cd32509e 1637 size_t set_hdr_offset, error_pkts;
fb8f22c1
BY
1638 size_t tx_packets = 0;
1639 size_t tx_errors = 0;
29089a54
RL
1640 enum ipfix_proto_l2 l2;
1641 enum ipfix_proto_l3 l3;
1642 enum ipfix_proto_l4 l4;
8b7ea2d4 1643 enum ipfix_proto_tunnel tunnel;
cd32509e 1644 enum ipfix_flow_direction flow_direction;
29089a54 1645
642c824b 1646 ipfix_init_template_msg(export_time_sec, exporter->seq_number,
d564c4bb
PS
1647 obs_domain_id, IPFIX_SET_ID_TEMPLATE, &msg,
1648 &set_hdr_offset);
29089a54
RL
1649 /* Define one template for each possible combination of
1650 * protocols. */
1651 for (l2 = 0; l2 < NUM_IPFIX_PROTO_L2; l2++) {
1652 for (l3 = 0; l3 < NUM_IPFIX_PROTO_L3; l3++) {
1653 for (l4 = 0; l4 < NUM_IPFIX_PROTO_L4; l4++) {
1654 if (l3 == IPFIX_PROTO_L3_UNKNOWN &&
1655 l4 != IPFIX_PROTO_L4_UNKNOWN) {
1656 continue;
1657 }
8b7ea2d4 1658 for (tunnel = 0; tunnel < NUM_IPFIX_PROTO_TUNNEL; tunnel++) {
cd32509e
MW
1659 for (flow_direction = 0;
1660 flow_direction < NUM_IPFIX_FLOW_DIRECTION;
1661 flow_direction++) {
1662 /* When the size of the template packet reaches
1663 * MAX_MESSAGE_LEN(1024), send it out.
1664 * And then reinitialize the msg to construct a new
1665 * packet for the following templates.
1666 */
1667 if (dp_packet_size(&msg) >= MAX_MESSAGE_LEN) {
1668 /* Send template message. */
1669 error_pkts =
1670 ipfix_send_template_msg(exporter->collectors,
1671 &msg, set_hdr_offset);
1672 tx_errors += error_pkts;
1673 tx_packets +=
1674 collectors_count(exporter->collectors)
1675 - error_pkts;
1676
1677 /* Reinitialize the template msg. */
1678 ipfix_init_template_msg(export_time_sec,
1679 exporter->seq_number,
1680 obs_domain_id,
1681 IPFIX_SET_ID_TEMPLATE,
1682 &msg, &set_hdr_offset);
1683 }
1684
1685 ipfix_add_template_record(l2, l3, l4, tunnel,
1686 flow_direction,
1687 exporter->virtual_obs_id != NULL, &msg);
8b7ea2d4 1688 }
8b7ea2d4 1689 }
29089a54
RL
1690 }
1691 }
1692 }
1693
8b7ea2d4 1694 /* Send template message. */
cd32509e
MW
1695 error_pkts = ipfix_send_template_msg(exporter->collectors, &msg,
1696 set_hdr_offset);
fb8f22c1
BY
1697 tx_errors += error_pkts;
1698 tx_packets += collectors_count(exporter->collectors) - error_pkts;
1699
fe520682 1700 exporter->ofproto_stats.tx_pkts += tx_packets;
1701 exporter->ofproto_stats.tx_errors += tx_errors;
29089a54 1702
fbd683d0 1703 /* XXX: Add Options Template Sets, at least to define a Flow Keys
29089a54
RL
1704 * Option Template. */
1705
d564c4bb
PS
1706 /* At the moment only a single Options Template Set is used, which contains
1707 * Exporting Process Statistics. It means that there is no specific
1708 * Observation Domain ID relevant for the entire IPFIX message and it
1709 * should be set to 0. */
1710 ipfix_send_options_template_msgs(exporter, export_time_sec, 0U, &msg);
1711
642c824b 1712 dp_packet_uninit(&msg);
29089a54
RL
1713}
1714
978427a5
RL
1715static inline uint32_t
1716ipfix_hash_flow_key(const struct ipfix_flow_key *flow_key, uint32_t basis)
1717{
1718 uint32_t hash;
1719 hash = hash_int(flow_key->obs_domain_id, basis);
1720 hash = hash_int(flow_key->template_id, hash);
1721 hash = hash_bytes(flow_key->flow_key_msg_part,
1722 flow_key->flow_key_msg_part_size, hash);
1723 return hash;
1724}
1725
1726static bool
1727ipfix_flow_key_equal(const struct ipfix_flow_key *a,
1728 const struct ipfix_flow_key *b)
1729{
1730 /* The template ID determines the flow key size, so not need to
1731 * compare it. */
1732 return (a->obs_domain_id == b->obs_domain_id
1733 && a->template_id == b->template_id
1734 && memcmp(a->flow_key_msg_part, b->flow_key_msg_part,
1735 a->flow_key_msg_part_size) == 0);
1736}
1737
1738static struct ipfix_flow_cache_entry*
1739ipfix_cache_find_entry(const struct dpif_ipfix_exporter *exporter,
1740 const struct ipfix_flow_key *flow_key)
1741{
1742 struct ipfix_flow_cache_entry *entry;
1743
1744 HMAP_FOR_EACH_WITH_HASH (entry, flow_key_map_node,
1745 ipfix_hash_flow_key(flow_key, 0),
1746 &exporter->cache_flow_key_map) {
1747 if (ipfix_flow_key_equal(&entry->flow_key, flow_key)) {
1748 return entry;
1749 }
1750 }
1751
1752 return NULL;
1753}
1754
1755static bool
1756ipfix_cache_next_timeout_msec(const struct dpif_ipfix_exporter *exporter,
1757 long long int *next_timeout_msec)
1758{
1759 struct ipfix_flow_cache_entry *entry;
1760
1761 LIST_FOR_EACH (entry, cache_flow_start_timestamp_list_node,
1762 &exporter->cache_flow_start_timestamp_list) {
1763 *next_timeout_msec = entry->flow_start_timestamp_usec / 1000LL
1764 + 1000LL * exporter->cache_active_timeout;
1765 return true;
1766 }
1767
1768 return false;
1769}
1770
1771static void
1772ipfix_cache_aggregate_entries(struct ipfix_flow_cache_entry *from_entry,
1773 struct ipfix_flow_cache_entry *to_entry)
1774{
1775 uint64_t *to_start, *to_end, *from_start, *from_end;
1776 uint16_t *to_min_len, *to_max_len, *from_min_len, *from_max_len;
1777
1778 to_start = &to_entry->flow_start_timestamp_usec;
1779 to_end = &to_entry->flow_end_timestamp_usec;
1780 from_start = &from_entry->flow_start_timestamp_usec;
1781 from_end = &from_entry->flow_end_timestamp_usec;
1782
1783 if (*to_start > *from_start) {
1784 *to_start = *from_start;
1785 }
1786 if (*to_end < *from_end) {
1787 *to_end = *from_end;
1788 }
1789
564230b6
PS
1790
1791 to_entry->dropped_packet_delta_count +=
1792 from_entry->dropped_packet_delta_count;
978427a5
RL
1793 to_entry->packet_delta_count += from_entry->packet_delta_count;
1794 to_entry->layer2_octet_delta_count += from_entry->layer2_octet_delta_count;
1795
564230b6
PS
1796 to_entry->dropped_packet_total_count =
1797 from_entry->dropped_packet_total_count;
fe520682 1798 to_entry->packet_total_count = from_entry->packet_total_count;
ebfe9a21
PS
1799 to_entry->in_ucast_packet_total_count =
1800 from_entry->in_ucast_packet_total_count;
1801 to_entry->in_mcast_packet_total_count =
1802 from_entry->in_mcast_packet_total_count;
1803 to_entry->in_bcast_packet_total_count =
1804 from_entry->in_bcast_packet_total_count;
1805 to_entry->out_ucast_packet_total_count =
1806 from_entry->out_ucast_packet_total_count;
1807 to_entry->out_bcast_packet_total_count =
1808 from_entry->out_bcast_packet_total_count;
fe520682 1809 to_entry->layer2_octet_total_count = from_entry->layer2_octet_total_count;
ebfe9a21
PS
1810 to_entry->post_mcast_packet_delta_count +=
1811 from_entry->post_mcast_packet_delta_count;
1812 to_entry->post_mcast_octet_delta_count +=
1813 from_entry->post_mcast_octet_delta_count;
fe520682 1814
564230b6
PS
1815 to_entry->dropped_octet_delta_count +=
1816 from_entry->dropped_octet_delta_count;
f51e8ccb 1817 to_entry->octet_delta_count += from_entry->octet_delta_count;
978427a5
RL
1818 to_entry->octet_delta_sum_of_squares +=
1819 from_entry->octet_delta_sum_of_squares;
1820
564230b6
PS
1821 to_entry->dropped_octet_total_count =
1822 from_entry->dropped_octet_total_count;
fe520682 1823 to_entry->octet_total_count = from_entry->octet_total_count;
1824 to_entry->octet_total_sum_of_squares =
1825 from_entry->octet_total_sum_of_squares;
1826
ebfe9a21
PS
1827 to_entry->post_mcast_packet_total_count =
1828 from_entry->post_mcast_packet_total_count;
1829 to_entry->post_mcast_octet_total_count =
1830 from_entry->post_mcast_octet_total_count;
1831
978427a5
RL
1832 to_min_len = &to_entry->minimum_ip_total_length;
1833 to_max_len = &to_entry->maximum_ip_total_length;
1834 from_min_len = &from_entry->minimum_ip_total_length;
1835 from_max_len = &from_entry->maximum_ip_total_length;
1836
1837 if (!*to_min_len || (*from_min_len && *to_min_len > *from_min_len)) {
1838 *to_min_len = *from_min_len;
1839 }
1840 if (*to_max_len < *from_max_len) {
1841 *to_max_len = *from_max_len;
1842 }
0b4f83e2
PS
1843
1844 to_entry->tcp_packet_delta_count += from_entry->tcp_packet_delta_count;
1845 to_entry->tcp_ack_total_count = from_entry->tcp_ack_total_count;
1846 to_entry->tcp_fin_total_count = from_entry->tcp_fin_total_count;
1847 to_entry->tcp_psh_total_count = from_entry->tcp_psh_total_count;
1848 to_entry->tcp_rst_total_count = from_entry->tcp_rst_total_count;
1849 to_entry->tcp_syn_total_count = from_entry->tcp_syn_total_count;
1850 to_entry->tcp_urg_total_count = from_entry->tcp_urg_total_count;
978427a5
RL
1851}
1852
fb8f22c1
BY
1853/* Get statistics */
1854static void
1855ipfix_get_stats__(const struct dpif_ipfix_exporter *exporter,
1856 ofproto_ipfix_stats *stats)
1857{
1858 memset(stats, 0xff, sizeof *stats);
1859
1860 if (!exporter) {
1861 return;
1862 }
1863
fe520682 1864 *stats = exporter->ofproto_stats;
fb8f22c1
BY
1865}
1866
1867static void
1868ipfix_get_bridge_stats(const struct dpif_ipfix_bridge_exporter *exporter,
1869 ofproto_ipfix_stats *stats)
1870{
1871 ipfix_get_stats__(&exporter->exporter, stats);
1872}
1873
1874static void
1875ipfix_get_flow_stats(const struct dpif_ipfix_flow_exporter *exporter,
1876 ofproto_ipfix_stats *stats)
1877{
1878 ipfix_get_stats__(&exporter->exporter, stats);
1879 stats->collector_set_id = exporter->options->collector_set_id;
1880}
1881
1882int
1883dpif_ipfix_get_stats(const struct dpif_ipfix *di,
1884 bool bridge_ipfix,
1885 struct ovs_list *replies)
1886 OVS_EXCLUDED(mutex)
1887{
1888 struct dpif_ipfix_flow_exporter_map_node *flow_exporter_node;
1889 struct ofputil_ipfix_stats ois;
1890
1891 ovs_mutex_lock(&mutex);
1892 if (bridge_ipfix) {
1893 if (!di->bridge_exporter.options) {
1894 ovs_mutex_unlock(&mutex);
1895 return OFPERR_NXST_NOT_CONFIGURED;
1896 }
1897
1898 ipfix_get_bridge_stats(&di->bridge_exporter, &ois);
1899 ofputil_append_ipfix_stat(replies, &ois);
1900 } else {
1901 if (hmap_count(&di->flow_exporter_map) == 0) {
1902 ovs_mutex_unlock(&mutex);
1903 return OFPERR_NXST_NOT_CONFIGURED;
1904 }
1905
1906 HMAP_FOR_EACH (flow_exporter_node, node,
1907 &di->flow_exporter_map) {
1908 ipfix_get_flow_stats(&flow_exporter_node->exporter, &ois);
1909 ofputil_append_ipfix_stat(replies, &ois);
1910 }
1911 }
1912 ovs_mutex_unlock(&mutex);
1913
1914 return 0;
1915}
1916
1917/* Update partial ipfix stats */
1918static void
1919ipfix_update_stats(struct dpif_ipfix_exporter *exporter,
1920 bool new_flow,
1921 size_t current_flows,
1922 enum ipfix_sampled_packet_type sampled_pkt_type)
1923{
1924 if (new_flow) {
fe520682 1925 exporter->ofproto_stats.total_flows++;
1926 exporter->ofproto_stats.current_flows = current_flows;
fb8f22c1 1927 }
fe520682 1928 exporter->ofproto_stats.pkts++;
fb8f22c1
BY
1929
1930 switch (sampled_pkt_type) {
1931 case IPFIX_SAMPLED_PKT_IPV4_OK:
fe520682 1932 exporter->ofproto_stats.ipv4_pkts++;
fb8f22c1
BY
1933 break;
1934 case IPFIX_SAMPLED_PKT_IPV6_OK:
fe520682 1935 exporter->ofproto_stats.ipv6_pkts++;
fb8f22c1
BY
1936 break;
1937 case IPFIX_SAMPLED_PKT_IPV4_ERROR:
fe520682 1938 exporter->ofproto_stats.ipv4_error_pkts++;
1939 exporter->ofproto_stats.error_pkts++;
fb8f22c1
BY
1940 break;
1941 case IPFIX_SAMPLED_PKT_IPV6_ERROR:
fe520682 1942 exporter->ofproto_stats.ipv6_error_pkts++;
1943 exporter->ofproto_stats.error_pkts++;
fb8f22c1
BY
1944 break;
1945 case IPFIX_SAMPLED_PKT_UNKNOWN:
fe520682 1946 exporter->ofproto_stats.error_pkts++;
fb8f22c1
BY
1947 break;
1948 case IPFIX_SAMPLED_PKT_OTHERS:
1949 default:
1950 break;
1951 }
1952}
1953
f88ee3a5
BP
1954/* Returns the current time in the form used by IPFIX (microseconds since the
1955 * epoch). */
1956static uint64_t
1957ipfix_now(void)
1958{
1959 return time_wall_msec() * 1000ULL;
1960}
1961
978427a5
RL
1962/* Add an entry into a flow cache. The entry is either aggregated into
1963 * an existing entry with the same flow key and free()d, or it is
fb8f22c1 1964 * inserted into the cache. And IPFIX stats will be updated */
978427a5
RL
1965static void
1966ipfix_cache_update(struct dpif_ipfix_exporter *exporter,
fb8f22c1
BY
1967 struct ipfix_flow_cache_entry *entry,
1968 enum ipfix_sampled_packet_type sampled_pkt_type)
978427a5
RL
1969{
1970 struct ipfix_flow_cache_entry *old_entry;
fb8f22c1 1971 size_t current_flows = 0;
978427a5
RL
1972
1973 old_entry = ipfix_cache_find_entry(exporter, &entry->flow_key);
1974
1975 if (old_entry == NULL) {
1976 hmap_insert(&exporter->cache_flow_key_map, &entry->flow_key_map_node,
1977 ipfix_hash_flow_key(&entry->flow_key, 0));
1978
1979 /* As the latest entry added into the cache, it should
1980 * logically have the highest flow_start_timestamp_usec, so
1981 * append it at the tail. */
417e7e66 1982 ovs_list_push_back(&exporter->cache_flow_start_timestamp_list,
978427a5
RL
1983 &entry->cache_flow_start_timestamp_list_node);
1984
1985 /* Enforce exporter->cache_max_flows limit. */
fb8f22c1
BY
1986 current_flows = hmap_count(&exporter->cache_flow_key_map);
1987 ipfix_update_stats(exporter, true, current_flows, sampled_pkt_type);
1988 if (current_flows > exporter->cache_max_flows) {
978427a5
RL
1989 dpif_ipfix_cache_expire_now(exporter, false);
1990 }
1991 } else {
1992 ipfix_cache_aggregate_entries(entry, old_entry);
1993 free(entry);
fb8f22c1 1994 ipfix_update_stats(exporter, false, current_flows, sampled_pkt_type);
978427a5
RL
1995 }
1996}
1997
cd32509e
MW
1998static void
1999ipfix_destroy_iface_data_record(struct ipfix_data_record_flow_key_iface *data)
2000{
2001 free(data->if_descr);
2002 free(data->if_name);
2003}
2004
2005/* Fills '*data' structure based on port number 'port_no'. Caller must destroy
2006 * 'data' with ipfix_destroy_iface_data_record(). */
2007static int
2008ipfix_get_iface_data_record(const struct dpif_ipfix *di, odp_port_t port_no,
2009 struct ipfix_data_record_flow_key_iface *data)
2010 OVS_REQUIRES(mutex)
2011{
2012 struct dpif_ipfix_port *port;
2013 struct smap netdev_status;
2014
2015 port = dpif_ipfix_find_port(di, port_no);
2016 if (!port) {
2017 return -1;
2018 }
2019
2020 smap_init(&netdev_status);
2021 if (!netdev_get_status(port->ofport->netdev, &netdev_status)) {
2022 data->if_type = htonl(smap_get_ullong(&netdev_status, "if_type", 0));
2023 data->if_descr = nullable_xstrdup(smap_get(&netdev_status,
2024 "if_descr"));
2025 } else {
2026 data->if_type = 0;
2027 data->if_descr = NULL;
2028 }
2029
2030 smap_destroy(&netdev_status);
2031 data->if_index = htonl(port->ifindex);
2032 data->if_descr_len = data->if_descr ? strlen(data->if_descr) : 0;
2033 data->if_name = nullable_xstrdup(netdev_get_name(port->ofport->netdev));
2034 data->if_name_len = data->if_name ? strlen(data->if_name) : 0;
2035
2036 return 0;
2037}
2038
2039static void
2040ipfix_put_iface_data_record(const struct dpif_ipfix *di, odp_port_t port_no,
2041 struct dp_packet *msg)
2042 OVS_REQUIRES(mutex)
2043{
2044 struct ipfix_data_record_flow_key_iface data;
2045 int err;
2046
2047 memset(&data, 0, sizeof(struct ipfix_data_record_flow_key_iface));
2048 err = ipfix_get_iface_data_record(di, port_no, &data);
2049 if (err == 0) {
2050 dp_packet_put(msg, &data.if_index, sizeof data.if_index);
2051 dp_packet_put(msg, &data.if_type, sizeof data.if_type);
2052 dp_packet_put(msg, &data.if_name_len, sizeof data.if_name_len);
2053 if (data.if_name_len) {
2054 dp_packet_put(msg, data.if_name, data.if_name_len);
2055 }
2056 dp_packet_put(msg, &data.if_descr_len, sizeof data.if_descr_len);
2057 if (data.if_descr_len) {
2058 dp_packet_put(msg, data.if_descr, data.if_descr_len);
2059 }
2060 ipfix_destroy_iface_data_record(&data);
2061 } else {
2062 dp_packet_put_zeros(msg, sizeof data.if_index);
2063 dp_packet_put_zeros(msg, sizeof data.if_type);
2064 dp_packet_put_zeros(msg, sizeof data.if_name_len);
2065 dp_packet_put_zeros(msg, sizeof data.if_descr_len);
2066 }
2067}
2068
fb8f22c1 2069static enum ipfix_sampled_packet_type
cd32509e
MW
2070ipfix_cache_entry_init(const struct dpif_ipfix *di,
2071 struct ipfix_flow_cache_entry *entry,
cf62fa4c 2072 const struct dp_packet *packet, const struct flow *flow,
978427a5 2073 uint64_t packet_delta_count, uint32_t obs_domain_id,
8b7ea2d4 2074 uint32_t obs_point_id, odp_port_t output_odp_port,
4930ea56 2075 enum nx_action_sample_direction direction,
8b7ea2d4 2076 const struct dpif_ipfix_port *tunnel_port,
fe520682 2077 const struct flow_tnl *tunnel_key,
564230b6
PS
2078 struct dpif_ipfix_global_stats *stats,
2079 const struct dpif_ipfix_actions *ipfix_actions)
cd32509e 2080 OVS_REQUIRES(mutex)
29089a54 2081{
978427a5 2082 struct ipfix_flow_key *flow_key;
cf62fa4c 2083 struct dp_packet msg;
29089a54
RL
2084 enum ipfix_proto_l2 l2;
2085 enum ipfix_proto_l3 l3;
2086 enum ipfix_proto_l4 l4;
8b7ea2d4 2087 enum ipfix_proto_tunnel tunnel = IPFIX_PROTO_NOT_TUNNELED;
fb8f22c1 2088 enum ipfix_sampled_packet_type sampled_pkt_type = IPFIX_SAMPLED_PKT_UNKNOWN;
978427a5
RL
2089 uint8_t ethernet_header_length;
2090 uint16_t ethernet_total_length;
ebfe9a21
PS
2091 bool is_multicast = false;
2092 bool is_broadcast = false;
29089a54 2093
978427a5 2094 flow_key = &entry->flow_key;
cf62fa4c
PS
2095 dp_packet_use_stub(&msg, flow_key->flow_key_msg_part,
2096 sizeof flow_key->flow_key_msg_part);
29089a54
RL
2097
2098 /* Choose the right template ID matching the protocols in the
2099 * sampled packet. */
f0fb825a 2100 l2 = (flow->vlans[0].tci == 0) ? IPFIX_PROTO_L2_ETH : IPFIX_PROTO_L2_VLAN;
29089a54
RL
2101
2102 switch(ntohs(flow->dl_type)) {
2103 case ETH_TYPE_IP:
2104 l3 = IPFIX_PROTO_L3_IPV4;
0b4f83e2 2105 sampled_pkt_type = IPFIX_SAMPLED_PKT_IPV4_OK;
f51e8ccb
RL
2106 switch(flow->nw_proto) {
2107 case IPPROTO_TCP:
0b4f83e2
PS
2108 l4 = IPFIX_PROTO_L4_TCP;
2109 break;
f51e8ccb 2110 case IPPROTO_UDP:
0b4f83e2
PS
2111 l4 = IPFIX_PROTO_L4_UDP;
2112 break;
f51e8ccb 2113 case IPPROTO_SCTP:
0b4f83e2 2114 l4 = IPFIX_PROTO_L4_SCTP;
f51e8ccb
RL
2115 break;
2116 case IPPROTO_ICMP:
2117 l4 = IPFIX_PROTO_L4_ICMP;
2118 break;
2119 default:
2120 l4 = IPFIX_PROTO_L4_UNKNOWN;
fb8f22c1 2121 sampled_pkt_type = IPFIX_SAMPLED_PKT_IPV4_ERROR;
f51e8ccb 2122 }
29089a54
RL
2123 break;
2124 case ETH_TYPE_IPV6:
2125 l3 = IPFIX_PROTO_L3_IPV6;
0b4f83e2 2126 sampled_pkt_type = IPFIX_SAMPLED_PKT_IPV6_OK;
29089a54 2127 switch(flow->nw_proto) {
f51e8ccb 2128 case IPPROTO_TCP:
0b4f83e2
PS
2129 l4 = IPFIX_PROTO_L4_TCP;
2130 break;
f51e8ccb 2131 case IPPROTO_UDP:
0b4f83e2
PS
2132 l4 = IPFIX_PROTO_L4_UDP;
2133 break;
f51e8ccb 2134 case IPPROTO_SCTP:
0b4f83e2 2135 l4 = IPFIX_PROTO_L4_SCTP;
f51e8ccb
RL
2136 break;
2137 case IPPROTO_ICMPV6:
2138 l4 = IPFIX_PROTO_L4_ICMP;
29089a54 2139 break;
f51e8ccb
RL
2140 default:
2141 l4 = IPFIX_PROTO_L4_UNKNOWN;
fb8f22c1 2142 sampled_pkt_type = IPFIX_SAMPLED_PKT_IPV6_ERROR;
29089a54 2143 }
f51e8ccb
RL
2144 break;
2145 default:
2146 l3 = IPFIX_PROTO_L3_UNKNOWN;
2147 l4 = IPFIX_PROTO_L4_UNKNOWN;
fb8f22c1 2148 sampled_pkt_type = IPFIX_SAMPLED_PKT_OTHERS;
29089a54
RL
2149 }
2150
8b7ea2d4
WZ
2151 if (tunnel_port && tunnel_key) {
2152 tunnel = IPFIX_PROTO_TUNNELED;
2153 }
2154
cd32509e
MW
2155 uint8_t flow_direction =
2156 (direction == NX_ACTION_SAMPLE_INGRESS ? INGRESS_FLOW
2157 : direction == NX_ACTION_SAMPLE_EGRESS ? EGRESS_FLOW
2158 : output_odp_port == ODPP_NONE ? INGRESS_FLOW : EGRESS_FLOW);
2159
978427a5 2160 flow_key->obs_domain_id = obs_domain_id;
cd32509e
MW
2161 flow_key->template_id = ipfix_get_template_id(l2, l3, l4, tunnel,
2162 flow_direction);
29089a54
RL
2163
2164 /* The fields defined in the ipfix_data_record_* structs and sent
2165 * below must match exactly the templates defined in
2166 * ipfix_define_template_fields. */
2167
978427a5
RL
2168 ethernet_header_length = (l2 == IPFIX_PROTO_L2_VLAN)
2169 ? VLAN_ETH_HEADER_LEN : ETH_HEADER_LEN;
cf62fa4c 2170 ethernet_total_length = dp_packet_size(packet);
978427a5 2171
29089a54
RL
2172 /* Common Ethernet entities. */
2173 {
978427a5 2174 struct ipfix_data_record_flow_key_common *data_common;
29089a54 2175
cf62fa4c 2176 data_common = dp_packet_put_zeros(&msg, sizeof *data_common);
29089a54 2177 data_common->observation_point_id = htonl(obs_point_id);
ebfe9a21 2178 data_common->flow_direction = flow_direction;
74ff3298
JR
2179 data_common->source_mac_address = flow->dl_src;
2180 data_common->destination_mac_address = flow->dl_dst;
29089a54 2181 data_common->ethernet_type = flow->dl_type;
29089a54
RL
2182 data_common->ethernet_header_length = ethernet_header_length;
2183 }
2184
cd32509e
MW
2185 /* Interface Information Elements */
2186 ipfix_put_iface_data_record(di, flow->in_port.odp_port, &msg);
2187
2188 if (flow_direction == EGRESS_FLOW) {
2189 ipfix_put_iface_data_record(di, output_odp_port, &msg);
2190 }
2191
29089a54 2192 if (l2 == IPFIX_PROTO_L2_VLAN) {
978427a5 2193 struct ipfix_data_record_flow_key_vlan *data_vlan;
f0fb825a
EG
2194 uint16_t vlan_id = vlan_tci_to_vid(flow->vlans[0].tci);
2195 uint8_t priority = vlan_tci_to_pcp(flow->vlans[0].tci);
29089a54 2196
cf62fa4c 2197 data_vlan = dp_packet_put_zeros(&msg, sizeof *data_vlan);
29089a54
RL
2198 data_vlan->vlan_id = htons(vlan_id);
2199 data_vlan->dot1q_vlan_id = htons(vlan_id);
2200 data_vlan->dot1q_priority = priority;
2201 }
2202
2203 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
978427a5 2204 struct ipfix_data_record_flow_key_ip *data_ip;
29089a54 2205
cf62fa4c 2206 data_ip = dp_packet_put_zeros(&msg, sizeof *data_ip);
29089a54
RL
2207 data_ip->ip_version = (l3 == IPFIX_PROTO_L3_IPV4) ? 4 : 6;
2208 data_ip->ip_ttl = flow->nw_ttl;
2209 data_ip->protocol_identifier = flow->nw_proto;
2210 data_ip->ip_diff_serv_code_point = flow->nw_tos >> 2;
2211 data_ip->ip_precedence = flow->nw_tos >> 5;
2212 data_ip->ip_class_of_service = flow->nw_tos;
2213
2214 if (l3 == IPFIX_PROTO_L3_IPV4) {
978427a5 2215 struct ipfix_data_record_flow_key_ipv4 *data_ipv4;
f51e8ccb 2216
cf62fa4c 2217 data_ipv4 = dp_packet_put_zeros(&msg, sizeof *data_ipv4);
29089a54
RL
2218 data_ipv4->source_ipv4_address = flow->nw_src;
2219 data_ipv4->destination_ipv4_address = flow->nw_dst;
2220 } else { /* l3 == IPFIX_PROTO_L3_IPV6 */
978427a5 2221 struct ipfix_data_record_flow_key_ipv6 *data_ipv6;
29089a54 2222
cf62fa4c 2223 data_ipv6 = dp_packet_put_zeros(&msg, sizeof *data_ipv6);
29089a54
RL
2224 memcpy(data_ipv6->source_ipv6_address, &flow->ipv6_src,
2225 sizeof flow->ipv6_src);
2226 memcpy(data_ipv6->destination_ipv6_address, &flow->ipv6_dst,
2227 sizeof flow->ipv6_dst);
2228 data_ipv6->flow_label_ipv6 = flow->ipv6_label;
2229 }
2230 }
2231
0b4f83e2
PS
2232 if (l4 == IPFIX_PROTO_L4_TCP
2233 || l4 == IPFIX_PROTO_L4_UDP
2234 || l4 == IPFIX_PROTO_L4_SCTP) {
f51e8ccb
RL
2235 struct ipfix_data_record_flow_key_transport *data_transport;
2236
cf62fa4c 2237 data_transport = dp_packet_put_zeros(&msg, sizeof *data_transport);
f51e8ccb
RL
2238 data_transport->source_transport_port = flow->tp_src;
2239 data_transport->destination_transport_port = flow->tp_dst;
2240 } else if (l4 == IPFIX_PROTO_L4_ICMP) {
2241 struct ipfix_data_record_flow_key_icmp *data_icmp;
29089a54 2242
cf62fa4c 2243 data_icmp = dp_packet_put_zeros(&msg, sizeof *data_icmp);
f51e8ccb
RL
2244 data_icmp->icmp_type = ntohs(flow->tp_src) & 0xff;
2245 data_icmp->icmp_code = ntohs(flow->tp_dst) & 0xff;
29089a54
RL
2246 }
2247
8b7ea2d4
WZ
2248 if (tunnel == IPFIX_PROTO_TUNNELED) {
2249 struct ipfix_data_record_flow_key_tunnel *data_tunnel;
2250 const uint8_t *tun_id;
2251
cf62fa4c 2252 data_tunnel = dp_packet_put_zeros(&msg, sizeof *data_tunnel +
8b7ea2d4
WZ
2253 tunnel_port->tunnel_key_length);
2254 data_tunnel->tunnel_source_ipv4_address = tunnel_key->ip_src;
2255 data_tunnel->tunnel_destination_ipv4_address = tunnel_key->ip_dst;
2256 /* The tunnel_protocol_identifier is from tunnel_proto array, which
2257 * contains protocol_identifiers of each tunnel type.
8b7ea2d4
WZ
2258 */
2259 data_tunnel->tunnel_protocol_identifier =
2260 tunnel_protocol[tunnel_port->tunnel_type];
2261 data_tunnel->tunnel_source_transport_port = tunnel_key->tp_src;
2262 data_tunnel->tunnel_destination_transport_port = tunnel_key->tp_dst;
2263 data_tunnel->tunnel_type = tunnel_port->tunnel_type;
2264 data_tunnel->tunnel_key_length = tunnel_port->tunnel_key_length;
2265 /* tun_id is in network order, and tunnel key is in low bits. */
2266 tun_id = (const uint8_t *) &tunnel_key->tun_id;
2267 memcpy(data_tunnel->tunnel_key,
2268 &tun_id[8 - tunnel_port->tunnel_key_length],
2269 tunnel_port->tunnel_key_length);
2270 }
2271
cf62fa4c 2272 flow_key->flow_key_msg_part_size = dp_packet_size(&msg);
978427a5 2273
ebfe9a21
PS
2274 if (eth_addr_is_broadcast(flow->dl_dst)) {
2275 is_broadcast = true;
2276 } else if (eth_addr_is_multicast(flow->dl_dst)) {
2277 is_multicast = true;
2278 }
2279
978427a5 2280 {
978427a5
RL
2281 uint64_t layer2_octet_delta_count;
2282
2283 /* Calculate the total matched octet count by considering as
2284 * an approximation that all matched packets have the same
2285 * length. */
2286 layer2_octet_delta_count = packet_delta_count * ethernet_total_length;
2287
f88ee3a5 2288 entry->flow_end_timestamp_usec = ipfix_now();
978427a5 2289 entry->flow_start_timestamp_usec = entry->flow_end_timestamp_usec;
564230b6
PS
2290
2291 if (ipfix_actions && ipfix_actions->output_action) {
2292 entry->dropped_packet_delta_count = 0;
2293 } else {
2294 entry->dropped_packet_delta_count = packet_delta_count;
2295 }
2296
978427a5
RL
2297 entry->packet_delta_count = packet_delta_count;
2298 entry->layer2_octet_delta_count = layer2_octet_delta_count;
fe520682 2299
564230b6 2300 stats->dropped_packet_total_count += entry->dropped_packet_delta_count;
fe520682 2301 stats->packet_total_count += packet_delta_count;
2302 stats->layer2_octet_total_count += layer2_octet_delta_count;
ebfe9a21
PS
2303
2304 entry->post_mcast_packet_delta_count = 0;
2305 if (is_broadcast) {
2306 if (flow_direction == INGRESS_FLOW) {
2307 stats->in_bcast_packet_total_count += packet_delta_count;
2308 } else if (flow_direction == EGRESS_FLOW) {
2309 stats->out_bcast_packet_total_count += packet_delta_count;
2310 }
2311 } else if (is_multicast) {
2312 if (flow_direction == INGRESS_FLOW) {
2313 stats->in_mcast_packet_total_count += packet_delta_count;
2314 } else if (flow_direction == EGRESS_FLOW) {
2315 entry->post_mcast_packet_delta_count = packet_delta_count;
2316 stats->post_mcast_packet_total_count += packet_delta_count;
2317 }
2318 } else {
2319 if (flow_direction == INGRESS_FLOW) {
2320 stats->in_ucast_packet_total_count += packet_delta_count;
2321 } else if (flow_direction == EGRESS_FLOW) {
2322 stats->out_ucast_packet_total_count += packet_delta_count;
2323 }
2324 }
2325
564230b6 2326 entry->dropped_packet_total_count = stats->dropped_packet_total_count;
fe520682 2327 entry->packet_total_count = stats->packet_total_count;
ebfe9a21
PS
2328 entry->in_ucast_packet_total_count =
2329 stats->in_ucast_packet_total_count;
2330 entry->in_mcast_packet_total_count =
2331 stats->in_mcast_packet_total_count;
2332 entry->in_bcast_packet_total_count =
2333 stats->in_bcast_packet_total_count;
2334 entry->out_ucast_packet_total_count =
2335 stats->out_ucast_packet_total_count;
2336 entry->out_bcast_packet_total_count =
2337 stats->out_bcast_packet_total_count;
2338 entry->post_mcast_packet_total_count =
2339 stats->post_mcast_packet_total_count;
fe520682 2340 entry->layer2_octet_total_count = stats->layer2_octet_total_count;
978427a5
RL
2341 }
2342
2343 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
2344 uint16_t ip_total_length =
2345 ethernet_total_length - ethernet_header_length;
f51e8ccb
RL
2346 uint64_t octet_delta_count;
2347
2348 /* Calculate the total matched octet count by considering as
2349 * an approximation that all matched packets have the same
2350 * length. */
2351 octet_delta_count = packet_delta_count * ip_total_length;
978427a5 2352
564230b6
PS
2353 if (ipfix_actions && ipfix_actions->output_action) {
2354 entry->dropped_octet_delta_count = 0;
2355 } else {
2356 entry->dropped_octet_delta_count = octet_delta_count;
2357 }
2358
f51e8ccb
RL
2359 entry->octet_delta_count = octet_delta_count;
2360 entry->octet_delta_sum_of_squares = octet_delta_count * ip_total_length;
978427a5
RL
2361 entry->minimum_ip_total_length = ip_total_length;
2362 entry->maximum_ip_total_length = ip_total_length;
fe520682 2363
564230b6 2364 stats->dropped_octet_total_count += entry->dropped_octet_delta_count;
fe520682 2365 stats->octet_total_count += octet_delta_count;
2366 stats->octet_total_sum_of_squares += entry->octet_delta_sum_of_squares;
fe520682 2367
ebfe9a21
PS
2368 if (is_multicast && flow_direction == EGRESS_FLOW) {
2369 entry->post_mcast_octet_delta_count = octet_delta_count;
2370 stats->post_mcast_octet_total_count += octet_delta_count;
2371 } else {
2372 entry->post_mcast_octet_delta_count = 0;
2373 }
978427a5
RL
2374 } else {
2375 entry->octet_delta_sum_of_squares = 0;
2376 entry->minimum_ip_total_length = 0;
2377 entry->maximum_ip_total_length = 0;
2378 }
fb8f22c1 2379
564230b6 2380 entry->dropped_octet_total_count = stats->dropped_octet_total_count;
0b4f83e2
PS
2381 entry->octet_total_sum_of_squares = stats->octet_total_sum_of_squares;
2382 entry->octet_total_count = stats->octet_total_count;
ebfe9a21
PS
2383 entry->post_mcast_octet_total_count =
2384 stats->post_mcast_octet_total_count;
0b4f83e2
PS
2385
2386 if (l4 == IPFIX_PROTO_L4_TCP) {
2387 uint16_t tcp_flags = ntohs(flow->tcp_flags);
2388 entry->tcp_packet_delta_count = packet_delta_count;
2389
2390 if (tcp_flags & TCP_ACK) {
2391 stats->tcp_ack_total_count += packet_delta_count;
2392 }
2393 if (tcp_flags & TCP_FIN) {
2394 stats->tcp_fin_total_count += packet_delta_count;
2395 }
2396 if (tcp_flags & TCP_PSH) {
2397 stats->tcp_psh_total_count += packet_delta_count;
2398 }
2399 if (tcp_flags & TCP_RST) {
2400 stats->tcp_rst_total_count += packet_delta_count;
2401 }
2402 if (tcp_flags & TCP_SYN) {
2403 stats->tcp_syn_total_count += packet_delta_count;
2404 }
2405 if (tcp_flags & TCP_URG) {
2406 stats->tcp_urg_total_count += packet_delta_count;
2407 }
2408 } else {
2409 entry->tcp_packet_delta_count = 0;
2410 }
2411
2412 entry->tcp_ack_total_count = stats->tcp_ack_total_count;
2413 entry->tcp_fin_total_count = stats->tcp_fin_total_count;
2414 entry->tcp_psh_total_count = stats->tcp_psh_total_count;
2415 entry->tcp_rst_total_count = stats->tcp_rst_total_count;
2416 entry->tcp_syn_total_count = stats->tcp_syn_total_count;
2417 entry->tcp_urg_total_count = stats->tcp_urg_total_count;
2418
fb8f22c1 2419 return sampled_pkt_type;
978427a5
RL
2420}
2421
2422/* Send each single data record in its own data set, to simplify the
2423 * implementation by avoiding having to group record by template ID
2424 * before sending. */
2425static void
2426ipfix_put_data_set(uint32_t export_time_sec,
2427 struct ipfix_flow_cache_entry *entry,
2428 enum ipfix_flow_end_reason flow_end_reason,
c97320eb
WZ
2429 const char *virtual_obs_id,
2430 uint8_t virtual_obs_len,
cf62fa4c 2431 struct dp_packet *msg)
978427a5
RL
2432{
2433 size_t set_hdr_offset;
2434 struct ipfix_set_header *set_hdr;
2435
cf62fa4c 2436 set_hdr_offset = dp_packet_size(msg);
978427a5
RL
2437
2438 /* Put a Data Set. */
cf62fa4c 2439 set_hdr = dp_packet_put_zeros(msg, sizeof *set_hdr);
978427a5
RL
2440 set_hdr->set_id = htons(entry->flow_key.template_id);
2441
2442 /* Copy the flow key part of the data record. */
cf62fa4c 2443 dp_packet_put(msg, entry->flow_key.flow_key_msg_part,
978427a5
RL
2444 entry->flow_key.flow_key_msg_part_size);
2445
c97320eb
WZ
2446 /* Export virtual observation ID. */
2447 if (virtual_obs_id) {
2448 dp_packet_put(msg, &virtual_obs_len, sizeof(virtual_obs_len));
2449 dp_packet_put(msg, virtual_obs_id, virtual_obs_len);
2450 }
2451
978427a5
RL
2452 /* Put the non-key part of the data record. */
2453
2454 {
2455 struct ipfix_data_record_aggregated_common *data_aggregated_common;
2456 uint64_t export_time_usec, flow_start_delta_usec, flow_end_delta_usec;
2457
2458 /* Calculate the negative deltas relative to the export time
2459 * in seconds sent in the header, not the exact export
2460 * time. */
2461 export_time_usec = 1000000LL * export_time_sec;
2462 flow_start_delta_usec = export_time_usec
2463 - entry->flow_start_timestamp_usec;
2464 flow_end_delta_usec = export_time_usec
2465 - entry->flow_end_timestamp_usec;
2466
cf62fa4c 2467 data_aggregated_common = dp_packet_put_zeros(
978427a5
RL
2468 msg, sizeof *data_aggregated_common);
2469 data_aggregated_common->flow_start_delta_microseconds = htonl(
2470 flow_start_delta_usec);
2471 data_aggregated_common->flow_end_delta_microseconds = htonl(
2472 flow_end_delta_usec);
564230b6
PS
2473 data_aggregated_common->dropped_packet_delta_count = htonll(
2474 entry->dropped_packet_delta_count);
2475 data_aggregated_common->dropped_packet_total_count = htonll(
2476 entry->dropped_packet_total_count);
978427a5
RL
2477 data_aggregated_common->packet_delta_count = htonll(
2478 entry->packet_delta_count);
fe520682 2479 data_aggregated_common->packet_total_count = htonll(
2480 entry->packet_total_count);
ebfe9a21
PS
2481 data_aggregated_common->in_ucast_packet_total_count = htonll(
2482 entry->in_ucast_packet_total_count);
2483 data_aggregated_common->in_mcast_packet_total_count = htonll(
2484 entry->in_mcast_packet_total_count);
2485 data_aggregated_common->in_bcast_packet_total_count = htonll(
2486 entry->in_bcast_packet_total_count);
2487 data_aggregated_common->out_ucast_packet_total_count = htonll(
2488 entry->out_ucast_packet_total_count);
2489 data_aggregated_common->out_bcast_packet_total_count = htonll(
2490 entry->out_bcast_packet_total_count);
978427a5
RL
2491 data_aggregated_common->layer2_octet_delta_count = htonll(
2492 entry->layer2_octet_delta_count);
fe520682 2493 data_aggregated_common->layer2_octet_total_count = htonll(
2494 entry->layer2_octet_total_count);
978427a5 2495 data_aggregated_common->flow_end_reason = flow_end_reason;
ebfe9a21
PS
2496 data_aggregated_common->post_mcast_packet_delta_count = htonll(
2497 entry->post_mcast_packet_delta_count);
2498 data_aggregated_common->post_mcast_packet_total_count = htonll(
2499 entry->post_mcast_packet_total_count);
978427a5
RL
2500 }
2501
2502 if (entry->octet_delta_sum_of_squares) { /* IP packet. */
2503 struct ipfix_data_record_aggregated_ip *data_aggregated_ip;
2504
cf62fa4c 2505 data_aggregated_ip = dp_packet_put_zeros(
978427a5 2506 msg, sizeof *data_aggregated_ip);
564230b6
PS
2507 data_aggregated_ip->dropped_octet_delta_count = htonll(
2508 entry->dropped_octet_delta_count);
2509 data_aggregated_ip->dropped_octet_total_count = htonll(
2510 entry->dropped_octet_total_count);
f51e8ccb
RL
2511 data_aggregated_ip->octet_delta_count = htonll(
2512 entry->octet_delta_count);
fe520682 2513 data_aggregated_ip->octet_total_count = htonll(
2514 entry->octet_total_count);
978427a5
RL
2515 data_aggregated_ip->octet_delta_sum_of_squares = htonll(
2516 entry->octet_delta_sum_of_squares);
fe520682 2517 data_aggregated_ip->octet_total_sum_of_squares = htonll(
2518 entry->octet_total_sum_of_squares);
978427a5
RL
2519 data_aggregated_ip->minimum_ip_total_length = htonll(
2520 entry->minimum_ip_total_length);
2521 data_aggregated_ip->maximum_ip_total_length = htonll(
2522 entry->maximum_ip_total_length);
ebfe9a21
PS
2523 data_aggregated_ip->post_mcast_octet_delta_count = htonll(
2524 entry->post_mcast_octet_delta_count);
2525 data_aggregated_ip->post_mcast_octet_total_count = htonll(
2526 entry->post_mcast_octet_total_count);
978427a5 2527 }
29089a54 2528
0b4f83e2
PS
2529 if (entry->tcp_packet_delta_count) {
2530 struct ipfix_data_record_aggregated_tcp *data_aggregated_tcp;
2531
2532 data_aggregated_tcp = dp_packet_put_zeros(
2533 msg, sizeof *data_aggregated_tcp);
2534 data_aggregated_tcp->tcp_ack_total_count = htonll(
2535 entry->tcp_ack_total_count);
2536 data_aggregated_tcp->tcp_fin_total_count = htonll(
2537 entry->tcp_fin_total_count);
2538 data_aggregated_tcp->tcp_psh_total_count = htonll(
2539 entry->tcp_psh_total_count);
2540 data_aggregated_tcp->tcp_rst_total_count = htonll(
2541 entry->tcp_rst_total_count);
2542 data_aggregated_tcp->tcp_syn_total_count = htonll(
2543 entry->tcp_syn_total_count);
2544 data_aggregated_tcp->tcp_urg_total_count = htonll(
2545 entry->tcp_urg_total_count);
2546 }
2547
cf62fa4c
PS
2548 set_hdr = (struct ipfix_set_header*)((uint8_t*)dp_packet_data(msg) + set_hdr_offset);
2549 set_hdr->length = htons(dp_packet_size(msg) - set_hdr_offset);
978427a5
RL
2550}
2551
d564c4bb
PS
2552static void
2553ipfix_put_exporter_data_set(uint32_t exporting_process_id,
2554 const ofproto_ipfix_stats *ofproto_stats,
2555 struct dp_packet *msg)
2556{
2557 size_t set_hdr_offset;
2558 struct ipfix_set_header *set_hdr;
2559
2560 set_hdr_offset = dp_packet_size(msg);
2561
2562 /* Put a Data Set. */
2563 set_hdr = dp_packet_put_zeros(msg, sizeof *set_hdr);
2564 set_hdr->set_id = htons(
2565 ipfix_get_options_template_id(IPFIX_OPTIONS_TEMPLATE_EXPORTER_STATS));
2566
2567 {
2568 struct ipfix_data_record_exporter_stats *data_exporter_stats;
2569
2570 data_exporter_stats = dp_packet_put_zeros(
2571 msg, sizeof *data_exporter_stats);
2572
2573 data_exporter_stats->exporting_process_id =
2574 htonl(exporting_process_id);
2575 data_exporter_stats->not_sent_packet_total_count = htonll(
2576 ofproto_stats->tx_errors);
2577 }
2578
2579 set_hdr = (struct ipfix_set_header *)
2580 ((uint8_t *)dp_packet_data(msg) + set_hdr_offset);
2581 set_hdr->length = htons(dp_packet_size(msg) - set_hdr_offset);
2582}
2583
2584/* Send an IPFIX message with a single data set containing Exporting Process
2585 * Reliability Statistics. */
2586static void
2587ipfix_send_exporter_data_msg(struct dpif_ipfix_exporter *exporter,
2588 uint32_t export_time_sec)
2589{
2590 uint64_t msg_stub[DIV_ROUND_UP(MAX_MESSAGE_LEN, 8)];
2591 struct dp_packet msg;
2592 size_t tx_errors;
2593
2594 dp_packet_use_stub(&msg, msg_stub, sizeof msg_stub);
2595
2596 /* In case of Exporting Process Statistics, Observation Domain ID should
2597 * be set to 0. */
2598 ipfix_init_header(export_time_sec, exporter->seq_number++, 0U, &msg);
2599 ipfix_put_exporter_data_set(exporter->exporter_id,
2600 &exporter->ofproto_stats, &msg);
2601 tx_errors = ipfix_send_msg(exporter->collectors, &msg);
2602
2603 dp_packet_uninit(&msg);
2604
2605 exporter->ofproto_stats.tx_pkts +=
2606 collectors_count(exporter->collectors) - tx_errors;
2607 exporter->ofproto_stats.tx_errors += tx_errors;
2608}
2609
978427a5
RL
2610/* Send an IPFIX message with a single data record. */
2611static void
2612ipfix_send_data_msg(struct dpif_ipfix_exporter *exporter,
2613 uint32_t export_time_sec,
2614 struct ipfix_flow_cache_entry *entry,
2615 enum ipfix_flow_end_reason flow_end_reason)
2616{
2617 uint64_t msg_stub[DIV_ROUND_UP(MAX_MESSAGE_LEN, 8)];
cf62fa4c 2618 struct dp_packet msg;
fb8f22c1
BY
2619 size_t tx_errors;
2620
cf62fa4c 2621 dp_packet_use_stub(&msg, msg_stub, sizeof msg_stub);
978427a5
RL
2622
2623 ipfix_init_header(export_time_sec, exporter->seq_number++,
2624 entry->flow_key.obs_domain_id, &msg);
c97320eb
WZ
2625 ipfix_put_data_set(export_time_sec, entry, flow_end_reason,
2626 exporter->virtual_obs_id, exporter->virtual_obs_len,
2627 &msg);
fb8f22c1 2628 tx_errors = ipfix_send_msg(exporter->collectors, &msg);
29089a54 2629
cf62fa4c 2630 dp_packet_uninit(&msg);
fb8f22c1 2631
fe520682 2632 exporter->ofproto_stats.current_flows--;
2633 exporter->ofproto_stats.tx_pkts +=
2634 collectors_count(exporter->collectors) - tx_errors;
2635 exporter->ofproto_stats.tx_errors += tx_errors;
29089a54
RL
2636}
2637
2638static void
cd32509e
MW
2639dpif_ipfix_sample(const struct dpif_ipfix *di,
2640 struct dpif_ipfix_exporter *exporter,
cf62fa4c 2641 const struct dp_packet *packet, const struct flow *flow,
29089a54 2642 uint64_t packet_delta_count, uint32_t obs_domain_id,
8b7ea2d4 2643 uint32_t obs_point_id, odp_port_t output_odp_port,
4930ea56 2644 enum nx_action_sample_direction direction,
8b7ea2d4 2645 const struct dpif_ipfix_port *tunnel_port,
564230b6
PS
2646 const struct flow_tnl *tunnel_key,
2647 const struct dpif_ipfix_actions *ipfix_actions)
cd32509e 2648 OVS_REQUIRES(mutex)
29089a54 2649{
978427a5 2650 struct ipfix_flow_cache_entry *entry;
fb8f22c1 2651 enum ipfix_sampled_packet_type sampled_packet_type;
29089a54 2652
978427a5
RL
2653 /* Create a flow cache entry from the sample. */
2654 entry = xmalloc(sizeof *entry);
fe520682 2655 sampled_packet_type =
cd32509e 2656 ipfix_cache_entry_init(di, entry, packet,
fe520682 2657 flow, packet_delta_count,
2658 obs_domain_id, obs_point_id,
2659 output_odp_port, direction,
2660 tunnel_port, tunnel_key,
564230b6
PS
2661 &exporter->ipfix_global_stats,
2662 ipfix_actions);
fe520682 2663
fb8f22c1 2664 ipfix_cache_update(exporter, entry, sampled_packet_type);
29089a54
RL
2665}
2666
7b85919c
JS
2667static bool
2668bridge_exporter_enabled(struct dpif_ipfix *di)
2669{
2670 return di->bridge_exporter.probability > 0;
2671}
2672
29089a54 2673void
cf62fa4c 2674dpif_ipfix_bridge_sample(struct dpif_ipfix *di, const struct dp_packet *packet,
8b7ea2d4
WZ
2675 const struct flow *flow,
2676 odp_port_t input_odp_port, odp_port_t output_odp_port,
564230b6
PS
2677 const struct flow_tnl *output_tunnel_key,
2678 const struct dpif_ipfix_actions *ipfix_actions)
8b7ea2d4 2679 OVS_EXCLUDED(mutex)
29089a54 2680{
626ace7b 2681 uint64_t packet_delta_count;
8b7ea2d4
WZ
2682 const struct flow_tnl *tunnel_key = NULL;
2683 struct dpif_ipfix_port * tunnel_port = NULL;
626ace7b
EJ
2684
2685 ovs_mutex_lock(&mutex);
a2abbcba
JS
2686 if (!bridge_exporter_enabled(di)) {
2687 ovs_mutex_unlock(&mutex);
2688 return;
2689 }
47946ac5
WZ
2690
2691 /* Skip BFD packets:
2692 * Bidirectional Forwarding Detection(BFD) packets are for monitoring
2693 * the tunnel link status and consumed by ovs itself. No need to
2694 * smaple them.
2695 * CF IETF RFC 5881, BFD control packet is the UDP packet with
2696 * destination port 3784, and BFD echo packet is the UDP packet with
2697 * destination port 3785.
2698 */
2699 if (is_ip_any(flow) &&
2700 flow->nw_proto == IPPROTO_UDP &&
2701 (flow->tp_dst == htons(BFD_CONTROL_DEST_PORT) ||
2702 flow->tp_dst == htons(BFD_ECHO_DEST_PORT))) {
2703 ovs_mutex_unlock(&mutex);
2704 return;
2705 }
2706
29089a54
RL
2707 /* Use the sampling probability as an approximation of the number
2708 * of matched packets. */
626ace7b 2709 packet_delta_count = UINT32_MAX / di->bridge_exporter.probability;
8b7ea2d4
WZ
2710 if (di->bridge_exporter.options->enable_tunnel_sampling) {
2711 if (output_odp_port == ODPP_NONE && flow->tunnel.ip_dst) {
2712 /* Input tunnel. */
2713 tunnel_key = &flow->tunnel;
cd32509e 2714 tunnel_port = dpif_ipfix_find_tunnel_port(di, input_odp_port);
8b7ea2d4
WZ
2715 }
2716 if (output_odp_port != ODPP_NONE && output_tunnel_key) {
2717 /* Output tunnel, output_tunnel_key must be valid. */
2718 tunnel_key = output_tunnel_key;
cd32509e 2719 tunnel_port = dpif_ipfix_find_tunnel_port(di, output_odp_port);
8b7ea2d4
WZ
2720 }
2721 }
47946ac5 2722
cd32509e 2723 dpif_ipfix_sample(di, &di->bridge_exporter.exporter, packet, flow,
29089a54
RL
2724 packet_delta_count,
2725 di->bridge_exporter.options->obs_domain_id,
8b7ea2d4 2726 di->bridge_exporter.options->obs_point_id,
4930ea56 2727 output_odp_port, NX_ACTION_SAMPLE_DEFAULT,
564230b6 2728 tunnel_port, tunnel_key, ipfix_actions);
626ace7b 2729 ovs_mutex_unlock(&mutex);
29089a54
RL
2730}
2731
2732void
cf62fa4c 2733dpif_ipfix_flow_sample(struct dpif_ipfix *di, const struct dp_packet *packet,
f69f713b 2734 const struct flow *flow,
8de6ff3e 2735 const struct user_action_cookie *cookie,
f69f713b 2736 odp_port_t input_odp_port,
564230b6
PS
2737 const struct flow_tnl *output_tunnel_key,
2738 const struct dpif_ipfix_actions *ipfix_actions)
f69f713b 2739 OVS_EXCLUDED(mutex)
29089a54
RL
2740{
2741 struct dpif_ipfix_flow_exporter_map_node *node;
f69f713b
BY
2742 const struct flow_tnl *tunnel_key = NULL;
2743 struct dpif_ipfix_port * tunnel_port = NULL;
2744 odp_port_t output_odp_port = cookie->flow_sample.output_odp_port;
2745 uint32_t collector_set_id = cookie->flow_sample.collector_set_id;
2746 uint16_t probability = cookie->flow_sample.probability;
2747
29089a54
RL
2748 /* Use the sampling probability as an approximation of the number
2749 * of matched packets. */
2750 uint64_t packet_delta_count = USHRT_MAX / probability;
2751
626ace7b 2752 ovs_mutex_lock(&mutex);
29089a54 2753 node = dpif_ipfix_find_flow_exporter_map_node(di, collector_set_id);
626ace7b 2754 if (node) {
f69f713b
BY
2755 if (node->exporter.options->enable_tunnel_sampling) {
2756 if (output_odp_port == ODPP_NONE && flow->tunnel.ip_dst) {
2757 /* Input tunnel. */
2758 tunnel_key = &flow->tunnel;
cd32509e 2759 tunnel_port = dpif_ipfix_find_tunnel_port(di, input_odp_port);
f69f713b
BY
2760 }
2761 if (output_odp_port != ODPP_NONE && output_tunnel_key) {
2762 /* Output tunnel, output_tunnel_key must be valid. */
2763 tunnel_key = output_tunnel_key;
cd32509e 2764 tunnel_port = dpif_ipfix_find_tunnel_port(di, output_odp_port);
f69f713b
BY
2765 }
2766 }
2767
cd32509e 2768 dpif_ipfix_sample(di, &node->exporter.exporter, packet, flow,
f69f713b
BY
2769 packet_delta_count,
2770 cookie->flow_sample.obs_domain_id,
2771 cookie->flow_sample.obs_point_id,
4930ea56 2772 output_odp_port, cookie->flow_sample.direction,
564230b6 2773 tunnel_port, tunnel_key, ipfix_actions);
29089a54 2774 }
626ace7b 2775 ovs_mutex_unlock(&mutex);
29089a54 2776}
978427a5
RL
2777
2778static void
2779dpif_ipfix_cache_expire(struct dpif_ipfix_exporter *exporter,
2780 bool forced_end, const uint64_t export_time_usec,
2781 const uint32_t export_time_sec)
2782{
2783 struct ipfix_flow_cache_entry *entry, *next_entry;
2784 uint64_t max_flow_start_timestamp_usec;
2785 bool template_msg_sent = false;
2786 enum ipfix_flow_end_reason flow_end_reason;
2787
417e7e66 2788 if (ovs_list_is_empty(&exporter->cache_flow_start_timestamp_list)) {
978427a5
RL
2789 return;
2790 }
2791
2792 max_flow_start_timestamp_usec = export_time_usec -
2793 1000000LL * exporter->cache_active_timeout;
2794
2795 LIST_FOR_EACH_SAFE (entry, next_entry, cache_flow_start_timestamp_list_node,
2796 &exporter->cache_flow_start_timestamp_list) {
2797 if (forced_end) {
2798 flow_end_reason = FORCED_END;
2799 } else if (entry->flow_start_timestamp_usec
2800 <= max_flow_start_timestamp_usec) {
2801 flow_end_reason = ACTIVE_TIMEOUT;
2802 } else if (hmap_count(&exporter->cache_flow_key_map)
2803 > exporter->cache_max_flows) {
2804 /* Enforce exporter->cache_max_flows. */
2805 flow_end_reason = LACK_OF_RESOURCES;
2806 } else {
2807 /* Remaining flows haven't expired yet. */
2808 break;
2809 }
2810
417e7e66 2811 ovs_list_remove(&entry->cache_flow_start_timestamp_list_node);
978427a5
RL
2812 hmap_remove(&exporter->cache_flow_key_map,
2813 &entry->flow_key_map_node);
2814
d564c4bb
PS
2815 /* XXX: Make frequency of the (Options) Template and Exporter Process
2816 * Statistics transmission configurable.
2817 * Cf. IETF RFC 5101 Section 4.3. and 10.3.6. */
978427a5
RL
2818 if (!template_msg_sent
2819 && (exporter->last_template_set_time + IPFIX_TEMPLATE_INTERVAL)
2820 <= export_time_sec) {
8b7ea2d4
WZ
2821 ipfix_send_template_msgs(exporter, export_time_sec,
2822 entry->flow_key.obs_domain_id);
978427a5
RL
2823 exporter->last_template_set_time = export_time_sec;
2824 template_msg_sent = true;
d564c4bb
PS
2825
2826 /* Send Exporter Process Statistics. */
2827 ipfix_send_exporter_data_msg(exporter, export_time_sec);
978427a5
RL
2828 }
2829
2830 /* XXX: Group multiple data records for the same obs domain id
2831 * into the same message. */
2832 ipfix_send_data_msg(exporter, export_time_sec, entry, flow_end_reason);
2833 free(entry);
2834 }
2835}
2836
2837static void
2838get_export_time_now(uint64_t *export_time_usec, uint32_t *export_time_sec)
2839{
f88ee3a5 2840 *export_time_usec = ipfix_now();
978427a5
RL
2841
2842 /* The IPFIX start and end deltas are negative deltas relative to
2843 * the export time, so set the export time 1 second off to
2844 * calculate those deltas. */
f88ee3a5 2845 *export_time_sec = DIV_ROUND_UP(*export_time_usec, 1000000);
978427a5
RL
2846}
2847
2848static void
2849dpif_ipfix_cache_expire_now(struct dpif_ipfix_exporter *exporter,
2850 bool forced_end)
2851{
2852 uint64_t export_time_usec;
2853 uint32_t export_time_sec;
2854
2855 get_export_time_now(&export_time_usec, &export_time_sec);
2856 dpif_ipfix_cache_expire(exporter, forced_end, export_time_usec,
2857 export_time_sec);
2858}
2859
2860void
2861dpif_ipfix_run(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
2862{
2863 uint64_t export_time_usec;
2864 uint32_t export_time_sec;
2865 struct dpif_ipfix_flow_exporter_map_node *flow_exporter_node;
2866
2867 ovs_mutex_lock(&mutex);
2868 get_export_time_now(&export_time_usec, &export_time_sec);
7b85919c 2869 if (bridge_exporter_enabled(di)) {
978427a5
RL
2870 dpif_ipfix_cache_expire(
2871 &di->bridge_exporter.exporter, false, export_time_usec,
2872 export_time_sec);
2873 }
2874 HMAP_FOR_EACH (flow_exporter_node, node, &di->flow_exporter_map) {
2875 dpif_ipfix_cache_expire(
2876 &flow_exporter_node->exporter.exporter, false, export_time_usec,
2877 export_time_sec);
2878 }
2879 ovs_mutex_unlock(&mutex);
2880}
2881
2882void
2883dpif_ipfix_wait(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
2884{
2885 long long int next_timeout_msec = LLONG_MAX;
2886 struct dpif_ipfix_flow_exporter_map_node *flow_exporter_node;
2887
2888 ovs_mutex_lock(&mutex);
7b85919c 2889 if (bridge_exporter_enabled(di)) {
978427a5
RL
2890 if (ipfix_cache_next_timeout_msec(
2891 &di->bridge_exporter.exporter, &next_timeout_msec)) {
2892 poll_timer_wait_until(next_timeout_msec);
2893 }
2894 }
2895 HMAP_FOR_EACH (flow_exporter_node, node, &di->flow_exporter_map) {
2896 if (ipfix_cache_next_timeout_msec(
2897 &flow_exporter_node->exporter.exporter, &next_timeout_msec)) {
2898 poll_timer_wait_until(next_timeout_msec);
2899 }
2900 }
2901 ovs_mutex_unlock(&mutex);
2902}
564230b6
PS
2903
2904static void
2905dpif_ipfix_read_sample_actions(const struct flow *flow,
2906 const struct nlattr *actions,
2907 size_t actions_len,
2908 struct dpif_ipfix_actions *ipfix_actions)
2909{
2910 const struct nlattr *a;
2911 unsigned int left;
2912 uint32_t probability = 0;
2913 struct dpif_ipfix_actions sample_actions = {0};
2914
2915 if (actions_len == 0) {
2916 return;
2917 }
2918
2919 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
2920
2921 enum ovs_sample_attr type = nl_attr_type(a);
2922 switch (type) {
2923 case OVS_SAMPLE_ATTR_PROBABILITY:
2924 probability = nl_attr_get_u32(a);
2925 break;
2926
2927 case OVS_SAMPLE_ATTR_ACTIONS:
2928 dpif_ipfix_read_actions(flow, nl_attr_get(a), nl_attr_get_size(a),
2929 &sample_actions);
2930 break;
2931
2932 case OVS_SAMPLE_ATTR_UNSPEC:
2933 case __OVS_SAMPLE_ATTR_MAX:
2934 default:
2935 OVS_NOT_REACHED();
2936 }
2937 }
2938
2939 /* An output action inside sample action is truly an output if the sampling
2940 * probability is set to 100% */
2941 if (probability == UINT32_MAX && sample_actions.output_action == true) {
2942 ipfix_actions->output_action = true;
2943 }
2944}
2945
2946void
2947dpif_ipfix_read_actions(const struct flow *flow,
2948 const struct nlattr *actions,
2949 size_t actions_len,
2950 struct dpif_ipfix_actions *ipfix_actions)
2951{
2952 const struct nlattr *a;
2953 unsigned int left;
2954
2955 if (actions_len == 0) {
2956 return;
2957 }
2958
2959 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
2960 enum ovs_action_attr type = nl_attr_type(a);
2961 switch (type) {
2962 case OVS_ACTION_ATTR_OUTPUT:
2963 ipfix_actions->output_action = true;
2964 break;
2965 case OVS_ACTION_ATTR_SAMPLE:
2966 dpif_ipfix_read_sample_actions(flow, nl_attr_get(a),
2967 nl_attr_get_size(a), ipfix_actions);
2968 break;
2969 case OVS_ACTION_ATTR_CLONE:
2970 dpif_ipfix_read_actions(flow, nl_attr_get(a), nl_attr_get_size(a),
2971 ipfix_actions);
2972 break;
2973
2974 /* OVS_ACTION_ATTR_USERSPACE and OVS_ACTION_ATTR_RECIRC actions can
2975 * yield absolutely any kind of behavior. Let's assume that flow drops
2976 * the packet if there isn't another clear OVS_ACTION_ATTR_OUTPUT
2977 * action associated with packet */
2978 case OVS_ACTION_ATTR_USERSPACE:
2979 case OVS_ACTION_ATTR_RECIRC:
2980
2981 case OVS_ACTION_ATTR_TUNNEL_POP:
2982 case OVS_ACTION_ATTR_TUNNEL_PUSH:
2983 case OVS_ACTION_ATTR_TRUNC:
2984 case OVS_ACTION_ATTR_HASH:
2985 case OVS_ACTION_ATTR_CT:
1fe178d2 2986 case OVS_ACTION_ATTR_CT_CLEAR:
564230b6
PS
2987 case OVS_ACTION_ATTR_METER:
2988 case OVS_ACTION_ATTR_SET_MASKED:
2989 case OVS_ACTION_ATTR_SET:
2990 case OVS_ACTION_ATTR_PUSH_VLAN:
2991 case OVS_ACTION_ATTR_POP_VLAN:
2992 case OVS_ACTION_ATTR_PUSH_MPLS:
2993 case OVS_ACTION_ATTR_POP_MPLS:
2994 case OVS_ACTION_ATTR_PUSH_ETH:
2995 case OVS_ACTION_ATTR_POP_ETH:
f59cb331
YY
2996 case OVS_ACTION_ATTR_PUSH_NSH:
2997 case OVS_ACTION_ATTR_POP_NSH:
564230b6
PS
2998 case OVS_ACTION_ATTR_UNSPEC:
2999 case __OVS_ACTION_ATTR_MAX:
3000 default:
3001 break;
3002 }
3003 }
3004}