]> git.proxmox.com Git - ovs.git/blame - ofproto/netflow.c
ovs-router: Fix sparse warning
[ovs.git] / ofproto / netflow.c
CommitLineData
064af421 1/*
8917f72c 2 * Copyright (c) 2008, 2009, 2010, 2011, 2013, 2014 Nicira, Inc.
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"
8bfaca5b 25#include "dpif.h"
064af421 26#include "flow.h"
9c8e276e 27#include "lib/netflow.h"
064af421
BP
28#include "ofpbuf.h"
29#include "ofproto.h"
9c8e276e 30#include "ofproto/netflow.h"
064af421 31#include "packets.h"
6fca1ffb 32#include "poll-loop.h"
064af421 33#include "socket-util.h"
064af421
BP
34#include "timeval.h"
35#include "util.h"
5136ce49 36#include "vlog.h"
064af421 37
d98e6007 38VLOG_DEFINE_THIS_MODULE(netflow);
064af421 39
064af421
BP
40struct netflow {
41 uint8_t engine_type; /* Value of engine_type to use. */
42 uint8_t engine_id; /* Value of engine_id to use. */
43 long long int boot_time; /* Time when netflow_create() was called. */
6bab3798 44 struct collectors *collectors; /* NetFlow collectors. */
ec9f40dc
AH
45 bool add_id_to_iface; /* Put the 7 least significiant bits of
46 * 'engine_id' into the most significant
064af421
BP
47 * bits of the interface fields. */
48 uint32_t netflow_cnt; /* Flow sequence number for NetFlow. */
49 struct ofpbuf packet; /* NetFlow packet being accumulated. */
0193b2af 50 long long int active_timeout; /* Timeout for flows that are still active. */
6fca1ffb 51 long long int next_timeout; /* Next scheduled active timeout. */
0193b2af 52 long long int reconfig_time; /* When we reconfigured the timeouts. */
8bfaca5b
EJ
53
54 struct hmap flows; /* Contains 'netflow_flows'. */
8e407f27 55
37bec3d3 56 struct ovs_refcount ref_cnt;
8bfaca5b
EJ
57};
58
59struct netflow_flow {
60 struct hmap_node hmap_node;
61
62 long long int last_expired; /* Time this flow last timed out. */
63 long long int created; /* Time flow was created since time out. */
64
65 ofp_port_t output_iface; /* Output interface index. */
66 uint16_t tcp_flags; /* Bitwise-OR of all TCP flags seen. */
67
68 ofp_port_t in_port; /* Input port. */
69 ovs_be32 nw_src; /* IPv4 source address. */
70 ovs_be32 nw_dst; /* IPv4 destination address. */
71 uint8_t nw_tos; /* IP ToS (including DSCP and ECN). */
72 uint8_t nw_proto; /* IP protocol. */
73 ovs_be16 tp_src; /* TCP/UDP/SCTP source port. */
74 ovs_be16 tp_dst; /* TCP/UDP/SCTP destination port. */
75
76 uint64_t packet_count; /* Packets from subrules. */
77 uint64_t byte_count; /* Bytes from subrules. */
78 long long int used; /* Last-used time (0 if never used). */
064af421
BP
79};
80
8e407f27 81static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
6750f628 82static atomic_count netflow_count = ATOMIC_COUNT_INIT(0);
8e407f27 83
8bfaca5b 84static struct netflow_flow *netflow_flow_lookup(const struct netflow *,
8e407f27
EJ
85 const struct flow *)
86 OVS_REQUIRES(mutex);
8bfaca5b 87static uint32_t netflow_flow_hash(const struct flow *);
8e407f27
EJ
88static void netflow_expire__(struct netflow *, struct netflow_flow *)
89 OVS_REQUIRES(mutex);
90static void netflow_run__(struct netflow *) OVS_REQUIRES(mutex);
8bfaca5b 91
1dd35f8a 92void
9b658910 93netflow_mask_wc(struct flow *flow, struct flow_wildcards *wc)
1dd35f8a 94{
9b658910
JP
95 if (flow->dl_type != htons(ETH_TYPE_IP)) {
96 return;
97 }
1dd35f8a
JP
98 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
99 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
100 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
d8d9c698 101 flow_unwildcard_tp_ports(flow, wc);
1dd35f8a
JP
102 wc->masks.nw_tos |= IP_DSCP_MASK;
103}
104
f79cb67e
JP
105static void
106gen_netflow_rec(struct netflow *nf, struct netflow_flow *nf_flow,
f79cb67e 107 uint32_t packet_count, uint32_t byte_count)
8e407f27 108 OVS_REQUIRES(mutex)
064af421
BP
109{
110 struct netflow_v5_header *nf_hdr;
111 struct netflow_v5_record *nf_rec;
0193b2af 112
1f317cb5 113 if (!ofpbuf_size(&nf->packet)) {
f79cb67e 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
1f317cb5 130 nf_hdr = ofpbuf_data(&nf->packet);
064af421
BP
131 nf_hdr->count = htons(ntohs(nf_hdr->count) + 1);
132
133 nf_rec = ofpbuf_put_zeros(&nf->packet, sizeof *nf_rec);
8bfaca5b
EJ
134 nf_rec->src_addr = nf_flow->nw_src;
135 nf_rec->dst_addr = nf_flow->nw_dst;
d84d4b88 136 nf_rec->nexthop = htonl(0);
064af421
BP
137 if (nf->add_id_to_iface) {
138 uint16_t iface = (nf->engine_id & 0x7f) << 9;
8bfaca5b 139 nf_rec->input = htons(iface | (ofp_to_u16(nf_flow->in_port) & 0x1ff));
4e022ec0
AW
140 nf_rec->output = htons(iface
141 | (ofp_to_u16(nf_flow->output_iface) & 0x1ff));
064af421 142 } else {
8bfaca5b 143 nf_rec->input = htons(ofp_to_u16(nf_flow->in_port));
4e022ec0 144 nf_rec->output = htons(ofp_to_u16(nf_flow->output_iface));
064af421 145 }
f79cb67e
JP
146 nf_rec->packet_count = htonl(packet_count);
147 nf_rec->byte_count = htonl(byte_count);
0193b2af 148 nf_rec->init_time = htonl(nf_flow->created - nf->boot_time);
8bfaca5b 149 nf_rec->used_time = htonl(MAX(nf_flow->created, nf_flow->used)
064af421 150 - nf->boot_time);
8bfaca5b 151 if (nf_flow->nw_proto == IPPROTO_ICMP) {
064af421
BP
152 /* In NetFlow, the ICMP type and code are concatenated and
153 * placed in the 'dst_port' field. */
8bfaca5b
EJ
154 uint8_t type = ntohs(nf_flow->tp_src);
155 uint8_t code = ntohs(nf_flow->tp_dst);
064af421
BP
156 nf_rec->src_port = htons(0);
157 nf_rec->dst_port = htons((type << 8) | code);
158 } else {
8bfaca5b
EJ
159 nf_rec->src_port = nf_flow->tp_src;
160 nf_rec->dst_port = nf_flow->tp_dst;
064af421 161 }
8bfaca5b
EJ
162 nf_rec->tcp_flags = (uint8_t) nf_flow->tcp_flags;
163 nf_rec->ip_proto = nf_flow->nw_proto;
164 nf_rec->ip_tos = nf_flow->nw_tos & IP_DSCP_MASK;
0193b2af 165
f79cb67e
JP
166 /* NetFlow messages are limited to 30 records. */
167 if (ntohs(nf_hdr->count) >= 30) {
8e407f27 168 netflow_run__(nf);
f79cb67e
JP
169 }
170}
171
172void
230f02bc 173netflow_flow_update(struct netflow *nf, const struct flow *flow,
8bfaca5b
EJ
174 ofp_port_t output_iface,
175 const struct dpif_flow_stats *stats)
8e407f27 176 OVS_EXCLUDED(mutex)
f79cb67e 177{
8bfaca5b
EJ
178 struct netflow_flow *nf_flow;
179 long long int used;
180
181 /* NetFlow only reports on IP packets. */
182 if (flow->dl_type != htons(ETH_TYPE_IP)) {
183 return;
184 }
185
8e407f27 186 ovs_mutex_lock(&mutex);
8bfaca5b
EJ
187 nf_flow = netflow_flow_lookup(nf, flow);
188 if (!nf_flow) {
189 nf_flow = xzalloc(sizeof *nf_flow);
190 nf_flow->in_port = flow->in_port.ofp_port;
191 nf_flow->nw_src = flow->nw_src;
192 nf_flow->nw_dst = flow->nw_dst;
193 nf_flow->nw_tos = flow->nw_tos;
194 nf_flow->nw_proto = flow->nw_proto;
195 nf_flow->tp_src = flow->tp_src;
196 nf_flow->tp_dst = flow->tp_dst;
197 nf_flow->created = stats->used;
198 nf_flow->output_iface = output_iface;
199 hmap_insert(&nf->flows, &nf_flow->hmap_node, netflow_flow_hash(flow));
200 }
201
202 if (nf_flow->output_iface != output_iface) {
203 netflow_expire__(nf, nf_flow);
204 nf_flow->created = stats->used;
205 nf_flow->output_iface = output_iface;
206 }
207
208 nf_flow->packet_count += stats->n_packets;
209 nf_flow->byte_count += stats->n_bytes;
210 nf_flow->tcp_flags |= stats->tcp_flags;
211
212 used = MAX(nf_flow->used, stats->used);
213 if (nf_flow->used != used) {
214 nf_flow->used = used;
215 if (!nf->active_timeout || !nf_flow->last_expired
216 || nf->reconfig_time > nf_flow->last_expired) {
217 /* Keep the time updated to prevent a flood of expiration in
218 * the future. */
219 nf_flow->last_expired = time_msec();
220 }
221 }
8e407f27
EJ
222
223 ovs_mutex_unlock(&mutex);
8bfaca5b
EJ
224}
225
226static void
227netflow_expire__(struct netflow *nf, struct netflow_flow *nf_flow)
8e407f27 228 OVS_REQUIRES(mutex)
8bfaca5b
EJ
229{
230 uint64_t pkts, bytes;
231
232 pkts = nf_flow->packet_count;
233 bytes = nf_flow->byte_count;
f79cb67e
JP
234
235 nf_flow->last_expired += nf->active_timeout;
236
8bfaca5b 237 if (pkts == 0) {
f79cb67e
JP
238 return;
239 }
240
8bfaca5b 241 if ((bytes >> 32) <= 175) {
9ebc44ae
BP
242 /* NetFlow v5 records are limited to 32-bit counters. If we've wrapped
243 * a counter, send as multiple records so we don't lose track of any
244 * traffic. We try to evenly distribute the packet and byte counters,
245 * so that the bytes-per-packet lengths don't look wonky across the
246 * records. */
8bfaca5b
EJ
247 while (bytes) {
248 int n_recs = (bytes + UINT32_MAX - 1) / UINT32_MAX;
249 uint32_t pkt_count = pkts / n_recs;
250 uint32_t byte_count = bytes / n_recs;
9ebc44ae 251
8bfaca5b 252 gen_netflow_rec(nf, nf_flow, pkt_count, byte_count);
9ebc44ae 253
8bfaca5b
EJ
254 pkts -= pkt_count;
255 bytes -= byte_count;
9ebc44ae 256 }
9ebc44ae
BP
257 } else {
258 /* In 600 seconds, a 10GbE link can theoretically transmit 75 * 10**10
259 * == 175 * 2**32 bytes. The byte counter is bigger than that, so it's
260 * probably a bug--for example, the netdev code uses UINT64_MAX to
261 * report "unknown value", and perhaps that has leaked through to here.
262 *
263 * We wouldn't want to hit the loop above in this case, because it
264 * would try to send up to UINT32_MAX netflow records, which would take
265 * a long time.
266 */
267 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
268
8bfaca5b 269 VLOG_WARN_RL(&rl, "impossible byte counter %"PRIu64, bytes);
48f846e6 270 }
f79cb67e 271
0193b2af 272 /* Update flow tracking data. */
8bfaca5b
EJ
273 nf_flow->packet_count = 0;
274 nf_flow->byte_count = 0;
0193b2af 275 nf_flow->tcp_flags = 0;
064af421
BP
276}
277
8bfaca5b 278void
8e407f27 279netflow_flow_clear(struct netflow *nf, struct flow *flow) OVS_EXCLUDED(mutex)
8bfaca5b 280{
8e407f27 281 struct netflow_flow *nf_flow;
8bfaca5b 282
8e407f27
EJ
283 ovs_mutex_lock(&mutex);
284 nf_flow = netflow_flow_lookup(nf, flow);
8bfaca5b 285 if (nf_flow) {
3a208109 286 netflow_expire__(nf, nf_flow);
8bfaca5b
EJ
287 hmap_remove(&nf->flows, &nf_flow->hmap_node);
288 free(nf_flow);
289 }
8e407f27 290 ovs_mutex_unlock(&mutex);
8bfaca5b
EJ
291}
292
6fca1ffb
BP
293/* Returns true if it's time to send out a round of NetFlow active timeouts,
294 * false otherwise. */
8e407f27
EJ
295static void
296netflow_run__(struct netflow *nf) OVS_REQUIRES(mutex)
064af421 297{
8bfaca5b
EJ
298 long long int now = time_msec();
299 struct netflow_flow *nf_flow, *next;
300
1f317cb5
PS
301 if (ofpbuf_size(&nf->packet)) {
302 collectors_send(nf->collectors, ofpbuf_data(&nf->packet), ofpbuf_size(&nf->packet));
303 ofpbuf_set_size(&nf->packet, 0);
064af421 304 }
6fca1ffb 305
8bfaca5b
EJ
306 if (!nf->active_timeout || now < nf->next_timeout) {
307 return;
308 }
309
310 nf->next_timeout = now + 1000;
311
312 HMAP_FOR_EACH_SAFE (nf_flow, next, hmap_node, &nf->flows) {
313 if (now > nf_flow->last_expired + nf->active_timeout) {
314 bool idle = nf_flow->used < nf_flow->last_expired;
315 netflow_expire__(nf, nf_flow);
316
317 if (idle) {
318 /* If the netflow_flow hasn't been used in a while, it's
319 * possible the upper layer lost track of it. */
320 hmap_remove(&nf->flows, &nf_flow->hmap_node);
321 free(nf_flow);
322 }
323 }
6fca1ffb
BP
324 }
325}
326
327void
8e407f27 328netflow_run(struct netflow *nf)
6fca1ffb 329{
8e407f27
EJ
330 ovs_mutex_lock(&mutex);
331 netflow_run__(nf);
332 ovs_mutex_unlock(&mutex);
333}
334
335void
336netflow_wait(struct netflow *nf) OVS_EXCLUDED(mutex)
337{
338 ovs_mutex_lock(&mutex);
6fca1ffb
BP
339 if (nf->active_timeout) {
340 poll_timer_wait_until(nf->next_timeout);
341 }
1f317cb5 342 if (ofpbuf_size(&nf->packet)) {
6fca1ffb
BP
343 poll_immediate_wake();
344 }
8e407f27 345 ovs_mutex_unlock(&mutex);
064af421
BP
346}
347
348int
0193b2af
JG
349netflow_set_options(struct netflow *nf,
350 const struct netflow_options *nf_options)
8e407f27 351 OVS_EXCLUDED(mutex)
064af421 352{
064af421 353 int error = 0;
0193b2af
JG
354 long long int old_timeout;
355
8e407f27 356 ovs_mutex_lock(&mutex);
0193b2af
JG
357 nf->engine_type = nf_options->engine_type;
358 nf->engine_id = nf_options->engine_id;
359 nf->add_id_to_iface = nf_options->add_id_to_iface;
064af421 360
6bab3798
BP
361 collectors_destroy(nf->collectors);
362 collectors_create(&nf_options->collectors, 0, &nf->collectors);
064af421 363
0193b2af 364 old_timeout = nf->active_timeout;
e9e2856e 365 if (nf_options->active_timeout >= 0) {
0193b2af
JG
366 nf->active_timeout = nf_options->active_timeout;
367 } else {
e9e2856e 368 nf->active_timeout = NF_ACTIVE_TIMEOUT_DEFAULT;
0193b2af
JG
369 }
370 nf->active_timeout *= 1000;
371 if (old_timeout != nf->active_timeout) {
372 nf->reconfig_time = time_msec();
6fca1ffb 373 nf->next_timeout = time_msec();
0193b2af 374 }
8e407f27 375 ovs_mutex_unlock(&mutex);
0193b2af
JG
376
377 return error;
064af421
BP
378}
379
380struct netflow *
381netflow_create(void)
382{
6fca1ffb 383 struct netflow *nf = xzalloc(sizeof *nf);
936604c0 384
064af421
BP
385 nf->engine_type = 0;
386 nf->engine_id = 0;
387 nf->boot_time = time_msec();
6bab3798 388 nf->collectors = NULL;
064af421
BP
389 nf->add_id_to_iface = false;
390 nf->netflow_cnt = 0;
8bfaca5b 391 hmap_init(&nf->flows);
37bec3d3 392 ovs_refcount_init(&nf->ref_cnt);
064af421 393 ofpbuf_init(&nf->packet, 1500);
6750f628 394 atomic_count_inc(&netflow_count);
064af421
BP
395 return nf;
396}
397
8e407f27
EJ
398struct netflow *
399netflow_ref(const struct netflow *nf_)
064af421 400{
8e407f27 401 struct netflow *nf = CONST_CAST(struct netflow *, nf_);
064af421 402 if (nf) {
37bec3d3 403 ovs_refcount_ref(&nf->ref_cnt);
8e407f27
EJ
404 }
405 return nf;
406}
407
408void
409netflow_unref(struct netflow *nf)
410{
24f83812 411 if (nf && ovs_refcount_unref_relaxed(&nf->ref_cnt) == 1) {
6750f628 412 atomic_count_dec(&netflow_count);
6bab3798 413 collectors_destroy(nf->collectors);
936604c0 414 ofpbuf_uninit(&nf->packet);
064af421
BP
415 free(nf);
416 }
417}
936604c0 418
6750f628
JR
419/* Returns true if there exist any netflow objects, false otherwise.
420 * Callers must cope with transient false positives, i.e., there is no tight
421 * synchronization with the count and the actual existence of netflow objects.
422 */
936604c0
EJ
423bool
424netflow_exists(void)
425{
6750f628 426 return atomic_count_get(&netflow_count) > 0;
936604c0 427}
8bfaca5b
EJ
428\f
429/* Helpers. */
0193b2af 430
8bfaca5b
EJ
431static struct netflow_flow *
432netflow_flow_lookup(const struct netflow *nf, const struct flow *flow)
8e407f27 433 OVS_REQUIRES(mutex)
d6de72a1 434{
8bfaca5b
EJ
435 struct netflow_flow *nf_flow;
436
437 HMAP_FOR_EACH_WITH_HASH (nf_flow, hmap_node, netflow_flow_hash(flow),
438 &nf->flows) {
439 if (flow->in_port.ofp_port == nf_flow->in_port
440 && flow->nw_src == nf_flow->nw_src
441 && flow->nw_dst == nf_flow->nw_dst
442 && flow->nw_tos == nf_flow->nw_tos
443 && flow->nw_proto == nf_flow->nw_proto
444 && flow->tp_src == nf_flow->tp_src
445 && flow->tp_dst == nf_flow->tp_dst) {
446 return nf_flow;
447 }
0193b2af
JG
448 }
449
8bfaca5b 450 return NULL;
0193b2af
JG
451}
452
8bfaca5b
EJ
453static uint32_t
454netflow_flow_hash(const struct flow *flow)
0193b2af 455{
8bfaca5b 456 uint32_t hash = 0;
0193b2af 457
33c6a1b9
JR
458 hash = hash_add(hash, (OVS_FORCE uint32_t) flow->in_port.ofp_port);
459 hash = hash_add(hash, ntohl(flow->nw_src));
460 hash = hash_add(hash, ntohl(flow->nw_dst));
461 hash = hash_add(hash, flow->nw_tos);
462 hash = hash_add(hash, flow->nw_proto);
463 hash = hash_add(hash, ntohs(flow->tp_src));
464 hash = hash_add(hash, ntohs(flow->tp_dst));
465
466 return hash_finish(hash, 28);
0193b2af 467}