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