37#define DBG do { printf("ng_device: %s\n", __func__ ); } while (0)
39#define DBG do {} while (0)
44#include <sys/ioccom.h>
45#include <sys/kernel.h>
46#include <sys/malloc.h>
52#include <sys/socket.h>
53#include <sys/syslog.h>
58#include <net/ethernet.h>
60#include <net/if_var.h>
61#include <netinet/in.h>
62#include <netinet/in_systm.h>
63#include <netinet/ip.h>
70#define ERROUT(x) do { error = (x); goto done; } while (0)
125#define NGDF_OPEN 0x0001
126#define NGDF_RWAIT 0x0002
141static d_ioctl_t ngdioctl;
146 .d_version = D_VERSION,
194 priv = malloc(
sizeof(*
priv), M_NETGRAPH, M_WAITOK | M_ZERO);
200 mtx_init(&
priv->ngd_mtx,
"ng_device", NULL, MTX_DEF);
201 mtx_init(&
priv->readq.ifq_mtx,
"ng_device queue", NULL, MTX_DEF);
210 if(
priv->ngddev == NULL) {
211 printf(
"%s(): make_dev() failed\n",__func__);
212 mtx_destroy(&
priv->ngd_mtx);
213 mtx_destroy(&
priv->readq.ifq_mtx);
215 free(
priv, M_NETGRAPH);
223 log(LOG_WARNING,
"%s: can't acquire netgraph name\n",
224 devtoname(
priv->ngddev));
254 dn = devtoname(
priv->ngddev);
255 strlcpy((
char *)resp->
data, dn, strlen(dn) + 1);
260#ifndef __NO_STRICT_ALIGNMENT
261 priv->ether_align = ETHER_ALIGN;
289 if (
priv->hook != NULL)
311 IF_LOCK(&
priv->readq);
312 if (_IF_QFULL(&
priv->readq)) {
313 IF_UNLOCK(&
priv->readq);
318 _IF_ENQUEUE(&
priv->readq, m);
319 IF_UNLOCK(&
priv->readq);
320 mtx_lock(&
priv->ngd_mtx);
322 priv->flags &= ~NGDF_RWAIT;
325 mtx_unlock(&
priv->ngd_mtx);
340 destroy_dev(
priv->ngddev);
341 mtx_destroy(&
priv->ngd_mtx);
343 IF_DRAIN(&
priv->readq);
344 mtx_destroy(&(
priv)->readq.ifq_mtx);
348 free(
priv, M_NETGRAPH);
373ngdopen(
struct cdev *dev,
int flag,
int mode,
struct thread *td)
379 mtx_lock(&
priv->ngd_mtx);
381 mtx_unlock(&
priv->ngd_mtx);
390ngdclose(
struct cdev *dev,
int flag,
int mode,
struct thread *td)
395 mtx_lock(&
priv->ngd_mtx);
396 priv->flags &= ~NGDF_OPEN;
397 mtx_unlock(&
priv->ngd_mtx);
413ngdioctl(
struct cdev *dev, u_long
cmd, caddr_t addr,
int flag,
struct thread *td)
415 struct ngd_softc *sc = &ngd_softc;
416 struct ngd_connection * connection = NULL;
417 struct ngd_connection * tmp;
420 struct ngd_param_s * datap;
427 printf(
"%s(): msg == NULL\n",__func__);
432 datap = (
struct ngd_param_s *)msg->
data;
437 printf(
"%s(): NG_SEND_MSG_HOOK error: %d\n",__func__,error);
450ngdread(
struct cdev *dev,
struct uio *uio,
int flag)
460 IF_DEQUEUE(&
priv->readq, m);
462 if (flag & IO_NDELAY)
463 return (EWOULDBLOCK);
464 mtx_lock(&
priv->ngd_mtx);
466 if ((error = msleep(
priv, &
priv->ngd_mtx,
467 PDROP | PCATCH | (PZERO + 1),
473 while (m && uio->uio_resid > 0 && error == 0) {
474 len = MIN(uio->uio_resid, m->m_len);
476 error = uiomove(mtod(m,
void *), len, uio);
492ngdwrite(
struct cdev *dev,
struct uio *uio,
int flag)
494 struct epoch_tracker et;
501 if (uio->uio_resid == 0)
504 if (uio->uio_resid < 0 || uio->uio_resid > IP_MAXPACKET)
507 m = m_uiotombuf(uio, M_NOWAIT, 0,
priv->ether_align, M_PKTHDR);
523ngdpoll(
struct cdev *dev,
int events,
struct thread *td)
528 if (events & (POLLIN | POLLRDNORM) &&
529 !IFQ_IS_EMPTY(&
priv->readq))
530 revents |= events & (POLLIN | POLLRDNORM);
#define NG_HOOK_NODE(hook)
int ng_rcvmsg_t(node_p node, item_p item, hook_p lasthook)
int ng_disconnect_t(hook_p hook)
#define NG_NODE_SET_PRIVATE(node, val)
#define NG_RESPOND_MSG(error, here, item, resp)
#define NG_NODE_UNREF(node)
int ng_rmnode_self(node_p here)
#define NG_SEND_DATA_ONLY(error, hook, m)
int ng_rcvdata_t(hook_p hook, item_p item)
int ng_shutdown_t(node_p node)
#define NG_FREE_ITEM(item)
int ng_name_node(node_p node, const char *name)
int ng_constructor_t(node_p node)
#define NGI_GET_MSG(i, m)
#define NG_NODE_PRIVATE(node)
#define NG_SEND_MSG_HOOK(error, here, msg, hook, retaddr)
int ng_newhook_t(node_p node, hook_p hook, const char *name)
static struct ng_type ngd_typestruct
static ng_shutdown_t ng_device_shutdown
static ng_newhook_t ng_device_newhook
struct ngd_private * priv_p
static int ng_device_mod_event(module_t, int, void *)
static struct unrhdr * ngd_unit
static struct cdevsw ngd_cdevsw
static ng_rcvmsg_t ng_device_rcvmsg
static ng_disconnect_t ng_device_disconnect
static const struct ng_cmdlist ng_device_cmds[]
static d_close_t ngdclose
NETGRAPH_INIT(device, &ngd_typestruct)
static d_write_t ngdwrite
static ng_rcvdata_t ng_device_rcvdata
static ng_constructor_t ng_device_constructor
#define NGM_DEVICE_COOKIE
#define NG_DEVICE_NODE_TYPE
#define NG_DEVICE_DEVNAME
#define NG_MKRESPONSE(rsp, msg, len, how)
#define NG_MKMESSAGE(msg, cookie, cmdid, len, how)
const struct ng_parse_type ng_parse_string_type
struct ng_mesg::ng_msghdr header