netarch.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. Coturn architecture, part 1
  2. Network architecture
  3. I. INTRODUCTION
  4. This document assumes that the reader is familiar with the various TURN specifications.
  5. The goal of this document is to provide general information for the Coturn
  6. administrators and code developers about organization of the network interaction
  7. in Coturn.
  8. Coturn is a TURN relay server that has several general types of main network interaction:
  9. 1) Session establishment and maintenance negotiations with the client application.
  10. 2) Accepting packets to be relayed from the Client application, on the client-facing
  11. sockets, and relaying those packets, through the relay sockets, to the Peer application.
  12. 3) Accepting packets to be relayed from the Peer application, on the peer-facing
  13. relay sockets, and relaying those packets, through the Client sockets, to the Client
  14. application.
  15. There are other, secondary, interactions:
  16. 1) Communications with the database servers.
  17. 2) Communications with the telnet admin console.
  18. 3) Communications with the client admin browser, over HTTPS.
  19. This document concentrates on the main network communications. It will describe
  20. how those communicatiuons are organized in the Coturn code.
  21. The key to the understanding how Coturn works is the notions of "listeners" and
  22. "general relay servers".
  23. II. LISTENERS
  24. In Coturn, a "listener" is the entity that initiates dialog with the new client. When a
  25. new client sends its first packet to TURN, then it is initially accepted by the UDP
  26. listener (the code in dtls_listener.c) or by TCP listener (the code in tls_listener.c).
  27. The listeners are smart enough to recognize whether the new session is a TLS session or
  28. "plain" protocol session, and it handles necessary SSL keys and negotiations.
  29. The listener then creates a client endpoint (depending on the protocol and on the
  30. "network engine" - see below).
  31. What happens next depends on the "network engine" that the Coturn is using in runtime.
  32. If the relay server that will be handling that session is located in a different thread,
  33. then the listener will "send" the endpoint to that relay server (see the "connect_cb"
  34. callback function). If the relay server is located in the same thread as the listener,
  35. then the listener will call the session establishment function itself. See the function
  36. open_client_connection_session() and where and how it is called in various cases,
  37. for reference.
  38. The listeners (and the relay servers) configuration is initiated in the function
  39. setup_server() in netengine.c. First, setup_listener() creates the necessary generic
  40. data structures for the listeners. Second, network-engine-specific functions associate
  41. listeners with the execution threads and with the relay servers.
  42. There may be multiple listeners in the server, and they may be running in different
  43. threads.
  44. III. RELAY SERVERS
  45. The relay servers take control over the client sessions after the initial contact was
  46. established by the listeners. The relay server will be reading the session sockets
  47. (the client and the relay sockets) and perform the necessary actions on them, according
  48. to the TURN specs.
  49. There can be multiple relay servers in the system, running in different threads.
  50. The client sessions are distributed among them in fairly random manner, for load
  51. balancing.
  52. The relay server will be responsible for the session as long as the session exists.
  53. It will exclusively handle all session communications. Thus, the session will stay
  54. within the same thread for its lifetime. The performance benefit is that there will be
  55. no CPU context switching when the session packets are handled.
  56. There is one exception when a relay server will transfer a session to another relay
  57. server: the mobility functionality. When the client address changes, it may require
  58. that the session must be using a different thread - and a different relay server, as
  59. the result. The the original relay server will have to pack the session, say
  60. "farewell" to it and ship it to another relay server. The destination relay server
  61. will adopt the session and the session will stay with the new relay server - until the
  62. next client address change.
  63. IV. NETWORK ENGINES
  64. UDP communications are rather under-developed, comparing to the TCP communications,
  65. in modern operational systems. Because TURN stresses UDP communications, UDP
  66. performance is very important. Different OS's have different capabilities, so Coturn,
  67. being a portable server, had to employ different strategies for different systems.
  68. There are three "network engines" (or rather "network threading patterns") implemented
  69. in Coturn:
  70. 1) UDP listener thread per frontend IP (FreeBSD, Solaris) with multiple UDP/TCP
  71. relay servers. Listeners and relays are in different threads.
  72. //TODO
  73. 2) UDP listener and relay thread per frontend IP, with multiple TCP relay threads
  74. (early Linux). The listener and the relay servers are related, form pairs and are
  75. working in the same thread.
  76. //TODO
  77. 3) Multiple UDP and TCP listeners and relay per each frontend IP (advanced Linuxes).
  78. The listener and the relay servers are related, form pairs and are
  79. working in the same thread.
  80. //TODO