gen_settings_ids.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #!/usr/bin/env python3
  2. # Copyright 2017 gRPC authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from __future__ import print_function
  16. import collections
  17. import sys
  18. import perfection
  19. _MAX_HEADER_LIST_SIZE = 16 * 1024 * 1024
  20. Setting = collections.namedtuple('Setting', 'id default min max on_error')
  21. OnError = collections.namedtuple('OnError', 'behavior code')
  22. clamp_invalid_value = OnError('CLAMP_INVALID_VALUE', 'PROTOCOL_ERROR')
  23. disconnect_on_invalid_value = lambda e: OnError('DISCONNECT_ON_INVALID_VALUE', e
  24. )
  25. DecoratedSetting = collections.namedtuple('DecoratedSetting',
  26. 'enum name setting')
  27. _SETTINGS = {
  28. 'HEADER_TABLE_SIZE':
  29. Setting(1, 4096, 0, 0xffffffff, clamp_invalid_value),
  30. 'ENABLE_PUSH':
  31. Setting(2, 1, 0, 1, disconnect_on_invalid_value('PROTOCOL_ERROR')),
  32. 'MAX_CONCURRENT_STREAMS':
  33. Setting(3, 0xffffffff, 0, 0xffffffff,
  34. disconnect_on_invalid_value('PROTOCOL_ERROR')),
  35. 'INITIAL_WINDOW_SIZE':
  36. Setting(4, 65535, 0, 0x7fffffff,
  37. disconnect_on_invalid_value('FLOW_CONTROL_ERROR')),
  38. 'MAX_FRAME_SIZE':
  39. Setting(5, 16384, 16384, 16777215,
  40. disconnect_on_invalid_value('PROTOCOL_ERROR')),
  41. 'MAX_HEADER_LIST_SIZE':
  42. Setting(6, _MAX_HEADER_LIST_SIZE, 0, _MAX_HEADER_LIST_SIZE,
  43. clamp_invalid_value),
  44. 'GRPC_ALLOW_TRUE_BINARY_METADATA':
  45. Setting(0xfe03, 0, 0, 1, clamp_invalid_value),
  46. }
  47. H = open('src/core/ext/transport/chttp2/transport/http2_settings.h', 'w')
  48. C = open('src/core/ext/transport/chttp2/transport/http2_settings.c', 'w')
  49. # utility: print a big comment block into a set of files
  50. def put_banner(files, banner):
  51. for f in files:
  52. print('/*', file=f)
  53. for line in banner:
  54. print(' * %s' % line, file=f)
  55. print(' */', file=f)
  56. print(file=f)
  57. # copy-paste copyright notice from this file
  58. with open(sys.argv[0]) as my_source:
  59. copyright = []
  60. for line in my_source:
  61. if line[0] != '#':
  62. break
  63. for line in my_source:
  64. if line[0] == '#':
  65. copyright.append(line)
  66. break
  67. for line in my_source:
  68. if line[0] != '#':
  69. break
  70. copyright.append(line)
  71. put_banner([H, C], [line[2:].rstrip() for line in copyright])
  72. put_banner(
  73. [H, C],
  74. ["Automatically generated by tools/codegen/core/gen_settings_ids.py"])
  75. print("#ifndef GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HTTP2_SETTINGS_H",
  76. file=H)
  77. print("#define GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HTTP2_SETTINGS_H",
  78. file=H)
  79. print(file=H)
  80. print("#include <stdint.h>", file=H)
  81. print("#include <stdbool.h>", file=H)
  82. print(file=H)
  83. print("#include \"src/core/ext/transport/chttp2/transport/http2_settings.h\"",
  84. file=C)
  85. print(file=C)
  86. print("#include <grpc/support/useful.h>", file=C)
  87. print("#include \"src/core/lib/transport/http2_errors.h\"", file=C)
  88. print(file=C)
  89. p = perfection.hash_parameters(sorted(x.id for x in list(_SETTINGS.values())))
  90. print(p)
  91. def hash(i):
  92. i += p.offset
  93. x = i % p.t
  94. y = i // p.t
  95. return x + p.r[y]
  96. decorated_settings = [
  97. DecoratedSetting(hash(setting.id), name, setting)
  98. for name, setting in _SETTINGS.items()
  99. ]
  100. print('typedef enum {', file=H)
  101. for decorated_setting in sorted(decorated_settings):
  102. print(' GRPC_CHTTP2_SETTINGS_%s = %d, /* wire id %d */' %
  103. (decorated_setting.name, decorated_setting.enum,
  104. decorated_setting.setting.id),
  105. file=H)
  106. print('} grpc_chttp2_setting_id;', file=H)
  107. print(file=H)
  108. print('#define GRPC_CHTTP2_NUM_SETTINGS %d' %
  109. (max(x.enum for x in decorated_settings) + 1),
  110. file=H)
  111. print('extern const uint16_t grpc_setting_id_to_wire_id[];', file=H)
  112. print('const uint16_t grpc_setting_id_to_wire_id[] = {%s};' %
  113. ','.join('%d' % s for s in p.slots),
  114. file=C)
  115. print(file=H)
  116. print(
  117. "bool grpc_wire_id_to_setting_id(uint32_t wire_id, grpc_chttp2_setting_id *out);",
  118. file=H)
  119. cgargs = {
  120. 'r': ','.join('%d' % (r if r is not None else 0) for r in p.r),
  121. 't': p.t,
  122. 'offset': abs(p.offset),
  123. 'offset_sign': '+' if p.offset > 0 else '-'
  124. }
  125. print("""
  126. bool grpc_wire_id_to_setting_id(uint32_t wire_id, grpc_chttp2_setting_id *out) {
  127. uint32_t i = wire_id %(offset_sign)s %(offset)d;
  128. uint32_t x = i %% %(t)d;
  129. uint32_t y = i / %(t)d;
  130. uint32_t h = x;
  131. switch (y) {
  132. """ % cgargs,
  133. file=C)
  134. for i, r in enumerate(p.r):
  135. if not r:
  136. continue
  137. if r < 0:
  138. print('case %d: h -= %d; break;' % (i, -r), file=C)
  139. else:
  140. print('case %d: h += %d; break;' % (i, r), file=C)
  141. print("""
  142. }
  143. *out = (grpc_chttp2_setting_id)h;
  144. return h < GPR_ARRAY_SIZE(grpc_setting_id_to_wire_id) && grpc_setting_id_to_wire_id[h] == wire_id;
  145. }
  146. """ % cgargs,
  147. file=C)
  148. print("""
  149. typedef enum {
  150. GRPC_CHTTP2_CLAMP_INVALID_VALUE,
  151. GRPC_CHTTP2_DISCONNECT_ON_INVALID_VALUE
  152. } grpc_chttp2_invalid_value_behavior;
  153. typedef struct {
  154. const char *name;
  155. uint32_t default_value;
  156. uint32_t min_value;
  157. uint32_t max_value;
  158. grpc_chttp2_invalid_value_behavior invalid_value_behavior;
  159. uint32_t error_value;
  160. } grpc_chttp2_setting_parameters;
  161. extern const grpc_chttp2_setting_parameters grpc_chttp2_settings_parameters[GRPC_CHTTP2_NUM_SETTINGS];
  162. """,
  163. file=H)
  164. print(
  165. "const grpc_chttp2_setting_parameters grpc_chttp2_settings_parameters[GRPC_CHTTP2_NUM_SETTINGS] = {",
  166. file=C)
  167. i = 0
  168. for decorated_setting in sorted(decorated_settings):
  169. while i < decorated_setting.enum:
  170. print(
  171. "{NULL, 0, 0, 0, GRPC_CHTTP2_DISCONNECT_ON_INVALID_VALUE, GRPC_HTTP2_PROTOCOL_ERROR},",
  172. file=C)
  173. i += 1
  174. print("{\"%s\", %du, %du, %du, GRPC_CHTTP2_%s, GRPC_HTTP2_%s}," % (
  175. decorated_setting.name,
  176. decorated_setting.setting.default,
  177. decorated_setting.setting.min,
  178. decorated_setting.setting.max,
  179. decorated_setting.setting.on_error.behavior,
  180. decorated_setting.setting.on_error.code,
  181. ),
  182. file=C)
  183. i += 1
  184. print("};", file=C)
  185. print(file=H)
  186. print("#endif /* GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HTTP2_SETTINGS_H */",
  187. file=H)
  188. H.close()
  189. C.close()