Perform ipflow timeout, only if the CPU has active ipflows; saves us some
[dragonfly.git] / sys / netinet / ip_flow.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1998 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by the 3am Software Foundry ("3am"). It was developed by Matt Thomas.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the NetBSD
19 * Foundation, Inc. and its contributors.
20 * 4. Neither the name of The NetBSD Foundation nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 *
36 * $FreeBSD: src/sys/netinet/ip_flow.c,v 1.9.2.2 2001/11/04 17:35:31 luigi Exp $
7fe5db3b 37 * $DragonFly: src/sys/netinet/ip_flow.c,v 1.18 2008/10/26 08:32:10 sephe Exp $
984263bc
MD
38 */
39
40#include <sys/param.h>
775a6411 41#include <sys/kernel.h>
984263bc
MD
42#include <sys/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/protosw.h>
45#include <sys/socket.h>
984263bc 46#include <sys/sysctl.h>
1cae611f 47#include <sys/thread2.h>
8eb08577 48#include <sys/in_cksum.h>
984263bc 49
775a6411
SZ
50#include <machine/smp.h>
51
984263bc
MD
52#include <net/if.h>
53#include <net/route.h>
775a6411
SZ
54#include <net/netisr.h>
55#include <net/netmsg2.h>
984263bc
MD
56
57#include <netinet/in.h>
984263bc
MD
58#include <netinet/ip.h>
59#include <netinet/in_var.h>
60#include <netinet/ip_var.h>
61#include <netinet/ip_flow.h>
62
63#define IPFLOW_TIMER (5 * PR_SLOWHZ)
64#define IPFLOW_HASHBITS 6 /* should not be a multiple of 8 */
65#define IPFLOW_HASHSIZE (1 << IPFLOW_HASHBITS)
984263bc
MD
66#define IPFLOW_MAX 256
67
775a6411
SZ
68#define IPFLOW_RTENTRY_ISDOWN(rt) \
69 (((rt)->rt_flags & RTF_UP) == 0 || ((rt)->rt_ifp->if_flags & IFF_UP) == 0)
70
4639df5f
SZ
71struct ipflow {
72 LIST_ENTRY(ipflow) ipf_next; /* next ipflow in bucket */
73 struct in_addr ipf_dst; /* destination address */
74 struct in_addr ipf_src; /* source address */
75
76 uint8_t ipf_tos; /* type-of-service */
77 struct route ipf_ro; /* associated route entry */
78 u_long ipf_uses; /* number of uses in this period */
79
80 int ipf_timer; /* remaining lifetime of this entry */
81 u_long ipf_dropped; /* ENOBUFS returned by if_output */
82 u_long ipf_errors; /* other errors returned by if_output */
83 u_long ipf_last_uses; /* number of uses in last period */
84};
85
775a6411
SZ
86#define ipflow_inuse ipflow_inuse_pcpu[mycpuid]
87#define ipflows ipflows_pcpu[mycpuid]
10c4b5fc 88
775a6411
SZ
89static LIST_HEAD(ipflowhead, ipflow) ipflows_pcpu[MAXCPU][IPFLOW_HASHSIZE];
90static int ipflow_inuse_pcpu[MAXCPU];
91static struct netmsg ipflow_timo_netmsgs[MAXCPU];
92static int ipflow_active = 0;
93
94SYSCTL_NODE(_net_inet_ip, OID_AUTO, ipflow, CTLFLAG_RW, 0, "ip flow");
984263bc 95SYSCTL_INT(_net_inet_ip, IPCTL_FASTFORWARDING, fastforwarding, CTLFLAG_RW,
10c4b5fc 96 &ipflow_active, 0, "Enable flow-based IP forwarding");
984263bc
MD
97
98static MALLOC_DEFINE(M_IPFLOW, "ip_flow", "IP flow");
99
100static unsigned
be23faf1 101ipflow_hash(struct in_addr dst, struct in_addr src, unsigned tos)
984263bc
MD
102{
103 unsigned hash = tos;
104 int idx;
be23faf1 105
984263bc
MD
106 for (idx = 0; idx < 32; idx += IPFLOW_HASHBITS)
107 hash += (dst.s_addr >> (32 - idx)) + (src.s_addr >> idx);
108 return hash & (IPFLOW_HASHSIZE-1);
109}
110
111static struct ipflow *
be23faf1 112ipflow_lookup(const struct ip *ip)
984263bc
MD
113{
114 unsigned hash;
115 struct ipflow *ipf;
116
117 hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
118
775a6411 119 crit_enter();
984263bc
MD
120 ipf = LIST_FIRST(&ipflows[hash]);
121 while (ipf != NULL) {
be23faf1
JH
122 if (ip->ip_dst.s_addr == ipf->ipf_dst.s_addr &&
123 ip->ip_src.s_addr == ipf->ipf_src.s_addr &&
124 ip->ip_tos == ipf->ipf_tos)
984263bc
MD
125 break;
126 ipf = LIST_NEXT(ipf, ipf_next);
127 }
775a6411
SZ
128 crit_exit();
129
984263bc
MD
130 return ipf;
131}
132
133int
297c8124 134ipflow_fastforward(struct mbuf *m)
984263bc
MD
135{
136 struct ip *ip;
137 struct ipflow *ipf;
138 struct rtentry *rt;
139 struct sockaddr *dst;
10c4b5fc 140 struct ifnet *ifp;
984263bc
MD
141 int error;
142
143 /*
144 * Are we forwarding packets? Big enough for an IP packet?
145 */
146 if (!ipforwarding || !ipflow_active || m->m_len < sizeof(struct ip))
147 return 0;
10c4b5fc 148
984263bc
MD
149 /*
150 * IP header with no option and valid version and length
151 */
152 ip = mtod(m, struct ip *);
be23faf1
JH
153 if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2) ||
154 ntohs(ip->ip_len) > m->m_pkthdr.len)
984263bc 155 return 0;
10c4b5fc 156
984263bc
MD
157 /*
158 * Find a flow.
159 */
10c4b5fc
SZ
160 ipf = ipflow_lookup(ip);
161 if (ipf == NULL)
984263bc
MD
162 return 0;
163
8eb08577
SZ
164 /*
165 * Verify the IP header checksum.
166 */
167 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
168 if (!(m->m_pkthdr.csum_flags & CSUM_IP_VALID))
169 return 0;
170 } else {
171 /* Must compute it ourselves. */
172 if (in_cksum_hdr(ip) != 0)
173 return 0;
174 }
175
984263bc
MD
176 /*
177 * Route and interface still up?
178 */
179 rt = ipf->ipf_ro.ro_rt;
775a6411 180 if (IPFLOW_RTENTRY_ISDOWN(rt))
984263bc 181 return 0;
10c4b5fc 182 ifp = rt->rt_ifp;
984263bc
MD
183
184 /*
185 * Packet size OK? TTL?
186 */
10c4b5fc 187 if (m->m_pkthdr.len > ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
984263bc
MD
188 return 0;
189
8eb08577
SZ
190 /*
191 * Clear any in-bound checksum flags for this packet.
192 */
193 m->m_pkthdr.csum_flags = 0;
194
984263bc
MD
195 /*
196 * Everything checks out and so we can forward this packet.
197 * Modify the TTL and incrementally change the checksum.
8eb08577
SZ
198 *
199 * This method of adding the checksum works on either endian CPU.
200 * If htons() is inlined, all the arithmetic is folded; otherwise
201 * the htons()s are combined by CSE due to the __const__ attribute.
202 *
203 * Don't bother using HW checksumming here -- the incremental
204 * update is pretty fast.
984263bc
MD
205 */
206 ip->ip_ttl -= IPTTLDEC;
8eb08577
SZ
207 if (ip->ip_sum >= (uint16_t)~htons(IPTTLDEC << 8))
208 ip->ip_sum -= ~htons(IPTTLDEC << 8);
be23faf1 209 else
984263bc 210 ip->ip_sum += htons(IPTTLDEC << 8);
984263bc
MD
211
212 /*
213 * Send the packet on its way. All we can get back is ENOBUFS
214 */
215 ipf->ipf_uses++;
216 ipf->ipf_timer = IPFLOW_TIMER;
217
218 if (rt->rt_flags & RTF_GATEWAY)
219 dst = rt->rt_gateway;
220 else
221 dst = &ipf->ipf_ro.ro_dst;
10c4b5fc 222
10c4b5fc
SZ
223 error = ifp->if_output(ifp, m, dst, rt);
224 if (error) {
984263bc
MD
225 if (error == ENOBUFS)
226 ipf->ipf_dropped++;
227 else
228 ipf->ipf_errors++;
229 }
230 return 1;
231}
be23faf1 232
984263bc 233static void
be23faf1 234ipflow_addstats(struct ipflow *ipf)
984263bc
MD
235{
236 ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
237 ipstat.ips_cantforward += ipf->ipf_errors + ipf->ipf_dropped;
238 ipstat.ips_forward += ipf->ipf_uses;
239 ipstat.ips_fastforward += ipf->ipf_uses;
240}
241
242static void
be23faf1 243ipflow_free(struct ipflow *ipf)
984263bc 244{
984263bc
MD
245 /*
246 * Remove the flow from the hash table (at elevated IPL).
247 * Once it's off the list, we can deal with it at normal
248 * network IPL.
249 */
1cae611f 250 crit_enter();
984263bc 251 LIST_REMOVE(ipf, ipf_next);
775a6411
SZ
252
253 KKASSERT(ipflow_inuse > 0);
254 ipflow_inuse--;
1cae611f 255 crit_exit();
10c4b5fc 256
984263bc
MD
257 ipflow_addstats(ipf);
258 RTFREE(ipf->ipf_ro.ro_rt);
efda3bd0 259 kfree(ipf, M_IPFLOW);
984263bc
MD
260}
261
262static struct ipflow *
be23faf1 263ipflow_reap(void)
984263bc
MD
264{
265 struct ipflow *ipf, *maybe_ipf = NULL;
266 int idx;
984263bc 267
775a6411 268 crit_enter();
984263bc
MD
269 for (idx = 0; idx < IPFLOW_HASHSIZE; idx++) {
270 ipf = LIST_FIRST(&ipflows[idx]);
271 while (ipf != NULL) {
272 /*
273 * If this no longer points to a valid route
274 * reclaim it.
275 */
276 if ((ipf->ipf_ro.ro_rt->rt_flags & RTF_UP) == 0)
277 goto done;
775a6411 278
984263bc
MD
279 /*
280 * choose the one that's been least recently used
f23061d4 281 * or has had the least uses in the last 1.5
984263bc
MD
282 * intervals.
283 */
be23faf1
JH
284 if (maybe_ipf == NULL ||
285 ipf->ipf_timer < maybe_ipf->ipf_timer ||
286 (ipf->ipf_timer == maybe_ipf->ipf_timer &&
287 ipf->ipf_last_uses + ipf->ipf_uses <
288 maybe_ipf->ipf_last_uses + maybe_ipf->ipf_uses))
984263bc
MD
289 maybe_ipf = ipf;
290 ipf = LIST_NEXT(ipf, ipf_next);
291 }
292 }
293 ipf = maybe_ipf;
be23faf1 294done:
984263bc
MD
295 /*
296 * Remove the entry from the flow table.
297 */
984263bc 298 LIST_REMOVE(ipf, ipf_next);
1cae611f 299 crit_exit();
10c4b5fc 300
984263bc
MD
301 ipflow_addstats(ipf);
302 RTFREE(ipf->ipf_ro.ro_rt);
303 return ipf;
304}
305
775a6411
SZ
306static void
307ipflow_timo_dispatch(struct netmsg *nmsg)
984263bc
MD
308{
309 struct ipflow *ipf;
310 int idx;
311
775a6411
SZ
312 crit_enter();
313 lwkt_replymsg(&nmsg->nm_lmsg, 0); /* reply ASAP */
314
984263bc
MD
315 for (idx = 0; idx < IPFLOW_HASHSIZE; idx++) {
316 ipf = LIST_FIRST(&ipflows[idx]);
317 while (ipf != NULL) {
318 struct ipflow *next_ipf = LIST_NEXT(ipf, ipf_next);
775a6411 319
984263bc
MD
320 if (--ipf->ipf_timer == 0) {
321 ipflow_free(ipf);
322 } else {
323 ipf->ipf_last_uses = ipf->ipf_uses;
324 ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
325 ipstat.ips_forward += ipf->ipf_uses;
326 ipstat.ips_fastforward += ipf->ipf_uses;
327 ipf->ipf_uses = 0;
328 }
329 ipf = next_ipf;
330 }
331 }
775a6411
SZ
332 crit_exit();
333}
334
335static void
336ipflow_timo_ipi(void *arg __unused)
337{
338 struct lwkt_msg *msg = &ipflow_timo_netmsgs[mycpuid].nm_lmsg;
339
340 crit_enter();
341 if (msg->ms_flags & MSGF_DONE)
342 lwkt_sendmsg(cpu_portfn(mycpuid), msg);
343 crit_exit();
984263bc
MD
344}
345
346void
775a6411
SZ
347ipflow_slowtimo(void)
348{
4110c8c5 349#ifdef SMP
7fe5db3b
SZ
350 uint32_t mask = 0;
351 int i;
352
353 for (i = 0; i < ncpus; ++i) {
354 if (ipflow_inuse_pcpu[i])
355 mask |= 1 << i;
356 }
357 if (mask != 0)
358 lwkt_send_ipiq_mask(mask, ipflow_timo_ipi, NULL);
4110c8c5 359#else
7fe5db3b
SZ
360 if (ipflow_inuse)
361 ipflow_timo_ipi(NULL);
4110c8c5 362#endif
775a6411
SZ
363}
364
297c8124
SZ
365void
366ipflow_create(const struct route *ro, struct mbuf *m)
984263bc
MD
367{
368 const struct ip *const ip = mtod(m, struct ip *);
369 struct ipflow *ipf;
370 unsigned hash;
984263bc 371
297c8124
SZ
372 /*
373 * Don't create cache entries for ICMP messages.
374 */
375 if (!ipflow_active || ip->ip_p == IPPROTO_ICMP)
376 return;
377
984263bc
MD
378 /*
379 * See if an existing flow struct exists. If so remove it from it's
380 * list and free the old route. If not, try to malloc a new one
381 * (if we aren't at our limit).
382 */
383 ipf = ipflow_lookup(ip);
384 if (ipf == NULL) {
385 if (ipflow_inuse == IPFLOW_MAX) {
386 ipf = ipflow_reap();
387 } else {
10c4b5fc
SZ
388 ipf = kmalloc(sizeof(*ipf), M_IPFLOW,
389 M_INTWAIT | M_NULLOK);
984263bc
MD
390 if (ipf == NULL)
391 return;
392 ipflow_inuse++;
393 }
10c4b5fc 394 bzero(ipf, sizeof(*ipf));
297c8124 395 } else {
1cae611f 396 crit_enter();
984263bc 397 LIST_REMOVE(ipf, ipf_next);
1cae611f 398 crit_exit();
10c4b5fc 399
984263bc
MD
400 ipflow_addstats(ipf);
401 RTFREE(ipf->ipf_ro.ro_rt);
402 ipf->ipf_uses = ipf->ipf_last_uses = 0;
403 ipf->ipf_errors = ipf->ipf_dropped = 0;
404 }
405
406 /*
407 * Fill in the updated information.
408 */
409 ipf->ipf_ro = *ro;
410 ro->ro_rt->rt_refcnt++;
411 ipf->ipf_dst = ip->ip_dst;
412 ipf->ipf_src = ip->ip_src;
413 ipf->ipf_tos = ip->ip_tos;
414 ipf->ipf_timer = IPFLOW_TIMER;
10c4b5fc 415
984263bc
MD
416 /*
417 * Insert into the approriate bucket of the flow table.
418 */
419 hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
1cae611f 420 crit_enter();
984263bc 421 LIST_INSERT_HEAD(&ipflows[hash], ipf, ipf_next);
1cae611f 422 crit_exit();
984263bc 423}
775a6411 424
775a6411
SZ
425static void
426ipflow_init(void)
427{
428 char oid_name[32];
429 int i;
430
431 for (i = 0; i < ncpus; ++i) {
432 netmsg_init(&ipflow_timo_netmsgs[i], &netisr_adone_rport, 0,
433 ipflow_timo_dispatch);
434
435 ksnprintf(oid_name, sizeof(oid_name), "inuse%d", i);
436
437 SYSCTL_ADD_INT(NULL,
438 SYSCTL_STATIC_CHILDREN(_net_inet_ip_ipflow),
439 OID_AUTO, oid_name, CTLFLAG_RD, &ipflow_inuse_pcpu[i], 0,
440 "# of ip flow being used");
441 }
442}
443SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, ipflow_init, 0);