XRootD
Loading...
Searching...
No Matches
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
28namespace XrdHttpTpc
29{
30PMarkManager::SocketInfo::SocketInfo(int fd, const struct sockaddr * sockP) {
31 netAddr.Set(sockP,fd);
32 client.addrInfo = static_cast<XrdNetAddrInfo*>(&netAddr);
33}
34
35PMarkManager::PMarkManager(XrdHttpExtReq & req, TPC::TpcType tpcType) : mPmark(req.pmark), mReq(req), mTransferWillStart(false), mTpcType(tpcType) {}
36
37void 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
44bool 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 return false;
52 }
53 }
54 // If pmark is not enabled, we leave libcurl doing the connection
55 return true;
56}
57
59 return mPmark && (mReq.mSciTag >= 0);
60}
61
63 mTransferWillStart = true;
64}
65
67 if(mSocketInfos.empty()) {
68 return;
69 }
70
71 if(mPmarkHandles.empty()) {
72 // Create the first pmark handle
73 std::stringstream ss;
74 ss << "scitag.flow=" << mReq.mSciTag
75 // 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
76 // do not emit a firefly, this server WILL emit a firefly.
77 //
78 // For PULL: it is expected that I send a GET request to the remote server
79 // however, it is myself who will emit the firefly, then I should consider that the GET is actually a PUT
80 // that I do on behalf of the remote server... Hence why if the tpc transfer type is Pull, the pmark.appname
81 // will be equal to http-put
82 //
83 // For PUSH: it is expected that I send a PUT request to the remote server.
84 // however, it is myself who will emit the firefly, then I should consider that the PUT is actually a GET
85 // 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.
86 << "&" << "pmark.appname=" << ((mTpcType == TPC::TpcType::Pull) ? "http-put" : "http-get");
87 SocketInfo & sockInfo = mSocketInfos.front();
88 auto pmark = mPmark->Begin(sockInfo.client, mReq.resource.c_str(), ss.str().c_str(), "http-tpc");
89 if(!pmark) {
90 return;
91 }
92 mPmarkHandles.emplace(sockInfo.client.addrInfo->SockFD(),std::unique_ptr<XrdNetPMark::Handle>(pmark));
93 mSocketInfos.pop();
94 }
95
96 auto pmarkHandleItor = mPmarkHandles.begin();
97 while(!mSocketInfos.empty()) {
98 SocketInfo & sockInfo = mSocketInfos.front();
99 auto pmark = mPmark->Begin(*sockInfo.client.addrInfo, *(pmarkHandleItor->second), nullptr);
100 if (!pmark) {
101 // The packet marking handle could not be created from the first handle, let's retry next time
102 break;
103 }
104
105 int fd = sockInfo.client.addrInfo->SockFD();
106 mPmarkHandles.emplace(fd, std::unique_ptr<XrdNetPMark::Handle>(pmark));
107 mSocketInfos.pop();
108 }
109}
110
112 // We need to delete the PMark handle associated to the fd passed in parameter
113 // we just look for it and reset the unique_ptr to nullptr to trigger the PMark handle deletion
114 mPmarkHandles.erase(fd);
115}
116} // 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)
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)
XrdNetAddrInfo * addrInfo
Entity's connection details.