forked from F-Stack/f-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathff_ng_base.c
3887 lines (3498 loc) · 115 KB
/
ff_ng_base.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 1996-1999 Whistle Communications, Inc.
* All rights reserved.
*
* Subject to the following obligations and disclaimer of warranty, use and
* redistribution of this software, in source or object code forms, with or
* without modifications are expressly permitted by Whistle Communications;
* provided, however, that:
* 1. Any and all reproductions of the source or object code must include the
* copyright notice above and the following disclaimer of warranties; and
* 2. No rights are granted, in any manner or form, to use Whistle
* Communications, Inc. trademarks, including the mark "WHISTLE
* COMMUNICATIONS" on advertising, endorsements, or otherwise except as
* such appears in the above copyright notice or in the software.
*
* THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
* TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
* REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
* INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
* WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
* REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
* SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
* IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
* RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
* WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* Authors: Julian Elischer <[email protected]>
* Archie Cobbs <[email protected]>
*
* $FreeBSD$
* $Whistle: ng_base.c,v 1.39 1999/01/28 23:54:53 julian Exp $
*/
/*
* This file implements the base netgraph code.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/ctype.h>
#include <sys/hash.h>
#include <sys/kdb.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/ktr.h>
#include <sys/limits.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/epoch.h>
#include <sys/queue.h>
#include <sys/refcount.h>
#include <sys/rwlock.h>
#include <sys/smp.h>
#include <sys/sysctl.h>
#include <sys/syslog.h>
#include <sys/unistd.h>
#include <machine/cpu.h>
#include <vm/uma.h>
#include <net/netisr.h>
#include <net/vnet.h>
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include <netgraph/ng_parse.h>
MODULE_VERSION(netgraph, NG_ABI_VERSION);
/* Mutex to protect topology events. */
static struct rwlock ng_topo_lock;
#define TOPOLOGY_RLOCK() rw_rlock(&ng_topo_lock)
#define TOPOLOGY_RUNLOCK() rw_runlock(&ng_topo_lock)
#define TOPOLOGY_WLOCK() rw_wlock(&ng_topo_lock)
#define TOPOLOGY_WUNLOCK() rw_wunlock(&ng_topo_lock)
#define TOPOLOGY_NOTOWNED() rw_assert(&ng_topo_lock, RA_UNLOCKED)
#ifdef NETGRAPH_DEBUG
static struct mtx ng_nodelist_mtx; /* protects global node/hook lists */
static struct mtx ngq_mtx; /* protects the queue item list */
static SLIST_HEAD(, ng_node) ng_allnodes;
static LIST_HEAD(, ng_node) ng_freenodes; /* in debug, we never free() them */
static SLIST_HEAD(, ng_hook) ng_allhooks;
static LIST_HEAD(, ng_hook) ng_freehooks; /* in debug, we never free() them */
static void ng_dumpitems(void);
static void ng_dumpnodes(void);
static void ng_dumphooks(void);
#endif /* NETGRAPH_DEBUG */
/*
* DEAD versions of the structures.
* In order to avoid races, it is sometimes necessary to point
* at SOMETHING even though theoretically, the current entity is
* INVALID. Use these to avoid these races.
*/
struct ng_type ng_deadtype = {
NG_ABI_VERSION,
"dead",
NULL, /* modevent */
NULL, /* constructor */
NULL, /* rcvmsg */
NULL, /* shutdown */
NULL, /* newhook */
NULL, /* findhook */
NULL, /* connect */
NULL, /* rcvdata */
NULL, /* disconnect */
NULL, /* cmdlist */
};
struct ng_node ng_deadnode = {
"dead",
&ng_deadtype,
NGF_INVALID,
0, /* numhooks */
NULL, /* private */
0, /* ID */
LIST_HEAD_INITIALIZER(ng_deadnode.nd_hooks),
{}, /* all_nodes list entry */
{}, /* id hashtable list entry */
{ 0,
0,
{}, /* should never use! (should hang) */
{}, /* workqueue entry */
STAILQ_HEAD_INITIALIZER(ng_deadnode.nd_input_queue.queue),
},
1, /* refs */
NULL, /* vnet */
#ifdef NETGRAPH_DEBUG
ND_MAGIC,
__FILE__,
__LINE__,
{NULL}
#endif /* NETGRAPH_DEBUG */
};
struct ng_hook ng_deadhook = {
"dead",
NULL, /* private */
HK_INVALID | HK_DEAD,
0, /* undefined data link type */
&ng_deadhook, /* Peer is self */
&ng_deadnode, /* attached to deadnode */
{}, /* hooks list */
NULL, /* override rcvmsg() */
NULL, /* override rcvdata() */
1, /* refs always >= 1 */
#ifdef NETGRAPH_DEBUG
HK_MAGIC,
__FILE__,
__LINE__,
{NULL}
#endif /* NETGRAPH_DEBUG */
};
/*
* END DEAD STRUCTURES
*/
/* List nodes with unallocated work */
static STAILQ_HEAD(, ng_node) ng_worklist = STAILQ_HEAD_INITIALIZER(ng_worklist);
static struct mtx ng_worklist_mtx; /* MUST LOCK NODE FIRST */
/* List of installed types */
static LIST_HEAD(, ng_type) ng_typelist;
static struct rwlock ng_typelist_lock;
#define TYPELIST_RLOCK() rw_rlock(&ng_typelist_lock)
#define TYPELIST_RUNLOCK() rw_runlock(&ng_typelist_lock)
#define TYPELIST_WLOCK() rw_wlock(&ng_typelist_lock)
#define TYPELIST_WUNLOCK() rw_wunlock(&ng_typelist_lock)
/* Hash related definitions. */
LIST_HEAD(nodehash, ng_node);
VNET_DEFINE_STATIC(struct nodehash *, ng_ID_hash);
VNET_DEFINE_STATIC(u_long, ng_ID_hmask);
VNET_DEFINE_STATIC(u_long, ng_nodes);
VNET_DEFINE_STATIC(struct nodehash *, ng_name_hash);
VNET_DEFINE_STATIC(u_long, ng_name_hmask);
VNET_DEFINE_STATIC(u_long, ng_named_nodes);
#define V_ng_ID_hash VNET(ng_ID_hash)
#define V_ng_ID_hmask VNET(ng_ID_hmask)
#define V_ng_nodes VNET(ng_nodes)
#define V_ng_name_hash VNET(ng_name_hash)
#define V_ng_name_hmask VNET(ng_name_hmask)
#define V_ng_named_nodes VNET(ng_named_nodes)
static struct rwlock ng_idhash_lock;
#define IDHASH_RLOCK() rw_rlock(&ng_idhash_lock)
#define IDHASH_RUNLOCK() rw_runlock(&ng_idhash_lock)
#define IDHASH_WLOCK() rw_wlock(&ng_idhash_lock)
#define IDHASH_WUNLOCK() rw_wunlock(&ng_idhash_lock)
/* Method to find a node.. used twice so do it here */
#define NG_IDHASH_FN(ID) ((ID) % (V_ng_ID_hmask + 1))
#define NG_IDHASH_FIND(ID, node) \
do { \
rw_assert(&ng_idhash_lock, RA_LOCKED); \
LIST_FOREACH(node, &V_ng_ID_hash[NG_IDHASH_FN(ID)], \
nd_idnodes) { \
if (NG_NODE_IS_VALID(node) \
&& (NG_NODE_ID(node) == ID)) { \
break; \
} \
} \
} while (0)
static struct rwlock ng_namehash_lock;
#define NAMEHASH_RLOCK() rw_rlock(&ng_namehash_lock)
#define NAMEHASH_RUNLOCK() rw_runlock(&ng_namehash_lock)
#define NAMEHASH_WLOCK() rw_wlock(&ng_namehash_lock)
#define NAMEHASH_WUNLOCK() rw_wunlock(&ng_namehash_lock)
/* Internal functions */
static int ng_add_hook(node_p node, const char *name, hook_p * hookp);
static int ng_generic_msg(node_p here, item_p item, hook_p lasthook);
static ng_ID_t ng_decodeidname(const char *name);
static int ngb_mod_event(module_t mod, int event, void *data);
#ifndef FSTACK
static void ng_worklist_add(node_p node);
static void ngthread(void *);
#endif
static int ng_apply_item(node_p node, item_p item, int rw);
#ifndef FSTACK
static void ng_flush_input_queue(node_p node);
#endif
static node_p ng_ID2noderef(ng_ID_t ID);
static int ng_con_nodes(item_p item, node_p node, const char *name,
node_p node2, const char *name2);
static int ng_con_part2(node_p node, item_p item, hook_p hook);
static int ng_con_part3(node_p node, item_p item, hook_p hook);
static int ng_mkpeer(node_p node, const char *name, const char *name2,
char *type);
static void ng_name_rehash(void);
static void ng_ID_rehash(void);
/* Imported, these used to be externally visible, some may go back. */
void ng_destroy_hook(hook_p hook);
int ng_path2noderef(node_p here, const char *path,
node_p *dest, hook_p *lasthook);
int ng_make_node(const char *type, node_p *nodepp);
int ng_path_parse(char *addr, char **node, char **path, char **hook);
void ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3);
void ng_unname(node_p node);
/* Our own netgraph malloc type */
MALLOC_DEFINE(M_NETGRAPH, "netgraph", "netgraph structures and ctrl messages");
MALLOC_DEFINE(M_NETGRAPH_MSG, "netgraph_msg", "netgraph name storage");
static MALLOC_DEFINE(M_NETGRAPH_HOOK, "netgraph_hook",
"netgraph hook structures");
static MALLOC_DEFINE(M_NETGRAPH_NODE, "netgraph_node",
"netgraph node structures");
static MALLOC_DEFINE(M_NETGRAPH_ITEM, "netgraph_item",
"netgraph item structures");
/* Should not be visible outside this file */
#define _NG_ALLOC_HOOK(hook) \
hook = malloc(sizeof(*hook), M_NETGRAPH_HOOK, M_NOWAIT | M_ZERO)
#define _NG_ALLOC_NODE(node) \
node = malloc(sizeof(*node), M_NETGRAPH_NODE, M_NOWAIT | M_ZERO)
#define NG_QUEUE_LOCK_INIT(n) \
mtx_init(&(n)->q_mtx, "ng_node", NULL, MTX_DEF)
#define NG_QUEUE_LOCK(n) \
mtx_lock(&(n)->q_mtx)
#define NG_QUEUE_UNLOCK(n) \
mtx_unlock(&(n)->q_mtx)
#define NG_WORKLIST_LOCK_INIT() \
mtx_init(&ng_worklist_mtx, "ng_worklist", NULL, MTX_DEF)
#define NG_WORKLIST_LOCK() \
mtx_lock(&ng_worklist_mtx)
#define NG_WORKLIST_UNLOCK() \
mtx_unlock(&ng_worklist_mtx)
#define NG_WORKLIST_SLEEP() \
mtx_sleep(&ng_worklist, &ng_worklist_mtx, PI_NET, "sleep", 0)
#define NG_WORKLIST_WAKEUP() \
wakeup_one(&ng_worklist)
#ifdef NETGRAPH_DEBUG /*----------------------------------------------*/
/*
* In debug mode:
* In an attempt to help track reference count screwups
* we do not free objects back to the malloc system, but keep them
* in a local cache where we can examine them and keep information safely
* after they have been freed.
* We use this scheme for nodes and hooks, and to some extent for items.
*/
static __inline hook_p
ng_alloc_hook(void)
{
hook_p hook;
SLIST_ENTRY(ng_hook) temp;
mtx_lock(&ng_nodelist_mtx);
hook = LIST_FIRST(&ng_freehooks);
if (hook) {
LIST_REMOVE(hook, hk_hooks);
bcopy(&hook->hk_all, &temp, sizeof(temp));
bzero(hook, sizeof(struct ng_hook));
bcopy(&temp, &hook->hk_all, sizeof(temp));
mtx_unlock(&ng_nodelist_mtx);
hook->hk_magic = HK_MAGIC;
} else {
mtx_unlock(&ng_nodelist_mtx);
_NG_ALLOC_HOOK(hook);
if (hook) {
hook->hk_magic = HK_MAGIC;
mtx_lock(&ng_nodelist_mtx);
SLIST_INSERT_HEAD(&ng_allhooks, hook, hk_all);
mtx_unlock(&ng_nodelist_mtx);
}
}
return (hook);
}
static __inline node_p
ng_alloc_node(void)
{
node_p node;
SLIST_ENTRY(ng_node) temp;
mtx_lock(&ng_nodelist_mtx);
node = LIST_FIRST(&ng_freenodes);
if (node) {
LIST_REMOVE(node, nd_nodes);
bcopy(&node->nd_all, &temp, sizeof(temp));
bzero(node, sizeof(struct ng_node));
bcopy(&temp, &node->nd_all, sizeof(temp));
mtx_unlock(&ng_nodelist_mtx);
node->nd_magic = ND_MAGIC;
} else {
mtx_unlock(&ng_nodelist_mtx);
_NG_ALLOC_NODE(node);
if (node) {
node->nd_magic = ND_MAGIC;
mtx_lock(&ng_nodelist_mtx);
SLIST_INSERT_HEAD(&ng_allnodes, node, nd_all);
mtx_unlock(&ng_nodelist_mtx);
}
}
return (node);
}
#define NG_ALLOC_HOOK(hook) do { (hook) = ng_alloc_hook(); } while (0)
#define NG_ALLOC_NODE(node) do { (node) = ng_alloc_node(); } while (0)
#define NG_FREE_HOOK(hook) \
do { \
mtx_lock(&ng_nodelist_mtx); \
LIST_INSERT_HEAD(&ng_freehooks, hook, hk_hooks); \
hook->hk_magic = 0; \
mtx_unlock(&ng_nodelist_mtx); \
} while (0)
#define NG_FREE_NODE(node) \
do { \
mtx_lock(&ng_nodelist_mtx); \
LIST_INSERT_HEAD(&ng_freenodes, node, nd_nodes); \
node->nd_magic = 0; \
mtx_unlock(&ng_nodelist_mtx); \
} while (0)
#else /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
#define NG_ALLOC_HOOK(hook) _NG_ALLOC_HOOK(hook)
#define NG_ALLOC_NODE(node) _NG_ALLOC_NODE(node)
#define NG_FREE_HOOK(hook) do { free((hook), M_NETGRAPH_HOOK); } while (0)
#define NG_FREE_NODE(node) do { free((node), M_NETGRAPH_NODE); } while (0)
#endif /* NETGRAPH_DEBUG */ /*----------------------------------------------*/
/* Set this to kdb_enter("X") to catch all errors as they occur */
#ifndef TRAP_ERROR
#define TRAP_ERROR()
#endif
VNET_DEFINE_STATIC(ng_ID_t, nextID) = 1;
#define V_nextID VNET(nextID)
#ifdef INVARIANTS
#define CHECK_DATA_MBUF(m) do { \
struct mbuf *n; \
int total; \
\
M_ASSERTPKTHDR(m); \
for (total = 0, n = (m); n != NULL; n = n->m_next) { \
total += n->m_len; \
if (n->m_nextpkt != NULL) \
panic("%s: m_nextpkt", __func__); \
} \
\
if ((m)->m_pkthdr.len != total) { \
panic("%s: %d != %d", \
__func__, (m)->m_pkthdr.len, total); \
} \
} while (0)
#else
#define CHECK_DATA_MBUF(m)
#endif
#define ERROUT(x) do { error = (x); goto done; } while (0)
/************************************************************************
Parse type definitions for generic messages
************************************************************************/
/* Handy structure parse type defining macro */
#define DEFINE_PARSE_STRUCT_TYPE(lo, up, args) \
static const struct ng_parse_struct_field \
ng_ ## lo ## _type_fields[] = NG_GENERIC_ ## up ## _INFO args; \
static const struct ng_parse_type ng_generic_ ## lo ## _type = { \
&ng_parse_struct_type, \
&ng_ ## lo ## _type_fields \
}
DEFINE_PARSE_STRUCT_TYPE(mkpeer, MKPEER, ());
DEFINE_PARSE_STRUCT_TYPE(connect, CONNECT, ());
DEFINE_PARSE_STRUCT_TYPE(name, NAME, ());
DEFINE_PARSE_STRUCT_TYPE(rmhook, RMHOOK, ());
DEFINE_PARSE_STRUCT_TYPE(nodeinfo, NODEINFO, ());
DEFINE_PARSE_STRUCT_TYPE(typeinfo, TYPEINFO, ());
DEFINE_PARSE_STRUCT_TYPE(linkinfo, LINKINFO, (&ng_generic_nodeinfo_type));
/* Get length of an array when the length is stored as a 32 bit
value immediately preceding the array -- as with struct namelist
and struct typelist. */
static int
ng_generic_list_getLength(const struct ng_parse_type *type,
const u_char *start, const u_char *buf)
{
return *((const u_int32_t *)(buf - 4));
}
/* Get length of the array of struct linkinfo inside a struct hooklist */
static int
ng_generic_linkinfo_getLength(const struct ng_parse_type *type,
const u_char *start, const u_char *buf)
{
const struct hooklist *hl = (const struct hooklist *)start;
return hl->nodeinfo.hooks;
}
/* Array type for a variable length array of struct namelist */
static const struct ng_parse_array_info ng_nodeinfoarray_type_info = {
&ng_generic_nodeinfo_type,
&ng_generic_list_getLength
};
static const struct ng_parse_type ng_generic_nodeinfoarray_type = {
&ng_parse_array_type,
&ng_nodeinfoarray_type_info
};
/* Array type for a variable length array of struct typelist */
static const struct ng_parse_array_info ng_typeinfoarray_type_info = {
&ng_generic_typeinfo_type,
&ng_generic_list_getLength
};
static const struct ng_parse_type ng_generic_typeinfoarray_type = {
&ng_parse_array_type,
&ng_typeinfoarray_type_info
};
/* Array type for array of struct linkinfo in struct hooklist */
static const struct ng_parse_array_info ng_generic_linkinfo_array_type_info = {
&ng_generic_linkinfo_type,
&ng_generic_linkinfo_getLength
};
static const struct ng_parse_type ng_generic_linkinfo_array_type = {
&ng_parse_array_type,
&ng_generic_linkinfo_array_type_info
};
DEFINE_PARSE_STRUCT_TYPE(typelist, TYPELIST, (&ng_generic_typeinfoarray_type));
DEFINE_PARSE_STRUCT_TYPE(hooklist, HOOKLIST,
(&ng_generic_nodeinfo_type, &ng_generic_linkinfo_array_type));
DEFINE_PARSE_STRUCT_TYPE(listnodes, LISTNODES,
(&ng_generic_nodeinfoarray_type));
/* List of commands and how to convert arguments to/from ASCII */
static const struct ng_cmdlist ng_generic_cmds[] = {
{
NGM_GENERIC_COOKIE,
NGM_SHUTDOWN,
"shutdown",
NULL,
NULL
},
{
NGM_GENERIC_COOKIE,
NGM_MKPEER,
"mkpeer",
&ng_generic_mkpeer_type,
NULL
},
{
NGM_GENERIC_COOKIE,
NGM_CONNECT,
"connect",
&ng_generic_connect_type,
NULL
},
{
NGM_GENERIC_COOKIE,
NGM_NAME,
"name",
&ng_generic_name_type,
NULL
},
{
NGM_GENERIC_COOKIE,
NGM_RMHOOK,
"rmhook",
&ng_generic_rmhook_type,
NULL
},
{
NGM_GENERIC_COOKIE,
NGM_NODEINFO,
"nodeinfo",
NULL,
&ng_generic_nodeinfo_type
},
{
NGM_GENERIC_COOKIE,
NGM_LISTHOOKS,
"listhooks",
NULL,
&ng_generic_hooklist_type
},
{
NGM_GENERIC_COOKIE,
NGM_LISTNAMES,
"listnames",
NULL,
&ng_generic_listnodes_type /* same as NGM_LISTNODES */
},
{
NGM_GENERIC_COOKIE,
NGM_LISTNODES,
"listnodes",
NULL,
&ng_generic_listnodes_type
},
{
NGM_GENERIC_COOKIE,
NGM_LISTTYPES,
"listtypes",
NULL,
&ng_generic_typelist_type
},
{
NGM_GENERIC_COOKIE,
NGM_TEXT_CONFIG,
"textconfig",
NULL,
&ng_parse_string_type
},
{
NGM_GENERIC_COOKIE,
NGM_TEXT_STATUS,
"textstatus",
NULL,
&ng_parse_string_type
},
{
NGM_GENERIC_COOKIE,
NGM_ASCII2BINARY,
"ascii2binary",
&ng_parse_ng_mesg_type,
&ng_parse_ng_mesg_type
},
{
NGM_GENERIC_COOKIE,
NGM_BINARY2ASCII,
"binary2ascii",
&ng_parse_ng_mesg_type,
&ng_parse_ng_mesg_type
},
{ 0 }
};
/************************************************************************
Node routines
************************************************************************/
/*
* Instantiate a node of the requested type
*/
int
ng_make_node(const char *typename, node_p *nodepp)
{
struct ng_type *type;
int error;
/* Check that the type makes sense */
if (typename == NULL) {
TRAP_ERROR();
return (EINVAL);
}
/* Locate the node type. If we fail we return. Do not try to load
* module.
*/
if ((type = ng_findtype(typename)) == NULL)
return (ENXIO);
/*
* If we have a constructor, then make the node and
* call the constructor to do type specific initialisation.
*/
if (type->constructor != NULL) {
if ((error = ng_make_node_common(type, nodepp)) == 0) {
if ((error = ((*type->constructor)(*nodepp))) != 0) {
NG_NODE_UNREF(*nodepp);
}
}
} else {
/*
* Node has no constructor. We cannot ask for one
* to be made. It must be brought into existence by
* some external agency. The external agency should
* call ng_make_node_common() directly to get the
* netgraph part initialised.
*/
TRAP_ERROR();
error = EINVAL;
}
return (error);
}
/*
* Generic node creation. Called by node initialisation for externally
* instantiated nodes (e.g. hardware, sockets, etc ).
* The returned node has a reference count of 1.
*/
int
ng_make_node_common(struct ng_type *type, node_p *nodepp)
{
node_p node;
/* Require the node type to have been already installed */
if (ng_findtype(type->name) == NULL) {
TRAP_ERROR();
return (EINVAL);
}
/* Make a node and try attach it to the type */
NG_ALLOC_NODE(node);
if (node == NULL) {
TRAP_ERROR();
return (ENOMEM);
}
node->nd_type = type;
#ifdef VIMAGE
node->nd_vnet = curvnet;
#endif
NG_NODE_REF(node); /* note reference */
type->refs++;
NG_QUEUE_LOCK_INIT(&node->nd_input_queue);
STAILQ_INIT(&node->nd_input_queue.queue);
node->nd_input_queue.q_flags = 0;
/* Initialize hook list for new node */
LIST_INIT(&node->nd_hooks);
/* Get an ID and put us in the hash chain. */
IDHASH_WLOCK();
for (;;) { /* wrap protection, even if silly */
node_p node2 = NULL;
node->nd_ID = V_nextID++; /* 137/sec for 1 year before wrap */
/* Is there a problem with the new number? */
NG_IDHASH_FIND(node->nd_ID, node2); /* already taken? */
if ((node->nd_ID != 0) && (node2 == NULL)) {
break;
}
}
V_ng_nodes++;
if (V_ng_nodes * 2 > V_ng_ID_hmask)
ng_ID_rehash();
LIST_INSERT_HEAD(&V_ng_ID_hash[NG_IDHASH_FN(node->nd_ID)], node,
nd_idnodes);
IDHASH_WUNLOCK();
/* Done */
*nodepp = node;
return (0);
}
/*
* Forceably start the shutdown process on a node. Either call
* its shutdown method, or do the default shutdown if there is
* no type-specific method.
*
* We can only be called from a shutdown message, so we know we have
* a writer lock, and therefore exclusive access. It also means
* that we should not be on the work queue, but we check anyhow.
*
* Persistent node types must have a type-specific method which
* allocates a new node in which case, this one is irretrievably going away,
* or cleans up anything it needs, and just makes the node valid again,
* in which case we allow the node to survive.
*
* XXX We need to think of how to tell a persistent node that we
* REALLY need to go away because the hardware has gone or we
* are rebooting.... etc.
*/
void
ng_rmnode(node_p node, hook_p dummy1, void *dummy2, int dummy3)
{
hook_p hook;
/* Check if it's already shutting down */
if ((node->nd_flags & NGF_CLOSING) != 0)
return;
if (node == &ng_deadnode) {
printf ("shutdown called on deadnode\n");
return;
}
/* Add an extra reference so it doesn't go away during this */
NG_NODE_REF(node);
/*
* Mark it invalid so any newcomers know not to try use it
* Also add our own mark so we can't recurse
* note that NGF_INVALID does not do this as it's also set during
* creation
*/
node->nd_flags |= NGF_INVALID|NGF_CLOSING;
/* If node has its pre-shutdown method, then call it first*/
if (node->nd_type && node->nd_type->close)
(*node->nd_type->close)(node);
/* Notify all remaining connected nodes to disconnect */
while ((hook = LIST_FIRST(&node->nd_hooks)) != NULL)
ng_destroy_hook(hook);
#ifndef FSTACK
/*
* Drain the input queue forceably.
* it has no hooks so what's it going to do, bleed on someone?
* Theoretically we came here from a queue entry that was added
* Just before the queue was closed, so it should be empty anyway.
* Also removes us from worklist if needed.
*/
ng_flush_input_queue(node);
#endif
/* Ask the type if it has anything to do in this case */
if (node->nd_type && node->nd_type->shutdown) {
(*node->nd_type->shutdown)(node);
if (NG_NODE_IS_VALID(node)) {
/*
* Well, blow me down if the node code hasn't declared
* that it doesn't want to die.
* Presumably it is a persistent node.
* If we REALLY want it to go away,
* e.g. hardware going away,
* Our caller should set NGF_REALLY_DIE in nd_flags.
*/
node->nd_flags &= ~(NGF_INVALID|NGF_CLOSING);
NG_NODE_UNREF(node); /* Assume they still have theirs */
return;
}
} else { /* do the default thing */
NG_NODE_UNREF(node);
}
ng_unname(node); /* basically a NOP these days */
/*
* Remove extra reference, possibly the last
* Possible other holders of references may include
* timeout callouts, but theoretically the node's supposed to
* have cancelled them. Possibly hardware dependencies may
* force a driver to 'linger' with a reference.
*/
NG_NODE_UNREF(node);
}
/*
* Remove a reference to the node, possibly the last.
* deadnode always acts as it it were the last.
*/
void
ng_unref_node(node_p node)
{
if (node == &ng_deadnode)
return;
CURVNET_SET(node->nd_vnet);
if (refcount_release(&node->nd_refs)) { /* we were the last */
node->nd_type->refs--; /* XXX maybe should get types lock? */
NAMEHASH_WLOCK();
if (NG_NODE_HAS_NAME(node)) {
V_ng_named_nodes--;
LIST_REMOVE(node, nd_nodes);
}
NAMEHASH_WUNLOCK();
IDHASH_WLOCK();
V_ng_nodes--;
LIST_REMOVE(node, nd_idnodes);
IDHASH_WUNLOCK();
mtx_destroy(&node->nd_input_queue.q_mtx);
NG_FREE_NODE(node);
}
CURVNET_RESTORE();
}
/************************************************************************
Node ID handling
************************************************************************/
static node_p
ng_ID2noderef(ng_ID_t ID)
{
node_p node;
IDHASH_RLOCK();
NG_IDHASH_FIND(ID, node);
if (node)
NG_NODE_REF(node);
IDHASH_RUNLOCK();
return(node);
}
ng_ID_t
ng_node2ID(node_p node)
{
return (node ? NG_NODE_ID(node) : 0);
}
/************************************************************************
Node name handling
************************************************************************/
/*
* Assign a node a name.
*/
int
ng_name_node(node_p node, const char *name)
{
uint32_t hash;
node_p node2;
int i;
/* Check the name is valid */
for (i = 0; i < NG_NODESIZ; i++) {
if (name[i] == '\0' || name[i] == '.' || name[i] == ':')
break;
}
if (i == 0 || name[i] != '\0') {
TRAP_ERROR();
return (EINVAL);
}
if (ng_decodeidname(name) != 0) { /* valid IDs not allowed here */
TRAP_ERROR();
return (EINVAL);
}
NAMEHASH_WLOCK();
if (V_ng_named_nodes * 2 > V_ng_name_hmask)
ng_name_rehash();
hash = hash32_str(name, HASHINIT) & V_ng_name_hmask;
/* Check the name isn't already being used. */
LIST_FOREACH(node2, &V_ng_name_hash[hash], nd_nodes)
if (NG_NODE_IS_VALID(node2) &&
(strcmp(NG_NODE_NAME(node2), name) == 0)) {
NAMEHASH_WUNLOCK();
return (EADDRINUSE);
}
if (NG_NODE_HAS_NAME(node))
LIST_REMOVE(node, nd_nodes);
else
V_ng_named_nodes++;
/* Copy it. */
strlcpy(NG_NODE_NAME(node), name, NG_NODESIZ);
/* Update name hash. */
LIST_INSERT_HEAD(&V_ng_name_hash[hash], node, nd_nodes);
NAMEHASH_WUNLOCK();
return (0);
}
/*
* Find a node by absolute name. The name should NOT end with ':'
* The name "." means "this node" and "[xxx]" means "the node
* with ID (ie, at address) xxx".
*
* Returns the node if found, else NULL.
* Eventually should add something faster than a sequential search.
* Note it acquires a reference on the node so you can be sure it's still
* there.
*/
node_p
ng_name2noderef(node_p here, const char *name)
{
node_p node;
ng_ID_t temp;
int hash;
/* "." means "this node" */
if (strcmp(name, ".") == 0) {
NG_NODE_REF(here);
return(here);
}
/* Check for name-by-ID */
if ((temp = ng_decodeidname(name)) != 0) {
return (ng_ID2noderef(temp));
}
/* Find node by name. */
hash = hash32_str(name, HASHINIT) & V_ng_name_hmask;
NAMEHASH_RLOCK();
LIST_FOREACH(node, &V_ng_name_hash[hash], nd_nodes)
if (NG_NODE_IS_VALID(node) &&
(strcmp(NG_NODE_NAME(node), name) == 0)) {
NG_NODE_REF(node);
break;
}
NAMEHASH_RUNLOCK();
return (node);
}
/*
* Decode an ID name, eg. "[f03034de]". Returns 0 if the
* string is not valid, otherwise returns the value.
*/
static ng_ID_t
ng_decodeidname(const char *name)
{
const int len = strlen(name);
char *eptr;
u_long val;
/* Check for proper length, brackets, no leading junk */
if ((len < 3) || (name[0] != '[') || (name[len - 1] != ']') ||
(!isxdigit(name[1])))
return ((ng_ID_t)0);
/* Decode number */
val = strtoul(name + 1, &eptr, 16);
if ((eptr - name != len - 1) || (val == ULONG_MAX) || (val == 0))
return ((ng_ID_t)0);
return ((ng_ID_t)val);
}
/*
* Remove a name from a node. This should only be called
* when shutting down and removing the node.
*/
void
ng_unname(node_p node)
{
}
/*
* Allocate a bigger name hash.
*/
static void
ng_name_rehash()
{
struct nodehash *new;
uint32_t hash;
u_long hmask;
node_p node, node2;
int i;
new = hashinit_flags((V_ng_name_hmask + 1) * 2, M_NETGRAPH_NODE, &hmask,
HASH_NOWAIT);
if (new == NULL)
return;
for (i = 0; i <= V_ng_name_hmask; i++)
LIST_FOREACH_SAFE(node, &V_ng_name_hash[i], nd_nodes, node2) {
#ifdef INVARIANTS
LIST_REMOVE(node, nd_nodes);
#endif