Add all the source files from the old CVS tree,
[Samba.git] / docs / devel / architecture.xml
blob312a63af97ec82d25a66f7339770f0337d6c11e6
1 <chapter id="architecture">
2 <chapterinfo>
3         <author>
4                 <firstname>Dan</firstname><surname>Shearer</surname>
5         </author>
6         <pubdate> November 1997</pubdate>
7 </chapterinfo>
9 <title>Samba Architecture</title>
11 <sect1>
12 <title>Introduction</title>
14 <para>
15 This document gives a general overview of how Samba works
16 internally. The Samba Team has tried to come up with a model which is
17 the best possible compromise between elegance, portability, security
18 and the constraints imposed by the very messy SMB and CIFS
19 protocol. 
20 </para>
22 <para>
23 It also tries to answer some of the frequently asked questions such as:
24 </para>
26 <orderedlist>
27 <listitem><para>
28         Is Samba secure when running on Unix? The xyz platform?
29         What about the root priveliges issue?
30 </para></listitem>
32 <listitem><para>Pros and cons of multithreading in various parts of Samba</para></listitem>
34 <listitem><para>Why not have a separate process for name resolution, WINS, and browsing?</para></listitem>
36 </orderedlist>
38 </sect1>
40 <sect1>
41 <title>Multithreading and Samba</title>
43 <para>
44 People sometimes tout threads as a uniformly good thing. They are very
45 nice in their place but are quite inappropriate for smbd. nmbd is
46 another matter, and multi-threading it would be very nice. 
47 </para>
49 <para>
50 The short version is that smbd is not multithreaded, and alternative
51 servers that take this approach under Unix (such as Syntax, at the
52 time of writing) suffer tremendous performance penalties and are less
53 robust. nmbd is not threaded either, but this is because it is not
54 possible to do it while keeping code consistent and portable across 35
55 or more platforms. (This drawback also applies to threading smbd.)
56 </para>
58 <para>
59 The longer versions is that there are very good reasons for not making
60 smbd multi-threaded.  Multi-threading would actually make Samba much
61 slower, less scalable, less portable and much less robust. The fact
62 that we use a separate process for each connection is one of Samba's
63 biggest advantages.
64 </para>
66 </sect1>
68 <sect1>
69 <title>Threading smbd</title>
71 <para>
72 A few problems that would arise from a threaded smbd are:
73 </para>
75 <orderedlist>
76 <listitem><para>
77         It's not only to create threads instead of processes, but you
78         must care about all variables if they have to be thread specific
79         (currently they would be global).
80 </para></listitem>
82 <listitem><para>
83         if one thread dies (eg. a seg fault) then all threads die. We can
84         immediately throw robustness out the window.
85 </para></listitem>
87 <listitem><para>
88         many of the system calls we make are blocking. Non-blocking
89         equivalents of many calls are either not available or are awkward (and
90         slow) to use. So while we block in one thread all clients are
91         waiting. Imagine if one share is a slow NFS filesystem and the others 
92         are fast, we will end up slowing all clients to the speed of NFS.
93 </para></listitem>
95 <listitem><para>
96         you can't run as a different uid in different threads. This means
97         we would have to switch uid/gid on _every_ SMB packet. It would be
98         horrendously slow.
99 </para></listitem>
101 <listitem><para>
102         the per process file descriptor limit would mean that we could only
103         support a limited number of clients.
104 </para></listitem>
106 <listitem><para>
107         we couldn't use the system locking calls as the locking context of
108         fcntl() is a process, not a thread.
109 </para></listitem>
111 </orderedlist>
113 </sect1>
115 <sect1>
116 <title>Threading nmbd</title>
118 <para>
119 This would be ideal, but gets sunk by portability requirements.
120 </para>
122 <para>
123 Andrew tried to write a test threads library for nmbd that used only
124 ansi-C constructs (using setjmp and longjmp). Unfortunately some OSes
125 defeat this by restricting longjmp to calling addresses that are
126 shallower than the current address on the stack (apparently AIX does
127 this). This makes a truly portable threads library impossible. So to
128 support all our current platforms we would have to code nmbd both with
129 and without threads, and as the real aim of threads is to make the
130 code clearer we would not have gained anything. (it is a myth that
131 threads make things faster. threading is like recursion, it can make
132 things clear but the same thing can always be done faster by some
133 other method)
134 </para>
136 <para>
137 Chris tried to spec out a general design that would abstract threading
138 vs separate processes (vs other methods?) and make them accessible
139 through some general API. This doesn't work because of the data
140 sharing requirements of the protocol (packets in the future depending
141 on packets now, etc.) At least, the code would work but would be very
142 clumsy, and besides the fork() type model would never work on Unix. (Is there an OS that it would work on, for nmbd?)
143 </para>
145 <para>
146 A fork() is cheap, but not nearly cheap enough to do on every UDP
147 packet that arrives. Having a pool of processes is possible but is
148 nasty to program cleanly due to the enormous amount of shared data (in
149 complex structures) between the processes. We can't rely on each
150 platform having a shared memory system.
151 </para>
153 </sect1>
155 <sect1>
156 <title>nbmd Design</title>
158 <para>
159 Originally Andrew used recursion to simulate a multi-threaded
160 environment, which use the stack enormously and made for really
161 confusing debugging sessions. Luke Leighton rewrote it to use a
162 queuing system that keeps state information on each packet.  The
163 first version used a single structure which was used by all the
164 pending states.  As the initialisation of this structure was
165 done by adding arguments, as the functionality developed, it got
166 pretty messy.  So, it was replaced with a higher-order function
167 and a pointer to a user-defined memory block.  This suddenly
168 made things much simpler: large numbers of functions could be
169 made static, and modularised.  This is the same principle as used
170 in NT's kernel, and achieves the same effect as threads, but in
171 a single process.
172 </para>
174 <para>
175 Then Jeremy rewrote nmbd. The packet data in nmbd isn't what's on the
176 wire. It's a nice format that is very amenable to processing but still
177 keeps the idea of a distinct packet. See "struct packet_struct" in
178 nameserv.h.  It has all the detail but none of the on-the-wire
179 mess. This makes it ideal for using in disk or memory-based databases
180 for browsing and WINS support. 
181 </para>
183 </sect1>
184 </chapter>