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