-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdLinkXeq.cc
1221 lines (1041 loc) · 38 KB
/
XrdLinkXeq.cc
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
/******************************************************************************/
/* */
/* X r d L i n k X e q . c c */
/* */
/* (c) 2018 by the Board of Trustees of the Leland Stanford, Jr., University */
/* Produced by Andrew Hanushevsky for Stanford University under contract */
/* DE-AC02-76-SFO0515 with the Department of Energy */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/******************************************************************************/
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/uio.h>
#ifdef __linux__
#include <netinet/tcp.h>
#if !defined(TCP_CORK)
#undef HAVE_SENDFILE
#endif
#endif
#ifdef HAVE_SENDFILE
#ifndef __APPLE__
#include <sys/sendfile.h>
#endif
#endif
#include "XrdSys/XrdSysAtomics.hh"
#include "XrdSys/XrdSysError.hh"
#include "XrdSys/XrdSysFD.hh"
#include "XrdSys/XrdSysPlatform.hh"
#include "Xrd/XrdBuffer.hh"
#include "Xrd/XrdLink.hh"
#include "Xrd/XrdLinkCtl.hh"
#include "Xrd/XrdLinkXeq.hh"
#include "Xrd/XrdPoll.hh"
#include "Xrd/XrdScheduler.hh"
#include "Xrd/XrdSendQ.hh"
#define TRACELINK this
#include "Xrd/XrdTrace.hh"
/******************************************************************************/
/* G l o b a l s */
/******************************************************************************/
namespace XrdGlobal
{
extern XrdSysError Log;
extern XrdOucTrace XrdTrace;
extern XrdScheduler Sched;
extern XrdTlsContext *tlsCtx;
extern int devNull;
};
using namespace XrdGlobal;
/******************************************************************************/
/* S t a t i c s */
/******************************************************************************/
const char *XrdLinkXeq::TraceID = "LinkXeq";
long long XrdLinkXeq::LinkBytesIn = 0;
long long XrdLinkXeq::LinkBytesOut = 0;
long long XrdLinkXeq::LinkConTime = 0;
long long XrdLinkXeq::LinkCountTot = 0;
int XrdLinkXeq::LinkCount = 0;
int XrdLinkXeq::LinkCountMax = 0;
int XrdLinkXeq::LinkTimeOuts = 0;
int XrdLinkXeq::LinkStalls = 0;
int XrdLinkXeq::LinkSfIntr = 0;
XrdSysMutex XrdLinkXeq::statsMutex;
/******************************************************************************/
/* C o n s t r u c t o r */
/******************************************************************************/
XrdLinkXeq::XrdLinkXeq() : XrdLink(*this), PollInfo((XrdLink *)this)
{
XrdLinkXeq::Reset();
}
void XrdLinkXeq::Reset()
{
Uname[sizeof(Uname)-1] = '@';
Uname[sizeof(Uname)-2] = '?';
Lname[0] = '?';
Lname[1] = '\0';
ID = &Uname[sizeof(Uname)-2];
sendQ = 0;
stallCnt = stallCntTot = 0;
tardyCnt = tardyCntTot = 0;
SfIntr = 0;
isIdle = 0;
BytesOut = BytesIn = BytesOutTot = BytesInTot = 0;
LockReads= false;
KeepFD = false;
Protocol = 0;
ProtoAlt = 0;
PollInfo.Zorch();
ResetLink();
}
/******************************************************************************/
/* B a c k l o g */
/******************************************************************************/
int XrdLinkXeq::Backlog()
{
XrdSysMutexHelper lck(wrMutex);
// Return backlog information
//
return (sendQ ? sendQ->Backlog() : 0);
}
/******************************************************************************/
/* C l i e n t */
/******************************************************************************/
int XrdLinkXeq::Client(char *nbuf, int nbsz)
{
int ulen;
// Generate full client name
//
if (nbsz <= 0) return 0;
ulen = (Lname - ID);
if ((ulen + HNlen) >= nbsz) ulen = 0;
else {strncpy(nbuf, ID, ulen);
strcpy(nbuf+ulen, HostName);
ulen += HNlen;
}
return ulen;
}
/******************************************************************************/
/* C l o s e */
/******************************************************************************/
int XrdLinkXeq::Close(bool defer)
{ XrdSysMutexHelper opHelper(opMutex);
int csec, fd, rc = 0;
// Cleanup TLS if it is active
//
if (isTLS) tlsIO.Shutdown();
// If a defer close is requested, we can close the descriptor but we must
// keep the slot number to prevent a new client getting the same fd number.
// Linux is peculiar in that any in-progress operations will remain in that
// state even after the FD is closed unless there is some activity either on
// the connection or an event occurs that causes an operation restart. We
// portably solve this problem by issuing a shutdown() on the socket prior
// closing it. On most platforms, this informs readers that the connection is
// gone (though not on old (i.e. <= 2.3) versions of Linux, sigh). Also, if
// nonblocking mode is enabled, we need to do this in a separate thread as
// a shutdown may block for a pretty long time is lot of messages are queued.
// We will ask the SendQ object to schedule the shutdown for us before it
// commits suicide.
// Note that we can hold the opMutex while we also get the wrMutex.
//
if (defer)
{if (!sendQ) Shutdown(false);
else {TRACEI(DEBUG, "Shutdown FD only via SendQ");
InUse++;
FD = -FD;
wrMutex.Lock();
sendQ->Terminate(this);
sendQ = 0;
wrMutex.UnLock();
}
return 0;
}
// If we got here then this is not a defered close so we just need to check
// if there is a sendq appendage we need to get rid of.
//
if (sendQ)
{wrMutex.Lock();
sendQ->Terminate();
sendQ = 0;
wrMutex.UnLock();
}
// Multiple protocols may be bound to this link. If it is in use, defer the
// actual close until the use count drops to one.
//
while(InUse > 1)
{opHelper.UnLock();
TRACEI(DEBUG, "Close defered, use count=" <<InUse);
Serialize();
opHelper.Lock(&opMutex);
}
InUse--;
Instance = 0;
// Add up the statistic for this link
//
syncStats(&csec);
// Clean this link up
//
if (Protocol) {Protocol->Recycle(this, csec, Etext); Protocol = 0;}
if (ProtoAlt) {ProtoAlt->Recycle(this, csec, Etext); ProtoAlt = 0;}
if (Etext) {free(Etext); Etext = 0;}
InUse = 0;
// At this point we can have no lock conflicts, so if someone is waiting for
// us to terminate let them know about it. Note that we will get the condvar
// mutex while we hold the opMutex. This is the required order! We will also
// zero out the pointer to the condvar while holding the opmutex.
//
if (KillcvP) {KillcvP-> Lock(); KillcvP->Signal();
KillcvP->UnLock(); KillcvP = 0;
}
// Remove ourselves from the poll table and then from the Link table. We may
// not hold on to the opMutex when we acquire the LTMutex. However, the link
// table needs to be cleaned up prior to actually closing the socket. So, we
// do some fancy footwork to prevent multiple closes of this link.
//
fd = (FD < 0 ? -FD : FD);
if (FD != -1)
{if (PollInfo.Poller) {XrdPoll::Detach(this); PollInfo.Poller = 0;}
FD = -1;
opHelper.UnLock();
XrdLinkCtl::Unhook(fd);
} else opHelper.UnLock();
// Close the file descriptor if it isn't being shared. Do it as the last
// thing because closes and accepts and not interlocked.
//
if (fd >= 2) {if (KeepFD) rc = 0;
else rc = (close(fd) < 0 ? errno : 0);
}
if (rc) Log.Emsg("Link", rc, "close", ID);
return rc;
}
/******************************************************************************/
/* D o I t */
/******************************************************************************/
void XrdLinkXeq::DoIt()
{
int rc;
// The Process() return code tells us what to do:
// < 0 -> Stop getting requests,
// -EINPROGRESS leave link disabled but otherwise all is well
// -n Error, disable and close the link
// = 0 -> OK, get next request, if allowed, o/w enable the link
// > 0 -> Slow link, stop getting requests and enable the link
//
if (Protocol)
do {rc = Protocol->Process(this);} while (!rc && Sched.canStick());
else {Log.Emsg("Link", "Dispatch on closed link", ID);
return;
}
// Either re-enable the link and cycle back waiting for a new request, leave
// disabled, or terminate the connection.
//
if (rc >= 0)
{if (PollInfo.Poller && !PollInfo.Poller->Enable(this)) Close();}
else if (rc != -EINPROGRESS) Close();
}
/******************************************************************************/
/* P e e k */
/******************************************************************************/
int XrdLinkXeq::Peek(char *Buff, int Blen, int timeout)
{
XrdSysMutexHelper theMutex;
struct pollfd polltab = {FD, POLLIN|POLLRDNORM, 0};
ssize_t mlen;
int retc;
// Lock the read mutex if we need to, the helper will unlock it upon exit
//
if (LockReads) theMutex.Lock(&rdMutex);
// Wait until we can actually read something
//
isIdle = 0;
do {retc = poll(&polltab, 1, timeout);} while(retc < 0 && errno == EINTR);
if (retc != 1)
{if (retc == 0) return 0;
return Log.Emsg("Link", -errno, "poll", ID);
}
// Verify it is safe to read now
//
if (!(polltab.revents & (POLLIN|POLLRDNORM)))
{Log.Emsg("Link", XrdPoll::Poll2Text(polltab.revents), "polling", ID);
return -1;
}
// Do the peek.
//
do {mlen = recv(FD, Buff, Blen, MSG_PEEK);}
while(mlen < 0 && errno == EINTR);
// Return the result
//
if (mlen >= 0) return int(mlen);
Log.Emsg("Link", errno, "peek on", ID);
return -1;
}
/******************************************************************************/
/* R e c v */
/******************************************************************************/
int XrdLinkXeq::Recv(char *Buff, int Blen)
{
ssize_t rlen;
// Note that we will read only as much as is queued. Use Recv() with a
// timeout to receive as much data as possible.
//
if (LockReads) rdMutex.Lock();
isIdle = 0;
do {rlen = read(FD, Buff, Blen);} while(rlen < 0 && errno == EINTR);
if (rlen > 0) AtomicAdd(BytesIn, rlen);
if (LockReads) rdMutex.UnLock();
if (rlen >= 0) return int(rlen);
if (FD >= 0) Log.Emsg("Link", errno, "receive from", ID);
return -1;
}
/******************************************************************************/
int XrdLinkXeq::Recv(char *Buff, int Blen, int timeout)
{
XrdSysMutexHelper theMutex;
struct pollfd polltab = {FD, POLLIN|POLLRDNORM, 0};
ssize_t rlen, totlen = 0;
int retc;
// Lock the read mutex if we need to, the helper will unlock it upon exit
//
if (LockReads) theMutex.Lock(&rdMutex);
// Wait up to timeout milliseconds for data to arrive
//
isIdle = 0;
while(Blen > 0)
{do {retc = poll(&polltab,1,timeout);} while(retc < 0 && errno == EINTR);
if (retc != 1)
{if (retc == 0)
{tardyCnt++;
if (totlen)
{if ((++stallCnt & 0xff) == 1) TRACEI(DEBUG,"read timed out");
AtomicAdd(BytesIn, totlen);
}
return int(totlen);
}
return (FD >= 0 ? Log.Emsg("Link", -errno, "poll", ID) : -1);
}
// Verify it is safe to read now
//
if (!(polltab.revents & (POLLIN|POLLRDNORM)))
{Log.Emsg("Link", XrdPoll::Poll2Text(polltab.revents),
"polling", ID);
return -1;
}
// Read as much data as you can. Note that we will force an error
// if we get a zero-length read after poll said it was OK.
//
do {rlen = recv(FD, Buff, Blen, 0);} while(rlen < 0 && errno == EINTR);
if (rlen <= 0)
{if (!rlen) return -ENOMSG;
return (FD<0 ? -1 : Log.Emsg("Link",-errno,"receive from",ID));
}
totlen += rlen; Blen -= rlen; Buff += rlen;
}
AtomicAdd(BytesIn, totlen);
return int(totlen);
}
/******************************************************************************/
/* R e c v A l l */
/******************************************************************************/
int XrdLinkXeq::RecvAll(char *Buff, int Blen, int timeout)
{
struct pollfd polltab = {FD, POLLIN|POLLRDNORM, 0};
ssize_t rlen;
int retc;
// Check if timeout specified. Notice that the timeout is the max we will
// for some data. We will wait forever for all the data. Yeah, it's weird.
//
if (timeout >= 0)
{do {retc = poll(&polltab,1,timeout);} while(retc < 0 && errno == EINTR);
if (retc != 1)
{if (!retc) return -ETIMEDOUT;
Log.Emsg("Link",errno,"poll",ID);
return -1;
}
if (!(polltab.revents & (POLLIN|POLLRDNORM)))
{Log.Emsg("Link",XrdPoll::Poll2Text(polltab.revents),"polling",ID);
return -1;
}
}
// Note that we will block until we receive all he bytes.
//
if (LockReads) rdMutex.Lock();
isIdle = 0;
do {rlen = recv(FD,Buff,Blen,MSG_WAITALL);} while(rlen < 0 && errno == EINTR);
if (rlen > 0) AtomicAdd(BytesIn, rlen);
if (LockReads) rdMutex.UnLock();
if (int(rlen) == Blen) return Blen;
if (!rlen) {TRACEI(DEBUG, "No RecvAll() data; errno=" <<errno);}
else if (rlen > 0) Log.Emsg("RecvAll","Premature end from", ID);
else if (FD >= 0) Log.Emsg("Link",errno,"recieve from",ID);
return -1;
}
/******************************************************************************/
/* S e n d */
/******************************************************************************/
int XrdLinkXeq::Send(const char *Buff, int Blen)
{
ssize_t retc = 0, bytesleft = Blen;
// Get a lock
//
wrMutex.Lock();
isIdle = 0;
AtomicAdd(BytesOut, Blen);
// Do non-blocking writes if we are setup to do so.
//
if (sendQ)
{retc = sendQ->Send(Buff, Blen);
wrMutex.UnLock();
return retc;
}
// Write the data out
//
while(bytesleft)
{if ((retc = write(FD, Buff, bytesleft)) < 0)
{if (errno == EINTR) continue;
else break;
}
bytesleft -= retc; Buff += retc;
}
// All done
//
wrMutex.UnLock();
if (retc >= 0) return Blen;
Log.Emsg("Link", errno, "send to", ID);
return -1;
}
/******************************************************************************/
int XrdLinkXeq::Send(const struct iovec *iov, int iocnt, int bytes)
{
ssize_t bytesleft, n, retc = 0;
const char *Buff;
// Get a lock and assume we will be successful (statistically we are)
//
wrMutex.Lock();
isIdle = 0;
AtomicAdd(BytesOut, bytes);
// Do non-blocking writes if we are setup to do so.
//
if (sendQ)
{retc = sendQ->Send(iov, iocnt, bytes);
wrMutex.UnLock();
return retc;
}
// Write the data out. On some version of Unix (e.g., Linux) a writev() may
// end at any time without writing all the bytes when directed to a socket.
// So, we attempt to resume the writev() using a combination of write() and
// a writev() continuation. This approach slowly converts a writev() to a
// series of writes if need be. We must do this inline because we must hold
// the lock until all the bytes are written or an error occurs.
//
bytesleft = static_cast<ssize_t>(bytes);
while(bytesleft)
{do {retc = writev(FD, iov, iocnt);} while(retc < 0 && errno == EINTR);
if (retc >= bytesleft || retc < 0) break;
bytesleft -= retc;
while(retc >= (n = static_cast<ssize_t>(iov->iov_len)))
{retc -= n; iov++; iocnt--;}
Buff = (const char *)iov->iov_base + retc; n -= retc; iov++; iocnt--;
while(n) {if ((retc = write(FD, Buff, n)) < 0)
{if (errno == EINTR) continue;
else break;
}
n -= retc; Buff += retc;
}
if (retc < 0 || iocnt < 1) break;
}
// All done
//
wrMutex.UnLock();
if (retc >= 0) return bytes;
Log.Emsg("Link", errno, "send to", ID);
return -1;
}
/******************************************************************************/
int XrdLinkXeq::Send(const sfVec *sfP, int sfN)
{
#if !defined(HAVE_SENDFILE) || defined(__APPLE__)
return -1;
#else
#ifdef __solaris__
sendfilevec_t vecSF[XrdOucSFVec::sfMax], *vecSFP = vecSF;
size_t xframt, totamt, bytes = 0;
ssize_t retc;
int i = 0;
// Construct the sendfilev() vector
//
for (i = 0; i < sfN; sfP++, i++)
{if (sfP->fdnum < 0)
{vecSF[i].sfv_fd = SFV_FD_SELF;
vecSF[i].sfv_off = (off_t)sfP->buffer;
} else {
vecSF[i].sfv_fd = sfP->fdnum;
vecSF[i].sfv_off = sfP->offset;
}
vecSF[i].sfv_flag = 0;
vecSF[i].sfv_len = sfP->sendsz;
bytes += sfP->sendsz;
}
totamt = bytes;
// Lock the link, issue sendfilev(), and unlock the link. The documentation
// is very spotty and inconsistent. We can only retry this operation under
// very limited conditions.
//
wrMutex.Lock();
isIdle = 0;
do{retc = sendfilev(FD, vecSFP, sfN, &xframt);
// Check if all went well and return if so (usual case)
//
if (xframt == bytes)
{AtomicAdd(BytesOut, bytes);
wrMutex.UnLock();
return totamt;
}
// The only one we will recover from is EINTR. We cannot legally get EAGAIN.
//
if (retc < 0 && errno != EINTR) break;
// Try to resume the transfer
//
if (xframt > 0)
{AtomicAdd(BytesOut, xframt); bytes -= xframt; SfIntr++;
while(xframt > 0 && sfN)
{if ((ssize_t)xframt < (ssize_t)vecSFP->sfv_len)
{vecSFP->sfv_off += xframt; vecSFP->sfv_len -= xframt; break;}
xframt -= vecSFP->sfv_len; vecSFP++; sfN--;
}
}
} while(sfN > 0);
// See if we can recover without destroying the connection
//
retc = (retc < 0 ? errno : ECANCELED);
wrMutex.UnLock();
Log.Emsg("Link", retc, "send file to", ID);
return -1;
#elif defined(__linux__)
static const int setON = 1, setOFF = 0;
ssize_t retc = 0, bytesleft;
off_t myOffset;
int i, xfrbytes = 0, uncork = 1, xIntr = 0;
// lock the link
//
wrMutex.Lock();
isIdle = 0;
// In linux we need to cork the socket. On permanent errors we do not uncork
// the socket because it will be closed in short order.
//
if (setsockopt(FD, SOL_TCP, TCP_CORK, &setON, sizeof(setON)) < 0)
{Log.Emsg("Link", errno, "cork socket for", ID);
uncork = 0; sfOK = 0;
}
// Send the header first
//
for (i = 0; i < sfN; sfP++, i++)
{if (sfP->fdnum < 0) retc = sendData(sfP->buffer, sfP->sendsz);
else {myOffset = sfP->offset; bytesleft = sfP->sendsz;
while(bytesleft
&& (retc=sendfile(FD,sfP->fdnum,&myOffset,bytesleft)) > 0)
{bytesleft -= retc; xIntr++;}
}
if (retc < 0 && errno == EINTR) continue;
if (retc <= 0) break;
xfrbytes += sfP->sendsz;
}
// Diagnose any sendfile errors
//
if (retc <= 0)
{if (retc == 0) errno = ECANCELED;
wrMutex.UnLock();
Log.Emsg("Link", errno, "send file to", ID);
return -1;
}
// Now uncork the socket
//
if (uncork && setsockopt(FD, SOL_TCP, TCP_CORK, &setOFF, sizeof(setOFF)) < 0)
Log.Emsg("Link", errno, "uncork socket for", ID);
// All done
//
if (xIntr > sfN) SfIntr += (xIntr - sfN);
AtomicAdd(BytesOut, xfrbytes);
wrMutex.UnLock();
return xfrbytes;
#endif
#endif
}
/******************************************************************************/
/* private s e n d D a t a */
/******************************************************************************/
int XrdLinkXeq::sendData(const char *Buff, int Blen)
{
ssize_t retc = 0, bytesleft = Blen;
// Write the data out
//
while(bytesleft)
{if ((retc = write(FD, Buff, bytesleft)) < 0)
{if (errno == EINTR) continue;
else break;
}
bytesleft -= retc; Buff += retc;
}
// All done
//
return retc;
}
/******************************************************************************/
/* s e t I D */
/******************************************************************************/
void XrdLinkXeq::setID(const char *userid, int procid)
{
char buff[sizeof(Uname)], *bp, *sp;
int ulen;
snprintf(buff, sizeof(buff), "%s.%d:%d", userid, procid, FD);
ulen = strlen(buff);
sp = buff + ulen - 1;
bp = &Uname[sizeof(Uname)-1];
if (ulen > (int)sizeof(Uname)) ulen = sizeof(Uname);
*bp = '@'; bp--;
while(ulen--) {*bp = *sp; bp--; sp--;}
ID = bp+1;
Comment = (const char *)ID;
}
/******************************************************************************/
/* s e t N B */
/******************************************************************************/
bool XrdLinkXeq::setNB()
{
// We don't support non-blocking output except for Linux at the moment
//
#if !defined(__linux__)
return false;
#else
// Trace this request
//
TRACEI(DEBUG,"enabling non-blocking output");
// If we don't already have a sendQ object get one. This is a one-time call
// so to optimize checking if this object exists we also get the opMutex.'
//
opMutex.Lock();
if (!sendQ)
{wrMutex.Lock();
sendQ = new XrdSendQ(*this, wrMutex);
wrMutex.UnLock();
}
opMutex.UnLock();
return true;
#endif
}
/******************************************************************************/
/* s e t P r o t o c o l */
/******************************************************************************/
XrdProtocol *XrdLinkXeq::setProtocol(XrdProtocol *pp, bool push)
{
// Set new protocol.
//
opMutex.Lock();
XrdProtocol *op = Protocol;
if (push) ProtoAlt = Protocol;
Protocol = pp;
opMutex.UnLock();
return op;
}
/******************************************************************************/
/* s e t T L S */
/******************************************************************************/
bool XrdLinkXeq::setTLS(bool enable)
{ //???
// static const XrdTlsConnection::RW_Mode rwMode=XrdTlsConnection::TLS_RNB_WBL;
static const XrdTlsSocket::RW_Mode rwMode=XrdTlsSocket::TLS_RBL_WBL;
static const XrdTlsSocket::HS_Mode hsMode=XrdTlsSocket::TLS_HS_BLOCK;
const char *eNote;
int rc;
// If we are already in a compatible mode, we are done
//
if (isTLS == enable) return true;
// If this is a shutdown, then do it now.
//
if (!enable)
{tlsIO.Shutdown();
isTLS = enable;
Addr.SetTLS(enable);
return true;
}
// We want to initialize TLS, do so now.
//
eNote = tlsIO.Init(*tlsCtx, FD, rwMode, hsMode, false);
// Check for errors
//
if (eNote)
{char buff[1024];
snprintf(buff, sizeof(buff), "Unable to enable tls for %s;", ID);
Log.Emsg("LinkXeq", buff, eNote);
return false;
}
// Now we need to accept this TLS connection
//
rc = tlsIO.Accept();
// Diagnose return state
//
if (rc == 0)
{isTLS = enable;
Addr.SetTLS(enable);
}
return rc == 0;
}
/******************************************************************************/
/* S F E r r o r */
/******************************************************************************/
int XrdLinkXeq::SFError(int rc)
{
Log.Emsg("TLS", rc, "send file to", ID);
return -1;
}
/******************************************************************************/
/* S h u t d o w n */
/******************************************************************************/
void XrdLinkXeq::Shutdown(bool getLock)
{
int temp, theFD;
// Trace the entry
//
TRACEI(DEBUG, (getLock ? "Async" : "Sync") <<" link shutdown in progress");
// Get the lock if we need too (external entry via another thread)
//
if (getLock) opMutex.Lock();
// If there is something to do, do it now
//
temp = Instance; Instance = 0;
if (!KeepFD)
{theFD = (FD < 0 ? -FD : FD);
shutdown(theFD, SHUT_RDWR);
if (dup2(devNull, theFD) < 0)
{Instance = temp;
Log.Emsg("Link", errno, "shutdown FD for", ID);
}
}
// All done
//
if (getLock) opMutex.UnLock();
}
/******************************************************************************/
/* S t a t s */
/******************************************************************************/
int XrdLinkXeq::Stats(char *buff, int blen, bool do_sync)
{
static const char statfmt[] = "<stats id=\"link\"><num>%d</num>"
"<maxn>%d</maxn><tot>%lld</tot><in>%lld</in><out>%lld</out>"
"<ctime>%lld</ctime><tmo>%d</tmo><stall>%d</stall>"
"<sfps>%d</sfps></stats>";
int i;
// Check if actual length wanted
//
if (!buff) return sizeof(statfmt)+17*6;
// We must synchronize the statistical counters
//
if (do_sync) XrdLinkCtl::SyncAll();
// Obtain lock on the stats area and format it
//
AtomicBeg(statsMutex);
i = snprintf(buff, blen, statfmt, AtomicGet(LinkCount),
AtomicGet(LinkCountMax),
AtomicGet(LinkCountTot),
AtomicGet(LinkBytesIn),
AtomicGet(LinkBytesOut),
AtomicGet(LinkConTime),
AtomicGet(LinkTimeOuts),
AtomicGet(LinkStalls),
AtomicGet(LinkSfIntr));
AtomicEnd(statsMutex);
return i;
}
/******************************************************************************/
/* s y n c S t a t s */
/******************************************************************************/
void XrdLinkXeq::syncStats(int *ctime)
{
long long tmpLL;
int tmpI4;
// If this is dynamic, get the opMutex lock
//
if (!ctime) opMutex.Lock();
// Either the caller has the opMutex or this is called out of close. In either
// case, we need to get the read and write mutexes; each followed by the stats
// mutex. This order is important because we should not hold the stats mutex
// for very long and the r/w mutexes may take a long time to acquire. If we
// must maintain the link count we need to actually acquire the stats mutex as
// we will be doing compound operations. Atomics are still used to keep other
// threads from seeing partial results.
//
AtomicBeg(rdMutex);
if (ctime)
{*ctime = time(0) - conTime;
AtomicAdd(LinkConTime, *ctime);
statsMutex.Lock();
if (LinkCount > 0) AtomicDec(LinkCount);
statsMutex.UnLock();
}
AtomicBeg(statsMutex);
tmpLL = AtomicFAZ(BytesIn);
AtomicAdd(LinkBytesIn, tmpLL); AtomicAdd(BytesInTot, tmpLL);
tmpI4 = AtomicFAZ(tardyCnt);
AtomicAdd(LinkTimeOuts, tmpI4); AtomicAdd(tardyCntTot, tmpI4);
tmpI4 = AtomicFAZ(stallCnt);
AtomicAdd(LinkStalls, tmpI4); AtomicAdd(stallCntTot, tmpI4);
AtomicEnd(statsMutex); AtomicEnd(rdMutex);
AtomicBeg(wrMutex); AtomicBeg(statsMutex);
tmpLL = AtomicFAZ(BytesOut);
AtomicAdd(LinkBytesOut, tmpLL); AtomicAdd(BytesOutTot, tmpLL);
tmpI4 = AtomicFAZ(SfIntr);
AtomicAdd(LinkSfIntr, tmpI4);
AtomicEnd(statsMutex); AtomicEnd(wrMutex);
// Make sure the protocol updates it's statistics as well
//
if (Protocol) Protocol->Stats(0, 0, 1);
// All done
//
if (!ctime) opMutex.UnLock();
}
/******************************************************************************/
/* Private: T L S _ E r r o r */
/******************************************************************************/
int XrdLinkXeq::TLS_Error(const char *act, int rc)
{
std::string reason = tlsIO.Err2Text(rc);
char msg[512];
snprintf(msg, sizeof(msg), "Unable to %s %s;", act, ID);
Log.Emsg("TLS", msg, reason.c_str());
return -1;
}
/******************************************************************************/
/* T L S _ P e e k */
/******************************************************************************/
int XrdLinkXeq::TLS_Peek(char *Buff, int Blen, int timeout)
{
XrdSysMutexHelper theMutex;
int retc, rlen;
// Lock the read mutex if we need to, the helper will unlock it upon exit
//
if (LockReads) theMutex.Lock(&rdMutex);
// Wait until we can actually read something
//
isIdle = 0;
if (timeout)
{retc = Wait4Data(timeout);
if (retc < 1) return retc;
}
// Do the peek and if sucessful, the number of bytes available.
//
retc = tlsIO.Peek(Buff, Blen, rlen);
if (retc == SSL_ERROR_NONE) return rlen;
// Dianose the TLS error and return failure
//
return TLS_Error("peek on", retc);
}
/******************************************************************************/
/* T L S _ R e c v */
/******************************************************************************/