xref: /dragonfly/sys/netinet/ip_flow.c (revision 62f7f702)
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.13 2008/04/03 16:02:32 sephe Exp $
38  */
39 
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/sysctl.h>
47 #include <sys/thread2.h>
48 
49 #include <machine/smp.h>
50 
51 #include <net/if.h>
52 #include <net/route.h>
53 #include <net/netisr.h>
54 #include <net/netmsg2.h>
55 
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>
61 
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
66 
67 #define IPFLOW_RTENTRY_ISDOWN(rt) \
68 	(((rt)->rt_flags & RTF_UP) == 0 || ((rt)->rt_ifp->if_flags & IFF_UP) == 0)
69 
70 #define ipflow_inuse		ipflow_inuse_pcpu[mycpuid]
71 #define ipflows			ipflows_pcpu[mycpuid]
72 
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;
77 
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");
81 
82 static MALLOC_DEFINE(M_IPFLOW, "ip_flow", "IP flow");
83 
84 static unsigned
85 ipflow_hash(struct in_addr dst, struct in_addr src, unsigned tos)
86 {
87 	unsigned hash = tos;
88 	int idx;
89 
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);
93 }
94 
95 static struct ipflow *
96 ipflow_lookup(const struct ip *ip)
97 {
98 	unsigned hash;
99 	struct ipflow *ipf;
100 
101 	hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
102 
103 	crit_enter();
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)
109 			break;
110 		ipf = LIST_NEXT(ipf, ipf_next);
111 	}
112 	crit_exit();
113 
114 	return ipf;
115 }
116 
117 int
118 ipflow_fastforward(struct mbuf *m, lwkt_serialize_t serializer)
119 {
120 	struct ip *ip;
121 	struct ipflow *ipf;
122 	struct rtentry *rt;
123 	struct sockaddr *dst;
124 	struct ifnet *ifp;
125 	int error;
126 
127 	/*
128 	 * Are we forwarding packets?  Big enough for an IP packet?
129 	 */
130 	if (!ipforwarding || !ipflow_active || m->m_len < sizeof(struct ip))
131 		return 0;
132 
133 	/*
134 	 * IP header with no option and valid version and length
135 	 */
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)
139 		return 0;
140 
141 	/*
142 	 * Find a flow.
143 	 */
144 	ipf = ipflow_lookup(ip);
145 	if (ipf == NULL)
146 		return 0;
147 
148 	/*
149 	 * Route and interface still up?
150 	 */
151 	rt = ipf->ipf_ro.ro_rt;
152 	if (IPFLOW_RTENTRY_ISDOWN(rt))
153 		return 0;
154 	ifp = rt->rt_ifp;
155 
156 	/*
157 	 * Packet size OK?  TTL?
158 	 */
159 	if (m->m_pkthdr.len > ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
160 		return 0;
161 
162 	/*
163 	 * Everything checks out and so we can forward this packet.
164 	 * Modify the TTL and incrementally change the checksum.
165 	 */
166 	ip->ip_ttl -= IPTTLDEC;
167 	if (ip->ip_sum >= htons(0xffff - (IPTTLDEC << 8)))
168 		ip->ip_sum += htons(IPTTLDEC << 8) + 1;
169 	else
170 		ip->ip_sum += htons(IPTTLDEC << 8);
171 
172 	/*
173 	 * Send the packet on its way.  All we can get back is ENOBUFS
174 	 */
175 	ipf->ipf_uses++;
176 	ipf->ipf_timer = IPFLOW_TIMER;
177 
178 	if (rt->rt_flags & RTF_GATEWAY)
179 		dst = rt->rt_gateway;
180 	else
181 		dst = &ipf->ipf_ro.ro_dst;
182 
183 	if (serializer)
184 		lwkt_serialize_exit(serializer);
185 
186 	lwkt_serialize_enter(ifp->if_serializer);
187 	error = ifp->if_output(ifp, m, dst, rt);
188 	if (error) {
189 		if (error == ENOBUFS)
190 			ipf->ipf_dropped++;
191 		else
192 			ipf->ipf_errors++;
193 	}
194 	lwkt_serialize_exit(ifp->if_serializer);
195 
196 	if (serializer)
197 		lwkt_serialize_enter(serializer);
198 	return 1;
199 }
200 
201 static void
202 ipflow_addstats(struct ipflow *ipf)
203 {
204 	ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
205 	ipstat.ips_cantforward += ipf->ipf_errors + ipf->ipf_dropped;
206 	ipstat.ips_forward += ipf->ipf_uses;
207 	ipstat.ips_fastforward += ipf->ipf_uses;
208 }
209 
210 static void
211 ipflow_free(struct ipflow *ipf)
212 {
213 	/*
214 	 * Remove the flow from the hash table (at elevated IPL).
215 	 * Once it's off the list, we can deal with it at normal
216 	 * network IPL.
217 	 */
218 	crit_enter();
219 	LIST_REMOVE(ipf, ipf_next);
220 
221 	KKASSERT(ipflow_inuse > 0);
222 	ipflow_inuse--;
223 	crit_exit();
224 
225 	ipflow_addstats(ipf);
226 	RTFREE(ipf->ipf_ro.ro_rt);
227 	kfree(ipf, M_IPFLOW);
228 }
229 
230 static struct ipflow *
231 ipflow_reap(void)
232 {
233 	struct ipflow *ipf, *maybe_ipf = NULL;
234 	int idx;
235 
236 	crit_enter();
237 	for (idx = 0; idx < IPFLOW_HASHSIZE; idx++) {
238 		ipf = LIST_FIRST(&ipflows[idx]);
239 		while (ipf != NULL) {
240 			/*
241 			 * If this no longer points to a valid route
242 			 * reclaim it.
243 			 */
244 			if ((ipf->ipf_ro.ro_rt->rt_flags & RTF_UP) == 0)
245 				goto done;
246 
247 			/*
248 			 * choose the one that's been least recently used
249 			 * or has had the least uses in the last 1.5
250 			 * intervals.
251 			 */
252 			if (maybe_ipf == NULL ||
253 			    ipf->ipf_timer < maybe_ipf->ipf_timer ||
254 			    (ipf->ipf_timer == maybe_ipf->ipf_timer &&
255 			     ipf->ipf_last_uses + ipf->ipf_uses <
256 			     maybe_ipf->ipf_last_uses + maybe_ipf->ipf_uses))
257 				maybe_ipf = ipf;
258 			ipf = LIST_NEXT(ipf, ipf_next);
259 		}
260 	}
261 	ipf = maybe_ipf;
262 done:
263 	/*
264 	 * Remove the entry from the flow table.
265 	 */
266 	LIST_REMOVE(ipf, ipf_next);
267 	crit_exit();
268 
269 	ipflow_addstats(ipf);
270 	RTFREE(ipf->ipf_ro.ro_rt);
271 	return ipf;
272 }
273 
274 static void
275 ipflow_timo_dispatch(struct netmsg *nmsg)
276 {
277 	struct ipflow *ipf;
278 	int idx;
279 
280 	crit_enter();
281 	lwkt_replymsg(&nmsg->nm_lmsg, 0);	/* reply ASAP */
282 
283 	for (idx = 0; idx < IPFLOW_HASHSIZE; idx++) {
284 		ipf = LIST_FIRST(&ipflows[idx]);
285 		while (ipf != NULL) {
286 			struct ipflow *next_ipf = LIST_NEXT(ipf, ipf_next);
287 
288 			if (--ipf->ipf_timer == 0) {
289 				ipflow_free(ipf);
290 			} else {
291 				ipf->ipf_last_uses = ipf->ipf_uses;
292 				ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
293 				ipstat.ips_forward += ipf->ipf_uses;
294 				ipstat.ips_fastforward += ipf->ipf_uses;
295 				ipf->ipf_uses = 0;
296 			}
297 			ipf = next_ipf;
298 		}
299 	}
300 	crit_exit();
301 }
302 
303 static void
304 ipflow_timo_ipi(void *arg __unused)
305 {
306 	struct lwkt_msg *msg = &ipflow_timo_netmsgs[mycpuid].nm_lmsg;
307 
308 	crit_enter();
309 	if (msg->ms_flags & MSGF_DONE)
310 		lwkt_sendmsg(cpu_portfn(mycpuid), msg);
311 	crit_exit();
312 }
313 
314 void
315 ipflow_slowtimo(void)
316 {
317 #ifdef SMP
318 	lwkt_send_ipiq_mask(smp_active_mask, ipflow_timo_ipi, NULL);
319 #else
320 	ipflow_timo_ipi(NULL);
321 #endif
322 }
323 
324 static void
325 ipflow_create_oncpu(const struct route *ro, struct mbuf *m)
326 {
327 	const struct ip *const ip = mtod(m, struct ip *);
328 	struct ipflow *ipf;
329 	unsigned hash;
330 
331 	/*
332 	 * See if an existing flow struct exists.  If so remove it from it's
333 	 * list and free the old route.  If not, try to malloc a new one
334 	 * (if we aren't at our limit).
335 	 */
336 	ipf = ipflow_lookup(ip);
337 	if (ipf == NULL) {
338 		if (ipflow_inuse == IPFLOW_MAX) {
339 			ipf = ipflow_reap();
340 		} else {
341 			ipf = kmalloc(sizeof(*ipf), M_IPFLOW,
342 				      M_INTWAIT | M_NULLOK);
343 			if (ipf == NULL)
344 				return;
345 			ipflow_inuse++;
346 		}
347 		bzero(ipf, sizeof(*ipf));
348 	} else if (IPFLOW_RTENTRY_ISDOWN(ipf->ipf_ro.ro_rt)) {
349 		crit_enter();
350 		LIST_REMOVE(ipf, ipf_next);
351 		crit_exit();
352 
353 		ipflow_addstats(ipf);
354 		RTFREE(ipf->ipf_ro.ro_rt);
355 		ipf->ipf_uses = ipf->ipf_last_uses = 0;
356 		ipf->ipf_errors = ipf->ipf_dropped = 0;
357 	} else {
358 		/*
359 		 * The route entry cached in ipf is still up,
360 		 * this could happen while the ipf installation
361 		 * is in transition state.
362 		 * XXX should not happen on UP box
363 		 */
364 		return;
365 	}
366 
367 	/*
368 	 * Fill in the updated information.
369 	 */
370 	ipf->ipf_ro = *ro;
371 	ro->ro_rt->rt_refcnt++;
372 	ipf->ipf_dst = ip->ip_dst;
373 	ipf->ipf_src = ip->ip_src;
374 	ipf->ipf_tos = ip->ip_tos;
375 	ipf->ipf_timer = IPFLOW_TIMER;
376 
377 	/*
378 	 * Insert into the approriate bucket of the flow table.
379 	 */
380 	hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
381 	crit_enter();
382 	LIST_INSERT_HEAD(&ipflows[hash], ipf, ipf_next);
383 	crit_exit();
384 }
385 
386 #ifdef SMP
387 
388 static void
389 ipflow_create_dispatch(struct netmsg *nmsg)
390 {
391 	struct netmsg_packet *nmp = (struct netmsg_packet *)nmsg;
392 	struct sockaddr_in *sin;
393 	struct route ro;
394 	int nextcpu;
395 
396 	bzero(&ro, sizeof(ro));
397 	sin = (struct sockaddr_in *)&ro.ro_dst;
398 	sin->sin_family = AF_INET;
399 	sin->sin_len = sizeof(struct sockaddr_in);
400 	sin->sin_addr.s_addr = (in_addr_t)nmsg->nm_lmsg.u.ms_result32;
401 
402 	rtalloc_ign(&ro, RTF_PRCLONING);
403 	if (ro.ro_rt != NULL) {
404 		ipflow_create_oncpu(&ro, nmp->nm_packet);
405 		RTFREE(ro.ro_rt);
406 	}
407 
408 	nextcpu = mycpuid + 1;
409 	if (nextcpu < ncpus)
410 		lwkt_forwardmsg(cpu_portfn(nextcpu), &nmsg->nm_lmsg);
411 	else
412 		m_freem(nmp->nm_packet);
413 }
414 
415 #endif	/* SMP */
416 
417 void
418 ipflow_create(const struct route *ro, struct mbuf *m)
419 {
420 	const struct ip *const ip = mtod(m, struct ip *);
421 #ifdef SMP
422 	struct netmsg_packet *nmp;
423 	struct netmsg *nmsg;
424 	int nextcpu;
425 #endif
426 
427 	/*
428 	 * Don't create cache entries for ICMP messages.
429 	 */
430 	if (!ipflow_active || ip->ip_p == IPPROTO_ICMP) {
431 		m_freem(m);
432 		return;
433 	}
434 
435 #ifdef SMP
436 	nmp = &m->m_hdr.mh_netmsg;
437 	nmsg = &nmp->nm_netmsg;
438 
439 	netmsg_init(nmsg, &netisr_apanic_rport, 0, ipflow_create_dispatch);
440 	nmp->nm_packet = m;
441 	nmsg->nm_lmsg.u.ms_result32 =
442 		((const struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr;
443 
444 	if (mycpuid == 0) {
445 		ipflow_create_oncpu(ro, m);
446 		nextcpu = 1;
447 	} else {
448 		nextcpu = 0;
449 	}
450 	if (nextcpu < ncpus)
451 		lwkt_sendmsg(cpu_portfn(nextcpu), &nmsg->nm_lmsg);
452 	else
453 		m_freem(m);
454 #else
455 	ipflow_create_oncpu(ro, m);
456 	m_freem(m);
457 #endif
458 }
459 
460 static void
461 ipflow_init(void)
462 {
463 	char oid_name[32];
464 	int i;
465 
466 	for (i = 0; i < ncpus; ++i) {
467 		netmsg_init(&ipflow_timo_netmsgs[i], &netisr_adone_rport, 0,
468 			    ipflow_timo_dispatch);
469 
470 		ksnprintf(oid_name, sizeof(oid_name), "inuse%d", i);
471 
472 		SYSCTL_ADD_INT(NULL,
473 		SYSCTL_STATIC_CHILDREN(_net_inet_ip_ipflow),
474 		OID_AUTO, oid_name, CTLFLAG_RD, &ipflow_inuse_pcpu[i], 0,
475 		"# of ip flow being used");
476 	}
477 }
478 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, ipflow_init, 0);
479