#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/mbuf.h>
#include <sys/uio.h>
#include <sys/queue.h>
#include <sys/malloc.h>
#include <sys/conf.h>
#include <sys/poll.h>
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include "ng_device.h"
#define NGD_DEBUG
static ng_constructor_t ng_device_cons;
static ng_rcvmsg_t ng_device_rcvmsg;
static ng_newhook_t ng_device_newhook;
static ng_connect_t ng_device_connect;
static ng_rcvdata_t ng_device_rcvdata;
static ng_disconnect_t ng_device_disconnect;
static int ng_device_mod_event(module_t mod, int event, void *data);
static int ng_device_init(void);
static int get_free_unit(void);
static struct ng_type typestruct = {
NG_VERSION,
NG_DEVICE_NODE_TYPE,
ng_device_mod_event,
ng_device_cons,
ng_device_rcvmsg,
NULL,
ng_device_newhook,
NULL,
ng_device_connect,
ng_device_rcvdata,
ng_device_rcvdata,
ng_device_disconnect,
NULL
};
NETGRAPH_INIT(device, &typestruct);
struct ngd_connection {
SLIST_ENTRY(ngd_connection) links;
cdev_t ngddev;
struct ng_hook *active_hook;
char *readq;
int loc;
int unit;
};
struct ngd_softc {
SLIST_HEAD(, ngd_connection) head;
node_p node;
char nodename[NG_NODESIZ];
} ngd_softc;
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define NGD_QUEUE_SIZE (1024*10)
#define MAX_NGD 25
static d_close_t ngdclose;
static d_open_t ngdopen;
static d_read_t ngdread;
static d_write_t ngdwrite;
static d_ioctl_t ngdioctl;
static d_poll_t ngdpoll;
#define NGD_CDEV_MAJOR 20
static struct cdevsw ngd_cdevsw = {
"ngd",
NGD_CDEV_MAJOR,
0,
NULL,
NULL,
ngdopen,
ngdclose,
ngdread,
ngdwrite,
ngdioctl,
ngdpoll,
nommap,
nostrategy,
nodump,
nopsize
};
static int
ng_device_mod_event(module_t mod, int event, void *data)
{
int error = 0;
#ifdef NGD_DEBUG
kprintf("%s()\n", __func__);
#endif
switch (event) {
case MOD_LOAD:
cdevsw_add(&ngd_cdevsw, 0, 0);
ng_device_init();
break;
case MOD_UNLOAD:
cdevsw_remove(&ngd_cdevsw, 0, 0);
error = EBUSY;
break;
default:
error = EOPNOTSUPP;
break;
}
return(error);
}
static int
ng_device_init(void)
{
struct ngd_softc *sc = &ngd_softc;
#ifdef NGD_DEBUG
kprintf("%s()\n", __func__);
#endif
SLIST_INIT(&sc->head);
if (ng_make_node_common(&typestruct, &sc->node) != 0) {
kprintf("%s(): ng_make_node_common failed\n", __func__);
return(ENXIO);
}
ksprintf(sc->nodename, "%s", NG_DEVICE_NODE_TYPE);
if (ng_name_node(sc->node, sc->nodename)) {
NG_NODE_UNREF(sc->node);
kprintf("%s(): ng_name_node failed\n", __func__);
return(ENXIO);
}
NG_NODE_SET_PRIVATE(sc->node, sc);
return(0);
}
static int
ng_device_cons(node_p node)
{
#ifdef NGD_DEBUG
kprintf("%s()\n", __func__);
#endif
return(EINVAL);
}
static int
ng_device_rcvmsg(node_p node,
struct ng_mesg *msg, const char *retaddr, struct ng_mesg **rptr)
{
kfree(msg, M_NETGRAPH);
return(ENOTTY);
}
static int
get_free_unit(void)
{
struct ngd_connection *tmp = NULL;
struct ngd_softc *sc = &ngd_softc;
int n = 0;
int unit = -1;
#ifdef NGD_DEBUG
kprintf("%s()\n", __func__);
#endif
if (SLIST_EMPTY(&sc->head)) {
unit = 0;
return(unit);
}
for(n = 0;n<MAX_NGD && unit == -1;n++) {
SLIST_FOREACH(tmp, &sc->head, links) {
if(tmp->unit == n) {
unit = -1;
break;
}
unit = n;
}
}
return(unit);
}
static int
ng_device_newhook(node_p node, hook_p hook, const char *name)
{
struct ngd_softc *sc = &ngd_softc;
struct ngd_connection * new_connection = NULL;
#ifdef NGD_DEBUG
kprintf("%s()\n", __func__);
#endif
new_connection = kmalloc(sizeof(struct ngd_connection), M_DEVBUF, M_NOWAIT);
if(new_connection == NULL) {
kprintf("%s(): ERROR: new_connection == NULL\n", __func__);
return(-1);
}
new_connection->unit = get_free_unit();
if(new_connection->unit<0) {
kprintf("%s: No free unit found by get_free_unit(), "
"increas MAX_NGD\n", __func__);
kfree(new_connection, M_DEVBUF);
return(-1);
}
new_connection->ngddev = make_dev(&ngd_cdevsw,
new_connection->unit, 0, 0, 0600, "ngd%d", new_connection->unit);
if(new_connection->ngddev == NULL) {
kprintf("%s(): make_dev failed\n", __func__);
SLIST_REMOVE(&sc->head, new_connection, ngd_connection, links);
kfree(new_connection, M_DEVBUF);
return(-1);
}
new_connection->readq =
kmalloc(sizeof(char)*NGD_QUEUE_SIZE, M_DEVBUF, M_NOWAIT | M_ZERO);
if(new_connection->readq == NULL) {
kprintf("%s(): readq malloc failed\n", __func__);
destroy_dev(new_connection->ngddev);
SLIST_REMOVE(&sc->head, new_connection, ngd_connection, links);
kfree(new_connection, M_DEVBUF);
return(-1);
}
new_connection->loc = 0;
new_connection->active_hook = hook;
SLIST_INSERT_HEAD(&sc->head, new_connection, links);
return(0);
}
static int
ng_device_connect(hook_p hook)
{
#ifdef NGD_DEBUG
kprintf("%s()\n", __func__);
#endif
return(0);
}
static int
ng_device_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
{
struct ngd_softc *sc = &ngd_softc;
struct ngd_connection * connection = NULL;
struct ngd_connection * tmp;
char *buffer;
#ifdef NGD_DEBUG
kprintf("%s()\n", __func__);
#endif
SLIST_FOREACH(tmp, &sc->head, links) {
if(tmp->active_hook == hook) {
connection = tmp;
}
}
if(connection == NULL) {
kprintf("%s(): connection still NULL, no hook found\n", __func__);
return(-1);
}
NG_FREE_META(meta);
m = m_pullup(m, m->m_len);
if(m == NULL) {
kprintf("%s(): ERROR: m_pullup failed\n", __func__);
return(-1);
}
buffer = kmalloc(sizeof(char)*m->m_len, M_DEVBUF, M_NOWAIT | M_ZERO);
if(buffer == NULL) {
kprintf("%s(): ERROR: buffer malloc failed\n", __func__);
return(-1);
}
buffer = mtod(m, char *);
if( (connection->loc+m->m_len) < NGD_QUEUE_SIZE) {
memcpy(connection->readq+connection->loc, buffer, m->m_len);
connection->loc += m->m_len;
} else
kprintf("%s(): queue full, first read out a bit\n", __func__);
kfree(buffer, M_DEVBUF);
return(0);
}
static int
ng_device_disconnect(hook_p hook)
{
struct ngd_softc *sc = &ngd_softc;
struct ngd_connection * connection = NULL;
struct ngd_connection * tmp;
#ifdef NGD_DEBUG
kprintf("%s()\n", __func__);
#endif
SLIST_FOREACH(tmp, &sc->head, links) {
if(tmp->active_hook == hook) {
connection = tmp;
}
}
if(connection == NULL) {
kprintf("%s(): connection still NULL, no hook found\n",
__func__);
return(-1);
}
kfree(connection->readq, M_DEVBUF);
destroy_dev(connection->ngddev);
SLIST_REMOVE(&sc->head, connection, ngd_connection, links);
return(0);
}
static int
ngdopen(cdev_t dev, int flag, int mode, struct thread *td)
{
#ifdef NGD_DEBUG
kprintf("%s()\n", __func__);
#endif
return(0);
}
static int
ngdclose(cdev_t dev, int flag, int mode, struct thread *td)
{
#ifdef NGD_DEBUG
kprintf("%s()\n", __func__);
#endif
return(0);
}
static int
ngdioctl(cdev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
{
struct ngd_softc *sc = &ngd_softc;
struct ngd_connection * connection = NULL;
struct ngd_connection * tmp;
int error = 0;
struct ng_mesg *msg;
struct ngd_param_s * datap;
#ifdef NGD_DEBUG
kprintf("%s()\n", __func__);
#endif
SLIST_FOREACH(tmp, &sc->head, links) {
if(tmp->ngddev == dev) {
connection = tmp;
}
}
if(connection == NULL) {
kprintf("%s(): connection still NULL, no dev found\n",
__func__);
return(-1);
}
NG_MKMESSAGE(msg, NGM_DEVICE_COOKIE, cmd, sizeof(struct ngd_param_s),
M_NOWAIT);
if (msg == NULL) {
kprintf("%s(): msg == NULL\n", __func__);
goto nomsg;
}
datap = (struct ngd_param_s *)msg->data;
datap->p = addr;
error = ng_send_msg(sc->node, msg,
NG_HOOK_NAME(connection->active_hook), NULL);
if(error)
kprintf("%s(): ng_send_msg() error: %d\n", __func__, error);
nomsg:
return(0);
}
static int
ngdread(cdev_t dev, struct uio *uio, int flag)
{
int ret = 0, amnt;
char buffer[uio->uio_resid+1];
struct ngd_softc *sc = &ngd_softc;
struct ngd_connection * connection = NULL;
struct ngd_connection * tmp;
#ifdef NGD_DEBUG
kprintf("%s()\n", __func__);
#endif
SLIST_FOREACH(tmp, &sc->head, links) {
if(tmp->ngddev == dev) {
connection = tmp;
}
}
if(connection == NULL) {
kprintf("%s(): connection still NULL, no dev found\n", __func__);
return(-1);
}
while ( ( uio->uio_resid > 0 ) && ( connection->loc > 0 ) ) {
amnt = MIN(uio->uio_resid, connection->loc);
memcpy(buffer, connection->readq, amnt);
memcpy(connection->readq, connection->readq+amnt,
connection->loc-amnt);
connection->loc -= amnt;
ret = uiomove((caddr_t)buffer, amnt, uio);
if(ret != 0)
goto error;
}
return(0);
error:
kprintf("%s(): uiomove returns error %d\n", __func__, ret);
return(ret);
}
static int
ngdwrite(cdev_t dev, struct uio *uio, int flag)
{
int ret;
int error = 0;
struct mbuf *m;
char buffer[uio->uio_resid];
int len = uio->uio_resid;
struct ngd_softc *sc =& ngd_softc;
struct ngd_connection * connection = NULL;
struct ngd_connection * tmp;
#ifdef NGD_DEBUG
kprintf("%s()\n", __func__);
#endif
SLIST_FOREACH(tmp, &sc->head, links) {
if(tmp->ngddev == dev) {
connection = tmp;
}
}
if(connection == NULL) {
kprintf("%s(): connection still NULL, no dev found\n", __func__);
return(-1);
}
if (len > 0) {
if ((ret = uiomove((caddr_t)buffer, len, uio)) != 0)
goto error;
} else
kprintf("%s(): len <= 0 : supposed to happen?!\n", __func__);
m = m_devget(buffer, len, 0, NULL);
NG_SEND_DATA_ONLY(error, connection->active_hook, m);
return(0);
error:
kprintf("%s(): uiomove returned err: %d\n", __func__, ret);
return(ret);
}
static int
ngdpoll(cdev_t dev, int events, struct thread *td)
{
int revents = 0;
struct ngd_softc *sc = &ngd_softc;
struct ngd_connection * connection = NULL;
struct ngd_connection * tmp;
if (events & (POLLIN | POLLRDNORM)) {
SLIST_FOREACH(tmp, &sc->head, links) {
if(tmp->ngddev == dev) {
connection = tmp;
}
}
if(connection == NULL) {
kprintf("%s(): ERROR: connection still NULL, "
"no dev found\n", __func__);
return(-1);
}
if (connection->loc > 0)
revents |= events & (POLLIN | POLLRDNORM);
}
return(revents);
}