Clean it up a little bit
[dragonfly.git] / sys / netinet / ip_flow.c
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 $
37  * $DragonFly: src/sys/netinet/ip_flow.c,v 1.11 2008/03/30 10:15:46 sephe Exp $
38  */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/globaldata.h>
45 #include <sys/thread.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/kernel.h>
49
50 #include <sys/sysctl.h>
51 #include <sys/thread2.h>
52
53 #include <net/if.h>
54 #include <net/route.h>
55
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
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)
66 #define IPFLOW_MAX              256
67
68 static LIST_HEAD(ipflowhead, ipflow) ipflows[IPFLOW_HASHSIZE];
69 static int      ipflow_inuse;
70 static int      ipflow_active = 0;
71
72 SYSCTL_INT(_net_inet_ip, IPCTL_FASTFORWARDING, fastforwarding, CTLFLAG_RW,
73            &ipflow_active, 0, "Enable flow-based IP forwarding");
74
75 static MALLOC_DEFINE(M_IPFLOW, "ip_flow", "IP flow");
76
77 static unsigned
78 ipflow_hash(struct in_addr dst, struct in_addr src, unsigned tos)
79 {
80         unsigned hash = tos;
81         int idx;
82
83         for (idx = 0; idx < 32; idx += IPFLOW_HASHBITS)
84                 hash += (dst.s_addr >> (32 - idx)) + (src.s_addr >> idx);
85         return hash & (IPFLOW_HASHSIZE-1);
86 }
87
88 static struct ipflow *
89 ipflow_lookup(const struct ip *ip)
90 {
91         unsigned hash;
92         struct ipflow *ipf;
93
94         hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
95
96         ipf = LIST_FIRST(&ipflows[hash]);
97         while (ipf != NULL) {
98                 if (ip->ip_dst.s_addr == ipf->ipf_dst.s_addr &&
99                     ip->ip_src.s_addr == ipf->ipf_src.s_addr &&
100                     ip->ip_tos == ipf->ipf_tos)
101                         break;
102                 ipf = LIST_NEXT(ipf, ipf_next);
103         }
104         return ipf;
105 }
106
107 int
108 ipflow_fastforward(struct mbuf *m, lwkt_serialize_t serializer)
109 {
110         struct ip *ip;
111         struct ipflow *ipf;
112         struct rtentry *rt;
113         struct sockaddr *dst;
114         struct ifnet *ifp;
115         int error;
116
117         /*
118          * Are we forwarding packets?  Big enough for an IP packet?
119          */
120         if (!ipforwarding || !ipflow_active || m->m_len < sizeof(struct ip))
121                 return 0;
122
123         /*
124          * IP header with no option and valid version and length
125          */
126         ip = mtod(m, struct ip *);
127         if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2) ||
128             ntohs(ip->ip_len) > m->m_pkthdr.len)
129                 return 0;
130
131         /*
132          * Find a flow.
133          */
134         ipf = ipflow_lookup(ip);
135         if (ipf == NULL)
136                 return 0;
137
138         /*
139          * Route and interface still up?
140          */
141         rt = ipf->ipf_ro.ro_rt;
142         if ((rt->rt_flags & RTF_UP) == 0 ||
143             (rt->rt_ifp->if_flags & IFF_UP) == 0)
144                 return 0;
145         ifp = rt->rt_ifp;
146
147         /*
148          * Packet size OK?  TTL?
149          */
150         if (m->m_pkthdr.len > ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
151                 return 0;
152
153         /*
154          * Everything checks out and so we can forward this packet.
155          * Modify the TTL and incrementally change the checksum.
156          */
157         ip->ip_ttl -= IPTTLDEC;
158         if (ip->ip_sum >= htons(0xffff - (IPTTLDEC << 8)))
159                 ip->ip_sum += htons(IPTTLDEC << 8) + 1;
160         else
161                 ip->ip_sum += htons(IPTTLDEC << 8);
162
163         /*
164          * Send the packet on its way.  All we can get back is ENOBUFS
165          */
166         ipf->ipf_uses++;
167         ipf->ipf_timer = IPFLOW_TIMER;
168
169         if (rt->rt_flags & RTF_GATEWAY)
170                 dst = rt->rt_gateway;
171         else
172                 dst = &ipf->ipf_ro.ro_dst;
173
174         if (serializer)
175                 lwkt_serialize_exit(serializer);
176
177         lwkt_serialize_enter(ifp->if_serializer);
178         error = ifp->if_output(ifp, m, dst, rt);
179         if (error) {
180                 if (error == ENOBUFS)
181                         ipf->ipf_dropped++;
182                 else
183                         ipf->ipf_errors++;
184         }
185         lwkt_serialize_exit(ifp->if_serializer);
186
187         if (serializer)
188                 lwkt_serialize_enter(serializer);
189         return 1;
190 }
191
192 static void
193 ipflow_addstats(struct ipflow *ipf)
194 {
195         ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
196         ipstat.ips_cantforward += ipf->ipf_errors + ipf->ipf_dropped;
197         ipstat.ips_forward += ipf->ipf_uses;
198         ipstat.ips_fastforward += ipf->ipf_uses;
199 }
200
201 static void
202 ipflow_free(struct ipflow *ipf)
203 {
204         /*
205          * Remove the flow from the hash table (at elevated IPL).
206          * Once it's off the list, we can deal with it at normal
207          * network IPL.
208          */
209         crit_enter();
210         LIST_REMOVE(ipf, ipf_next);
211         crit_exit();
212
213         ipflow_addstats(ipf);
214         RTFREE(ipf->ipf_ro.ro_rt);
215         ipflow_inuse--;
216         kfree(ipf, M_IPFLOW);
217 }
218
219 static struct ipflow *
220 ipflow_reap(void)
221 {
222         struct ipflow *ipf, *maybe_ipf = NULL;
223         int idx;
224
225         for (idx = 0; idx < IPFLOW_HASHSIZE; idx++) {
226                 ipf = LIST_FIRST(&ipflows[idx]);
227                 while (ipf != NULL) {
228                         /*
229                          * If this no longer points to a valid route
230                          * reclaim it.
231                          */
232                         if ((ipf->ipf_ro.ro_rt->rt_flags & RTF_UP) == 0)
233                                 goto done;
234                         /*
235                          * choose the one that's been least recently used
236                          * or has had the least uses in the last 1.5
237                          * intervals.
238                          */
239                         if (maybe_ipf == NULL ||
240                             ipf->ipf_timer < maybe_ipf->ipf_timer ||
241                             (ipf->ipf_timer == maybe_ipf->ipf_timer &&
242                              ipf->ipf_last_uses + ipf->ipf_uses <
243                              maybe_ipf->ipf_last_uses + maybe_ipf->ipf_uses))
244                                 maybe_ipf = ipf;
245                         ipf = LIST_NEXT(ipf, ipf_next);
246                 }
247         }
248         ipf = maybe_ipf;
249 done:
250         /*
251          * Remove the entry from the flow table.
252          */
253         crit_enter();
254         LIST_REMOVE(ipf, ipf_next);
255         crit_exit();
256
257         ipflow_addstats(ipf);
258         RTFREE(ipf->ipf_ro.ro_rt);
259         return ipf;
260 }
261
262 void
263 ipflow_slowtimo(void)
264 {
265         struct ipflow *ipf;
266         int idx;
267
268         for (idx = 0; idx < IPFLOW_HASHSIZE; idx++) {
269                 ipf = LIST_FIRST(&ipflows[idx]);
270                 while (ipf != NULL) {
271                         struct ipflow *next_ipf = LIST_NEXT(ipf, ipf_next);
272                         if (--ipf->ipf_timer == 0) {
273                                 ipflow_free(ipf);
274                         } else {
275                                 ipf->ipf_last_uses = ipf->ipf_uses;
276                                 ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
277                                 ipstat.ips_forward += ipf->ipf_uses;
278                                 ipstat.ips_fastforward += ipf->ipf_uses;
279                                 ipf->ipf_uses = 0;
280                         }
281                         ipf = next_ipf;
282                 }
283         }
284 }
285
286 void
287 ipflow_create(const struct route *ro, struct mbuf *m)
288 {
289         const struct ip *const ip = mtod(m, struct ip *);
290         struct ipflow *ipf;
291         unsigned hash;
292
293         /*
294          * Don't create cache entries for ICMP messages.
295          */
296         if (!ipflow_active || ip->ip_p == IPPROTO_ICMP)
297                 return;
298
299         /*
300          * See if an existing flow struct exists.  If so remove it from it's
301          * list and free the old route.  If not, try to malloc a new one
302          * (if we aren't at our limit).
303          */
304         ipf = ipflow_lookup(ip);
305         if (ipf == NULL) {
306                 if (ipflow_inuse == IPFLOW_MAX) {
307                         ipf = ipflow_reap();
308                 } else {
309                         ipf = kmalloc(sizeof(*ipf), M_IPFLOW,
310                                       M_INTWAIT | M_NULLOK);
311                         if (ipf == NULL)
312                                 return;
313                         ipflow_inuse++;
314                 }
315                 bzero(ipf, sizeof(*ipf));
316         } else {
317                 crit_enter();
318                 LIST_REMOVE(ipf, ipf_next);
319                 crit_exit();
320
321                 ipflow_addstats(ipf);
322                 RTFREE(ipf->ipf_ro.ro_rt);
323                 ipf->ipf_uses = ipf->ipf_last_uses = 0;
324                 ipf->ipf_errors = ipf->ipf_dropped = 0;
325         }
326
327         /*
328          * Fill in the updated information.
329          */
330         ipf->ipf_ro = *ro;
331         ro->ro_rt->rt_refcnt++;
332         ipf->ipf_dst = ip->ip_dst;
333         ipf->ipf_src = ip->ip_src;
334         ipf->ipf_tos = ip->ip_tos;
335         ipf->ipf_timer = IPFLOW_TIMER;
336
337         /*
338          * Insert into the approriate bucket of the flow table.
339          */
340         hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
341         crit_enter();
342         LIST_INSERT_HEAD(&ipflows[hash], ipf, ipf_next);
343         crit_exit();
344 }