-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux-2.6-690-web100.patch
3761 lines (3657 loc) · 109 KB
/
linux-2.6-690-web100.patch
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
diff --git a/Documentation/web100/locking.txt b/Documentation/web100/locking.txt
new file mode 100644
index 0000000..b8ed239
--- /dev/null
+++ b/Documentation/web100/locking.txt
@@ -0,0 +1,33 @@
+Web100 Locking Model for Linux 2.4
+John Heffner <jheffner@psc.edu>
+August 2, 2001
+
+
+1. Lookup Structures
+
+The connections entries are kept linked together simultaneously in a table
+and in a list. Only entries in these structures can be looked up. To
+protect these lookup structures, we have a single global reader-writer
+spinlock, web100_linkage_lock. Since we grab the lock both from user space
+and in the bottom half, we must do a [read/write]_lock_bh. As this disables
+the local BH's, this lock should *not* be held for very long.
+
+
+2. Data Integrity
+
+The statistics are protected by the sock's lock. Any code modifying or
+reading the statistics should hold the sock lock while doing so. We assume
+that if the socket is gone, the statistics should not be modified, so
+readers need not hold any lock.
+
+
+3. Statistics Destruction
+
+A statistics structure keeps a count of the number of references to it,
+wc_users. When a lookup is performed, the reference count should be
+incremented (while the linkage lock is held) by calling web100_stats_use.
+When the reference is no longer needed, decrement the count by calling
+web100_stats_unuse. The latter function will free the statistics when there
+are no remaining references. The lookup structures keep one reference. The
+sock also keeps one, since the sock may be destroyed before it ever enters
+the ESTABLISHED state.
diff --git a/Documentation/web100/proc_interface.txt b/Documentation/web100/proc_interface.txt
new file mode 100644
index 0000000..772a763
--- /dev/null
+++ b/Documentation/web100/proc_interface.txt
@@ -0,0 +1,102 @@
+WEB100 proc interface notes
+===========================
+
+The web100 modifications to the kernel collect information about the
+state of a TCP transfer in a kernel data structure that is linked
+out of the "sock" TCP structure in sock.h. Please see
+"include/net/web100_stats.h" for the structure definition.
+
+The API for this structure is provided through the /proc interface.
+This document provides a brief description of this interface. Please
+see fs/proc/web100.c for source code.
+
+First, kernel creates the /proc/web100 directory and the file
+/proc/web100/header at system boot time.
+
+Each new TCP connection is assigned a unique, unchanging number
+(similar to a pid), and its directory name is that number as ASCII
+decimal. These directories persist for about sixty seconds after the
+connection is terminated (goes into a CLOSED or TIME_WAIT state). The
+connection stats will not change after the connection is terminated.
+(So a connection whose state variable is TIME_WAIT is not necessarily
+still in TIME_WAIT.) It should be noted that what is meant by a
+"connection" here is actually one side of a connection. If a
+connection is created from the local host to the local host, two
+connection ID's will be created.
+
+When writing an application to read from the proc interface, it should be
+taken into consideration that the directories and their files can disappear at
+any time (they do so at an interrupt level). So if a file open fails on a
+file you just looked up (say, with glob), that's probably normal and the
+program should handle it gracefully.
+
+Another seemingly strange thing that can happen is that stats for multiple
+connections with the same four-tuple can show up. No more than one of the
+connections may be in any state but CLOSED or TIME_WAIT. This behavior is
+correct, and should be handled as such.
+
+The algorithms governing the connection numbers are not yet final.
+Currently, for simplification, it is only possible to have 32768
+connections.
+
+Inside each connection directory is an identical set of files. One is
+spec-ascii, which contains the connection four-tuple in human-readable
+format. One can, for example, see all outgoing ssh connections by executing
+"grep ':22$' /proc/web100/*/spec-ascii" from the command prompt.
+
+The remaining files provide access to states of TCP-KIS variables in
+local host byte-order. Since the number, names, and contents of these
+files can and will change with releases, they are described in a
+header file -- /proc/web100/header. A file named spec, which contains the
+variables describing the connection's four-tuple, should be present
+for any release.
+
+The header file is in human-readable format as follows:
+ <version>
+
+ /<filename>
+ <varname> <offset> <type>
+ <varname> <offset> <type>
+ ...
+
+ /<filename>
+ ...
+The filename is the name of the file inside each connection directory. (The
+/ is prepended to make it clear it is a new file, not a new variable in the
+previous file. There is also an empty line before each filename.) Each
+file has an arbitrary number of variables, and there are an arbitrary number
+of files. The type is an integer, and is currently defined something like:
+
+ enum {
+ WEB100_TYPE_INTEGER,
+ WEB100_TYPE_INTEGER32,
+ WEB100_TYPE_IP_ADDRESS,
+ WEB100_TYPE_COUNTER32,
+ WEB100_TYPE_GAUGE32,
+ WEB100_TYPE_UNSIGNED32,
+ WEB100_TYPE_TIME_TICKS,
+ WEB100_TYPE_COUNTER64,
+ WEB100_TYPE_UNSIGNED16
+ };
+
+in the kernel source file fs/proc/web100.c. These correspond to
+MIB-II types. (RFC2578)
+
+To read variables, seek to the appropriate offset, then read the appropriate
+amount of data. (Length is implied by the type.) Multiple variables may be
+read with a single read, and will be read atomically when doing so.
+Currently, all variables are readable, but this may not be true in the
+future.
+
+To write variables, seek to the appropriate offset, and write the
+appropriate amount of data. Only a single variable may be written at one
+time. If variables must be atomically written, a variable should be used as
+a flag to signal that the write is done, and the kernel code depending on
+the variables should be written to handle this.
+
+See: http://www.web100.org
+Please send coments to prog@web100.org
+
+John Heffner, Matt Mathis, R. Reddy
+August 2000, Jan 2001
+
diff --git a/Documentation/web100/sysctl.txt b/Documentation/web100/sysctl.txt
new file mode 100644
index 0000000..8dd8e55
--- /dev/null
+++ b/Documentation/web100/sysctl.txt
@@ -0,0 +1,24 @@
+Web100 sysctl variables
+John Heffner <jheffner@psc.edu>
+October 10, 2002
+
+net.ipv4.WAD_FloydAIMD
+ This value is used for WAD_FloydAIMD by a connection when its KIS
+ variable is 0. This variable requires that private extenisons be
+ enabled.
+
+net.ipv4.WAD_IFQ
+ This value is used for WAD_IFQ by a connection when its KIS
+ variable is 0. This variable requires that Net100 extensions be
+ enabled.
+
+net.ipv4.WAD_MaxBurst
+ This value is used for WAD_MaxBurst by a connection when its KIS
+ variable is 0. This variable requires that Net100 extensions be
+ enabled.
+
+net.ipv4.web100_fperms
+ Sets the file permissions of the files in /proc/web100/*/
+
+net.ipv4.web100_gid
+ Sets the group of the files in /proc/web100/*/
diff --git a/Makefile b/Makefile
index 05e12a6..ee76ce9 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 32
-EXTRAVERSION =
+EXTRAVERSION = -web100
NAME = Man-Eating Seals of Antiquity
RHEL_MAJOR = 6
RHEL_MINOR = 8
diff --git a/fs/proc/Makefile b/fs/proc/Makefile
index 461c50c..be9b824 100644
--- a/fs/proc/Makefile
+++ b/fs/proc/Makefile
@@ -27,3 +27,4 @@ proc-$(CONFIG_PROC_VMCORE) += vmcore.o
proc-$(CONFIG_PROC_DEVICETREE) += proc_devtree.o
proc-$(CONFIG_PRINTK) += kmsg.o
proc-$(CONFIG_PROC_PAGE_MONITOR) += page.o
+proc-$(CONFIG_WEB100_STATS) += web100.o
diff --git a/fs/proc/root.c b/fs/proc/root.c
index f84ed68..dac3e7b 100644
--- a/fs/proc/root.c
+++ b/fs/proc/root.c
@@ -189,6 +189,10 @@ void __init proc_root_init(void)
proc_mkdir("bus", NULL);
proc_sys_init();
proc_vx_init();
+
+#ifdef CONFIG_WEB100_STATS
+ proc_web100_init();
+#endif
}
static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat
diff --git a/fs/proc/web100.c b/fs/proc/web100.c
new file mode 100644
index 0000000..6babde3
--- /dev/null
+++ b/fs/proc/web100.c
@@ -0,0 +1,1375 @@
+/*
+ * fs/proc/web100.c
+ *
+ * Copyright (C) 2001 Matt Mathis <mathis@psc.edu>
+ * Copyright (C) 2001 John Heffner <jheffner@psc.edu>
+ *
+ * The Web 100 project. See http://www.web100.org
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ */
+
+#include <linux/proc_fs.h>
+#include <net/sock.h>
+#include <net/tcp.h>
+#include <net/web100.h>
+#include <linux/init.h>
+#include <linux/sysctl.h>
+#include <linux/mount.h>
+
+#include "internal.h"
+
+#define WEB100MIB_BLOCK_SIZE PAGE_SIZE - 1024
+
+extern __u32 sysctl_wmem_default;
+extern __u32 sysctl_wmem_max;
+
+struct proc_dir_entry *proc_web100_dir;
+static struct proc_dir_entry *proc_web100_header;
+
+
+/*
+ * Web100 variable reading/writing
+ */
+
+enum web100_connection_inos {
+ PROC_CONN_SPEC_ASCII = 1,
+ PROC_CONN_SPEC,
+ PROC_CONN_READ,
+ PROC_CONN_TEST,
+ PROC_CONN_TUNE,
+ PROC_CONN_HIGH_INO /* Keep at the end */
+};
+
+enum {
+ WEB100_TYPE_INTEGER = 0,
+ WEB100_TYPE_INTEGER32,
+ WEB100_TYPE_INET_ADDRESS_IPV4,
+ WEB100_TYPE_IP_ADDRESS = WEB100_TYPE_INET_ADDRESS_IPV4, /* Depricated */
+ WEB100_TYPE_COUNTER32,
+ WEB100_TYPE_GAUGE32,
+ WEB100_TYPE_UNSIGNED32,
+ WEB100_TYPE_TIME_TICKS,
+ WEB100_TYPE_COUNTER64,
+ WEB100_TYPE_INET_PORT_NUMBER,
+ WEB100_TYPE_UNSIGNED16 = WEB100_TYPE_INET_PORT_NUMBER, /* Depricated */
+ WEB100_TYPE_INET_ADDRESS,
+ WEB100_TYPE_INET_ADDRESS_IPV6,
+};
+
+struct web100_var;
+typedef int (*web100_rwfunc_t)(void *buf, struct web100stats *stats,
+ struct web100_var *vp);
+
+/* The printed variable description should look something like this (in ASCII):
+ * varname offset type
+ * where offset is the offset into the file.
+ */
+struct web100_var {
+ char *name;
+ __u32 type;
+ int len;
+
+ web100_rwfunc_t read;
+ unsigned long read_data; /* read handler-specific data */
+
+ web100_rwfunc_t write;
+ unsigned long write_data; /* write handler-specific data */
+
+ struct web100_var *next;
+};
+
+struct web100_file {
+ int len;
+ char *name;
+ int low_ino;
+ mode_t mode;
+
+ struct web100_var *first_var;
+};
+
+#define F(name,ino,perm) { sizeof (name) - 1, (name), (ino), (perm), NULL }
+static struct web100_file web100_file_arr[] = {
+ F("spec-ascii", PROC_CONN_SPEC_ASCII, S_IFREG | S_IRUGO),
+ F("spec", PROC_CONN_SPEC, S_IFREG | S_IRUGO),
+ F("read", PROC_CONN_READ, 0),
+ F("test", PROC_CONN_TEST, 0),
+ F("tune", PROC_CONN_TUNE, 0),
+ F(NULL, 0, 0) };
+#undef F
+#define WEB100_FILE_ARR_SIZE (sizeof (web100_file_arr) / sizeof (struct web100_file))
+
+/* This works only if the array is built in the correct order. */
+static inline struct web100_file *web100_file_lookup(int ino) {
+ return &web100_file_arr[ino - 1];
+}
+
+static void add_var(struct web100_file *file, char *name, int type,
+ web100_rwfunc_t read, unsigned long read_data,
+ web100_rwfunc_t write, unsigned long write_data)
+{
+ struct web100_var *var;
+
+ /* Again, assuming add_var is only called at init. */
+ if ((var = kmalloc(sizeof (struct web100_var), GFP_KERNEL)) == NULL)
+ panic("No memory available for Web100 var.\n");
+
+ var->name = name;
+ var->type = type;
+ switch (type) {
+ case WEB100_TYPE_INET_PORT_NUMBER:
+ var->len = 2;
+ break;
+ case WEB100_TYPE_INTEGER:
+ case WEB100_TYPE_INTEGER32:
+ case WEB100_TYPE_COUNTER32:
+ case WEB100_TYPE_GAUGE32:
+ case WEB100_TYPE_UNSIGNED32:
+ case WEB100_TYPE_TIME_TICKS:
+ var->len = 4;
+ break;
+ case WEB100_TYPE_COUNTER64:
+ var->len = 8;
+ break;
+ case WEB100_TYPE_INET_ADDRESS:
+ var->len = 17;
+ break;
+ default:
+ printk("Web100: Warning: Adding variable of unknown type.\n");
+ var->len = 0;
+ }
+
+ var->read = read;
+ var->read_data = read_data;
+
+ var->write = write;
+ var->write_data = write_data;
+
+ var->next = file->first_var;
+ file->first_var = var;
+}
+
+
+/*
+ * proc filesystem routines
+ */
+
+static struct inode *proc_web100_make_inode(struct super_block *sb, int ino)
+{
+ struct inode *inode;
+
+ inode = new_inode(sb);
+ if (!inode)
+ goto out;
+
+ inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
+ inode->i_ino = ino;
+
+ inode->i_uid = 0;
+ inode->i_gid = 0;
+
+out:
+ return inode;
+}
+
+static inline ino_t ino_from_cid(int cid)
+{
+ return (cid << 8) | 0x80000000;
+}
+
+static inline ino_t ino_from_parts(ino_t dir_ino, __u16 low_ino)
+{
+ return (dir_ino & ~0xff) | low_ino;
+}
+
+static inline int cid_from_ino(ino_t ino)
+{
+ return (ino & 0x7fffff00) >> 8;
+}
+
+static inline int low_from_ino(ino_t ino)
+{
+ return ino & 0xff;
+}
+
+static int connection_file_open(struct inode *inode, struct file *file)
+{
+ int cid = cid_from_ino(inode->i_ino);
+ struct web100stats *stats;
+
+ read_lock_bh(&web100_linkage_lock);
+ stats = web100stats_lookup(cid);
+ if (stats == NULL || stats->wc_dead) {
+ read_unlock_bh(&web100_linkage_lock);
+ return -ENOENT;
+ }
+ web100_stats_use(stats);
+ read_unlock_bh(&web100_linkage_lock);
+
+ return 0;
+}
+
+static int connection_file_release(struct inode *inode, struct file *file)
+{
+ int cid = cid_from_ino(inode->i_ino);
+ struct web100stats *stats;
+
+ read_lock_bh(&web100_linkage_lock);
+ stats = web100stats_lookup(cid);
+ if (stats == NULL) {
+ read_unlock_bh(&web100_linkage_lock);
+ return -ENOENT;
+ }
+ read_unlock_bh(&web100_linkage_lock);
+ web100_stats_unuse(stats);
+
+ return 0;
+}
+
+/** /proc/web100/<connection>/<binary variable files> **/
+static ssize_t connection_file_rw(int read, struct file *file,
+ char *buf, size_t nbytes, loff_t *ppos)
+{
+ int low_ino = low_from_ino(file->f_dentry->d_inode->i_ino);
+ int cid = cid_from_ino(file->f_dentry->d_inode->i_ino);
+ struct web100stats *stats;
+ struct web100_file *fp;
+ struct web100_var *vp;
+ int pos;
+ int n;
+ int err;
+ web100_rwfunc_t rwfunc;
+ char *page;
+
+ /* We're only going to let them read one page at a time.
+ * We shouldn't ever read more than a page, anyway, though.
+ */
+ if (nbytes > PAGE_SIZE)
+ nbytes = PAGE_SIZE;
+
+ if (!access_ok(read ? VERIFY_WRITE : VERIFY_READ, buf, nbytes))
+ return -EFAULT;
+
+ if ((page = (char *)__get_free_page(GFP_KERNEL)) == NULL)
+ return -ENOMEM;
+
+ if (!read) {
+ if (copy_from_user(page, buf, nbytes))
+ return -EFAULT;
+ }
+
+ fp = web100_file_lookup(low_ino);
+ if (fp == NULL) {
+ printk("Unregistered Web100 file.\n");
+ return 0;
+ }
+
+ read_lock_bh(&web100_linkage_lock);
+ stats = web100stats_lookup(cid);
+ read_unlock_bh(&web100_linkage_lock);
+ if (stats == NULL)
+ return -ENOENT;
+
+ lock_sock(stats->wc_sk);
+
+ /* TODO: seek in constant time, not linear. -JWH */
+ pos = 0;
+ n = 0;
+ vp = fp->first_var;
+ while (vp && nbytes > n) {
+ if (pos > *ppos) {
+ err = -ESPIPE;
+ goto err_out;
+ }
+ if (pos == *ppos) {
+ if (vp->len > nbytes - n)
+ break;
+
+ if (read)
+ rwfunc = vp->read;
+ else
+ rwfunc = vp->write;
+ if (rwfunc == NULL) {
+ err = -EACCES;
+ goto err_out;
+ }
+
+ err = rwfunc(page + n, stats, vp);
+
+ if (err < 0)
+ goto err_out;
+ n += vp->len;
+ *ppos += vp->len;
+ }
+ pos += vp->len;
+ vp = vp->next;
+ }
+
+ release_sock(stats->wc_sk);
+
+ if (read) {
+ if (copy_to_user(buf, page, n))
+ return -EFAULT;
+ }
+ free_page((unsigned long)page);
+
+ return n;
+
+err_out:
+ release_sock(stats->wc_sk);
+
+ return err;
+}
+
+static ssize_t connection_file_read(struct file *file,
+ char *buf, size_t nbytes, loff_t *ppos)
+{
+ return connection_file_rw(1, file, buf, nbytes, ppos);
+}
+
+static ssize_t connection_file_write(struct file *file,
+ const char *buf, size_t nbytes, loff_t *ppos)
+{
+ return connection_file_rw(0, file, (char *)buf, nbytes, ppos);
+}
+
+static struct file_operations connection_file_fops = {
+ open: connection_file_open,
+ release: connection_file_release,
+ read: connection_file_read,
+ write: connection_file_write
+};
+
+
+static size_t v6addr_str(char *dest, short *addr)
+{
+ int start = -1, end = -1;
+ int i, j;
+ int pos;
+
+ /* Find longest subsequence of 0's in addr */
+ for (i = 0; i < 8; i++) {
+ if (addr[i] == 0) {
+ for (j = i + 1; addr[j] == 0 && j < 8; j++);
+ if (j - i > end - start) {
+ end = j;
+ start = i;
+ }
+ i = j;
+ }
+ }
+ if (end - start == 1)
+ start = -1;
+
+ pos = 0;
+ for (i = 0; i < 8; i++) {
+ if (i > 0)
+ pos += sprintf(dest + pos, ":");
+ if (i == start) {
+ pos += sprintf(dest + pos, ":");
+ i += end - start - 1;
+ } else {
+ pos += sprintf(dest + pos, "%hx", ntohs(addr[i]));
+ }
+ }
+
+ return pos;
+}
+
+/** /proc/web100/<connection>/spec_ascii **/
+static ssize_t connection_spec_ascii_read(struct file * file, char * buf,
+ size_t nbytes, loff_t *ppos)
+{
+ __u32 local_addr, remote_addr;
+ __u16 local_port, remote_port;
+ int cid;
+ struct web100stats *stats;
+ struct web100directs *vars;
+ char tmpbuf[100];
+ int len = 0;
+
+ if (*ppos != 0)
+ return 0;
+
+ cid = cid_from_ino(file->f_dentry->d_parent->d_inode->i_ino);
+
+ read_lock_bh(&web100_linkage_lock);
+ stats = web100stats_lookup(cid);
+ read_unlock_bh(&web100_linkage_lock);
+ if (stats == NULL)
+ return -ENOENT;
+ vars = &stats->wc_vars;
+
+ if (vars->LocalAddressType == WC_ADDRTYPE_IPV4) {
+ /* These values should not change while stats are linked.
+ * We don't need to lock the sock. */
+ local_addr = ntohl(vars->LocalAddress.v4addr);
+ remote_addr = ntohl(vars->RemAddress.v4addr);
+ local_port = vars->LocalPort;
+ remote_port = vars->RemPort;
+
+ len = sprintf(tmpbuf, "%d.%d.%d.%d:%d %d.%d.%d.%d:%d\n",
+ (local_addr >> 24) & 0xff,
+ (local_addr >> 16) & 0xff,
+ (local_addr >> 8) & 0xff,
+ local_addr & 0xff,
+ local_port,
+ (remote_addr >> 24) & 0xff,
+ (remote_addr >> 16) & 0xff,
+ (remote_addr >> 8) & 0xff,
+ remote_addr & 0xff,
+ remote_port);
+ } else if (vars->LocalAddressType == WC_ADDRTYPE_IPV6) {
+ local_port = vars->LocalPort;
+ remote_port = vars->RemPort;
+
+ len += v6addr_str(tmpbuf + len, (short *)&vars->LocalAddress.v6addr.addr);
+ len += sprintf(tmpbuf + len, ".%d ", local_port);
+ len += v6addr_str(tmpbuf + len, (short *)&vars->RemAddress.v6addr.addr);
+ len += sprintf(tmpbuf + len, ".%d\n", remote_port);
+ } else {
+ printk(KERN_ERR "connection_spec_ascii_read: LocalAddressType invalid\n");
+ return 0;
+ }
+
+ len = len > nbytes ? nbytes : len;
+ if (copy_to_user(buf, tmpbuf, len))
+ return -EFAULT;
+ *ppos += len;
+ return len;
+}
+
+static struct file_operations connection_spec_ascii_fops = {
+ open: connection_file_open,
+ release: connection_file_release,
+ read: connection_spec_ascii_read
+};
+
+
+/** /proc/web100/<connection>/ **/
+static int connection_dir_readdir(struct file *filp,
+ void *dirent, filldir_t filldir)
+{
+ int i;
+ struct inode *inode = filp->f_dentry->d_inode;
+ struct web100_file *p;
+
+ i = filp->f_pos;
+ switch (i) {
+ case 0:
+ if (filldir(dirent, ".", 1, i, inode->i_ino, DT_DIR) < 0)
+ return 0;
+ i++;
+ filp->f_pos++;
+ /* fall through */
+ case 1:
+ if (filldir(dirent, "..", 2, i, proc_web100_dir->low_ino, DT_DIR) < 0)
+ return 0;
+ i++;
+ filp->f_pos++;
+ /* fall through */
+ default:
+ i -= 2;
+ if (i >= WEB100_FILE_ARR_SIZE)
+ return 1;
+ p = &web100_file_arr[i];
+ while (p->name) {
+ if (filldir(dirent, p->name, p->len, filp->f_pos,
+ ino_from_parts(inode->i_ino, p->low_ino),
+ p->mode >> 12) < 0)
+ return 0;
+ filp->f_pos++;
+ p++;
+ }
+ }
+
+ return 1;
+}
+
+static struct dentry *connection_dir_lookup(struct inode *dir,
+ struct dentry *dentry, struct nameidata *nd)
+{
+ struct inode *inode;
+ struct web100_file *p;
+ struct web100stats *stats;
+ uid_t uid;
+
+ inode = NULL;
+ for (p = &web100_file_arr[0]; p->name; p++) {
+ if (p->len != dentry->d_name.len)
+ continue;
+ if (!memcmp(dentry->d_name.name, p->name, p->len))
+ break;
+ }
+ if (!p->name)
+ return ERR_PTR(-ENOENT);
+
+ read_lock_bh(&web100_linkage_lock);
+ if ((stats = web100stats_lookup(cid_from_ino(dir->i_ino))) == NULL) {
+ read_unlock_bh(&web100_linkage_lock);
+ printk("connection_dir_lookup: stats == NULL\n");
+ return ERR_PTR(-ENOENT);
+ }
+ uid = sock_i_uid(stats->wc_sk);
+ read_unlock_bh(&web100_linkage_lock);
+
+ inode = proc_web100_make_inode(dir->i_sb, ino_from_parts(dir->i_ino, p->low_ino));
+ if (!inode)
+ return ERR_PTR(-ENOMEM);
+ inode->i_mode = p->mode ? p->mode : S_IFREG | sysctl_web100_fperms;
+ inode->i_uid = uid;
+ inode->i_gid = sysctl_web100_gid;
+
+ switch (p->low_ino) {
+ case PROC_CONN_SPEC_ASCII:
+ inode->i_fop = &connection_spec_ascii_fops;
+ break;
+ case PROC_CONN_SPEC:
+ case PROC_CONN_READ:
+ case PROC_CONN_TEST:
+ case PROC_CONN_TUNE:
+ inode->i_fop = &connection_file_fops;
+ break;
+ default:
+ printk("Web100: impossible type (%d)\n", p->low_ino);
+ iput(inode);
+ return ERR_PTR(-EINVAL);
+ }
+
+ d_add(dentry, inode);
+ return NULL;
+}
+
+static struct inode_operations connection_dir_iops = {
+ .lookup = connection_dir_lookup
+};
+
+static struct file_operations connection_dir_fops = {
+ .readdir = connection_dir_readdir
+};
+
+
+/** /proc/web100/header **/
+static ssize_t header_read(struct file * file, char * buf,
+ size_t nbytes, loff_t *ppos)
+{
+ int len = 0;
+ loff_t offset;
+ char *tmpbuf;
+ struct web100_file *fp;
+ struct web100_var *vp;
+ int n, tmp;
+ int i;
+ int ret = 0;
+
+ /* We will assume the variable description list will not change
+ * after init. (True at least right now.) Otherwise, we would have
+ * to have a lock on it.
+ */
+
+ if ((tmpbuf = (char *)__get_free_page(GFP_KERNEL)) == NULL)
+ return -ENOMEM;
+
+ offset = sprintf(tmpbuf, "%s\n", web100_version_string);
+
+ for (i = 0; i < WEB100_FILE_ARR_SIZE; i++) {
+ int file_offset = 0;
+
+ if ((fp = &web100_file_arr[i]) == NULL)
+ continue;
+
+ if (fp->first_var == NULL)
+ continue;
+
+ offset += sprintf(tmpbuf + offset, "\n/%s\n", fp->name);
+
+ vp = fp->first_var;
+ while (vp) {
+ if (offset > WEB100MIB_BLOCK_SIZE) {
+ len += offset;
+ if (*ppos < len) {
+ n = min(offset, min_t(loff_t, nbytes, len - *ppos));
+ if (copy_to_user(buf, tmpbuf + max_t(loff_t, *ppos - len + offset, 0), n))
+ return -EFAULT;
+ buf += n;
+ if (nbytes == n) {
+ *ppos += n;
+ ret = n;
+ goto out;
+ }
+ }
+ offset = 0;
+ }
+
+ offset += sprintf(tmpbuf + offset, "%s %d %d %d\n",
+ vp->name, file_offset, vp->type, vp->len);
+ file_offset += vp->len;
+
+ vp = vp->next;
+ }
+ }
+ len += offset;
+ if (*ppos < len) {
+ n = min(offset, min_t(loff_t, nbytes, len - *ppos));
+ if (copy_to_user(buf, tmpbuf + max_t(loff_t, *ppos - len + offset, 0), n))
+ return -EFAULT;
+ if (nbytes <= len - *ppos) {
+ *ppos += nbytes;
+ ret = nbytes;
+ goto out;
+ } else {
+ tmp = len - *ppos;
+ *ppos = len;
+ ret = tmp;
+ goto out;
+ }
+ }
+
+out:
+ free_page((unsigned long)tmpbuf);
+ return ret;
+}
+
+static struct file_operations header_file_operations = {
+ read: header_read
+};
+
+
+/** /proc/web100/ **/
+#define FIRST_CONNECTION_ENTRY 256
+#define NUMBUF_LEN 11
+
+static int get_connection_list(int pos, int *cids, int max)
+{
+ struct web100stats *stats;
+ int n;
+
+ pos -= FIRST_CONNECTION_ENTRY;
+ n = 0;
+
+ read_lock_bh(&web100_linkage_lock);
+
+ stats = web100stats_first;
+ while (stats && n < max) {
+ if (!stats->wc_dead) {
+ if (pos <= 0)
+ cids[n++] = stats->wc_cid;
+ else
+ pos--;
+ }
+
+ stats = stats->wc_next;
+ }
+
+ read_unlock_bh(&web100_linkage_lock);
+
+ return n;
+}
+
+static int cid_to_str(int cid, char *buf)
+{
+ int len, tmp, i;
+
+ if (cid == 0) { /* a special case */
+ len = 1;
+ } else {
+ tmp = cid;
+ for (len = 0; len < NUMBUF_LEN - 1 && tmp > 0; len++)
+ tmp /= 10;
+ }
+
+ for (i = 0; i < len; i++) {
+ buf[len - i - 1] = '0' + (cid % 10);
+ cid /= 10;
+ }
+ buf[len] = '\0';
+
+ return len;
+}
+
+static int web100_dir_readdir(struct file *filp,
+ void *dirent, filldir_t filldir)
+{
+ int err;
+ unsigned n, i;
+ int *cids;
+ int len;
+ ino_t ino;
+ char name[NUMBUF_LEN];
+ int n_conns;
+
+ if (filp->f_pos < FIRST_CONNECTION_ENTRY) {
+ if ((err = proc_readdir(filp, dirent, filldir)) < 0)
+ return err;
+ filp->f_pos = FIRST_CONNECTION_ENTRY;
+ }
+ n_conns = WEB100_MAX_CONNS * 2;
+ do {
+ n_conns /= 2;
+ cids = kmalloc(n_conns * sizeof (int), GFP_KERNEL);
+ } while (cids == NULL && n_conns > 0);
+ if (cids == NULL)
+ return -ENOMEM;
+ n = get_connection_list(filp->f_pos, cids, n_conns);
+
+ for (i = 0; i < n; i++) {
+ ino = ino_from_cid(cids[i]);
+ len = cid_to_str(cids[i], name);
+ if (filldir(dirent, name, len, filp->f_pos,
+ ino, DT_DIR) < 0) {
+ break;
+ }
+ filp->f_pos++;
+ }
+
+ kfree(cids);
+
+ return 0;
+}
+
+static inline struct dentry *web100_dir_dent(void)
+{
+ struct qstr qstr;
+ struct pid_namespace *ns;
+
+ if ((ns = task_active_pid_ns(current)) == NULL)
+ return NULL;
+
+ if (ns->proc_mnt == NULL)
+ return NULL;
+
+ qstr.name = "web100";
+ qstr.len = 6;
+ qstr.hash = full_name_hash(qstr.name, qstr.len);
+
+ return d_lookup(ns->proc_mnt->mnt_sb->s_root, &qstr);
+}
+
+void web100_proc_nlink_update(nlink_t nlink)
+{
+ struct dentry *dent;
+
+ dent = web100_dir_dent();
+ if (dent)
+ dent->d_inode->i_nlink = nlink;
+ dput(dent);
+}
+
+int web100_proc_dointvec_update(ctl_table *ctl, int write,
+ void *buffer, size_t *lenp, loff_t *ppos)
+{
+ unsigned n, i;
+ int *cids;
+ int err;
+ struct qstr qstr;
+ struct dentry *web100_dent, *conn_dent, *dent;
+ struct inode *inode;
+ struct web100_file *p;
+ char name[NUMBUF_LEN];
+
+ if ((err = proc_dointvec(ctl, write, buffer, lenp, ppos)) != 0)
+ return err;
+
+ if ((web100_dent = web100_dir_dent()) == NULL)
+ return 0;
+
+ if ((cids = kmalloc(WEB100_MAX_CONNS * sizeof (int), GFP_KERNEL)) == NULL)