xrootd
XrdLink.hh
Go to the documentation of this file.
1 #ifndef __XRD_LINK_H__
2 #define __XRD_LINK_H__
3 /******************************************************************************/
4 /* */
5 /* X r d L i n k . h h */
6 /* */
7 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* Produced by Andrew Hanushevsky for Stanford University under contract */
9 /* DE-AC02-76-SFO0515 with the Department of Energy */
10 /* */
11 /* This file is part of the XRootD software suite. */
12 /* */
13 /* XRootD is free software: you can redistribute it and/or modify it under */
14 /* the terms of the GNU Lesser General Public License as published by the */
15 /* Free Software Foundation, either version 3 of the License, or (at your */
16 /* option) any later version. */
17 /* */
18 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
19 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
20 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
21 /* License for more details. */
22 /* */
23 /* You should have received a copy of the GNU Lesser General Public License */
24 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
25 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
26 /* */
27 /* The copyright holder's institutional names and contributor's names may not */
28 /* be used to endorse or promote products derived from this software without */
29 /* specific prior written permission of the institution or contributor. */
30 /******************************************************************************/
31 
32 #include <sys/types.h>
33 #include <fcntl.h>
34 #include <time.h>
35 
36 #include "XrdNet/XrdNetAddr.hh"
37 #include "XrdOuc/XrdOucSFVec.hh"
38 #include "XrdSys/XrdSysPthread.hh"
39 
40 #include "Xrd/XrdJob.hh"
41 #include "Xrd/XrdLinkMatch.hh"
42 #include "Xrd/XrdProtocol.hh"
43 
44 /******************************************************************************/
45 /* X r d L i n k O p t i o n s */
46 /******************************************************************************/
47 
48 #define XRDLINK_RDLOCK 0x0001
49 #define XRDLINK_NOCLOSE 0x0002
50 
51 /******************************************************************************/
52 /* C l a s s D e f i n i t i o n */
53 /******************************************************************************/
54 
55 class XrdInet;
56 class XrdNetAddr;
57 class XrdPoll;
58 class XrdOucTrace;
59 class XrdScheduler;
60 class XrdSendQ;
61 class XrdSysError;
62 
63 class XrdLink : XrdJob
64 {
65 public:
66 friend class XrdLinkScan;
67 friend class XrdPoll;
68 friend class XrdPollPoll;
69 friend class XrdPollDev;
70 friend class XrdPollE;
71 
72 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
78 inline
79 XrdNetAddrInfo *AddrInfo() {return (XrdNetAddrInfo *)&Addr;}
80 
81 //-----------------------------------------------------------------------------
91 //-----------------------------------------------------------------------------
92 
93 static XrdLink *Alloc(XrdNetAddr &peer, int opts=0);
94 
95 int Backlog();
96 
97 void Bind() {} // Obsolete
98 void Bind(pthread_t tid) { (void)tid; } // Obsolete
99 
100 int Client(char *buff, int blen);
101 
102 int Close(int defer=0);
103 
104 void DoIt();
105 
106 void Enable();
107 
108 int FDnum() {int fd = FD; return (fd < 0 ? -fd : fd);}
109 
110 static XrdLink *fd2link(int fd)
111  {if (fd < 0) fd = -fd;
112  return (fd <= LTLast && LinkBat[fd] ? LinkTab[fd] : 0);
113  }
114 
115 static XrdLink *fd2link(int fd, unsigned int inst)
116  {if (fd < 0) fd = -fd;
117  if (fd <= LTLast && LinkBat[fd] && LinkTab[fd]
118  && LinkTab[fd]->Instance == inst) return LinkTab[fd];
119  return (XrdLink *)0;
120  }
121 
122 static XrdLink *Find(int &curr, XrdLinkMatch *who=0);
123 
124  int getIOStats(long long &inbytes, long long &outbytes,
125  int &numstall, int &numtardy)
126  { inbytes = BytesIn + BytesInTot;
127  outbytes = BytesOut+BytesOutTot;
128  numstall = stallCnt + stallCntTot;
129  numtardy = tardyCnt + tardyCntTot;
130  return InUse;
131  }
132 
133 static int getName(int &curr, char *bname, int blen, XrdLinkMatch *who=0);
134 
135 XrdProtocol *getProtocol() {return Protocol;} // opmutex must be locked
136 
137 void Hold(int lk) {(lk ? opMutex.Lock() : opMutex.UnLock());}
138 
139 //-----------------------------------------------------------------------------
144 //-----------------------------------------------------------------------------
145 
146 const char *Host() {return (const char *)HostName;}
147 
148 char *ID; // This is referenced a lot
149 
150 static void Init(XrdSysError *eP, XrdOucTrace *tP, XrdScheduler *sP)
151  {XrdLog = eP; XrdTrace = tP; XrdSched = sP;}
152 
153 static void Init(XrdInet *iP) {XrdNetTCP = iP;}
154 
155 //-----------------------------------------------------------------------------
159 //-----------------------------------------------------------------------------
160 inline
161 unsigned int Inst() {return Instance;}
162 
163 //-----------------------------------------------------------------------------
168 //-----------------------------------------------------------------------------
169 inline
170 bool isFlawed() {return Etext != 0;}
171 
172 //-----------------------------------------------------------------------------
180 //-----------------------------------------------------------------------------
181 inline
182 bool isInstance(unsigned int inst)
183  {return FD >= 0 && Instance == inst;}
184 
185 //-----------------------------------------------------------------------------
190 //-----------------------------------------------------------------------------
191 inline
192 const char *Name() {return (const char *)Lname;}
193 
194 //-----------------------------------------------------------------------------
199 //-----------------------------------------------------------------------------
200 inline const
201 XrdNetAddr *NetAddr() {return &Addr;}
202 
203 int Peek(char *buff, int blen, int timeout=-1);
204 
205 int Recv(char *buff, int blen);
206 int Recv(char *buff, int blen, int timeout);
207 
208 int RecvAll(char *buff, int blen, int timeout=-1);
209 
210 int Send(const char *buff, int blen);
211 int Send(const struct iovec *iov, int iocnt, int bytes=0);
212 
213 static int sfOK; // True if Send(sfVec) enabled
214 
215 typedef XrdOucSFVec sfVec;
216 
217 int Send(const sfVec *sdP, int sdn); // Iff sfOK > 0
218 
219 void Serialize(); // ASYNC Mode
220 
221 int setEtext(const char *text);
222 
223 void setID(const char *userid, int procid);
224 
225 static void setKWT(int wkSec, int kwSec);
226 
227 void setLocation(XrdNetAddrInfo::LocInfo &loc) {Addr.SetLocation(loc);}
228 
229 bool setNB();
230 
232 
233 void setRef(int cnt); // ASYNC Mode
234 
235 static int Setup(int maxfd, int idlewait);
236 
237  void Shutdown(bool getLock);
238 
239 static int Stats(char *buff, int blen, int do_sync=0);
240 
241  void syncStats(int *ctime=0);
242 
243  int Terminate(const XrdLink *owner, int fdnum, unsigned int inst);
244 
245 time_t timeCon() {return conTime;}
246 
247 int UseCnt() {return InUse;}
248 
249 void armBridge() {isBridged = 1;}
250 int hasBridge() {return isBridged;}
251 
252  XrdLink();
253  ~XrdLink() {} // Is never deleted!
254 
255 private:
256 
257 void Reset();
258 int sendData(const char *Buff, int Blen);
259 
263 static XrdInet *XrdNetTCP;
264 
265 static XrdSysMutex LTMutex; // For the LinkTab only LTMutex->IOMutex allowed
266 static XrdLink **LinkTab;
267 static char *LinkBat;
268 static unsigned int LinkAlloc;
269 static int LTLast;
270 static const char *TraceID;
271 static int devNull;
272 static short killWait;
273 static short waitKill;
274 
275 // Statistical area (global and local)
276 //
277 static long long LinkBytesIn;
278 static long long LinkBytesOut;
279 static long long LinkConTime;
280 static long long LinkCountTot;
281 static int LinkCount;
282 static int LinkCountMax;
283 static int LinkTimeOuts;
284 static int LinkStalls;
285 static int LinkSfIntr;
286 static int maxFD;
287  long long BytesIn;
288  long long BytesInTot;
289  long long BytesOut;
290  long long BytesOutTot;
291  int stallCnt;
293  int tardyCnt;
295  int SfIntr;
297 
298 // Identification section
299 //
300 XrdNetAddr Addr;
301 char Uname[24]; // Uname and Lname must be adjacent!
302 char Lname[232];
303 char *HostName;
304 int HNlen;
305 #if defined( __linux__ ) || defined( __solaris__ )
306 pthread_t TID; // Hack to keep abi compatability
307 #else
308 XrdLink *Next; // Only used by PollPoll.icc
309 #endif
314 XrdSysCondVar *KillcvP; // Protected by opMutex!
315 XrdSendQ *sendQ; // Protected by wrMutex && opMutex
319 struct pollfd *PollEnt;
320 char *Etext;
321 int FD;
322 unsigned int Instance;
323 time_t conTime;
324 int InUse;
325 int doPost;
327 char KeepFD;
329 char isIdle;
330 char inQ; // Only used by PollPoll.icc
332 char KillCnt; // Protected by opMutex!
333 static const char KillMax = 60;
334 static const char KillMsk = 0x7f;
335 static const char KillXwt = 0x80;
336 };
337 #endif
Definition: XrdProtocol.hh:116
Definition: XrdOucTrace.hh:35
Definition: XrdSysError.hh:89
Definition: XrdScheduler.hh:44
Definition: XrdSysPthread.hh:140
Definition: XrdSysPthread.hh:53
Definition: XrdSysPthread.hh:332
void Lock()
Definition: XrdSysPthread.hh:149
Definition: XrdLinkMatch.hh:35
void UnLock()
Definition: XrdSysPthread.hh:151
Definition: XrdJob.hh:42