2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Author: Hartmut Brandt <harti@freebsd.org>
29 * $FreeBSD: src/sys/netgraph/atm/ng_atm.c,v 1.15 2005/08/10 06:25:40 obrien Exp $
33 * Netgraph module to connect NATM interfaces to netgraph.
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
40 #include <sys/errno.h>
41 #include <sys/syslog.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
45 #include <sys/sysctl.h>
48 #include <net/if_types.h>
49 #include <net/if_arp.h>
50 #include <net/if_var.h>
51 #include <net/if_media.h>
52 #include <net/if_atm.h>
54 #include "ng_message.h"
57 #include "atm/ng_atm.h"
60 * Hooks in the NATM code
62 extern void (*ng_atm_attach_p)(struct ifnet *);
63 extern void (*ng_atm_detach_p)(struct ifnet *);
64 extern int (*ng_atm_output_p)(struct ifnet *, struct mbuf **);
65 extern void (*ng_atm_input_p)(struct ifnet *, struct mbuf **,
66 struct atm_pseudohdr *, void *);
67 extern void (*ng_atm_input_orphan_p)(struct ifnet *, struct mbuf *,
68 struct atm_pseudohdr *, void *);
69 extern void (*ng_atm_event_p)(struct ifnet *, uint32_t, void *);
74 SYSCTL_NODE(_net_graph, OID_AUTO, atm, CTLFLAG_RW, 0, "atm related stuff");
77 static int allow_shutdown;
79 SYSCTL_INT(_net_graph_atm, OID_AUTO, allow_shutdown, CTLFLAG_RW,
80 &allow_shutdown, 0, "allow ng_atm nodes to shutdown");
87 uint16_t vpi; /* VPI of this hook */
88 uint16_t vci; /* VCI of this hook, 0 if none */
89 uint32_t flags; /* private flags */
90 hook_p hook; /* the connected hook */
92 LIST_ENTRY(ngvcc) link;
94 #define VCC_OPEN 0x0001 /* open */
100 struct ifnet *ifp; /* the ATM interface */
101 hook_p input; /* raw input hook */
102 hook_p orphans; /* packets to nowhere */
103 hook_p output; /* catch output packets */
104 hook_p manage; /* has also entry in vccs */
107 uint64_t out_packets;
110 LIST_HEAD(, ngvcc) vccs;
114 * Parse ifstate state
116 static const struct ng_parse_struct_field ng_atm_if_change_info[] =
117 NGM_ATM_IF_CHANGE_INFO;
118 static const struct ng_parse_type ng_atm_if_change_type = {
119 &ng_parse_struct_type,
120 &ng_atm_if_change_info
124 * Parse vcc state change
126 static const struct ng_parse_struct_field ng_atm_vcc_change_info[] =
127 NGM_ATM_VCC_CHANGE_INFO;
128 static const struct ng_parse_type ng_atm_vcc_change_type = {
129 &ng_parse_struct_type,
130 &ng_atm_vcc_change_info
136 static const struct ng_parse_struct_field ng_atm_acr_change_info[] =
137 NGM_ATM_ACR_CHANGE_INFO;
138 static const struct ng_parse_type ng_atm_acr_change_type = {
139 &ng_parse_struct_type,
140 &ng_atm_acr_change_info
144 * Parse the configuration structure ng_atm_config
146 static const struct ng_parse_struct_field ng_atm_config_type_info[] =
149 static const struct ng_parse_type ng_atm_config_type = {
150 &ng_parse_struct_type,
151 &ng_atm_config_type_info
155 * Parse a single vcc structure and a variable array of these ng_atm_vccs
157 static const struct ng_parse_struct_field ng_atm_tparam_type_info[] =
159 static const struct ng_parse_type ng_atm_tparam_type = {
160 &ng_parse_struct_type,
161 &ng_atm_tparam_type_info
163 static const struct ng_parse_struct_field ng_atm_vcc_type_info[] =
165 static const struct ng_parse_type ng_atm_vcc_type = {
166 &ng_parse_struct_type,
167 &ng_atm_vcc_type_info
172 ng_atm_vccarray_getlen(const struct ng_parse_type *type,
173 const u_char *start, const u_char *buf)
175 const struct atmio_vcctable *vp;
177 vp = (const struct atmio_vcctable *)
178 (buf - offsetof(struct atmio_vcctable, vccs));
182 static const struct ng_parse_array_info ng_atm_vccarray_info =
183 NGM_ATM_VCCARRAY_INFO;
184 static const struct ng_parse_type ng_atm_vccarray_type = {
185 &ng_parse_array_type,
186 &ng_atm_vccarray_info
190 static const struct ng_parse_struct_field ng_atm_vcctable_type_info[] =
191 NGM_ATM_VCCTABLE_INFO;
193 static const struct ng_parse_type ng_atm_vcctable_type = {
194 &ng_parse_struct_type,
195 &ng_atm_vcctable_type_info
199 * Parse CPCS INIT structure ng_atm_cpcs_init
201 static const struct ng_parse_struct_field ng_atm_cpcs_init_type_info[] =
202 NGM_ATM_CPCS_INIT_INFO;
204 static const struct ng_parse_type ng_atm_cpcs_init_type = {
205 &ng_parse_struct_type,
206 &ng_atm_cpcs_init_type_info
210 * Parse CPCS TERM structure ng_atm_cpcs_term
212 static const struct ng_parse_struct_field ng_atm_cpcs_term_type_info[] =
213 NGM_ATM_CPCS_TERM_INFO;
215 static const struct ng_parse_type ng_atm_cpcs_term_type = {
216 &ng_parse_struct_type,
217 &ng_atm_cpcs_term_type_info
221 * Parse statistic struct
223 static const struct ng_parse_struct_field ng_atm_stats_type_info[] =
226 static const struct ng_parse_type ng_atm_stats_type = {
227 &ng_parse_struct_type,
228 &ng_atm_stats_type_info
231 static const struct ng_cmdlist ng_atm_cmdlist[] = {
237 &ng_parse_string_type
251 &ng_atm_vcctable_type
257 &ng_atm_cpcs_init_type,
264 &ng_atm_cpcs_term_type,
271 &ng_parse_hookbuf_type,
294 &ng_atm_if_change_type,
295 &ng_atm_if_change_type,
301 &ng_atm_vcc_change_type,
302 &ng_atm_vcc_change_type,
308 &ng_atm_acr_change_type,
309 &ng_atm_acr_change_type,
314 static int ng_atm_mod_event(module_t, int, void *);
316 static ng_constructor_t ng_atm_constructor;
317 static ng_shutdown_t ng_atm_shutdown;
318 static ng_rcvmsg_t ng_atm_rcvmsg;
319 static ng_newhook_t ng_atm_newhook;
320 static ng_connect_t ng_atm_connect;
321 static ng_disconnect_t ng_atm_disconnect;
322 static ng_rcvdata_t ng_atm_rcvdata;
323 static ng_rcvdata_t ng_atm_rcvdrop;
325 static struct ng_type ng_atm_typestruct = {
326 .version = NG_ABI_VERSION,
327 .name = NG_ATM_NODE_TYPE,
328 .mod_event = ng_atm_mod_event,
329 .constructor = ng_atm_constructor,
330 .rcvmsg = ng_atm_rcvmsg,
331 .shutdown = ng_atm_shutdown,
332 .newhook = ng_atm_newhook,
333 .connect = ng_atm_connect,
334 .rcvdata = ng_atm_rcvdata,
335 .disconnect = ng_atm_disconnect,
336 .cmdlist = ng_atm_cmdlist,
338 NETGRAPH_INIT(atm, &ng_atm_typestruct);
340 static const struct {
343 } atmmedia[] = IFM_SUBTYPE_ATM_DESCRIPTIONS;
346 #define IFP2NG(IFP) ((node_p)((struct ifatm *)(IFP)->if_softc)->ngpriv)
347 #define IFP2NG_SET(IFP, val) (((struct ifatm *)(IFP)->if_softc)->ngpriv = (val))
349 #define IFFLAGS "\020\001UP\002BROADCAST\003DEBUG\004LOOPBACK" \
350 "\005POINTOPOINT\006SMART\007RUNNING\010NOARP" \
351 "\011PROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX" \
352 "\015LINK0\016LINK1\017LINK2\020MULTICAST"
355 /************************************************************/
360 * A packet is received from an interface.
361 * If we have an input hook, prepend the pseudoheader to the data and
362 * deliver it out to that hook. If not, look whether it is destined for
363 * use. If so locate the appropriate hook, deliver the packet without the
364 * header and we are done. If it is not for us, leave it alone.
367 ng_atm_input(struct ifnet *ifp, struct mbuf **mp,
368 struct atm_pseudohdr *ah, void *rxhand)
370 node_p node = IFP2NG(ifp);
372 const struct ngvcc *vcc;
377 priv = NG_NODE_PRIVATE(node);
378 if (priv->input != NULL) {
380 * Prepend the atm_pseudoheader.
382 M_PREPEND(*mp, sizeof(*ah), MB_DONTWAIT);
385 memcpy(mtod(*mp, struct atm_pseudohdr *), ah, sizeof(*ah));
386 NG_SEND_DATA_ONLY(error, priv->input, *mp);
392 printf("%s: error=%d\n", __func__, error);
398 if ((ATM_PH_FLAGS(ah) & ATMIO_FLAG_NG) == 0)
401 vcc = (struct ngvcc *)rxhand;
403 NG_SEND_DATA_ONLY(error, vcc->hook, *mp);
409 printf("%s: error=%d\n", __func__, error);
416 * ATM packet is about to be output. The atm_pseudohdr is already prepended.
417 * If the hook is set, reroute the packet to the hook.
420 ng_atm_output(struct ifnet *ifp, struct mbuf **mp)
422 const node_p node = IFP2NG(ifp);
423 const struct priv *priv;
428 priv = NG_NODE_PRIVATE(node);
430 NG_SEND_DATA_ONLY(error, priv->output, *mp);
438 * Well, this doesn't make much sense for ATM.
441 ng_atm_input_orphans(struct ifnet *ifp, struct mbuf *m,
442 struct atm_pseudohdr *ah, void *rxhand)
444 node_p node = IFP2NG(ifp);
452 priv = NG_NODE_PRIVATE(node);
453 if (priv->orphans == NULL) {
458 * Prepend the atm_pseudoheader.
460 M_PREPEND(m, sizeof(*ah), MB_DONTWAIT);
463 memcpy(mtod(m, struct atm_pseudohdr *), ah, sizeof(*ah));
464 NG_SEND_DATA_ONLY(error, priv->orphans, m);
470 printf("%s: error=%d\n", __func__, error);
475 /************************************************************/
480 ng_atm_rcvdata(hook_p hook, item_p item)
482 node_p node = NG_HOOK_NODE(hook);
483 struct priv *priv = NG_NODE_PRIVATE(node);
484 const struct ngvcc *vcc = NG_HOOK_PRIVATE(hook);
486 struct atm_pseudohdr *aph;
498 * Prepend pseudo-hdr. Drivers don't care about the flags.
500 M_PREPEND(m, sizeof(*aph), MB_DONTWAIT);
505 aph = mtod(m, struct atm_pseudohdr *);
506 ATM_PH_VPI(aph) = vcc->vpi;
507 ATM_PH_SETVCI(aph, vcc->vci);
508 ATM_PH_FLAGS(aph) = 0;
510 if ((error = atm_output(priv->ifp, m, NULL, NULL)) == 0)
518 ng_atm_rcvdrop(hook_p hook, item_p item)
525 /************************************************************
530 ng_atm_event_func(node_p node, hook_p hook, void *arg, int event)
532 const struct priv *priv = NG_NODE_PRIVATE(node);
534 struct ng_mesg *mesg;
539 case ATMEV_FLOW_CONTROL:
541 struct atmev_flow_control *ev = arg;
543 /* find the connection */
544 LIST_FOREACH(vcc, &priv->vccs, link)
545 if (vcc->vci == ev->vci && vcc->vpi == ev->vpi)
550 /* convert into a flow control message */
551 NG_MKMESSAGE(mesg, NGM_FLOW_COOKIE,
552 ev->busy ? NGM_HIGH_WATER_PASSED : NGM_LOW_WATER_PASSED,
553 sizeof(struct ngm_queue_state), M_WAITOK | M_NULLOK);
557 /* XXX have to figure out how to get that info */
559 NG_SEND_MSG_HOOK(error, node, mesg, vcc->hook, 0);
563 case ATMEV_VCC_CHANGED:
565 struct atmev_vcc_changed *ev = arg;
566 struct ngm_atm_vcc_change *chg;
568 if (priv->manage == NULL)
570 NG_MKMESSAGE(mesg, NGM_ATM_COOKIE, NGM_ATM_VCC_CHANGE,
571 sizeof(struct ngm_atm_vcc_change), M_WAITOK | M_NULLOK);
574 chg = (struct ngm_atm_vcc_change *)mesg->data;
577 chg->state = (ev->up != 0);
578 chg->node = NG_NODE_ID(node);
579 NG_SEND_MSG_HOOK(error, node, mesg, priv->manage, 0);
583 case ATMEV_IFSTATE_CHANGED:
585 struct atmev_ifstate_changed *ev = arg;
586 struct ngm_atm_if_change *chg;
588 if (priv->manage == NULL)
590 NG_MKMESSAGE(mesg, NGM_ATM_COOKIE, NGM_ATM_IF_CHANGE,
591 sizeof(struct ngm_atm_if_change), M_WAITOK | M_NULLOK);
594 chg = (struct ngm_atm_if_change *)mesg->data;
595 chg->carrier = (ev->carrier != 0);
596 chg->running = (ev->running != 0);
597 chg->node = NG_NODE_ID(node);
598 NG_SEND_MSG_HOOK(error, node, mesg, priv->manage, 0);
602 case ATMEV_ACR_CHANGED:
604 struct atmev_acr_changed *ev = arg;
605 struct ngm_atm_acr_change *acr;
607 /* find the connection */
608 LIST_FOREACH(vcc, &priv->vccs, link)
609 if (vcc->vci == ev->vci && vcc->vpi == ev->vpi)
614 /* convert into a flow control message */
615 NG_MKMESSAGE(mesg, NGM_ATM_COOKIE, NGM_ATM_ACR_CHANGE,
616 sizeof(struct ngm_atm_acr_change), M_WAITOK | M_NULLOK);
619 acr = (struct ngm_atm_acr_change *)mesg->data;
620 acr->node = NG_NODE_ID(node);
625 NG_SEND_MSG_HOOK(error, node, mesg, vcc->hook, 0);
632 * Use send_fn to get the right lock
635 ng_atm_event(struct ifnet *ifp, uint32_t event, void *arg)
637 const node_p node = IFP2NG(ifp);
640 /* may happen during attach/detach */
641 (void)ng_send_fn(node, NULL, ng_atm_event_func, arg, event);
644 /************************************************************
649 * Open a channel for the user
652 ng_atm_cpcs_init(node_p node, const struct ngm_atm_cpcs_init *arg)
654 struct priv *priv = NG_NODE_PRIVATE(node);
656 struct atmio_openvcc data;
659 if(priv->ifp->if_ioctl == NULL)
662 LIST_FOREACH(vcc, &priv->vccs, link)
663 if (strcmp(arg->name, NG_HOOK_NAME(vcc->hook)) == 0)
667 if (vcc->flags & VCC_OPEN)
671 * Check user arguments and construct ioctl argument
673 memset(&data, 0, sizeof(data));
677 switch (data.param.aal = arg->aal) {
691 data.param.vpi = arg->vpi;
693 /* allow 0.0 as catch all receive channel */
694 if (arg->vci == 0 && (arg->vpi != 0 || !(arg->flags & ATMIO_FLAG_NOTX)))
696 data.param.vci = arg->vci;
698 data.param.tparam.pcr = arg->pcr;
700 if (arg->mcr > arg->pcr)
702 data.param.tparam.mcr = arg->mcr;
704 if (!(arg->flags & ATMIO_FLAG_NOTX)) {
706 data.param.tmtu = priv->ifp->if_mtu;
708 data.param.tmtu = arg->tmtu;
711 if (!(arg->flags & ATMIO_FLAG_NORX)) {
713 data.param.rmtu = priv->ifp->if_mtu;
715 data.param.rmtu = arg->rmtu;
719 switch (data.param.traffic = arg->traffic) {
721 case ATMIO_TRAFFIC_UBR:
722 case ATMIO_TRAFFIC_CBR:
725 case ATMIO_TRAFFIC_VBR:
726 if (arg->scr > arg->pcr)
728 data.param.tparam.scr = arg->scr;
730 if (arg->mbs > (1 << 24))
732 data.param.tparam.mbs = arg->mbs;
735 case ATMIO_TRAFFIC_ABR:
736 if (arg->icr > arg->pcr || arg->icr < arg->mcr)
738 data.param.tparam.icr = arg->icr;
740 if (arg->tbe == 0 || arg->tbe > (1 << 24))
742 data.param.tparam.tbe = arg->tbe;
746 data.param.tparam.nrm = arg->nrm;
750 data.param.tparam.trm = arg->trm;
752 if (arg->adtf > 0x3ff)
754 data.param.tparam.adtf = arg->adtf;
758 data.param.tparam.rif = arg->rif;
762 data.param.tparam.rdf = arg->rdf;
766 data.param.tparam.cdf = arg->cdf;
774 if ((arg->flags & ATMIO_FLAG_NORX) && (arg->flags & ATMIO_FLAG_NOTX))
777 data.param.flags = arg->flags & ~(ATM_PH_AAL5 | ATM_PH_LLCSNAP);
778 data.param.flags |= ATMIO_FLAG_NG;
780 err = (*priv->ifp->if_ioctl)(priv->ifp, SIOCATMOPENVCC, (caddr_t)&data);
783 vcc->vci = data.param.vci;
784 vcc->vpi = data.param.vpi;
785 vcc->flags = VCC_OPEN;
792 * Issue the close command to the driver
795 cpcs_term(const struct priv *priv, u_int vpi, u_int vci)
797 struct atmio_closevcc data;
799 if (priv->ifp->if_ioctl == NULL)
805 return ((*priv->ifp->if_ioctl)(priv->ifp,
806 SIOCATMCLOSEVCC, (caddr_t)&data));
811 * Close a channel by request of the user
814 ng_atm_cpcs_term(node_p node, const struct ngm_atm_cpcs_term *arg)
816 struct priv *priv = NG_NODE_PRIVATE(node);
820 LIST_FOREACH(vcc, &priv->vccs, link)
821 if(strcmp(arg->name, NG_HOOK_NAME(vcc->hook)) == 0)
825 if (!(vcc->flags & VCC_OPEN))
828 error = cpcs_term(priv, vcc->vpi, vcc->vci);
837 /************************************************************/
843 * Produce a textual description of the current status
846 text_status(node_p node, char *arg, u_int len)
848 const struct priv *priv = NG_NODE_PRIVATE(node);
849 const struct ifatm_mib *mib;
853 static const struct {
860 mib = (const struct ifatm_mib *)(priv->ifp->if_linkmib);
862 sbuf_new(&sbuf, arg, len, SBUF_FIXEDLEN);
863 sbuf_printf(&sbuf, "interface: %s\n", priv->ifp->if_xname);
865 if (mib->device >= NELEM(devices))
866 sbuf_printf(&sbuf, "device=unknown\nvendor=unknown\n");
868 sbuf_printf(&sbuf, "device=%s\nvendor=%s\n",
869 devices[mib->device].name, devices[mib->device].vendor);
871 for (i = 0; atmmedia[i].name; i++)
872 if(mib->media == atmmedia[i].media) {
873 sbuf_printf(&sbuf, "media=%s\n", atmmedia[i].name);
876 if(atmmedia[i].name == NULL)
877 sbuf_printf(&sbuf, "media=unknown\n");
879 sbuf_printf(&sbuf, "serial=%u esi=%s hardware=%u software=%u\n",
880 mib->serial, ether_sprintf(mib->esi), mib->hw_version, mib->sw_version);
881 sbuf_printf(&sbuf, "pcr=%u vpi_bits=%u vci_bits=%u max_vpcs=%u "
882 "max_vccs=%u\n", mib->pcr, mib->vpi_bits, mib->vci_bits,
883 mib->max_vpcs, mib->max_vccs);
884 sbuf_printf(&sbuf, "ifflags=%b\n", priv->ifp->if_flags, IFFLAGS);
888 return (sbuf_len(&sbuf));
892 * Get control message
895 ng_atm_rcvmsg(node_p node, item_p item, hook_p lasthook)
897 const struct priv *priv = NG_NODE_PRIVATE(node);
898 struct ng_mesg *resp = NULL;
900 struct ifatm_mib *mib = (struct ifatm_mib *)(priv->ifp->if_linkmib);
903 NGI_GET_MSG(item, msg);
905 switch (msg->header.typecookie) {
907 case NGM_GENERIC_COOKIE:
908 switch (msg->header.cmd) {
910 case NGM_TEXT_STATUS:
911 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_WAITOK | M_NULLOK);
917 resp->header.arglen = text_status(node,
918 (char *)resp->data, resp->header.arglen) + 1;
928 switch (msg->header.cmd) {
930 case NGM_ATM_GET_IFNAME:
931 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_WAITOK | M_NULLOK);
936 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
939 case NGM_ATM_GET_CONFIG:
941 struct ngm_atm_config *config;
943 NG_MKRESPONSE(resp, msg, sizeof(*config), M_WAITOK | M_NULLOK);
948 config = (struct ngm_atm_config *)resp->data;
949 config->pcr = mib->pcr;
950 config->vpi_bits = mib->vpi_bits;
951 config->vci_bits = mib->vci_bits;
952 config->max_vpcs = mib->max_vpcs;
953 config->max_vccs = mib->max_vccs;
957 case NGM_ATM_GET_VCCS:
959 struct atmio_vcctable *vccs;
962 if (priv->ifp->if_ioctl == NULL) {
966 error = (*priv->ifp->if_ioctl)(priv->ifp,
967 SIOCATMGETVCCS, (caddr_t)&vccs);
971 len = sizeof(*vccs) +
972 vccs->count * sizeof(vccs->vccs[0]);
973 NG_MKRESPONSE(resp, msg, len, M_WAITOK | M_NULLOK);
976 kfree(vccs, M_DEVBUF);
980 (void)memcpy(resp->data, vccs, len);
981 kfree(vccs, M_DEVBUF);
986 case NGM_ATM_GET_VCC:
988 char hook[NG_HOOKSIZ];
989 struct atmio_vcctable *vccs;
993 if (priv->ifp->if_ioctl == NULL) {
997 if (msg->header.arglen != NG_HOOKSIZ) {
1001 strncpy(hook, msg->data, NG_HOOKSIZ);
1002 hook[NG_HOOKSIZ - 1] = '\0';
1003 LIST_FOREACH(vcc, &priv->vccs, link)
1004 if (strcmp(NG_HOOK_NAME(vcc->hook), hook) == 0)
1010 error = (*priv->ifp->if_ioctl)(priv->ifp,
1011 SIOCATMGETVCCS, (caddr_t)&vccs);
1015 for (i = 0; i < vccs->count; i++)
1016 if (vccs->vccs[i].vpi == vcc->vpi &&
1017 vccs->vccs[i].vci == vcc->vci)
1019 if (i == vccs->count) {
1021 kfree(vccs, M_DEVBUF);
1025 NG_MKRESPONSE(resp, msg, sizeof(vccs->vccs[0]),
1026 M_WAITOK | M_NULLOK);
1029 kfree(vccs, M_DEVBUF);
1033 *(struct atmio_vcc *)resp->data = vccs->vccs[i];
1034 kfree(vccs, M_DEVBUF);
1038 case NGM_ATM_GET_VCCID:
1040 struct atmio_vcc *arg;
1041 struct atmio_vcctable *vccs;
1044 if (priv->ifp->if_ioctl == NULL) {
1048 if (msg->header.arglen != sizeof(*arg)) {
1052 arg = (struct atmio_vcc *)msg->data;
1054 error = (*priv->ifp->if_ioctl)(priv->ifp,
1055 SIOCATMGETVCCS, (caddr_t)&vccs);
1059 for (i = 0; i < vccs->count; i++)
1060 if (vccs->vccs[i].vpi == arg->vpi &&
1061 vccs->vccs[i].vci == arg->vci)
1063 if (i == vccs->count) {
1065 kfree(vccs, M_DEVBUF);
1069 NG_MKRESPONSE(resp, msg, sizeof(vccs->vccs[0]),
1070 M_WAITOK | M_NULLOK);
1073 kfree(vccs, M_DEVBUF);
1077 *(struct atmio_vcc *)resp->data = vccs->vccs[i];
1078 kfree(vccs, M_DEVBUF);
1082 case NGM_ATM_CPCS_INIT:
1083 if (msg->header.arglen !=
1084 sizeof(struct ngm_atm_cpcs_init)) {
1088 error = ng_atm_cpcs_init(node,
1089 (struct ngm_atm_cpcs_init *)msg->data);
1092 case NGM_ATM_CPCS_TERM:
1093 if (msg->header.arglen !=
1094 sizeof(struct ngm_atm_cpcs_term)) {
1098 error = ng_atm_cpcs_term(node,
1099 (struct ngm_atm_cpcs_term *)msg->data);
1102 case NGM_ATM_GET_STATS:
1104 struct ngm_atm_stats *p;
1106 if (msg->header.arglen != 0) {
1110 NG_MKRESPONSE(resp, msg, sizeof(*p), M_WAITOK | M_NULLOK);
1115 p = (struct ngm_atm_stats *)resp->data;
1116 p->in_packets = priv->in_packets;
1117 p->out_packets = priv->out_packets;
1118 p->in_errors = priv->in_errors;
1119 p->out_errors = priv->out_errors;
1135 NG_RESPOND_MSG(error, node, item, resp);
1140 /************************************************************/
1146 * A new hook is create that will be connected to the node.
1147 * Check, whether the name is one of the predefined ones.
1148 * If not, create a new entry into the vcc list.
1151 ng_atm_newhook(node_p node, hook_p hook, const char *name)
1153 struct priv *priv = NG_NODE_PRIVATE(node);
1156 if (strcmp(name, "input") == 0) {
1158 NG_HOOK_SET_RCVDATA(hook, ng_atm_rcvdrop);
1161 if (strcmp(name, "output") == 0) {
1162 priv->output = hook;
1163 NG_HOOK_SET_RCVDATA(hook, ng_atm_rcvdrop);
1166 if (strcmp(name, "orphans") == 0) {
1167 priv->orphans = hook;
1168 NG_HOOK_SET_RCVDATA(hook, ng_atm_rcvdrop);
1173 * Allocate a new entry
1175 vcc = kmalloc(sizeof(*vcc), M_NETGRAPH, M_WAITOK | M_NULLOK | M_ZERO);
1180 NG_HOOK_SET_PRIVATE(hook, vcc);
1182 LIST_INSERT_HEAD(&priv->vccs, vcc, link);
1184 if (strcmp(name, "manage") == 0)
1185 priv->manage = hook;
1191 * Connect. Set the peer to queuing.
1194 ng_atm_connect(hook_p hook)
1196 if (NG_HOOK_PRIVATE(hook) != NULL)
1197 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
1206 ng_atm_disconnect(hook_p hook)
1208 struct priv *priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1209 struct ngvcc *vcc = NG_HOOK_PRIVATE(hook);
1212 if (hook == priv->output) {
1213 priv->output = NULL;
1216 if (hook == priv->input) {
1220 if (hook == priv->orphans) {
1221 priv->orphans = NULL;
1224 log(LOG_ERR, "ng_atm: bad hook '%s'", NG_HOOK_NAME(hook));
1228 /* don't terminate if we are detaching from the interface */
1229 if ((vcc->flags & VCC_OPEN) && priv->ifp != NULL)
1230 (void)cpcs_term(priv, vcc->vpi, vcc->vci);
1232 NG_HOOK_SET_PRIVATE(hook, NULL);
1234 LIST_REMOVE(vcc, link);
1235 kfree(vcc, M_NETGRAPH);
1237 if (hook == priv->manage)
1238 priv->manage = NULL;
1243 /************************************************************/
1249 * ATM interface attached - create a node and name it like the interface.
1252 ng_atm_attach(struct ifnet *ifp)
1257 KASSERT(IFP2NG(ifp) == 0, ("%s: node alreay exists?", __func__));
1259 if (ng_make_node_common(&ng_atm_typestruct, &node) != 0) {
1260 log(LOG_ERR, "%s: can't create node for %s\n",
1261 __func__, ifp->if_xname);
1265 priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_NULLOK | M_ZERO);
1267 log(LOG_ERR, "%s: can't allocate memory for %s\n",
1268 __func__, ifp->if_xname);
1269 NG_NODE_UNREF(node);
1272 NG_NODE_SET_PRIVATE(node, priv);
1274 LIST_INIT(&priv->vccs);
1275 IFP2NG_SET(ifp, node);
1277 if (ng_name_node(node, ifp->if_xname) != 0) {
1278 log(LOG_WARNING, "%s: can't name node %s\n",
1279 __func__, ifp->if_xname);
1284 * ATM interface detached - destroy node.
1287 ng_atm_detach(struct ifnet *ifp)
1289 const node_p node = IFP2NG(ifp);
1295 NG_NODE_REALLY_DIE(node);
1297 priv = NG_NODE_PRIVATE(node);
1298 IFP2NG_SET(priv->ifp, NULL);
1301 ng_rmnode_self(node);
1305 * Shutdown the node. This is called from the shutdown message processing.
1308 ng_atm_shutdown(node_p node)
1310 struct priv *priv = NG_NODE_PRIVATE(node);
1312 if (node->nd_flags & NGF_REALLY_DIE) {
1314 * We are called from unloading the ATM driver. Really,
1315 * really need to shutdown this node. The ifp was
1316 * already handled in the detach routine.
1318 NG_NODE_SET_PRIVATE(node, NULL);
1319 kfree(priv, M_NETGRAPH);
1321 NG_NODE_UNREF(node);
1326 if (!allow_shutdown)
1327 NG_NODE_REVIVE(node); /* we persist */
1329 IFP2NG_SET(priv->ifp, NULL);
1330 NG_NODE_SET_PRIVATE(node, NULL);
1331 kfree(priv, M_NETGRAPH);
1332 NG_NODE_UNREF(node);
1336 * We are persistant - reinitialize
1338 NG_NODE_REVIVE(node);
1344 * Nodes are constructed only via interface attaches.
1347 ng_atm_constructor(node_p nodep)
1352 /************************************************************/
1357 * Loading and unloading of node type
1359 * The assignments to the globals for the hooks should be ok without
1360 * a special hook. The use pattern is generally: check that the pointer
1361 * is not NULL, call the function. In the attach case this is no problem.
1362 * In the detach case we can detach only when no ATM node exists. That
1363 * means that there is no ATM interface anymore. So we are sure that
1364 * we are not in the code path in if_atmsubr.c. To prevent someone
1365 * from adding an interface after we have started to unload the node, we
1366 * take the iflist lock so an if_attach will be blocked until we are done.
1367 * XXX: perhaps the function pointers should be 'volatile' for this to work
1371 ng_atm_mod_event(module_t mod, int event, void *data)
1380 * Register function hooks
1382 if (ng_atm_attach_p != NULL) {
1388 ng_atm_attach_p = ng_atm_attach;
1389 ng_atm_detach_p = ng_atm_detach;
1390 ng_atm_output_p = ng_atm_output;
1391 ng_atm_input_p = ng_atm_input;
1392 ng_atm_input_orphan_p = ng_atm_input_orphans;
1393 ng_atm_event_p = ng_atm_event;
1395 /* Create nodes for existing ATM interfaces */
1396 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1397 if (ifp->if_type == IFT_ATM)
1406 ng_atm_attach_p = NULL;
1407 ng_atm_detach_p = NULL;
1408 ng_atm_output_p = NULL;
1409 ng_atm_input_p = NULL;
1410 ng_atm_input_orphan_p = NULL;
1411 ng_atm_event_p = NULL;
1413 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1414 if (ifp->if_type == IFT_ATM)