]> git.proxmox.com Git - ovs.git/blame - ofproto/netflow.c
packets: New function snap_compose(); rename compose_packet() for consistency.
[ovs.git] / ofproto / netflow.c
CommitLineData
064af421 1/*
81e2083f 2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
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.
064af421
BP
15 */
16
17#include <config.h>
18#include "netflow.h"
19#include <arpa/inet.h>
20#include <errno.h>
21#include <stdlib.h>
22#include <unistd.h>
10a24935 23#include "byte-order.h"
6bab3798 24#include "collectors.h"
064af421
BP
25#include "flow.h"
26#include "netflow.h"
27#include "ofpbuf.h"
28#include "ofproto.h"
29#include "packets.h"
30#include "socket-util.h"
064af421
BP
31#include "timeval.h"
32#include "util.h"
5136ce49 33#include "vlog.h"
064af421 34
d98e6007 35VLOG_DEFINE_THIS_MODULE(netflow);
064af421
BP
36
37#define NETFLOW_V5_VERSION 5
38
39/* Every NetFlow v5 message contains the header that follows. This is
40 * followed by up to thirty records that describe a terminating flow.
41 * We only send a single record per NetFlow message.
42 */
43struct netflow_v5_header {
44 uint16_t version; /* NetFlow version is 5. */
45 uint16_t count; /* Number of records in this message. */
46 uint32_t sysuptime; /* System uptime in milliseconds. */
47 uint32_t unix_secs; /* Number of seconds since Unix epoch. */
48 uint32_t unix_nsecs; /* Number of residual nanoseconds
49 after epoch seconds. */
50 uint32_t flow_seq; /* Number of flows since sending
51 messages began. */
52 uint8_t engine_type; /* Engine type. */
53 uint8_t engine_id; /* Engine id. */
54 uint16_t sampling_interval; /* Set to zero. */
55};
56BUILD_ASSERT_DECL(sizeof(struct netflow_v5_header) == 24);
57
58/* A NetFlow v5 description of a terminating flow. It is preceded by a
59 * NetFlow v5 header.
60 */
61struct netflow_v5_record {
62 uint32_t src_addr; /* Source IP address. */
63 uint32_t dst_addr; /* Destination IP address. */
64 uint32_t nexthop; /* IP address of next hop. Set to 0. */
65 uint16_t input; /* Input interface index. */
66 uint16_t output; /* Output interface index. */
67 uint32_t packet_count; /* Number of packets. */
68 uint32_t byte_count; /* Number of bytes. */
69 uint32_t init_time; /* Value of sysuptime on first packet. */
70 uint32_t used_time; /* Value of sysuptime on last packet. */
71
72 /* The 'src_port' and 'dst_port' identify the source and destination
73 * port, respectively, for TCP and UDP. For ICMP, the high-order
74 * byte identifies the type and low-order byte identifies the code
75 * in the 'dst_port' field. */
76 uint16_t src_port;
77 uint16_t dst_port;
78
79 uint8_t pad1;
80 uint8_t tcp_flags; /* Union of seen TCP flags. */
81 uint8_t ip_proto; /* IP protocol. */
82 uint8_t ip_tos; /* IP TOS value. */
83 uint16_t src_as; /* Source AS ID. Set to 0. */
84 uint16_t dst_as; /* Destination AS ID. Set to 0. */
85 uint8_t src_mask; /* Source mask bits. Set to 0. */
86 uint8_t dst_mask; /* Destination mask bits. Set to 0. */
87 uint8_t pad[2];
88};
89BUILD_ASSERT_DECL(sizeof(struct netflow_v5_record) == 48);
90
91struct netflow {
92 uint8_t engine_type; /* Value of engine_type to use. */
93 uint8_t engine_id; /* Value of engine_id to use. */
94 long long int boot_time; /* Time when netflow_create() was called. */
6bab3798 95 struct collectors *collectors; /* NetFlow collectors. */
d295e8e9
JP
96 bool add_id_to_iface; /* Put the 7 least signficiant bits of
97 * 'engine_id' into the most signficant
064af421
BP
98 * bits of the interface fields. */
99 uint32_t netflow_cnt; /* Flow sequence number for NetFlow. */
100 struct ofpbuf packet; /* NetFlow packet being accumulated. */
0193b2af
JG
101 long long int active_timeout; /* Timeout for flows that are still active. */
102 long long int reconfig_time; /* When we reconfigured the timeouts. */
064af421
BP
103};
104
f79cb67e
JP
105static void
106gen_netflow_rec(struct netflow *nf, struct netflow_flow *nf_flow,
107 struct ofexpired *expired,
108 uint32_t packet_count, uint32_t byte_count)
064af421
BP
109{
110 struct netflow_v5_header *nf_hdr;
111 struct netflow_v5_record *nf_rec;
0193b2af 112
f79cb67e
JP
113 if (!nf->packet.size) {
114 struct timespec now;
064af421 115
f79cb67e 116 time_wall_timespec(&now);
064af421 117
064af421
BP
118 nf_hdr = ofpbuf_put_zeros(&nf->packet, sizeof *nf_hdr);
119 nf_hdr->version = htons(NETFLOW_V5_VERSION);
120 nf_hdr->count = htons(0);
121 nf_hdr->sysuptime = htonl(time_msec() - nf->boot_time);
122 nf_hdr->unix_secs = htonl(now.tv_sec);
c73814a3 123 nf_hdr->unix_nsecs = htonl(now.tv_nsec);
064af421
BP
124 nf_hdr->flow_seq = htonl(nf->netflow_cnt++);
125 nf_hdr->engine_type = nf->engine_type;
126 nf_hdr->engine_id = nf->engine_id;
127 nf_hdr->sampling_interval = htons(0);
128 }
129
130 nf_hdr = nf->packet.data;
131 nf_hdr->count = htons(ntohs(nf_hdr->count) + 1);
132
133 nf_rec = ofpbuf_put_zeros(&nf->packet, sizeof *nf_rec);
134 nf_rec->src_addr = expired->flow.nw_src;
135 nf_rec->dst_addr = expired->flow.nw_dst;
136 nf_rec->nexthop = htons(0);
137 if (nf->add_id_to_iface) {
138 uint16_t iface = (nf->engine_id & 0x7f) << 9;
139 nf_rec->input = htons(iface | (expired->flow.in_port & 0x1ff));
0193b2af 140 nf_rec->output = htons(iface | (nf_flow->output_iface & 0x1ff));
064af421
BP
141 } else {
142 nf_rec->input = htons(expired->flow.in_port);
0193b2af 143 nf_rec->output = htons(nf_flow->output_iface);
064af421 144 }
f79cb67e
JP
145 nf_rec->packet_count = htonl(packet_count);
146 nf_rec->byte_count = htonl(byte_count);
0193b2af
JG
147 nf_rec->init_time = htonl(nf_flow->created - nf->boot_time);
148 nf_rec->used_time = htonl(MAX(nf_flow->created, expired->used)
064af421 149 - nf->boot_time);
6767a2cc 150 if (expired->flow.nw_proto == IPPROTO_ICMP) {
064af421
BP
151 /* In NetFlow, the ICMP type and code are concatenated and
152 * placed in the 'dst_port' field. */
153 uint8_t type = ntohs(expired->flow.tp_src);
154 uint8_t code = ntohs(expired->flow.tp_dst);
155 nf_rec->src_port = htons(0);
156 nf_rec->dst_port = htons((type << 8) | code);
157 } else {
158 nf_rec->src_port = expired->flow.tp_src;
159 nf_rec->dst_port = expired->flow.tp_dst;
160 }
0193b2af 161 nf_rec->tcp_flags = nf_flow->tcp_flags;
064af421 162 nf_rec->ip_proto = expired->flow.nw_proto;
abfec865 163 nf_rec->ip_tos = expired->flow.nw_tos;
0193b2af 164
f79cb67e
JP
165 /* NetFlow messages are limited to 30 records. */
166 if (ntohs(nf_hdr->count) >= 30) {
167 netflow_run(nf);
168 }
169}
170
171void
172netflow_expire(struct netflow *nf, struct netflow_flow *nf_flow,
173 struct ofexpired *expired)
174{
175 uint64_t pkt_delta = expired->packet_count - nf_flow->packet_count_off;
176 uint64_t byte_delta = expired->byte_count - nf_flow->byte_count_off;
177
178 nf_flow->last_expired += nf->active_timeout;
179
180 /* NetFlow only reports on IP packets and we should only report flows
181 * that actually have traffic. */
182 if (expired->flow.dl_type != htons(ETH_TYPE_IP) || pkt_delta == 0) {
183 return;
184 }
185
9ebc44ae
BP
186 if ((byte_delta >> 32) <= 175) {
187 /* NetFlow v5 records are limited to 32-bit counters. If we've wrapped
188 * a counter, send as multiple records so we don't lose track of any
189 * traffic. We try to evenly distribute the packet and byte counters,
190 * so that the bytes-per-packet lengths don't look wonky across the
191 * records. */
c06955bc
JP
192 while (byte_delta) {
193 int n_recs = (byte_delta + UINT32_MAX - 1) / UINT32_MAX;
9ebc44ae
BP
194 uint32_t pkt_count = pkt_delta / n_recs;
195 uint32_t byte_count = byte_delta / n_recs;
196
197 gen_netflow_rec(nf, nf_flow, expired, pkt_count, byte_count);
198
199 pkt_delta -= pkt_count;
200 byte_delta -= byte_count;
201 }
9ebc44ae
BP
202 } else {
203 /* In 600 seconds, a 10GbE link can theoretically transmit 75 * 10**10
204 * == 175 * 2**32 bytes. The byte counter is bigger than that, so it's
205 * probably a bug--for example, the netdev code uses UINT64_MAX to
206 * report "unknown value", and perhaps that has leaked through to here.
207 *
208 * We wouldn't want to hit the loop above in this case, because it
209 * would try to send up to UINT32_MAX netflow records, which would take
210 * a long time.
211 */
212 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
213
214 VLOG_WARN_RL(&rl, "impossible byte counter %"PRIu64, byte_delta);
48f846e6 215 }
f79cb67e 216
0193b2af
JG
217 /* Update flow tracking data. */
218 nf_flow->created = 0;
219 nf_flow->packet_count_off = expired->packet_count;
220 nf_flow->byte_count_off = expired->byte_count;
221 nf_flow->tcp_flags = 0;
064af421
BP
222}
223
224void
225netflow_run(struct netflow *nf)
226{
6bab3798
BP
227 if (nf->packet.size) {
228 collectors_send(nf->collectors, nf->packet.data, nf->packet.size);
229 nf->packet.size = 0;
064af421 230 }
064af421
BP
231}
232
233int
0193b2af
JG
234netflow_set_options(struct netflow *nf,
235 const struct netflow_options *nf_options)
064af421 236{
064af421 237 int error = 0;
0193b2af
JG
238 long long int old_timeout;
239
240 nf->engine_type = nf_options->engine_type;
241 nf->engine_id = nf_options->engine_id;
242 nf->add_id_to_iface = nf_options->add_id_to_iface;
064af421 243
6bab3798
BP
244 collectors_destroy(nf->collectors);
245 collectors_create(&nf_options->collectors, 0, &nf->collectors);
064af421 246
0193b2af 247 old_timeout = nf->active_timeout;
e9e2856e 248 if (nf_options->active_timeout >= 0) {
0193b2af
JG
249 nf->active_timeout = nf_options->active_timeout;
250 } else {
e9e2856e 251 nf->active_timeout = NF_ACTIVE_TIMEOUT_DEFAULT;
0193b2af
JG
252 }
253 nf->active_timeout *= 1000;
254 if (old_timeout != nf->active_timeout) {
255 nf->reconfig_time = time_msec();
256 }
257
258 return error;
064af421
BP
259}
260
261struct netflow *
262netflow_create(void)
263{
264 struct netflow *nf = xmalloc(sizeof *nf);
265 nf->engine_type = 0;
266 nf->engine_id = 0;
267 nf->boot_time = time_msec();
6bab3798 268 nf->collectors = NULL;
064af421
BP
269 nf->add_id_to_iface = false;
270 nf->netflow_cnt = 0;
271 ofpbuf_init(&nf->packet, 1500);
272 return nf;
273}
274
275void
276netflow_destroy(struct netflow *nf)
277{
278 if (nf) {
279 ofpbuf_uninit(&nf->packet);
6bab3798 280 collectors_destroy(nf->collectors);
064af421
BP
281 free(nf);
282 }
283}
0193b2af 284
d6de72a1
BP
285/* Initializes a new 'nf_flow' given that the caller has already cleared it to
286 * all-zero-bits. */
287void
288netflow_flow_init(struct netflow_flow *nf_flow OVS_UNUSED)
289{
290 /* Nothing to do. */
291}
292
0193b2af
JG
293void
294netflow_flow_clear(struct netflow_flow *nf_flow)
295{
296 uint16_t output_iface = nf_flow->output_iface;
297
298 memset(nf_flow, 0, sizeof *nf_flow);
299 nf_flow->output_iface = output_iface;
300}
301
302void
303netflow_flow_update_time(struct netflow *nf, struct netflow_flow *nf_flow,
304 long long int used)
305{
306 if (!nf_flow->created) {
307 nf_flow->created = used;
308 }
309
310 if (!nf || !nf->active_timeout || !nf_flow->last_expired ||
311 nf->reconfig_time > nf_flow->last_expired) {
312 /* Keep the time updated to prevent a flood of expiration in
313 * the future. */
314 nf_flow->last_expired = time_msec();
315 }
316}
317
318void
abfec865 319netflow_flow_update_flags(struct netflow_flow *nf_flow, uint8_t tcp_flags)
0193b2af 320{
0193b2af
JG
321 nf_flow->tcp_flags |= tcp_flags;
322}
323
324bool
325netflow_active_timeout_expired(struct netflow *nf, struct netflow_flow *nf_flow)
326{
327 if (nf->active_timeout) {
328 return time_msec() > nf_flow->last_expired + nf->active_timeout;
329 }
330
331 return false;
332}