2 * Copyright (c) 1999, 2002 Hellmuth Michaelis. All rights reserved.
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.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 /*---------------------------------------------------------------------------
28 * i4b_ing.c - isdn4bsd B-channel to netgraph driver
29 * -------------------------------------------------
30 * last edit-date: [Sat Mar 9 14:09:53 2002]
32 *---------------------------------------------------------------------------*/
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/errno.h>
45 #include <sys/malloc.h>
49 #include <netgraph/ng_message.h>
50 #include <netgraph/ng_parse.h>
51 #include <netgraph/netgraph.h>
53 #include <machine/i4b_ioctl.h>
54 #include <machine/i4b_debug.h>
56 #include <i4b/include/i4b_global.h>
57 #include <i4b/include/i4b_l3l4.h>
59 #include <i4b/layer4/i4b_l4.h>
61 #define I4BINGACCT 1 /* enable accounting messages */
62 #define I4BINGACCTINTVL 2 /* accounting msg interval in secs */
64 #define I4BINGMAXQLEN 50 /* max queue length */
66 /* initialized by L4 */
68 static drvr_link_t ing_drvr_linktab[NI4BING];
69 static isdn_link_t *isdn_linktab[NI4BING];
72 int sc_unit; /* unit number */
73 int sc_state; /* state of the interface */
74 call_desc_t *sc_cdp; /* ptr to call descriptor */
75 int sc_updown; /* soft state of interface */
76 struct ifqueue sc_fastq; /* interactive traffic */
77 int sc_dialresp; /* dialresponse */
78 int sc_lastdialresp;/* last dialresponse */
81 struct callout_handle sc_callout;
82 int sc_iinb; /* isdn driver # of inbytes */
83 int sc_ioutb; /* isdn driver # of outbytes */
84 int sc_inb; /* # of bytes rx'd */
85 int sc_outb; /* # of bytes tx'd */
86 int sc_linb; /* last # of bytes rx'd */
87 int sc_loutb; /* last # of bytes tx'd */
88 int sc_fn; /* flag, first null acct */
91 int sc_inpkt; /* incoming packets */
92 int sc_outpkt; /* outgoing packets */
94 struct ifqueue xmitq_hipri; /* hi-priority transmit queue */
95 struct ifqueue xmitq; /* transmit queue */
97 node_p node; /* back pointer to node */
98 char nodename[NG_NODESIZ]; /* store our node name */
102 u_int packets_in; /* packets in from downstream */
103 u_int packets_out; /* packets out towards downstream */
106 } ing_softc[NI4BING];
109 ST_IDLE, /* initialized, ready, idle */
110 ST_DIALING, /* dialling out to remote */
111 ST_CONNECTED /* connected to remote */
114 static void i4bingattach(void *);
116 PSEUDO_SET(i4bingattach, i4b_ing);
118 static void ing_init_linktab(int unit);
119 static void ing_tx_queue_empty(int unit);
121 /* ========= NETGRAPH ============= */
123 #define NG_ING_NODE_TYPE "i4bing" /* node type name */
124 #define NGM_ING_COOKIE 947513046 /* node type cookie */
127 #define NG_ING_HOOK_DEBUG "debug"
128 #define NG_ING_HOOK_RAW "rawdata"
130 /* Netgraph commands understood by this node type */
132 NGM_ING_SET_FLAG = 1,
136 /* This structure is returned by the NGM_ING_GET_STATUS command */
138 u_int packets_in; /* packets in from downstream */
139 u_int packets_out; /* packets out towards downstream */
143 * This is used to define the 'parse type' for a struct ngingstat, which
144 * is bascially a description of how to convert a binary struct ngingstat
145 * to an ASCII string and back. See ng_parse.h for more info.
147 * This needs to be kept in sync with the above structure definition
149 #define NG_ING_STATS_TYPE_INFO { \
150 { "packets_in", &ng_parse_int32_type }, \
151 { "packets_out", &ng_parse_int32_type }, \
156 * This section contains the netgraph method declarations for the
157 * sample node. These methods define the netgraph 'type'.
160 static ng_constructor_t ng_ing_constructor;
161 static ng_rcvmsg_t ng_ing_rcvmsg;
162 static ng_shutdown_t ng_ing_shutdown;
163 static ng_newhook_t ng_ing_newhook;
164 static ng_connect_t ng_ing_connect;
165 static ng_rcvdata_t ng_ing_rcvdata;
166 static ng_disconnect_t ng_ing_disconnect;
168 /* Parse type for struct ngingstat */
170 ng_parse_struct_field ng_ing_stat_type_fields[] =
171 NG_ING_STATS_TYPE_INFO;
173 static const struct ng_parse_type ng_ing_stat_type = {
174 &ng_parse_struct_type,
175 &ng_ing_stat_type_fields
178 /* List of commands and how to convert arguments to/from ASCII */
180 static const struct ng_cmdlist ng_ing_cmdlist[] = {
192 &ng_parse_int32_type,
198 /* Netgraph node type descriptor */
199 static struct ng_type typestruct = {
200 .version = NG_ABI_VERSION,
201 .name = NG_ING_NODE_TYPE,
202 .constructor = ng_ing_constructor,
203 .rcvmsg = ng_ing_rcvmsg,
204 .shutdown = ng_ing_shutdown,
205 .newhook = ng_ing_newhook,
206 .connect = ng_ing_connect,
207 .rcvdata = ng_ing_rcvdata,
208 .disconnect = ng_ing_disconnect,
209 .cmdlist = ng_ing_cmdlist,
212 NETGRAPH_INIT_ORDERED(ing, &typestruct, SI_SUB_DRIVERS, SI_ORDER_ANY);
214 /*===========================================================================*
215 * DEVICE DRIVER ROUTINES
216 *===========================================================================*/
218 /*---------------------------------------------------------------------------*
219 * interface attach routine at kernel boot time
220 *---------------------------------------------------------------------------*/
222 i4bingattach(void *dummy)
224 struct ing_softc *sc = ing_softc;
228 printf("i4bing: %d i4b NetGraph ISDN B-channel device(s) attached\n", NI4BING);
230 for(i=0; i < NI4BING; sc++, i++)
236 NDBGL4(L4_DIALST, "setting dial state to ST_IDLE");
238 sc->sc_state = ST_IDLE;
240 sc->sc_fastq.ifq_maxlen = I4BINGMAXQLEN;
241 if(!mtx_initialized(&sc->sc_fastq.ifq_mtx))
242 mtx_init(&sc->sc_fastq.ifq_mtx, "i4b_ing_fastq", NULL, MTX_DEF);
245 callout_handle_init(&sc->sc_callout);
258 sc->sc_updown = SOFT_ENA; /* soft enabled */
260 sc->sc_dialresp = DSTAT_NONE; /* no response */
261 sc->sc_lastdialresp = DSTAT_NONE;
263 /* setup a netgraph node */
265 if ((ret = ng_make_node_common(&typestruct, &sc->node)))
267 printf("ing: ng_make_node_common, ret = %d\n!", ret);
270 /* name the netgraph node */
272 sprintf(sc->nodename, "%s%d", NG_ING_NODE_TYPE, sc->sc_unit);
273 if((ret = ng_name_node(sc->node, sc->nodename)))
275 printf("ing: ng_name node, ret = %d\n!", ret);
276 NG_NODE_UNREF(sc->node);
280 NG_NODE_SET_PRIVATE(sc->node, sc);
282 sc->xmitq.ifq_maxlen = IFQ_MAXLEN;
283 sc->xmitq_hipri.ifq_maxlen = IFQ_MAXLEN;
284 if(!mtx_initialized(&sc->xmitq.ifq_mtx))
285 mtx_init(&sc->xmitq.ifq_mtx, "i4b_ing_xmitq", NULL, MTX_DEF);
286 if(!mtx_initialized(&sc->xmitq_hipri.ifq_mtx))
287 mtx_init(&sc->xmitq_hipri.ifq_mtx, "i4b_ing_hipri", NULL, MTX_DEF);
292 /*---------------------------------------------------------------------------*
293 * accounting timeout routine
294 *---------------------------------------------------------------------------*/
296 ing_timeout(struct ing_softc *sc)
298 bchan_statistics_t bs;
299 int unit = sc->sc_unit;
301 /* get # of bytes in and out from the HSCX driver */
303 (*isdn_linktab[unit]->bch_stat)
304 (isdn_linktab[unit]->unit, isdn_linktab[unit]->channel, &bs);
306 sc->sc_ioutb += bs.outbytes;
307 sc->sc_iinb += bs.inbytes;
309 if((sc->sc_iinb != sc->sc_linb) || (sc->sc_ioutb != sc->sc_loutb) || sc->sc_fn)
311 int ri = (sc->sc_iinb - sc->sc_linb)/I4BINGACCTINTVL;
312 int ro = (sc->sc_ioutb - sc->sc_loutb)/I4BINGACCTINTVL;
314 if((sc->sc_iinb == sc->sc_linb) && (sc->sc_ioutb == sc->sc_loutb))
319 sc->sc_linb = sc->sc_iinb;
320 sc->sc_loutb = sc->sc_ioutb;
322 i4b_l4_accounting(BDRV_ING, unit, ACCT_DURING,
323 sc->sc_ioutb, sc->sc_iinb, ro, ri, sc->sc_ioutb, sc->sc_iinb);
326 sc->sc_callout = timeout((TIMEOUT_FUNC_T)ing_timeout,
327 (void *)sc, I4BINGACCTINTVL*hz);
329 #endif /* I4BINGACCT */
332 /*---------------------------------------------------------------------------*
333 * clear the interface's send queues
334 *---------------------------------------------------------------------------*/
336 ingclearqueue(struct ifqueue *iq)
346 /*===========================================================================*
347 * ISDN INTERFACE ROUTINES
348 *===========================================================================*/
350 /*---------------------------------------------------------------------------*
351 * this routine is called from L4 handler at connect time
352 *---------------------------------------------------------------------------*/
354 ing_connect(int unit, void *cdp)
356 struct ing_softc *sc = &ing_softc[unit];
359 sc->sc_cdp = (call_desc_t *)cdp;
363 NDBGL4(L4_DIALST, "ing%d: setting dial state to ST_CONNECTED", unit);
365 sc->sc_dialresp = DSTAT_NONE;
366 sc->sc_lastdialresp = DSTAT_NONE;
375 sc->sc_callout = timeout((TIMEOUT_FUNC_T)ing_timeout,
376 (void *)sc, I4BINGACCTINTVL*hz);
379 sc->sc_state = ST_CONNECTED;
384 /*---------------------------------------------------------------------------*
385 * this routine is called from L4 handler at disconnect time
386 *---------------------------------------------------------------------------*/
388 ing_disconnect(int unit, void *cdp)
390 call_desc_t *cd = (call_desc_t *)cdp;
391 struct ing_softc *sc = &ing_softc[unit];
393 /* new stuff to check that the active channel is being closed */
395 if (cd != sc->sc_cdp)
397 NDBGL4(L4_INGDBG, "ing%d: channel %d not active",
398 cd->driver_unit, cd->channelid);
403 untimeout((TIMEOUT_FUNC_T)ing_timeout,
404 (void *)sc, sc->sc_callout);
407 i4b_l4_accounting(BDRV_ING, cd->driver_unit, ACCT_FINAL,
408 sc->sc_ioutb, sc->sc_iinb, 0, 0, sc->sc_outb, sc->sc_inb);
410 sc->sc_cdp = (call_desc_t *)0;
412 NDBGL4(L4_DIALST, "setting dial state to ST_IDLE");
414 sc->sc_dialresp = DSTAT_NONE;
415 sc->sc_lastdialresp = DSTAT_NONE;
417 sc->sc_state = ST_IDLE;
420 /*---------------------------------------------------------------------------*
421 * this routine is used to give a feedback from userland daemon
422 * in case of dial problems
423 *---------------------------------------------------------------------------*/
425 ing_dialresponse(int unit, int status, cause_t cause)
427 struct ing_softc *sc = &ing_softc[unit];
428 sc->sc_dialresp = status;
430 NDBGL4(L4_INGDBG, "ing%d: last=%d, this=%d",
431 unit, sc->sc_lastdialresp, sc->sc_dialresp);
433 if(status != DSTAT_NONE)
435 NDBGL4(L4_INGDBG, "ing%d: clearing queues", unit);
436 /* ingclearqueues(sc); */
440 /*---------------------------------------------------------------------------*
441 * interface soft up/down
442 *---------------------------------------------------------------------------*/
444 ing_updown(int unit, int updown)
446 struct ing_softc *sc = &ing_softc[unit];
447 sc->sc_updown = updown;
450 /*---------------------------------------------------------------------------*
451 * this routine is called from the HSCX interrupt handler
452 * when a new frame (mbuf) has been received and was put on
453 * the rx queue. It is assumed that this routines runs at
454 * pri level splimp() ! Keep it short !
455 *---------------------------------------------------------------------------*/
457 ing_rx_data_rdy(int unit)
459 register struct ing_softc *sc = &ing_softc[unit];
460 register struct mbuf *m;
463 if((m = *isdn_linktab[unit]->rx_mbuf) == NULL)
467 sc->sc_inb += m->m_pkthdr.len;
470 m->m_pkthdr.rcvif = NULL;
474 NG_SEND_DATA_ONLY(error, sc->hook, m);
477 /*---------------------------------------------------------------------------*
478 * this routine is called from the HSCX interrupt handler
479 * when the last frame has been sent out and there is no
480 * further frame (mbuf) in the tx queue.
481 *---------------------------------------------------------------------------*/
483 ing_tx_queue_empty(int unit)
485 register struct ing_softc *sc = &ing_softc[unit];
486 register struct mbuf *m;
489 if(sc->sc_state != ST_CONNECTED)
494 IF_DEQUEUE(&sc->xmitq_hipri, m);
498 IF_DEQUEUE(&sc->xmitq, m);
504 sc->sc_outb += m->m_pkthdr.len;
509 if(! IF_HANDOFF(isdn_linktab[unit]->tx_queue, m, NULL))
511 NDBGL4(L4_INGDBG, "ing%d: tx queue full!", unit);
516 (*isdn_linktab[unit]->bch_tx_start)(isdn_linktab[unit]->unit, isdn_linktab[unit]->channel);
519 /*---------------------------------------------------------------------------*
520 * this routine is called from the HSCX interrupt handler
521 * each time a packet is received or transmitted. It should
522 * be used to implement an activity timeout mechanism.
523 *---------------------------------------------------------------------------*/
525 ing_activity(int unit, int rxtx)
527 ing_softc[unit].sc_cdp->last_active_time = SECOND;
530 /*---------------------------------------------------------------------------*
531 * return this drivers linktab address
532 *---------------------------------------------------------------------------*/
534 ing_ret_linktab(int unit)
536 return(&ing_drvr_linktab[unit]);
539 /*---------------------------------------------------------------------------*
540 * setup the isdn_linktab for this driver
541 *---------------------------------------------------------------------------*/
543 ing_set_linktab(int unit, isdn_link_t *ilt)
545 isdn_linktab[unit] = ilt;
548 /*---------------------------------------------------------------------------*
549 * initialize this drivers linktab
550 *---------------------------------------------------------------------------*/
552 ing_init_linktab(int unit)
554 ing_drvr_linktab[unit].unit = unit;
555 ing_drvr_linktab[unit].bch_rx_data_ready = ing_rx_data_rdy;
556 ing_drvr_linktab[unit].bch_tx_queue_empty = ing_tx_queue_empty;
557 ing_drvr_linktab[unit].bch_activity = ing_activity;
558 ing_drvr_linktab[unit].line_connected = ing_connect;
559 ing_drvr_linktab[unit].line_disconnected = ing_disconnect;
560 ing_drvr_linktab[unit].dial_response = ing_dialresponse;
561 ing_drvr_linktab[unit].updown_ind = ing_updown;
564 /*===========================================================================*
565 * NETGRAPH INTERFACE ROUTINES
566 *===========================================================================*/
568 /*---------------------------------------------------------------------------*
569 * It is not possible or allowable to create a node of this type.
570 * If the hardware exists, it will already have created it.
571 *---------------------------------------------------------------------------*/
573 ng_ing_constructor(node_p node)
578 /*---------------------------------------------------------------------------*
579 * Give our ok for a hook to be added...
580 * Add the hook's private info to the hook structure.
581 *---------------------------------------------------------------------------*/
583 ng_ing_newhook(node_p node, hook_p hook, const char *name)
585 struct ing_softc *sc = NG_NODE_PRIVATE(node);
588 * check if it's our friend the debug hook
590 if(strcmp(name, NG_ING_HOOK_DEBUG) == 0)
592 NG_HOOK_SET_PRIVATE(hook, NULL); /* paranoid */
593 sc->debughook = hook;
597 * Check for raw mode hook.
599 if(strcmp(name, NG_ING_HOOK_RAW) == 0)
601 NG_HOOK_SET_PRIVATE(hook, sc);
609 /*---------------------------------------------------------------------------*
610 * Get a netgraph control message.
611 * Check it is one we understand. If needed, send a response.
612 * We could save the address for an async action later, but don't here.
613 * Always free the message.
614 * The response should be in a malloc'd region that the caller can 'free'.
615 * A response is not required.
616 *---------------------------------------------------------------------------*/
618 ng_ing_rcvmsg(node_p node, item_p item, hook_p lasthook)
620 struct ing_softc *sc = NG_NODE_PRIVATE(node);
622 struct ng_mesg *resp = NULL;
626 NGI_GET_MSG(item, msg);
628 if(msg->header.typecookie == NGM_GENERIC_COOKIE)
630 switch(msg->header.cmd)
632 case NGM_TEXT_STATUS:
638 NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg) + NG_TEXTRESPONSE, M_NOWAIT);
645 arg = (char *) resp->data;
663 pos = sprintf(arg, "state = %s (%d)\n", p, sc->sc_state);
665 pos += sprintf(arg + pos, "%d bytes in, %d bytes out\n", sc->sc_inb, sc->sc_outb);
667 pos += sprintf(arg + pos, "%d pkts in, %d pkts out\n", sc->sc_inpkt, sc->sc_outpkt);
669 resp->header.arglen = pos + 1;
678 else if(msg->header.typecookie == NGM_ING_COOKIE)
680 switch (msg->header.cmd)
682 case NGM_ING_GET_STATUS:
684 struct ngingstat *stats;
686 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
694 stats = (struct ngingstat *) resp->data;
695 stats->packets_in = sc->packets_in;
696 stats->packets_out = sc->packets_out;
700 case NGM_ING_SET_FLAG:
701 if (msg->header.arglen != sizeof(u_int32_t))
706 sc->flags = *((u_int32_t *) msg->data);
710 error = EINVAL; /* unknown command */
716 error = EINVAL; /* unknown cookie type */
719 /* Take care of synchronous response, if any */
720 NG_RESPOND_MSG(error, node, item, resp);
721 /* Free the message and return */
726 /*---------------------------------------------------------------------------*
727 * get data from another node and transmit it out on a B-channel
728 *---------------------------------------------------------------------------*/
730 ng_ing_rcvdata(hook_p hook, item_p item)
732 struct ing_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
733 struct ifqueue *xmitq_p;
736 struct ng_tag_prio *ptag;
741 if(NG_HOOK_PRIVATE(hook) == NULL)
747 if(sc->sc_state == ST_IDLE || sc->sc_state == ST_DIALING)
749 i4b_l4_dialout(BDRV_ING, sc->sc_unit);
750 sc->sc_state = ST_DIALING;
756 * Now queue the data for when it can be sent
758 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
759 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
760 xmitq_p = (&sc->xmitq_hipri);
762 xmitq_p = (&sc->xmitq);
767 if (_IF_QFULL(xmitq_p))
776 _IF_ENQUEUE(xmitq_p, m);
779 ing_tx_queue_empty(sc->sc_unit);
785 /*---------------------------------------------------------------------------*
786 * Do local shutdown processing..
787 * If we are a persistant device, we might refuse to go away, and
788 * we'd only remove our links and reset ourself.
789 *---------------------------------------------------------------------------*/
791 ng_ing_shutdown(node_p node)
793 struct ing_softc *sc = NG_NODE_PRIVATE(node);
798 sc->packets_in = 0; /* reset stats */
801 if ((ret = ng_make_node_common(&typestruct, &sc->node)))
803 printf("ing: ng_make_node_common, ret = %d\n!", ret);
806 /* name the netgraph node */
807 sprintf(sc->nodename, "%s%d", NG_ING_NODE_TYPE, sc->sc_unit);
808 if((ret = ng_name_node(sc->node, sc->nodename)))
810 printf("ing: ng_name node, ret = %d\n!", ret);
811 NG_NODE_UNREF(sc->node);
815 NG_NODE_SET_PRIVATE(sc->node, sc);
820 /*---------------------------------------------------------------------------*
821 * This is called once we've already connected a new hook to the other node.
822 *---------------------------------------------------------------------------*/
824 ng_ing_connect(hook_p hook)
826 /* probably not at splnet, force outward queueing */
827 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
834 * For this type, removal of the last link destroys the node
837 ng_ing_disconnect(hook_p hook)
839 struct ing_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
842 if(NG_HOOK_PRIVATE(hook))
849 sc->debughook = NULL;
854 /*===========================================================================*/