-
Notifications
You must be signed in to change notification settings - Fork 42
/
ctc_ctci.c
1552 lines (1257 loc) · 49.2 KB
/
ctc_ctci.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
/* CTC_CTCI.C (c) Copyright Roger Bowler, 2000-2012 */
/* (c) Copyright James A. Pierson, 2002-2009 */
/* (c) Copyright "Fish" (David B. Trout), 2002-2009 */
/* (c) Copyright Fritz Elfert, 2001-2009 */
/* Hercules IP Channel-to-Channel Support (CTCI) */
#include "hstdinc.h"
/* jbs 10/27/2007 added _SOLARIS_ silly typo fixed 01/18/08 when looked at this again */
#if !defined(__SOLARIS__)
#include "hercules.h"
#include "ctcadpt.h"
#include "tuntap.h"
#include "hercifc.h"
#include "opcode.h"
/* getopt dynamic linking kludge */
#include "herc_getopt.h"
#if defined(OPTION_W32_CTCI)
#include "tt32api.h"
#endif
/*-------------------------------------------------------------------*/
/* Ivan Warren 20040227 */
/* This table is used by channel.c to determine if a CCW code is an */
/* immediate command or not */
/* The tape is addressed in the DEVHND structure as 'DEVIMM immed' */
/* 0 : Command is NOT an immediate command */
/* 1 : Command is an immediate command */
/* Note : An immediate command is defined as a command which returns */
/* CE (channel end) during initialisation (that is, no data is */
/* actually transfered. In this case, IL is not indicated for a CCW */
/* Format 0 or for a CCW Format 1 when IL Suppression Mode is in */
/* effect */
/*-------------------------------------------------------------------*/
static BYTE CTCI_Immed_Commands[256]=
{ 0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
// ====================================================================
// Declarations
// ====================================================================
static void* CTCI_ReadThread( PCTCBLK pCTCBLK );
static int CTCI_EnqueueIPFrame( DEVBLK* pDEVBLK,
BYTE* pData, size_t iSize );
static int ParseArgs( DEVBLK* pDEVBLK, PCTCBLK pCTCBLK,
int argc, char** argv );
// --------------------------------------------------------------------
// Device Handler Information Block
// --------------------------------------------------------------------
DEVHND ctci_device_hndinfo =
{
&CTCI_Init, /* Device Initialisation */
&CTCI_ExecuteCCW, /* Device CCW execute */
&CTCI_Close, /* Device Close */
&CTCI_Query, /* Device Query */
NULL, /* Device Start channel pgm */
NULL, /* Device End channel pgm */
NULL, /* Device Resume channel pgm */
NULL, /* Device Suspend channel pgm */
NULL, /* Device Read */
NULL, /* Device Write */
NULL, /* Device Query used */
NULL, /* Device Reserve */
NULL, /* Device Release */
NULL, /* Device Attention */
CTCI_Immed_Commands, /* Immediate CCW Codes */
NULL, /* Signal Adapter Input */
NULL, /* Signal Adapter Output */
NULL, /* Hercules suspend */
NULL /* Hercules resume */
};
// ====================================================================
//
// ====================================================================
//
// CTCI_Init
//
#define CTC_DEVICES_IN_GROUP 2 // a read and write device
int CTCI_Init( DEVBLK* pDEVBLK, int argc, char *argv[] )
{
PCTCBLK pWrkCTCBLK = NULL; // Working CTCBLK
PCTCBLK pDevCTCBLK = NULL; // Device CTCBLK
int rc = 0; // Return code
int nIFType; // Interface type
int nIFFlags; // Interface flags
char thread_name[32]; // CTCI_ReadThread
nIFType = // Interface type
0
| IFF_TUN // ("TUN", not "tap")
| IFF_NO_PI // (no packet info)
;
nIFFlags = // Interface flags
0
| IFF_UP // (interface is being enabled)
| IFF_BROADCAST // (interface broadcast addr is valid)
;
#if defined( TUNTAP_IFF_RUNNING_NEEDED )
nIFFlags |= // ADDITIONAL Interface flags
0
| IFF_RUNNING // (interface is ALSO operational)
;
#endif /* defined( TUNTAP_IFF_RUNNING_NEEDED ) */
pDEVBLK->devtype = 0x3088;
// CTC is a group device
if(!group_device(pDEVBLK, CTC_DEVICES_IN_GROUP))
return 0;
// Housekeeping
pWrkCTCBLK = malloc( sizeof( CTCBLK ) );
if( !pWrkCTCBLK )
{
logmsg( _("HHCCT037E %4.4X: Unable to allocate CTCBLK\n"),
pDEVBLK->devnum );
return -1;
}
memset( pWrkCTCBLK, 0, sizeof( CTCBLK ) );
// Parse configuration file statement
if( ParseArgs( pDEVBLK, pWrkCTCBLK, argc, (char**)argv ) != 0 )
{
free( pWrkCTCBLK );
pWrkCTCBLK = NULL;
return -1;
}
// Allocate the device CTCBLK and copy parsed information.
pDevCTCBLK = malloc( sizeof( CTCBLK ) );
if( !pDevCTCBLK )
{
logmsg( _("HHCCT038E %4.4X: Unable to allocate CTCBLK\n"),
pDEVBLK->devnum );
free( pWrkCTCBLK );
pWrkCTCBLK = NULL;
return -1;
}
memcpy( pDevCTCBLK, pWrkCTCBLK, sizeof( CTCBLK ) );
// New format has only one device statement for both addresses
// We need to dynamically allocate the read device block
pDevCTCBLK->pDEVBLK[0] = pDEVBLK->group->memdev[0];
pDevCTCBLK->pDEVBLK[1] = pDEVBLK->group->memdev[1];
pDevCTCBLK->pDEVBLK[0]->dev_data = pDevCTCBLK;
pDevCTCBLK->pDEVBLK[1]->dev_data = pDevCTCBLK;
SetSIDInfo( pDevCTCBLK->pDEVBLK[0], 0x3088, 0x08, 0x3088, 0x01 );
SetSIDInfo( pDevCTCBLK->pDEVBLK[1], 0x3088, 0x08, 0x3088, 0x01 );
pDevCTCBLK->pDEVBLK[0]->ctctype = CTC_CTCI;
pDevCTCBLK->pDEVBLK[0]->ctcxmode = 1;
pDevCTCBLK->pDEVBLK[1]->ctctype = CTC_CTCI;
pDevCTCBLK->pDEVBLK[1]->ctcxmode = 1;
pDevCTCBLK->sMTU = atoi( pDevCTCBLK->szMTU );
pDevCTCBLK->iMaxFrameBufferSize = sizeof(pDevCTCBLK->bFrameBuffer);
initialize_lock( &pDevCTCBLK->Lock );
initialize_lock( &pDevCTCBLK->EventLock );
initialize_condition( &pDevCTCBLK->Event );
// Give both Herc devices a reasonable name...
strlcpy( pDevCTCBLK->pDEVBLK[0]->filename,
pDevCTCBLK->szTUNCharName,
sizeof( pDevCTCBLK->pDEVBLK[0]->filename ) );
strlcpy( pDevCTCBLK->pDEVBLK[1]->filename,
pDevCTCBLK->szTUNCharName,
sizeof( pDevCTCBLK->pDEVBLK[1]->filename ) );
rc = TUNTAP_CreateInterface( pDevCTCBLK->szTUNCharName,
IFF_TUN | IFF_NO_PI,
&pDevCTCBLK->fd,
pDevCTCBLK->szTUNDevName );
if( rc < 0 )
{
free( pWrkCTCBLK );
pWrkCTCBLK = NULL;
return -1;
}
else
{
logmsg(_("HHCCT073I %4.4X: TUN device %s opened\n"),
pDevCTCBLK->pDEVBLK[0]->devnum,
pDevCTCBLK->szTUNDevName);
}
#if defined(OPTION_W32_CTCI)
// Set the specified driver/dll i/o buffer sizes..
{
struct tt32ctl tt32ctl;
memset( &tt32ctl, 0, sizeof(tt32ctl) );
strlcpy( tt32ctl.tt32ctl_name, pDevCTCBLK->szTUNDevName, sizeof(tt32ctl.tt32ctl_name) );
tt32ctl.tt32ctl_devbuffsize = pDevCTCBLK->iKernBuff;
if( TUNTAP_IOCtl( pDevCTCBLK->fd, TT32SDEVBUFF, (char*)&tt32ctl ) != 0 )
{
logmsg( _("HHCCT074W TT32SDEVBUFF failed for device %s: %s.\n"),
pDevCTCBLK->szTUNDevName, strerror( errno ) );
}
tt32ctl.tt32ctl_iobuffsize = pDevCTCBLK->iIOBuff;
if( TUNTAP_IOCtl( pDevCTCBLK->fd, TT32SIOBUFF, (char*)&tt32ctl ) != 0 )
{
logmsg( _("HHCCT075W TT32SIOBUFF failed for device %s: %s.\n"),
pDevCTCBLK->szTUNDevName, strerror( errno ) );
}
}
#endif
#ifdef OPTION_TUNTAP_CLRIPADDR
VERIFY( TUNTAP_ClrIPAddr ( pDevCTCBLK->szTUNDevName ) == 0 );
#endif
#ifdef OPTION_TUNTAP_SETMACADDR
if( !pDevCTCBLK->szMACAddress[0] ) // (if MAC address unspecified)
{
in_addr_t wrk_guest_ip_addr;
MAC wrk_guest_mac_addr;
if ((in_addr_t)-1 != (wrk_guest_ip_addr = inet_addr( pDevCTCBLK->szGuestIPAddr )))
{
build_herc_iface_mac ( wrk_guest_mac_addr, (const BYTE*) &wrk_guest_ip_addr );
snprintf
(
pDevCTCBLK->szMACAddress, sizeof( pDevCTCBLK->szMACAddress ),
"%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X"
,wrk_guest_mac_addr[0]
,wrk_guest_mac_addr[1]
,wrk_guest_mac_addr[2]
,wrk_guest_mac_addr[3]
,wrk_guest_mac_addr[4]
,wrk_guest_mac_addr[5]
);
}
}
TRACE
(
"** CTCI_Init: %4.4X (%s): IP \"%s\" --> default MAC \"%s\"\n"
,pDevCTCBLK->pDEVBLK[0]->devnum
,pDevCTCBLK->szTUNDevName
,pDevCTCBLK->szGuestIPAddr
,pDevCTCBLK->szMACAddress
);
VERIFY( TUNTAP_SetMACAddr ( pDevCTCBLK->szTUNDevName, pDevCTCBLK->szMACAddress ) == 0 );
#endif
VERIFY( TUNTAP_SetIPAddr ( pDevCTCBLK->szTUNDevName, pDevCTCBLK->szDriveIPAddr ) == 0 );
VERIFY( TUNTAP_SetDestAddr( pDevCTCBLK->szTUNDevName, pDevCTCBLK->szGuestIPAddr ) == 0 );
#ifdef OPTION_TUNTAP_SETNETMASK
VERIFY( TUNTAP_SetNetMask ( pDevCTCBLK->szTUNDevName, pDevCTCBLK->szNetMask ) == 0 );
#endif
VERIFY( TUNTAP_SetMTU ( pDevCTCBLK->szTUNDevName, pDevCTCBLK->szMTU ) == 0 );
VERIFY( TUNTAP_SetFlags ( pDevCTCBLK->szTUNDevName, nIFFlags ) == 0 );
// Copy the fd to make panel.c happy
pDevCTCBLK->pDEVBLK[0]->fd =
pDevCTCBLK->pDEVBLK[1]->fd = pDevCTCBLK->fd;
snprintf(thread_name,sizeof(thread_name),"CTCI %4.4X ReadThread",pDEVBLK->devnum);
thread_name[sizeof(thread_name)-1]=0;
create_thread( &pDevCTCBLK->tid, JOINABLE, CTCI_ReadThread, pDevCTCBLK, thread_name );
pDevCTCBLK->pDEVBLK[0]->tid = pDevCTCBLK->tid;
pDevCTCBLK->pDEVBLK[1]->tid = pDevCTCBLK->tid;
free( pWrkCTCBLK );
pWrkCTCBLK = NULL;
return 0;
}
//
// CTCI_ExecuteCCW
//
void CTCI_ExecuteCCW( DEVBLK* pDEVBLK, BYTE bCode,
BYTE bFlags, BYTE bChained,
U16 sCount, BYTE bPrevCode,
int iCCWSeq, BYTE* pIOBuf,
BYTE* pMore, BYTE* pUnitStat,
U16* pResidual )
{
int iNum; // Number of bytes to move
BYTE bOpCode; // CCW opcode with modifier
// bits masked off
UNREFERENCED( bFlags );
UNREFERENCED( bChained );
UNREFERENCED( bPrevCode );
UNREFERENCED( iCCWSeq );
// Intervention required if the device file is not open
if( pDEVBLK->fd < 0 &&
!IS_CCW_SENSE( bCode ) &&
!IS_CCW_CONTROL( bCode ) )
{
pDEVBLK->sense[0] = SENSE_IR;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
return;
}
// Mask off the modifier bits in the CCW bOpCode
if( ( bCode & 0x07 ) == 0x07 )
bOpCode = 0x07;
else if( ( bCode & 0x03 ) == 0x02 )
bOpCode = 0x02;
else if( ( bCode & 0x0F ) == 0x0C )
bOpCode = 0x0C;
else if( ( bCode & 0x03 ) == 0x01 )
bOpCode = pDEVBLK->ctcxmode ? ( bCode & 0x83 ) : 0x01;
else if( ( bCode & 0x1F ) == 0x14 )
bOpCode = 0x14;
else if( ( bCode & 0x47 ) == 0x03 )
bOpCode = 0x03;
else if( ( bCode & 0xC7 ) == 0x43 )
bOpCode = 0x43;
else
bOpCode = bCode;
// Process depending on CCW bOpCode
switch (bOpCode)
{
case 0x01: // 0MMMMM01 WRITE
//------------------------------------------------------------
// WRITE
//------------------------------------------------------------
// Return normal status if CCW count is zero
if( sCount == 0 )
{
*pUnitStat = CSW_CE | CSW_DE;
break;
}
CTCI_Write( pDEVBLK, sCount, pIOBuf, pUnitStat, pResidual );
break;
case 0x81: // 1MMMMM01 WEOF
//------------------------------------------------------------
// WRITE EOF
//------------------------------------------------------------
// Return normal status
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0x02: // MMMMMM10 READ
case 0x0C: // MMMM1100 RDBACK
// -----------------------------------------------------------
// READ & READ BACKWARDS
// -----------------------------------------------------------
// Read data and set unit status and residual byte count
CTCI_Read( pDEVBLK, sCount, pIOBuf, pUnitStat, pResidual, pMore );
break;
case 0x07: // MMMMM111 CTL
// -----------------------------------------------------------
// CONTROL
// -----------------------------------------------------------
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0x03: // M0MMM011 NOP
// -----------------------------------------------------------
// CONTROL NO-OPERATON
// -----------------------------------------------------------
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0x43: // 00XXX011 SBM
// -----------------------------------------------------------
// SET BASIC MODE
// -----------------------------------------------------------
// Command reject if in basic mode
if( pDEVBLK->ctcxmode == 0 )
{
pDEVBLK->sense[0] = SENSE_CR;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
break;
}
// Reset extended mode and return normal status
pDEVBLK->ctcxmode = 0;
*pResidual = 0;
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0xC3: // 11000011 SEM
// -----------------------------------------------------------
// SET EXTENDED MODE
// -----------------------------------------------------------
pDEVBLK->ctcxmode = 1;
*pResidual = 0;
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0xE3: // 11100011
// -----------------------------------------------------------
// PREPARE (PREP)
// -----------------------------------------------------------
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0x14: // XXX10100 SCB
// -----------------------------------------------------------
// SENSE COMMAND BYTE
// -----------------------------------------------------------
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0x04: // 00000100 SENSE
// -----------------------------------------------------------
// SENSE
// -----------------------------------------------------------
// Command reject if in basic mode
if( pDEVBLK->ctcxmode == 0 )
{
pDEVBLK->sense[0] = SENSE_CR;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
break;
}
// Calculate residual byte count
iNum = ( sCount < pDEVBLK->numsense ) ?
sCount : pDEVBLK->numsense;
*pResidual = sCount - iNum;
if( sCount < pDEVBLK->numsense )
*pMore = 1;
// Copy device sense bytes to channel I/O buffer
memcpy( pIOBuf, pDEVBLK->sense, iNum );
// Clear the device sense bytes
memset( pDEVBLK->sense, 0, sizeof( pDEVBLK->sense ) );
// Return unit status
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0xE4: // 11100100 SID
// -----------------------------------------------------------
// SENSE ID
// -----------------------------------------------------------
// Calculate residual byte count
iNum = ( sCount < pDEVBLK->numdevid ) ?
sCount : pDEVBLK->numdevid;
*pResidual = sCount - iNum;
if( sCount < pDEVBLK->numdevid )
*pMore = 1;
// Copy device identifier bytes to channel I/O buffer
memcpy( pIOBuf, pDEVBLK->devid, iNum );
// Return unit status
*pUnitStat = CSW_CE | CSW_DE;
break;
default:
// ------------------------------------------------------------
// INVALID OPERATION
// ------------------------------------------------------------
// Set command reject sense byte, and unit check status
pDEVBLK->sense[0] = SENSE_CR;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
}
}
// -------------------------------------------------------------------
// CTCI_Close
// -------------------------------------------------------------------
int CTCI_Close( DEVBLK* pDEVBLK )
{
/* DEVBLK* pDEVBLK2; */
PCTCBLK pCTCBLK = (PCTCBLK)pDEVBLK->dev_data;
// Close the device file (if not already closed)
if( pCTCBLK->fd >= 0 )
{
// PROGRAMMING NOTE: there's currently no way to interrupt
// the "CTCI_ReadThread"s TUNTAP_Read of the adapter. Thus
// we must simply wait for CTCI_ReadThread to eventually
// notice that we're doing a close (via our setting of the
// fCloseInProgress flag). Its TUNTAP_Read will eventually
// timeout after a few seconds (currently 5, which is dif-
// ferent than the CTC_READ_TIMEOUT_SECS timeout value the
// CTCI_Read function uses) and will then do the close of
// the adapter for us (TUNTAP_Close) so we don't have to.
// All we need to do is ask it to exit (via our setting of
// the fCloseInProgress flag) and then wait for it to exit
// (which, as stated, could take up to a max of 5 seconds).
// All of this is simply because it's poor form to close a
// device from one thread while another thread is reading
// from it. Attempting to do so could trip a race condition
// wherein the internal i/o buffers used to process the
// read request could have been freed (by the close call)
// by the time the read request eventually gets serviced.
TID tid = pCTCBLK->tid;
pCTCBLK->fCloseInProgress = 1; // (ask read thread to exit)
signal_thread( tid, SIGUSR2 ); // (for non-Win32 platforms)
//FIXME signal_thread not working for non-MSVC platforms
#if defined(_MSVC_)
join_thread( tid, NULL ); // (wait for thread to end)
#endif
detach_thread( tid ); // (wait for thread to end)
}
pDEVBLK->fd = -1; // indicate we're now closed
return 0;
}
// -------------------------------------------------------------------
// CTCI_Query
// -------------------------------------------------------------------
void CTCI_Query( DEVBLK* pDEVBLK, char** ppszClass,
int iBufLen, char* pBuffer )
{
CTCBLK* pCTCBLK;
BEGIN_DEVICE_CLASS_QUERY( "CTCA", pDEVBLK, ppszClass, iBufLen, pBuffer );
pCTCBLK = (CTCBLK*) pDEVBLK->dev_data;
if(!pCTCBLK)
{
strlcpy(pBuffer,"*Uninitialized",iBufLen);
return;
}
snprintf( pBuffer, iBufLen, "CTCI %s/%s (%s)%s",
pCTCBLK->szGuestIPAddr,
pCTCBLK->szDriveIPAddr,
pCTCBLK->szTUNDevName,
pCTCBLK->fDebug ? " -d" : "" );
}
// -------------------------------------------------------------------
// CTCI_Read
// -------------------------------------------------------------------
//
// Once an IP frame is received by the Read Thread, it is enqueued
// on the device frame buffer for presentation to the host program.
// The residual byte count is set to indicate the amount of the buffer
// which was not filled.
//
// For details regarding the actual buffer layout, please refer to
// the comments preceding the CTCI_ReadThread function.
// Input:
// pDEVBLK A pointer to the CTC adapter device block
// sCount The I/O buffer length from the read CCW
// pIOBuf The I/O buffer from the read CCW
//
// Output:
// pUnitStat The CSW status (CE+DE or CE+DE+UC or CE+DE+UC+SM)
// pResidual The CSW residual byte count
// pMore Set to 1 if packet data exceeds CCW count
//
void CTCI_Read( DEVBLK* pDEVBLK, U16 sCount,
BYTE* pIOBuf, BYTE* pUnitStat,
U16* pResidual, BYTE* pMore )
{
PCTCBLK pCTCBLK = (PCTCBLK)pDEVBLK->dev_data;
PCTCIHDR pFrame = NULL;
size_t iLength = 0;
int rc = 0;
for ( ; ; )
{
obtain_lock( &pCTCBLK->Lock );
if( !pCTCBLK->fDataPending )
{
struct timespec waittime;
struct timeval now;
release_lock( &pCTCBLK->Lock );
gettimeofday( &now, NULL );
waittime.tv_sec = now.tv_sec + CTC_READ_TIMEOUT_SECS;
waittime.tv_nsec = now.tv_usec * 1000;
obtain_lock( &pCTCBLK->EventLock );
rc = timed_wait_condition( &pCTCBLK->Event,
&pCTCBLK->EventLock,
&waittime );
release_lock( &pCTCBLK->EventLock );
if( rc == ETIMEDOUT || rc == EINTR )
{
// check for halt condition
if( pDEVBLK->scsw.flag2 & SCSW2_FC_HALT ||
pDEVBLK->scsw.flag2 & SCSW2_FC_CLEAR )
{
if( pDEVBLK->ccwtrace || pDEVBLK->ccwstep )
logmsg( _("HHCCT040I %4.4X: Halt or Clear Recognized\n"),
pDEVBLK->devnum );
*pUnitStat = CSW_CE | CSW_DE;
*pResidual = sCount;
return;
}
continue;
}
obtain_lock( &pCTCBLK->Lock );
}
// Sanity check
if( pCTCBLK->iFrameOffset == 0 )
{
release_lock( &pCTCBLK->Lock );
continue;
}
// Fix-up frame pointer and terminate block
pFrame = (PCTCIHDR)( pCTCBLK->bFrameBuffer +
sizeof( CTCIHDR ) +
pCTCBLK->iFrameOffset );
STORE_HW( pFrame->hwOffset, 0x0000 );
// (fix for day-1 bug offered by Vince Weaver [[email protected]])
// iLength = pCTCBLK->iFrameOffset + sizeof( CTCIHDR ) + 2;
iLength = pCTCBLK->iFrameOffset + sizeof( CTCIHDR );
if( sCount < iLength )
{
*pMore = 1;
*pResidual = 0;
iLength = sCount;
}
else
{
*pMore = 0;
*pResidual -= iLength;
}
*pUnitStat = CSW_CE | CSW_DE;
memcpy( pIOBuf, pCTCBLK->bFrameBuffer, iLength );
if( pCTCBLK->fDebug )
{
logmsg( _("HHCCT041I %4.4X: CTC Received Frame (%d bytes):\n"),
pDEVBLK->devnum, iLength );
packet_trace( pCTCBLK->bFrameBuffer, iLength );
}
// Reset frame buffer
pCTCBLK->iFrameOffset = 0;
pCTCBLK->fDataPending = 0;
release_lock( &pCTCBLK->Lock );
return;
}
}
// -------------------------------------------------------------------
// CTCI_Write
// -------------------------------------------------------------------
//
// For details regarding the actual buffer layout, please refer to
// the comments preceding the CTCI_ReadThread function.
//
void CTCI_Write( DEVBLK* pDEVBLK, U16 sCount,
BYTE* pIOBuf, BYTE* pUnitStat,
U16* pResidual )
{
PCTCBLK pCTCBLK = (PCTCBLK)pDEVBLK->dev_data;
PCTCIHDR pFrame; // -> Frame header
PCTCISEG pSegment; // -> Segment in buffer
U16 sOffset; // Offset of next frame
U16 sSegLen; // Current segment length
U16 sDataLen; // Length of IP Frame data
int iPos; // Offset into buffer
U16 i; // Array subscript
int rc; // Return code
BYTE szStackID[33]; // VSE IP stack identity
U32 iStackCmd; // VSE IP stack command
// Check that CCW count is sufficient to contain block header
if( sCount < sizeof( CTCIHDR ) )
{
logmsg( _("HHCCT042E %4.4X: Write CCW count %u is invalid\n"),
pDEVBLK->devnum, sCount );
pDEVBLK->sense[0] = SENSE_DC;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
return;
}
// Fix-up frame pointer
pFrame = (PCTCIHDR)pIOBuf;
// Extract the frame length from the header
FETCH_HW( sOffset, pFrame->hwOffset );
// Check for special VSE TCP/IP stack command packet
if( sOffset == 0 && sCount == 40 )
{
// Extract the 32-byte stack identity string
for( i = 0;
i < sizeof( szStackID ) - 1 && i < sCount - 4;
i++)
szStackID[i] = guest_to_host( pIOBuf[i+4] );
szStackID[i] = '\0';
// Extract the stack command word
FETCH_FW( iStackCmd, *((FWORD*)&pIOBuf[36]) );
// Display stack command and discard the packet
logmsg( _("HHCCT043I %4.4X: Interface command: %s %8.8X\n"),
pDEVBLK->devnum, szStackID, iStackCmd );
*pUnitStat = CSW_CE | CSW_DE;
*pResidual = 0;
return;
}
// Check for special L/390 initialization packet
if( sOffset == 0 )
{
// Return normal status and discard the packet
*pUnitStat = CSW_CE | CSW_DE;
*pResidual = 0;
return;
}
#if 0
// Notes: It appears that TurboLinux has gotten sloppy in their
// ways. They are now giving us buffer sizes that are
// greater than the CCW count, but the segment size
// is within the count.
// Check that the frame offset is valid
if( sOffset < sizeof( CTCIHDR ) || sOffset > sCount )
{
logmsg( _("CTC101W %4.4X: Write buffer contains invalid "
"frame offset %u\n"),
pDEVBLK->devnum, sOffset );
pDEVBLK->sense[0] = SENSE_CR;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
return;
}
#endif
// Adjust the residual byte count
*pResidual -= sizeof( CTCIHDR );
// Process each segment in the buffer
for( iPos = sizeof( CTCIHDR );
iPos < sOffset;
iPos += sSegLen )
{
// Check that the segment is fully contained within the block
if( iPos + sizeof( CTCISEG ) > sOffset )
{
logmsg( _("HHCCT044E %4.4X: Write buffer contains incomplete "
"segment header at offset %4.4X\n"),
pDEVBLK->devnum, iPos );
pDEVBLK->sense[0] = SENSE_DC;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
return;
}
// Fix-up segment header in the I/O buffer
pSegment = (PCTCISEG)(pIOBuf + iPos);
// Extract the segment length from the segment header
FETCH_HW( sSegLen, pSegment->hwLength );
// Check that the segment length is valid
if( ( sSegLen < sizeof( CTCISEG ) ) ||
( iPos + sSegLen > sOffset ) ||
( iPos + sSegLen > sCount ) )
{
logmsg( _("HHCCT045E %4.4X: Write buffer contains invalid "
"segment length %u at offset %4.4X\n"),
pDEVBLK->devnum, sSegLen, iPos );
pDEVBLK->sense[0] = SENSE_DC;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
return;
}
// Calculate length of IP frame data
sDataLen = sSegLen - sizeof( CTCISEG );
// Trace the IP packet before sending to TUN device
if( pCTCBLK->fDebug )
{
logmsg( _("HHCCT046I %4.4X: Sending packet to %s:\n"),
pDEVBLK->devnum, pCTCBLK->szTUNDevName );
packet_trace( pSegment->bData, sDataLen );
}
// Write the IP packet to the TUN/TAP interface
rc = TUNTAP_Write( pCTCBLK->fd, pSegment->bData, sDataLen );
if( rc < 0 )
{
logmsg( _("HHCCT047E %4.4X: Error writing to %s: rc=%d errno=%d %s\n"),
pDEVBLK->devnum, pCTCBLK->szTUNDevName,
rc, errno, strerror(errno));
}
/* Kludge for Ubuntu 10.04 by Martin Truebner */
if (rc == -1 && errno == 22) rc = 0;
if (rc < 0)
{
pDEVBLK->sense[0] = SENSE_EC;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
return;
}
// Adjust the residual byte count
*pResidual -= sSegLen;
// We are done if current segment satisfies CCW count
if( iPos + sSegLen == sCount )
{
*pResidual -= sSegLen;
*pUnitStat = CSW_CE | CSW_DE;
return;
}
}
// Set unit status and residual byte count
*pUnitStat = CSW_CE | CSW_DE;
*pResidual = 0;
}
// --------------------------------------------------------------------
// CTCI_ReadThread
// --------------------------------------------------------------------
//
// When an IP frame is received from the TUN/TAP interface, the frame
// is enqueued on the device frame buffer.
//
// The device frame buffer is a chain of blocks. The first 2 bytes of
// a block (CTCIHDR) specify the offset in the buffer of the next block.
// The final block in indicated by a CTCIHDR offset value of 0x0000.
//
// Within each block, each IP frame is preceeded by a segment header
// (CTCISEG). This segment header has a 2 byte length field that
// specifies the length of the segment (including the segment header),
// a 2 byte frame type field (always 0x0800 = IPv4), and a 2 byte
// reserved area (always 0000), followed by the actual frame data.
//
// The CTCI_ReadThread reads the IP frame, then CTCI_EnqueueIPFrame
// function is called to add it to the frame buffer (which precedes
// each one with a CTCISEG and adjusts the block header (CTCIHDR)
// offset value as appropriate.
//
// Oddly, it is the CTCI_Read function (called by CCW processing in
// response to a guest SIO request) that adds the CTCIHDR with the
// 000 offset value marking the end of the buffer's chain of blocks,
// and not the CTCI_EnqueueIPFrame nor the CTCI_ReadThread as would
// be expected.
//
// Also note that the iFrameOffset field in the CTCI device's CTCBLK
// control block is the offset from the end of the buffer's first
// CTCIHDR to where the end-of-chain CTCIHDR is, and is identical to
// all of the queued CTCISEG's hwLength fields added together.
//
static void* CTCI_ReadThread( PCTCBLK pCTCBLK )
{
DEVBLK* pDEVBLK = pCTCBLK->pDEVBLK[0];
int iLength;
BYTE szBuff[2048];
// ZZ FIXME: Try to avoid race condition at startup with hercifc
SLEEP(10);
pCTCBLK->pid = getpid();
while( pCTCBLK->fd != -1 && !pCTCBLK->fCloseInProgress )
{
// Read frame from the TUN/TAP interface
iLength = TUNTAP_Read( pCTCBLK->fd, szBuff, sizeof(szBuff) );
// Check for error condition
if( iLength < 0 )
{
logmsg( _("HHCCT048E %4.4X: Error reading from %s: %s\n"),
pDEVBLK->devnum, pCTCBLK->szTUNDevName,
strerror( errno ) );
break;
}
if( iLength == 0 ) // (probably EINTR; ignore)
continue;
if( pCTCBLK->fDebug )
{
logmsg( _("HHCCT049I %4.4X: Received packet from %s (%d bytes):\n"),
pDEVBLK->devnum, pCTCBLK->szTUNDevName, iLength );
packet_trace( szBuff, iLength );
}
// Enqueue frame on buffer, if buffer is full, keep trying
while( CTCI_EnqueueIPFrame( pDEVBLK, szBuff, iLength ) < 0
&& pCTCBLK->fd != -1 && !pCTCBLK->fCloseInProgress )
{
if( EMSGSIZE == errno ) // (if too large for buffer)