XRootD
XrdTpcPMarkManager.cc
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of XrdTpcTPC
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>
24 #include "XrdTpcPMarkManager.hh"
25 #include "XrdNet/XrdNetUtils.hh"
26 
27 namespace XrdTpc
28 {
29 PMarkManager::SocketInfo::SocketInfo(int fd, const struct sockaddr * sockP) {
30  netAddr.Set(sockP,fd);
31  client.addrInfo = static_cast<XrdNetAddrInfo*>(&netAddr);
32 }
33 
34 PMarkManager::PMarkManager(XrdHttpExtReq & req) : mPmark(req.pmark), mReq(req), mTransferWillStart(false) {}
35 
36 void PMarkManager::addFd(int fd, const struct sockaddr * sockP) {
37  if(isEnabled() && mTransferWillStart) {
38  // The transfer will start and the packet marking has been configured, this socket must be registered for future packet marking
39  mSocketInfos.emplace(fd, sockP);
40  }
41 }
42 
43 bool PMarkManager::connect(int fd, const struct sockaddr *sockP, size_t sockPLen, uint32_t timeout_sec, std::stringstream &err) {
44  if(isEnabled()) {
45  // We only connect if the packet marking is enabled
46  bool couldConnect = XrdNetUtils::ConnectWithTimeout(fd,sockP,sockPLen,timeout_sec,err);
47  if(couldConnect) {
48  addFd(fd,sockP);
49  } else {
50  return false;
51  }
52  }
53  // If pmark is not enabled, we leave libcurl doing the connection
54  return true;
55 }
56 
58  return mPmark && (mReq.mSciTag >= 0);
59 }
60 
62  mTransferWillStart = true;
63 }
64 
66  if(mSocketInfos.empty()) {
67  return;
68  }
69 
70  if(mPmarkHandles.empty()) {
71  // Create the first pmark handle
72  std::stringstream ss;
73  ss << "scitag.flow=" << mReq.mSciTag;
74  SocketInfo & sockInfo = mSocketInfos.front();
75  auto pmark = mPmark->Begin(sockInfo.client, mReq.resource.c_str(), ss.str().c_str(), "http-tpc");
76  if(!pmark) {
77  return;
78  }
79  mPmarkHandles.emplace(sockInfo.client.addrInfo->SockFD(),std::unique_ptr<XrdNetPMark::Handle>(pmark));
80  mSocketInfos.pop();
81  }
82 
83  auto pmarkHandleItor = mPmarkHandles.begin();
84  while(!mSocketInfos.empty()) {
85  SocketInfo & sockInfo = mSocketInfos.front();
86  auto pmark = mPmark->Begin(*sockInfo.client.addrInfo, *(pmarkHandleItor->second), nullptr);
87  if (!pmark) {
88  // The packet marking handle could not be created from the first handle, let's retry next time
89  break;
90  }
91 
92  int fd = sockInfo.client.addrInfo->SockFD();
93  mPmarkHandles.emplace(fd, std::unique_ptr<XrdNetPMark::Handle>(pmark));
94  mSocketInfos.pop();
95  }
96 }
97 
98 void PMarkManager::endPmark(int fd) {
99  // We need to delete the PMark handle associated to the fd passed in parameter
100  // we just look for it and reset the unique_ptr to nullptr to trigger the PMark handle deletion
101  mPmarkHandles.erase(fd);
102 }
103 } // namespace XrdTpc
std::string resource
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:946
XrdNetAddrInfo * addrInfo
Entity's connection details.
Definition: XrdSecEntity.hh:80
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)