Deleted Added
sdiff udiff text old ( 194012 ) new ( 195699 )
full compact
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 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>
71#include <net/vnet.h>
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
79/* Mutex to protect topology events. */
80static struct mtx ng_topo_mtx;
81
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 */
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
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
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
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
367static VNET_DEFINE(ng_ID_t, nextID) = 1;
368#define V_nextID VNET_GET(nextID)
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{
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{
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{
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{
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{
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
3071#ifdef VIMAGE
3072static const vnet_modinfo_t vnet_netgraph_modinfo = {
3073 .vmi_id = VNET_MOD_NETGRAPH,
3074 .vmi_name = "netgraph",
3075 .vmi_dependson = VNET_MOD_LOIF,
3076 .vmi_idetach = vnet_netgraph_idetach
3077};
3078#endif
3079
3080#ifdef VIMAGE
3081static int
3082vnet_netgraph_idetach(const void *unused __unused)
3083{
3084#if 0
3085 node_p node, last_killed = NULL;
3086
3087 /* XXXRW: utterly bogus. */
3088 while ((node = LIST_FIRST(&V_ng_allnodes)) != NULL) {
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 */
3096 if (node == LIST_FIRST(&V_ng_allnodes))
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
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. */
3123#ifdef VIMAGE
3124 vnet_mod_register(&vnet_netgraph_modinfo);
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 ---