zwoop commented on a change in pull request #7516:
URL: https://github.com/apache/trafficserver/pull/7516#discussion_r575510929
##########
File path: plugins/header_rewrite/conditions.cc
##########
@@ -1333,4 +1333,77 @@ ConditionSessionTransactCount::append_value(std::string
&s, Resources const &res
TSDebug(PLUGIN_NAME, "Appending SSN-TXN-COUNT %s to evaluation value
%.*s", _qualifier.c_str(), length, value);
s.append(value, length);
}
-}
\ No newline at end of file
+}
+
+void
+ConditionTcpInfo::initialize(Parser &p)
+{
+ Condition::initialize(p);
+ TSDebug(PLUGIN_NAME, "Initializing TCP Info");
+ MatcherType *match = new MatcherType(_cond_op);
+ std::string const &arg = p.get_arg();
+
+ match->set(strtol(arg.c_str(), nullptr, 10));
+ _matcher = match;
+}
+
+void
+ConditionTcpInfo::initialize_hooks()
+{
+ add_allowed_hook(TS_HTTP_TXN_START_HOOK);
+ add_allowed_hook(TS_HTTP_TXN_CLOSE_HOOK);
+ add_allowed_hook(TS_HTTP_SEND_RESPONSE_HDR_HOOK);
+}
+
+bool
+ConditionTcpInfo::eval(const Resources &res)
+{
+ std::string s;
+
+ append_value(s, res);
+ bool rval = static_cast<const Matchers<std::string> *>(_matcher)->test(s);
+
+ TSDebug(PLUGIN_NAME, "Evaluating TCP-Info: %s - rval: %d", s.c_str(), rval);
+
+ return rval;
+}
+
+void
+ConditionTcpInfo::append_value(std::string &s, Resources const &res)
+{
+#if defined(TCP_INFO) && defined(HAVE_STRUCT_TCP_INFO)
+ TSReturnCode tsSsn;
+ int fd;
+ struct tcp_info info;
+ socklen_t tcp_info_len = sizeof(info);
+ tsSsn = TSHttpTxnClientFdGet(res.txnp, &fd);
+ if (tsSsn != TS_SUCCESS || fd <= 0) {
+ TSDebug(PLUGIN_NAME, "error getting the client socket fd from ssn");
+ }
+ if (getsockopt(fd, IPPROTO_TCP, TCP_INFO, &info, &tcp_info_len) != 0) {
+ TSDebug(PLUGIN_NAME, "getsockopt(%d, TCP_INFO) failed: %s", fd,
strerror(errno));
+ }
+
+ if (tsSsn == TS_SUCCESS) {
+ uint32_t rtt = info.tcpi_rtt;
+ uint32_t rto = info.tcpi_rto;
+ uint32_t snd_cwnd = info.tcpi_snd_cwnd;
+ uint32_t all_retx;
+#if !defined(freebsd) || defined(__GLIBC__)
+ all_retx = info.tcpi_retrans;
+#else
+ all_retx = info.__tcpi_retrans;
+#endif
+
+ if (tcp_info_len > 0) {
+ s += std::to_string(rtt);
Review comment:
Something like this is probably more efficient:
```
char buf[12 * 4 + 5]; // 4 uint32's
snprintf(buf, sizeof(buf), "%u; %u; %u; %u; ", rtt, rto, snd_cwnd,
all_rtx);
s += buf;
```
Although, use PRIu32 in the printf.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]