XRootD
XrdTpcTPC.hh
Go to the documentation of this file.
1 
2 #include <memory>
3 #include <string>
4 #include <vector>
5 #include <sys/time.h>
6 
8 
10 #include "XrdHttp/XrdHttpUtils.hh"
11 
12 #include "XrdTls/XrdTlsTempCA.hh"
13 #include "XrdTpcPMarkManager.hh"
14 
15 #include <curl/curl.h>
16 
17 class XrdOucErrInfo;
18 class XrdOucStream;
19 class XrdSfsFile;
20 class XrdSfsFileSystem;
21 class XrdXrootdTpcMon;
22 typedef void CURL;
23 
24 namespace TPC {
25 class State;
26 
27 enum LogMask {
28  Debug = 0x01,
29  Info = 0x02,
30  Warning = 0x04,
31  Error = 0x08,
32  All = 0xff
33 };
34 
35 
36 struct CurlDeleter {
37  void operator()(CURL *curl);
38 };
39 using ManagedCurlHandle = std::unique_ptr<CURL, CurlDeleter>;
40 
41 
42 class TPCHandler : public XrdHttpExtHandler {
43 public:
44  TPCHandler(XrdSysError *log, const char *config, XrdOucEnv *myEnv);
45  virtual ~TPCHandler();
46 
47  virtual bool MatchesPath(const char *verb, const char *path);
48  virtual int ProcessReq(XrdHttpExtReq &req);
49  // Abstract method in the base class, but does not seem to be used
50  virtual int Init(const char *cfgfile) {return 0;}
51 
52 private:
53 
54  static int sockopt_callback(void * clientp, curl_socket_t curlfd, curlsocktype purpose);
55  static int opensocket_callback(void *clientp,
56  curlsocktype purpose,
57  struct curl_sockaddr *address);
58 
59  static int closesocket_callback(void *clientp, curl_socket_t fd);
60 
61  struct TPCLogRecord {
62 
63  TPCLogRecord(XrdHttpExtReq & req) : bytes_transferred( -1 ), status( -1 ),
64  tpc_status(-1), streams( 1 ), isIPv6(false), mReq(req), pmarkManager(mReq)
65  {
66  gettimeofday(&begT, 0); // Set effective start time
67  }
68  ~TPCLogRecord();
69 
70  std::string log_prefix;
71  std::string local;
72  std::string remote;
73  std::string name;
74  std::string clID;
75  static XrdXrootdTpcMon* tpcMonitor;
76  timeval begT;
77  off_t bytes_transferred;
78  int status;
79  int tpc_status;
80  unsigned int streams;
81  bool isIPv6;
82  XrdHttpExtReq & mReq;
83  XrdTpc::PMarkManager pmarkManager;
84  XrdSysError * m_log;
85  };
86 
87  int ProcessOptionsReq(XrdHttpExtReq &req);
88 
89  static std::string GetAuthz(XrdHttpExtReq &req);
90 
91  // Configure curl handle's CA settings. The CA files present here should
92  // be valid for the lifetime of the process.
93  void ConfigureCurlCA(CURL *curl);
94 
95  // Redirect the transfer according to the contents of an XrdOucErrInfo object.
96  int RedirectTransfer(CURL *curl, const std::string &redirect_resource, XrdHttpExtReq &req,
97  XrdOucErrInfo &error, TPCLogRecord &);
98 
99  int OpenWaitStall(XrdSfsFile &fh, const std::string &resource, int mode,
100  int openMode, const XrdSecEntity &sec,
101  const std::string &authz);
102 
103 #ifdef XRD_CHUNK_RESP
104  int DetermineXferSize(CURL *curl, XrdHttpExtReq &req, TPC::State &state,
105  bool &success, TPCLogRecord &, bool shouldReturnErrorToClient = true);
106 
107  int GetContentLengthTPCPull(CURL *curl, XrdHttpExtReq &req, uint64_t & contentLength, bool & success, TPCLogRecord &rec);
108 
109  // Send a 'performance marker' back to the TPC client, informing it of our
110  // progress. The TPC client will use this information to determine whether
111  // the transfer is making sufficient progress and/or other monitoring info
112  // (such as whether the transfer is happening over IPv4, IPv6, or both).
113  int SendPerfMarker(XrdHttpExtReq &req, TPCLogRecord &rec, TPC::State &state);
114  int SendPerfMarker(XrdHttpExtReq &req, TPCLogRecord &rec, std::vector<State*> &state,
115  off_t bytes_transferred);
116 
117  // Perform the libcurl transfer, periodically sending back chunked updates.
118  int RunCurlWithUpdates(CURL *curl, XrdHttpExtReq &req, TPC::State &state,
119  TPCLogRecord &rec);
120 
121  // Experimental multi-stream version of RunCurlWithUpdates
122  int RunCurlWithStreams(XrdHttpExtReq &req, TPC::State &state,
123  size_t streams, TPCLogRecord &rec);
124  int RunCurlWithStreamsImpl(XrdHttpExtReq &req, TPC::State &state,
125  size_t streams, std::vector<TPC::State*> &streams_handles,
126  std::vector<ManagedCurlHandle> &curl_handles,
127  TPCLogRecord &rec);
128 #else
129  int RunCurlBasic(CURL *curl, XrdHttpExtReq &req, TPC::State &state,
130  TPCLogRecord &rec);
131 #endif
132 
133  int ProcessPushReq(const std::string & resource, XrdHttpExtReq &req);
134  int ProcessPullReq(const std::string &resource, XrdHttpExtReq &req);
135 
136  bool ConfigureFSLib(XrdOucStream &Config, std::string &path1, bool &path1_alt,
137  std::string &path2, bool &path2_alt);
138  bool Configure(const char *configfn, XrdOucEnv *myEnv);
139  bool ConfigureLogger(XrdOucStream &Config);
140 
141  // Generate a consistently-formatted log message.
142  void logTransferEvent(LogMask lvl, const TPCLogRecord &record,
143  const std::string &event, const std::string &message="");
144 
145  std::string generateClientErr(std::stringstream &err_ss, const TPCLogRecord &rec, CURLcode cCode = CURLcode::CURLE_OK);
146 
147  std::string prepareURL(XrdHttpExtReq &req, bool & hasSetOpaque);
148  std::string prepareURL(XrdHttpExtReq &req);
149 
150  static int m_marker_period;
151  static size_t m_block_size;
152  static size_t m_small_block_size;
153  bool m_desthttps;
154  bool m_fixed_route; // If 'true' the Destination IP in an HTTP-TPC is forced to be the same as the IP used to contact the server
155  // when 'false' any IP available can be selected
156  int m_timeout; // the 'timeout interval'; if no bytes have been received during this time period, abort the transfer.
157  int m_first_timeout; // the 'first timeout interval'; the amount of time we're willing to wait to get the first byte.
158  // Unless explicitly specified, this is 2x the timeout interval.
159  std::string m_cadir; // The directory to use for CAs.
160  std::string m_cafile; // The file to use for CAs in libcurl
161  static XrdSysMutex m_monid_mutex;
162  static uint64_t m_monid;
163  XrdSysError m_log;
164  XrdSfsFileSystem *m_sfs;
165  std::shared_ptr<XrdTlsTempCA> m_ca_file;
166 
167  // 16 blocks in flight at 16 MB each, meaning that there will be up to 256MB
168  // in flight; this is equal to the bandwidth delay product of a 200ms transcontinental
169  // connection at 10Gbps.
170 #ifdef USE_PIPELINING
171  static const int m_pipelining_multiplier = 16;
172 #else
173  static const int m_pipelining_multiplier = 1;
174 #endif
175 
176  bool usingEC; // indicate if XrdEC is used
177 
178  // Time to connect the curl socket to the remote server uses the linux's default value
179  // of 60 seconds
180  static const long CONNECT_TIMEOUT = 60;
181 
182  // hdr2cgimap
183  std::map<std::string,std::string> hdr2cgimap;
184 };
185 }
Utility functions for XrdHTTP.
void CURL
Definition: XrdTpcState.hh:13
void CURL
Definition: XrdTpcTPC.hh:21
TPCHandler(XrdSysError *log, const char *config, XrdOucEnv *myEnv)
Definition: XrdTpcTPC.cc:304
virtual int ProcessReq(XrdHttpExtReq &req)
Definition: XrdTpcTPC.cc:268
virtual ~TPCHandler()
Definition: XrdTpcTPC.cc:296
virtual int Init(const char *cfgfile)
Initializes the external request handler.
Definition: XrdTpcTPC.hh:50
virtual bool MatchesPath(const char *verb, const char *path)
Tells if the incoming path is recognized as one of the paths that have to be processed.
Definition: XrdTpcTPC.cc:249
std::unique_ptr< CURL, CurlDeleter > ManagedCurlHandle
Definition: XrdTpcTPC.hh:39
LogMask
Definition: XrdTpcTPC.hh:27
@ All
Definition: XrdTpcTPC.hh:32
@ Info
Definition: XrdTpcTPC.hh:29
@ Error
Definition: XrdTpcTPC.hh:31
@ Debug
Definition: XrdTpcTPC.hh:28
@ Warning
Definition: XrdTpcTPC.hh:30
XrdCmsConfig Config
void operator()(CURL *curl)
Definition: XrdTpcTPC.cc:84