📄 architecture.doc
字号:
Samba Architecture------------------First preliminary version Dan Shearer Nov 97Quickly scrabbled together from odd bits of mail and memory. Please update.This document gives a general overview of how Samba worksinternally. The Samba Team has tried to come up with a model which isthe best possible compromise between elegance, portability, securityand the constraints imposed by the very messy SMB and CIFSprotocol. It also tries to answer some of the frequently asked questions such as: * Is Samba secure when running on Unix? The xyz platform? What about the root priveliges issue? * Pros and cons of multithreading in various parts of Samba * Why not have a separate process for name resolution, WINS, and browsing?Multithreading and Samba------------------------People sometimes tout threads as a uniformly good thing. They are verynice in their place but are quite inappropriate for smbd. nmbd isanother matter, and multi-threading it would be very nice. The short version is that smbd is not multithreaded, and alternativeservers that take this approach under Unix (such as Syntax, at thetime of writing) suffer tremendous performance penalties and are lessrobust. nmbd is not threaded either, but this is because it is notpossible to do it while keeping code consistent and portable across 35or more platforms. (This drawback also applies to threading smbd.)The longer versions is that there are very good reasons for not makingsmbd multi-threaded. Multi-threading would actually make Samba muchslower, less scalable, less portable and much less robust. The factthat we use a separate process for each connection is one of Samba'sbiggest advantages.Threading smbd--------------A few problems that would arise from a threaded smbd are:0) It's not only to create threads instead of processes, but you must care about all variables if they have to be thread specific (currently they would be global).1) if one thread dies (eg. a seg fault) then all threads die. We canimmediately throw robustness out the window.2) many of the system calls we make are blocking. Non-blockingequivalents of many calls are either not available or are awkward (andslow) to use. So while we block in one thread all clients arewaiting. Imagine if one share is a slow NFS filesystem and the othersare fast, we will end up slowing all clients to the speed of NFS.3) you can't run as a different uid in different threads. This meanswe would have to switch uid/gid on _every_ SMB packet. It would behorrendously slow.4) the per process file descriptor limit would mean that we could onlysupport a limited number of clients.5) we couldn't use the system locking calls as the locking context offcntl() is a process, not a thread.Threading nmbd--------------This would be ideal, but gets sunk by portability requirements.Andrew tried to write a test threads library for nmbd that used onlyansi-C constructs (using setjmp and longjmp). Unfortunately some OSesdefeat this by restricting longjmp to calling addresses that areshallower than the current address on the stack (apparently AIX doesthis). This makes a truly portable threads library impossible. So tosupport all our current platforms we would have to code nmbd both withand without threads, and as the real aim of threads is to make thecode clearer we would not have gained anything. (it is a myth thatthreads make things faster. threading is like recursion, it can makethings clear but the same thing can always be done faster by someother method)Chris tried to spec out a general design that would abstract threadingvs separate processes (vs other methods?) and make them accessiblethrough some general API. This doesn't work because of the datasharing requirements of the protocol (packets in the future dependingon packets now, etc.) At least, the code would work but would be veryclumsy, and besides the fork() type model would never work on Unix. (Is there an OS that it would work on, for nmbd?)A fork() is cheap, but not nearly cheap enough to do on every UDPpacket that arrives. Having a pool of processes is possible but isnasty to program cleanly due to the enormous amount of shared data (incomplex structures) between the processes. We can't rely on eachplatform having a shared memory system.nbmd Design-----------Originally Andrew used recursion to simulate a multi-threadedenvironment, which use the stack enormously and made for reallyconfusing debugging sessions. Luke Leighton rewrote it to use aqueuing system that keeps state information on each packet. Thefirst version used a single structure which was used by all thepending states. As the initialisation of this structure wasdone by adding arguments, as the functionality developed, it gotpretty messy. So, it was replaced with a higher-order functionand a pointer to a user-defined memory block. This suddenlymade things much simpler: large numbers of functions could bemade static, and modularised. This is the same principle as usedin NT's kernel, and achieves the same effect as threads, but ina single process.Then Jeremy rewrote nmbd. The packet data in nmbd isn't what's on thewire. It's a nice format that is very amenable to processing but stillkeeps the idea of a distinct packet. See "struct packet_struct" innameserv.h. It has all the detail but none of the on-the-wiremess. This makes it ideal for using in disk or memory-based databasesfor browsing and WINS support. nmbd now consists of a series of modules. It...Samba Design and Security-------------------------Why Isn't nmbd Multiple Daemons?--------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -