netflow: Use inet_open_active() to factor out some code.
[sliver-openvswitch.git] / ofproto / netflow.c
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
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 "cfg.h"
24 #include "flow.h"
25 #include "netflow.h"
26 #include "ofpbuf.h"
27 #include "ofproto.h"
28 #include "packets.h"
29 #include "socket-util.h"
30 #include "svec.h"
31 #include "timeval.h"
32 #include "util.h"
33 #include "xtoxll.h"
34
35 #define THIS_MODULE VLM_netflow
36 #include "vlog.h"
37
38 #define NETFLOW_V5_VERSION 5
39
40 static const int ACTIVE_TIMEOUT_DEFAULT = 600;
41
42 /* Every NetFlow v5 message contains the header that follows.  This is
43  * followed by up to thirty records that describe a terminating flow.
44  * We only send a single record per NetFlow message.
45  */
46 struct netflow_v5_header {
47     uint16_t version;              /* NetFlow version is 5. */
48     uint16_t count;                /* Number of records in this message. */
49     uint32_t sysuptime;            /* System uptime in milliseconds. */
50     uint32_t unix_secs;            /* Number of seconds since Unix epoch. */
51     uint32_t unix_nsecs;           /* Number of residual nanoseconds
52                                       after epoch seconds. */
53     uint32_t flow_seq;             /* Number of flows since sending
54                                       messages began. */
55     uint8_t  engine_type;          /* Engine type. */
56     uint8_t  engine_id;            /* Engine id. */
57     uint16_t sampling_interval;    /* Set to zero. */
58 };
59 BUILD_ASSERT_DECL(sizeof(struct netflow_v5_header) == 24);
60
61 /* A NetFlow v5 description of a terminating flow.  It is preceded by a
62  * NetFlow v5 header.
63  */
64 struct netflow_v5_record {
65     uint32_t src_addr;             /* Source IP address. */
66     uint32_t dst_addr;             /* Destination IP address. */
67     uint32_t nexthop;              /* IP address of next hop.  Set to 0. */
68     uint16_t input;                /* Input interface index. */
69     uint16_t output;               /* Output interface index. */
70     uint32_t packet_count;         /* Number of packets. */
71     uint32_t byte_count;           /* Number of bytes. */
72     uint32_t init_time;            /* Value of sysuptime on first packet. */
73     uint32_t used_time;            /* Value of sysuptime on last packet. */
74
75     /* The 'src_port' and 'dst_port' identify the source and destination
76      * port, respectively, for TCP and UDP.  For ICMP, the high-order
77      * byte identifies the type and low-order byte identifies the code
78      * in the 'dst_port' field. */
79     uint16_t src_port;
80     uint16_t dst_port;
81
82     uint8_t  pad1;
83     uint8_t  tcp_flags;            /* Union of seen TCP flags. */
84     uint8_t  ip_proto;             /* IP protocol. */
85     uint8_t  ip_tos;               /* IP TOS value. */
86     uint16_t src_as;               /* Source AS ID.  Set to 0. */
87     uint16_t dst_as;               /* Destination AS ID.  Set to 0. */
88     uint8_t  src_mask;             /* Source mask bits.  Set to 0. */
89     uint8_t  dst_mask;             /* Destination mask bits.  Set to 0. */
90     uint8_t  pad[2];
91 };
92 BUILD_ASSERT_DECL(sizeof(struct netflow_v5_record) == 48);
93
94 struct netflow {
95     uint8_t engine_type;          /* Value of engine_type to use. */
96     uint8_t engine_id;            /* Value of engine_id to use. */
97     long long int boot_time;      /* Time when netflow_create() was called. */
98     int *fds;                     /* Sockets for NetFlow collectors. */
99     size_t n_fds;                 /* Number of Netflow collectors. */
100     bool add_id_to_iface;         /* Put the 7 least signficiant bits of 
101                                    * 'engine_id' into the most signficant 
102                                    * bits of the interface fields. */
103     uint32_t netflow_cnt;         /* Flow sequence number for NetFlow. */
104     struct ofpbuf packet;         /* NetFlow packet being accumulated. */
105     long long int active_timeout; /* Timeout for flows that are still active. */
106     long long int reconfig_time;  /* When we reconfigured the timeouts. */
107 };
108
109 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
110
111 static int
112 open_collector(char *dst)
113 {
114     int error, fd;
115
116     error = inet_open_active(SOCK_DGRAM, dst, 0, NULL, &fd);
117     return fd >= 0 ? fd : -error;
118 }
119
120 void
121 netflow_expire(struct netflow *nf, struct netflow_flow *nf_flow,
122                struct ofexpired *expired)
123 {
124     struct netflow_v5_header *nf_hdr;
125     struct netflow_v5_record *nf_rec;
126     struct timeval now;
127
128     nf_flow->last_expired += nf->active_timeout;
129
130     /* NetFlow only reports on IP packets and we should only report flows
131      * that actually have traffic. */
132     if (expired->flow.dl_type != htons(ETH_TYPE_IP) ||
133         expired->packet_count - nf_flow->packet_count_off == 0) {
134         return;
135     }
136
137     time_timeval(&now);
138
139     if (!nf->packet.size) {
140         nf_hdr = ofpbuf_put_zeros(&nf->packet, sizeof *nf_hdr);
141         nf_hdr->version = htons(NETFLOW_V5_VERSION);
142         nf_hdr->count = htons(0);
143         nf_hdr->sysuptime = htonl(time_msec() - nf->boot_time);
144         nf_hdr->unix_secs = htonl(now.tv_sec);
145         nf_hdr->unix_nsecs = htonl(now.tv_usec * 1000);
146         nf_hdr->flow_seq = htonl(nf->netflow_cnt++);
147         nf_hdr->engine_type = nf->engine_type;
148         nf_hdr->engine_id = nf->engine_id;
149         nf_hdr->sampling_interval = htons(0);
150     }
151
152     nf_hdr = nf->packet.data;
153     nf_hdr->count = htons(ntohs(nf_hdr->count) + 1);
154
155     nf_rec = ofpbuf_put_zeros(&nf->packet, sizeof *nf_rec);
156     nf_rec->src_addr = expired->flow.nw_src;
157     nf_rec->dst_addr = expired->flow.nw_dst;
158     nf_rec->nexthop = htons(0);
159     if (nf->add_id_to_iface) {
160         uint16_t iface = (nf->engine_id & 0x7f) << 9;
161         nf_rec->input = htons(iface | (expired->flow.in_port & 0x1ff));
162         nf_rec->output = htons(iface | (nf_flow->output_iface & 0x1ff));
163     } else {
164         nf_rec->input = htons(expired->flow.in_port);
165         nf_rec->output = htons(nf_flow->output_iface);
166     }
167     nf_rec->packet_count = htonl(MIN(expired->packet_count -
168                                      nf_flow->packet_count_off, UINT32_MAX));
169     nf_rec->byte_count = htonl(MIN(expired->byte_count -
170                                    nf_flow->byte_count_off, UINT32_MAX));
171     nf_rec->init_time = htonl(nf_flow->created - nf->boot_time);
172     nf_rec->used_time = htonl(MAX(nf_flow->created, expired->used)
173                              - nf->boot_time);
174     if (expired->flow.nw_proto == IP_TYPE_ICMP) {
175         /* In NetFlow, the ICMP type and code are concatenated and
176          * placed in the 'dst_port' field. */
177         uint8_t type = ntohs(expired->flow.tp_src);
178         uint8_t code = ntohs(expired->flow.tp_dst);
179         nf_rec->src_port = htons(0);
180         nf_rec->dst_port = htons((type << 8) | code);
181     } else {
182         nf_rec->src_port = expired->flow.tp_src;
183         nf_rec->dst_port = expired->flow.tp_dst;
184     }
185     nf_rec->tcp_flags = nf_flow->tcp_flags;
186     nf_rec->ip_proto = expired->flow.nw_proto;
187     nf_rec->ip_tos = nf_flow->ip_tos;
188
189     /* Update flow tracking data. */
190     nf_flow->created = 0;
191     nf_flow->packet_count_off = expired->packet_count;
192     nf_flow->byte_count_off = expired->byte_count;
193     nf_flow->tcp_flags = 0;
194
195     /* NetFlow messages are limited to 30 records. */
196     if (ntohs(nf_hdr->count) >= 30) {
197         netflow_run(nf);
198     }
199 }
200
201 void
202 netflow_run(struct netflow *nf)
203 {
204     size_t i;
205
206     if (!nf->packet.size) {
207         return;
208     }
209
210     for (i = 0; i < nf->n_fds; i++) {
211         if (send(nf->fds[i], nf->packet.data, nf->packet.size, 0) == -1) {
212             VLOG_WARN_RL(&rl, "netflow message send failed: %s",
213                          strerror(errno));
214         }
215     }
216     nf->packet.size = 0;
217 }
218
219 static void
220 clear_collectors(struct netflow *nf)
221 {
222     size_t i;
223
224     for (i = 0; i < nf->n_fds; i++) {
225         close(nf->fds[i]);
226     }
227     free(nf->fds);
228     nf->fds = NULL;
229     nf->n_fds = 0;
230 }
231
232 int
233 netflow_set_options(struct netflow *nf,
234                     const struct netflow_options *nf_options)
235 {
236     struct svec collectors;
237     int error = 0;
238     size_t i;
239     long long int old_timeout;
240
241     nf->engine_type = nf_options->engine_type;
242     nf->engine_id = nf_options->engine_id;
243     nf->add_id_to_iface = nf_options->add_id_to_iface;
244
245     clear_collectors(nf);
246
247     svec_clone(&collectors, &nf_options->collectors);
248     svec_sort_unique(&collectors);
249
250     nf->fds = xmalloc(sizeof *nf->fds * collectors.n);
251     for (i = 0; i < collectors.n; i++) {
252         const char *name = collectors.names[i];
253         char *tmpname = xstrdup(name);
254         int fd = open_collector(tmpname);
255         free(tmpname);
256         if (fd >= 0) {
257             nf->fds[nf->n_fds++] = fd;
258         } else {
259             VLOG_WARN("couldn't open connection to collector (%s), "
260                       "ignoring %s\n", strerror(-fd), name);
261             if (!error) {
262                 error = -fd;
263             }
264         }
265     }
266
267     svec_destroy(&collectors);
268
269     old_timeout = nf->active_timeout;
270     if (nf_options->active_timeout != -1) {
271         nf->active_timeout = nf_options->active_timeout;
272     } else {
273         nf->active_timeout = ACTIVE_TIMEOUT_DEFAULT;
274     }
275     nf->active_timeout *= 1000;
276     if (old_timeout != nf->active_timeout) {
277         nf->reconfig_time = time_msec();
278     }
279
280     return error;
281 }
282
283 struct netflow *
284 netflow_create(void)
285 {
286     struct netflow *nf = xmalloc(sizeof *nf);
287     nf->engine_type = 0;
288     nf->engine_id = 0;
289     nf->boot_time = time_msec();
290     nf->fds = NULL;
291     nf->n_fds = 0;
292     nf->add_id_to_iface = false;
293     nf->netflow_cnt = 0;
294     ofpbuf_init(&nf->packet, 1500);
295     return nf;
296 }
297
298 void
299 netflow_destroy(struct netflow *nf)
300 {
301     if (nf) {
302         ofpbuf_uninit(&nf->packet);
303         clear_collectors(nf);
304         free(nf);
305     }
306 }
307
308 void
309 netflow_flow_clear(struct netflow_flow *nf_flow)
310 {
311     uint16_t output_iface = nf_flow->output_iface;
312
313     memset(nf_flow, 0, sizeof *nf_flow);
314     nf_flow->output_iface = output_iface;
315 }
316
317 void
318 netflow_flow_update_time(struct netflow *nf, struct netflow_flow *nf_flow,
319                          long long int used)
320 {
321     if (!nf_flow->created) {
322         nf_flow->created = used;
323     }
324
325     if (!nf || !nf->active_timeout || !nf_flow->last_expired ||
326         nf->reconfig_time > nf_flow->last_expired) {
327         /* Keep the time updated to prevent a flood of expiration in
328          * the future. */
329         nf_flow->last_expired = time_msec();
330     }
331 }
332
333 void
334 netflow_flow_update_flags(struct netflow_flow *nf_flow, uint8_t ip_tos,
335                           uint8_t tcp_flags)
336 {
337     nf_flow->ip_tos = ip_tos;
338     nf_flow->tcp_flags |= tcp_flags;
339 }
340
341 bool
342 netflow_active_timeout_expired(struct netflow *nf, struct netflow_flow *nf_flow)
343 {
344     if (nf->active_timeout) {
345         return time_msec() > nf_flow->last_expired + nf->active_timeout;
346     }
347
348     return false;
349 }