-
Notifications
You must be signed in to change notification settings - Fork 8
/
minicache.c
1084 lines (1009 loc) · 32.1 KB
/
minicache.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
/*
* MiniCache Initialization & Event Loop
*
* Authors: Simon Kuenzer <[email protected]>
*
*
* Copyright (c) 2013-2017, NEC Europe Ltd., NEC Corporation All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON 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 ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <target/sys.h>
#include <target/netdev.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <sys/time.h>
#include <lwip/ip_addr.h>
#include <netif/etharp.h>
#include <lwip/netif.h>
#include <lwip/inet.h>
#include <lwip/tcp.h>
#include <lwip/tcp_impl.h>
#include <lwip/tcpip.h>
#include <lwip/dhcp.h>
#include <lwip/dns.h>
#include <lwip/ip_frag.h>
#include <lwip/init.h>
#include <lwip/stats.h>
#ifdef CONFIG_LWIP_IPDEV
#include <lwip/ip.h>
#endif
#include "likely.h"
#include "mempool.h"
#include "http.h"
#ifdef HAVE_SHELL
#include "shell.h"
#include "shell_extras.h"
#endif
#include "shfs.h"
#include "shfs_tools.h"
#ifdef HAVE_CTLDIR
#include <target/ctldir.h>
#endif
#ifdef SHFS_STATS
#include "shfs_stats.h"
#endif
#ifdef TESTSUITE
#include "testsuite.h"
#endif
#include "debug.h"
/* r = a - b on struct timeval */
#define TV_SUB(r, a, b) \
do { \
if ((a)->tv_usec < (b)->tv_usec) { \
(r)->tv_sec = (a)->tv_sec - (b)->tv_sec - 1; \
(r)->tv_usec = (a)->tv_usec - (b)->tv_usec + 1000000; \
} else { \
(r)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
(r)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
} \
} while(0)
/* runs (func) a command on a timeout */
#define TIMED(ms_now, ms_till, ms_next, ms_interval, func) \
do { \
if (unlikely((ms_next) <= (ms_now))) { \
(ms_next) = (ms_now) + (ms_interval); \
(func); \
} \
/* update ms_till only if current nextin \
* is smaller than the passed one */ \
(ms_till) = (ms_next) < (ms_till) ? (ms_next) : (ms_till); \
} while(0)
/* boot time tracing helper */
#ifdef TRACE_BOOTTIME
#define TT_DECLARE(var) uint64_t (var) = 0
#define TT_START(var) do { (var) = target_now_ns(); } while(0)
#define TT_END(var) do { (var) = (target_now_ns() - (var)); } while(0)
#define TT_PRINT(desc, var) \
printk(" %-32s: %"PRIu64".%06"PRIu64"s\n", \
(desc), \
(var) / 1000000000l, \
((var) / 1000l) % 1000000l);
#ifdef CONFIG_AUTOMOUNT
extern uint64_t shfs_tt_vbdopen;
#endif
#else /* TRACE_BOOTTIME */
#define TT_DECLARE(var) while(0) {}
#define TT_START(var) while(0) {}
#define TT_END(var) while(0) {}
#endif /* TRACE_BOOTTIME */
#ifdef CONFIG_MINDER_PRINT
#define MINDER_INTERVAL 500
static inline void minder_print(void)
{
static int minder_step = 0;
switch (minder_step) {
case 1:
printk("\r >))'> ");
minder_step = 2;
break;
case 2:
printk("\r >))'> ");
minder_step = 3;
break;
case 3:
printk("\r >))'> ");
minder_step = 4;
break;
case 4:
printk("\r >))'>");
minder_step = 5;
break;
case 5:
printk("\r <'((<");
minder_step = 6;
break;
case 6:
printk("\r <'((< ");
minder_step = 7;
break;
case 7:
printk("\r <'((< ");
minder_step = 8;
break;
case 8:
printk("\r <'((< ");
minder_step = 9;
break;
case 9:
printk("\r<'((< ");
minder_step = 0;
break;
default:
printk("\r>))'> ");
minder_step = 1;
}
fflush(stdout);
}
#endif /* CONFIG_MINDER_PRINT */
#ifdef CONFIG_DEBUG_PRINT
#define DEBUG_INTERVAL 1000
#if LWIP_STATS_DISPLAY
#include <lwip/stats.h>
#endif
static inline void debug_print(void)
{
static unsigned int debug_step = 0;
static int sys_cfd = -1;
static FILE *sys_cio;
#if LWIP_STATS_DISPLAY && MEMP_STATS
char * memp_names[] = {
#define LWIP_MEMPOOL(name,num,size,desc) desc,
#include <lwip/memp_std.h>
};
s16_t i;
#endif
/* open system in/out device on first call */
if (unlikely(sys_cfd < 0)) {
#ifdef __MINIOS__
sys_cfd = open("/var/log/", O_RDWR); /* workaround to
* access stdin/stdout */
if (sys_cfd < 0) {
printk("Could not open sysin/sysout\n");
return;
}
sys_cio = fdopen(sys_cfd, "r+");
#else
sys_cfd = 0;
sys_cio = stdin; /* FIXME */
#endif
}
printk("DEBUG[%u] --->>>\n", debug_step++);
#if LWIP_STATS_DISPLAY
#if LINK_STATS
printk("lwip.link.drop: %"STAT_COUNTER_F"\n", lwip_stats.link.drop);
printk("lwip.link.memerr: %"STAT_COUNTER_F"\n", lwip_stats.link.memerr);
printk("lwip.link.err: %"STAT_COUNTER_F"\n", lwip_stats.link.err);
#endif
#if IP_STATS
printk("lwip.ip.drop: %"STAT_COUNTER_F"\n", lwip_stats.ip.drop);
printk("lwip.ip.memerr: %"STAT_COUNTER_F"\n", lwip_stats.ip.memerr);
printk("lwip.ip.err: %"STAT_COUNTER_F"\n", lwip_stats.ip.err);
#endif
#if TCP_STATS
printk("lwip.tcp.drop: %"STAT_COUNTER_F"\n", lwip_stats.tcp.drop);
printk("lwip.tcp.memerr: %"STAT_COUNTER_F"\n", lwip_stats.tcp.memerr);
printk("lwip.tcp.err: %"STAT_COUNTER_F"\n", lwip_stats.tcp.err);
#endif
#if MEMP_STATS
for (i = 0; i < MEMP_MAX; i++) {
printk("lwip.memp.%s.err: %"U32_F"\n", memp_names[i], lwip_stats.memp[i].err);
}
#endif
#endif /* LWIP_STATS_DISPLAY */
#if SHFS_CACHE_STATS
printk("shfs.cache.hit: %"PRIu32"\n", shfs_cache_stat_get(hit));
printk("shfs.cache.hit+wait: %"PRIu32"\n", shfs_cache_stat_get(hitwait));
printk("shfs.cache.rdahead: %"PRIu32"\n", shfs_cache_stat_get(rdahead));
printk("shfs.cache.miss: %"PRIu32"\n", shfs_cache_stat_get(miss));
printk("shfs.cache.blank: %"PRIu32"\n", shfs_cache_stat_get(blank));
printk("shfs.cache.evict: %"PRIu32"\n", shfs_cache_stat_get(evict));
printk("shfs.cache.memerr: %"PRIu32"\n", shfs_cache_stat_get(memerr));
printk("shfs.cache.iosuc: %"PRIu32"\n", shfs_cache_stat_get(iosuc));
printk("shfs.cache.ioerr: %"PRIu32"\n", shfs_cache_stat_get(ioerr));
#endif /* SHFS_CACHE_STATS */
#ifdef HTTP_INFO
shcmd_http_info(sys_cio, 0, NULL);
#endif
printk("---<<<\n");
}
#endif /* CONFIG_DEBUG_PRINT */
#define MAX_NB_STATIC_ARP_ENTRIES 6
/**
* ARGUMENT PARSING
*/
struct mcargs {
int dhclient;
struct eth_addr mac;
ip4_addr_t ip;
ip4_addr_t mask;
ip4_addr_t gw;
#if LWIP_DNS
ip4_addr_t dns0;
ip4_addr_t dns1;
#endif
unsigned int nb_http_sess;
int bd_detect;
unsigned int nb_bds;
blkdev_id_t bd_id[MAX_NB_TRY_BLKDEVS];
int stats_bd;
blkdev_id_t stats_bd_id;
int no_ctldir;
int prefetch;
unsigned int startup_delay;
/* static arp entries can only be added if DHCP is disabled */
struct {
ip4_addr_t ip;
struct eth_addr mac;
} sarp_entry[MAX_NB_STATIC_ARP_ENTRIES];
unsigned int nb_sarp_entries;
} args;
static int parse_args_setval_cut(char delimiter, char **out_presnip, char **out_postsnip,
const char *buf)
{
size_t len = strlen(buf);
size_t p;
for (p = 0; p < len; ++p) {
if (buf[p] == delimiter) {
*out_presnip = strndup(buf, p);
*out_postsnip = strdup(&buf[p+1]);
if (!*out_presnip || !*out_postsnip) {
if (out_postsnip)
free(*out_postsnip);
if (out_presnip)
free(*out_presnip);
return -ENOMEM;
}
return 0;
}
}
return -1; /* delimiter not found */
}
static int parse_args_setval_ipv4cidr(ip4_addr_t *out_ip, ip4_addr_t *out_mask, const char *buf)
{
int ip0, ip1, ip2, ip3;
int rprefix;
uint32_t mask;
if (sscanf(buf, "%d.%d.%d.%d/%d", &ip0, &ip1, &ip2, &ip3, &rprefix) != 5)
return -1;
if ((ip0 < 0 || ip0 > 255) ||
(ip1 < 0 || ip1 > 255) ||
(ip2 < 0 || ip2 > 255) ||
(ip3 < 0 || ip3 > 255) ||
(rprefix < 0 || rprefix > 32))
return -1;
IP4_ADDR(out_ip, ip0, ip1, ip2, ip3);
if (rprefix == 0)
mask = 0x0;
else if (rprefix == 32)
mask = 0xFFFFFFFF;
else
mask = ~((1 << (32 - rprefix)) - 1);
IP4_ADDR(out_mask,
(mask & 0xFF000000) >> 24,
(mask & 0x00FF0000) >> 16,
(mask & 0x0000FF00) >> 8,
(mask & 0x000000FF));
return 0;
}
static int parse_args_setval_ipv4(ip4_addr_t *out, const char *buf)
{
int ip0, ip1, ip2, ip3;
if (sscanf(buf, "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3) != 4)
return -1;
if ((ip0 < 0 || ip0 > 255) ||
(ip1 < 0 || ip1 > 255) ||
(ip2 < 0 || ip2 > 255) ||
(ip3 < 0 || ip3 > 255))
return -1;
IP4_ADDR(out, ip0, ip1, ip2, ip3);
return 0;
}
static int parse_args_setval_hwaddr(struct eth_addr *out, const char *buf)
{
uint8_t hwaddr[6];
if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
&hwaddr[0], &hwaddr[1], &hwaddr[2],
&hwaddr[3], &hwaddr[4], &hwaddr[5]) != 6)
return -1;
out->addr[0] = hwaddr[0];
out->addr[1] = hwaddr[1];
out->addr[2] = hwaddr[2];
out->addr[3] = hwaddr[3];
out->addr[4] = hwaddr[4];
out->addr[5] = hwaddr[5];
return 0;
}
static int parse_args_setval_int(int *out, const char *buf)
{
if (sscanf(buf, "%d", out) != 1)
return -1;
return 0;
}
static int parse_args(int argc, char *argv[])
{
char *presnip;
char *postsnip;
int opt;
int ret;
int ival;
blkdev_id_t ibd;
/* default arguments */
memset(&args, 0, sizeof(args));
IP4_ADDR(&args.ip, 192, 168, 128, 124);
IP4_ADDR(&args.mask, 255, 255, 255, 252);
IP4_ADDR(&args.gw, 0, 0, 0, 0);
#if LWIP_DNS
IP4_ADDR(&args.dns0, 0, 0, 0, 0);
IP4_ADDR(&args.dns1, 0, 0, 0, 0);
#endif
args.nb_bds = 0;
args.stats_bd = 0; /* disable stats bd */
#ifdef CAN_DETECT_BLKDEVS
args.bd_detect = 1;
#else
args.bd_detect = 0;
#endif
args.dhclient = 1; /* dhcp as default */
args.startup_delay = 0;
args.no_ctldir = 0;
args.nb_http_sess = CONFIG_LWIP_NUM_TCPCON;
#if (!MEMP_MEM_MALLOC) && ((CONFIG_LWIP_NUM_TCPCON) < (MEMP_NUM_TCP_PCB))
#error "MEMP_NUM_TCP_PCB has to be a least CONFIG_LWIP_NUM_TCPCON"
#endif
args.nb_sarp_entries = 0;
args.prefetch = 0;
while ((opt = getopt(argc, argv,
"s:i:g:b:hc:a:P"
#if LWIP_DNS
"d:e:"
#endif
#ifdef SHFS_STATS
"x:"
#endif
)) != -1) {
switch(opt) {
case 's': /* startup delay */
ret = parse_args_setval_int(&ival, optarg);
if (ret < 0 || ival < 0) {
printk("invalid delay specified\n");
return -1;
}
args.startup_delay = (unsigned int) ival;
break;
case 'i': /* IP address/mask */
ret = parse_args_setval_ipv4cidr(&args.ip, &args.mask, optarg);
if (ret < 0) {
printk("invalid host IP in CIDR notation specified (e.g., 192.168.0.2/24)\n");
return -1;
}
args.dhclient = 0;
break;
case 'g': /* gateway */
ret = parse_args_setval_ipv4(&args.gw, optarg);
if (ret < 0) {
printk("invalid gateway IP specified (e.g., 192.168.0.1)\n");
return -1;
}
break;
#if LWIP_DNS
case 'd': /* dns0 */
ret = parse_args_setval_ipv4(&args.dns0, optarg);
if (ret < 0) {
printk("invalid primary DNS IP specified (e.g., 192.168.0.1)\n");
return -1;
}
break;
case 'e': /* dns1 */
ret = parse_args_setval_ipv4(&args.dns1, optarg);
if (ret < 0) {
printk("invalid secondary DNS IP specified (e.g., 192.168.0.1)\n");
return -1;
}
break;
#endif
case 'a': /* static arp entry */
if (args.nb_sarp_entries == (MAX_NB_STATIC_ARP_ENTRIES - 1)) {
printk("At most %d static ARP entries can be specified\n",
MAX_NB_STATIC_ARP_ENTRIES);
return -1;
}
ret = parse_args_setval_cut('/', &presnip, &postsnip, optarg);
if (ret < 0) {
if (ret == -ENOMEM)
printk("static ARP parsing error: Out of memory\n");
else
printk("invalid static ARP entry specified (e.g., 01:23:45:67:89:AB/192.168.0.1)\n");
return -1;
}
ret = parse_args_setval_hwaddr(&args.sarp_entry[args.nb_sarp_entries].mac, presnip);
if (ret < 0) {
printk("invalid static ARP entry specified (e.g., 01:23:45:67:89:AB/192.168.0.1)\n");
free(postsnip);
free(presnip);
return -1;
}
ret = parse_args_setval_ipv4(&args.sarp_entry[args.nb_sarp_entries].ip, postsnip);
if (ret < 0) {
printk("invalid static ARP entry specified (e.g., 01:23:45:67:89:AB/192.168.0.1)\n");
free(postsnip);
free(presnip);
return -1;
}
free(postsnip);
free(presnip);
args.nb_sarp_entries++;
break;
case 'P': /* prefetch */
args.prefetch = 1;
break;
case 'b': /* virtual block device (specified manually to skip detection) */
if (blkdev_id_parse(optarg, &ibd) < 0) {
printk("invalid block device id specified\n");
return -1;
}
if (args.nb_bds == sizeof(args.bd_id)) {
printk("only %u block devices can be specified\n", sizeof(args.bd_id));
return -1;
}
args.bd_detect = 0; /* disable bd detection */
blkdev_id_cpy(args.bd_id[args.nb_bds++], ibd);
break;
case 'h': /* hide xenstore control entries */
args.no_ctldir = 1;
break;
#ifdef SHFS_STATS
case 'x': /* virtual block device for exporting statistics */
if (blkdev_id_parse(optarg, &ibd) < 0) {
printk("invalid block device id specified\n");
return -1;
}
if (args.stats_bd) {
printk("only one stats devices can be specified\n");
return -1;
}
args.stats_bd = 1; /* enable stats bd */
blkdev_id_cpy(args.stats_bd_id, ibd);
break;
#endif
case 'c': /* number of http connections */
ret = parse_args_setval_int(&ival, optarg);
if (ret < 0 || ival < 1 || ival > CONFIG_LWIP_NUM_TCPCON) {
printk("at most %u http connections supported\n",
CONFIG_LWIP_NUM_TCPCON);
return -1;
}
args.nb_http_sess = ival;
break;
default:
return -1;
}
}
return 0;
}
/**
* SHUTDOWN/SUSPEND
*/
static volatile int shall_shutdown = 0;
static volatile int shall_reboot = 0;
static volatile int shall_suspend = 0;
#ifdef HAVE_SHELL
static int shcmd_halt(FILE *cio, int argc, char *argv[])
{
shall_reboot = 0;
shall_shutdown = 1;
return SH_CLOSE; /* special return code: closes the shell session */
}
static int shcmd_reboot(FILE *cio, int argc, char *argv[])
{
shall_reboot = 1;
shall_shutdown = 1;
return SH_CLOSE;
}
static int shcmd_suspend(FILE *cio, int argc, char *argv[])
{
shall_suspend = 1;
return 0;
}
#endif
void app_shutdown(unsigned reason)
{
switch (reason) {
case TARGET_SHTDN_POWEROFF:
printk("Poweroff requested\n");
shall_reboot = 0;
shall_shutdown = 1;
break;
case TARGET_SHTDN_REBOOT:
printk("Reboot requested\n");
shall_reboot = 1;
shall_shutdown = 1;
break;
case TARGET_SHTDN_SUSPEND:
printk("Suspend requested\n");
shall_suspend = 1;
break;
default:
printk("Unknown shutdown action requested: %d. Ignoring\n", reason);
break;
}
}
/**
* MAIN
*/
int main(int argc, char *argv[])
{
struct netif netif;
struct netif *niret;
#ifdef HAVE_CTLDIR
struct ctldir *cd = NULL;
#endif
int ret;
err_t err;
unsigned int i;
#if defined CONFIG_SELECT_POLL && defined CAN_POLL_BLKDEV && defined CAN_POLL_NETDEV
int poll_netif_fd;
fd_set poll_rfdset;
fd_set poll_wfdset;
struct timeval poll_to;
#endif
#if defined CONFIG_LWIP_NOTHREADS || defined CONFIG_MINDER_PRINT
uint64_t ts_now;
uint64_t ts_till;
uint64_t ts_to;
#endif
#ifdef CONFIG_LWIP_NOTHREADS
uint64_t ts_tcp = 0;
uint64_t ts_etharp = 0;
uint64_t ts_ipreass = 0;
#if LWIP_DNS
uint64_t ts_dns = 0;
#endif
uint64_t ts_dhcp_fine = 0;
uint64_t ts_dhcp_coarse = 0;
#endif /* CONFIG_LWIP_NOTHREADS */
#ifdef CONFIG_MINDER_PRINT
uint64_t ts_minder = 0;
#endif /* CONFIG_MINDER_PRINT */
#ifdef CONFIG_DEBUG_PRINT
uint64_t ts_debug = 0;
#endif /* CONFIG_DEBUG_PRINT */
TT_DECLARE(tt_boot);
TT_DECLARE(tt_netifadd);
TT_DECLARE(tt_lwipinit);
TT_DECLARE(tt_bddetect);
#ifdef CONFIG_AUTOMOUNT
TT_DECLARE(tt_automount);
#endif
TT_DECLARE(tt_ctldirstart);
#ifdef SHFS_STATS
TT_DECLARE(tt_statsdev);
#endif
target_init();
TT_START(tt_boot);
init_debug();
/* -----------------------------------
* banner
* ----------------------------------- */
#ifndef CONFIG_HIDE_BANNER
printk("\n");
printk("______ _______ ______________ ______ \n");
printk("___ |/ /__(_)_________(_)_ ____/_____ _________ /______ \n");
printk("__ /|_/ /__ /__ __ \\_ /_ / _ __ `/ ___/_ __ \\ _ \\\n");
printk("_ / / / _ / _ / / / / / /___ / /_/ // /__ _ / / / __/\n");
printk("/_/ /_/ /_/ /_/ /_//_/ \\____/ \\__,_/ \\___/ /_/ /_/\\___/ \n");
#ifdef CONFIG_BANNER_VERSION
printk("%61s\n", ""CONFIG_BANNER_VERSION"");
#endif
printk("\n");
printk("Copyright(C) 2013-2020 NEC Laboratories Europe GmbH\n");
printk("Authors: Simon Kuenzer <[email protected]>\n");
printk("\n");
#endif
/* -----------------------------------
* argument parsing
* ----------------------------------- */
if (parse_args(argc, argv) < 0) {
printk("Argument parsing error!\n" \
"Please check your arguments\n");
goto out;
}
if (args.startup_delay) {
unsigned int s;
printk("Startup delay");
fflush(stdout);
for (s = 0; s < args.startup_delay; ++s) {
printf(".");
fflush(stdout);
msleep(1000);
}
printk("\n");
}
/* -----------------------------------
* control dir - phase 1/2
* ----------------------------------- */
#ifdef HAVE_CTLDIR
if (!args.no_ctldir) {
printk("Initialize xenstore control entries...\n");
cd = create_ctldir("minicache");
if (!cd) {
printk("Warning: Could not initialize xenstore control entries: %s\n", strerror(errno));
printk(" Disabling xenstore cotrol entries\n");
}
}
#endif
/* -----------------------------------
* lwIP initialization
* ----------------------------------- */
printk("Starting networking...\n");
TT_START(tt_lwipinit);
#ifdef CONFIG_LWIP_NOTHREADS
lwip_init();
#else
tcpip_init(NULL, NULL);
#endif
TT_END(tt_lwipinit);
/* -----------------------------------
* network interface initialization
* ----------------------------------- */
printk("Initialize network interface: ");
if (args.dhclient)
printk("DHCP...\n");
else
printk("%u.%u.%u.%u netmask %u.%u.%u.%u gw %u.%u.%u.%u...\n",
ip4_addr1(&args.ip), ip4_addr2(&args.ip), ip4_addr3(&args.ip), ip4_addr4(&args.ip),
ip4_addr1(&args.mask), ip4_addr2(&args.mask), ip4_addr3(&args.mask), ip4_addr4(&args.mask),
ip4_addr1(&args.gw), ip4_addr2(&args.gw), ip4_addr3(&args.gw), ip4_addr4(&args.gw));
TT_START(tt_netifadd);
/* NOTE: IP-level devices are currently only
* supported in non-threaded env */
#ifdef CONFIG_LWIP_NOTHREADS
#ifdef CONFIG_LWIP_IPDEV
niret = netif_add(&netif, &args.ip, &args.mask, &args.gw, NULL,
target_netif_init, ip4_input);
#else
niret = netif_add(&netif, &args.ip, &args.mask, &args.gw, NULL,
target_netif_init, ethernet_input);
#endif
#else /* CONFIG_LWIP_NOTHREADS */
niret = netif_add(&netif, &args.ip, &args.mask, &args.gw, NULL,
target_netif_init, tcpip_input);
#endif /* CONFIG_LWIP_NOTHREADS */
TT_END(tt_netifadd);
/* device init function is user-defined
* use ip_input instead of ethernet_input for non-ethernet hardware
* (this function is assigned to netif.input and should be called by
* the hardware driver) */
/*
* The final parameter input is the function that a driver will
* call when it has received a new packet. This parameter
* typically takes one of the following values:
* ethernet_input: If you are not using a threaded environment
* and the driver should use ARP (such as for
* an Ethernet device), the driver will call
* this function which permits ARP packets to
* be handled, as well as IP packets.
* ip_input: If you are not using a threaded environment
* and the interface is not an Ethernet device,
* the driver will directly call the IP stack.
* tcpip_ethinput: If you are using the tcpip application thread
* (see lwIP and threads), the driver uses ARP,
* and has defined the ETHARP_TCPIP_ETHINPUT lwIP
* option. This function is used for drivers that
* passes all IP and ARP packets to the input function.
* tcpip_input: If you are using the tcpip application thread
* and have defined ETHARP_TCPIP_INPUT option.
* This function is used for drivers that pass
* only IP packets to the input function.
* (The driver probably separates out ARP packets
* and passes these directly to the ARP module).
* (Someone please recheck this: in lwip 1.4.1
* there is no tcpip_ethinput() ; tcp_input()
* handles ARP packets as well).
*/
if (!niret) {
printk("FATAL: Could not initialize the network interface\n");
goto out;
}
netif_set_default(&netif);
netif_set_up(&netif);
#if defined CONFIG_SELECT_POLL && defined CAN_POLL_BLKDEV && defined CAN_POLL_NETDEV
poll_netif_fd = target_netif_fd(&netif);
#endif
if (args.dhclient) {
printk("Starting DHCP client (background)...\n");
dhcp_start(&netif);
} else {
for (i = 0; i < args.nb_sarp_entries; ++i) {
err = etharp_add_static_entry(&args.sarp_entry[i].ip, &args.sarp_entry[i].mac);
if (err != ERR_OK) {
printk("Could not add static ARP entry: %02x:%02x:%02x:%02x:%02x:%02x\n",
args.sarp_entry[i].mac.addr[0],
args.sarp_entry[i].mac.addr[1],
args.sarp_entry[i].mac.addr[2],
args.sarp_entry[i].mac.addr[3],
args.sarp_entry[i].mac.addr[4],
args.sarp_entry[i].mac.addr[5]);
}
}
}
/* -----------------------------------
* detect available block devices
* ----------------------------------- */
#ifdef CAN_DETECT_BLKDEVS
if (args.bd_detect) {
printk("Detecting block devices...\n");
TT_START(tt_bddetect);
args.nb_bds = detect_blkdevs(args.bd_id, sizeof(args.bd_id));
TT_END(tt_bddetect);
}
#endif
/* -----------------------------------
* filesystem initialization & automount
* ----------------------------------- */
printk("Loading SHFS...\n");
init_shfs();
#ifdef CONFIG_AUTOMOUNT
if (args.nb_bds) {
printk("Automount cache filesystem...\n");
TT_START(tt_automount);
ret = mount_shfs(args.bd_id, args.nb_bds);
TT_END(tt_automount);
if (ret < 0)
printk("Warning: Could not find or mount a cache filesystem\n");
}
#endif
/* -----------------------------------
* service initialization
* ----------------------------------- */
#ifdef HAVE_SHELL
printk("Starting shell...\n");
init_shell(0, 4); /* no local session + 4 telnet sessions */
#ifdef HAVE_CTLDIR
register_shell_extras(cd); /* Note: cd might be NULL */
#else
register_shell_extras();
#endif
#endif
printk("Starting HTTP server (max number of connections: %u)...\n",
args.nb_http_sess);
init_http(args.nb_http_sess,
args.nb_http_sess << 1); /* nb reqs have to be at least double to
* ensure all connections can be used simultaneously */
/* add custom commands to the shell */
#ifdef HAVE_SHELL
shell_register_cmd("halt", shcmd_halt);
shell_register_cmd("reboot", shcmd_reboot);
shell_register_cmd("suspend", shcmd_suspend);
#ifdef HAVE_CTLDIR
register_shfs_tools(cd); /* Note: cd might be NULL */
#else
register_shfs_tools();
#endif
#endif
#ifdef SHFS_STATS
/* -----------------------------------
* stats device
* ----------------------------------- */
printk("Initializing stats device...\n");
if(args.stats_bd) {
TT_START(tt_statsdev);
ret = init_shfs_stats_export(args.stats_bd_id);
TT_END(tt_statsdev);
if (ret < 0) {
printk("Warning: Could not open stats device: %s\n", strerror(-ret));
args.stats_bd = 0;
}
}
#ifdef HAVE_CTLDIR
register_shfs_stats_tools(cd); /* Note: cd might be NULL */
#else
register_shfs_stats_tools();
#endif
#endif /* SHFS_STATS */
/* -----------------------------------
* testsuite commands
* ----------------------------------- */
#ifdef TESTSUITE
#ifdef HAVE_CTLDIR
register_testsuite(cd); /* Note: cd might be NULL */
#else
register_testsuite();
#endif
#endif
/* -----------------------------------
* control dir - phase 2/2
* ----------------------------------- */
#ifdef HAVE_CTLDIR
if (cd) {
printk("Registering xenstore control entries...\n");
TT_START(tt_ctldirstart);
ret = ctldir_start_watcher(cd);
TT_END(tt_ctldirstart);
if (ret < 0) {
printk("FATAL: Could not register xenstore control entries: %s\n", strerror(-ret));
goto out;
}
}
#endif
/* -----------------------------------
* Initialize select/poll
* ----------------------------------- */
#if defined CONFIG_SELECT_POLL && defined CAN_POLL_BLKDEV && defined CAN_POLL_NETDEV
FD_ZERO(&poll_rfdset);
FD_ZERO(&poll_wfdset);
ts_to = 0;
#endif
/* -----------------------------------
* Boot banner/time trace output
* ----------------------------------- */
printk("*** MiniCache is up and running ***\n");
#ifdef TRACE_BOOTTIME
TT_END(tt_boot);
TT_PRINT("boot time since invoking main", tt_boot);
TT_PRINT("lwip initialization", tt_lwipinit);
TT_PRINT("vif addition", tt_netifadd);
if (args.bd_detect)
TT_PRINT("vbd detection", tt_bddetect);
#ifdef CONFIG_AUTOMOUNT
if (args.nb_bds) {
tt_automount -= shfs_tt_vbdopen;
TT_PRINT("vbd open", shfs_tt_vbdopen);
TT_PRINT("file system mount time", tt_automount);
}
#endif
#ifdef SHFS_STATS
if (args.stats_bd)
TT_PRINT("stats device initialization", tt_statsdev);
#endif
#ifdef HAVE_CTLDIR
if (cd)
TT_PRINT("xenstore registration", tt_ctldirstart);
#endif
printk("***\n");
#endif /* TRACE_BOOTTIME */
#ifdef CONFIG_MINDER_PRINT
printk("\n");
#endif
#ifdef __MINIOS__
/* -----------------------------------
* Prefetch data to cache (after 250ms)
* ----------------------------------- */
if (args.prefetch)
shfs_prefetch_bgnd(250);
#endif
/* -----------------------------------
* Processing loop
* ----------------------------------- */
while(likely(!shall_shutdown)) {
#if defined CONFIG_SELECT_POLL && defined CAN_POLL_BLKDEV && defined CAN_POLL_NETDEV
/* select with ignoring return reason */
FD_SET(poll_netif_fd, &poll_rfdset);
#if defined CONFIG_LWIP_NOTHREADS || defined CONFIG_MINDER_PRINT
if (likely(ts_to)) {
poll_to.tv_sec = ts_to / 1000;
poll_to.tv_usec = (ts_to % 1000) * 1000;
#else
poll_to.tv_sec = 0;
poll_to.tv_usec = 0;
#endif
if (shfs_mounted) {
/* poll network and block devices */
shfs_blkdevs_fdset(&poll_rfdset);
select(max(shfs_vol.members_maxfd, poll_netif_fd) + 1,
&poll_rfdset, &poll_wfdset, NULL, &poll_to);
} else {
/* poll network only */
select(poll_netif_fd + 1, &poll_rfdset, NULL, NULL, &poll_to);
}
#if defined CONFIG_LWIP_NOTHREADS || defined CONFIG_MINDER_PRINT
}
#endif
#else
schedule(); /* yield CPU */
#endif
/* poll block devices */
shfs_poll_blkdevs();