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