llcircuit.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. /**
  2. * @file llcircuit.cpp
  3. * @brief Class to track UDP endpoints for the message system.
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2002-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #if LL_WINDOWS
  34. # include <process.h>
  35. #else
  36. # if LL_LINUX
  37. # include <dlfcn.h> // RTLD_LAZY
  38. # endif
  39. # include <sys/types.h>
  40. # include <sys/socket.h>
  41. # include <netinet/in.h>
  42. #endif
  43. #ifndef USE_CIRCUIT_LIST
  44. # include <algorithm>
  45. #endif
  46. #include <sstream>
  47. #include <iterator>
  48. #include <stack>
  49. #include "llcircuit.h"
  50. #include "llmessage.h"
  51. #include "llrand.h"
  52. #include "lltransfermanager.h"
  53. // Helper template (used to be in llmath/llmodularmath.h but is only used here)
  54. namespace LLModularMath
  55. {
  56. // Return difference between lhs and rhs treating the U32 operands and
  57. // result as unsigned values of given width.
  58. template<int width>
  59. LL_INLINE U32 subtract(U32 lhs, U32 rhs)
  60. {
  61. // Generate a bit mask which will truncate unsigned values to given
  62. // width at compile time.
  63. const U32 mask = (1 << width) - 1;
  64. // Operands are unsigned, so modular arithmetic applies. If lhs < rhs,
  65. // difference will wrap in to lower bits of result, which is then
  66. // masked to give a value that can be represented by an unsigned value
  67. // of width bits.
  68. return mask & (lhs - rhs);
  69. }
  70. }
  71. // How many pings behind we have to be to consider ourself blocked.
  72. constexpr S32 PING_START_BLOCK = 3;
  73. // How many pings behind we have to be to consider ourself unblocked.
  74. constexpr S32 PING_RELEASE_BLOCK = 2;
  75. constexpr F32 TARGET_PERIOD_LENGTH = 5.f; // seconds
  76. // This can be long, as time-based cleanup is only done when wrapping
  77. // packetids, now...
  78. constexpr F32 LL_DUPLICATE_SUPPRESSION_TIMEOUT = 60.f; // seconds
  79. LLCircuitData::LLCircuitData(const LLHost& host, TPACKETID in_id,
  80. F32 circuit_heartbeat_interval,
  81. F32 circuit_timeout)
  82. : mHost (host),
  83. mWrapID(0),
  84. mPacketsOutID(0),
  85. mPacketsInID(in_id),
  86. mHighestPacketID(in_id),
  87. mTimeoutCallback(NULL),
  88. mTimeoutUserData(NULL),
  89. mTrusted(false),
  90. mAllowTimeout(true),
  91. mAlive(true),
  92. mBlocked(false),
  93. mPingTime(0.0),
  94. mLastPingSendTime(0.0),
  95. mLastPingReceivedTime(0.0),
  96. mNextPingSendTime(0.0),
  97. mPingsInTransit(0),
  98. mLastPingID(0),
  99. mPingDelay(INITIAL_PING_VALUE_MSEC),
  100. mPingDelayAveraged((F32)INITIAL_PING_VALUE_MSEC),
  101. mUnackedPacketCount(0),
  102. mUnackedPacketBytes(0),
  103. mLastPacketInTime(0.0),
  104. mLocalEndPointID(),
  105. mPacketsOut(0),
  106. mPacketsIn(0),
  107. mPacketsLost(0),
  108. mBytesIn(0),
  109. mBytesOut(0),
  110. mLastPeriodLength(-1.f),
  111. mBytesInLastPeriod(0),
  112. mBytesOutLastPeriod(0),
  113. mBytesInThisPeriod(0),
  114. mBytesOutThisPeriod(0),
  115. mPeakBPSIn(0.f),
  116. mPeakBPSOut(0.f),
  117. mPeriodTime(0.0),
  118. mExistenceTimer(),
  119. mAckCreationTime(0.f),
  120. mCurrentResendCount(0),
  121. mLastPacketGap(0),
  122. mHeartbeatInterval(circuit_heartbeat_interval),
  123. mHeartbeatTimeout(circuit_timeout)
  124. {
  125. // Need to guarantee that this time is up to date, we may be creating a
  126. // circuit even though we haven't been running a message system loop.
  127. F64 mt_sec = LLMessageSystem::getMessageTimeSeconds(true);
  128. mPingTime = mLastPingReceivedTime = mPeriodTime = mt_sec;
  129. mLastPingSendTime = mt_sec + mHeartbeatInterval * ll_frand();
  130. mNextPingSendTime = mLastPingSendTime +
  131. (F64)(0.9f * mHeartbeatInterval +
  132. ll_frand(0.2f * mHeartbeatInterval));
  133. mLocalEndPointID.generate();
  134. }
  135. LLCircuitData::~LLCircuitData()
  136. {
  137. LLReliablePacket* packetp = NULL;
  138. // Clean up all pending transfers.
  139. gTransferManager.cleanupConnection(mHost);
  140. // remove all pending reliable messages on this circuit
  141. std::vector<TPACKETID> doomed;
  142. reliable_iter iter;
  143. reliable_iter end = mUnackedPackets.end();
  144. for (iter = mUnackedPackets.begin(); iter != end; ++iter)
  145. {
  146. packetp = iter->second;
  147. LLMessageSystem* msg = gMessageSystemp;
  148. ++msg->mFailedResendPackets;
  149. if (msg->mVerboseLog)
  150. {
  151. doomed.push_back(packetp->mPacketID);
  152. }
  153. if (packetp->mCallback)
  154. {
  155. packetp->mCallback(packetp->mCallbackData, LL_ERR_CIRCUIT_GONE);
  156. }
  157. // Update stats
  158. --mUnackedPacketCount;
  159. mUnackedPacketBytes -= packetp->mBufferLength;
  160. delete packetp;
  161. }
  162. // remove all pending final retry reliable messages on this circuit
  163. LLMessageSystem* msg = gMessageSystemp;
  164. end = mFinalRetryPackets.end();
  165. for (iter = mFinalRetryPackets.begin(); iter != end; ++iter)
  166. {
  167. packetp = iter->second;
  168. ++msg->mFailedResendPackets;
  169. if (msg->mVerboseLog)
  170. {
  171. doomed.push_back(packetp->mPacketID);
  172. }
  173. if (packetp->mCallback)
  174. {
  175. packetp->mCallback(packetp->mCallbackData,LL_ERR_CIRCUIT_GONE);
  176. }
  177. // Update stats
  178. --mUnackedPacketCount;
  179. mUnackedPacketBytes -= packetp->mBufferLength;
  180. delete packetp;
  181. }
  182. // log aborted reliable packets for this circuit.
  183. if (msg->mVerboseLog && !doomed.empty())
  184. {
  185. std::ostringstream str;
  186. std::ostream_iterator<TPACKETID> append(str, " ");
  187. str << "MSG: -> " << mHost << "\tABORTING RELIABLE:\t";
  188. std::copy(doomed.begin(), doomed.end(), append);
  189. llinfos << str.str() << llendl;
  190. }
  191. }
  192. void LLCircuitData::ackReliablePacket(TPACKETID packet_num)
  193. {
  194. reliable_iter iter;
  195. LLReliablePacket* packetp;
  196. iter = mUnackedPackets.find(packet_num);
  197. if (iter != mUnackedPackets.end())
  198. {
  199. packetp = iter->second;
  200. if (gMessageSystemp->mVerboseLog)
  201. {
  202. std::ostringstream str;
  203. str << "MSG: <- " << packetp->mHost << "\tRELIABLE ACKED:\t"
  204. << packetp->mPacketID;
  205. llinfos << str.str() << llendl;
  206. }
  207. if (packetp->mCallback)
  208. {
  209. if (packetp->mTimeout < 0.f)
  210. {
  211. // negative timeout will always return timeout even for
  212. // successful ack, for debugging
  213. packetp->mCallback(packetp->mCallbackData, LL_ERR_TCP_TIMEOUT);
  214. }
  215. else
  216. {
  217. packetp->mCallback(packetp->mCallbackData, LL_ERR_NOERR);
  218. }
  219. }
  220. // Update stats
  221. --mUnackedPacketCount;
  222. mUnackedPacketBytes -= packetp->mBufferLength;
  223. // Cleanup
  224. delete packetp;
  225. mUnackedPackets.erase(iter);
  226. return;
  227. }
  228. iter = mFinalRetryPackets.find(packet_num);
  229. if (iter != mFinalRetryPackets.end())
  230. {
  231. packetp = iter->second;
  232. if (gMessageSystemp->mVerboseLog)
  233. {
  234. std::ostringstream str;
  235. str << "MSG: <- " << packetp->mHost << "\tRELIABLE ACKED:\t"
  236. << packetp->mPacketID;
  237. llinfos << str.str() << llendl;
  238. }
  239. if (packetp->mCallback)
  240. {
  241. if (packetp->mTimeout < 0.f)
  242. {
  243. // negative timeout will always return timeout even for
  244. // successful ack, for debugging
  245. packetp->mCallback(packetp->mCallbackData, LL_ERR_TCP_TIMEOUT);
  246. }
  247. else
  248. {
  249. packetp->mCallback(packetp->mCallbackData, LL_ERR_NOERR);
  250. }
  251. }
  252. // Update stats
  253. --mUnackedPacketCount;
  254. mUnackedPacketBytes -= packetp->mBufferLength;
  255. // Cleanup
  256. delete packetp;
  257. mFinalRetryPackets.erase(iter);
  258. }
  259. #if 0
  260. else
  261. {
  262. // Couldn't find this packet on either of the unacked lists.
  263. // maybe it's a duplicate ack ?
  264. }
  265. #endif
  266. }
  267. S32 LLCircuitData::resendUnackedPackets(F64 now)
  268. {
  269. LLReliablePacket* packetp;
  270. LLMessageSystem* msg = gMessageSystemp;
  271. // Theoretically we should search through the list for the packet with the
  272. // oldest packet ID, as otherwise when we WRAP we will resend reliable
  273. // packets out of order. Since resends are ALREADY out of order, and
  274. // wrapping is highly rare (16+ million packets), I'm not going to worry
  275. // about this for now - djs
  276. reliable_iter iter;
  277. bool have_resend_overflow = false;
  278. for (iter = mUnackedPackets.begin(); iter != mUnackedPackets.end(); )
  279. {
  280. packetp = iter->second;
  281. // Only check overflow if we haven't had one yet.
  282. if (!have_resend_overflow)
  283. {
  284. have_resend_overflow = mThrottles.checkOverflow(TC_RESEND, 0);
  285. }
  286. if (have_resend_overflow)
  287. {
  288. // We've exceeded our bandwidth for resends. Time to stop trying to
  289. // send them.
  290. // If we have too many unacked packets, we need to start dropping
  291. // expired ones.
  292. if (mUnackedPacketBytes > 512000)
  293. {
  294. if (now > packetp->mExpirationTime)
  295. {
  296. // This circuit has overflowed. Do not retry. Do not pass go.
  297. packetp->mRetries = 0;
  298. // Remove it from this list and add it to the final list.
  299. mUnackedPackets.erase(iter++);
  300. mFinalRetryPackets[packetp->mPacketID] = packetp;
  301. }
  302. else
  303. {
  304. ++iter;
  305. }
  306. // Move on to the next unacked packet.
  307. continue;
  308. }
  309. if (mUnackedPacketBytes > 256000 && !(getPacketsOut() % 1024))
  310. {
  311. // Warn if we've got a lot of resends waiting.
  312. llwarns << mHost << " has " << mUnackedPacketBytes
  313. << " bytes of reliable messages waiting" << llendl;
  314. }
  315. // Stop resending. There are less than 512000 unacked packets.
  316. break;
  317. }
  318. if (now > packetp->mExpirationTime)
  319. {
  320. --packetp->mRetries;
  321. // retry
  322. ++mCurrentResendCount;
  323. ++msg->mResentPackets;
  324. if (msg->mVerboseLog)
  325. {
  326. std::ostringstream str;
  327. str << "MSG: -> " << packetp->mHost
  328. << "\tRESENDING RELIABLE:\t" << packetp->mPacketID;
  329. llinfos << str.str() << llendl;
  330. }
  331. // tag packet id as being a resend
  332. packetp->mBuffer[0] |= LL_RESENT_FLAG;
  333. msg->mPacketRing.sendPacket(packetp->mSocket,
  334. (char*)packetp->mBuffer,
  335. packetp->mBufferLength,
  336. packetp->mHost);
  337. mThrottles.throttleOverflow(TC_RESEND,
  338. packetp->mBufferLength * 8.f);
  339. // The new method, retry time based on ping
  340. if (packetp->mPingBasedRetry)
  341. {
  342. packetp->mExpirationTime = now +
  343. llmax(LL_MINIMUM_RELIABLE_TIMEOUT_SECONDS,
  344. LL_RELIABLE_TIMEOUT_FACTOR *
  345. getPingDelayAveraged());
  346. }
  347. else
  348. {
  349. // Custom, constant retry time
  350. packetp->mExpirationTime = now + packetp->mTimeout;
  351. }
  352. if (!packetp->mRetries)
  353. {
  354. // Last resend, remove it from this list and add it to the
  355. // final list.
  356. mUnackedPackets.erase(iter++);
  357. mFinalRetryPackets[packetp->mPacketID] = packetp;
  358. }
  359. else
  360. {
  361. // Do not remove it yet, it still gets to try to resend at least
  362. // once.
  363. ++iter;
  364. }
  365. }
  366. else
  367. {
  368. // Do not need to do anything with this packet, keep iterating.
  369. ++iter;
  370. }
  371. }
  372. for (iter = mFinalRetryPackets.begin(); iter != mFinalRetryPackets.end();)
  373. {
  374. packetp = iter->second;
  375. if (now > packetp->mExpirationTime)
  376. {
  377. // fail (too many retries)
  378. LL_DEBUGS("Circuit") << "Packet " << packetp->mPacketID
  379. << " removed from the pending list: exceeded retry limit";
  380. if (packetp->mMessageName)
  381. {
  382. LL_CONT << "Packet name " << packetp->mMessageName;
  383. }
  384. LL_CONT << "." << LL_ENDL;
  385. ++msg->mFailedResendPackets;
  386. if (msg->mVerboseLog)
  387. {
  388. std::ostringstream str;
  389. str << "MSG: -> " << packetp->mHost << "\tABORTING RELIABLE:\t"
  390. << packetp->mPacketID;
  391. llinfos << str.str() << llendl;
  392. }
  393. if (packetp->mCallback)
  394. {
  395. packetp->mCallback(packetp->mCallbackData,LL_ERR_TCP_TIMEOUT);
  396. }
  397. // Update stats
  398. --mUnackedPacketCount;
  399. mUnackedPacketBytes -= packetp->mBufferLength;
  400. mFinalRetryPackets.erase(iter++);
  401. delete packetp;
  402. }
  403. else
  404. {
  405. ++iter;
  406. }
  407. }
  408. return mUnackedPacketCount;
  409. }
  410. LLCircuit::LLCircuit(F32 circuit_heartbeat_interval, F32 circuit_timeout)
  411. : mLastCircuit(NULL),
  412. mHeartbeatInterval(circuit_heartbeat_interval),
  413. mHeartbeatTimeout(circuit_timeout)
  414. {
  415. }
  416. LLCircuit::~LLCircuit()
  417. {
  418. for (circ_data_map_t::iterator it = mCircuitData.begin(),
  419. end = mCircuitData.end();
  420. it != end; ++it)
  421. {
  422. delete it->second;
  423. }
  424. mCircuitData.clear();
  425. }
  426. LLCircuitData* LLCircuit::addCircuitData(const LLHost& host, TPACKETID in_id)
  427. {
  428. // This should really validate if one already exists
  429. llinfos << "Adding circuit data for " << host << llendl;
  430. LLCircuitData* tempp = new LLCircuitData(host, in_id, mHeartbeatInterval,
  431. mHeartbeatTimeout);
  432. mCircuitData[host] = tempp;
  433. mPingSet.insert(tempp);
  434. mLastCircuit = tempp;
  435. return tempp;
  436. }
  437. void LLCircuit::removeCircuitData(const LLHost& host)
  438. {
  439. llinfos << "Removing circuit data for " << host << llendl;
  440. mLastCircuit = NULL;
  441. circ_data_map_t::iterator it = mCircuitData.find(host);
  442. if (it != mCircuitData.end())
  443. {
  444. LLCircuitData* cdp = it->second;
  445. mCircuitData.erase(it);
  446. LLCircuit::ping_set_t::iterator psit = mPingSet.find(cdp);
  447. if (psit != mPingSet.end())
  448. {
  449. mPingSet.erase(psit);
  450. }
  451. else
  452. {
  453. llwarns << "Couldn't find entry for next ping in ping set !"
  454. << llendl;
  455. }
  456. // Clean up from optimization maps
  457. mUnackedCircuitMap.erase(host);
  458. mSendAckMap.erase(host);
  459. delete cdp;
  460. }
  461. // This also has to happen AFTER we nuke the circuit, because various
  462. // callbacks for the circuit may result in messages being sent to this
  463. // circuit, and the setting of mLastCircuit. We don't check if the host
  464. // matches, but we don't really care because mLastCircuit is an
  465. // optimization, and this happens VERY rarely.
  466. mLastCircuit = NULL;
  467. }
  468. void LLCircuitData::setAlive(bool b_alive)
  469. {
  470. if (mAlive != b_alive)
  471. {
  472. mPacketsOutID = 0;
  473. mPacketsInID = 0;
  474. mAlive = b_alive;
  475. }
  476. if (b_alive)
  477. {
  478. mLastPingReceivedTime = LLMessageSystem::getMessageTimeSeconds();
  479. mPingsInTransit = 0;
  480. mBlocked = false;
  481. }
  482. }
  483. void LLCircuitData::setAllowTimeout(bool allow)
  484. {
  485. mAllowTimeout = allow;
  486. if (allow)
  487. {
  488. // resuming circuit
  489. // make sure it's alive
  490. setAlive(true);
  491. }
  492. }
  493. // Reset per-period counters if necessary.
  494. void LLCircuitData::checkPeriodTime()
  495. {
  496. F64 mt_sec = LLMessageSystem::getMessageTimeSeconds();
  497. F64 period_length = mt_sec - mPeriodTime;
  498. if (period_length > TARGET_PERIOD_LENGTH)
  499. {
  500. F32 bps_in = (F32)(mBytesInThisPeriod * 8.f / period_length);
  501. if (bps_in > mPeakBPSIn)
  502. {
  503. mPeakBPSIn = bps_in;
  504. }
  505. F32 bps_out = (F32)(mBytesOutThisPeriod * 8.f / period_length);
  506. if (bps_out > mPeakBPSOut)
  507. {
  508. mPeakBPSOut = bps_out;
  509. }
  510. mBytesInLastPeriod = mBytesInThisPeriod;
  511. mBytesOutLastPeriod = mBytesOutThisPeriod;
  512. mBytesInThisPeriod = 0;
  513. mBytesOutThisPeriod = 0;
  514. mLastPeriodLength = (F32)period_length;
  515. mPeriodTime = mt_sec;
  516. }
  517. }
  518. void LLCircuitData::addBytesIn(S32 bytes)
  519. {
  520. mBytesIn += bytes;
  521. mBytesInThisPeriod += bytes;
  522. }
  523. void LLCircuitData::addBytesOut(S32 bytes)
  524. {
  525. mBytesOut += bytes;
  526. mBytesOutThisPeriod += bytes;
  527. }
  528. void LLCircuitData::addReliablePacket(S32 mSocket, U8* buf_ptr, S32 buf_len,
  529. LLReliablePacketParams* params)
  530. {
  531. LLReliablePacket* packet_info;
  532. packet_info = new LLReliablePacket(mSocket, buf_ptr, buf_len, params);
  533. ++mUnackedPacketCount;
  534. mUnackedPacketBytes += packet_info->mBufferLength;
  535. if (params && params->mRetries)
  536. {
  537. mUnackedPackets[packet_info->mPacketID] = packet_info;
  538. }
  539. else
  540. {
  541. mFinalRetryPackets[packet_info->mPacketID] = packet_info;
  542. }
  543. }
  544. void LLCircuit::resendUnackedPackets(S32& unacked_list_length,
  545. S32& unacked_list_size)
  546. {
  547. F64 now = LLMessageSystem::getMessageTimeSeconds();
  548. unacked_list_length = 0;
  549. unacked_list_size = 0;
  550. LLCircuitData* circ;
  551. circ_data_map_t::iterator end = mUnackedCircuitMap.end();
  552. for (circ_data_map_t::iterator it = mUnackedCircuitMap.begin(); it != end;
  553. ++it)
  554. {
  555. circ = it->second;
  556. unacked_list_length += circ->resendUnackedPackets(now);
  557. unacked_list_size += circ->getUnackedPacketBytes();
  558. }
  559. }
  560. bool LLCircuitData::isDuplicateResend(TPACKETID packetnum)
  561. {
  562. return mRecentlyReceivedReliablePackets.find(packetnum) !=
  563. mRecentlyReceivedReliablePackets.end();
  564. }
  565. void LLCircuit::dumpResends()
  566. {
  567. circ_data_map_t::iterator end = mCircuitData.end();
  568. for (circ_data_map_t::iterator it = mCircuitData.begin(); it != end; ++it)
  569. {
  570. it->second->dumpResendCountAndReset();
  571. }
  572. }
  573. LLCircuitData* LLCircuit::findCircuit(const LLHost& host) const
  574. {
  575. // An optimization on finding the previously found circuit.
  576. if (mLastCircuit && mLastCircuit->mHost == host)
  577. {
  578. return mLastCircuit;
  579. }
  580. circ_data_map_t::const_iterator it = mCircuitData.find(host);
  581. if (it == mCircuitData.end())
  582. {
  583. return NULL;
  584. }
  585. mLastCircuit = it->second;
  586. return mLastCircuit;
  587. }
  588. bool LLCircuit::isCircuitAlive(const LLHost& host) const
  589. {
  590. LLCircuitData* cdp = findCircuit(host);
  591. if (cdp)
  592. {
  593. return cdp->mAlive;
  594. }
  595. return false;
  596. }
  597. void LLCircuitData::setTimeoutCallback(void (*callback_func)(const LLHost&,
  598. void*),
  599. void* user_data)
  600. {
  601. mTimeoutCallback = callback_func;
  602. mTimeoutUserData = user_data;
  603. }
  604. void LLCircuitData::checkPacketInID(TPACKETID id, bool receive_resent)
  605. {
  606. // Done as floats so we don't have to worry about running out of room
  607. // with U32 getting poked into an S32.
  608. F32 delta = (F32)mHighestPacketID - (F32)id;
  609. if (delta > 0.5f * LL_MAX_OUT_PACKET_ID)
  610. {
  611. // We've almost definitely wrapped, reset the mLastPacketID to be low
  612. // again.
  613. mHighestPacketID = id;
  614. }
  615. else if (delta < -0.5f * LL_MAX_OUT_PACKET_ID)
  616. {
  617. // This is almost definitely an old packet coming in after a wrap,
  618. // ignore it.
  619. }
  620. else
  621. {
  622. mHighestPacketID = llmax(mHighestPacketID, id);
  623. }
  624. // Save packet arrival time
  625. mLastPacketInTime = LLMessageSystem::getMessageTimeSeconds();
  626. // Have we received anything on this circuit yet?
  627. if (0 == mPacketsIn)
  628. {
  629. // Must be first packet from unclosed circuit.
  630. ++mPacketsIn;
  631. setPacketInID((id + 1) % LL_MAX_OUT_PACKET_ID);
  632. mLastPacketGap = 0;
  633. return;
  634. }
  635. ++mPacketsIn;
  636. // now, check to see if we've got a gap
  637. U32 gap = 0;
  638. if (mPacketsInID == id)
  639. {
  640. // nope! bump and wrap the counter, then return
  641. ++mPacketsInID;
  642. mPacketsInID = mPacketsInID % LL_MAX_OUT_PACKET_ID;
  643. }
  644. else if (id < mWrapID)
  645. {
  646. // id < mWrapID will happen if the first few packets are out of order;
  647. // at that point we haven't marked anything "potentially lost" and the
  648. // out-of-order packet will cause a full wrap marking all the IDs
  649. // "potentially lost"
  650. // do nothing
  651. }
  652. else
  653. {
  654. // we have a gap! if that id is in the map, remove it from the map,
  655. // leave mCurrentCircuit->mPacketsInID alone otherwise, walk from
  656. // mCurrentCircuit->mPacketsInID to id with wrapping, adding the values
  657. // to the map and setting mPacketsInID to id + 1 % LL_MAX_OUT_PACKET_ID
  658. // Babbage: all operands in expression are unsigned, so modular
  659. // arithmetic will always find correct gap, regardless of wrap arounds.
  660. constexpr U8 width = 24;
  661. gap = LLModularMath::subtract<width>(mPacketsInID, id);
  662. if (mPotentialLostPackets.find(id) != mPotentialLostPackets.end())
  663. {
  664. if (gMessageSystemp->mVerboseLog)
  665. {
  666. std::ostringstream str;
  667. str << "MSG: <- " << mHost << "\tRECOVERING LOST:\t" << id;
  668. llinfos << str.str() << llendl;
  669. }
  670. LL_DEBUGS("Circuit") << "Removing potential lost: " << id
  671. << LL_ENDL;
  672. mPotentialLostPackets.erase(id);
  673. }
  674. // don't freak out over out-of-order reliable resends
  675. else if (!receive_resent)
  676. {
  677. LLMessageSystem* msg = gMessageSystemp;
  678. U64 time = LLMessageSystem::getMessageTimeUsecs();
  679. TPACKETID index = mPacketsInID;
  680. S32 gap_count = 0;
  681. if (index < id && id - index < 16)
  682. {
  683. while (index != id)
  684. {
  685. if (msg->mVerboseLog)
  686. {
  687. std::ostringstream str;
  688. str << "MSG: <- " << mHost << "\tPACKET GAP:\t"
  689. << index;
  690. llinfos << str.str() << llendl;
  691. }
  692. LL_DEBUGS("Circuit") << "Adding potential lost: " << index
  693. << LL_ENDL;
  694. mPotentialLostPackets[index++] = time;
  695. index = index % LL_MAX_OUT_PACKET_ID;
  696. ++gap_count;
  697. }
  698. }
  699. else
  700. {
  701. llinfos << "Packet out of order - got packet " << id
  702. << " expecting " << index << " from " << mHost
  703. << llendl;
  704. if (msg->mVerboseLog)
  705. {
  706. std::ostringstream str;
  707. str << "MSG: <- " << mHost << "\tPACKET GAP:\t" << id
  708. << " expected " << index;
  709. llinfos << str.str() << llendl;
  710. }
  711. }
  712. mPacketsInID = id + 1;
  713. mPacketsInID = (mPacketsInID) % LL_MAX_OUT_PACKET_ID;
  714. if (gap_count > 128)
  715. {
  716. llwarns << "Packet loss gap filler running amok !" << llendl;
  717. }
  718. else if (gap_count > 16)
  719. {
  720. llwarns << "Sustaining large amounts of packet loss !"
  721. << llendl;
  722. }
  723. }
  724. }
  725. mLastPacketGap = gap;
  726. }
  727. void LLCircuit::updateWatchDogTimers(LLMessageSystem* msgsys)
  728. {
  729. F64 cur_time = LLMessageSystem::getMessageTimeSeconds();
  730. S32 count = mPingSet.size();
  731. S32 cur = 0;
  732. // Only process each circuit once at most, stop processing if no circuits
  733. while (cur++ < count && !mPingSet.empty())
  734. {
  735. LLCircuit::ping_set_t::iterator psit = mPingSet.begin();
  736. LLCircuitData* cdp = *psit;
  737. if (!cdp->mAlive)
  738. {
  739. // We suspect that this case should never happen, given how the
  740. // alive status is set. Skip over dead circuits, just add the ping
  741. // interval and push it to the back. Always remember to remove it
  742. // from the set before changing the sorting key (mNextPingSendTime)
  743. mPingSet.erase(psit);
  744. cdp->mNextPingSendTime = cur_time + mHeartbeatInterval;
  745. mPingSet.insert(cdp);
  746. continue;
  747. }
  748. else
  749. {
  750. // Check to see if this needs a ping
  751. if (cur_time < cdp->mNextPingSendTime)
  752. {
  753. // This circuit doesn't need a ping, break out because we have
  754. // a sorted list, thus no more circuits need pings
  755. break;
  756. }
  757. // Update watchdog timers
  758. if (cdp->updateWatchDogTimers(msgsys))
  759. {
  760. // Randomize our pings a bit by doing some up to 10% early or
  761. // late
  762. F64 dt = (F64)(0.9f * mHeartbeatInterval +
  763. ll_frand(0.2f * mHeartbeatInterval));
  764. // Remove it, and reinsert it with the new next ping time.
  765. // Always remove before changing the sorting key.
  766. mPingSet.erase(psit);
  767. cdp->mNextPingSendTime = cur_time + dt;
  768. mPingSet.insert(cdp);
  769. // Update our throttles
  770. cdp->mThrottles.dynamicAdjust();
  771. // Update some stats, this is not terribly important
  772. cdp->checkPeriodTime();
  773. }
  774. else
  775. {
  776. #if 0 // This mPingSet.erase isn't necessary, because removing the
  777. // circuit will remove the ping set.
  778. mPingSet.erase(psit);
  779. #endif
  780. removeCircuitData(cdp->mHost);
  781. }
  782. }
  783. }
  784. }
  785. bool LLCircuitData::updateWatchDogTimers(LLMessageSystem* msgsys)
  786. {
  787. F64 cur_time = LLMessageSystem::getMessageTimeSeconds();
  788. mLastPingSendTime = cur_time;
  789. if (!checkCircuitTimeout())
  790. {
  791. // Pass this back to the calling LLCircuit, this circuit needs to be
  792. // cleaned up.
  793. return false;
  794. }
  795. // WARNING !
  796. // Duplicate suppression can FAIL if packets are delivered out of order,
  797. // although it's EXTREMELY unlikely. It would require that the ping gets
  798. // delivered out of order enough that the ACK for the packet that it was
  799. // out of order with was received BEFORE the ping was sent.
  800. // Find the current oldest reliable packetID
  801. // This is to handle the case if we actually manage to wrap our/ packet IDs
  802. // - the oldest will actually have a higher packet ID than the current.
  803. bool wrapped = false;
  804. reliable_iter iter;
  805. iter = mUnackedPackets.upper_bound(getPacketOutID());
  806. if (iter == mUnackedPackets.end())
  807. {
  808. // Nothing AFTER this one, so we want the lowest packet ID
  809. // then.
  810. iter = mUnackedPackets.begin();
  811. wrapped = true;
  812. }
  813. TPACKETID packet_id = 0;
  814. // Check against the "final" packets
  815. bool wrapped_final = false;
  816. reliable_iter iter_final;
  817. iter_final = mFinalRetryPackets.upper_bound(getPacketOutID());
  818. if (iter_final == mFinalRetryPackets.end())
  819. {
  820. iter_final = mFinalRetryPackets.begin();
  821. wrapped_final = true;
  822. }
  823. LL_DEBUGS("Circuit") << mHost << " - unacked count "
  824. << mUnackedPackets.size() << " - final count "
  825. << mFinalRetryPackets.size() << LL_ENDL;
  826. if (wrapped != wrapped_final)
  827. {
  828. // One of the "unacked" or "final" lists hasn't wrapped. Whichever one
  829. // hasn't has the oldest packet.
  830. if (!wrapped)
  831. {
  832. // Hasn't wrapped, so the one on the
  833. // unacked packet list is older
  834. packet_id = iter->first;
  835. LL_DEBUGS("Circuit") << mHost << ": nowrapped unacked" << LL_ENDL;
  836. }
  837. else
  838. {
  839. packet_id = iter_final->first;
  840. LL_DEBUGS("Circuit") << mHost << ": nowrapped final" << LL_ENDL;
  841. }
  842. }
  843. else
  844. {
  845. // They both wrapped, we can just use the minimum of the two.
  846. if (iter == mUnackedPackets.end() &&
  847. iter_final == mFinalRetryPackets.end())
  848. {
  849. // Wow! No unacked packets at all!
  850. // Send the ID of the last packet we sent out.
  851. // This will flush all of the destination's
  852. // unacked packets, theoretically.
  853. LL_DEBUGS("Circuit") << mHost << ": No unacked !" << LL_ENDL;
  854. packet_id = getPacketOutID();
  855. }
  856. else
  857. {
  858. bool had_unacked = false;
  859. if (iter != mUnackedPackets.end())
  860. {
  861. // Unacked list has the lowest so far
  862. packet_id = iter->first;
  863. had_unacked = true;
  864. LL_DEBUGS("Circuit") << mHost << ": Unacked" << LL_ENDL;
  865. }
  866. if (iter_final != mFinalRetryPackets.end())
  867. {
  868. // Use the lowest of the unacked list and the final list
  869. if (had_unacked)
  870. {
  871. // Both had a packet, use the lowest.
  872. packet_id = llmin(packet_id, iter_final->first);
  873. LL_DEBUGS("Circuit") << mHost << ": Min of unacked/final"
  874. << LL_ENDL;
  875. }
  876. else
  877. {
  878. // Only the final had a packet, use it.
  879. packet_id = iter_final->first;
  880. LL_DEBUGS("Circuit") << mHost << ": Final !" << LL_ENDL;
  881. }
  882. }
  883. }
  884. }
  885. // Send off the another ping.
  886. pingTimerStart();
  887. msgsys->newMessageFast(_PREHASH_StartPingCheck);
  888. msgsys->nextBlock(_PREHASH_PingID);
  889. msgsys->addU8Fast(_PREHASH_PingID, nextPingID());
  890. msgsys->addU32Fast(_PREHASH_OldestUnacked, packet_id);
  891. msgsys->sendMessage(mHost);
  892. // Also do lost packet accounting. Check to see if anything on our lost
  893. // list is old enough to be considered lost
  894. LLCircuitData::packet_time_map::iterator it;
  895. U64 timeout = (U64)(1000000.0 * llmin(LL_MAX_LOST_TIMEOUT,
  896. getPingDelayAveraged() *
  897. LL_LOST_TIMEOUT_FACTOR));
  898. U64 mt_usec = LLMessageSystem::getMessageTimeUsecs();
  899. for (it = mPotentialLostPackets.begin();
  900. it != mPotentialLostPackets.end(); )
  901. {
  902. U64 delta_t_usec = mt_usec - it->second;
  903. if (delta_t_usec > timeout)
  904. {
  905. // let's call this one a loss!
  906. ++mPacketsLost;
  907. ++msgsys->mDroppedPackets;
  908. if (msgsys->mVerboseLog)
  909. {
  910. std::ostringstream str;
  911. str << "MSG: <- " << mHost << "\tLOST PACKET:\t"
  912. << it->first;
  913. llinfos << str.str() << llendl;
  914. }
  915. mPotentialLostPackets.erase(it++);
  916. }
  917. else
  918. {
  919. ++it;
  920. }
  921. }
  922. return true;
  923. }
  924. void LLCircuitData::clearDuplicateList(TPACKETID oldest_id)
  925. {
  926. // Purge old data from the duplicate suppression queue. We want to KEEP all
  927. // x where oldest_id <= x <= last incoming packet, and delete everything
  928. // else.
  929. LL_DEBUGS("Circuit") << mHost << ": clearing before oldest " << oldest_id
  930. << " - Recent list size before: "
  931. << mRecentlyReceivedReliablePackets.size() << LL_ENDL;
  932. if (oldest_id < mHighestPacketID)
  933. {
  934. // Clean up everything with a packet ID less than oldest_id.
  935. packet_time_map::iterator pit_start;
  936. packet_time_map::iterator pit_end;
  937. pit_start = mRecentlyReceivedReliablePackets.begin();
  938. pit_end = mRecentlyReceivedReliablePackets.lower_bound(oldest_id);
  939. mRecentlyReceivedReliablePackets.erase(pit_start, pit_end);
  940. }
  941. // Do timeout checks on everything with an ID > mHighestPacketID.
  942. // This should be empty except for wrapping IDs. Thus, this should be
  943. // highly rare.
  944. U64 mt_usec = LLMessageSystem::getMessageTimeUsecs();
  945. packet_time_map::iterator pit;
  946. for (pit = mRecentlyReceivedReliablePackets.upper_bound(mHighestPacketID);
  947. pit != mRecentlyReceivedReliablePackets.end(); )
  948. {
  949. // Validate that the packet ID seems far enough away
  950. if (pit->first - mHighestPacketID < 100)
  951. {
  952. llwarns << "Probably incorrectly timing out non-wrapped packets !"
  953. << llendl;
  954. }
  955. U64 delta_t_usec = mt_usec - pit->second;
  956. F64 delta_t_sec = delta_t_usec * SEC_PER_USEC;
  957. if (delta_t_sec > LL_DUPLICATE_SUPPRESSION_TIMEOUT)
  958. {
  959. // enough time has elapsed we're not likely to get a duplicate on
  960. // this one
  961. llinfos << "Clearing " << pit->first << " from recent list"
  962. << llendl;
  963. mRecentlyReceivedReliablePackets.erase(pit++);
  964. }
  965. else
  966. {
  967. ++pit;
  968. }
  969. }
  970. LL_DEBUGS("Circuit") << "Recent list size after: "
  971. << mRecentlyReceivedReliablePackets.size() << LL_ENDL;
  972. }
  973. bool LLCircuitData::checkCircuitTimeout()
  974. {
  975. F64 time_since_last_ping = LLMessageSystem::getMessageTimeSeconds() -
  976. mLastPingReceivedTime;
  977. // Nota Bene: This needs to be turned off if you are debugging multiple
  978. // simulators
  979. if (time_since_last_ping > mHeartbeatTimeout)
  980. {
  981. llinfos << "Considering: " << mHost << ", last ping "
  982. << time_since_last_ping << " seconds ago." << llendl;
  983. setAlive(false);
  984. if (mTimeoutCallback)
  985. {
  986. llinfos << "Calling callback for: " << mHost << llendl;
  987. mTimeoutCallback(mHost, mTimeoutUserData);
  988. }
  989. if (!isAlive())
  990. {
  991. // No callback, or the callback didn't try and resurrect the
  992. // circuit. We should kill it.
  993. if (mTimeoutCallback)
  994. {
  995. llwarns << mHost << " still dead after callback, dropping."
  996. << llendl;
  997. }
  998. else
  999. {
  1000. llinfos << mHost << " is dead, dropping." << llendl;
  1001. }
  1002. return false;
  1003. }
  1004. }
  1005. return true;
  1006. }
  1007. // Call this method when a reliable message comes in - this will correctly
  1008. // place the packet in the correct list to be acked later.
  1009. void LLCircuitData::collectRAck(TPACKETID packet_num)
  1010. {
  1011. if (mAcks.empty())
  1012. {
  1013. // First extra ack, we need to add ourselves to the list of circuits
  1014. // that need to send acks
  1015. gMessageSystemp->mCircuitInfo.mSendAckMap[mHost] = this;
  1016. }
  1017. mAcks.push_back(packet_num);
  1018. if (mAckCreationTime == 0.f)
  1019. {
  1020. mAckCreationTime = getAgeInSeconds();
  1021. }
  1022. }
  1023. // This method is called during the message system processAcks() to send out
  1024. // any ack that did not get sent already.
  1025. void LLCircuit::sendAcks(F32 collect_time)
  1026. {
  1027. LLMessageSystem* msg = gMessageSystemp;
  1028. LLCircuitData* cd;
  1029. circ_data_map_t::iterator it = mSendAckMap.begin();
  1030. collect_time = llclamp(collect_time, 0.f, LL_COLLECT_ACK_TIME_MAX);
  1031. while (it != mSendAckMap.end())
  1032. {
  1033. cd = it->second;
  1034. if (!cd) // Paranoia
  1035. {
  1036. mSendAckMap.erase(it++);
  1037. continue;
  1038. }
  1039. S32 count = (S32)cd->mAcks.size();
  1040. F32 age = cd->getAgeInSeconds() - cd->mAckCreationTime;
  1041. if (age > collect_time || count == 0)
  1042. {
  1043. if (count > 0)
  1044. {
  1045. // send the packet acks
  1046. S32 acks_this_packet = 0;
  1047. for (S32 i = 0; i < count; ++i)
  1048. {
  1049. if (acks_this_packet == 0)
  1050. {
  1051. msg->newMessageFast(_PREHASH_PacketAck);
  1052. }
  1053. msg->nextBlockFast(_PREHASH_Packets);
  1054. msg->addU32Fast(_PREHASH_ID, cd->mAcks[i]);
  1055. if (++acks_this_packet > 250)
  1056. {
  1057. msg->sendMessage(cd->mHost);
  1058. acks_this_packet = 0;
  1059. }
  1060. }
  1061. if (acks_this_packet > 0)
  1062. {
  1063. msg->sendMessage(cd->mHost);
  1064. }
  1065. if (msg->mVerboseLog)
  1066. {
  1067. std::ostringstream str;
  1068. str << "MSG: -> " << cd->mHost << "\tPACKET ACKS:\t";
  1069. std::ostream_iterator<TPACKETID> append(str, " ");
  1070. std::copy(cd->mAcks.begin(), cd->mAcks.end(), append);
  1071. llinfos << str.str() << llendl;
  1072. }
  1073. // empty out the acks list
  1074. cd->mAcks.clear();
  1075. cd->mAckCreationTime = 0.f;
  1076. }
  1077. // remove data map
  1078. mSendAckMap.erase(it++);
  1079. }
  1080. else
  1081. {
  1082. // continue collecting acks for this map
  1083. ++it;
  1084. }
  1085. }
  1086. }
  1087. std::ostream& operator<<(std::ostream& s, LLCircuitData& circuit)
  1088. {
  1089. F32 age = circuit.mExistenceTimer.getElapsedTimeF32();
  1090. using namespace std;
  1091. s << "Circuit " << circuit.mHost << " ";
  1092. s << circuit.mRemoteID << " ";
  1093. s << (circuit.mAlive ? "Alive" : "Not Alive") << " ";
  1094. s << (circuit.mAllowTimeout ? "Timeout Allowed" : "Timeout Not Allowed");
  1095. s << endl;
  1096. s << " Packets Lost: " << circuit.mPacketsLost;
  1097. s << " Measured Ping: " << circuit.mPingDelay;
  1098. s << " Averaged Ping: " << circuit.mPingDelayAveraged;
  1099. s << endl;
  1100. s << "Global In/Out " << S32(age) << " sec";
  1101. s << " KBytes: " << circuit.mBytesIn / 1024 << "/" << circuit.mBytesOut / 1024;
  1102. s << " Kbps: ";
  1103. s << S32(circuit.mBytesIn * 8.f / circuit.mExistenceTimer.getElapsedTimeF32() / 1024.f);
  1104. s << "/";
  1105. s << S32(circuit.mBytesOut * 8.f / circuit.mExistenceTimer.getElapsedTimeF32() / 1024.f);
  1106. s << " Packets: " << circuit.mPacketsIn << "/" << circuit.mPacketsOut;
  1107. s << endl;
  1108. s << "Recent In/Out " << S32(circuit.mLastPeriodLength) << " sec";
  1109. s << " KBytes: ";
  1110. s << circuit.mBytesInLastPeriod / 1024;
  1111. s << "/";
  1112. s << circuit.mBytesOutLastPeriod / 1024;
  1113. s << " Kbps: ";
  1114. s << S32(circuit.mBytesInLastPeriod * 8.f / circuit.mLastPeriodLength / 1024.f);
  1115. s << "/";
  1116. s << S32(circuit.mBytesOutLastPeriod * 8.f / circuit.mLastPeriodLength / 1024.f);
  1117. s << " Peak kbps: ";
  1118. s << S32(circuit.mPeakBPSIn / 1024.f);
  1119. s << "/";
  1120. s << S32(circuit.mPeakBPSOut / 1024.f);
  1121. s << endl;
  1122. return s;
  1123. }
  1124. void LLCircuitData::getInfo(LLSD& info) const
  1125. {
  1126. info["Host"] = mHost.getIPandPort();
  1127. info["Alive"] = mAlive;
  1128. info["Age"] = mExistenceTimer.getElapsedTimeF32();
  1129. }
  1130. void LLCircuitData::dumpResendCountAndReset()
  1131. {
  1132. if (mCurrentResendCount)
  1133. {
  1134. llinfos << "Circuit: " << mHost << " resent " << mCurrentResendCount
  1135. << " packets" << llendl;
  1136. mCurrentResendCount = 0;
  1137. }
  1138. }
  1139. std::ostream& operator<<(std::ostream& s, LLCircuit& circuit)
  1140. {
  1141. s << "Circuit Info:" << std::endl;
  1142. LLCircuit::circ_data_map_t::iterator end = circuit.mCircuitData.end();
  1143. LLCircuit::circ_data_map_t::iterator it;
  1144. for (it = circuit.mCircuitData.begin(); it != end; ++it)
  1145. {
  1146. s << *(it->second) << std::endl;
  1147. }
  1148. return s;
  1149. }
  1150. void LLCircuit::getInfo(LLSD& info) const
  1151. {
  1152. LLCircuit::circ_data_map_t::const_iterator end = mCircuitData.end();
  1153. LLCircuit::circ_data_map_t::const_iterator it;
  1154. LLSD circuit_info;
  1155. for (it = mCircuitData.begin(); it != end; ++it)
  1156. {
  1157. it->second->getInfo(circuit_info);
  1158. info["Circuits"].append(circuit_info);
  1159. }
  1160. }
  1161. void LLCircuit::getCircuitRange(const LLHost& key,
  1162. LLCircuit::circ_data_map_t::iterator& first,
  1163. LLCircuit::circ_data_map_t::iterator& end)
  1164. {
  1165. end = mCircuitData.end();
  1166. first = mCircuitData.upper_bound(key);
  1167. }
  1168. TPACKETID LLCircuitData::nextPacketOutID()
  1169. {
  1170. ++mPacketsOut;
  1171. TPACKETID id = (mPacketsOutID + 1) % LL_MAX_OUT_PACKET_ID;
  1172. if (id < mPacketsOutID)
  1173. {
  1174. // we just wrapped on a circuit, reset the wrap ID to zero
  1175. mWrapID = 0;
  1176. }
  1177. mPacketsOutID = id;
  1178. return id;
  1179. }
  1180. void LLCircuitData::setPacketInID(TPACKETID id)
  1181. {
  1182. id = id % LL_MAX_OUT_PACKET_ID;
  1183. mPacketsInID = id;
  1184. mRecentlyReceivedReliablePackets.clear();
  1185. mWrapID = id;
  1186. }
  1187. void LLCircuitData::pingTimerStop(U8 ping_id)
  1188. {
  1189. F64 mt_secs = LLMessageSystem::getMessageTimeSeconds();
  1190. // Nota Bene: no averaging of ping times until we get a feel for how this
  1191. // works
  1192. F64 time = mt_secs - mPingTime;
  1193. if (time == 0.0)
  1194. {
  1195. // Ack, we got our ping response on the same frame! Sigh, let's get a
  1196. // real time otherwise all of our ping calculations will be skewed.
  1197. mt_secs = LLMessageSystem::getMessageTimeSeconds(true);
  1198. }
  1199. mLastPingReceivedTime = mt_secs;
  1200. // If ping is longer than 1 second, we'll get sequence deltas in the ping.
  1201. // Approximate by assuming each ping counts for 1 second (slightly low,
  1202. // probably)
  1203. S32 delta_ping = (S32)mLastPingID - (S32)ping_id;
  1204. if (delta_ping < 0)
  1205. {
  1206. delta_ping += 256;
  1207. }
  1208. U32 msec = (U32) ((delta_ping*mHeartbeatInterval + time) * 1000.f);
  1209. setPingDelay(msec);
  1210. mPingsInTransit = delta_ping;
  1211. if (mBlocked && mPingsInTransit <= PING_RELEASE_BLOCK)
  1212. {
  1213. mBlocked = false;
  1214. }
  1215. }
  1216. void LLCircuitData::pingTimerStart()
  1217. {
  1218. mPingTime = LLMessageSystem::getMessageTimeSeconds();
  1219. ++mPingsInTransit;
  1220. if (!mBlocked && mPingsInTransit > PING_START_BLOCK)
  1221. {
  1222. mBlocked = true;
  1223. }
  1224. }
  1225. F32 LLCircuitData::getPingInTransitTime()
  1226. {
  1227. // This may be inaccurate in the case of a circuit that was "dead" and then
  1228. // revived, but only until the first round trip ping is sent - djs
  1229. F32 time_since_ping_was_sent = 0;
  1230. if (mPingsInTransit)
  1231. {
  1232. time_since_ping_was_sent = (F32)(mPingsInTransit * mHeartbeatInterval - 1 +
  1233. LLMessageSystem::getMessageTimeSeconds() -
  1234. mPingTime) * 1000.f;
  1235. }
  1236. return time_since_ping_was_sent;
  1237. }
  1238. void LLCircuitData::setPingDelay(U32 ping)
  1239. {
  1240. mPingDelay = ping;
  1241. mPingDelayAveraged = llmax((F32)ping, getPingDelayAveraged());
  1242. mPingDelayAveraged = (1.f - LL_AVERAGED_PING_ALPHA) * mPingDelayAveraged +
  1243. LL_AVERAGED_PING_ALPHA * (F32)ping;
  1244. mPingDelayAveraged = llclamp(mPingDelayAveraged,
  1245. LL_AVERAGED_PING_MIN,
  1246. LL_AVERAGED_PING_MAX);
  1247. }
  1248. F32 LLCircuitData::getPingDelayAveraged()
  1249. {
  1250. return llmin(llmax(getPingInTransitTime(), mPingDelayAveraged),
  1251. LL_AVERAGED_PING_MAX);
  1252. }
  1253. F32 LLCircuitData::getAgeInSeconds() const
  1254. {
  1255. return mExistenceTimer.getElapsedTimeF32();
  1256. }