30 #include <sys/types.h>
40 #define TRACELINK this
65 char *XrdLinkCtl::LinkBat = 0;
66 unsigned int XrdLinkCtl::LinkAlloc= 0;
67 int XrdLinkCtl::LTLast = -1;
68 int XrdLinkCtl::maxFD = 0;
73 const char *XrdLinkCtl::TraceID =
"LinkCtl";
78 unsigned int myInstance = 1;
82 static const int XRDLINK_USED = 0x01;
83 static const int XRDLINK_FREE = 0x00;
85 class LinkScan :
public XrdJob
92 LinkScan() :
XrdJob(
"Idle link scan") {}
104 char hName[1024], *unp, buff[32];
105 int bl, peerFD = peer.
SockFD();
109 if (peerFD < 0 || peerFD >= maxFD)
110 {snprintf(hName,
sizeof(hName),
"%d", peerFD);
111 Log.
Emsg(
"Link",
"attempt to alloc out of range FD -",hName);
120 snprintf(hName,
sizeof(hName),
"%d", peerFD);
121 Log.
Emsg(
"Link",
"attempt to reuse active link FD -",hName);
128 if (!(lp = LinkTab[peerFD]))
133 Log.
Emsg(
"Link", ENOMEM,
"create link");
136 blp = &LinkTab[peerFD/LinkAlloc*LinkAlloc];
137 for (i = 0; i < LinkAlloc; i++, blp++) *blp = &nlp[i];
138 lp = LinkTab[peerFD];
141 LinkBat[peerFD] = XRDLINK_USED;
142 if (peerFD > LTLast) LTLast = peerFD;
158 lp->
HNlen = strlen(hName);
162 bl = sprintf(buff,
"anon.0:%d", peerFD);
163 unp = lp->
Uname +
sizeof(Uname) - bl - 1;
164 memcpy(unp, buff, bl);
167 lp->
Comment = (
const char *)unp;
180 if (LinkCountMax <=
AtomicInc(LinkCount)) LinkCountMax = LinkCount;
197 const int MaxSeek = 16;
199 int i, seeklim = MaxSeek;
204 if (curr >= 0 && LinkTab[curr]) LinkTab[curr]->
setRef(-1);
211 for (i = curr+1; i <= LTLast; i++)
212 {
if ((lp = LinkTab[i]) && LinkBat[i] && lp->
HostName)
219 if (myINS == lp->
Instance)
return lp;
222 if (!seeklim--) {LTMutex.UnLock(); seeklim = MaxSeek; LTMutex.Lock();}
243 const int MaxSeek = 16;
244 int i, ulen = 0, seeklim = MaxSeek;
251 for (i = curr+1; i <= LTLast; i++)
252 {
if ((lp = LinkTab[i]) && LinkBat[i] && lp->
HostName)
255 {ulen = lp->
Client(nbuf, nbsz);
260 if (!seeklim--) {LTMutex.UnLock(); seeklim = MaxSeek; LTMutex.Lock();}
280 int i, ltlast, lnum = 0, tmo = 0, tmod = 0;
291 for (i = 0; i <= ltlast; i++)
292 {
if (LinkBat[i] != XRDLINK_USED
293 || !(lp = LinkTab[i]))
continue;
298 if ((
int(lp->
isIdle)) < idleTicks)
302 Log.
Emsg(
"LinkScan",
"Link",lp->
ID,
"is disabled and idle.");
312 TRACE(CONN, lnum <<
" links; " <<tmo <<
" idle; " <<tmod <<
" force closed");
321 if (wkSec > 0) waitKill =
static_cast<short>(wkSec);
322 if (kwSec > 0) killWait =
static_cast<short>(kwSec);
337 numalloc = 8192 /
sizeof(
XrdLink);
339 while((numalloc = numalloc/2)) LinkAlloc = LinkAlloc*2;
340 TRACE(
DEBUG,
"Allocating " <<LinkAlloc <<
" link objects at a time");
345 {
Log.
Emsg(
"Link", ENOMEM,
"create LinkTab");
return 0;}
346 memset((
void *)LinkTab, 0, maxfds*
sizeof(
XrdLinkCtl *));
350 if (!(LinkBat = (
char *)malloc(maxfds*
sizeof(
char)+LinkAlloc)))
351 {
Log.
Emsg(
"Link", ENOMEM,
"create LinkBat");
return 0;}
352 memset((
void *)LinkBat, XRDLINK_FREE, maxfds*
sizeof(
char));
357 {
if ((idleCheck = idlewait/3)) idleTicks = 3;
359 idleCheck = idlewait;
361 LinkScan *ls =
new LinkScan;
380 LTMutex.Lock(); myLTLast = LTLast; LTMutex.UnLock();
384 for (
int i = 0; i <= myLTLast; i++)
385 {
if (LinkBat[i] == XRDLINK_USED && LinkTab[i]) LinkTab[i]->syncStats();}
398 LinkBat[fd] = XRDLINK_FREE;
399 if (fd == LTLast)
while(LTLast && !(LinkBat[LTLast])) LTLast--;
int DoIt(int argpnt, int argc, char **argv, bool singleshot)
static XrdLink * Alloc(XrdNetAddr &peer, int opts=0)
static void SyncAll()
Synchronize statustics for ll links.
static int Setup(int maxfds, int idlewt)
static XrdLink * Find(int &curr, XrdLinkMatch *who=0)
static void setKWT(int wkSec, int kwSec)
static void idleScan()
Look for idle links and close hem down.
static short killWait
Link destruction control constants.
static int getName(int &curr, char *bname, int blen, XrdLinkMatch *who=0)
static void Unhook(int fd)
Unhook a link from the active table of links.
int Match(const char *uname, int unlen, const char *hname, int hnlen)
int Client(char *buff, int blen)
char * ID
Pointer to the client's link identity.
static const int noPort
Do not add port number.
static const int old6Map4
Use deprecated IPV6 mapped format.
int Format(char *bAddr, int bLen, fmtUse fmtType=fmtAuto, int fmtOpts=0)
@ fmtAuto
Hostname if already resolved o/w use fmtAddr.
virtual void Disable(XrdPollInfo &pInfo, const char *etxt=0)=0
void Schedule(XrdJob *jp)
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)