rpc-cache.txt 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. This document gives a brief introduction to the caching
  2. mechanisms in the sunrpc layer that is used, in particular,
  3. for NFS authentication.
  4. CACHES
  5. ======
  6. The caching replaces the old exports table and allows for
  7. a wide variety of values to be caches.
  8. There are a number of caches that are similar in structure though
  9. quite possibly very different in content and use. There is a corpus
  10. of common code for managing these caches.
  11. Examples of caches that are likely to be needed are:
  12. - mapping from IP address to client name
  13. - mapping from client name and filesystem to export options
  14. - mapping from UID to list of GIDs, to work around NFS's limitation
  15. of 16 gids.
  16. - mappings between local UID/GID and remote UID/GID for sites that
  17. do not have uniform uid assignment
  18. - mapping from network identify to public key for crypto authentication.
  19. The common code handles such things as:
  20. - general cache lookup with correct locking
  21. - supporting 'NEGATIVE' as well as positive entries
  22. - allowing an EXPIRED time on cache items, and removing
  23. items after they expire, and are no longe in-use.
  24. Future code extensions are expect to handle
  25. - making requests to user-space to fill in cache entries
  26. - allowing user-space to directly set entries in the cache
  27. - delaying RPC requests that depend on as-yet incomplete
  28. cache entries, and replaying those requests when the cache entry
  29. is complete.
  30. - maintaining last-access times on cache entries
  31. - clean out old entries when the caches become full
  32. The code for performing a cache lookup is also common, but in the form
  33. of a template. i.e. a #define.
  34. Each cache defines a lookup function by using the DefineCacheLookup
  35. macro, or the simpler DefineSimpleCacheLookup macro
  36. Creating a Cache
  37. ----------------
  38. 1/ A cache needs a datum to cache. This is in the form of a
  39. structure definition that must contain a
  40. struct cache_head
  41. as an element, usually the first.
  42. It will also contain a key and some content.
  43. Each cache element is reference counted and contains
  44. expiry and update times for use in cache management.
  45. 2/ A cache needs a "cache_detail" structure that
  46. describes the cache. This stores the hash table, and some
  47. parameters for cache management.
  48. 3/ A cache needs a lookup function. This is created using
  49. the DefineCacheLookup macro. This lookup function is used both
  50. to find entries and to update entries. The normal mode for
  51. updating an entry is to replace the old entry with a new
  52. entry. However it is possible to allow update-in-place
  53. for those caches where it makes sense (no atomicity issues
  54. or indirect reference counting issue)
  55. 4/ A cache needs to be registered using cache_register(). This
  56. includes in on a list of caches that will be regularly
  57. cleaned to discard old data. For this to work, some
  58. thread must periodically call cache_clean
  59. Using a cache
  60. -------------
  61. To find a value in a cache, call the lookup function passing it a the
  62. datum which contains key, and possibly content, and a flag saying
  63. whether to update the cache with new data from the datum. Depending
  64. on how the cache lookup function was defined, it may take an extra
  65. argument to identify the particular cache in question.
  66. Except in cases of kmalloc failure, the lookup function
  67. will return a new datum which will store the key and
  68. may contain valid content, or may not.
  69. This datum is typically passed to cache_check which determines the
  70. validity of the datum and may later initiate an upcall to fill
  71. in the data.
  72. cache_check can be passed a "struct cache_req *". This structure is
  73. typically embedded in the actual request and can be used to create a
  74. deferred copy of the request (struct cache_deferred_req). This is
  75. done when the found cache item is not uptodate, but the is reason to
  76. believe that userspace might provide information soon. When the cache
  77. item does become valid, the deferred copy of the request will be
  78. revisited (->revisit). It is expected that this method will
  79. reschedule the request for processing.
  80. Populating a cache
  81. ------------------
  82. Each cache has a name, and when the cache is registered, a directory
  83. with that name is created in /proc/net/rpc
  84. This directory contains a file called 'channel' which is a channel
  85. for communicating between kernel and user for populating the cache.
  86. This directory may later contain other files of interacting
  87. with the cache.
  88. The 'channel' works a bit like a datagram socket. Each 'write' is
  89. passed as a whole to the cache for parsing and interpretation.
  90. Each cache can treat the write requests differently, but it is
  91. expected that a message written will contain:
  92. - a key
  93. - an expiry time
  94. - a content.
  95. with the intention that an item in the cache with the give key
  96. should be create or updated to have the given content, and the
  97. expiry time should be set on that item.
  98. Reading from a channel is a bit more interesting. When a cache
  99. lookup fail, or when it suceeds but finds an entry that may soon
  100. expiry, a request is lodged for that cache item to be updated by
  101. user-space. These requests appear in the channel file.
  102. Successive reads will return successive requests.
  103. If there are no more requests to return, read will return EOF, but a
  104. select or poll for read will block waiting for another request to be
  105. added.
  106. Thus a user-space helper is likely to:
  107. open the channel.
  108. select for readable
  109. read a request
  110. write a response
  111. loop.
  112. If it dies and needs to be restarted, any requests that have not be
  113. answered will still appear in the file and will be read by the new
  114. instance of the helper.
  115. Each cache should define a "cache_parse" method which takes a message
  116. written from user-space and processes it. It should return an error
  117. (which propagates back to the write syscall) or 0.
  118. Each cache should also define a "cache_request" method which
  119. takes a cache item and encodes a request into the buffer
  120. provided.
  121. Note: If a cache has no active readers on the channel, and has had not
  122. active readers for more than 60 seconds, further requests will not be
  123. added to the channel but instead all looks that do not find a valid
  124. entry will fail. This is partly for backward compatibility: The
  125. previous nfs exports table was deemed to be authoritative and a
  126. failed lookup meant a definite 'no'.
  127. request/response format
  128. -----------------------
  129. While each cache is free to use it's own format for requests
  130. and responses over channel, the following is recommended are
  131. appropriate and support routines are available to help:
  132. Each request or response record should be printable ASCII
  133. with precisely one newline character which should be at the end.
  134. Fields within the record should be separated by spaces, normally one.
  135. If spaces, newlines, or nul characters are needed in a field they
  136. much be quotes. two mechanisms are available:
  137. 1/ If a field begins '\x' then it must contain an even number of
  138. hex digits, and pairs of these digits provide the bytes in the
  139. field.
  140. 2/ otherwise a \ in the field must be followed by 3 octal digits
  141. which give the code for a byte. Other characters are treated
  142. as them selves. At the very least, space, newlines nul, and
  143. '\' must be quoted in this way.