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 27 ) 28DecoratedSetting = collections.namedtuple('DecoratedSetting', 29 'enum name setting') 30 31_SETTINGS = { 32 'HEADER_TABLE_SIZE': 33 Setting(1, 4096, 0, 0xffffffff, clamp_invalid_value), 34 'ENABLE_PUSH': 35 Setting(2, 1, 0, 1, disconnect_on_invalid_value('PROTOCOL_ERROR')), 36 'MAX_CONCURRENT_STREAMS': 37 Setting(3, 0xffffffff, 0, 0xffffffff, 38 disconnect_on_invalid_value('PROTOCOL_ERROR')), 39 'INITIAL_WINDOW_SIZE': 40 Setting(4, 65535, 0, 0x7fffffff, 41 disconnect_on_invalid_value('FLOW_CONTROL_ERROR')), 42 'MAX_FRAME_SIZE': 43 Setting(5, 16384, 16384, 16777215, 44 disconnect_on_invalid_value('PROTOCOL_ERROR')), 45 'MAX_HEADER_LIST_SIZE': 46 Setting(6, _MAX_HEADER_LIST_SIZE, 0, _MAX_HEADER_LIST_SIZE, 47 clamp_invalid_value), 48 'GRPC_ALLOW_TRUE_BINARY_METADATA': 49 Setting(0xfe03, 0, 0, 1, clamp_invalid_value), 50} 51 52H = open('src/core/ext/transport/chttp2/transport/http2_settings.h', 'w') 53C = open('src/core/ext/transport/chttp2/transport/http2_settings.c', 'w') 54 55 56# utility: print a big comment block into a set of files 57def put_banner(files, banner): 58 for f in files: 59 print >> f, '/*' 60 for line in banner: 61 print >> f, ' * %s' % line 62 print >> f, ' */' 63 print >> f 64 65 66# copy-paste copyright notice from this file 67with open(sys.argv[0]) as my_source: 68 copyright = [] 69 for line in my_source: 70 if line[0] != '#': break 71 for line in my_source: 72 if line[0] == '#': 73 copyright.append(line) 74 break 75 for line in my_source: 76 if line[0] != '#': 77 break 78 copyright.append(line) 79 put_banner([H, C], [line[2:].rstrip() for line in copyright]) 80 81put_banner( 82 [H, C], 83 ["Automatically generated by tools/codegen/core/gen_settings_ids.py"]) 84 85print >> H, "#ifndef GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HTTP2_SETTINGS_H" 86print >> H, "#define GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HTTP2_SETTINGS_H" 87print >> H 88print >> H, "#include <stdint.h>" 89print >> H, "#include <stdbool.h>" 90print >> H 91 92print >> C, "#include \"src/core/ext/transport/chttp2/transport/http2_settings.h\"" 93print >> C 94print >> C, "#include <grpc/support/useful.h>" 95print >> C, "#include \"src/core/lib/transport/http2_errors.h\"" 96print >> C 97 98p = perfection.hash_parameters(sorted(x.id for x in _SETTINGS.values())) 99print p 100 101 102def hash(i): 103 i += p.offset 104 x = i % p.t 105 y = i / p.t 106 return x + p.r[y] 107 108 109decorated_settings = [ 110 DecoratedSetting(hash(setting.id), name, setting) 111 for name, setting in _SETTINGS.iteritems() 112] 113 114print >> H, 'typedef enum {' 115for decorated_setting in sorted(decorated_settings): 116 print >> H, ' GRPC_CHTTP2_SETTINGS_%s = %d, /* wire id %d */' % ( 117 decorated_setting.name, decorated_setting.enum, 118 decorated_setting.setting.id) 119print >> H, '} grpc_chttp2_setting_id;' 120print >> H 121print >> H, '#define GRPC_CHTTP2_NUM_SETTINGS %d' % ( 122 max(x.enum for x in decorated_settings) + 1) 123 124print >> H, 'extern const uint16_t grpc_setting_id_to_wire_id[];' 125print >> C, 'const uint16_t grpc_setting_id_to_wire_id[] = {%s};' % ','.join( 126 '%d' % s for s in p.slots) 127print >> H 128print >> H, "bool grpc_wire_id_to_setting_id(uint32_t wire_id, grpc_chttp2_setting_id *out);" 129cgargs = { 130 'r': ','.join('%d' % (r if r is not None else 0) for r in p.r), 131 't': p.t, 132 'offset': abs(p.offset), 133 'offset_sign': '+' if p.offset > 0 else '-' 134} 135print >> C, """ 136bool grpc_wire_id_to_setting_id(uint32_t wire_id, grpc_chttp2_setting_id *out) { 137 uint32_t i = wire_id %(offset_sign)s %(offset)d; 138 uint32_t x = i %% %(t)d; 139 uint32_t y = i / %(t)d; 140 uint32_t h = x; 141 switch (y) { 142""" % cgargs 143for i, r in enumerate(p.r): 144 if not r: continue 145 if r < 0: print >> C, 'case %d: h -= %d; break;' % (i, -r) 146 else: print >> C, 'case %d: h += %d; break;' % (i, r) 147print >> C, """ 148 } 149 *out = (grpc_chttp2_setting_id)h; 150 return h < GPR_ARRAY_SIZE(grpc_setting_id_to_wire_id) && grpc_setting_id_to_wire_id[h] == wire_id; 151} 152""" % cgargs 153 154print >> H, """ 155typedef enum { 156 GRPC_CHTTP2_CLAMP_INVALID_VALUE, 157 GRPC_CHTTP2_DISCONNECT_ON_INVALID_VALUE 158} grpc_chttp2_invalid_value_behavior; 159 160typedef struct { 161 const char *name; 162 uint32_t default_value; 163 uint32_t min_value; 164 uint32_t max_value; 165 grpc_chttp2_invalid_value_behavior invalid_value_behavior; 166 uint32_t error_value; 167} grpc_chttp2_setting_parameters; 168 169extern const grpc_chttp2_setting_parameters grpc_chttp2_settings_parameters[GRPC_CHTTP2_NUM_SETTINGS]; 170""" 171print >> C, "const grpc_chttp2_setting_parameters grpc_chttp2_settings_parameters[GRPC_CHTTP2_NUM_SETTINGS] = {" 172i = 0 173for decorated_setting in sorted(decorated_settings): 174 while i < decorated_setting.enum: 175 print >> C, "{NULL, 0, 0, 0, GRPC_CHTTP2_DISCONNECT_ON_INVALID_VALUE, GRPC_HTTP2_PROTOCOL_ERROR}," 176 i += 1 177 print >> C, "{\"%s\", %du, %du, %du, GRPC_CHTTP2_%s, GRPC_HTTP2_%s}," % ( 178 decorated_setting.name, 179 decorated_setting.setting.default, 180 decorated_setting.setting.min, 181 decorated_setting.setting.max, 182 decorated_setting.setting.on_error.behavior, 183 decorated_setting.setting.on_error.code, 184 ) 185 i += 1 186print >> C, "};" 187 188print >> H 189print >> H, "#endif /* GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HTTP2_SETTINGS_H */" 190 191H.close() 192C.close() 193