2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <arpa/inet.h>
23 #include "byte-order.h"
24 #include "collectors.h"
26 #include "lib/netflow.h"
29 #include "ofproto/netflow.h"
31 #include "poll-loop.h"
32 #include "socket-util.h"
37 VLOG_DEFINE_THIS_MODULE(netflow);
40 uint8_t engine_type; /* Value of engine_type to use. */
41 uint8_t engine_id; /* Value of engine_id to use. */
42 long long int boot_time; /* Time when netflow_create() was called. */
43 struct collectors *collectors; /* NetFlow collectors. */
44 bool add_id_to_iface; /* Put the 7 least signficiant bits of
45 * 'engine_id' into the most signficant
46 * bits of the interface fields. */
47 uint32_t netflow_cnt; /* Flow sequence number for NetFlow. */
48 struct ofpbuf packet; /* NetFlow packet being accumulated. */
49 long long int active_timeout; /* Timeout for flows that are still active. */
50 long long int next_timeout; /* Next scheduled active timeout. */
51 long long int reconfig_time; /* When we reconfigured the timeouts. */
55 gen_netflow_rec(struct netflow *nf, struct netflow_flow *nf_flow,
56 struct ofexpired *expired,
57 uint32_t packet_count, uint32_t byte_count)
59 struct netflow_v5_header *nf_hdr;
60 struct netflow_v5_record *nf_rec;
62 if (!nf->packet.size) {
65 time_wall_timespec(&now);
67 nf_hdr = ofpbuf_put_zeros(&nf->packet, sizeof *nf_hdr);
68 nf_hdr->version = htons(NETFLOW_V5_VERSION);
69 nf_hdr->count = htons(0);
70 nf_hdr->sysuptime = htonl(time_msec() - nf->boot_time);
71 nf_hdr->unix_secs = htonl(now.tv_sec);
72 nf_hdr->unix_nsecs = htonl(now.tv_nsec);
73 nf_hdr->flow_seq = htonl(nf->netflow_cnt++);
74 nf_hdr->engine_type = nf->engine_type;
75 nf_hdr->engine_id = nf->engine_id;
76 nf_hdr->sampling_interval = htons(0);
79 nf_hdr = nf->packet.data;
80 nf_hdr->count = htons(ntohs(nf_hdr->count) + 1);
82 nf_rec = ofpbuf_put_zeros(&nf->packet, sizeof *nf_rec);
83 nf_rec->src_addr = expired->flow.nw_src;
84 nf_rec->dst_addr = expired->flow.nw_dst;
85 nf_rec->nexthop = htonl(0);
86 if (nf->add_id_to_iface) {
87 uint16_t iface = (nf->engine_id & 0x7f) << 9;
88 nf_rec->input = htons(iface | (expired->flow.in_port & 0x1ff));
89 nf_rec->output = htons(iface | (nf_flow->output_iface & 0x1ff));
91 nf_rec->input = htons(expired->flow.in_port);
92 nf_rec->output = htons(nf_flow->output_iface);
94 nf_rec->packet_count = htonl(packet_count);
95 nf_rec->byte_count = htonl(byte_count);
96 nf_rec->init_time = htonl(nf_flow->created - nf->boot_time);
97 nf_rec->used_time = htonl(MAX(nf_flow->created, expired->used)
99 if (expired->flow.nw_proto == IPPROTO_ICMP) {
100 /* In NetFlow, the ICMP type and code are concatenated and
101 * placed in the 'dst_port' field. */
102 uint8_t type = ntohs(expired->flow.tp_src);
103 uint8_t code = ntohs(expired->flow.tp_dst);
104 nf_rec->src_port = htons(0);
105 nf_rec->dst_port = htons((type << 8) | code);
107 nf_rec->src_port = expired->flow.tp_src;
108 nf_rec->dst_port = expired->flow.tp_dst;
110 nf_rec->tcp_flags = nf_flow->tcp_flags;
111 nf_rec->ip_proto = expired->flow.nw_proto;
112 nf_rec->ip_tos = expired->flow.nw_tos & IP_DSCP_MASK;
114 /* NetFlow messages are limited to 30 records. */
115 if (ntohs(nf_hdr->count) >= 30) {
121 netflow_expire(struct netflow *nf, struct netflow_flow *nf_flow,
122 struct ofexpired *expired)
124 uint64_t pkt_delta = expired->packet_count - nf_flow->packet_count_off;
125 uint64_t byte_delta = expired->byte_count - nf_flow->byte_count_off;
127 nf_flow->last_expired += nf->active_timeout;
129 /* NetFlow only reports on IP packets and we should only report flows
130 * that actually have traffic. */
131 if (expired->flow.dl_type != htons(ETH_TYPE_IP) || pkt_delta == 0) {
135 if ((byte_delta >> 32) <= 175) {
136 /* NetFlow v5 records are limited to 32-bit counters. If we've wrapped
137 * a counter, send as multiple records so we don't lose track of any
138 * traffic. We try to evenly distribute the packet and byte counters,
139 * so that the bytes-per-packet lengths don't look wonky across the
142 int n_recs = (byte_delta + UINT32_MAX - 1) / UINT32_MAX;
143 uint32_t pkt_count = pkt_delta / n_recs;
144 uint32_t byte_count = byte_delta / n_recs;
146 gen_netflow_rec(nf, nf_flow, expired, pkt_count, byte_count);
148 pkt_delta -= pkt_count;
149 byte_delta -= byte_count;
152 /* In 600 seconds, a 10GbE link can theoretically transmit 75 * 10**10
153 * == 175 * 2**32 bytes. The byte counter is bigger than that, so it's
154 * probably a bug--for example, the netdev code uses UINT64_MAX to
155 * report "unknown value", and perhaps that has leaked through to here.
157 * We wouldn't want to hit the loop above in this case, because it
158 * would try to send up to UINT32_MAX netflow records, which would take
161 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
163 VLOG_WARN_RL(&rl, "impossible byte counter %"PRIu64, byte_delta);
166 /* Update flow tracking data. */
167 nf_flow->created = 0;
168 nf_flow->packet_count_off = expired->packet_count;
169 nf_flow->byte_count_off = expired->byte_count;
170 nf_flow->tcp_flags = 0;
173 /* Returns true if it's time to send out a round of NetFlow active timeouts,
174 * false otherwise. */
176 netflow_run(struct netflow *nf)
178 if (nf->packet.size) {
179 collectors_send(nf->collectors, nf->packet.data, nf->packet.size);
183 if (nf->active_timeout && time_msec() >= nf->next_timeout) {
184 nf->next_timeout = time_msec() + 1000;
192 netflow_wait(struct netflow *nf)
194 if (nf->active_timeout) {
195 poll_timer_wait_until(nf->next_timeout);
197 if (nf->packet.size) {
198 poll_immediate_wake();
203 netflow_set_options(struct netflow *nf,
204 const struct netflow_options *nf_options)
207 long long int old_timeout;
209 nf->engine_type = nf_options->engine_type;
210 nf->engine_id = nf_options->engine_id;
211 nf->add_id_to_iface = nf_options->add_id_to_iface;
213 collectors_destroy(nf->collectors);
214 collectors_create(&nf_options->collectors, 0, &nf->collectors);
216 old_timeout = nf->active_timeout;
217 if (nf_options->active_timeout >= 0) {
218 nf->active_timeout = nf_options->active_timeout;
220 nf->active_timeout = NF_ACTIVE_TIMEOUT_DEFAULT;
222 nf->active_timeout *= 1000;
223 if (old_timeout != nf->active_timeout) {
224 nf->reconfig_time = time_msec();
225 nf->next_timeout = time_msec();
234 struct netflow *nf = xzalloc(sizeof *nf);
237 nf->boot_time = time_msec();
238 nf->collectors = NULL;
239 nf->add_id_to_iface = false;
241 ofpbuf_init(&nf->packet, 1500);
246 netflow_destroy(struct netflow *nf)
249 ofpbuf_uninit(&nf->packet);
250 collectors_destroy(nf->collectors);
255 /* Initializes a new 'nf_flow' given that the caller has already cleared it to
258 netflow_flow_init(struct netflow_flow *nf_flow OVS_UNUSED)
264 netflow_flow_clear(struct netflow_flow *nf_flow)
266 uint16_t output_iface = nf_flow->output_iface;
268 memset(nf_flow, 0, sizeof *nf_flow);
269 nf_flow->output_iface = output_iface;
273 netflow_flow_update_time(struct netflow *nf, struct netflow_flow *nf_flow,
276 if (!nf_flow->created) {
277 nf_flow->created = used;
280 if (!nf || !nf->active_timeout || !nf_flow->last_expired ||
281 nf->reconfig_time > nf_flow->last_expired) {
282 /* Keep the time updated to prevent a flood of expiration in
284 nf_flow->last_expired = time_msec();
289 netflow_flow_update_flags(struct netflow_flow *nf_flow, uint8_t tcp_flags)
291 nf_flow->tcp_flags |= tcp_flags;
295 netflow_active_timeout_expired(struct netflow *nf, struct netflow_flow *nf_flow)
297 if (nf->active_timeout) {
298 return time_msec() > nf_flow->last_expired + nf->active_timeout;