142b244f2404217ad007a08f580301364480bdb8
[quix0rs-apt-p2p.git] / docs / motivation / motivation.tex
1 \documentclass[conference]{IEEEtran}
2
3 \usepackage[noadjust]{cite}
4
5 \usepackage[dvips]{graphicx}
6
7 \usepackage{url}
8 \usepackage[cmex10]{amsmath}
9 \interdisplaylinepenalty=2500
10 \usepackage{threeparttable}
11 \usepackage{multirow}
12
13 \begin{document}
14
15 \title{Using Altruistic Peers to Help With Free Web Downloads}
16 \author{\IEEEauthorblockN{Cameron Dale}
17 \IEEEauthorblockA{School of Computing Science\\
18 Simon Fraser University\\
19 Burnaby, British Columbia, Canada\\
20 Email: camerond@cs.sfu.ca}}
21
22 \maketitle
23
24 \begin{abstract}
25 A large amount of free content is available over the Internet from
26 many different sources. Most of this content uses the traditional
27 client-server model of requests from users. However, there is an
28 opportunity to use peer-to-peer techniques to reduce the cost of
29 much of this distribution, especially due to the altruistic nature
30 of many of these users. We present a general technique for
31 satisfying the needs of this P2P distribution, which is applicable
32 to many of these distributors systems. Our method makes use of a DHT
33 for storing the location of peers, using the cryptographic hash of
34 the file as a key. We then go further and implement a solution for
35 the distribution of Debian software packages.
36 \end{abstract}
37
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%  Section  %%%%%%%%%%%%%%%%%%%%%%%%%%%%
39
40 \section{Introduction}
41 \label{intro}
42
43 There are a large number of free content distributors with using
44 package distribution systems over the Internet to distribute content
45 to their users. These distributors have developed many different
46 methods for this distribution, but they mostly use a client-server
47 model to satisfy user requests. The popularity and number of users
48 results in a large number of requests, which usually requires a
49 network of mirrors to handle. Due to the free nature of this
50 content, many users are willing and able to contribute upload
51 bandwidth to this distribution, but have no current way to do this.
52
53 We present a new peer-to-peer distribution model to meet these
54 demands. It is based on many previous implementations of successful
55 peer-to-peer protocols, including distributed hash tables (DHT) and
56 BitTorrent. The model relies on the pre-existence of cryptographic
57 hashes of the content, which should uniquely identify it for a
58 request from other peers. If the peer-to-peer download fails, then
59 the original request to the server is used as a fallback to prevent
60 any dissatisfaction from mirrors. The peer can then share this new
61 content with others.
62
63 First, we examine the opportunity that is available for many of
64 these free content distributors. We present an overview of a system
65 that would efficiently satisfy the demands of a large number of
66 users, and should substantially reduce the currently substantial
67 bandwidth requirements of hosting this content. We then present an
68 example implementation based on the Debian package distribution
69 system. This implementation will be used by a large number of users,
70 and serves as an example for other free content distributors of the
71 opportunity that can be met with such a system.
72
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%  Section  %%%%%%%%%%%%%%%%%%%%%%%%%%%%
74
75 \section{Related Work}
76 \label{related}
77
78 There are many peer-to-peer implementations available today, but
79 none is very well suited to this specific problem.
80
81 Many distributors make their content available in some form using
82 BitTorrent \cite{COHEN03}, e.g. for the distribution of CD
83 images. This is not an ideal situation though, as it requires the
84 peers to download large amounts of content that they are not
85 interested in, and prevents them from updating to new content
86 without downloading another image containing a lot of the same
87 content they already have. There have also been other attempted
88 implementations, usually based on some form of BitTorrent, to
89 address this problem. Unfortunately, BitTorrent is not ideally
90 suited to an application such as this one, for the following
91 reasons:
92 \begin{itemize}
93  \item The packages are too small and there are too many to create
94        individual torrents for each one.
95  \item All the packages together are too large to track efficiently
96        as a single torrent.
97  \item BitTorrent's piece sizes are bigger than many of the
98        packages, which wastes bandwidth downloading parts of other
99        packages.
100  \item A small number of the packages can be updated every day,
101        which may require a new torrent, therefore splitting the
102        download population.
103  \item Incentives to share (upload) are no longer needed, as the
104        content is freely available for anyone to download without
105        sharing (seeds are also not needed).
106 \end{itemize}
107
108 Some other implementations have used BitTorrent almost unmodified,
109 while others have only looked at using DHTs to replace the tracker
110 in a BitTorrent system. apt-torrent \cite{apttorrent} creates
111 torrents for some of the larger content available, but it ignores
112 the smaller content, which is often the more requested content.
113 DebTorrent \cite{debtorrent} makes widespread modifications to a
114 traditional BitTorrent client, but also requires some changes to the
115 distribution system to support it, and separates peers into groups
116 based on their characteristics which limits the effectiveness of the
117 sharing. Kenosis \cite{kenosis} is a P2P Remote Procedure Call
118 client also based on the Kademlia DHT, but it is meant as a P2P
119 primitive system on which other tools can be built, and so it has no
120 file sharing functionality at all. Many have used a DHT as a drop-in
121 replacement for the tracker in a BitTorrent system
122 \cite{bittorrent-dht, azureus-dht}, but such systems only use the
123 DHT to find peers for the same torrent, so the file sharing uses
124 traditional BitTorrent and so is not ideal for the reasons listed
125 above.
126
127 %%%%%%%%%%%%%%%%%%%%%%%%%%%  Section  %%%%%%%%%%%%%%%%%%%%%%%%%%%%
128
129 \section{Situation}
130 \label{situation}
131
132 There are a large number of groups using the Internet to distribute
133 their free content. This content is usually available from a free
134 download site, which usually requires a network of mirrors to
135 support the large number of requests. This content almost always
136 supports some kind of file verification, usually cryptographic
137 hashes, to verify the completed downloads accuracy or authenticity.
138
139 \subsection{Examples}
140 \label{examples}
141
142 Most Linux distributions use a software package management system
143 that fetches packages to be installed from a network of mirrors. The
144 Debian project \cite{debian} (and other Debian-based distributions)
145 uses the \texttt{apt} program, which downloads Debian packages in
146 the \texttt{.deb} format from one of many mirrors. The program will
147 first download index files that contain a listing of which packages
148 are available, as well as important information such as their size,
149 location, and a hash of their content. Red Hat's Fedora project
150 \cite{fedora} (and other RPM-based distributions) use the
151 \texttt{yum} program to obtain RPMs, and Gentoo \cite{gentoo} uses
152 \texttt{portage} in a similar way.
153
154 Other software vendors also use a similar system. CPAN \cite{cpan}
155 distrbutes files containing software packages for the PERL
156 programming language. CYGWIN \cite{cygwin} provides many of the
157 standard Unix/Linux tools in a Windows environment, also using a
158 package management tool that requests packages from websites. There
159 are two software distribution systems for Mac OSX, fink and
160 MacPorts, that also retrieve packages in this way.
161
162 Also, some systems use direct web downloads, but with a hash
163 verification file also available for download next to the desired
164 file. These hash files usually have the same file name, but with an
165 added extension identifying the hash used (e.g. \texttt{.md5} for
166 the MD5 hash).
167
168 Finally, there are other programs that use cryptographic hashing to
169 identify files. Git is a version control system in which all files,
170 commits, and tags, are identified by their SHA1 hash. These hashes
171 are used to verify the origin of these items, but are also used when
172 clients update their local files with new remote information.
173
174 \subsection{Similarities}
175 \label{similarities}
176
177 The important things to note for each of the systems mentioned in
178 section \ref{examples}, is that they all have the following in
179 common:
180 \begin{itemize}
181  \item The content is avaliable for anyone to freely download.
182  \item The content is divided into packages, each of which contains
183        a small independent part of all the content.
184  \item No peers are interested in downloading all of the content
185        available.
186  \item Hashes of the content and its size are available before the
187        download is attempted.
188  \item Requests for the content are sent by a tool, not directly by
189        the user, though the tool is responding to requests from the user.
190 \end{itemize}
191
192 We also expect that there are a number of users of these systems
193 that are motivated by altruism to want to help out with this
194 distribution. This is common in these systems due to the free nature
195 of the content being delivered, which encourages some to want to
196 help out in some way. A number of the systems are also used by
197 groups that are staffed mostly, or sometimes completely, by
198 volunteers. This also encourages users to want to \emph{give back}
199 to the volunteer community that has created the content they are
200 using.
201
202 \subsection{Differences}
203 \label{differences}
204
205 Although at first it might seem that a single all-reaching solution
206 is possible for this situation, there are some differences in each
207 system that require independent solutions. The systems all use
208 different tools for their distribution, so any implementation would
209 have to be specific to the tool it is to integrate with. In
210 particular, how to obtain requests from the tool or user, and how to
211 find a hash that corresponds to the file being requested, is very
212 specific to each system.
213
214 Also, there may be no benefit in creating a single large solution to
215 integrate all these problems. For one, though they sometimes
216 distribute nearly identical content (e.g. the same software
217 available in multiple Linux distributions), it is not exactly
218 identical and has usually been tailored to the system. The small
219 differences will make it impossible to distribute similar content
220 across systems. And, although peer-to-peer systems scale very well
221 with the number of peers in the system, there is some overhead
222 involved. So, having a much larger system of peers would mean that
223 requests could take longer to complete.
224
225 %%%%%%%%%%%%%%%%%%%%%%%%%%%  Section  %%%%%%%%%%%%%%%%%%%%%%%%%%%%
226
227 \section{Opportunity}
228 \label{opportunity}
229
230 The situation described in section \ref{situation} presents a clear
231 opportunity to use some form of peer-to-peer file sharing protocols.
232 This sparse interest in a large number of packages, and constant
233 updating, is well suited to the functionality provided by a
234 Distributed Hash Table (DHT). DHTs require unique keys to store and
235 retrieve strings of data, for which the cryptographic hashes used by
236 these package management systems are perfect for. The stored and
237 retrieved strings can then be pointers to the peers that have the
238 content package that hashes to that key. A downloading peer can then
239 lookup the package hash in the DHT, and then, if it is found,
240 download the file from those peers and verify the content. If the
241 package hash can not be found in the DHT, the peer will fallback to
242 downloading from the original content location (i.e. the network of
243 mirrors), and once complete will add a new entry to the DHT
244 indicating that it has the package.
245
246 \subsection{Implementation Options}
247 \label{imp_options}
248
249 There are several ways to implement the desired P2P functionality
250 into the existing package management software. The functionality can
251 be directly integrated into the software, though this can be
252 difficult as the DHT should be running at all times, both for
253 efficient lookups and to support uploading of already downloaded
254 content. Many of the package management software implmentations use
255 HTTP requests to download the files, which makes it possible to
256 implement the P2P aspect as a standard HTTP caching proxy, which
257 will get uncached requests first from the P2P system, and then
258 fallback to the normal HTTP request from a server. For methods that
259 don't use HTTP requests, other types of proxies may also be
260 possible.
261
262 \subsection{Downloading From Peers}
263 \label{downloading}
264
265 Downloading a file efficiently from a number of peers is where
266 BitTorrent shines as a peer-to-peer application. Its method of
267 breaking up larger files into sub-pieces, each with its own hash,
268 makes it very easy to parallelize the downloading process and
269 maximize the download speed. For very small packages (i.e. less than
270 the sub-piece size), this parallel downloading is not necessary, or
271 even desirable. However, this method can still be used in
272 conjunction with the DHT, for the larger packages that are
273 available.
274
275 In addition to storing the file download location (which would still
276 be used for small files), a peer will store a \emph{torrent string}
277 containing the peer's hashes of the individual pieces for the larger
278 files. These piece hashes could be compared ahead of time to
279 determine which peers have the same piece hashes (they all should),
280 and then used during the download to verify the downloaded pieces.
281
282 For very large files (about 5 or more pieces), the torrent strings
283 are too long to store in the DHT (a single UDP packet should be less
284 than 1472 bytes to avoid fragmentation). Instead, the peers will
285 store the torrent string for large files separately in the DHT, and
286 only contain a reference to it in their stored value for the hash of
287 the file. The reference would be a hash of the torrent string. If
288 the torrent string is short enough to store in the DHT (i.e. less
289 than 1472 bytes, or about 70 pieces for the SHA1 hash), then a
290 lookup of that hash in the DHT would give the torrent-like string.
291 Otherwise, a request to the peer for the hash (just like files are
292 downloaded), should return the torrent string.
293
294 % This can cause a problem with hash checking the returned data, as
295 % hashes for the pieces are not known. Any file that fails a hash
296 % check should be downloaded again, with each piece being downloaded
297 % from different peers than it was previously. The peers are shifted
298 % by 1, so that if a peers previously downloaded piece i, it now
299 % downloads piece i+1, and the first piece is downloaded by the
300 % previous downloader of the last piece, or preferably a previously
301 % unused peer. As each piece is downloaded the running hash of the
302 % file should be checked to determine the place at which the file
303 % differs from the previous download.
304
305 % If the hash check then passes, then the peer who originally provided
306 % the bad piece can be assessed blame for the error. Otherwise, the
307 % peer who originally provided the piece is probably at fault, since
308 % he is now providing a later piece. This doesn't work if the
309 % differing piece is the first piece, in which case it is downloaded
310 % from a 3rd peer, with consensus revealing the misbehaving peer.
311
312 %%%%%%%%%%%%%%%%%%%%%%%%%%%  Section  %%%%%%%%%%%%%%%%%%%%%%%%%%%%
313
314 \section{Sample Implementation}
315 \label{implementation}
316
317 A sample implementation has been created that functions as described
318 in section \ref{opportunity}. This software, called
319 \texttt{apt-dht}, interacts with the \texttt{apt} tool found in most
320 Debian-based Linux distributions. Our tool uses SHA1 hashes to
321 verify most downloaded files, including the large index files that
322 contain the hashes of the individual packages. We chose this
323 distribution system as it is familiar to us, and there are
324 interesting statistics available for analyzing the popularity of the
325 software packages \cite{popcon}.
326
327 Since all requests from apt are in the form of HTTP requests to a
328 server, the implementation takes the form of a caching HTTP proxy.
329 Pointing a standard apt implementation to use the proxy is as simple
330 as prepending the proxy location and port to the front of the mirror
331 name (i.e. ``localhost:9977/mirrorname.debian.org/\ldots'').
332
333 The DHT is based on Khashmir, which is an implementation of the
334 Kademlia DHT using methods familiar to BitTorrent developers. The
335 communication is all handled by UDP messages, and RPC requests are
336 bencoded similar to torrent files. Khashmir uses the high-level
337 Twisted event-driven networking engine, so Twisted is also used for
338 all other networking needs.
339
340 The torrent strings stored in the DHT are all bencoded dictionaries
341 containing similar information to what is in a torrent file. This
342 includes the piece size used, the length of the piece hash, and of
343 course the hashes of all the sub-pieces of the content.
344
345 Downloading is accomplished by sending simple HTTP requests to the
346 peer's identified by lookups in the DHT to have the desired file.
347 The HTTP server used for the proxy also doubles as the server
348 listeneing for requests for downloads from other peers. All peers
349 support HTTP/1.1, both in the server and the client, which allows
350 for pipelining of multiple requests to a peer, and the requesting of
351 smaller pieces of a large file using the Range request header.
352
353 \begin{figure}
354 \centering
355 \includegraphics[width=\columnwidth]{apt_dht_simulation-size_CDF.eps}
356 \caption{The CDF of the size of packages in a Debian system, both
357 for the actual size and adjusted size based on the popularity of
358 the package.}
359 \label{size_CDF}
360 \end{figure}
361
362 Figure \ref{size_CDF} shows the package size of the 22,298 packages
363 available in Debian in January 2008. We can see that most of the
364 packages are quite small, and so most will therefore not require
365 sub-piece information to download. We have chosen a maximum piece
366 size of 512 kB, which means that 17,515 (78\%) of the packages will
367 not require this information. There are 3054 packages that will
368 require 2 to 4 pieces, for which the torrent string can be stored
369 directly with the package hash in the DHT. There are 1667 packages
370 that will require a separate lookup in the DHT for the longer
371 torrent string as they require 5 to 70 pieces. Finally, there are
372 only 62 packages that require more than 70 pieces, and so will
373 require a separate request to the user for the torrent string.
374
375 \subsection{Current Status}
376 \label{status}
377
378 Though functional and useful, the current implementation is not
379 complete, and is missing some of the downloading details necessary
380 to make it complete. It operates as a caching HTTP proxy, serving
381 requests from apt and other peers. Index files are identified by the
382 current implementation, and parsed to find hashed of files for DHT
383 lookups if they are later requested. Files are downloaded from
384 peers, and those not available in peers are downloaded correctly
385 from the mirror. Any downloaded files are hash checked to verify
386 them, and then added to the DHT.
387
388 However, the current implementation does not use the sub-piece
389 hashing described in section \ref{downloading}, or any parallelizing
390 of downloads. Instead, it currently chooses a peer at random from
391 the list of possible peers, and downloads the entire file from that
392 peer, regardless of the size of the file. This needs to change if
393 both a) the file is large (more than 512 KB), and b) there are
394 multiple peers with the file. The storage and retrieval of sub-piece
395 hashes has not yet been finalized though, and until it is this final
396 step is not possible.
397
398 %%%%%%%%%%%%%%%%%%%%%%%%%%%  Section  %%%%%%%%%%%%%%%%%%%%%%%%%%%%
399
400 \section{Conclusions}
401 \label{conclusions}
402
403 We have designed a generally applicable peer-to-peer content
404 distribution model to be used by many of the free content
405 distributors operating today. It makes use of already existing
406 features of most package management systems, to create a
407 peer-to-peer distribution that should substantially reduce the costs
408 of hosting the content.
409
410 We have also implemented our design in software to be used in
411 conjuction with Debian-based distribution of Linux software
412 packages. It will soone be in use by many users of the Debian
413 project's distribution, and so serves as an example of the
414 possibilities that exist.
415
416 \bibliographystyle{IEEEtran}
417 \bibliography{./IEEEabrv,./all}
418
419 \end{document}