tcp_vtw.h revision 1.6 1 1.6 joerg /* $NetBSD: tcp_vtw.h,v 1.6 2012/11/23 14:48:31 joerg Exp $ */
2 1.1 dyoung /*
3 1.1 dyoung * Copyright (c) 2011 The NetBSD Foundation, Inc.
4 1.1 dyoung * All rights reserved.
5 1.1 dyoung *
6 1.1 dyoung * This code is derived from software contributed to The NetBSD Foundation
7 1.1 dyoung * by Coyote Point Systems, Inc.
8 1.1 dyoung *
9 1.1 dyoung * Redistribution and use in source and binary forms, with or without
10 1.1 dyoung * modification, are permitted provided that the following conditions
11 1.1 dyoung * are met:
12 1.1 dyoung * 1. Redistributions of source code must retain the above copyright
13 1.1 dyoung * notice, this list of conditions and the following disclaimer.
14 1.1 dyoung * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 dyoung * notice, this list of conditions and the following disclaimer in the
16 1.1 dyoung * documentation and/or other materials provided with the distribution.
17 1.1 dyoung *
18 1.1 dyoung * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 dyoung * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 dyoung * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 dyoung * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 dyoung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 dyoung * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 dyoung * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 dyoung * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 dyoung * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 dyoung * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 dyoung * POSSIBILITY OF SUCH DAMAGE.
29 1.1 dyoung */
30 1.1 dyoung
31 1.1 dyoung /*
32 1.1 dyoung * Vestigial time-wait.
33 1.1 dyoung *
34 1.1 dyoung * This implementation uses cache-efficient techniques, which will
35 1.1 dyoung * appear somewhat peculiar. The main philosophy is to optimise the
36 1.1 dyoung * amount of information available within a cache line. Cache miss is
37 1.1 dyoung * expensive. So we employ ad-hoc techniques to pull a series of
38 1.1 dyoung * linked-list follows into a cache line. One cache line, multiple
39 1.1 dyoung * linked-list equivalents.
40 1.1 dyoung *
41 1.1 dyoung * One such ad-hoc technique is fat pointers. Additional degrees of
42 1.1 dyoung * ad-hoqueness result from having to hand tune it for pointer size
43 1.1 dyoung * and for cache line size.
44 1.1 dyoung *
45 1.1 dyoung * The 'fat pointer' approach aggregates, for x86_32, 15 linked-list
46 1.1 dyoung * data structures into one cache line. The additional 32 bits in the
47 1.1 dyoung * cache line are used for linking fat pointers, and for
48 1.1 dyoung * allocation/bookkeeping.
49 1.1 dyoung *
50 1.1 dyoung * The 15 32-bit tags encode the pointers to the linked list elements,
51 1.1 dyoung * and also encode the results of a search comparison.
52 1.1 dyoung *
53 1.1 dyoung * First, some more assumptions/restrictions.
54 1.1 dyoung *
55 1.1 dyoung * All the fat pointers are from a contiguous allocation arena. Thus,
56 1.1 dyoung * we can refer to them by offset from a base, not as full pointers.
57 1.1 dyoung *
58 1.1 dyoung * All the linked list data elements are also from a contiguous
59 1.1 dyoung * allocation arena, again so that we can refer to them as offset from
60 1.1 dyoung * a base.
61 1.1 dyoung *
62 1.1 dyoung * In order to add a data element to a fat pointer, a key value is
63 1.1 dyoung * computed, based on unique data within the data element. It is the
64 1.1 dyoung * linear searching of the linked lists of these elements based on
65 1.1 dyoung * these unique data that are being optimised here.
66 1.1 dyoung *
67 1.1 dyoung * Lets call the function that computes the key k(e), where e is the
68 1.1 dyoung * data element. In this example, k(e) returns 32-bits.
69 1.1 dyoung *
70 1.1 dyoung * Consider a set E (say of order 15) of data elements. Let K be
71 1.1 dyoung * the set of the k(e) for e in E.
72 1.1 dyoung *
73 1.1 dyoung * Let O be the set of the offsets from the base of the data elements in E.
74 1.1 dyoung *
75 1.1 dyoung * For each x in K, for each matching o in O, let t be x ^ o. These
76 1.1 dyoung * are the tags. (More or less).
77 1.1 dyoung *
78 1.1 dyoung * In order to search all the data elements in E, we compute the
79 1.1 dyoung * search key, and one at a time, XOR the key into the tags. If any
80 1.1 dyoung * result is a valid data element index, we have a possible match. If
81 1.1 dyoung * not, there is no match.
82 1.1 dyoung *
83 1.1 dyoung * The no-match cases mean we do not have to de-reference the pointer
84 1.1 dyoung * to the data element in question. We save cache miss penalty and
85 1.1 dyoung * cache load decreases. Only in the case of a valid looking data
86 1.1 dyoung * element index, do we have to look closer.
87 1.1 dyoung *
88 1.1 dyoung * Thus, in the absence of false positives, 15 data elements can be
89 1.1 dyoung * searched with one cache line fill, as opposed to 15 cache line
90 1.1 dyoung * fills for the usual implementation.
91 1.1 dyoung *
92 1.1 dyoung * The vestigial time waits (vtw_t), the data elements in the above, are
93 1.1 dyoung * searched by faddr, fport, laddr, lport. The key is a function of
94 1.1 dyoung * these values.
95 1.1 dyoung *
96 1.1 dyoung * We hash these keys into the traditional hash chains to reduce the
97 1.1 dyoung * search time, and use fat pointers to reduce the cache impacts of
98 1.1 dyoung * searching.
99 1.1 dyoung *
100 1.1 dyoung * The vtw_t are, per requirement, in a contiguous chunk. Allocation
101 1.1 dyoung * is done with a clock hand, and all vtw_t within one allocation
102 1.1 dyoung * domain have the same lifetime, so they will always be sorted by
103 1.1 dyoung * age.
104 1.1 dyoung *
105 1.1 dyoung * A vtw_t will be allocated, timestamped, and have a fixed future
106 1.1 dyoung * expiration. It will be added to a hash bucket implemented with fat
107 1.1 dyoung * pointers, which means that a cache line will be allocated in the
108 1.1 dyoung * hash bucket, placed at the head (more recent in time) and the vtw_t
109 1.1 dyoung * will be added to this. As more entries are added, the fat pointer
110 1.1 dyoung * cache line will fill, requiring additional cache lines for fat
111 1.1 dyoung * pointers to be allocated. These will be added at the head, and the
112 1.1 dyoung * aged entries will hang down, tapeworm like. As the vtw_t entries
113 1.1 dyoung * expire, the corresponding slot in the fat pointer will be
114 1.1 dyoung * reclaimed, and eventually the cache line will completely empty and
115 1.1 dyoung * be re-cycled, if not at the head of the chain.
116 1.1 dyoung *
117 1.1 dyoung * At times, a time-wait timer is restarted. This corresponds to
118 1.1 dyoung * deleting the current entry and re-adding it.
119 1.1 dyoung *
120 1.1 dyoung * Most of the time, they are just placed here to die.
121 1.1 dyoung */
122 1.1 dyoung #ifndef _NETINET_TCP_VTW_H
123 1.1 dyoung #define _NETINET_TCP_VTW_H
124 1.1 dyoung
125 1.1 dyoung #include <sys/types.h>
126 1.1 dyoung #include <sys/socket.h>
127 1.1 dyoung #include <sys/sysctl.h>
128 1.1 dyoung #include <net/if.h>
129 1.1 dyoung #include <net/route.h>
130 1.1 dyoung #include <netinet/in.h>
131 1.1 dyoung #include <netinet/in_systm.h>
132 1.1 dyoung #include <netinet/ip.h>
133 1.1 dyoung #include <netinet/in_pcb.h>
134 1.1 dyoung #include <netinet/in_var.h>
135 1.1 dyoung #include <netinet/ip_var.h>
136 1.1 dyoung #include <netinet/in.h>
137 1.1 dyoung #include <netinet/tcp.h>
138 1.1 dyoung #include <netinet/tcp_timer.h>
139 1.1 dyoung #include <netinet/tcp_var.h>
140 1.1 dyoung #include <netinet6/in6.h>
141 1.1 dyoung #include <netinet/ip6.h>
142 1.1 dyoung #include <netinet6/ip6_var.h>
143 1.1 dyoung #include <netinet6/in6_pcb.h>
144 1.1 dyoung #include <netinet6/ip6_var.h>
145 1.1 dyoung #include <netinet6/in6_var.h>
146 1.1 dyoung #include <netinet/icmp6.h>
147 1.1 dyoung #include <netinet6/nd6.h>
148 1.1 dyoung
149 1.1 dyoung #define VTW_NCLASS (1+3) /* # different classes */
150 1.1 dyoung
151 1.1 dyoung /*
152 1.1 dyoung * fat pointers, MI.
153 1.1 dyoung */
154 1.1 dyoung struct fatp_mi;
155 1.1 dyoung
156 1.1 dyoung typedef uint32_t fatp_word_t;
157 1.1 dyoung
158 1.1 dyoung typedef struct fatp_mi fatp_t;
159 1.1 dyoung
160 1.1 dyoung /* Supported cacheline sizes: 32 64 128 bytes. See fatp_key(),
161 1.1 dyoung * fatp_slot_from_key(), fatp_xtra[].
162 1.1 dyoung */
163 1.1 dyoung #define FATP_NTAGS (CACHE_LINE_SIZE / sizeof(fatp_word_t) - 1)
164 1.1 dyoung #define FATP_NXT_WIDTH (sizeof(fatp_word_t) * NBBY - FATP_NTAGS)
165 1.1 dyoung
166 1.1 dyoung #define FATP_MAX (1 << FATP_NXT_WIDTH)
167 1.1 dyoung
168 1.1 dyoung /* Worked example: ULP32 with 64-byte cacheline (32-bit x86):
169 1.1 dyoung * 15 tags per cacheline. At most 2^17 fat pointers per fatp_ctl_t.
170 1.1 dyoung * The comments on the fatp_mi members, below, correspond to the worked
171 1.1 dyoung * example.
172 1.1 dyoung */
173 1.1 dyoung struct fatp_mi {
174 1.1 dyoung fatp_word_t inuse : FATP_NTAGS; /* (1+15)*4 == CL_SIZE */
175 1.1 dyoung fatp_word_t nxt : FATP_NXT_WIDTH;/* at most 2^17 fat pointers */
176 1.1 dyoung fatp_word_t tag[FATP_NTAGS]; /* 15 tags per CL */
177 1.1 dyoung };
178 1.1 dyoung
179 1.1 dyoung static inline int
180 1.1 dyoung fatp_ntags(void)
181 1.1 dyoung {
182 1.1 dyoung return FATP_NTAGS;
183 1.1 dyoung }
184 1.1 dyoung
185 1.1 dyoung static inline int
186 1.1 dyoung fatp_full(fatp_t *fp)
187 1.1 dyoung {
188 1.1 dyoung fatp_t full;
189 1.1 dyoung
190 1.6 joerg full.inuse = (1U << FATP_NTAGS) - 1U;
191 1.1 dyoung
192 1.1 dyoung return (fp->inuse == full.inuse);
193 1.1 dyoung }
194 1.1 dyoung
195 1.1 dyoung struct vtw_common;
196 1.1 dyoung struct vtw_v4;
197 1.1 dyoung struct vtw_v6;
198 1.1 dyoung struct vtw_ctl;
199 1.1 dyoung
200 1.1 dyoung /*!\brief common to all vtw
201 1.1 dyoung */
202 1.1 dyoung typedef struct vtw_common {
203 1.1 dyoung struct timeval expire; /* date of birth+msl */
204 1.1 dyoung uint32_t key; /* hash key: full hash */
205 1.1 dyoung uint32_t port_key; /* hash key: local port hash */
206 1.1 dyoung uint32_t rcv_nxt;
207 1.1 dyoung uint32_t rcv_wnd;
208 1.1 dyoung uint32_t snd_nxt;
209 1.1 dyoung uint32_t snd_scale : 8; /* window scaling for send win */
210 1.1 dyoung uint32_t msl_class : 2; /* TCP MSL class {0,1,2,3} */
211 1.1 dyoung uint32_t reuse_port : 1;
212 1.1 dyoung uint32_t reuse_addr : 1;
213 1.1 dyoung uint32_t v6only : 1;
214 1.1 dyoung uint32_t hashed : 1; /* reachable via FATP */
215 1.1 dyoung uint32_t uid;
216 1.1 dyoung } vtw_t;
217 1.1 dyoung
218 1.1 dyoung /*!\brief vestigial timewait for IPv4
219 1.1 dyoung */
220 1.1 dyoung typedef struct vtw_v4 {
221 1.1 dyoung vtw_t common; /* must be first */
222 1.1 dyoung uint16_t lport;
223 1.1 dyoung uint16_t fport;
224 1.1 dyoung uint32_t laddr;
225 1.1 dyoung uint32_t faddr;
226 1.1 dyoung } vtw_v4_t;
227 1.1 dyoung
228 1.1 dyoung /*!\brief vestigial timewait for IPv6
229 1.1 dyoung */
230 1.1 dyoung typedef struct vtw_v6 {
231 1.1 dyoung vtw_t common; /* must be first */
232 1.1 dyoung uint16_t lport;
233 1.1 dyoung uint16_t fport;
234 1.1 dyoung struct in6_addr laddr;
235 1.1 dyoung struct in6_addr faddr;
236 1.1 dyoung } vtw_v6_t;
237 1.1 dyoung
238 1.1 dyoung struct fatp_ctl;
239 1.1 dyoung typedef struct vtw_ctl vtw_ctl_t;
240 1.1 dyoung typedef struct fatp_ctl fatp_ctl_t;
241 1.1 dyoung
242 1.1 dyoung /*
243 1.3 dholland * The vestigial time waits are kept in a contiguous chunk.
244 1.1 dyoung * Allocation and free pointers run as clock hands thru this array.
245 1.1 dyoung */
246 1.1 dyoung struct vtw_ctl {
247 1.1 dyoung fatp_ctl_t *fat; /* collection of fatp to use */
248 1.1 dyoung vtw_ctl_t *ctl; /* <! controller's controller */
249 1.1 dyoung union {
250 1.1 dyoung vtw_t *v; /* common */
251 1.1 dyoung struct vtw_v4 *v4; /* IPv4 resources */
252 1.1 dyoung struct vtw_v6 *v6; /* IPv6 resources */
253 1.1 dyoung } base, /* base of vtw_t array */
254 1.1 dyoung /**/ lim, /* extent of vtw_t array */
255 1.1 dyoung /**/ alloc, /* allocation pointer */
256 1.1 dyoung /**/ oldest; /* ^ to oldest */
257 1.1 dyoung uint32_t nfree; /* # free */
258 1.1 dyoung uint32_t nalloc; /* # allocated */
259 1.1 dyoung uint32_t idx_mask; /* mask capturing all index bits*/
260 1.1 dyoung uint32_t is_v4 : 1;
261 1.1 dyoung uint32_t is_v6 : 1;
262 1.1 dyoung uint32_t idx_bits: 6;
263 1.1 dyoung uint32_t clidx : 3; /* <! class index */
264 1.1 dyoung };
265 1.1 dyoung
266 1.1 dyoung /*!\brief Collections of fat pointers.
267 1.1 dyoung */
268 1.1 dyoung struct fatp_ctl {
269 1.1 dyoung vtw_ctl_t *vtw; /* associated VTWs */
270 1.1 dyoung fatp_t *base; /* base of fatp_t array */
271 1.1 dyoung fatp_t *lim; /* extent of fatp_t array */
272 1.1 dyoung fatp_t *free; /* free list */
273 1.1 dyoung uint32_t mask; /* hash mask */
274 1.1 dyoung uint32_t nfree; /* # free */
275 1.1 dyoung uint32_t nalloc; /* # allocated */
276 1.1 dyoung fatp_t **hash; /* hash anchors */
277 1.1 dyoung fatp_t **port; /* port hash anchors */
278 1.1 dyoung };
279 1.1 dyoung
280 1.1 dyoung /*!\brief stats
281 1.1 dyoung */
282 1.1 dyoung struct vtw_stats {
283 1.1 dyoung uint64_t ins; /* <! inserts */
284 1.1 dyoung uint64_t del; /* <! deleted */
285 1.1 dyoung uint64_t kill; /* <! assassination */
286 1.1 dyoung uint64_t look[2]; /* <! lookup: full hash, port hash */
287 1.1 dyoung uint64_t hit[2]; /* <! lookups that hit */
288 1.1 dyoung uint64_t miss[2]; /* <! lookups that miss */
289 1.1 dyoung uint64_t probe[2]; /* <! hits+miss */
290 1.1 dyoung uint64_t losing[2]; /* <! misses requiring dereference */
291 1.1 dyoung uint64_t max_chain[2]; /* <! max fatp chain traversed */
292 1.1 dyoung uint64_t max_probe[2]; /* <! max probes in any one chain */
293 1.1 dyoung uint64_t max_loss[2]; /* <! max losing probes in any one
294 1.1 dyoung * chain
295 1.1 dyoung */
296 1.1 dyoung };
297 1.1 dyoung
298 1.1 dyoung typedef struct vtw_stats vtw_stats_t;
299 1.1 dyoung
300 1.1 dyoung /*!\brief follow fatp next 'pointer'
301 1.1 dyoung */
302 1.1 dyoung static inline fatp_t *
303 1.1 dyoung fatp_next(fatp_ctl_t *fat, fatp_t *fp)
304 1.1 dyoung {
305 1.1 dyoung return fp->nxt ? fat->base + fp->nxt-1 : 0;
306 1.1 dyoung }
307 1.1 dyoung
308 1.1 dyoung /*!\brief determine a collection-relative fat pointer index.
309 1.1 dyoung */
310 1.1 dyoung static inline uint32_t
311 1.1 dyoung fatp_index(fatp_ctl_t *fat, fatp_t *fp)
312 1.1 dyoung {
313 1.1 dyoung return fp ? 1 + (fp - fat->base) : 0;
314 1.1 dyoung }
315 1.1 dyoung
316 1.1 dyoung
317 1.1 dyoung static inline uint32_t
318 1.1 dyoung v4_tag(uint32_t faddr, uint32_t fport, uint32_t laddr, uint32_t lport)
319 1.1 dyoung {
320 1.1 dyoung return (ntohl(faddr) + ntohs(fport)
321 1.1 dyoung + ntohl(laddr) + ntohs(lport));
322 1.1 dyoung }
323 1.1 dyoung
324 1.1 dyoung static inline uint32_t
325 1.1 dyoung v6_tag(const struct in6_addr *faddr, uint16_t fport,
326 1.1 dyoung const struct in6_addr *laddr, uint16_t lport)
327 1.1 dyoung {
328 1.1 dyoung #ifdef IN6_HASH
329 1.1 dyoung return IN6_HASH(faddr, fport, laddr, lport);
330 1.1 dyoung #else
331 1.1 dyoung return 0;
332 1.1 dyoung #endif
333 1.1 dyoung }
334 1.1 dyoung
335 1.1 dyoung static inline uint32_t
336 1.1 dyoung v4_port_tag(uint16_t lport)
337 1.1 dyoung {
338 1.1 dyoung uint32_t tag = lport ^ (lport << 11);
339 1.1 dyoung
340 1.1 dyoung tag ^= tag << 3;
341 1.1 dyoung tag += tag >> 5;
342 1.1 dyoung tag ^= tag << 4;
343 1.1 dyoung tag += tag >> 17;
344 1.1 dyoung tag ^= tag << 25;
345 1.1 dyoung tag += tag >> 6;
346 1.1 dyoung
347 1.1 dyoung return tag;
348 1.1 dyoung }
349 1.1 dyoung
350 1.1 dyoung static inline uint32_t
351 1.1 dyoung v6_port_tag(uint16_t lport)
352 1.1 dyoung {
353 1.1 dyoung return v4_port_tag(lport);
354 1.1 dyoung }
355 1.1 dyoung
356 1.1 dyoung struct tcpcb;
357 1.1 dyoung struct tcphdr;
358 1.1 dyoung
359 1.1 dyoung int vtw_add(int, struct tcpcb *);
360 1.1 dyoung void vtw_del(vtw_ctl_t *, vtw_t *);
361 1.1 dyoung int vtw_lookup_v4(const struct ip *ip, const struct tcphdr *th,
362 1.1 dyoung uint32_t faddr, uint16_t fport,
363 1.1 dyoung uint32_t laddr, uint16_t lport);
364 1.1 dyoung struct ip6_hdr;
365 1.1 dyoung struct in6_addr;
366 1.1 dyoung
367 1.1 dyoung int vtw_lookup_v6(const struct ip6_hdr *ip, const struct tcphdr *th,
368 1.1 dyoung const struct in6_addr *faddr, uint16_t fport,
369 1.1 dyoung const struct in6_addr *laddr, uint16_t lport);
370 1.1 dyoung
371 1.1 dyoung typedef struct vestigial_inpcb {
372 1.1 dyoung union {
373 1.1 dyoung struct in_addr v4;
374 1.1 dyoung struct in6_addr v6;
375 1.1 dyoung } faddr, laddr;
376 1.1 dyoung uint16_t fport, lport;
377 1.1 dyoung uint32_t valid : 1;
378 1.1 dyoung uint32_t v4 : 1;
379 1.1 dyoung uint32_t reuse_addr : 1;
380 1.1 dyoung uint32_t reuse_port : 1;
381 1.1 dyoung uint32_t v6only : 1;
382 1.1 dyoung uint32_t more_tbd : 1;
383 1.1 dyoung uint32_t uid;
384 1.1 dyoung uint32_t rcv_nxt;
385 1.1 dyoung uint32_t rcv_wnd;
386 1.1 dyoung uint32_t snd_nxt;
387 1.1 dyoung struct vtw_common *vtw;
388 1.1 dyoung struct vtw_ctl *ctl;
389 1.1 dyoung } vestigial_inpcb_t;
390 1.1 dyoung
391 1.5 joerg #ifdef _KERNEL
392 1.1 dyoung void vtw_restart(vestigial_inpcb_t*);
393 1.1 dyoung int vtw_earlyinit(void);
394 1.4 dyoung int sysctl_tcp_vtw_enable(SYSCTLFN_PROTO);
395 1.5 joerg #endif /* _KERNEL */
396 1.1 dyoung
397 1.1 dyoung #ifdef VTW_DEBUG
398 1.1 dyoung typedef struct sin_either {
399 1.1 dyoung uint8_t sin_len;
400 1.1 dyoung uint8_t sin_family;
401 1.1 dyoung uint16_t sin_port;
402 1.1 dyoung union {
403 1.1 dyoung struct in_addr v4;
404 1.1 dyoung struct in6_addr v6;
405 1.1 dyoung } sin_addr;
406 1.1 dyoung } sin_either_t;
407 1.1 dyoung
408 1.1 dyoung int vtw_debug_add(int af, sin_either_t *, sin_either_t *, int, int);
409 1.1 dyoung
410 1.1 dyoung typedef struct vtw_sysargs {
411 1.1 dyoung uint32_t op;
412 1.1 dyoung sin_either_t fa;
413 1.1 dyoung sin_either_t la;
414 1.1 dyoung } vtw_sysargs_t;
415 1.1 dyoung
416 1.1 dyoung #endif /* VTW_DEBUG */
417 1.1 dyoung
418 1.1 dyoung #endif /* _NETINET_TCP_VTW_H */
419