2 * Copyright (c) 1998 The NetBSD Foundation, Inc.
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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
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.15 2008/07/27 10:06:57 sephe Exp $
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/sysctl.h>
47 #include <sys/thread2.h>
49 #include <machine/smp.h>
52 #include <net/route.h>
53 #include <net/netisr.h>
54 #include <net/netmsg2.h>
56 #include <netinet/in.h>
57 #include <netinet/ip.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/ip_flow.h>
62 #define IPFLOW_TIMER (5 * PR_SLOWHZ)
63 #define IPFLOW_HASHBITS 6 /* should not be a multiple of 8 */
64 #define IPFLOW_HASHSIZE (1 << IPFLOW_HASHBITS)
65 #define IPFLOW_MAX 256
67 #define IPFLOW_RTENTRY_ISDOWN(rt) \
68 (((rt)->rt_flags & RTF_UP) == 0 || ((rt)->rt_ifp->if_flags & IFF_UP) == 0)
70 #define ipflow_inuse ipflow_inuse_pcpu[mycpuid]
71 #define ipflows ipflows_pcpu[mycpuid]
73 static LIST_HEAD(ipflowhead, ipflow) ipflows_pcpu[MAXCPU][IPFLOW_HASHSIZE];
74 static int ipflow_inuse_pcpu[MAXCPU];
75 static struct netmsg ipflow_timo_netmsgs[MAXCPU];
76 static int ipflow_active = 0;
78 SYSCTL_NODE(_net_inet_ip, OID_AUTO, ipflow, CTLFLAG_RW, 0, "ip flow");
79 SYSCTL_INT(_net_inet_ip, IPCTL_FASTFORWARDING, fastforwarding, CTLFLAG_RW,
80 &ipflow_active, 0, "Enable flow-based IP forwarding");
82 static MALLOC_DEFINE(M_IPFLOW, "ip_flow", "IP flow");
85 ipflow_hash(struct in_addr dst, struct in_addr src, unsigned tos)
90 for (idx = 0; idx < 32; idx += IPFLOW_HASHBITS)
91 hash += (dst.s_addr >> (32 - idx)) + (src.s_addr >> idx);
92 return hash & (IPFLOW_HASHSIZE-1);
95 static struct ipflow *
96 ipflow_lookup(const struct ip *ip)
101 hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
104 ipf = LIST_FIRST(&ipflows[hash]);
105 while (ipf != NULL) {
106 if (ip->ip_dst.s_addr == ipf->ipf_dst.s_addr &&
107 ip->ip_src.s_addr == ipf->ipf_src.s_addr &&
108 ip->ip_tos == ipf->ipf_tos)
110 ipf = LIST_NEXT(ipf, ipf_next);
118 ipflow_fastforward(struct mbuf *m)
123 struct sockaddr *dst;
128 * Are we forwarding packets? Big enough for an IP packet?
130 if (!ipforwarding || !ipflow_active || m->m_len < sizeof(struct ip))
134 * IP header with no option and valid version and length
136 ip = mtod(m, struct ip *);
137 if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2) ||
138 ntohs(ip->ip_len) > m->m_pkthdr.len)
144 ipf = ipflow_lookup(ip);
149 * Route and interface still up?
151 rt = ipf->ipf_ro.ro_rt;
152 if (IPFLOW_RTENTRY_ISDOWN(rt))
157 * Packet size OK? TTL?
159 if (m->m_pkthdr.len > ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
163 * Everything checks out and so we can forward this packet.
164 * Modify the TTL and incrementally change the checksum.
166 ip->ip_ttl -= IPTTLDEC;
167 if (ip->ip_sum >= htons(0xffff - (IPTTLDEC << 8)))
168 ip->ip_sum += htons(IPTTLDEC << 8) + 1;
170 ip->ip_sum += htons(IPTTLDEC << 8);
173 * Send the packet on its way. All we can get back is ENOBUFS
176 ipf->ipf_timer = IPFLOW_TIMER;
178 if (rt->rt_flags & RTF_GATEWAY)
179 dst = rt->rt_gateway;
181 dst = &ipf->ipf_ro.ro_dst;
183 error = ifp->if_output(ifp, m, dst, rt);
185 if (error == ENOBUFS)
194 ipflow_addstats(struct ipflow *ipf)
196 ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
197 ipstat.ips_cantforward += ipf->ipf_errors + ipf->ipf_dropped;
198 ipstat.ips_forward += ipf->ipf_uses;
199 ipstat.ips_fastforward += ipf->ipf_uses;
203 ipflow_free(struct ipflow *ipf)
206 * Remove the flow from the hash table (at elevated IPL).
207 * Once it's off the list, we can deal with it at normal
211 LIST_REMOVE(ipf, ipf_next);
213 KKASSERT(ipflow_inuse > 0);
217 ipflow_addstats(ipf);
218 RTFREE(ipf->ipf_ro.ro_rt);
219 kfree(ipf, M_IPFLOW);
222 static struct ipflow *
225 struct ipflow *ipf, *maybe_ipf = NULL;
229 for (idx = 0; idx < IPFLOW_HASHSIZE; idx++) {
230 ipf = LIST_FIRST(&ipflows[idx]);
231 while (ipf != NULL) {
233 * If this no longer points to a valid route
236 if ((ipf->ipf_ro.ro_rt->rt_flags & RTF_UP) == 0)
240 * choose the one that's been least recently used
241 * or has had the least uses in the last 1.5
244 if (maybe_ipf == NULL ||
245 ipf->ipf_timer < maybe_ipf->ipf_timer ||
246 (ipf->ipf_timer == maybe_ipf->ipf_timer &&
247 ipf->ipf_last_uses + ipf->ipf_uses <
248 maybe_ipf->ipf_last_uses + maybe_ipf->ipf_uses))
250 ipf = LIST_NEXT(ipf, ipf_next);
256 * Remove the entry from the flow table.
258 LIST_REMOVE(ipf, ipf_next);
261 ipflow_addstats(ipf);
262 RTFREE(ipf->ipf_ro.ro_rt);
267 ipflow_timo_dispatch(struct netmsg *nmsg)
273 lwkt_replymsg(&nmsg->nm_lmsg, 0); /* reply ASAP */
275 for (idx = 0; idx < IPFLOW_HASHSIZE; idx++) {
276 ipf = LIST_FIRST(&ipflows[idx]);
277 while (ipf != NULL) {
278 struct ipflow *next_ipf = LIST_NEXT(ipf, ipf_next);
280 if (--ipf->ipf_timer == 0) {
283 ipf->ipf_last_uses = ipf->ipf_uses;
284 ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
285 ipstat.ips_forward += ipf->ipf_uses;
286 ipstat.ips_fastforward += ipf->ipf_uses;
296 ipflow_timo_ipi(void *arg __unused)
298 struct lwkt_msg *msg = &ipflow_timo_netmsgs[mycpuid].nm_lmsg;
301 if (msg->ms_flags & MSGF_DONE)
302 lwkt_sendmsg(cpu_portfn(mycpuid), msg);
307 ipflow_slowtimo(void)
310 lwkt_send_ipiq_mask(smp_active_mask, ipflow_timo_ipi, NULL);
312 ipflow_timo_ipi(NULL);
317 ipflow_create(const struct route *ro, struct mbuf *m)
319 const struct ip *const ip = mtod(m, struct ip *);
324 * Don't create cache entries for ICMP messages.
326 if (!ipflow_active || ip->ip_p == IPPROTO_ICMP)
330 * See if an existing flow struct exists. If so remove it from it's
331 * list and free the old route. If not, try to malloc a new one
332 * (if we aren't at our limit).
334 ipf = ipflow_lookup(ip);
336 if (ipflow_inuse == IPFLOW_MAX) {
339 ipf = kmalloc(sizeof(*ipf), M_IPFLOW,
340 M_INTWAIT | M_NULLOK);
345 bzero(ipf, sizeof(*ipf));
348 LIST_REMOVE(ipf, ipf_next);
351 ipflow_addstats(ipf);
352 RTFREE(ipf->ipf_ro.ro_rt);
353 ipf->ipf_uses = ipf->ipf_last_uses = 0;
354 ipf->ipf_errors = ipf->ipf_dropped = 0;
358 * Fill in the updated information.
361 ro->ro_rt->rt_refcnt++;
362 ipf->ipf_dst = ip->ip_dst;
363 ipf->ipf_src = ip->ip_src;
364 ipf->ipf_tos = ip->ip_tos;
365 ipf->ipf_timer = IPFLOW_TIMER;
368 * Insert into the approriate bucket of the flow table.
370 hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
372 LIST_INSERT_HEAD(&ipflows[hash], ipf, ipf_next);
382 for (i = 0; i < ncpus; ++i) {
383 netmsg_init(&ipflow_timo_netmsgs[i], &netisr_adone_rport, 0,
384 ipflow_timo_dispatch);
386 ksnprintf(oid_name, sizeof(oid_name), "inuse%d", i);
389 SYSCTL_STATIC_CHILDREN(_net_inet_ip_ipflow),
390 OID_AUTO, oid_name, CTLFLAG_RD, &ipflow_inuse_pcpu[i], 0,
391 "# of ip flow being used");
394 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, ipflow_init, 0);