2 * Copyright (c) 2015 Gleb Smirnoff <glebius@FreeBSD.org>
3 * Copyright (c) 2015 Adrian Chadd <adrian@FreeBSD.org>
4 * Copyright (c) 1982, 1986, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/eventhandler.h>
42 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/limits.h>
48 #include <sys/mutex.h>
49 #include <sys/sysctl.h>
50 #include <sys/socket.h>
53 #include <net/if_var.h>
54 #include <net/rss_config.h>
55 #include <net/netisr.h>
58 #include <netinet/in.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/in_rss.h>
63 #include <security/mac/mac_framework.h>
66 SYSCTL_DECL(_net_inet_ip);
69 * Reassembly headers are stored in hash buckets.
71 #define IPREASS_NHASH_LOG2 10
72 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
73 #define IPREASS_HMASK (V_ipq_hashsize - 1)
76 TAILQ_HEAD(ipqhead, ipq) head;
85 VNET_DEFINE_STATIC(struct ipqbucket *, ipq);
86 #define V_ipq VNET(ipq)
87 VNET_DEFINE_STATIC(uint32_t, ipq_hashseed);
88 #define V_ipq_hashseed VNET(ipq_hashseed)
89 VNET_DEFINE_STATIC(uint32_t, ipq_hashsize);
90 #define V_ipq_hashsize VNET(ipq_hashsize)
92 #define IPQ_LOCK(i) mtx_lock(&V_ipq[i].lock)
93 #define IPQ_TRYLOCK(i) mtx_trylock(&V_ipq[i].lock)
94 #define IPQ_UNLOCK(i) mtx_unlock(&V_ipq[i].lock)
95 #define IPQ_LOCK_ASSERT(i) mtx_assert(&V_ipq[i].lock, MA_OWNED)
96 #define IPQ_BUCKET_LOCK_ASSERT(b) mtx_assert(&(b)->lock, MA_OWNED)
98 VNET_DEFINE_STATIC(int, ipreass_maxbucketsize);
99 #define V_ipreass_maxbucketsize VNET(ipreass_maxbucketsize)
101 void ipreass_init(void);
102 void ipreass_vnet_init(void);
104 void ipreass_destroy(void);
106 static int sysctl_maxfragpackets(SYSCTL_HANDLER_ARGS);
107 static int sysctl_maxfragbucketsize(SYSCTL_HANDLER_ARGS);
108 static int sysctl_fragttl(SYSCTL_HANDLER_ARGS);
109 static void ipreass_zone_change(void *);
110 static void ipreass_drain_tomax(void);
111 static void ipq_free(struct ipqbucket *, struct ipq *);
112 static struct ipq * ipq_reuse(int);
113 static void ipreass_callout(void *);
114 static void ipreass_reschedule(struct ipqbucket *);
117 ipq_timeout(struct ipqbucket *bucket, struct ipq *fp)
120 IPSTAT_ADD(ips_fragtimeout, fp->ipq_nfrags);
121 ipq_free(bucket, fp);
125 ipq_drop(struct ipqbucket *bucket, struct ipq *fp)
128 IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
129 ipq_free(bucket, fp);
130 ipreass_reschedule(bucket);
134 * By default, limit the number of IP fragments across all reassembly
135 * queues to 1/32 of the total number of mbuf clusters.
137 * Limit the total number of reassembly queues per VNET to the
138 * IP fragment limit, but ensure the limit will not allow any bucket
139 * to grow above 100 items. (The bucket limit is
140 * IP_MAXFRAGPACKETS / (V_ipq_hashsize / 2), so the 50 is the correct
141 * multiplier to reach a 100-item limit.)
142 * The 100-item limit was chosen as brief testing seems to show that
143 * this produces "reasonable" performance on some subset of systems
146 #define IP_MAXFRAGS (nmbclusters / 32)
147 #define IP_MAXFRAGPACKETS (imin(IP_MAXFRAGS, V_ipq_hashsize * 50))
150 static u_int __exclusive_cache_line nfrags;
151 SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfrags, CTLFLAG_RW,
153 "Maximum number of IPv4 fragments allowed across all reassembly queues");
154 SYSCTL_UINT(_net_inet_ip, OID_AUTO, curfrags, CTLFLAG_RD,
156 "Current number of IPv4 fragments across all reassembly queues");
158 VNET_DEFINE_STATIC(uma_zone_t, ipq_zone);
159 #define V_ipq_zone VNET(ipq_zone)
161 SYSCTL_UINT(_net_inet_ip, OID_AUTO, reass_hashsize,
162 CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(ipq_hashsize), 0,
163 "Size of IP fragment reassembly hashtable");
165 SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets,
166 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
167 NULL, 0, sysctl_maxfragpackets, "I",
168 "Maximum number of IPv4 fragment reassembly queue entries");
169 SYSCTL_UMA_CUR(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_VNET,
170 &VNET_NAME(ipq_zone),
171 "Current number of IPv4 fragment reassembly queue entries");
173 VNET_DEFINE_STATIC(int, noreass);
174 #define V_noreass VNET(noreass)
176 VNET_DEFINE_STATIC(int, maxfragsperpacket);
177 #define V_maxfragsperpacket VNET(maxfragsperpacket)
178 SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_VNET | CTLFLAG_RW,
179 &VNET_NAME(maxfragsperpacket), 0,
180 "Maximum number of IPv4 fragments allowed per packet");
181 SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragbucketsize,
182 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0,
183 sysctl_maxfragbucketsize, "I",
184 "Maximum number of IPv4 fragment reassembly queue entries per bucket");
186 VNET_DEFINE_STATIC(u_int, ipfragttl) = 30;
187 #define V_ipfragttl VNET(ipfragttl)
188 SYSCTL_PROC(_net_inet_ip, OID_AUTO, fragttl, CTLTYPE_INT | CTLFLAG_RW |
189 CTLFLAG_MPSAFE | CTLFLAG_VNET, NULL, 0, sysctl_fragttl, "IU",
190 "IP fragment life time on reassembly queue (seconds)");
193 * Take incoming datagram fragment and try to reassemble it into
194 * whole datagram. If the argument is the first fragment or one
195 * in between the function will return NULL and store the mbuf
196 * in the fragment chain. If the argument is the last fragment
197 * the packet will be reassembled and the pointer to the new
198 * mbuf returned for further processing. Only m_tags attached
199 * to the first packet/fragment are preserved.
200 * The IP header is *NOT* adjusted out of iplen.
202 #define M_IP_FRAG M_PROTO9
204 ip_reass(struct mbuf *m)
207 struct mbuf *p, *q, *nq, *t;
209 struct ifnet *srcifp;
210 struct ipqhead *head;
211 int i, hlen, next, tmpmax;
213 uint32_t hash, hashkey[3];
215 uint32_t rss_hash, rss_type;
219 * If no reassembling or maxfragsperpacket are 0,
220 * never accept fragments.
221 * Also, drop packet if it would exceed the maximum
222 * number of fragments.
225 if (V_noreass == 1 || V_maxfragsperpacket == 0 ||
226 (tmpmax >= 0 && atomic_load_int(&nfrags) >= (u_int)tmpmax)) {
227 IPSTAT_INC(ips_fragments);
228 IPSTAT_INC(ips_fragdropped);
233 ip = mtod(m, struct ip *);
234 hlen = ip->ip_hl << 2;
237 * Adjust ip_len to not reflect header,
238 * convert offset of this to bytes.
240 ip->ip_len = htons(ntohs(ip->ip_len) - hlen);
242 * Make sure that fragments have a data length
243 * that's a non-zero multiple of 8 bytes, unless
244 * this is the last fragment.
246 if (ip->ip_len == htons(0) ||
247 ((ip->ip_off & htons(IP_MF)) && (ntohs(ip->ip_len) & 0x7) != 0)) {
248 IPSTAT_INC(ips_toosmall); /* XXX */
249 IPSTAT_INC(ips_fragdropped);
253 if (ip->ip_off & htons(IP_MF))
254 m->m_flags |= M_IP_FRAG;
256 m->m_flags &= ~M_IP_FRAG;
257 ip->ip_off = htons(ntohs(ip->ip_off) << 3);
260 * Make sure the fragment lies within a packet of valid size.
262 if (ntohs(ip->ip_len) + ntohs(ip->ip_off) > IP_MAXPACKET) {
263 IPSTAT_INC(ips_toolong);
264 IPSTAT_INC(ips_fragdropped);
270 * Store receive network interface pointer for later.
272 srcifp = m->m_pkthdr.rcvif;
275 * Attempt reassembly; if it succeeds, proceed.
276 * ip_reass() will return a different mbuf.
278 IPSTAT_INC(ips_fragments);
279 m->m_pkthdr.PH_loc.ptr = ip;
282 * Presence of header sizes in mbufs
283 * would confuse code below.
288 hashkey[0] = ip->ip_src.s_addr;
289 hashkey[1] = ip->ip_dst.s_addr;
290 hashkey[2] = (uint32_t)ip->ip_p << 16;
291 hashkey[2] += ip->ip_id;
292 hash = jenkins_hash32(hashkey, nitems(hashkey), V_ipq_hashseed);
293 hash &= IPREASS_HMASK;
294 head = &V_ipq[hash].head;
298 * Look for queue of fragments
301 TAILQ_FOREACH(fp, head, ipq_list)
302 if (ip->ip_id == fp->ipq_id &&
303 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
304 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
306 mac_ipq_match(m, fp) &&
308 ip->ip_p == fp->ipq_p)
311 * If first fragment to arrive, create a reassembly queue.
314 if (V_ipq[hash].count < V_ipreass_maxbucketsize)
315 fp = uma_zalloc(V_ipq_zone, M_NOWAIT);
317 fp = ipq_reuse(hash);
321 if (mac_ipq_init(fp, M_NOWAIT) != 0) {
322 uma_zfree(V_ipq_zone, fp);
326 mac_ipq_create(m, fp);
328 TAILQ_INSERT_HEAD(head, fp, ipq_list);
331 atomic_add_int(&nfrags, 1);
332 fp->ipq_expire = time_uptime + V_ipfragttl;
333 fp->ipq_p = ip->ip_p;
334 fp->ipq_id = ip->ip_id;
335 fp->ipq_src = ip->ip_src;
336 fp->ipq_dst = ip->ip_dst;
338 if (m->m_flags & M_IP_FRAG)
341 fp->ipq_maxoff = ntohs(ip->ip_off) + ntohs(ip->ip_len);
343 if (fp == TAILQ_LAST(head, ipqhead))
344 callout_reset_sbt(&V_ipq[hash].timer,
345 SBT_1S * V_ipfragttl, SBT_1S, ipreass_callout,
348 MPASS(callout_active(&V_ipq[hash].timer));
352 * If we already saw the last fragment, make sure
353 * this fragment's offset looks sane. Otherwise, if
354 * this is the last fragment, record its endpoint.
356 if (fp->ipq_maxoff > 0) {
357 i = ntohs(ip->ip_off) + ntohs(ip->ip_len);
358 if (((m->m_flags & M_IP_FRAG) && i >= fp->ipq_maxoff) ||
359 ((m->m_flags & M_IP_FRAG) == 0 &&
360 i != fp->ipq_maxoff)) {
364 } else if ((m->m_flags & M_IP_FRAG) == 0)
365 fp->ipq_maxoff = ntohs(ip->ip_off) + ntohs(ip->ip_len);
367 atomic_add_int(&nfrags, 1);
369 mac_ipq_update(m, fp);
373 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.PH_loc.ptr))
376 * Handle ECN by comparing this segment with the first one;
377 * if CE is set, do not lose CE.
378 * drop if CE and not-ECT are mixed for the same packet.
380 ecn = ip->ip_tos & IPTOS_ECN_MASK;
381 ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
382 if (ecn == IPTOS_ECN_CE) {
383 if (ecn0 == IPTOS_ECN_NOTECT)
385 if (ecn0 != IPTOS_ECN_CE)
386 GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
388 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
392 * Find a segment which begins after this one does.
394 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
395 if (ntohs(GETIP(q)->ip_off) > ntohs(ip->ip_off))
399 * If there is a preceding segment, it may provide some of
400 * our data already. If so, drop the data from the incoming
401 * segment. If it provides all of our data, drop us, otherwise
402 * stick new segment in the proper place.
404 * If some of the data is dropped from the preceding
405 * segment, then it's checksum is invalidated.
408 i = ntohs(GETIP(p)->ip_off) + ntohs(GETIP(p)->ip_len) -
411 if (i >= ntohs(ip->ip_len))
414 m->m_pkthdr.csum_flags = 0;
415 ip->ip_off = htons(ntohs(ip->ip_off) + i);
416 ip->ip_len = htons(ntohs(ip->ip_len) - i);
418 m->m_nextpkt = p->m_nextpkt;
421 m->m_nextpkt = fp->ipq_frags;
426 * While we overlap succeeding segments trim them or,
427 * if they are completely covered, dequeue them.
429 for (; q != NULL && ntohs(ip->ip_off) + ntohs(ip->ip_len) >
430 ntohs(GETIP(q)->ip_off); q = nq) {
431 i = (ntohs(ip->ip_off) + ntohs(ip->ip_len)) -
432 ntohs(GETIP(q)->ip_off);
433 if (i < ntohs(GETIP(q)->ip_len)) {
434 GETIP(q)->ip_len = htons(ntohs(GETIP(q)->ip_len) - i);
435 GETIP(q)->ip_off = htons(ntohs(GETIP(q)->ip_off) + i);
437 q->m_pkthdr.csum_flags = 0;
442 IPSTAT_INC(ips_fragdropped);
444 atomic_subtract_int(&nfrags, 1);
449 * Check for complete reassembly and perform frag per packet
452 * Frag limiting is performed here so that the nth frag has
453 * a chance to complete the packet before we drop the packet.
454 * As a result, n+1 frags are actually allowed per packet, but
455 * only n will ever be stored. (n = maxfragsperpacket.)
459 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
460 if (ntohs(GETIP(q)->ip_off) != next) {
461 if (fp->ipq_nfrags > V_maxfragsperpacket)
462 ipq_drop(&V_ipq[hash], fp);
465 next += ntohs(GETIP(q)->ip_len);
467 /* Make sure the last packet didn't have the IP_MF flag */
468 if (p->m_flags & M_IP_FRAG) {
469 if (fp->ipq_nfrags > V_maxfragsperpacket)
470 ipq_drop(&V_ipq[hash], fp);
475 * Reassembly is complete. Make sure the packet is a sane size.
479 if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
480 IPSTAT_INC(ips_toolong);
481 ipq_drop(&V_ipq[hash], fp);
486 * Concatenate fragments.
494 for (q = nq; q != NULL; q = nq) {
497 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
498 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
503 * In order to do checksumming faster we do 'end-around carry' here
504 * (and not in for{} loop), though it implies we are not going to
505 * reassemble more than 64k fragments.
507 while (m->m_pkthdr.csum_data & 0xffff0000)
508 m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
509 (m->m_pkthdr.csum_data >> 16);
510 atomic_subtract_int(&nfrags, fp->ipq_nfrags);
512 mac_ipq_reassemble(fp, m);
517 * Create header for new ip packet by modifying header of first
518 * packet; dequeue and discard fragment reassembly header.
519 * Make header visible.
521 ip->ip_len = htons((ip->ip_hl << 2) + next);
522 ip->ip_src = fp->ipq_src;
523 ip->ip_dst = fp->ipq_dst;
524 TAILQ_REMOVE(head, fp, ipq_list);
526 uma_zfree(V_ipq_zone, fp);
527 m->m_len += (ip->ip_hl << 2);
528 m->m_data -= (ip->ip_hl << 2);
529 /* some debugging cruft by sklower, below, will go away soon */
530 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
532 /* set valid receive interface pointer */
533 m->m_pkthdr.rcvif = srcifp;
535 IPSTAT_INC(ips_reassembled);
536 ipreass_reschedule(&V_ipq[hash]);
541 * Query the RSS layer for the flowid / flowtype for the
544 * For now, just assume we have to calculate a new one.
545 * Later on we should check to see if the assigned flowid matches
546 * what RSS wants for the given IP protocol and if so, just keep it.
548 * We then queue into the relevant netisr so it can be dispatched
549 * to the correct CPU.
551 * Note - this may return 1, which means the flowid in the mbuf
552 * is correct for the configured RSS hash types and can be used.
554 if (rss_mbuf_software_hash_v4(m, 0, &rss_hash, &rss_type) == 0) {
555 m->m_pkthdr.flowid = rss_hash;
556 M_HASHTYPE_SET(m, rss_type);
560 * Queue/dispatch for reprocessing.
562 * Note: this is much slower than just handling the frame in the
563 * current receive context. It's likely worth investigating
566 netisr_dispatch(NETISR_IP_DIRECT, m);
574 IPSTAT_INC(ips_fragdropped);
577 atomic_subtract_int(&nfrags, 1);
588 * Timer expired on a bucket.
589 * There should be at least one ipq to be timed out.
592 ipreass_callout(void *arg)
594 struct ipqbucket *bucket = arg;
597 IPQ_BUCKET_LOCK_ASSERT(bucket);
598 MPASS(atomic_load_int(&nfrags) > 0);
600 CURVNET_SET(bucket->vnet);
601 fp = TAILQ_LAST(&bucket->head, ipqhead);
602 KASSERT(fp != NULL && fp->ipq_expire <= time_uptime,
603 ("%s: stray callout on bucket %p, %ju < %ju", __func__, bucket,
604 fp ? (uintmax_t)fp->ipq_expire : 0, (uintmax_t)time_uptime));
606 while (fp != NULL && fp->ipq_expire <= time_uptime) {
607 ipq_timeout(bucket, fp);
608 fp = TAILQ_LAST(&bucket->head, ipqhead);
610 ipreass_reschedule(bucket);
615 ipreass_reschedule(struct ipqbucket *bucket)
619 IPQ_BUCKET_LOCK_ASSERT(bucket);
621 if ((fp = TAILQ_LAST(&bucket->head, ipqhead)) != NULL) {
624 /* Protect against time_uptime tick. */
625 t = fp->ipq_expire - time_uptime;
627 callout_reset_sbt(&bucket->timer, SBT_1S * t, SBT_1S,
628 ipreass_callout, bucket, 0);
630 callout_stop(&bucket->timer);
634 ipreass_drain_vnet(void)
638 for (int i = 0; i < V_ipq_hashsize; i++) {
642 resched = !TAILQ_EMPTY(&V_ipq[i].head);
643 while(!TAILQ_EMPTY(&V_ipq[i].head)) {
644 struct ipq *fp = TAILQ_FIRST(&V_ipq[i].head);
646 dropped += fp->ipq_nfrags;
647 ipq_free(&V_ipq[i], fp);
650 ipreass_reschedule(&V_ipq[i]);
651 KASSERT(V_ipq[i].count == 0,
652 ("%s: V_ipq[%d] count %d (V_ipq=%p)", __func__, i,
653 V_ipq[i].count, V_ipq));
656 IPSTAT_ADD(ips_fragdropped, dropped);
660 * Drain off all datagram fragments.
665 VNET_ITERATOR_DECL(vnet_iter);
667 VNET_FOREACH(vnet_iter) {
668 CURVNET_SET(vnet_iter);
669 ipreass_drain_vnet();
676 * Initialize IP reassembly structures.
678 MALLOC_DEFINE(M_IPREASS_HASH, "IP reass", "IP packet reassembly hash headers");
680 ipreass_vnet_init(void)
684 V_ipq_hashsize = IPREASS_NHASH;
685 TUNABLE_INT_FETCH("net.inet.ip.reass_hashsize", &V_ipq_hashsize);
686 V_ipq = malloc(sizeof(struct ipqbucket) * V_ipq_hashsize,
687 M_IPREASS_HASH, M_WAITOK);
689 for (int i = 0; i < V_ipq_hashsize; i++) {
690 TAILQ_INIT(&V_ipq[i].head);
691 mtx_init(&V_ipq[i].lock, "IP reassembly", NULL,
692 MTX_DEF | MTX_DUPOK | MTX_NEW);
693 callout_init_mtx(&V_ipq[i].timer, &V_ipq[i].lock, 0);
696 V_ipq[i].vnet = curvnet;
699 V_ipq_hashseed = arc4random();
700 V_maxfragsperpacket = 16;
701 V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
702 NULL, UMA_ALIGN_PTR, 0);
703 max = IP_MAXFRAGPACKETS;
704 max = uma_zone_set_max(V_ipq_zone, max);
705 V_ipreass_maxbucketsize = imax(max / (V_ipq_hashsize / 2), 1);
712 maxfrags = IP_MAXFRAGS;
713 EVENTHANDLER_REGISTER(nmbclusters_change, ipreass_zone_change,
714 NULL, EVENTHANDLER_PRI_ANY);
715 EVENTHANDLER_REGISTER(vm_lowmem, ipreass_drain, NULL,
717 EVENTHANDLER_REGISTER(mbuf_lowmem, ipreass_drain, NULL,
722 * Drain off all datagram fragments belonging to
723 * the given network interface.
726 ipreass_cleanup(void *arg __unused, struct ifnet *ifp)
728 struct ipq *fp, *temp;
732 KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__));
734 CURVNET_SET_QUIET(ifp->if_vnet);
737 * Skip processing if IPv4 reassembly is not initialised or
738 * torn down by ipreass_destroy().
740 if (V_ipq_zone == NULL) {
745 for (i = 0; i < V_ipq_hashsize; i++) {
747 /* Scan fragment list. */
748 TAILQ_FOREACH_SAFE(fp, &V_ipq[i].head, ipq_list, temp) {
749 for (m = fp->ipq_frags; m != NULL; m = m->m_nextpkt) {
750 /* clear no longer valid rcvif pointer */
751 if (m->m_pkthdr.rcvif == ifp)
752 m->m_pkthdr.rcvif = NULL;
759 EVENTHANDLER_DEFINE(ifnet_departure_event, ipreass_cleanup, NULL, 0);
763 * Destroy IP reassembly structures.
766 ipreass_destroy(void)
769 ipreass_drain_vnet();
770 uma_zdestroy(V_ipq_zone);
772 for (int i = 0; i < V_ipq_hashsize; i++)
773 mtx_destroy(&V_ipq[i].lock);
774 free(V_ipq, M_IPREASS_HASH);
779 * After maxnipq has been updated, propagate the change to UMA. The UMA zone
780 * max has slightly different semantics than the sysctl, for historical
784 ipreass_drain_tomax(void)
790 * Make sure each bucket is under the new limit. If
791 * necessary, drop enough of the oldest elements from
792 * each bucket to get under the new limit.
794 for (int i = 0; i < V_ipq_hashsize; i++) {
796 while (V_ipq[i].count > V_ipreass_maxbucketsize &&
797 (fp = TAILQ_LAST(&V_ipq[i].head, ipqhead)) != NULL)
798 ipq_timeout(&V_ipq[i], fp);
799 ipreass_reschedule(&V_ipq[i]);
804 * If we are over the maximum number of fragments,
805 * drain off enough to get down to the new limit,
806 * stripping off last elements on queues. Every
807 * run we strip the oldest element from each bucket.
809 target = uma_zone_get_max(V_ipq_zone);
810 while (uma_zone_get_cur(V_ipq_zone) > target) {
811 for (int i = 0; i < V_ipq_hashsize; i++) {
813 fp = TAILQ_LAST(&V_ipq[i].head, ipqhead);
815 ipq_timeout(&V_ipq[i], fp);
816 ipreass_reschedule(&V_ipq[i]);
824 ipreass_zone_change(void *tag)
826 VNET_ITERATOR_DECL(vnet_iter);
829 maxfrags = IP_MAXFRAGS;
830 max = IP_MAXFRAGPACKETS;
831 VNET_LIST_RLOCK_NOSLEEP();
832 VNET_FOREACH(vnet_iter) {
833 CURVNET_SET(vnet_iter);
834 max = uma_zone_set_max(V_ipq_zone, max);
835 V_ipreass_maxbucketsize = imax(max / (V_ipq_hashsize / 2), 1);
836 ipreass_drain_tomax();
839 VNET_LIST_RUNLOCK_NOSLEEP();
843 * Change the limit on the UMA zone, or disable the fragment allocation
844 * at all. Since 0 and -1 is a special values here, we need our own handler,
845 * instead of sysctl_handle_uma_zone_max().
848 sysctl_maxfragpackets(SYSCTL_HANDLER_ARGS)
852 if (V_noreass == 0) {
853 max = uma_zone_get_max(V_ipq_zone);
858 error = sysctl_handle_int(oidp, &max, 0, req);
859 if (error || !req->newptr)
863 * XXXRW: Might be a good idea to sanity check the argument
864 * and place an extreme upper bound.
866 max = uma_zone_set_max(V_ipq_zone, max);
867 V_ipreass_maxbucketsize = imax(max / (V_ipq_hashsize / 2), 1);
868 ipreass_drain_tomax();
870 } else if (max == 0) {
873 } else if (max == -1) {
875 uma_zone_set_max(V_ipq_zone, 0);
876 V_ipreass_maxbucketsize = INT_MAX;
883 * Seek for old fragment queue header that can be reused. Try to
884 * reuse a header from currently locked hash bucket.
892 IPQ_LOCK_ASSERT(start);
894 for (i = 0; i < V_ipq_hashsize; i++) {
895 bucket = (start + i) % V_ipq_hashsize;
896 if (bucket != start && IPQ_TRYLOCK(bucket) == 0)
898 fp = TAILQ_LAST(&V_ipq[bucket].head, ipqhead);
902 IPSTAT_ADD(ips_fragtimeout, fp->ipq_nfrags);
903 atomic_subtract_int(&nfrags, fp->ipq_nfrags);
904 while (fp->ipq_frags) {
906 fp->ipq_frags = m->m_nextpkt;
909 TAILQ_REMOVE(&V_ipq[bucket].head, fp, ipq_list);
910 V_ipq[bucket].count--;
911 ipreass_reschedule(&V_ipq[bucket]);
919 IPQ_LOCK_ASSERT(start);
924 * Free a fragment reassembly header and all associated datagrams.
927 ipq_free(struct ipqbucket *bucket, struct ipq *fp)
931 atomic_subtract_int(&nfrags, fp->ipq_nfrags);
932 while (fp->ipq_frags) {
934 fp->ipq_frags = q->m_nextpkt;
937 TAILQ_REMOVE(&bucket->head, fp, ipq_list);
939 uma_zfree(V_ipq_zone, fp);
943 * Get or set the maximum number of reassembly queues per bucket.
946 sysctl_maxfragbucketsize(SYSCTL_HANDLER_ARGS)
950 max = V_ipreass_maxbucketsize;
951 error = sysctl_handle_int(oidp, &max, 0, req);
952 if (error || !req->newptr)
956 V_ipreass_maxbucketsize = max;
957 ipreass_drain_tomax();
962 * Get or set the IP fragment time to live.
965 sysctl_fragttl(SYSCTL_HANDLER_ARGS)
971 error = sysctl_handle_int(oidp, &ttl, 0, req);
972 if (error || !req->newptr)
975 if (ttl < 1 || ttl > MAXTTL)
978 atomic_store_int(&V_ipfragttl, ttl);