ofproto: Modularize netflow.
[cascardo/ovs.git] / ofproto / netflow.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 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 "netflow.h"
19 #include <arpa/inet.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include "byte-order.h"
24 #include "collectors.h"
25 #include "dpif.h"
26 #include "flow.h"
27 #include "lib/netflow.h"
28 #include "ofpbuf.h"
29 #include "ofproto.h"
30 #include "ofproto/netflow.h"
31 #include "packets.h"
32 #include "poll-loop.h"
33 #include "socket-util.h"
34 #include "timeval.h"
35 #include "util.h"
36 #include "vlog.h"
37
38 VLOG_DEFINE_THIS_MODULE(netflow);
39
40 struct 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. */
44     struct collectors *collectors; /* NetFlow collectors. */
45     bool add_id_to_iface;         /* Put the 7 least significiant bits of
46                                    * 'engine_id' into the most significant
47                                    * bits of the interface fields. */
48     uint32_t netflow_cnt;         /* Flow sequence number for NetFlow. */
49     struct ofpbuf packet;         /* NetFlow packet being accumulated. */
50     long long int active_timeout; /* Timeout for flows that are still active. */
51     long long int next_timeout;   /* Next scheduled active timeout. */
52     long long int reconfig_time;  /* When we reconfigured the timeouts. */
53
54     struct hmap flows;            /* Contains 'netflow_flows'. */
55 };
56
57 struct netflow_flow {
58     struct hmap_node hmap_node;
59
60     long long int last_expired;   /* Time this flow last timed out. */
61     long long int created;        /* Time flow was created since time out. */
62
63     ofp_port_t output_iface;      /* Output interface index. */
64     uint16_t tcp_flags;           /* Bitwise-OR of all TCP flags seen. */
65
66     ofp_port_t in_port;           /* Input port. */
67     ovs_be32 nw_src;              /* IPv4 source address. */
68     ovs_be32 nw_dst;              /* IPv4 destination address. */
69     uint8_t nw_tos;               /* IP ToS (including DSCP and ECN). */
70     uint8_t nw_proto;             /* IP protocol. */
71     ovs_be16 tp_src;              /* TCP/UDP/SCTP source port. */
72     ovs_be16 tp_dst;              /* TCP/UDP/SCTP destination port. */
73
74     uint64_t packet_count;        /* Packets from subrules. */
75     uint64_t byte_count;          /* Bytes from subrules. */
76     long long int used;           /* Last-used time (0 if never used). */
77 };
78
79 static struct netflow_flow *netflow_flow_lookup(const struct netflow *,
80                                                 const struct flow *);
81 static uint32_t netflow_flow_hash(const struct flow *);
82 static void netflow_expire__(struct netflow *, struct netflow_flow *);
83
84 void
85 netflow_mask_wc(struct flow *flow, struct flow_wildcards *wc)
86 {
87     if (flow->dl_type != htons(ETH_TYPE_IP)) {
88         return;
89     }
90     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
91     memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
92     memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
93     memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
94     memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
95     wc->masks.nw_tos |= IP_DSCP_MASK;
96 }
97
98 static void
99 gen_netflow_rec(struct netflow *nf, struct netflow_flow *nf_flow,
100                 uint32_t packet_count, uint32_t byte_count)
101 {
102     struct netflow_v5_header *nf_hdr;
103     struct netflow_v5_record *nf_rec;
104
105     if (!nf->packet.size) {
106         struct timespec now;
107
108         time_wall_timespec(&now);
109
110         nf_hdr = ofpbuf_put_zeros(&nf->packet, sizeof *nf_hdr);
111         nf_hdr->version = htons(NETFLOW_V5_VERSION);
112         nf_hdr->count = htons(0);
113         nf_hdr->sysuptime = htonl(time_msec() - nf->boot_time);
114         nf_hdr->unix_secs = htonl(now.tv_sec);
115         nf_hdr->unix_nsecs = htonl(now.tv_nsec);
116         nf_hdr->flow_seq = htonl(nf->netflow_cnt++);
117         nf_hdr->engine_type = nf->engine_type;
118         nf_hdr->engine_id = nf->engine_id;
119         nf_hdr->sampling_interval = htons(0);
120     }
121
122     nf_hdr = nf->packet.data;
123     nf_hdr->count = htons(ntohs(nf_hdr->count) + 1);
124
125     nf_rec = ofpbuf_put_zeros(&nf->packet, sizeof *nf_rec);
126     nf_rec->src_addr = nf_flow->nw_src;
127     nf_rec->dst_addr = nf_flow->nw_dst;
128     nf_rec->nexthop = htonl(0);
129     if (nf->add_id_to_iface) {
130         uint16_t iface = (nf->engine_id & 0x7f) << 9;
131         nf_rec->input = htons(iface | (ofp_to_u16(nf_flow->in_port) & 0x1ff));
132         nf_rec->output = htons(iface
133             | (ofp_to_u16(nf_flow->output_iface) & 0x1ff));
134     } else {
135         nf_rec->input = htons(ofp_to_u16(nf_flow->in_port));
136         nf_rec->output = htons(ofp_to_u16(nf_flow->output_iface));
137     }
138     nf_rec->packet_count = htonl(packet_count);
139     nf_rec->byte_count = htonl(byte_count);
140     nf_rec->init_time = htonl(nf_flow->created - nf->boot_time);
141     nf_rec->used_time = htonl(MAX(nf_flow->created, nf_flow->used)
142                              - nf->boot_time);
143     if (nf_flow->nw_proto == IPPROTO_ICMP) {
144         /* In NetFlow, the ICMP type and code are concatenated and
145          * placed in the 'dst_port' field. */
146         uint8_t type = ntohs(nf_flow->tp_src);
147         uint8_t code = ntohs(nf_flow->tp_dst);
148         nf_rec->src_port = htons(0);
149         nf_rec->dst_port = htons((type << 8) | code);
150     } else {
151         nf_rec->src_port = nf_flow->tp_src;
152         nf_rec->dst_port = nf_flow->tp_dst;
153     }
154     nf_rec->tcp_flags = (uint8_t) nf_flow->tcp_flags;
155     nf_rec->ip_proto = nf_flow->nw_proto;
156     nf_rec->ip_tos = nf_flow->nw_tos & IP_DSCP_MASK;
157
158     /* NetFlow messages are limited to 30 records. */
159     if (ntohs(nf_hdr->count) >= 30) {
160         netflow_run(nf);
161     }
162 }
163
164 void
165 netflow_flow_update(struct netflow *nf, struct flow *flow,
166                     ofp_port_t output_iface,
167                     const struct dpif_flow_stats *stats)
168 {
169     struct netflow_flow *nf_flow;
170     long long int used;
171
172     /* NetFlow only reports on IP packets. */
173     if (flow->dl_type != htons(ETH_TYPE_IP)) {
174         return;
175     }
176
177     nf_flow = netflow_flow_lookup(nf, flow);
178     if (!nf_flow) {
179         nf_flow = xzalloc(sizeof *nf_flow);
180         nf_flow->in_port = flow->in_port.ofp_port;
181         nf_flow->nw_src = flow->nw_src;
182         nf_flow->nw_dst = flow->nw_dst;
183         nf_flow->nw_tos = flow->nw_tos;
184         nf_flow->nw_proto = flow->nw_proto;
185         nf_flow->tp_src = flow->tp_src;
186         nf_flow->tp_dst = flow->tp_dst;
187         nf_flow->created = stats->used;
188         nf_flow->output_iface = output_iface;
189         hmap_insert(&nf->flows, &nf_flow->hmap_node, netflow_flow_hash(flow));
190     }
191
192     if (nf_flow->output_iface != output_iface) {
193         netflow_expire__(nf, nf_flow);
194         nf_flow->created = stats->used;
195         nf_flow->output_iface = output_iface;
196     }
197
198     nf_flow->packet_count += stats->n_packets;
199     nf_flow->byte_count += stats->n_bytes;
200     nf_flow->tcp_flags |= stats->tcp_flags;
201
202     used = MAX(nf_flow->used, stats->used);
203     if (nf_flow->used != used) {
204         nf_flow->used = used;
205         if (!nf->active_timeout || !nf_flow->last_expired
206             || nf->reconfig_time > nf_flow->last_expired) {
207             /* Keep the time updated to prevent a flood of expiration in
208              * the future. */
209             nf_flow->last_expired = time_msec();
210         }
211     }
212 }
213
214 static void
215 netflow_expire__(struct netflow *nf, struct netflow_flow *nf_flow)
216 {
217     uint64_t pkts, bytes;
218
219     pkts = nf_flow->packet_count;
220     bytes = nf_flow->byte_count;
221
222     nf_flow->last_expired += nf->active_timeout;
223
224     if (pkts == 0) {
225         return;
226     }
227
228     if ((bytes >> 32) <= 175) {
229         /* NetFlow v5 records are limited to 32-bit counters.  If we've wrapped
230          * a counter, send as multiple records so we don't lose track of any
231          * traffic.  We try to evenly distribute the packet and byte counters,
232          * so that the bytes-per-packet lengths don't look wonky across the
233          * records. */
234         while (bytes) {
235             int n_recs = (bytes + UINT32_MAX - 1) / UINT32_MAX;
236             uint32_t pkt_count = pkts / n_recs;
237             uint32_t byte_count = bytes / n_recs;
238
239             gen_netflow_rec(nf, nf_flow, pkt_count, byte_count);
240
241             pkts -= pkt_count;
242             bytes -= byte_count;
243         }
244     } else {
245         /* In 600 seconds, a 10GbE link can theoretically transmit 75 * 10**10
246          * == 175 * 2**32 bytes.  The byte counter is bigger than that, so it's
247          * probably a bug--for example, the netdev code uses UINT64_MAX to
248          * report "unknown value", and perhaps that has leaked through to here.
249          *
250          * We wouldn't want to hit the loop above in this case, because it
251          * would try to send up to UINT32_MAX netflow records, which would take
252          * a long time.
253          */
254         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
255
256         VLOG_WARN_RL(&rl, "impossible byte counter %"PRIu64, bytes);
257     }
258
259     /* Update flow tracking data. */
260     nf_flow->created = 0;
261     nf_flow->packet_count = 0;
262     nf_flow->byte_count = 0;
263     nf_flow->tcp_flags = 0;
264 }
265
266 void
267 netflow_expire(struct netflow *nf, struct flow *flow)
268 {
269     struct netflow_flow *nf_flow = netflow_flow_lookup(nf, flow);
270
271     if (nf_flow) {
272         netflow_expire__(nf, nf_flow);
273     }
274 }
275
276 void
277 netflow_flow_clear(struct netflow *nf, struct flow *flow)
278 {
279     struct netflow_flow *nf_flow = netflow_flow_lookup(nf, flow);
280
281     if (nf_flow) {
282         ovs_assert(!nf_flow->packet_count);
283         ovs_assert(!nf_flow->byte_count);
284         hmap_remove(&nf->flows, &nf_flow->hmap_node);
285         free(nf_flow);
286     }
287 }
288
289 /* Returns true if it's time to send out a round of NetFlow active timeouts,
290  * false otherwise. */
291 void
292 netflow_run(struct netflow *nf)
293 {
294     long long int now = time_msec();
295     struct netflow_flow *nf_flow, *next;
296
297     if (nf->packet.size) {
298         collectors_send(nf->collectors, nf->packet.data, nf->packet.size);
299         nf->packet.size = 0;
300     }
301
302     if (!nf->active_timeout || now < nf->next_timeout) {
303         return;
304     }
305
306     nf->next_timeout = now + 1000;
307
308     HMAP_FOR_EACH_SAFE (nf_flow, next, hmap_node, &nf->flows) {
309         if (now > nf_flow->last_expired + nf->active_timeout) {
310             bool idle = nf_flow->used < nf_flow->last_expired;
311             netflow_expire__(nf, nf_flow);
312
313             if (idle) {
314                 /* If the netflow_flow hasn't been used in a while, it's
315                  * possible the upper layer lost track of it. */
316                 hmap_remove(&nf->flows, &nf_flow->hmap_node);
317                 free(nf_flow);
318             }
319         }
320     }
321 }
322
323 void
324 netflow_wait(struct netflow *nf)
325 {
326     if (nf->active_timeout) {
327         poll_timer_wait_until(nf->next_timeout);
328     }
329     if (nf->packet.size) {
330         poll_immediate_wake();
331     }
332 }
333
334 int
335 netflow_set_options(struct netflow *nf,
336                     const struct netflow_options *nf_options)
337 {
338     int error = 0;
339     long long int old_timeout;
340
341     nf->engine_type = nf_options->engine_type;
342     nf->engine_id = nf_options->engine_id;
343     nf->add_id_to_iface = nf_options->add_id_to_iface;
344
345     collectors_destroy(nf->collectors);
346     collectors_create(&nf_options->collectors, 0, &nf->collectors);
347
348     old_timeout = nf->active_timeout;
349     if (nf_options->active_timeout >= 0) {
350         nf->active_timeout = nf_options->active_timeout;
351     } else {
352         nf->active_timeout = NF_ACTIVE_TIMEOUT_DEFAULT;
353     }
354     nf->active_timeout *= 1000;
355     if (old_timeout != nf->active_timeout) {
356         nf->reconfig_time = time_msec();
357         nf->next_timeout = time_msec();
358     }
359
360     return error;
361 }
362
363 struct netflow *
364 netflow_create(void)
365 {
366     struct netflow *nf = xzalloc(sizeof *nf);
367     nf->engine_type = 0;
368     nf->engine_id = 0;
369     nf->boot_time = time_msec();
370     nf->collectors = NULL;
371     nf->add_id_to_iface = false;
372     nf->netflow_cnt = 0;
373     hmap_init(&nf->flows);
374     ofpbuf_init(&nf->packet, 1500);
375     return nf;
376 }
377
378 void
379 netflow_destroy(struct netflow *nf)
380 {
381     if (nf) {
382         ofpbuf_uninit(&nf->packet);
383         collectors_destroy(nf->collectors);
384         free(nf);
385     }
386 }
387 \f
388 /* Helpers. */
389
390 static struct netflow_flow *
391 netflow_flow_lookup(const struct netflow *nf, const struct flow *flow)
392 {
393     struct netflow_flow *nf_flow;
394
395     HMAP_FOR_EACH_WITH_HASH (nf_flow, hmap_node, netflow_flow_hash(flow),
396                              &nf->flows) {
397         if (flow->in_port.ofp_port == nf_flow->in_port
398             && flow->nw_src == nf_flow->nw_src
399             && flow->nw_dst == nf_flow->nw_dst
400             && flow->nw_tos == nf_flow->nw_tos
401             && flow->nw_proto == nf_flow->nw_proto
402             && flow->tp_src == nf_flow->tp_src
403             && flow->tp_dst == nf_flow->tp_dst) {
404             return nf_flow;
405         }
406     }
407
408     return NULL;
409 }
410
411 static uint32_t
412 netflow_flow_hash(const struct flow *flow)
413 {
414     uint32_t hash = 0;
415
416     hash = mhash_add(hash, (OVS_FORCE uint32_t) flow->in_port.ofp_port);
417     hash = mhash_add(hash, ntohl(flow->nw_src));
418     hash = mhash_add(hash, ntohl(flow->nw_dst));
419     hash = mhash_add(hash, flow->nw_tos);
420     hash = mhash_add(hash, flow->nw_proto);
421     hash = mhash_add(hash, ntohs(flow->tp_src));
422     hash = mhash_add(hash, ntohs(flow->tp_dst));
423
424     return mhash_finish(hash, 28);
425 }