ng_device.c (1ede983cc905643549d8cae56a9d0e28fc68375f) ng_device.c (a8353960351ce8bf978c3dc031b56ba85df5a109)
1/*-
1/*
2 * Copyright (c) 2002 Mark Santcroos <marks@ripe.net>
2 * Copyright (c) 2002 Mark Santcroos <marks@ripe.net>
3 * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * Netgraph "device" node
26 *
14 *
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.
25 *
26 * Netgraph "device" node
27 *
27 * This node presents a /dev/ngd%d device that interfaces to an other
28 * This node presents a /dev/ngd%d device that interfaces to an other
28 * netgraph node.
29 *
30 * $FreeBSD$
31 *
32 */
33
29 * netgraph node.
30 *
31 * $FreeBSD$
32 *
33 */
34
34#if 0
35#define DBG do { printf("ng_device: %s\n", __func__ ); } while (0)
36#else
37#define DBG do {} while (0)
38#endif
39
40#include <sys/param.h>
35#include <sys/param.h>
41#include <sys/conf.h>
42#include <sys/ioccom.h>
36#include <sys/systm.h>
43#include <sys/kernel.h>
37#include <sys/kernel.h>
44#include <sys/malloc.h>
45#include <sys/mbuf.h>
38#include <sys/mbuf.h>
46#include <sys/poll.h>
47#include <sys/queue.h>
48#include <sys/socket.h>
49#include <sys/systm.h>
50#include <sys/uio.h>
39#include <sys/uio.h>
51#include <sys/vnode.h>
40#include <sys/queue.h>
41#include <sys/malloc.h>
42#include <sys/conf.h>
43#include <sys/poll.h>
44#include <sys/ioccom.h>
52
45
53#include <net/if.h>
54#include <net/if_var.h>
55#include <netinet/in.h>
56#include <netinet/in_systm.h>
57#include <netinet/ip.h>
58
59#include <netgraph/ng_message.h>
60#include <netgraph/netgraph.h>
46#include <netgraph/ng_message.h>
47#include <netgraph/netgraph.h>
61#include <netgraph/ng_device.h>
62
48
63#define ERROUT(x) do { error = (x); goto done; } while (0)
49#include "ng_device.h"
64
50
51/* turn this on for verbose messages */
52#define NGD_DEBUG
53
65/* Netgraph methods */
54/* Netgraph methods */
66static int ng_device_mod_event(module_t, int, void *);
67static ng_constructor_t ng_device_constructor;
55static ng_constructor_t ng_device_cons;
68static ng_rcvmsg_t ng_device_rcvmsg;
56static ng_rcvmsg_t ng_device_rcvmsg;
69static ng_shutdown_t ng_device_shutdown;
70static ng_newhook_t ng_device_newhook;
57static ng_newhook_t ng_device_newhook;
58static ng_connect_t ng_device_connect;
71static ng_rcvdata_t ng_device_rcvdata;
72static ng_disconnect_t ng_device_disconnect;
59static ng_rcvdata_t ng_device_rcvdata;
60static ng_disconnect_t ng_device_disconnect;
61static int ng_device_mod_event(module_t mod, int event, void *data);
73
62
63static int ng_device_init(void);
64static int get_free_unit(void);
65
74/* Netgraph type */
66/* Netgraph type */
75static struct ng_type ngd_typestruct = {
76 .version = NG_ABI_VERSION,
77 .name = NG_DEVICE_NODE_TYPE,
78 .mod_event = ng_device_mod_event,
79 .constructor = ng_device_constructor,
80 .rcvmsg = ng_device_rcvmsg,
81 .shutdown = ng_device_shutdown,
82 .newhook = ng_device_newhook,
83 .rcvdata = ng_device_rcvdata,
84 .disconnect = ng_device_disconnect,
67static 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 */
73 NULL, /* shutdown */
74 ng_device_newhook, /* newhook */
75 NULL, /* findhook */
76 ng_device_connect, /* connect */
77 ng_device_rcvdata, /* receive data */
78 ng_device_disconnect, /* disconnect */
79 NULL
85};
80};
86NETGRAPH_INIT(device, &ngd_typestruct);
81NETGRAPH_INIT(device, &typestruct);
87
82
88/* per node data */
89struct ngd_private {
90 struct ifqueue readq;
91 struct ng_node *node;
92 struct ng_hook *hook;
93 struct cdev *ngddev;
94 struct mtx ngd_mtx;
95 int unit;
96 uint16_t flags;
97#define NGDF_OPEN 0x0001
98#define NGDF_RWAIT 0x0002
83/* per hook data */
84struct ngd_connection {
85 SLIST_ENTRY(ngd_connection) links;
86
87 dev_t ngddev;
88 struct ng_hook *active_hook;
89 char *readq;
90 int loc;
91 int unit;
99};
92};
100typedef struct ngd_private *priv_p;
101
93
102/* unit number allocator entity */
103static struct unrhdr *ngd_unit;
94/* global data */
95struct ngd_softc {
96 SLIST_HEAD(, ngd_connection) head;
104
97
98 node_p node;
99 char nodename[NG_NODELEN + 1];
100} ngd_softc;
101
102/* helper definition */
103#define MIN(a, b) ((a) < (b) ? (a) : (b))
104
105/* the per connection receiving queue maximum */
106#define NGD_QUEUE_SIZE (1024*10)
107
105/* Maximum number of NGD devices */
108/* Maximum number of NGD devices */
106#define MAX_NGD 999
109#define MAX_NGD 25 /* should be more than enough for now */
107
108static d_close_t ngdclose;
109static d_open_t ngdopen;
110static d_read_t ngdread;
111static d_write_t ngdwrite;
110
111static d_close_t ngdclose;
112static d_open_t ngdopen;
113static d_read_t ngdread;
114static d_write_t ngdwrite;
112#if 0
113static d_ioctl_t ngdioctl;
115static d_ioctl_t ngdioctl;
114#endif
115static d_poll_t ngdpoll;
116
116static d_poll_t ngdpoll;
117
118#define NGD_CDEV_MAJOR 20
117static struct cdevsw ngd_cdevsw = {
119static struct cdevsw ngd_cdevsw = {
118 .d_version = D_VERSION,
119 .d_open = ngdopen,
120 .d_close = ngdclose,
121 .d_read = ngdread,
122 .d_write = ngdwrite,
123#if 0
124 .d_ioctl = ngdioctl,
125#endif
126 .d_poll = ngdpoll,
127 .d_name = NG_DEVICE_DEVNAME,
120 /* open */ ngdopen,
121 /* close */ ngdclose,
122 /* read */ ngdread,
123 /* write */ ngdwrite,
124 /* ioctl */ ngdioctl,
125 /* poll */ ngdpoll,
126 /* mmap */ nommap,
127 /* strategy */ nostrategy,
128 /* name */ "ngd",
129 /* maj */ NGD_CDEV_MAJOR,
130 /* dump */ nodump,
131 /* psize */ nopsize,
132 /* flags */ 0,
128};
129
133};
134
130/******************************************************************************
131 * Netgraph methods
132 ******************************************************************************/
133
134/*
135 * Handle loading and unloading for this node type.
135/*
136 * this holds all the stuff that should be done at load time
136 */
137static int
138ng_device_mod_event(module_t mod, int event, void *data)
139{
140 int error = 0;
141
137 */
138static int
139ng_device_mod_event(module_t mod, int event, void *data)
140{
141 int error = 0;
142
143#ifdef NGD_DEBUG
144 printf("%s()\n",__func__);
145#endif /* NGD_DEBUG */
146
142 switch (event) {
147 switch (event) {
143 case MOD_LOAD:
144 ngd_unit = new_unrhdr(0, MAX_NGD, NULL);
145 break;
146 case MOD_UNLOAD:
147 delete_unrhdr(ngd_unit);
148 break;
149 default:
150 error = EOPNOTSUPP;
151 break;
148 case MOD_LOAD:
149
150 ng_device_init();
151 break;
152
153 case MOD_UNLOAD:
154 /* XXX do we need to do something specific ? */
155 /* ng_device_breakdown */
156 break;
157
158 default:
159 error = EOPNOTSUPP;
160 break;
152 }
161 }
153 return (error);
162
163 return(error);
154}
155
164}
165
156/*
157 * create new node
158 */
166
159static int
167static int
160ng_device_constructor(node_p node)
168ng_device_init()
161{
169{
162 priv_p priv;
170 struct ngd_softc *sc = &ngd_softc;
171
172#ifdef NGD_DEBUG
173 printf("%s()\n",__func__);
174#endif /* NGD_DEBUG */
163
175
164 DBG;
176 SLIST_INIT(&sc->head);
165
177
166 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
167 if (priv == NULL)
168 return (ENOMEM);
178 if (ng_make_node_common(&typestruct, &sc->node) != 0) {
179 printf("%s(): ng_make_node_common failed\n",__func__);
180 return(ENXIO);
181 }
182 sprintf(sc->nodename, "%s", NG_DEVICE_NODE_TYPE);
183 if (ng_name_node(sc->node, sc->nodename)) {
184 NG_NODE_UNREF(sc->node); /* make it go away again */
185 printf("%s(): ng_name_node failed\n",__func__);
186 return(ENXIO);
187 }
188 NG_NODE_SET_PRIVATE(sc->node, sc);
169
189
170 /* Allocate unit number */
171 priv->unit = alloc_unr(ngd_unit);
190 return(0);
191}
172
192
173 /* Initialize mutexes and queue */
174 mtx_init(&priv->ngd_mtx, "ng_device", NULL, MTX_DEF);
175 mtx_init(&priv->readq.ifq_mtx, "ng_device queue", NULL, MTX_DEF);
176 IFQ_SET_MAXLEN(&priv->readq, ifqmaxlen);
193/*
194 * don't allow to be created, only the device can do that
195 */
196static int
197ng_device_cons(node_p node)
198{
177
199
178 /* Link everything together */
179 NG_NODE_SET_PRIVATE(node, priv);
180 priv->node = node;
181
182 priv->ngddev = make_dev(&ngd_cdevsw, priv->unit, UID_ROOT,
183 GID_WHEEL, 0600, NG_DEVICE_DEVNAME "%d", priv->unit);
184 if(priv->ngddev == NULL) {
185 printf("%s(): make_dev() failed\n",__func__);
186 mtx_destroy(&priv->ngd_mtx);
187 mtx_destroy(&priv->readq.ifq_mtx);
188 free_unr(ngd_unit, priv->unit);
189 free(priv, M_NETGRAPH);
190 return(EINVAL);
191 }
192 /* XXX: race here? */
193 priv->ngddev->si_drv1 = priv;
194
195 return(0);
200#ifdef NGD_DEBUG
201 printf("%s()\n",__func__);
202#endif /* NGD_DEBUG */
203
204 return(EINVAL);
196}
197
198/*
205}
206
207/*
199 * Process control message.
208 * Receive control message. We just bounce it back as a reply.
200 */
209 */
201
202static int
203ng_device_rcvmsg(node_p node, item_p item, hook_p lasthook)
204{
210static int
211ng_device_rcvmsg(node_p node, item_p item, hook_p lasthook)
212{
205 const priv_p priv = NG_NODE_PRIVATE(node);
213 struct ngd_softc *sc = &ngd_softc;
206 struct ng_mesg *msg;
214 struct ng_mesg *msg;
207 struct ng_mesg *resp = NULL;
208 int error = 0;
215 int error = 0;
216 struct ngd_connection * connection = NULL;
217 struct ngd_connection *tmp = NULL;
209
218
210 NGI_GET_MSG(item, msg);
219#ifdef NGD_DEBUG
220 printf("%s()\n",__func__);
221#endif /* NGD_DEBUG */
211
222
212 if (msg->header.typecookie == NGM_DEVICE_COOKIE) {
213 switch (msg->header.cmd) {
214 case NGM_DEVICE_GET_DEVNAME:
215 /* XXX: Fix when MAX_NGD us bigger */
216 NG_MKRESPONSE(resp, msg,
217 strlen(NG_DEVICE_DEVNAME) + 4, M_NOWAIT);
223 NGI_GET_MSG(item, msg);
218
224
219 if (resp == NULL)
220 ERROUT(ENOMEM);
225 SLIST_FOREACH(tmp,&sc->head,links) {
226 if(tmp->active_hook == lasthook) {
227 connection = tmp;
228 }
229 }
230 if(connection == NULL) {
231 printf("%s(): connection is still NULL, no hook found\n",__func__);
232 return(-1);
233 }
221
234
222 strlcpy((char *)resp->data, priv->ngddev->si_name,
223 strlen(priv->ngddev->si_name) + 1);
224 break;
235 return(error);
236}
225
237
226 default:
227 error = EINVAL;
228 break;
238static int
239get_free_unit()
240{
241 struct ngd_connection *tmp = NULL;
242 struct ngd_softc *sc = &ngd_softc;
243 int n = 0;
244 int unit = -1;
245
246#ifdef NGD_DEBUG
247 printf("%s()\n",__func__);
248#endif /* NGD_DEBUG */
249
250 /* When there is no list yet, the first device unit is always 0. */
251 if SLIST_EMPTY(&sc->head) {
252 unit = 0;
253 return(unit);
254 }
255
256 /* Just do a brute force loop to find the first free unit that is
257 * smaller than MAX_NGD.
258 * Set MAX_NGD to a large value, doesn't impact performance.
259 */
260 for(n = 0;n<MAX_NGD && unit == -1;n++) {
261 SLIST_FOREACH(tmp,&sc->head,links) {
262
263 if(tmp->unit == n) {
264 unit = -1;
265 break;
266 }
267 unit = n;
229 }
268 }
230 } else
231 error = EINVAL;
269 }
232
270
233done:
234 NG_RESPOND_MSG(error, node, item, resp);
235 NG_FREE_MSG(msg);
236 return (error);
271 return(unit);
237}
238
239/*
272}
273
274/*
240 * Accept incoming hook. We support only one hook per node.
275 * incoming hook
241 */
242static int
243ng_device_newhook(node_p node, hook_p hook, const char *name)
244{
276 */
277static int
278ng_device_newhook(node_p node, hook_p hook, const char *name)
279{
245 priv_p priv = NG_NODE_PRIVATE(node);
280 struct ngd_softc *sc = &ngd_softc;
281 struct ngd_connection * new_connection = NULL;
246
282
247 DBG;
283#ifdef NGD_DEBUG
284 printf("%s()\n",__func__);
285#endif /* NGD_DEBUG */
248
286
249 /* We have only one hook per node */
250 if (priv->hook != NULL)
251 return (EISCONN);
287 new_connection = malloc(sizeof(struct ngd_connection), M_DEVBUF, M_NOWAIT);
288 if(new_connection == NULL) {
289 printf("%s(): ERROR: new_connection == NULL\n",__func__);
290 return(-1);
291 }
252
292
253 priv->hook = hook;
293 new_connection->unit = get_free_unit();
294 if(new_connection->unit<0) {
295 printf("%s: No free unit found by get_free_unit(), "
296 "increas MAX_NGD\n",__func__);
297 return(-1);
298 }
299 new_connection->ngddev = make_dev(&ngd_cdevsw, new_connection->unit, 0, 0,0600,"ngd%d",new_connection->unit);
300 if(new_connection->ngddev == NULL) {
301 printf("%s(): make_dev failed\n",__func__);
302 return(-1);
303 }
254
304
305 new_connection->readq = malloc(sizeof(char)*NGD_QUEUE_SIZE, M_DEVBUF, M_NOWAIT | M_ZERO);
306 if(new_connection->readq == NULL) {
307 printf("%s(): readq malloc failed\n",__func__);
308 return(-1);
309 }
310
311 /* point to begin of buffer */
312 new_connection->loc = 0;
313 new_connection->active_hook = hook;
314
315 SLIST_INSERT_HEAD(&sc->head, new_connection, links);
316
255 return(0);
256}
257
258/*
317 return(0);
318}
319
320/*
259 * Receive data from hook, write it to device.
321 * we gave ok to a new hook
322 * now connect
260 */
261static int
323 */
324static int
325ng_device_connect(hook_p hook)
326{
327
328#ifdef NGD_DEBUG
329 printf("%s()\n",__func__);
330#endif /* NGD_DEBUG */
331
332 return(0);
333}
334
335
336/*
337 * Receive data from hook
338 */
339static int
262ng_device_rcvdata(hook_p hook, item_p item)
263{
340ng_device_rcvdata(hook_p hook, item_p item)
341{
264 priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
265 struct mbuf *m;
342 struct mbuf *m;
343 struct ngd_softc *sc = &ngd_softc;
344 struct ngd_connection * connection = NULL;
345 struct ngd_connection * tmp;
346 char *buffer;
266
347
267 DBG;
348#ifdef NGD_DEBUG
349 printf("%s()\n",__func__);
350#endif /* NGD_DEBUG */
268
351
352 SLIST_FOREACH(tmp,&sc->head,links) {
353 if(tmp->active_hook == hook) {
354 connection = tmp;
355 }
356 }
357 if(connection == NULL) {
358 printf("%s(): connection is still NULL, no hook found\n",__func__);
359 return(-1);
360 }
361
269 NGI_GET_M(item, m);
270 NG_FREE_ITEM(item);
271
362 NGI_GET_M(item, m);
363 NG_FREE_ITEM(item);
364
272 IF_LOCK(&priv->readq);
273 if (_IF_QFULL(&priv->readq)) {
274 _IF_DROP(&priv->readq);
275 IF_UNLOCK(&priv->readq);
276 NG_FREE_M(m);
277 return (ENOBUFS);
365 m = m_pullup(m,m->m_len);
366 if(m == NULL) {
367 printf("%s(): ERROR: m_pullup failed\n",__func__);
368 return(-1);
278 }
279
369 }
370
280 _IF_ENQUEUE(&priv->readq, m);
281 IF_UNLOCK(&priv->readq);
282 mtx_lock(&priv->ngd_mtx);
283 if (priv->flags & NGDF_RWAIT) {
284 priv->flags &= ~NGDF_RWAIT;
285 wakeup(priv);
371 buffer = malloc(sizeof(char)*m->m_len, M_DEVBUF, M_NOWAIT | M_ZERO);
372 if(buffer == NULL) {
373 printf("%s(): ERROR: buffer malloc failed\n",__func__);
374 return(-1);
286 }
375 }
287 mtx_unlock(&priv->ngd_mtx);
288
376
377 buffer = mtod(m,char *);
378
379 if( (connection->loc+m->m_len) < NGD_QUEUE_SIZE) {
380 memcpy(connection->readq+connection->loc, buffer, m->m_len);
381 connection->loc += m->m_len;
382 } else
383 printf("%s(): queue full, first read out a bit\n",__func__);
384
385 free(buffer,M_DEVBUF);
386
289 return(0);
290}
291
292/*
387 return(0);
388}
389
390/*
293 * Removal of the hook destroys the node.
391 * Removal of the last link destroys the node
294 */
295static int
296ng_device_disconnect(hook_p hook)
297{
392 */
393static int
394ng_device_disconnect(hook_p hook)
395{
298 priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
396 struct ngd_softc *sc = &ngd_softc;
397 struct ngd_connection * connection = NULL;
398 struct ngd_connection * tmp;
299
399
300 DBG;
400#ifdef NGD_DEBUG
401 printf("%s()\n",__func__);
402#endif /* NGD_DEBUG */
301
403
302 destroy_dev(priv->ngddev);
303 mtx_destroy(&priv->ngd_mtx);
404 SLIST_FOREACH(tmp,&sc->head,links) {
405 if(tmp->active_hook == hook) {
406 connection = tmp;
407 }
408 }
409 if(connection == NULL) {
410 printf("%s(): connection is still NULL, no hook found\n",__func__);
411 return(-1);
412 }
304
413
305 IF_DRAIN(&priv->readq);
306 mtx_destroy(&(priv)->readq.ifq_mtx);
414 free(connection->readq,M_DEVBUF);
307
415
308 free_unr(ngd_unit, priv->unit);
416 destroy_dev(connection->ngddev);
309
417
310 free(priv, M_NETGRAPH);
418 SLIST_REMOVE(&sc->head,connection,ngd_connection,links);
311
419
312 ng_rmnode_self(NG_HOOK_NODE(hook));
313
314 return(0);
315}
420 return(0);
421}
316
317/*
422/*
318 * Node shutdown. Everything is already done in disconnect method.
423 * the device is opened
319 */
320static int
424 */
425static int
321ng_device_shutdown(node_p node)
426ngdopen(dev_t dev, int flag, int mode, struct thread *td)
322{
427{
323 NG_NODE_UNREF(node);
324 return (0);
325}
326
428
327/******************************************************************************
328 * Device methods
329 ******************************************************************************/
429#ifdef NGD_DEBUG
430 printf("%s()\n",__func__);
431#endif /* NGD_DEBUG */
330
432
331/*
332 * the device is opened
333 */
334static int
335ngdopen(struct cdev *dev, int flag, int mode, struct thread *td)
336{
337 priv_p priv = (priv_p )dev->si_drv1;
338
339 DBG;
340
341 mtx_lock(&priv->ngd_mtx);
342 priv->flags |= NGDF_OPEN;
343 mtx_unlock(&priv->ngd_mtx);
344
345 return(0);
346}
347
348/*
433 return(0);
434}
435
436/*
349 * the device is closed
437 * the device is closed
350 */
351static int
438 */
439static int
352ngdclose(struct cdev *dev, int flag, int mode, struct thread *td)
440ngdclose(dev_t dev, int flag, int mode, struct thread *td)
353{
441{
354 priv_p priv = (priv_p )dev->si_drv1;
355
442
356 DBG;
357 mtx_lock(&priv->ngd_mtx);
358 priv->flags &= ~NGDF_OPEN;
359 mtx_unlock(&priv->ngd_mtx);
443#ifdef NGD_DEBUG
444 printf("%s()\n",__func__);
445#endif
360
361 return(0);
362}
363
446
447 return(0);
448}
449
364#if 0 /*
365 * The ioctl is transformed into netgraph control message.
366 * We do not process them, yet.
367 */
450
368/*
369 * process ioctl
370 *
371 * they are translated into netgraph messages and passed on
451/*
452 * process ioctl
453 *
454 * they are translated into netgraph messages and passed on
372 *
455 *
373 */
374static int
456 */
457static int
375ngdioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
458ngdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
376{
377 struct ngd_softc *sc = &ngd_softc;
378 struct ngd_connection * connection = NULL;
379 struct ngd_connection * tmp;
380 int error = 0;
381 struct ng_mesg *msg;
459{
460 struct ngd_softc *sc = &ngd_softc;
461 struct ngd_connection * connection = NULL;
462 struct ngd_connection * tmp;
463 int error = 0;
464 struct ng_mesg *msg;
382 struct ngd_param_s * datap;
465 struct ngd_param_s * datap;
383
466
384 DBG;
467#ifdef NGD_DEBUG
468 printf("%s()\n",__func__);
469#endif /* NGD_DEBUG */
385
470
386 NG_MKMESSAGE(msg, NGM_DEVICE_COOKIE, cmd, sizeof(struct ngd_param_s),
471 SLIST_FOREACH(tmp,&sc->head,links) {
472 if(tmp->ngddev == dev) {
473 connection = tmp;
474 }
475 }
476 if(connection == NULL) {
477 printf("%s(): connection is still NULL, no dev found\n",__func__);
478 return(-1);
479 }
480
481 /* NG_MKMESSAGE(msg, cookie, cmdid, len, how) */
482 NG_MKMESSAGE(msg, NGM_DEVICE_COOKIE, cmd, sizeof(struct ngd_param_s),
387 M_NOWAIT);
388 if (msg == NULL) {
389 printf("%s(): msg == NULL\n",__func__);
390 goto nomsg;
391 }
392
393 /* pass the ioctl data into the ->data area */
394 datap = (struct ngd_param_s *)msg->data;
483 M_NOWAIT);
484 if (msg == NULL) {
485 printf("%s(): msg == NULL\n",__func__);
486 goto nomsg;
487 }
488
489 /* pass the ioctl data into the ->data area */
490 datap = (struct ngd_param_s *)msg->data;
395 datap->p = addr;
491 datap->p = addr;
396
492
397 NG_SEND_MSG_HOOK(error, sc->node, msg, connection->active_hook, 0);
493 /* NG_SEND_MSG_HOOK(error, here, msg, hook, retaddr) */
494 NG_SEND_MSG_HOOK(error, sc->node, msg, connection->active_hook, NULL);
398 if(error)
399 printf("%s(): NG_SEND_MSG_HOOK error: %d\n",__func__,error);
400
401nomsg:
402
403 return(0);
404}
495 if(error)
496 printf("%s(): NG_SEND_MSG_HOOK error: %d\n",__func__,error);
497
498nomsg:
499
500 return(0);
501}
405#endif /* if 0 */
406
502
503
407/*
408 * This function is called when a read(2) is done to our device.
504/*
505 * This function is called when a read(2) is done to our device.
409 * We process one mbuf from queue.
506 * We pass the data available in kernelspace on into userland using
507 * uiomove.
410 */
411static int
508 */
509static int
412ngdread(struct cdev *dev, struct uio *uio, int flag)
510ngdread(dev_t dev, struct uio *uio, int flag)
413{
511{
414 priv_p priv = (priv_p )dev->si_drv1;
415 struct mbuf *m;
416 int len, error = 0;
512 int ret = 0, amnt;
513 char buffer[uio->uio_resid+1];
514 struct ngd_softc *sc = &ngd_softc;
515 struct ngd_connection * connection = NULL;
516 struct ngd_connection * tmp;
417
517
418 DBG;
518#ifdef NGD_DEBUG
519 printf("%s()\n",__func__);
520#endif /* NGD_DEBUG */
419
521
420 /* get an mbuf */
421 do {
422 IF_DEQUEUE(&priv->readq, m);
423 if (m == NULL) {
424 if (flag & IO_NDELAY)
425 return (EWOULDBLOCK);
426 mtx_lock(&priv->ngd_mtx);
427 priv->flags |= NGDF_RWAIT;
428 if ((error = msleep(priv, &priv->ngd_mtx,
429 PDROP | PCATCH | (PZERO + 1),
430 "ngdread", 0)) != 0)
431 return (error);
522 SLIST_FOREACH(tmp,&sc->head,links) {
523 if(tmp->ngddev == dev) {
524 connection = tmp;
432 }
525 }
433 } while (m == NULL);
434
435 while (m && uio->uio_resid > 0 && error == 0) {
436 len = MIN(uio->uio_resid, m->m_len);
437 if (len != 0)
438 error = uiomove(mtod(m, void *), len, uio);
439 m = m_free(m);
440 }
526 }
527 if(connection == NULL) {
528 printf("%s(): connection is still NULL, no dev found\n",__func__);
529 return(-1);
530 }
441
531
442 if (m)
443 m_freem(m);
532 while ( ( uio->uio_resid > 0 ) && ( connection->loc > 0 ) ) {
533 amnt = MIN(uio->uio_resid,connection->loc);
444
534
445 return (error);
535 memcpy(buffer,connection->readq, amnt);
536 memcpy(connection->readq, connection->readq+amnt,
537 connection->loc-amnt);
538 connection->loc -= amnt;
539
540 ret = uiomove((caddr_t)buffer, amnt, uio);
541 if(ret != 0)
542 goto error;
543
544 }
545 return(0);
546
547error:
548 printf("%s(): uiomove returns error %d\n",__func__,ret);
549 /* do error cleanup here */
550 return(ret);
446}
447
448
551}
552
553
449/*
554/*
450 * This function is called when our device is written to.
555 * This function is called when our device is written to.
451 * We read the data from userland into mbuf chain and pass it to the remote hook.
556 * We read the data from userland into our local buffer and pass it on
557 * into the remote hook.
452 *
453 */
454static int
558 *
559 */
560static int
455ngdwrite(struct cdev *dev, struct uio *uio, int flag)
561ngdwrite(dev_t dev, struct uio *uio, int flag)
456{
562{
457 priv_p priv = (priv_p )dev->si_drv1;
458 struct mbuf *m;
563 int ret;
459 int error = 0;
564 int error = 0;
565 struct mbuf *m;
566 char buffer[uio->uio_resid];
567 int len = uio->uio_resid;
568 struct ngd_softc *sc =& ngd_softc;
569 struct ngd_connection * connection = NULL;
570 struct ngd_connection * tmp;
460
571
461 DBG;
572#ifdef NGD_DEBUG
573 printf("%s()\n",__func__);
574#endif /* NGD_DEBUG */
462
575
463 if (uio->uio_resid == 0)
464 return (0);
576 SLIST_FOREACH(tmp,&sc->head,links) {
577 if(tmp->ngddev == dev) {
578 connection = tmp;
579 }
580 }
465
581
466 if (uio->uio_resid < 0 || uio->uio_resid > IP_MAXPACKET)
467 return (EIO);
582 if(connection == NULL) {
583 printf("%s(): connection is still NULL, no dev found\n",__func__);
584 return(-1);
585 }
468
586
469 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0, 0, M_PKTHDR)) == NULL)
470 return (ENOBUFS);
587 if (len > 0) {
588 if ((ret = uiomove((caddr_t)buffer, len, uio)) != 0)
589 goto error;
590 } else
591 printf("%s(): len <= 0 : is this supposed to happen?!\n",__func__);
471
592
472 NG_SEND_DATA_ONLY(error, priv->hook, m);
593 m = m_devget(buffer,len,0,NULL,NULL);
473
594
474 return (error);
595 NG_SEND_DATA_ONLY(error,connection->active_hook,m);
596
597 return(0);
598
599error:
600 /* do error cleanup here */
601 printf("%s(): uiomove returned err: %d\n",__func__,ret);
602
603 return(ret);
475}
476
477/*
478 * we are being polled/selected
479 * check if there is data available for read
480 */
481static int
604}
605
606/*
607 * we are being polled/selected
608 * check if there is data available for read
609 */
610static int
482ngdpoll(struct cdev *dev, int events, struct thread *td)
611ngdpoll(dev_t dev, int events, struct thread *td)
483{
612{
484 priv_p priv = (priv_p )dev->si_drv1;
485 int revents = 0;
613 int revents = 0;
614 struct ngd_softc *sc = &ngd_softc;
615 struct ngd_connection * connection = NULL;
616 struct ngd_connection * tmp;
486
617
487 if (events & (POLLIN | POLLRDNORM) &&
488 !IFQ_IS_EMPTY(&priv->readq))
489 revents |= events & (POLLIN | POLLRDNORM);
490
618
491 return (revents);
619 if (events & (POLLIN | POLLRDNORM)) {
620 /* get the connection we have to know the loc from */
621 SLIST_FOREACH(tmp,&sc->head,links) {
622 if(tmp->ngddev == dev) {
623 connection = tmp;
624 }
625 }
626 if(connection == NULL) {
627 printf("%s(): ERROR: connection is still NULL,"
628 "no dev found\n",__func__);
629 return(-1);
630 }
631
632 if (connection->loc > 0)
633 revents |= events & (POLLIN | POLLRDNORM);
634 }
635
636 return(revents);
492}
637}