Deleted Added
full compact
ng_base.c (194012) ng_base.c (195699)
1/*
2 * ng_base.c
3 */
4
5/*-
6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
7 * All rights reserved.
8 *

--- 24 unchanged lines hidden (view full) ---

33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36 * OF SUCH DAMAGE.
37 *
38 * Authors: Julian Elischer <julian@freebsd.org>
39 * Archie Cobbs <archie@freebsd.org>
40 *
1/*
2 * ng_base.c
3 */
4
5/*-
6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
7 * All rights reserved.
8 *

--- 24 unchanged lines hidden (view full) ---

33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36 * OF SUCH DAMAGE.
37 *
38 * Authors: Julian Elischer <julian@freebsd.org>
39 * Archie Cobbs <archie@freebsd.org>
40 *
41 * $FreeBSD: head/sys/netgraph/ng_base.c 194012 2009-06-11 16:50:49Z zec $
41 * $FreeBSD: head/sys/netgraph/ng_base.c 195699 2009-07-14 22:48:30Z rwatson $
42 * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $
43 */
44
45/*
46 * This file implements the base netgraph code.
47 */
48
49#include <sys/param.h>

--- 13 unchanged lines hidden (view full) ---

63#include <sys/proc.h>
64#include <sys/vimage.h>
65#include <sys/unistd.h>
66#include <sys/kthread.h>
67#include <sys/smp.h>
68#include <machine/cpu.h>
69
70#include <net/netisr.h>
42 * $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $
43 */
44
45/*
46 * This file implements the base netgraph code.
47 */
48
49#include <sys/param.h>

--- 13 unchanged lines hidden (view full) ---

63#include <sys/proc.h>
64#include <sys/vimage.h>
65#include <sys/unistd.h>
66#include <sys/kthread.h>
67#include <sys/smp.h>
68#include <machine/cpu.h>
69
70#include <net/netisr.h>
71#include <net/vnet.h>
71
72#include <netgraph/ng_message.h>
73#include <netgraph/netgraph.h>
74#include <netgraph/ng_parse.h>
75
76MODULE_VERSION(netgraph, NG_ABI_VERSION);
77
72
73#include <netgraph/ng_message.h>
74#include <netgraph/netgraph.h>
75#include <netgraph/ng_parse.h>
76
77MODULE_VERSION(netgraph, NG_ABI_VERSION);
78
78#ifndef VIMAGE
79#ifndef VIMAGE_GLOBALS
80struct vnet_netgraph vnet_netgraph_0;
81#endif
82#endif
83
84/* Mutex to protect topology events. */
85static struct mtx ng_topo_mtx;
86
79/* Mutex to protect topology events. */
80static struct mtx ng_topo_mtx;
81
87static vnet_attach_fn vnet_netgraph_iattach;
88#ifdef VIMAGE
89static vnet_detach_fn vnet_netgraph_idetach;
90#endif
91
92#ifdef NETGRAPH_DEBUG
93static struct mtx ng_nodelist_mtx; /* protects global node/hook lists */
94static struct mtx ngq_mtx; /* protects the queue item list */
95

--- 81 unchanged lines hidden (view full) ---

177static struct mtx ng_worklist_mtx; /* MUST LOCK NODE FIRST */
178
179/* List of installed types */
180static LIST_HEAD(, ng_type) ng_typelist;
181static struct mtx ng_typelist_mtx;
182
183/* Hash related definitions */
184/* XXX Don't need to initialise them because it's a LIST */
82#ifdef VIMAGE
83static vnet_detach_fn vnet_netgraph_idetach;
84#endif
85
86#ifdef NETGRAPH_DEBUG
87static struct mtx ng_nodelist_mtx; /* protects global node/hook lists */
88static struct mtx ngq_mtx; /* protects the queue item list */
89

--- 81 unchanged lines hidden (view full) ---

171static struct mtx ng_worklist_mtx; /* MUST LOCK NODE FIRST */
172
173/* List of installed types */
174static LIST_HEAD(, ng_type) ng_typelist;
175static struct mtx ng_typelist_mtx;
176
177/* Hash related definitions */
178/* XXX Don't need to initialise them because it's a LIST */
185#ifdef VIMAGE_GLOBALS
186static LIST_HEAD(, ng_node) ng_ID_hash[NG_ID_HASH_SIZE];
187#endif
179static VNET_DEFINE(LIST_HEAD(, ng_node), ng_ID_hash[NG_ID_HASH_SIZE]);
180#define V_ng_ID_hash VNET_GET(ng_ID_hash)
181
188static struct mtx ng_idhash_mtx;
189/* Method to find a node.. used twice so do it here */
190#define NG_IDHASH_FN(ID) ((ID) % (NG_ID_HASH_SIZE))
191#define NG_IDHASH_FIND(ID, node) \
192 do { \
193 mtx_assert(&ng_idhash_mtx, MA_OWNED); \
194 LIST_FOREACH(node, &V_ng_ID_hash[NG_IDHASH_FN(ID)], \
195 nd_idnodes) { \
196 if (NG_NODE_IS_VALID(node) \
197 && (NG_NODE_ID(node) == ID)) { \
198 break; \
199 } \
200 } \
201 } while (0)
202
182static struct mtx ng_idhash_mtx;
183/* Method to find a node.. used twice so do it here */
184#define NG_IDHASH_FN(ID) ((ID) % (NG_ID_HASH_SIZE))
185#define NG_IDHASH_FIND(ID, node) \
186 do { \
187 mtx_assert(&ng_idhash_mtx, MA_OWNED); \
188 LIST_FOREACH(node, &V_ng_ID_hash[NG_IDHASH_FN(ID)], \
189 nd_idnodes) { \
190 if (NG_NODE_IS_VALID(node) \
191 && (NG_NODE_ID(node) == ID)) { \
192 break; \
193 } \
194 } \
195 } while (0)
196
203#ifdef VIMAGE_GLOBALS
204static LIST_HEAD(, ng_node) ng_name_hash[NG_NAME_HASH_SIZE];
205#endif
197static VNET_DEFINE(LIST_HEAD(, ng_node), ng_name_hash[NG_NAME_HASH_SIZE]);
198#define V_ng_name_hash VNET_GET(ng_name_hash)
199
206static struct mtx ng_namehash_mtx;
207#define NG_NAMEHASH(NAME, HASH) \
208 do { \
209 u_char h = 0; \
210 const u_char *c; \
211 for (c = (const u_char*)(NAME); *c; c++)\
212 h += *c; \
213 (HASH) = h % (NG_NAME_HASH_SIZE); \

--- 151 unchanged lines hidden (view full) ---

365
366#endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
367
368/* Set this to kdb_enter("X") to catch all errors as they occur */
369#ifndef TRAP_ERROR
370#define TRAP_ERROR()
371#endif
372
200static struct mtx ng_namehash_mtx;
201#define NG_NAMEHASH(NAME, HASH) \
202 do { \
203 u_char h = 0; \
204 const u_char *c; \
205 for (c = (const u_char*)(NAME); *c; c++)\
206 h += *c; \
207 (HASH) = h % (NG_NAME_HASH_SIZE); \

--- 151 unchanged lines hidden (view full) ---

359
360#endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
361
362/* Set this to kdb_enter("X") to catch all errors as they occur */
363#ifndef TRAP_ERROR
364#define TRAP_ERROR()
365#endif
366
373#ifdef VIMAGE_GLOBALS
374static ng_ID_t nextID;
375#endif
367static VNET_DEFINE(ng_ID_t, nextID) = 1;
368#define V_nextID VNET_GET(nextID)
376
377#ifdef INVARIANTS
378#define CHECK_DATA_MBUF(m) do { \
379 struct mbuf *n; \
380 int total; \
381 \
382 M_ASSERTPKTHDR(m); \
383 for (total = 0, n = (m); n != NULL; n = n->m_next) { \

--- 245 unchanged lines hidden (view full) ---

629/*
630 * Generic node creation. Called by node initialisation for externally
631 * instantiated nodes (e.g. hardware, sockets, etc ).
632 * The returned node has a reference count of 1.
633 */
634int
635ng_make_node_common(struct ng_type *type, node_p *nodepp)
636{
369
370#ifdef INVARIANTS
371#define CHECK_DATA_MBUF(m) do { \
372 struct mbuf *n; \
373 int total; \
374 \
375 M_ASSERTPKTHDR(m); \
376 for (total = 0, n = (m); n != NULL; n = n->m_next) { \

--- 245 unchanged lines hidden (view full) ---

622/*
623 * Generic node creation. Called by node initialisation for externally
624 * instantiated nodes (e.g. hardware, sockets, etc ).
625 * The returned node has a reference count of 1.
626 */
627int
628ng_make_node_common(struct ng_type *type, node_p *nodepp)
629{
637 INIT_VNET_NETGRAPH(curvnet);
638 node_p node;
639
640 /* Require the node type to have been already installed */
641 if (ng_findtype(type->name) == NULL) {
642 TRAP_ERROR();
643 return (EINVAL);
644 }
645

--- 168 unchanged lines hidden (view full) ---

814}
815
816/************************************************************************
817 Node ID handling
818************************************************************************/
819static node_p
820ng_ID2noderef(ng_ID_t ID)
821{
630 node_p node;
631
632 /* Require the node type to have been already installed */
633 if (ng_findtype(type->name) == NULL) {
634 TRAP_ERROR();
635 return (EINVAL);
636 }
637

--- 168 unchanged lines hidden (view full) ---

806}
807
808/************************************************************************
809 Node ID handling
810************************************************************************/
811static node_p
812ng_ID2noderef(ng_ID_t ID)
813{
822 INIT_VNET_NETGRAPH(curvnet);
823 node_p node;
824 mtx_lock(&ng_idhash_mtx);
825 NG_IDHASH_FIND(ID, node);
826 if(node)
827 NG_NODE_REF(node);
828 mtx_unlock(&ng_idhash_mtx);
829 return(node);
830}

--- 9 unchanged lines hidden (view full) ---

840************************************************************************/
841
842/*
843 * Assign a node a name. Once assigned, the name cannot be changed.
844 */
845int
846ng_name_node(node_p node, const char *name)
847{
814 node_p node;
815 mtx_lock(&ng_idhash_mtx);
816 NG_IDHASH_FIND(ID, node);
817 if(node)
818 NG_NODE_REF(node);
819 mtx_unlock(&ng_idhash_mtx);
820 return(node);
821}

--- 9 unchanged lines hidden (view full) ---

831************************************************************************/
832
833/*
834 * Assign a node a name. Once assigned, the name cannot be changed.
835 */
836int
837ng_name_node(node_p node, const char *name)
838{
848 INIT_VNET_NETGRAPH(curvnet);
849 int i, hash;
850 node_p node2;
851
852 /* Check the name is valid */
853 for (i = 0; i < NG_NODESIZ; i++) {
854 if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
855 break;
856 }

--- 34 unchanged lines hidden (view full) ---

891 * Returns the node if found, else NULL.
892 * Eventually should add something faster than a sequential search.
893 * Note it acquires a reference on the node so you can be sure it's still
894 * there.
895 */
896node_p
897ng_name2noderef(node_p here, const char *name)
898{
839 int i, hash;
840 node_p node2;
841
842 /* Check the name is valid */
843 for (i = 0; i < NG_NODESIZ; i++) {
844 if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
845 break;
846 }

--- 34 unchanged lines hidden (view full) ---

881 * Returns the node if found, else NULL.
882 * Eventually should add something faster than a sequential search.
883 * Note it acquires a reference on the node so you can be sure it's still
884 * there.
885 */
886node_p
887ng_name2noderef(node_p here, const char *name)
888{
899 INIT_VNET_NETGRAPH(curvnet);
900 node_p node;
901 ng_ID_t temp;
902 int hash;
903
904 /* "." means "this node" */
905 if (strcmp(name, ".") == 0) {
906 NG_NODE_REF(here);
907 return(here);

--- 1544 unchanged lines hidden (view full) ---

2452}
2453
2454/***********************************************************************
2455 * Implement the 'generic' control messages
2456 ***********************************************************************/
2457static int
2458ng_generic_msg(node_p here, item_p item, hook_p lasthook)
2459{
889 node_p node;
890 ng_ID_t temp;
891 int hash;
892
893 /* "." means "this node" */
894 if (strcmp(name, ".") == 0) {
895 NG_NODE_REF(here);
896 return(here);

--- 1544 unchanged lines hidden (view full) ---

2441}
2442
2443/***********************************************************************
2444 * Implement the 'generic' control messages
2445 ***********************************************************************/
2446static int
2447ng_generic_msg(node_p here, item_p item, hook_p lasthook)
2448{
2460 INIT_VNET_NETGRAPH(curvnet);
2461 int error = 0;
2462 struct ng_mesg *msg;
2463 struct ng_mesg *resp = NULL;
2464
2465 NGI_GET_MSG(item, msg);
2466 if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
2467 TRAP_ERROR();
2468 error = EINVAL;

--- 606 unchanged lines hidden (view full) ---

3075 error = (*type->mod_event)(mod, event, data);
3076 else
3077 error = EOPNOTSUPP; /* XXX ? */
3078 break;
3079 }
3080 return (error);
3081}
3082
2449 int error = 0;
2450 struct ng_mesg *msg;
2451 struct ng_mesg *resp = NULL;
2452
2453 NGI_GET_MSG(item, msg);
2454 if (msg->header.typecookie != NGM_GENERIC_COOKIE) {
2455 TRAP_ERROR();
2456 error = EINVAL;

--- 606 unchanged lines hidden (view full) ---

3063 error = (*type->mod_event)(mod, event, data);
3064 else
3065 error = EOPNOTSUPP; /* XXX ? */
3066 break;
3067 }
3068 return (error);
3069}
3070
3083#ifndef VIMAGE_GLOBALS
3071#ifdef VIMAGE
3084static const vnet_modinfo_t vnet_netgraph_modinfo = {
3085 .vmi_id = VNET_MOD_NETGRAPH,
3086 .vmi_name = "netgraph",
3072static const vnet_modinfo_t vnet_netgraph_modinfo = {
3073 .vmi_id = VNET_MOD_NETGRAPH,
3074 .vmi_name = "netgraph",
3087 .vmi_size = sizeof(struct vnet_netgraph),
3088 .vmi_dependson = VNET_MOD_LOIF,
3075 .vmi_dependson = VNET_MOD_LOIF,
3089 .vmi_iattach = vnet_netgraph_iattach,
3090#ifdef VIMAGE
3091 .vmi_idetach = vnet_netgraph_idetach
3076 .vmi_idetach = vnet_netgraph_idetach
3092#endif
3093};
3094#endif
3095
3077};
3078#endif
3079
3096static int
3097vnet_netgraph_iattach(const void *unused __unused)
3098{
3099 INIT_VNET_NETGRAPH(curvnet);
3100
3101 V_nextID = 1;
3102
3103 return (0);
3104}
3105
3106#ifdef VIMAGE
3107static int
3108vnet_netgraph_idetach(const void *unused __unused)
3109{
3080#ifdef VIMAGE
3081static int
3082vnet_netgraph_idetach(const void *unused __unused)
3083{
3110 INIT_VNET_NETGRAPH(curvnet);
3084#if 0
3111 node_p node, last_killed = NULL;
3112
3085 node_p node, last_killed = NULL;
3086
3113 while ((node = LIST_FIRST(&V_ng_nodelist)) != NULL) {
3087 /* XXXRW: utterly bogus. */
3088 while ((node = LIST_FIRST(&V_ng_allnodes)) != NULL) {
3114 if (node == last_killed) {
3115 /* This should never happen */
3116 node->nd_flags |= NGF_REALLY_DIE;
3117 printf("netgraph node %s needs NGF_REALLY_DIE\n",
3118 node->nd_name);
3119 ng_rmnode(node, NULL, NULL, 0);
3120 /* This must never happen */
3089 if (node == last_killed) {
3090 /* This should never happen */
3091 node->nd_flags |= NGF_REALLY_DIE;
3092 printf("netgraph node %s needs NGF_REALLY_DIE\n",
3093 node->nd_name);
3094 ng_rmnode(node, NULL, NULL, 0);
3095 /* This must never happen */
3121 if (node == LIST_FIRST(&V_ng_nodelist))
3096 if (node == LIST_FIRST(&V_ng_allnodes))
3122 panic("netgraph node %s won't die",
3123 node->nd_name);
3124 }
3125 ng_rmnode(node, NULL, NULL, 0);
3126 last_killed = node;
3127 }
3097 panic("netgraph node %s won't die",
3098 node->nd_name);
3099 }
3100 ng_rmnode(node, NULL, NULL, 0);
3101 last_killed = node;
3102 }
3103#endif
3128
3129 return (0);
3130}
3131#endif /* VIMAGE */
3132
3133/*
3134 * Handle loading and unloading for this code.
3135 * The only thing we need to link into is the NETISR strucure.
3136 */
3137static int
3138ngb_mod_event(module_t mod, int event, void *data)
3139{
3140 struct proc *p;
3141 struct thread *td;
3142 int i, error = 0;
3143
3144 switch (event) {
3145 case MOD_LOAD:
3146 /* Initialize everything. */
3104
3105 return (0);
3106}
3107#endif /* VIMAGE */
3108
3109/*
3110 * Handle loading and unloading for this code.
3111 * The only thing we need to link into is the NETISR strucure.
3112 */
3113static int
3114ngb_mod_event(module_t mod, int event, void *data)
3115{
3116 struct proc *p;
3117 struct thread *td;
3118 int i, error = 0;
3119
3120 switch (event) {
3121 case MOD_LOAD:
3122 /* Initialize everything. */
3147#ifndef VIMAGE_GLOBALS
3123#ifdef VIMAGE
3148 vnet_mod_register(&vnet_netgraph_modinfo);
3124 vnet_mod_register(&vnet_netgraph_modinfo);
3149#else
3150 vnet_netgraph_iattach(NULL);
3151#endif
3152 NG_WORKLIST_LOCK_INIT();
3153 mtx_init(&ng_typelist_mtx, "netgraph types mutex", NULL,
3154 MTX_DEF);
3155 mtx_init(&ng_idhash_mtx, "netgraph idhash mutex", NULL,
3156 MTX_DEF);
3157 mtx_init(&ng_namehash_mtx, "netgraph namehash mutex", NULL,
3158 MTX_DEF);

--- 665 unchanged lines hidden ---
3125#endif
3126 NG_WORKLIST_LOCK_INIT();
3127 mtx_init(&ng_typelist_mtx, "netgraph types mutex", NULL,
3128 MTX_DEF);
3129 mtx_init(&ng_idhash_mtx, "netgraph idhash mutex", NULL,
3130 MTX_DEF);
3131 mtx_init(&ng_namehash_mtx, "netgraph namehash mutex", NULL,
3132 MTX_DEF);

--- 665 unchanged lines hidden ---