XRootD
XrdHttpTpcPMarkManager.cc
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of XrdHttpTpcTPC
3 //
4 // Copyright (c) 2023 by European Organization for Nuclear Research (CERN)
5 // Author: Cedric Caffy <ccaffy@cern.ch>
6 // File Date: Oct 2023
7 //------------------------------------------------------------------------------
8 // XRootD is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // XRootD is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
20 //------------------------------------------------------------------------------
21 
22 
23 #include <sstream>
25 #include "XrdNet/XrdNetUtils.hh"
27 
28 namespace XrdHttpTpc
29 {
30 PMarkManager::SocketInfo::SocketInfo(int fd, const struct sockaddr * sockP) {
31  netAddr.Set(sockP,fd);
32  client.addrInfo = static_cast<XrdNetAddrInfo*>(&netAddr);
33 }
34 
35 PMarkManager::PMarkManager(XrdHttpExtReq & req, TPC::TpcType tpcType) : mPmark(req.pmark), mReq(req), mTransferWillStart(false), mTpcType(tpcType) {}
36 
37 void PMarkManager::addFd(int fd, const struct sockaddr * sockP) {
38  if(isEnabled() && mTransferWillStart) {
39  // The transfer will start and the packet marking has been configured, this socket must be registered for future packet marking
40  mSocketInfos.emplace(fd, sockP);
41  }
42 }
43 
44 bool PMarkManager::connect(int fd, const struct sockaddr *sockP, size_t sockPLen, uint32_t timeout_sec, std::stringstream &err) {
45  if(isEnabled()) {
46  // We only connect if the packet marking is enabled
47  bool couldConnect = XrdNetUtils::ConnectWithTimeout(fd,sockP,sockPLen,timeout_sec,err);
48  if(couldConnect) {
49  addFd(fd,sockP);
50  } else {
51  // in this case the ConnectWithTimeout function has closed fd
52  return false;
53  }
54  }
55  // If pmark is not enabled, we leave libcurl doing the connection
56  return true;
57 }
58 
60  return mPmark && (mReq.mSciTag >= 0);
61 }
62 
64  mTransferWillStart = true;
65 }
66 
68  if(mSocketInfos.empty()) {
69  return;
70  }
71 
72  if(mPmarkHandles.empty()) {
73  // Create the first pmark handle
74  std::stringstream ss;
75  ss << "scitag.flow=" << mReq.mSciTag
76  // One has to consider that this server is the client side of a normal HTTP PUT/GET. But unlike normal HTTP PUT and GET requests where clients
77  // do not emit a firefly, this server WILL emit a firefly.
78  //
79  // For PULL: it is expected that I send a GET request to the remote server
80  // however, it is myself who will emit the firefly, then I should consider that the GET is actually a PUT
81  // that I do on behalf of the remote server... Hence why if the tpc transfer type is Pull, the pmark.appname
82  // will be equal to http-put
83  //
84  // For PUSH: it is expected that I send a PUT request to the remote server.
85  // however, it is myself who will emit the firefly, then I should consider that the PUT is actually a GET
86  // that I do on behalf of the remote server... Hence why if the tpc transfer is Push, the pmark.appname will be equal to http-get.
87  << "&" << "pmark.appname=" << ((mTpcType == TPC::TpcType::Pull) ? "http-put" : "http-get");
88  SocketInfo & sockInfo = mSocketInfos.front();
89  auto pmark = mPmark->Begin(sockInfo.client, mReq.resource.c_str(), ss.str().c_str(), "http-tpc");
90  if(!pmark) {
91  return;
92  }
93  mPmarkHandles.emplace(sockInfo.client.addrInfo->SockFD(),std::unique_ptr<XrdNetPMark::Handle>(pmark));
94  mSocketInfos.pop();
95  }
96 
97  auto pmarkHandleItor = mPmarkHandles.begin();
98  while(!mSocketInfos.empty()) {
99  SocketInfo & sockInfo = mSocketInfos.front();
100  auto pmark = mPmark->Begin(*sockInfo.client.addrInfo, *(pmarkHandleItor->second), nullptr);
101  if (!pmark) {
102  // The packet marking handle could not be created from the first handle, let's retry next time
103  break;
104  }
105 
106  int fd = sockInfo.client.addrInfo->SockFD();
107  mPmarkHandles.emplace(fd, std::unique_ptr<XrdNetPMark::Handle>(pmark));
108  mSocketInfos.pop();
109  }
110 }
111 
113  // We need to delete the PMark handle associated to the fd passed in parameter
114  // we just look for it and reset the unique_ptr to nullptr to trigger the PMark handle deletion
115  mPmarkHandles.erase(fd);
116 }
117 } // namespace XrdHttpTpc
std::string resource
SocketInfo(int fd, const struct sockaddr *sockP)
bool connect(int fd, const struct sockaddr *sockP, size_t sockPLen, uint32_t timeout_sec, std::stringstream &err)
PMarkManager(XrdHttpExtReq &req, const TPC::TpcType type)
const char * Set(const char *hSpec, int pNum=PortInSpec)
Definition: XrdNetAddr.cc:216
virtual Handle * Begin(XrdSecEntity &Client, const char *path=0, const char *cgi=0, const char *app=0)=0
static bool ConnectWithTimeout(int sockfd, const struct sockaddr *clientAddr, size_t clientAddrLen, uint32_t timeout_sec, std::stringstream &errMsg)
Definition: XrdNetUtils.cc:981
XrdNetAddrInfo * addrInfo
Entity's connection details.
Definition: XrdSecEntity.hh:80