ofproto: Replace reval_seq with a struct seq.
[sliver-openvswitch.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     atomic_int ref_cnt;
57 };
58
59 struct 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). */
79 };
80
81 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
82 static atomic_uint netflow_count = ATOMIC_VAR_INIT(0);
83
84 static struct netflow_flow *netflow_flow_lookup(const struct netflow *,
85                                                 const struct flow *)
86     OVS_REQUIRES(mutex);
87 static uint32_t netflow_flow_hash(const struct flow *);
88 static void netflow_expire__(struct netflow *, struct netflow_flow *)
89     OVS_REQUIRES(mutex);
90 static void netflow_run__(struct netflow *) OVS_REQUIRES(mutex);
91
92 void
93 netflow_mask_wc(struct flow *flow, struct flow_wildcards *wc)
94 {
95     if (flow->dl_type != htons(ETH_TYPE_IP)) {
96         return;
97     }
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);
101     memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
102     memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
103     wc->masks.nw_tos |= IP_DSCP_MASK;
104 }
105
106 static void
107 gen_netflow_rec(struct netflow *nf, struct netflow_flow *nf_flow,
108                 uint32_t packet_count, uint32_t byte_count)
109     OVS_REQUIRES(mutex)
110 {
111     struct netflow_v5_header *nf_hdr;
112     struct netflow_v5_record *nf_rec;
113
114     if (!nf->packet.size) {
115         struct timespec now;
116
117         time_wall_timespec(&now);
118
119         nf_hdr = ofpbuf_put_zeros(&nf->packet, sizeof *nf_hdr);
120         nf_hdr->version = htons(NETFLOW_V5_VERSION);
121         nf_hdr->count = htons(0);
122         nf_hdr->sysuptime = htonl(time_msec() - nf->boot_time);
123         nf_hdr->unix_secs = htonl(now.tv_sec);
124         nf_hdr->unix_nsecs = htonl(now.tv_nsec);
125         nf_hdr->flow_seq = htonl(nf->netflow_cnt++);
126         nf_hdr->engine_type = nf->engine_type;
127         nf_hdr->engine_id = nf->engine_id;
128         nf_hdr->sampling_interval = htons(0);
129     }
130
131     nf_hdr = nf->packet.data;
132     nf_hdr->count = htons(ntohs(nf_hdr->count) + 1);
133
134     nf_rec = ofpbuf_put_zeros(&nf->packet, sizeof *nf_rec);
135     nf_rec->src_addr = nf_flow->nw_src;
136     nf_rec->dst_addr = nf_flow->nw_dst;
137     nf_rec->nexthop = htonl(0);
138     if (nf->add_id_to_iface) {
139         uint16_t iface = (nf->engine_id & 0x7f) << 9;
140         nf_rec->input = htons(iface | (ofp_to_u16(nf_flow->in_port) & 0x1ff));
141         nf_rec->output = htons(iface
142             | (ofp_to_u16(nf_flow->output_iface) & 0x1ff));
143     } else {
144         nf_rec->input = htons(ofp_to_u16(nf_flow->in_port));
145         nf_rec->output = htons(ofp_to_u16(nf_flow->output_iface));
146     }
147     nf_rec->packet_count = htonl(packet_count);
148     nf_rec->byte_count = htonl(byte_count);
149     nf_rec->init_time = htonl(nf_flow->created - nf->boot_time);
150     nf_rec->used_time = htonl(MAX(nf_flow->created, nf_flow->used)
151                              - nf->boot_time);
152     if (nf_flow->nw_proto == IPPROTO_ICMP) {
153         /* In NetFlow, the ICMP type and code are concatenated and
154          * placed in the 'dst_port' field. */
155         uint8_t type = ntohs(nf_flow->tp_src);
156         uint8_t code = ntohs(nf_flow->tp_dst);
157         nf_rec->src_port = htons(0);
158         nf_rec->dst_port = htons((type << 8) | code);
159     } else {
160         nf_rec->src_port = nf_flow->tp_src;
161         nf_rec->dst_port = nf_flow->tp_dst;
162     }
163     nf_rec->tcp_flags = (uint8_t) nf_flow->tcp_flags;
164     nf_rec->ip_proto = nf_flow->nw_proto;
165     nf_rec->ip_tos = nf_flow->nw_tos & IP_DSCP_MASK;
166
167     /* NetFlow messages are limited to 30 records. */
168     if (ntohs(nf_hdr->count) >= 30) {
169         netflow_run__(nf);
170     }
171 }
172
173 void
174 netflow_flow_update(struct netflow *nf, struct flow *flow,
175                     ofp_port_t output_iface,
176                     const struct dpif_flow_stats *stats)
177     OVS_EXCLUDED(mutex)
178 {
179     struct netflow_flow *nf_flow;
180     long long int used;
181
182     /* NetFlow only reports on IP packets. */
183     if (flow->dl_type != htons(ETH_TYPE_IP)) {
184         return;
185     }
186
187     ovs_mutex_lock(&mutex);
188     nf_flow = netflow_flow_lookup(nf, flow);
189     if (!nf_flow) {
190         nf_flow = xzalloc(sizeof *nf_flow);
191         nf_flow->in_port = flow->in_port.ofp_port;
192         nf_flow->nw_src = flow->nw_src;
193         nf_flow->nw_dst = flow->nw_dst;
194         nf_flow->nw_tos = flow->nw_tos;
195         nf_flow->nw_proto = flow->nw_proto;
196         nf_flow->tp_src = flow->tp_src;
197         nf_flow->tp_dst = flow->tp_dst;
198         nf_flow->created = stats->used;
199         nf_flow->output_iface = output_iface;
200         hmap_insert(&nf->flows, &nf_flow->hmap_node, netflow_flow_hash(flow));
201     }
202
203     if (nf_flow->output_iface != output_iface) {
204         netflow_expire__(nf, nf_flow);
205         nf_flow->created = stats->used;
206         nf_flow->output_iface = output_iface;
207     }
208
209     nf_flow->packet_count += stats->n_packets;
210     nf_flow->byte_count += stats->n_bytes;
211     nf_flow->tcp_flags |= stats->tcp_flags;
212
213     used = MAX(nf_flow->used, stats->used);
214     if (nf_flow->used != used) {
215         nf_flow->used = used;
216         if (!nf->active_timeout || !nf_flow->last_expired
217             || nf->reconfig_time > nf_flow->last_expired) {
218             /* Keep the time updated to prevent a flood of expiration in
219              * the future. */
220             nf_flow->last_expired = time_msec();
221         }
222     }
223
224     ovs_mutex_unlock(&mutex);
225 }
226
227 static void
228 netflow_expire__(struct netflow *nf, struct netflow_flow *nf_flow)
229     OVS_REQUIRES(mutex)
230 {
231     uint64_t pkts, bytes;
232
233     pkts = nf_flow->packet_count;
234     bytes = nf_flow->byte_count;
235
236     nf_flow->last_expired += nf->active_timeout;
237
238     if (pkts == 0) {
239         return;
240     }
241
242     if ((bytes >> 32) <= 175) {
243         /* NetFlow v5 records are limited to 32-bit counters.  If we've wrapped
244          * a counter, send as multiple records so we don't lose track of any
245          * traffic.  We try to evenly distribute the packet and byte counters,
246          * so that the bytes-per-packet lengths don't look wonky across the
247          * records. */
248         while (bytes) {
249             int n_recs = (bytes + UINT32_MAX - 1) / UINT32_MAX;
250             uint32_t pkt_count = pkts / n_recs;
251             uint32_t byte_count = bytes / n_recs;
252
253             gen_netflow_rec(nf, nf_flow, pkt_count, byte_count);
254
255             pkts -= pkt_count;
256             bytes -= byte_count;
257         }
258     } else {
259         /* In 600 seconds, a 10GbE link can theoretically transmit 75 * 10**10
260          * == 175 * 2**32 bytes.  The byte counter is bigger than that, so it's
261          * probably a bug--for example, the netdev code uses UINT64_MAX to
262          * report "unknown value", and perhaps that has leaked through to here.
263          *
264          * We wouldn't want to hit the loop above in this case, because it
265          * would try to send up to UINT32_MAX netflow records, which would take
266          * a long time.
267          */
268         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
269
270         VLOG_WARN_RL(&rl, "impossible byte counter %"PRIu64, bytes);
271     }
272
273     /* Update flow tracking data. */
274     nf_flow->created = 0;
275     nf_flow->packet_count = 0;
276     nf_flow->byte_count = 0;
277     nf_flow->tcp_flags = 0;
278 }
279
280 void
281 netflow_expire(struct netflow *nf, struct flow *flow) OVS_EXCLUDED(mutex)
282 {
283     struct netflow_flow *nf_flow;
284
285     ovs_mutex_lock(&mutex);
286     nf_flow = netflow_flow_lookup(nf, flow);
287     if (nf_flow) {
288         netflow_expire__(nf, nf_flow);
289     }
290     ovs_mutex_unlock(&mutex);
291 }
292
293 void
294 netflow_flow_clear(struct netflow *nf, struct flow *flow) OVS_EXCLUDED(mutex)
295 {
296     struct netflow_flow *nf_flow;
297
298     ovs_mutex_lock(&mutex);
299     nf_flow = netflow_flow_lookup(nf, flow);
300     if (nf_flow) {
301         ovs_assert(!nf_flow->packet_count);
302         ovs_assert(!nf_flow->byte_count);
303         hmap_remove(&nf->flows, &nf_flow->hmap_node);
304         free(nf_flow);
305     }
306     ovs_mutex_unlock(&mutex);
307 }
308
309 /* Returns true if it's time to send out a round of NetFlow active timeouts,
310  * false otherwise. */
311 static void
312 netflow_run__(struct netflow *nf) OVS_REQUIRES(mutex)
313 {
314     long long int now = time_msec();
315     struct netflow_flow *nf_flow, *next;
316
317     if (nf->packet.size) {
318         collectors_send(nf->collectors, nf->packet.data, nf->packet.size);
319         nf->packet.size = 0;
320     }
321
322     if (!nf->active_timeout || now < nf->next_timeout) {
323         return;
324     }
325
326     nf->next_timeout = now + 1000;
327
328     HMAP_FOR_EACH_SAFE (nf_flow, next, hmap_node, &nf->flows) {
329         if (now > nf_flow->last_expired + nf->active_timeout) {
330             bool idle = nf_flow->used < nf_flow->last_expired;
331             netflow_expire__(nf, nf_flow);
332
333             if (idle) {
334                 /* If the netflow_flow hasn't been used in a while, it's
335                  * possible the upper layer lost track of it. */
336                 hmap_remove(&nf->flows, &nf_flow->hmap_node);
337                 free(nf_flow);
338             }
339         }
340     }
341 }
342
343 void
344 netflow_run(struct netflow *nf)
345 {
346     ovs_mutex_lock(&mutex);
347     netflow_run__(nf);
348     ovs_mutex_unlock(&mutex);
349 }
350
351 void
352 netflow_wait(struct netflow *nf) OVS_EXCLUDED(mutex)
353 {
354     ovs_mutex_lock(&mutex);
355     if (nf->active_timeout) {
356         poll_timer_wait_until(nf->next_timeout);
357     }
358     if (nf->packet.size) {
359         poll_immediate_wake();
360     }
361     ovs_mutex_unlock(&mutex);
362 }
363
364 int
365 netflow_set_options(struct netflow *nf,
366                     const struct netflow_options *nf_options)
367     OVS_EXCLUDED(mutex)
368 {
369     int error = 0;
370     long long int old_timeout;
371
372     ovs_mutex_lock(&mutex);
373     nf->engine_type = nf_options->engine_type;
374     nf->engine_id = nf_options->engine_id;
375     nf->add_id_to_iface = nf_options->add_id_to_iface;
376
377     collectors_destroy(nf->collectors);
378     collectors_create(&nf_options->collectors, 0, &nf->collectors);
379
380     old_timeout = nf->active_timeout;
381     if (nf_options->active_timeout >= 0) {
382         nf->active_timeout = nf_options->active_timeout;
383     } else {
384         nf->active_timeout = NF_ACTIVE_TIMEOUT_DEFAULT;
385     }
386     nf->active_timeout *= 1000;
387     if (old_timeout != nf->active_timeout) {
388         nf->reconfig_time = time_msec();
389         nf->next_timeout = time_msec();
390     }
391     ovs_mutex_unlock(&mutex);
392
393     return error;
394 }
395
396 struct netflow *
397 netflow_create(void)
398 {
399     struct netflow *nf = xzalloc(sizeof *nf);
400     int junk;
401
402     nf->engine_type = 0;
403     nf->engine_id = 0;
404     nf->boot_time = time_msec();
405     nf->collectors = NULL;
406     nf->add_id_to_iface = false;
407     nf->netflow_cnt = 0;
408     hmap_init(&nf->flows);
409     atomic_init(&nf->ref_cnt, 1);
410     ofpbuf_init(&nf->packet, 1500);
411     atomic_add(&netflow_count, 1, &junk);
412     return nf;
413 }
414
415 struct netflow *
416 netflow_ref(const struct netflow *nf_)
417 {
418     struct netflow *nf = CONST_CAST(struct netflow *, nf_);
419     if (nf) {
420         int orig;
421         atomic_add(&nf->ref_cnt, 1, &orig);
422         ovs_assert(orig > 0);
423     }
424     return nf;
425 }
426
427 void
428 netflow_unref(struct netflow *nf)
429 {
430     int orig;
431
432     if (!nf) {
433         return;
434     }
435
436     atomic_sub(&nf->ref_cnt, 1, &orig);
437     ovs_assert(orig > 0);
438     if (orig == 1) {
439         atomic_sub(&netflow_count, 1, &orig);
440         collectors_destroy(nf->collectors);
441         ofpbuf_uninit(&nf->packet);
442         free(nf);
443     }
444 }
445
446 /* Returns true if there exist any netflow objects, false otherwise. */
447 bool
448 netflow_exists(void)
449 {
450     int n;
451
452     atomic_read(&netflow_count, &n);
453     return n > 0;
454 }
455 \f
456 /* Helpers. */
457
458 static struct netflow_flow *
459 netflow_flow_lookup(const struct netflow *nf, const struct flow *flow)
460     OVS_REQUIRES(mutex)
461 {
462     struct netflow_flow *nf_flow;
463
464     HMAP_FOR_EACH_WITH_HASH (nf_flow, hmap_node, netflow_flow_hash(flow),
465                              &nf->flows) {
466         if (flow->in_port.ofp_port == nf_flow->in_port
467             && flow->nw_src == nf_flow->nw_src
468             && flow->nw_dst == nf_flow->nw_dst
469             && flow->nw_tos == nf_flow->nw_tos
470             && flow->nw_proto == nf_flow->nw_proto
471             && flow->tp_src == nf_flow->tp_src
472             && flow->tp_dst == nf_flow->tp_dst) {
473             return nf_flow;
474         }
475     }
476
477     return NULL;
478 }
479
480 static uint32_t
481 netflow_flow_hash(const struct flow *flow)
482 {
483     uint32_t hash = 0;
484
485     hash = mhash_add(hash, (OVS_FORCE uint32_t) flow->in_port.ofp_port);
486     hash = mhash_add(hash, ntohl(flow->nw_src));
487     hash = mhash_add(hash, ntohl(flow->nw_dst));
488     hash = mhash_add(hash, flow->nw_tos);
489     hash = mhash_add(hash, flow->nw_proto);
490     hash = mhash_add(hash, ntohs(flow->tp_src));
491     hash = mhash_add(hash, ntohs(flow->tp_dst));
492
493     return mhash_finish(hash, 28);
494 }