aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ClientStack/LindenUDP/LLUDPClient.cs
diff options
context:
space:
mode:
authorMelanie2009-10-08 08:07:38 +0100
committerMelanie2009-10-08 08:07:38 +0100
commitfe679be9e76190ac0dc8892469787e63a7a48b5c (patch)
tree220ef33da75f09b3e7ef3684c5ed7367e175691e /OpenSim/Region/ClientStack/LindenUDP/LLUDPClient.cs
parentstore owner_uuid in the region table (diff)
parentOne last attempt at tunning the locking/no locking behaviour. The previous on... (diff)
downloadopensim-SC_OLD-fe679be9e76190ac0dc8892469787e63a7a48b5c.zip
opensim-SC_OLD-fe679be9e76190ac0dc8892469787e63a7a48b5c.tar.gz
opensim-SC_OLD-fe679be9e76190ac0dc8892469787e63a7a48b5c.tar.bz2
opensim-SC_OLD-fe679be9e76190ac0dc8892469787e63a7a48b5c.tar.xz
Merge branch 'htb-throttle'
Diffstat (limited to 'OpenSim/Region/ClientStack/LindenUDP/LLUDPClient.cs')
-rw-r--r--OpenSim/Region/ClientStack/LindenUDP/LLUDPClient.cs442
1 files changed, 442 insertions, 0 deletions
diff --git a/OpenSim/Region/ClientStack/LindenUDP/LLUDPClient.cs b/OpenSim/Region/ClientStack/LindenUDP/LLUDPClient.cs
new file mode 100644
index 0000000..871e8e8
--- /dev/null
+++ b/OpenSim/Region/ClientStack/LindenUDP/LLUDPClient.cs
@@ -0,0 +1,442 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System;
29using System.Collections.Generic;
30using System.Net;
31using OpenSim.Framework;
32using OpenMetaverse;
33
34namespace OpenSim.Region.ClientStack.LindenUDP
35{
36 #region Delegates
37
38 /// <summary>
39 /// Fired when updated networking stats are produced for this client
40 /// </summary>
41 /// <param name="inPackets">Number of incoming packets received since this
42 /// event was last fired</param>
43 /// <param name="outPackets">Number of outgoing packets sent since this
44 /// event was last fired</param>
45 /// <param name="unAckedBytes">Current total number of bytes in packets we
46 /// are waiting on ACKs for</param>
47 public delegate void PacketStats(int inPackets, int outPackets, int unAckedBytes);
48 /// <summary>
49 /// Fired when the queue for a packet category is empty. This event can be
50 /// hooked to put more data on the empty queue
51 /// </summary>
52 /// <param name="category">Category of the packet queue that is empty</param>
53 public delegate void QueueEmpty(ThrottleOutPacketType category);
54
55 #endregion Delegates
56
57 /// <summary>
58 /// Tracks state for a client UDP connection and provides client-specific methods
59 /// </summary>
60 public sealed class LLUDPClient
61 {
62 /// <summary>The number of packet categories to throttle on. If a throttle category is added
63 /// or removed, this number must also change</summary>
64 const int THROTTLE_CATEGORY_COUNT = 7;
65
66 /// <summary>Fired when updated networking stats are produced for this client</summary>
67 public event PacketStats OnPacketStats;
68 /// <summary>Fired when the queue for a packet category is empty. This event can be
69 /// hooked to put more data on the empty queue</summary>
70 public event QueueEmpty OnQueueEmpty;
71
72 /// <summary>AgentID for this client</summary>
73 public readonly UUID AgentID;
74 /// <summary>The remote address of the connected client</summary>
75 public readonly IPEndPoint RemoteEndPoint;
76 /// <summary>Circuit code that this client is connected on</summary>
77 public readonly uint CircuitCode;
78 /// <summary>Sequence numbers of packets we've received (for duplicate checking)</summary>
79 public readonly IncomingPacketHistoryCollection PacketArchive = new IncomingPacketHistoryCollection(200);
80 /// <summary>Packets we have sent that need to be ACKed by the client</summary>
81 public readonly UnackedPacketCollection NeedAcks = new UnackedPacketCollection();
82 /// <summary>ACKs that are queued up, waiting to be sent to the client</summary>
83 public readonly LocklessQueue<uint> PendingAcks = new LocklessQueue<uint>();
84
85 /// <summary>Reference to the IClientAPI for this client</summary>
86 public LLClientView ClientAPI;
87 /// <summary>Current packet sequence number</summary>
88 public int CurrentSequence;
89 /// <summary>Current ping sequence number</summary>
90 public byte CurrentPingSequence;
91 /// <summary>True when this connection is alive, otherwise false</summary>
92 public bool IsConnected = true;
93 /// <summary>True when this connection is paused, otherwise false</summary>
94 public bool IsPaused = true;
95 /// <summary>Environment.TickCount when the last packet was received for this client</summary>
96 public int TickLastPacketReceived;
97
98 /// <summary>Timer granularity. This is set to the measured resolution of Environment.TickCount</summary>
99 public readonly float G;
100 /// <summary>Smoothed round-trip time. A smoothed average of the round-trip time for sending a
101 /// reliable packet to the client and receiving an ACK</summary>
102 public float SRTT;
103 /// <summary>Round-trip time variance. Measures the consistency of round-trip times</summary>
104 public float RTTVAR;
105 /// <summary>Retransmission timeout. Packets that have not been acknowledged in this number of
106 /// milliseconds or longer will be resent</summary>
107 /// <remarks>Calculated from <seealso cref="SRTT"/> and <seealso cref="RTTVAR"/> using the
108 /// guidelines in RFC 2988</remarks>
109 public int RTO;
110 /// <summary>Number of bytes received since the last acknowledgement was sent out. This is used
111 /// to loosely follow the TCP delayed ACK algorithm in RFC 1122 (4.2.3.2)</summary>
112 public int BytesSinceLastACK;
113 /// <summary>Number of packets received from this client</summary>
114 public int PacketsReceived;
115 /// <summary>Number of packets sent to this client</summary>
116 public int PacketsSent;
117 /// <summary>Total byte count of unacked packets sent to this client</summary>
118 public int UnackedBytes;
119
120 /// <summary>Total number of received packets that we have reported to the OnPacketStats event(s)</summary>
121 private int m_packetsReceivedReported;
122 /// <summary>Total number of sent packets that we have reported to the OnPacketStats event(s)</summary>
123 private int m_packetsSentReported;
124
125 /// <summary>Throttle bucket for this agent's connection</summary>
126 private readonly TokenBucket throttle;
127 /// <summary>Throttle buckets for each packet category</summary>
128 private readonly TokenBucket[] throttleCategories;
129 /// <summary>Throttle rate defaults and limits</summary>
130 private readonly ThrottleRates defaultThrottleRates;
131 /// <summary>Outgoing queues for throttled packets</summary>
132 private readonly LocklessQueue<OutgoingPacket>[] packetOutboxes = new LocklessQueue<OutgoingPacket>[THROTTLE_CATEGORY_COUNT];
133 /// <summary>A container that can hold one packet for each outbox, used to store
134 /// dequeued packets that are being held for throttling</summary>
135 private readonly OutgoingPacket[] nextPackets = new OutgoingPacket[THROTTLE_CATEGORY_COUNT];
136 /// <summary>An optimization to store the length of dequeued packets being held
137 /// for throttling. This avoids expensive calls to Packet.Length</summary>
138 private readonly int[] nextPacketLengths = new int[THROTTLE_CATEGORY_COUNT];
139 /// <summary>A reference to the LLUDPServer that is managing this client</summary>
140 private readonly LLUDPServer udpServer;
141
142 /// <summary>
143 /// Default constructor
144 /// </summary>
145 /// <param name="server">Reference to the UDP server this client is connected to</param>
146 /// <param name="rates">Default throttling rates and maximum throttle limits</param>
147 /// <param name="parentThrottle">Parent HTB (hierarchical token bucket)
148 /// that the child throttles will be governed by</param>
149 /// <param name="circuitCode">Circuit code for this connection</param>
150 /// <param name="agentID">AgentID for the connected agent</param>
151 /// <param name="remoteEndPoint">Remote endpoint for this connection</param>
152 public LLUDPClient(LLUDPServer server, ThrottleRates rates, TokenBucket parentThrottle, uint circuitCode, UUID agentID, IPEndPoint remoteEndPoint)
153 {
154 udpServer = server;
155 AgentID = agentID;
156 RemoteEndPoint = remoteEndPoint;
157 CircuitCode = circuitCode;
158 defaultThrottleRates = rates;
159
160 for (int i = 0; i < THROTTLE_CATEGORY_COUNT; i++)
161 packetOutboxes[i] = new LocklessQueue<OutgoingPacket>();
162
163 throttle = new TokenBucket(parentThrottle, 0, 0);
164 throttleCategories = new TokenBucket[THROTTLE_CATEGORY_COUNT];
165 throttleCategories[(int)ThrottleOutPacketType.Resend] = new TokenBucket(throttle, rates.ResendLimit, rates.Resend);
166 throttleCategories[(int)ThrottleOutPacketType.Land] = new TokenBucket(throttle, rates.LandLimit, rates.Land);
167 throttleCategories[(int)ThrottleOutPacketType.Wind] = new TokenBucket(throttle, rates.WindLimit, rates.Wind);
168 throttleCategories[(int)ThrottleOutPacketType.Cloud] = new TokenBucket(throttle, rates.CloudLimit, rates.Cloud);
169 throttleCategories[(int)ThrottleOutPacketType.Task] = new TokenBucket(throttle, rates.TaskLimit, rates.Task);
170 throttleCategories[(int)ThrottleOutPacketType.Texture] = new TokenBucket(throttle, rates.TextureLimit, rates.Texture);
171 throttleCategories[(int)ThrottleOutPacketType.Asset] = new TokenBucket(throttle, rates.AssetLimit, rates.Asset);
172
173 // Set the granularity variable used for retransmission calculations to
174 // the measured resolution of Environment.TickCount
175 G = server.TickCountResolution;
176
177 // Default the retransmission timeout to three seconds
178 RTO = 3000;
179 }
180
181 /// <summary>
182 /// Shuts down this client connection
183 /// </summary>
184 public void Shutdown()
185 {
186 // TODO: Do we need to invalidate the circuit?
187 IsConnected = false;
188 }
189
190 /// <summary>
191 /// Gets information about this client connection
192 /// </summary>
193 /// <returns>Information about the client connection</returns>
194 public ClientInfo GetClientInfo()
195 {
196 // TODO: This data structure is wrong in so many ways. Locking and copying the entire lists
197 // of pending and needed ACKs for every client every time some method wants information about
198 // this connection is a recipe for poor performance
199 ClientInfo info = new ClientInfo();
200 info.pendingAcks = new Dictionary<uint, uint>();
201 info.needAck = new Dictionary<uint, byte[]>();
202
203 info.resendThrottle = throttleCategories[(int)ThrottleOutPacketType.Resend].DripRate;
204 info.landThrottle = throttleCategories[(int)ThrottleOutPacketType.Land].DripRate;
205 info.windThrottle = throttleCategories[(int)ThrottleOutPacketType.Wind].DripRate;
206 info.cloudThrottle = throttleCategories[(int)ThrottleOutPacketType.Cloud].DripRate;
207 info.taskThrottle = throttleCategories[(int)ThrottleOutPacketType.Task].DripRate;
208 info.assetThrottle = throttleCategories[(int)ThrottleOutPacketType.Asset].DripRate;
209 info.textureThrottle = throttleCategories[(int)ThrottleOutPacketType.Texture].DripRate;
210 info.totalThrottle = info.resendThrottle + info.landThrottle + info.windThrottle + info.cloudThrottle +
211 info.taskThrottle + info.assetThrottle + info.textureThrottle;
212
213 return info;
214 }
215
216 /// <summary>
217 /// Modifies the UDP throttles
218 /// </summary>
219 /// <param name="info">New throttling values</param>
220 public void SetClientInfo(ClientInfo info)
221 {
222 // TODO: Allowing throttles to be manually set from this function seems like a reasonable
223 // idea. On the other hand, letting external code manipulate our ACK accounting is not
224 // going to happen
225 throw new NotImplementedException();
226 }
227
228 public string GetStats()
229 {
230 // TODO: ???
231 return string.Format("{0,7} {1,7} {2,7} {3,7} {4,7} {5,7} {6,7} {7,7} {8,7} {9,7}",
232 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
233 }
234
235 public void SendPacketStats()
236 {
237 PacketStats callback = OnPacketStats;
238 if (callback != null)
239 {
240 int newPacketsReceived = PacketsReceived - m_packetsReceivedReported;
241 int newPacketsSent = PacketsSent - m_packetsSentReported;
242
243 callback(newPacketsReceived, newPacketsSent, UnackedBytes);
244
245 m_packetsReceivedReported += newPacketsReceived;
246 m_packetsSentReported += newPacketsSent;
247 }
248 }
249
250 public void SetThrottles(byte[] throttleData)
251 {
252 byte[] adjData;
253 int pos = 0;
254
255 if (!BitConverter.IsLittleEndian)
256 {
257 byte[] newData = new byte[7 * 4];
258 Buffer.BlockCopy(throttleData, 0, newData, 0, 7 * 4);
259
260 for (int i = 0; i < 7; i++)
261 Array.Reverse(newData, i * 4, 4);
262
263 adjData = newData;
264 }
265 else
266 {
267 adjData = throttleData;
268 }
269
270 int resend = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
271 int land = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
272 int wind = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
273 int cloud = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
274 int task = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
275 int texture = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
276 int asset = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f);
277
278 resend = (resend <= defaultThrottleRates.ResendLimit) ? resend : defaultThrottleRates.ResendLimit;
279 land = (land <= defaultThrottleRates.LandLimit) ? land : defaultThrottleRates.LandLimit;
280 wind = (wind <= defaultThrottleRates.WindLimit) ? wind : defaultThrottleRates.WindLimit;
281 cloud = (cloud <= defaultThrottleRates.CloudLimit) ? cloud : defaultThrottleRates.CloudLimit;
282 task = (task <= defaultThrottleRates.TaskLimit) ? task : defaultThrottleRates.TaskLimit;
283 texture = (texture <= defaultThrottleRates.TextureLimit) ? texture : defaultThrottleRates.TextureLimit;
284 asset = (asset <= defaultThrottleRates.AssetLimit) ? asset : defaultThrottleRates.AssetLimit;
285
286 SetThrottle(ThrottleOutPacketType.Resend, resend);
287 SetThrottle(ThrottleOutPacketType.Land, land);
288 SetThrottle(ThrottleOutPacketType.Wind, wind);
289 SetThrottle(ThrottleOutPacketType.Cloud, cloud);
290 SetThrottle(ThrottleOutPacketType.Task, task);
291 SetThrottle(ThrottleOutPacketType.Texture, texture);
292 SetThrottle(ThrottleOutPacketType.Asset, asset);
293 }
294
295 public byte[] GetThrottlesPacked()
296 {
297 byte[] data = new byte[7 * 4];
298 int i = 0;
299
300 Buffer.BlockCopy(Utils.FloatToBytes((float)throttleCategories[(int)ThrottleOutPacketType.Resend].DripRate), 0, data, i, 4); i += 4;
301 Buffer.BlockCopy(Utils.FloatToBytes((float)throttleCategories[(int)ThrottleOutPacketType.Land].DripRate), 0, data, i, 4); i += 4;
302 Buffer.BlockCopy(Utils.FloatToBytes((float)throttleCategories[(int)ThrottleOutPacketType.Wind].DripRate), 0, data, i, 4); i += 4;
303 Buffer.BlockCopy(Utils.FloatToBytes((float)throttleCategories[(int)ThrottleOutPacketType.Cloud].DripRate), 0, data, i, 4); i += 4;
304 Buffer.BlockCopy(Utils.FloatToBytes((float)throttleCategories[(int)ThrottleOutPacketType.Task].DripRate), 0, data, i, 4); i += 4;
305 Buffer.BlockCopy(Utils.FloatToBytes((float)throttleCategories[(int)ThrottleOutPacketType.Texture].DripRate), 0, data, i, 4); i += 4;
306 Buffer.BlockCopy(Utils.FloatToBytes((float)throttleCategories[(int)ThrottleOutPacketType.Asset].DripRate), 0, data, i, 4); i += 4;
307
308 return data;
309 }
310
311 public void SetThrottle(ThrottleOutPacketType category, int rate)
312 {
313 int i = (int)category;
314 if (i >= 0 && i < throttleCategories.Length)
315 {
316 TokenBucket bucket = throttleCategories[(int)category];
317 bucket.MaxBurst = rate;
318 bucket.DripRate = rate;
319 }
320 }
321
322 public bool EnqueueOutgoing(OutgoingPacket packet)
323 {
324 int category = (int)packet.Category;
325
326 if (category >= 0 && category < packetOutboxes.Length)
327 {
328 LocklessQueue<OutgoingPacket> queue = packetOutboxes[category];
329 TokenBucket bucket = throttleCategories[category];
330
331 if (throttleCategories[category].RemoveTokens(packet.Buffer.DataLength))
332 {
333 // Enough tokens were removed from the bucket, the packet will not be queued
334 return false;
335 }
336 else
337 {
338 // Not enough tokens in the bucket, queue this packet
339 queue.Enqueue(packet);
340 return true;
341 }
342 }
343 else
344 {
345 // We don't have a token bucket for this category, so it will not be queued
346 return false;
347 }
348 }
349
350 /// <summary>
351 /// Loops through all of the packet queues for this client and tries to send
352 /// any outgoing packets, obeying the throttling bucket limits
353 /// </summary>
354 /// <remarks>This function is only called from a synchronous loop in the
355 /// UDPServer so we don't need to bother making this thread safe</remarks>
356 /// <returns>True if any packets were sent, otherwise false</returns>
357 public bool DequeueOutgoing()
358 {
359 OutgoingPacket packet;
360 LocklessQueue<OutgoingPacket> queue;
361 TokenBucket bucket;
362 bool packetSent = false;
363
364 for (int i = 0; i < THROTTLE_CATEGORY_COUNT; i++)
365 {
366 bucket = throttleCategories[i];
367
368 if (nextPackets[i] != null)
369 {
370 // This bucket was empty the last time we tried to send a packet,
371 // leaving a dequeued packet still waiting to be sent out. Try to
372 // send it again
373 if (bucket.RemoveTokens(nextPacketLengths[i]))
374 {
375 // Send the packet
376 udpServer.SendPacketFinal(nextPackets[i]);
377 nextPackets[i] = null;
378 packetSent = true;
379 }
380 }
381 else
382 {
383 // No dequeued packet waiting to be sent, try to pull one off
384 // this queue
385 queue = packetOutboxes[i];
386 if (queue.Dequeue(out packet))
387 {
388 // A packet was pulled off the queue. See if we have
389 // enough tokens in the bucket to send it out
390 if (bucket.RemoveTokens(packet.Buffer.DataLength))
391 {
392 // Send the packet
393 udpServer.SendPacketFinal(packet);
394 packetSent = true;
395 }
396 else
397 {
398 // Save the dequeued packet and the length calculation for
399 // the next iteration
400 nextPackets[i] = packet;
401 nextPacketLengths[i] = packet.Buffer.DataLength;
402 }
403 }
404 else
405 {
406 // No packets in this queue. Fire the queue empty callback
407 QueueEmpty callback = OnQueueEmpty;
408 if (callback != null)
409 callback((ThrottleOutPacketType)i);
410 }
411 }
412 }
413
414 return packetSent;
415 }
416
417 public void UpdateRoundTrip(float r)
418 {
419 const float ALPHA = 0.125f;
420 const float BETA = 0.25f;
421 const float K = 4.0f;
422
423 if (RTTVAR == 0.0f)
424 {
425 // First RTT measurement
426 SRTT = r;
427 RTTVAR = r * 0.5f;
428 }
429 else
430 {
431 // Subsequence RTT measurement
432 RTTVAR = (1.0f - BETA) * RTTVAR + BETA * Math.Abs(SRTT - r);
433 SRTT = (1.0f - ALPHA) * SRTT + ALPHA * r;
434 }
435
436 // Always round retransmission timeout up to two seconds
437 RTO = Math.Max(2000, (int)(SRTT + Math.Max(G, K * RTTVAR)));
438 //Logger.Debug("Setting agent " + this.Agent.FullName + "'s RTO to " + RTO + "ms with an RTTVAR of " +
439 // RTTVAR + " based on new RTT of " + r + "ms");
440 }
441 }
442}