2 * Copyright (c) 2002 Mark Santcroos <marks@ripe.net>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * Netgraph "device" node
28 * This node presents a /dev/ngd%d device that interfaces to an other
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
40 #include <sys/queue.h>
41 #include <sys/malloc.h>
44 #include <sys/ioccom.h>
46 #include <netgraph/ng_message.h>
47 #include <netgraph/netgraph.h>
49 #include "ng_device.h"
51 /* turn this on for verbose messages */
54 /* Netgraph methods */
55 static ng_constructor_t ng_device_cons;
56 static ng_rcvmsg_t ng_device_rcvmsg;
57 static ng_newhook_t ng_device_newhook;
58 static ng_connect_t ng_device_connect;
59 static ng_rcvdata_t ng_device_rcvdata;
60 static ng_disconnect_t ng_device_disconnect;
61 static int ng_device_mod_event(module_t mod, int event, void *data);
63 static int ng_device_init(void);
64 static int get_free_unit(void);
67 static struct ng_type typestruct = {
68 NG_ABI_VERSION, /* version */
69 NG_DEVICE_NODE_TYPE, /* name */
70 ng_device_mod_event, /* modevent */
71 ng_device_cons, /* constructor */
72 ng_device_rcvmsg, /* receive msg */
74 ng_device_newhook, /* newhook */
76 ng_device_connect, /* connect */
77 ng_device_rcvdata, /* receive data */
78 ng_device_disconnect, /* disconnect */
81 NETGRAPH_INIT(device, &typestruct);
84 struct ngd_connection {
85 SLIST_ENTRY(ngd_connection) links;
88 struct ng_hook *active_hook;
96 SLIST_HEAD(, ngd_connection) head;
99 char nodename[NG_NODESIZ];
102 /* the per connection receiving queue maximum */
103 #define NGD_QUEUE_SIZE (1024*10)
105 /* Maximum number of NGD devices */
106 #define MAX_NGD 25 /* should be more than enough for now */
108 static d_close_t ngdclose;
109 static d_open_t ngdopen;
110 static d_read_t ngdread;
111 static d_write_t ngdwrite;
112 static d_ioctl_t ngdioctl;
113 static d_poll_t ngdpoll;
115 static struct cdevsw ngd_cdevsw = {
116 .d_version = D_VERSION,
117 .d_flags = D_NEEDGIANT,
128 * this holds all the stuff that should be done at load time
131 ng_device_mod_event(module_t mod, int event, void *data)
136 printf("%s()\n",__func__);
137 #endif /* NGD_DEBUG */
146 /* XXX do we need to do something specific ? */
147 /* ng_device_breakdown */
162 struct ngd_softc *sc = &ngd_softc;
165 printf("%s()\n",__func__);
166 #endif /* NGD_DEBUG */
168 SLIST_INIT(&sc->head);
170 if (ng_make_node_common(&typestruct, &sc->node) != 0) {
171 printf("%s(): ng_make_node_common failed\n",__func__);
174 sprintf(sc->nodename, "%s", NG_DEVICE_NODE_TYPE);
175 if (ng_name_node(sc->node, sc->nodename)) {
176 NG_NODE_UNREF(sc->node); /* make it go away again */
177 printf("%s(): ng_name_node failed\n",__func__);
180 NG_NODE_SET_PRIVATE(sc->node, sc);
186 * don't allow to be created, only the device can do that
189 ng_device_cons(node_p node)
193 printf("%s()\n",__func__);
194 #endif /* NGD_DEBUG */
200 * Receive control message. We just bounce it back as a reply.
203 ng_device_rcvmsg(node_p node, item_p item, hook_p lasthook)
205 struct ngd_softc *sc = &ngd_softc;
208 struct ngd_connection * connection = NULL;
209 struct ngd_connection *tmp = NULL;
212 printf("%s()\n",__func__);
213 #endif /* NGD_DEBUG */
215 NGI_GET_MSG(item, msg);
217 SLIST_FOREACH(tmp,&sc->head,links) {
218 if(tmp->active_hook == lasthook) {
222 if(connection == NULL) {
223 printf("%s(): connection is still NULL, no hook found\n",__func__);
233 struct ngd_connection *tmp = NULL;
234 struct ngd_softc *sc = &ngd_softc;
239 printf("%s()\n",__func__);
240 #endif /* NGD_DEBUG */
242 /* When there is no list yet, the first device unit is always 0. */
243 if SLIST_EMPTY(&sc->head) {
248 /* Just do a brute force loop to find the first free unit that is
249 * smaller than MAX_NGD.
250 * Set MAX_NGD to a large value, doesn't impact performance.
252 for(n = 0;n<MAX_NGD && unit == -1;n++) {
253 SLIST_FOREACH(tmp,&sc->head,links) {
270 ng_device_newhook(node_p node, hook_p hook, const char *name)
272 struct ngd_softc *sc = &ngd_softc;
273 struct ngd_connection * new_connection = NULL;
276 printf("%s()\n",__func__);
277 #endif /* NGD_DEBUG */
279 new_connection = malloc(sizeof(struct ngd_connection), M_DEVBUF, M_NOWAIT);
280 if(new_connection == NULL) {
281 printf("%s(): ERROR: new_connection == NULL\n",__func__);
285 new_connection->unit = get_free_unit();
286 if(new_connection->unit<0) {
287 printf("%s: No free unit found by get_free_unit(), "
288 "increas MAX_NGD\n",__func__);
291 new_connection->ngddev = make_dev(&ngd_cdevsw, new_connection->unit, 0, 0,0600,"ngd%d",new_connection->unit);
292 if(new_connection->ngddev == NULL) {
293 printf("%s(): make_dev failed\n",__func__);
297 new_connection->readq = malloc(sizeof(char)*NGD_QUEUE_SIZE, M_DEVBUF, M_NOWAIT | M_ZERO);
298 if(new_connection->readq == NULL) {
299 printf("%s(): readq malloc failed\n",__func__);
303 /* point to begin of buffer */
304 new_connection->loc = 0;
305 new_connection->active_hook = hook;
307 SLIST_INSERT_HEAD(&sc->head, new_connection, links);
313 * we gave ok to a new hook
317 ng_device_connect(hook_p hook)
321 printf("%s()\n",__func__);
322 #endif /* NGD_DEBUG */
329 * Receive data from hook
332 ng_device_rcvdata(hook_p hook, item_p item)
335 struct ngd_softc *sc = &ngd_softc;
336 struct ngd_connection * connection = NULL;
337 struct ngd_connection * tmp;
341 printf("%s()\n",__func__);
342 #endif /* NGD_DEBUG */
344 SLIST_FOREACH(tmp,&sc->head,links) {
345 if(tmp->active_hook == hook) {
349 if(connection == NULL) {
350 printf("%s(): connection is still NULL, no hook found\n",__func__);
357 m = m_pullup(m,m->m_len);
359 printf("%s(): ERROR: m_pullup failed\n",__func__);
363 buffer = malloc(sizeof(char)*m->m_len, M_DEVBUF, M_NOWAIT | M_ZERO);
365 printf("%s(): ERROR: buffer malloc failed\n",__func__);
369 buffer = mtod(m,char *);
371 if( (connection->loc+m->m_len) < NGD_QUEUE_SIZE) {
372 memcpy(connection->readq+connection->loc, buffer, m->m_len);
373 connection->loc += m->m_len;
375 printf("%s(): queue full, first read out a bit\n",__func__);
377 free(buffer,M_DEVBUF);
383 * Removal of the last link destroys the node
386 ng_device_disconnect(hook_p hook)
388 struct ngd_softc *sc = &ngd_softc;
389 struct ngd_connection * connection = NULL;
390 struct ngd_connection * tmp;
393 printf("%s()\n",__func__);
394 #endif /* NGD_DEBUG */
396 SLIST_FOREACH(tmp,&sc->head,links) {
397 if(tmp->active_hook == hook) {
401 if(connection == NULL) {
402 printf("%s(): connection is still NULL, no hook found\n",__func__);
406 free(connection->readq,M_DEVBUF);
408 destroy_dev(connection->ngddev);
410 SLIST_REMOVE(&sc->head,connection,ngd_connection,links);
415 * the device is opened
418 ngdopen(dev_t dev, int flag, int mode, struct thread *td)
422 printf("%s()\n",__func__);
423 #endif /* NGD_DEBUG */
429 * the device is closed
432 ngdclose(dev_t dev, int flag, int mode, struct thread *td)
436 printf("%s()\n",__func__);
446 * they are translated into netgraph messages and passed on
450 ngdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
452 struct ngd_softc *sc = &ngd_softc;
453 struct ngd_connection * connection = NULL;
454 struct ngd_connection * tmp;
457 struct ngd_param_s * datap;
460 printf("%s()\n",__func__);
461 #endif /* NGD_DEBUG */
463 SLIST_FOREACH(tmp,&sc->head,links) {
464 if(tmp->ngddev == dev) {
468 if(connection == NULL) {
469 printf("%s(): connection is still NULL, no dev found\n",__func__);
473 /* NG_MKMESSAGE(msg, cookie, cmdid, len, how) */
474 NG_MKMESSAGE(msg, NGM_DEVICE_COOKIE, cmd, sizeof(struct ngd_param_s),
477 printf("%s(): msg == NULL\n",__func__);
481 /* pass the ioctl data into the ->data area */
482 datap = (struct ngd_param_s *)msg->data;
485 /* NG_SEND_MSG_HOOK(error, here, msg, hook, retaddr) */
486 NG_SEND_MSG_HOOK(error, sc->node, msg, connection->active_hook, NULL);
488 printf("%s(): NG_SEND_MSG_HOOK error: %d\n",__func__,error);
497 * This function is called when a read(2) is done to our device.
498 * We pass the data available in kernelspace on into userland using
502 ngdread(dev_t dev, struct uio *uio, int flag)
505 char buffer[uio->uio_resid+1];
506 struct ngd_softc *sc = &ngd_softc;
507 struct ngd_connection * connection = NULL;
508 struct ngd_connection * tmp;
511 printf("%s()\n",__func__);
512 #endif /* NGD_DEBUG */
514 SLIST_FOREACH(tmp,&sc->head,links) {
515 if(tmp->ngddev == dev) {
519 if(connection == NULL) {
520 printf("%s(): connection is still NULL, no dev found\n",__func__);
524 while ( ( uio->uio_resid > 0 ) && ( connection->loc > 0 ) ) {
525 amnt = MIN(uio->uio_resid,connection->loc);
527 memcpy(buffer,connection->readq, amnt);
528 memcpy(connection->readq, connection->readq+amnt,
529 connection->loc-amnt);
530 connection->loc -= amnt;
532 ret = uiomove((caddr_t)buffer, amnt, uio);
540 printf("%s(): uiomove returns error %d\n",__func__,ret);
541 /* do error cleanup here */
547 * This function is called when our device is written to.
548 * We read the data from userland into our local buffer and pass it on
549 * into the remote hook.
553 ngdwrite(dev_t dev, struct uio *uio, int flag)
558 char buffer[uio->uio_resid];
559 int len = uio->uio_resid;
560 struct ngd_softc *sc =& ngd_softc;
561 struct ngd_connection * connection = NULL;
562 struct ngd_connection * tmp;
565 printf("%s()\n",__func__);
566 #endif /* NGD_DEBUG */
568 SLIST_FOREACH(tmp,&sc->head,links) {
569 if(tmp->ngddev == dev) {
574 if(connection == NULL) {
575 printf("%s(): connection is still NULL, no dev found\n",__func__);
580 if ((ret = uiomove((caddr_t)buffer, len, uio)) != 0)
583 printf("%s(): len <= 0 : is this supposed to happen?!\n",__func__);
585 m = m_devget(buffer,len,0,NULL,NULL);
587 NG_SEND_DATA_ONLY(error,connection->active_hook,m);
592 /* do error cleanup here */
593 printf("%s(): uiomove returned err: %d\n",__func__,ret);
599 * we are being polled/selected
600 * check if there is data available for read
603 ngdpoll(dev_t dev, int events, struct thread *td)
606 struct ngd_softc *sc = &ngd_softc;
607 struct ngd_connection * connection = NULL;
608 struct ngd_connection * tmp;
611 if (events & (POLLIN | POLLRDNORM)) {
612 /* get the connection we have to know the loc from */
613 SLIST_FOREACH(tmp,&sc->head,links) {
614 if(tmp->ngddev == dev) {
618 if(connection == NULL) {
619 printf("%s(): ERROR: connection is still NULL,"
620 "no dev found\n",__func__);
624 if (connection->loc > 0)
625 revents |= events & (POLLIN | POLLRDNORM);