Pre-2.0 release: Sync with HAMMER 64 - NFS and cross-device link fixes.
[dragonfly.git] / sys / netgraph7 / ng_device.c
blobbda3221eb309932395ec2da7944388ad319cf210
1 /*-
2 * Copyright (c) 2002 Mark Santcroos <marks@ripe.net>
3 * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
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.
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.
25 * Netgraph "device" node
27 * This node presents a /dev/ngd%d device that interfaces to an other
28 * netgraph node.
30 * $FreeBSD: src/sys/netgraph/ng_device.c,v 1.22 2006/11/02 17:37:21 andre Exp $
31 * $DragonFly: src/sys/netgraph7/ng_device.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
35 #if 0
36 #define DBG do { printf("ng_device: %s\n", __func__ ); } while (0)
37 #else
38 #define DBG do {} while (0)
39 #endif
41 #include <sys/param.h>
42 #include <sys/conf.h>
43 #include <sys/ioccom.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/poll.h>
48 #include <sys/queue.h>
49 #include <sys/socket.h>
50 #include <sys/systm.h>
51 #include <sys/uio.h>
52 #include <sys/vnode.h>
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
60 #include "ng_message.h"
61 #include "netgraph.h"
62 #include "ng_device.h"
64 #define ERROUT(x) do { error = (x); goto done; } while (0)
66 /* Netgraph methods */
67 static int ng_device_mod_event(module_t, int, void *);
68 static ng_constructor_t ng_device_constructor;
69 static ng_rcvmsg_t ng_device_rcvmsg;
70 static ng_shutdown_t ng_device_shutdown;
71 static ng_newhook_t ng_device_newhook;
72 static ng_rcvdata_t ng_device_rcvdata;
73 static ng_disconnect_t ng_device_disconnect;
75 /* Netgraph type */
76 static struct ng_type ngd_typestruct = {
77 .version = NG_ABI_VERSION,
78 .name = NG_DEVICE_NODE_TYPE,
79 .mod_event = ng_device_mod_event,
80 .constructor = ng_device_constructor,
81 .rcvmsg = ng_device_rcvmsg,
82 .shutdown = ng_device_shutdown,
83 .newhook = ng_device_newhook,
84 .rcvdata = ng_device_rcvdata,
85 .disconnect = ng_device_disconnect,
87 NETGRAPH_INIT(device, &ngd_typestruct);
89 /* per node data */
90 struct ngd_private {
91 struct ifqueue readq;
92 struct ng_node *node;
93 struct ng_hook *hook;
94 struct cdev *ngddev;
95 struct mtx ngd_mtx;
96 int unit;
97 uint16_t flags;
98 #define NGDF_OPEN 0x0001
99 #define NGDF_RWAIT 0x0002
101 typedef struct ngd_private *priv_p;
103 /* unit number allocator entity */
104 static struct unrhdr *ngd_unit;
106 /* Maximum number of NGD devices */
107 #define MAX_NGD 999
109 static d_close_t ngdclose;
110 static d_open_t ngdopen;
111 static d_read_t ngdread;
112 static d_write_t ngdwrite;
113 #if 0
114 static d_ioctl_t ngdioctl;
115 #endif
116 static d_poll_t ngdpoll;
118 static struct cdevsw ngd_cdevsw = {
119 .d_version = D_VERSION,
120 .d_open = ngdopen,
121 .d_close = ngdclose,
122 .d_read = ngdread,
123 .d_write = ngdwrite,
124 #if 0
125 .d_ioctl = ngdioctl,
126 #endif
127 .d_poll = ngdpoll,
128 .d_name = NG_DEVICE_DEVNAME,
131 /******************************************************************************
132 * Netgraph methods
133 ******************************************************************************/
136 * Handle loading and unloading for this node type.
138 static int
139 ng_device_mod_event(module_t mod, int event, void *data)
141 int error = 0;
143 switch (event) {
144 case MOD_LOAD:
145 ngd_unit = new_unrhdr(0, MAX_NGD, NULL);
146 break;
147 case MOD_UNLOAD:
148 delete_unrhdr(ngd_unit);
149 break;
150 default:
151 error = EOPNOTSUPP;
152 break;
154 return (error);
158 * create new node
160 static int
161 ng_device_constructor(node_p node)
163 priv_p priv;
165 DBG;
167 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
168 if (priv == NULL)
169 return (ENOMEM);
171 /* Allocate unit number */
172 priv->unit = alloc_unr(ngd_unit);
174 /* Initialize mutexes and queue */
175 mtx_init(&priv->ngd_mtx, "ng_device", NULL, MTX_DEF);
176 mtx_init(&priv->readq.ifq_mtx, "ng_device queue", NULL, MTX_DEF);
177 IFQ_SET_MAXLEN(&priv->readq, ifqmaxlen);
179 /* Link everything together */
180 NG_NODE_SET_PRIVATE(node, priv);
181 priv->node = node;
183 priv->ngddev = make_dev(&ngd_cdevsw, unit2minor(priv->unit), UID_ROOT,
184 GID_WHEEL, 0600, NG_DEVICE_DEVNAME "%d", priv->unit);
185 if(priv->ngddev == NULL) {
186 printf("%s(): make_dev() failed\n",__func__);
187 mtx_destroy(&priv->ngd_mtx);
188 mtx_destroy(&priv->readq.ifq_mtx);
189 free_unr(ngd_unit, priv->unit);
190 FREE(priv, M_NETGRAPH);
191 return(EINVAL);
193 /* XXX: race here? */
194 priv->ngddev->si_drv1 = priv;
196 return(0);
200 * Process control message.
203 static int
204 ng_device_rcvmsg(node_p node, item_p item, hook_p lasthook)
206 const priv_p priv = NG_NODE_PRIVATE(node);
207 struct ng_mesg *msg;
208 struct ng_mesg *resp = NULL;
209 int error = 0;
211 NGI_GET_MSG(item, msg);
213 if (msg->header.typecookie == NGM_DEVICE_COOKIE) {
214 switch (msg->header.cmd) {
215 case NGM_DEVICE_GET_DEVNAME:
216 /* XXX: Fix when MAX_NGD us bigger */
217 NG_MKRESPONSE(resp, msg,
218 strlen(NG_DEVICE_DEVNAME) + 4, M_WAITOK);
220 if (resp == NULL)
221 ERROUT(ENOMEM);
223 strlcpy((char *)resp->data, priv->ngddev->si_name,
224 strlen(priv->ngddev->si_name) + 1);
225 break;
227 default:
228 error = EINVAL;
229 break;
231 } else
232 error = EINVAL;
234 done:
235 NG_RESPOND_MSG(error, node, item, resp);
236 NG_FREE_MSG(msg);
237 return (error);
241 * Accept incoming hook. We support only one hook per node.
243 static int
244 ng_device_newhook(node_p node, hook_p hook, const char *name)
246 priv_p priv = NG_NODE_PRIVATE(node);
248 DBG;
250 /* We have only one hook per node */
251 if (priv->hook != NULL)
252 return (EISCONN);
254 priv->hook = hook;
256 return(0);
260 * Receive data from hook, write it to device.
262 static int
263 ng_device_rcvdata(hook_p hook, item_p item)
265 priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
266 struct mbuf *m;
268 DBG;
270 NGI_GET_M(item, m);
271 NG_FREE_ITEM(item);
273 IF_LOCK(&priv->readq);
274 if (_IF_QFULL(&priv->readq)) {
275 _IF_DROP(&priv->readq);
276 IF_UNLOCK(&priv->readq);
277 NG_FREE_M(m);
278 return (ENOBUFS);
281 _IF_ENQUEUE(&priv->readq, m);
282 IF_UNLOCK(&priv->readq);
283 mtx_lock(&priv->ngd_mtx);
284 if (priv->flags & NGDF_RWAIT) {
285 priv->flags &= ~NGDF_RWAIT;
286 wakeup(priv);
288 mtx_unlock(&priv->ngd_mtx);
290 return(0);
294 * Removal of the hook destroys the node.
296 static int
297 ng_device_disconnect(hook_p hook)
299 priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
301 DBG;
303 destroy_dev(priv->ngddev);
304 mtx_destroy(&priv->ngd_mtx);
306 IF_DRAIN(&priv->readq);
307 mtx_destroy(&(priv)->readq.ifq_mtx);
309 free_unr(ngd_unit, priv->unit);
311 FREE(priv, M_NETGRAPH);
313 ng_rmnode_self(NG_HOOK_NODE(hook));
315 return(0);
319 * Node shutdown. Everything is already done in disconnect method.
321 static int
322 ng_device_shutdown(node_p node)
324 NG_NODE_UNREF(node);
325 return (0);
328 /******************************************************************************
329 * Device methods
330 ******************************************************************************/
333 * the device is opened
335 static int
336 ngdopen(struct cdev *dev, int flag, int mode, struct thread *td)
338 priv_p priv = (priv_p )dev->si_drv1;
340 DBG;
342 mtx_lock(&priv->ngd_mtx);
343 priv->flags |= NGDF_OPEN;
344 mtx_unlock(&priv->ngd_mtx);
346 return(0);
350 * the device is closed
352 static int
353 ngdclose(struct cdev *dev, int flag, int mode, struct thread *td)
355 priv_p priv = (priv_p )dev->si_drv1;
357 DBG;
358 mtx_lock(&priv->ngd_mtx);
359 priv->flags &= ~NGDF_OPEN;
360 mtx_unlock(&priv->ngd_mtx);
362 return(0);
365 #if 0 /*
366 * The ioctl is transformed into netgraph control message.
367 * We do not process them, yet.
370 * process ioctl
372 * they are translated into netgraph messages and passed on
375 static int
376 ngdioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
378 struct ngd_softc *sc = &ngd_softc;
379 struct ngd_connection * connection = NULL;
380 struct ngd_connection * tmp;
381 int error = 0;
382 struct ng_mesg *msg;
383 struct ngd_param_s * datap;
385 DBG;
387 NG_MKMESSAGE(msg, NGM_DEVICE_COOKIE, cmd, sizeof(struct ngd_param_s),
388 M_WAITOK);
389 if (msg == NULL) {
390 printf("%s(): msg == NULL\n",__func__);
391 goto nomsg;
394 /* pass the ioctl data into the ->data area */
395 datap = (struct ngd_param_s *)msg->data;
396 datap->p = addr;
398 NG_SEND_MSG_HOOK(error, sc->node, msg, connection->active_hook, 0);
399 if(error)
400 printf("%s(): NG_SEND_MSG_HOOK error: %d\n",__func__,error);
402 nomsg:
404 return(0);
406 #endif /* if 0 */
409 * This function is called when a read(2) is done to our device.
410 * We process one mbuf from queue.
412 static int
413 ngdread(struct cdev *dev, struct uio *uio, int flag)
415 priv_p priv = (priv_p )dev->si_drv1;
416 struct mbuf *m;
417 int len, error = 0;
419 DBG;
421 /* get an mbuf */
422 do {
423 IF_DEQUEUE(&priv->readq, m);
424 if (m == NULL) {
425 if (flag & IO_NDELAY)
426 return (EWOULDBLOCK);
427 mtx_lock(&priv->ngd_mtx);
428 priv->flags |= NGDF_RWAIT;
429 if ((error = msleep(priv, &priv->ngd_mtx,
430 PDROP | PCATCH | (PZERO + 1),
431 "ngdread", 0)) != 0)
432 return (error);
434 } while (m == NULL);
436 while (m && uio->uio_resid > 0 && error == 0) {
437 len = MIN(uio->uio_resid, m->m_len);
438 if (len != 0)
439 error = uiomove(mtod(m, void *), len, uio);
440 m = m_free(m);
443 if (m)
444 m_freem(m);
446 return (error);
451 * This function is called when our device is written to.
452 * We read the data from userland into mbuf chain and pass it to the remote hook.
455 static int
456 ngdwrite(struct cdev *dev, struct uio *uio, int flag)
458 priv_p priv = (priv_p )dev->si_drv1;
459 struct mbuf *m;
460 int error = 0;
462 DBG;
464 if (uio->uio_resid == 0)
465 return (0);
467 if (uio->uio_resid < 0 || uio->uio_resid > IP_MAXPACKET)
468 return (EIO);
470 if ((m = m_uiotombuf(uio, MB_DONTWAIT, 0, 0, M_PKTHDR)) == NULL)
471 return (ENOBUFS);
473 NG_SEND_DATA_ONLY(error, priv->hook, m);
475 return (error);
479 * we are being polled/selected
480 * check if there is data available for read
482 static int
483 ngdpoll(struct cdev *dev, int events, struct thread *td)
485 priv_p priv = (priv_p )dev->si_drv1;
486 int revents = 0;
488 if (events & (POLLIN | POLLRDNORM) &&
489 !IFQ_IS_EMPTY(&priv->readq))
490 revents |= events & (POLLIN | POLLRDNORM);
492 return (revents);