hyperv/vmbus: Fix interrupt timer detection logic.
[dragonfly.git] / sys / netgraph / vjc / ng_vjc.c
blob59ea1a51bdbfbcc614668044d22496beee57a1d9
2 /*
3 * ng_vjc.c
5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
6 * All rights reserved.
7 *
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 * copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 * Communications, Inc. trademarks, including the mark "WHISTLE
16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 * such appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35 * OF SUCH DAMAGE.
37 * Author: Archie Cobbs <archie@freebsd.org>
39 * $FreeBSD: src/sys/netgraph/ng_vjc.c,v 1.9.2.5 2002/07/02 23:44:03 archie Exp $
40 * $Whistle: ng_vjc.c,v 1.17 1999/11/01 09:24:52 julian Exp $
44 * This node performs Van Jacobson IP header (de)compression.
45 * You must have included net/slcompress.c in your kernel compilation.
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/errno.h>
51 #include <sys/kernel.h>
52 #include <sys/mbuf.h>
53 #include <sys/malloc.h>
55 #include <netgraph/ng_message.h>
56 #include <netgraph/netgraph.h>
57 #include <netgraph/ng_parse.h>
58 #include "ng_vjc.h"
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet/tcp.h>
65 #include <net/slcompress.h>
67 /* Check agreement with slcompress.c */
68 #if MAX_STATES != NG_VJC_MAX_CHANNELS
69 #error NG_VJC_MAX_CHANNELS must be the same as MAX_STATES
70 #endif
72 /* Maximum length of a compressed TCP VJ header */
73 #define MAX_VJHEADER 19
75 /* Node private data */
76 struct ng_vjc_private {
77 struct ngm_vjc_config conf;
78 struct slcompress slc;
79 hook_p ip;
80 hook_p vjcomp;
81 hook_p vjuncomp;
82 hook_p vjip;
84 typedef struct ng_vjc_private *priv_p;
86 #define ERROUT(x) do { error = (x); goto done; } while (0)
88 /* Netgraph node methods */
89 static ng_constructor_t ng_vjc_constructor;
90 static ng_rcvmsg_t ng_vjc_rcvmsg;
91 static ng_shutdown_t ng_vjc_rmnode;
92 static ng_newhook_t ng_vjc_newhook;
93 static ng_rcvdata_t ng_vjc_rcvdata;
94 static ng_disconnect_t ng_vjc_disconnect;
96 /* Helper stuff */
97 static struct mbuf *ng_vjc_pulluphdrs(struct mbuf *m, int knownTCP);
99 /* Parse type for struct ngm_vjc_config */
100 static const struct ng_parse_struct_field ng_vjc_config_type_fields[]
101 = NG_VJC_CONFIG_TYPE_INFO;
102 static const struct ng_parse_type ng_vjc_config_type = {
103 &ng_parse_struct_type,
104 &ng_vjc_config_type_fields
107 /* Parse type for the 'last_cs' and 'cs_next' fields in struct slcompress,
108 which are pointers converted to integer indices, so parse them that way. */
109 #ifndef __LP64__
110 #define NG_VJC_TSTATE_PTR_TYPE &ng_parse_uint32_type
111 #else
112 #define NG_VJC_TSTATE_PTR_TYPE &ng_parse_uint64_type
113 #endif
115 /* Parse type for the 'cs_hdr' field in a struct cstate. Ideally we would
116 like to use a 'struct ip' type instead of a simple array of bytes. */
117 static const struct ng_parse_fixedarray_info ng_vjc_cs_hdr_type_info = {
118 &ng_parse_hint8_type,
119 MAX_HDR
121 static const struct ng_parse_type ng_vjc_cs_hdr_type = {
122 &ng_parse_fixedarray_type,
123 &ng_vjc_cs_hdr_type_info
126 /* Parse type for a struct cstate */
127 static const struct ng_parse_struct_field ng_vjc_cstate_type_fields[] = {
128 { "cs_next", NG_VJC_TSTATE_PTR_TYPE },
129 { "cs_hlen", &ng_parse_uint16_type },
130 { "cs_id", &ng_parse_uint8_type },
131 { "cs_filler", &ng_parse_uint8_type },
132 { "cs_hdr", &ng_vjc_cs_hdr_type },
133 { NULL }
135 static const struct ng_parse_type ng_vjc_cstate_type = {
136 &ng_parse_struct_type,
137 &ng_vjc_cstate_type_fields
140 /* Parse type for an array of MAX_STATES struct cstate's, ie, tstate & rstate */
141 static const struct ng_parse_fixedarray_info ng_vjc_cstatearray_type_info = {
142 &ng_vjc_cstate_type,
143 MAX_STATES
145 static const struct ng_parse_type ng_vjc_cstatearray_type = {
146 &ng_parse_fixedarray_type,
147 &ng_vjc_cstatearray_type_info
150 /* Parse type for struct slcompress. Keep this in sync with the
151 definition of struct slcompress defined in <net/slcompress.h> */
152 static const struct ng_parse_struct_field ng_vjc_slcompress_type_fields[] = {
153 { "last_cs", NG_VJC_TSTATE_PTR_TYPE },
154 { "last_recv", &ng_parse_uint8_type },
155 { "last_xmit", &ng_parse_uint8_type },
156 { "flags", &ng_parse_hint16_type },
157 #ifndef SL_NO_STATS
158 { "sls_packets", &ng_parse_uint32_type },
159 { "sls_compressed", &ng_parse_uint32_type },
160 { "sls_searches", &ng_parse_uint32_type },
161 { "sls_misses", &ng_parse_uint32_type },
162 { "sls_uncompressedin", &ng_parse_uint32_type },
163 { "sls_compressedin", &ng_parse_uint32_type },
164 { "sls_errorin", &ng_parse_uint32_type },
165 { "sls_tossed", &ng_parse_uint32_type },
166 #endif
167 { "tstate", &ng_vjc_cstatearray_type },
168 { "rstate", &ng_vjc_cstatearray_type },
169 { NULL }
171 static const struct ng_parse_type ng_vjc_slcompress_type = {
172 &ng_parse_struct_type,
173 &ng_vjc_slcompress_type_fields
176 /* List of commands and how to convert arguments to/from ASCII */
177 static const struct ng_cmdlist ng_vjc_cmds[] = {
179 NGM_VJC_COOKIE,
180 NGM_VJC_SET_CONFIG,
181 "setconfig",
182 &ng_vjc_config_type,
183 NULL
186 NGM_VJC_COOKIE,
187 NGM_VJC_GET_CONFIG,
188 "getconfig",
189 NULL,
190 &ng_vjc_config_type,
193 NGM_VJC_COOKIE,
194 NGM_VJC_GET_STATE,
195 "getstate",
196 NULL,
197 &ng_vjc_slcompress_type,
200 NGM_VJC_COOKIE,
201 NGM_VJC_CLR_STATS,
202 "clrstats",
203 NULL,
204 NULL,
207 NGM_VJC_COOKIE,
208 NGM_VJC_RECV_ERROR,
209 "recverror",
210 NULL,
211 NULL,
213 { 0 }
216 /* Node type descriptor */
217 static struct ng_type ng_vjc_typestruct = {
218 NG_VERSION,
219 NG_VJC_NODE_TYPE,
220 NULL,
221 ng_vjc_constructor,
222 ng_vjc_rcvmsg,
223 ng_vjc_rmnode,
224 ng_vjc_newhook,
225 NULL,
226 NULL,
227 ng_vjc_rcvdata,
228 ng_vjc_rcvdata,
229 ng_vjc_disconnect,
230 ng_vjc_cmds
232 NETGRAPH_INIT(vjc, &ng_vjc_typestruct);
234 /************************************************************************
235 NETGRAPH NODE METHODS
236 ************************************************************************/
239 * Create a new node
241 static int
242 ng_vjc_constructor(node_p *nodep)
244 priv_p priv;
245 int error;
247 /* Allocate private structure */
248 priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
249 if (priv == NULL)
250 return (ENOMEM);
252 /* Call generic node constructor */
253 if ((error = ng_make_node_common(&ng_vjc_typestruct, nodep))) {
254 kfree(priv, M_NETGRAPH);
255 return (error);
257 (*nodep)->private = priv;
259 /* Done */
260 return (0);
264 * Add a new hook
266 static int
267 ng_vjc_newhook(node_p node, hook_p hook, const char *name)
269 const priv_p priv = (priv_p) node->private;
270 hook_p *hookp;
272 /* Get hook */
273 if (strcmp(name, NG_VJC_HOOK_IP) == 0)
274 hookp = &priv->ip;
275 else if (strcmp(name, NG_VJC_HOOK_VJCOMP) == 0)
276 hookp = &priv->vjcomp;
277 else if (strcmp(name, NG_VJC_HOOK_VJUNCOMP) == 0)
278 hookp = &priv->vjuncomp;
279 else if (strcmp(name, NG_VJC_HOOK_VJIP) == 0)
280 hookp = &priv->vjip;
281 else
282 return (EINVAL);
284 /* See if already connected */
285 if (*hookp)
286 return (EISCONN);
288 /* OK */
289 *hookp = hook;
290 return (0);
294 * Receive a control message
296 static int
297 ng_vjc_rcvmsg(node_p node, struct ng_mesg *msg,
298 const char *raddr, struct ng_mesg **rptr)
300 const priv_p priv = (priv_p) node->private;
301 struct ng_mesg *resp = NULL;
302 int error = 0;
304 /* Check type cookie */
305 switch (msg->header.typecookie) {
306 case NGM_VJC_COOKIE:
307 switch (msg->header.cmd) {
308 case NGM_VJC_SET_CONFIG:
310 struct ngm_vjc_config *const c =
311 (struct ngm_vjc_config *) msg->data;
313 if (msg->header.arglen != sizeof(*c))
314 ERROUT(EINVAL);
315 if ((priv->conf.enableComp || priv->conf.enableDecomp)
316 && (c->enableComp || c->enableDecomp))
317 ERROUT(EALREADY);
318 if (c->enableComp) {
319 if (c->maxChannel > NG_VJC_MAX_CHANNELS - 1
320 || c->maxChannel < NG_VJC_MIN_CHANNELS - 1)
321 ERROUT(EINVAL);
322 } else
323 c->maxChannel = NG_VJC_MAX_CHANNELS - 1;
324 if (c->enableComp != 0 || c->enableDecomp != 0) {
325 bzero(&priv->slc, sizeof(priv->slc));
326 sl_compress_init(&priv->slc, c->maxChannel);
328 priv->conf = *c;
329 break;
331 case NGM_VJC_GET_CONFIG:
333 struct ngm_vjc_config *conf;
335 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
336 if (resp == NULL)
337 ERROUT(ENOMEM);
338 conf = (struct ngm_vjc_config *)resp->data;
339 *conf = priv->conf;
340 break;
342 case NGM_VJC_GET_STATE:
344 const struct slcompress *const sl0 = &priv->slc;
345 struct slcompress *sl;
346 u_int16_t index;
347 int i;
349 /* Get response structure */
350 NG_MKRESPONSE(resp, msg, sizeof(*sl), M_NOWAIT);
351 if (resp == NULL)
352 ERROUT(ENOMEM);
353 sl = (struct slcompress *)resp->data;
354 *sl = *sl0;
356 /* Replace pointers with integer indicies */
357 if (sl->last_cs != NULL) {
358 index = sl0->last_cs - sl0->tstate;
359 bzero(&sl->last_cs, sizeof(sl->last_cs));
360 *((u_int16_t *)&sl->last_cs) = index;
362 for (i = 0; i < MAX_STATES; i++) {
363 struct cstate *const cs = &sl->tstate[i];
365 index = sl0->tstate[i].cs_next - sl0->tstate;
366 bzero(&cs->cs_next, sizeof(cs->cs_next));
367 *((u_int16_t *)&cs->cs_next) = index;
369 break;
371 case NGM_VJC_CLR_STATS:
372 priv->slc.sls_packets = 0;
373 priv->slc.sls_compressed = 0;
374 priv->slc.sls_searches = 0;
375 priv->slc.sls_misses = 0;
376 priv->slc.sls_uncompressedin = 0;
377 priv->slc.sls_compressedin = 0;
378 priv->slc.sls_errorin = 0;
379 priv->slc.sls_tossed = 0;
380 break;
381 case NGM_VJC_RECV_ERROR:
382 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, &priv->slc);
383 break;
384 default:
385 error = EINVAL;
386 break;
388 break;
389 default:
390 error = EINVAL;
391 break;
393 if (rptr)
394 *rptr = resp;
395 else if (resp)
396 kfree(resp, M_NETGRAPH);
398 done:
399 kfree(msg, M_NETGRAPH);
400 return (error);
404 * Receive data
406 static int
407 ng_vjc_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
409 const node_p node = hook->node;
410 const priv_p priv = (priv_p) node->private;
411 int error = 0;
413 if (hook == priv->ip) { /* outgoing packet */
414 u_int type = TYPE_IP;
416 /* Compress packet if enabled and proto is TCP */
417 if (priv->conf.enableComp) {
418 struct ip *ip;
420 if ((m = ng_vjc_pulluphdrs(m, 0)) == NULL) {
421 NG_FREE_META(meta);
422 return (ENOBUFS);
424 ip = mtod(m, struct ip *);
425 if (ip->ip_p == IPPROTO_TCP) {
426 const int origLen = m->m_len;
428 type = sl_compress_tcp(m, ip,
429 &priv->slc, priv->conf.compressCID);
430 m->m_pkthdr.len += m->m_len - origLen;
434 /* Dispatch to the appropriate outgoing hook */
435 switch (type) {
436 case TYPE_IP:
437 hook = priv->vjip;
438 break;
439 case TYPE_UNCOMPRESSED_TCP:
440 hook = priv->vjuncomp;
441 break;
442 case TYPE_COMPRESSED_TCP:
443 hook = priv->vjcomp;
444 break;
445 default:
446 panic("%s: type=%d", __func__, type);
448 } else if (hook == priv->vjcomp) { /* incoming compressed packet */
449 int vjlen, need2pullup;
450 struct mbuf *hm;
451 u_char *hdr;
452 u_int hlen;
454 /* Are we decompressing? */
455 if (!priv->conf.enableDecomp) {
456 NG_FREE_DATA(m, meta);
457 return (ENXIO);
460 /* Pull up the necessary amount from the mbuf */
461 need2pullup = MAX_VJHEADER;
462 if (need2pullup > m->m_pkthdr.len)
463 need2pullup = m->m_pkthdr.len;
464 if (m->m_len < need2pullup
465 && (m = m_pullup(m, need2pullup)) == NULL) {
466 priv->slc.sls_errorin++;
467 NG_FREE_META(meta);
468 return (ENOBUFS);
471 /* Uncompress packet to reconstruct TCP/IP header */
472 vjlen = sl_uncompress_tcp_core(mtod(m, u_char *),
473 m->m_len, m->m_pkthdr.len, TYPE_COMPRESSED_TCP,
474 &priv->slc, &hdr, &hlen);
475 if (vjlen <= 0) {
476 NG_FREE_DATA(m, meta);
477 return (EINVAL);
479 m_adj(m, vjlen);
481 /* Copy the reconstructed TCP/IP headers into a new mbuf */
482 MGETHDR(hm, M_NOWAIT, MT_DATA);
483 if (hm == NULL) {
484 priv->slc.sls_errorin++;
485 NG_FREE_DATA(m, meta);
486 return (ENOBUFS);
488 hm->m_len = 0;
489 hm->m_pkthdr.rcvif = NULL;
490 if (hlen > MHLEN) { /* unlikely, but can happen */
491 MCLGET(hm, M_NOWAIT);
492 if ((hm->m_flags & M_EXT) == 0) {
493 m_freem(hm);
494 priv->slc.sls_errorin++;
495 NG_FREE_DATA(m, meta);
496 return (ENOBUFS);
499 bcopy(hdr, mtod(hm, u_char *), hlen);
500 hm->m_len = hlen;
502 /* Glue TCP/IP headers and rest of packet together */
503 hm->m_next = m;
504 hm->m_pkthdr.len = hlen + m->m_pkthdr.len;
505 m = hm;
506 hook = priv->ip;
507 } else if (hook == priv->vjuncomp) { /* incoming uncompressed pkt */
508 u_char *hdr;
509 u_int hlen;
511 /* Are we decompressing? */
512 if (!priv->conf.enableDecomp) {
513 NG_FREE_DATA(m, meta);
514 return (ENXIO);
517 /* Pull up IP+TCP headers */
518 if ((m = ng_vjc_pulluphdrs(m, 1)) == NULL) {
519 NG_FREE_META(meta);
520 return (ENOBUFS);
523 /* Run packet through uncompressor */
524 if (sl_uncompress_tcp_core(mtod(m, u_char *),
525 m->m_len, m->m_pkthdr.len, TYPE_UNCOMPRESSED_TCP,
526 &priv->slc, &hdr, &hlen) < 0) {
527 NG_FREE_DATA(m, meta);
528 return (EINVAL);
530 hook = priv->ip;
531 } else if (hook == priv->vjip) /* incoming regular packet (bypass) */
532 hook = priv->ip;
533 else
534 panic("%s: unknown hook", __func__);
536 /* Send result back out */
537 NG_SEND_DATA(error, hook, m, meta);
538 return (error);
542 * Shutdown node
544 static int
545 ng_vjc_rmnode(node_p node)
547 const priv_p priv = (priv_p) node->private;
549 node->flags |= NG_INVALID;
550 ng_cutlinks(node);
551 ng_unname(node);
552 bzero(priv, sizeof(*priv));
553 kfree(priv, M_NETGRAPH);
554 node->private = NULL;
555 ng_unref(node);
556 return (0);
560 * Hook disconnection
562 static int
563 ng_vjc_disconnect(hook_p hook)
565 const node_p node = hook->node;
566 const priv_p priv = node->private;
568 /* Zero out hook pointer */
569 if (hook == priv->ip)
570 priv->ip = NULL;
571 else if (hook == priv->vjcomp)
572 priv->vjcomp = NULL;
573 else if (hook == priv->vjuncomp)
574 priv->vjuncomp = NULL;
575 else if (hook == priv->vjip)
576 priv->vjip = NULL;
577 else
578 panic("%s: unknown hook", __func__);
580 /* Go away if no hooks left */
581 if (node->numhooks == 0)
582 ng_rmnode(node);
583 return (0);
586 /************************************************************************
587 HELPER STUFF
588 ************************************************************************/
591 * Pull up the full IP and TCP headers of a packet. If packet is not
592 * a TCP packet, just pull up the IP header.
594 static struct mbuf *
595 ng_vjc_pulluphdrs(struct mbuf *m, int knownTCP)
597 struct ip *ip;
598 struct tcphdr *tcp;
599 int ihlen, thlen;
601 if (m->m_len < sizeof(*ip) && (m = m_pullup(m, sizeof(*ip))) == NULL)
602 return (NULL);
603 ip = mtod(m, struct ip *);
604 if (!knownTCP && ip->ip_p != IPPROTO_TCP)
605 return (m);
606 ihlen = ip->ip_hl << 2;
607 if (m->m_len < ihlen + sizeof(*tcp)) {
608 if ((m = m_pullup(m, ihlen + sizeof(*tcp))) == NULL)
609 return (NULL);
610 ip = mtod(m, struct ip *);
612 tcp = (struct tcphdr *)((u_char *)ip + ihlen);
613 thlen = tcp->th_off << 2;
614 if (m->m_len < ihlen + thlen)
615 m = m_pullup(m, ihlen + thlen);
616 return (m);