set credits on new connections
[cor_2_6_31.git] / net / cor / common.c
blobdf56e054a60d182a29e3bf063a270f9b60ed306c
1 /*
2 * Connection oriented routing
3 * Copyright (C) 2007-2010 Michael Blizek
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA.
21 #include <linux/mutex.h>
23 #include "cor.h"
25 DEFINE_MUTEX(cor_bindnodes);
26 DEFINE_SPINLOCK(conn_free);
28 DEFINE_MUTEX(connid_gen);
30 LIST_HEAD(openports);
33 struct cell_hdr{
34 spinlock_t lock;
39 struct kmem_cache *conn_slab;
41 struct htable connid_table;
42 struct htable reverse_connid_table;
44 atomic_t num_conns;
46 struct kmem_cache *bindnode_slab;
47 struct kmem_cache *connlistener_slab;
49 /* see cor.h/KP_ACK_CONN */
50 static const __u32 log_64_11_table[] = {0,
51 64, 68, 73, 77, 82, 88, 93, 99, 106, 113, 120,
52 128, 136, 145, 155, 165, 175, 187, 199, 212, 226, 240,
53 256, 273, 290, 309, 329, 351, 374, 398, 424, 451, 481,
54 512, 545, 581, 619, 659, 702, 747, 796, 848, 903, 961,
55 1024, 1091, 1162, 1237, 1318, 1403, 1495, 1592, 1695, 1805,
56 1923,
57 2048, 2181, 2323, 2474, 2635, 2806, 2989, 3183, 3390, 3611,
58 3846,
59 4096, 4362, 4646, 4948, 5270, 5613, 5978, 6367, 6781, 7222,
60 7692,
61 8192, 8725, 9292, 9897, 10540, 11226, 11956, 12734, 13562,
62 14444, 15383,
63 16384, 17450, 18585, 19793, 21081, 22452, 23912, 25467, 27124,
64 28888, 30767,
65 32768, 34899, 37169, 39587, 42161, 44904, 47824, 50935, 54248,
66 57776, 61534,
67 65536, 69799, 74338, 79173, 84323, 89807, 95648, 101870, 108495,
68 115552, 123068,
69 131072, 139597, 148677, 158347, 168646, 179615, 191297, 203739,
70 216991, 231104, 246135,
71 262144, 279194, 297353, 316693, 337291, 359229, 382594, 407478,
72 433981, 462208, 492270,
73 524288, 558388, 594706, 633387, 674583, 718459, 765188, 814957,
74 867962, 924415, 984540,
75 1048576, 1116777, 1189413, 1266774, 1349166, 1436917, 1530376,
76 1629913, 1735924, 1848831, 1969081,
77 2097152, 2233553, 2378826, 2533547, 2698332, 2873834, 3060752,
78 3259826, 3471849, 3697662, 3938162,
79 4194304, 4467106, 4757652, 5067094, 5396664, 5747669, 6121503,
80 6519652, 6943698, 7395323, 7876323,
81 8388608, 8934212, 9515303, 10134189, 10793327, 11495337,
82 12243006, 13039305, 13887396, 14790647,
83 15752647,
84 16777216, 17868424, 19030606, 20268378, 21586655, 22990674,
85 24486013, 26078610, 27774791, 29581294,
86 31505293,
87 33554432, 35736849, 38061212, 40536755, 43173310, 45981349,
88 48972026, 52157220, 55549582, 59162588,
89 63010587,
90 67108864, 71473698, 76122425, 81073510, 86346620, 91962698,
91 97944052, 104314440, 111099165, 118325175,
92 126021174,
93 134217728, 142947395, 152244850, 162147020, 172693239,
94 183925396, 195888104, 208628880, 222198329,
95 236650351, 252042347,
96 268435456, 285894791, 304489699, 324294041, 345386479,
97 367850791, 391776208, 417257759, 444396658,
98 473300701, 504084694,
99 536870912, 571789581};
101 static const __u64 log_300_24_table[] = {0LL,
102 300LL, 308LL, 317LL, 327LL, 336LL, 346LL, 356LL, 367LL, 377LL,
103 389LL, 400LL, 412LL, 424LL, 436LL, 449LL, 462LL, 476LL,
104 490LL, 504LL, 519LL, 534LL, 550LL, 566LL, 582LL,
105 600LL, 617LL, 635LL, 654LL, 673LL, 693LL, 713LL, 734LL, 755LL,
106 778LL, 800LL, 824LL, 848LL, 873LL, 898LL, 925LL, 952LL,
107 980LL, 1009LL, 1038LL, 1069LL, 1100LL, 1132LL, 1165LL,
108 1200LL, 1235LL, 1271LL, 1308LL, 1346LL, 1386LL, 1427LL, 1468LL,
109 1511LL, 1556LL, 1601LL, 1648LL, 1697LL, 1746LL, 1797LL,
110 1850LL, 1904LL, 1960LL, 2018LL, 2077LL, 2138LL, 2200LL,
111 2265LL, 2331LL,
112 2400LL, 2470LL, 2542LL, 2617LL, 2693LL, 2772LL, 2854LL, 2937LL,
113 3023LL, 3112LL, 3203LL, 3297LL, 3394LL, 3493LL, 3595LL,
114 3701LL, 3809LL, 3921LL, 4036LL, 4154LL, 4276LL, 4401LL,
115 4530LL, 4663LL,
116 4800LL, 4940LL, 5085LL, 5234LL, 5387LL, 5545LL, 5708LL, 5875LL,
117 6047LL, 6224LL, 6407LL, 6594LL, 6788LL, 6987LL, 7191LL,
118 7402LL, 7619LL, 7842LL, 8072LL, 8309LL, 8552LL, 8803LL,
119 9061LL, 9326LL,
120 9600LL, 9881LL, 10170LL, 10468LL, 10775LL, 11091LL, 11416LL,
121 11750LL, 12095LL, 12449LL, 12814LL, 13189LL, 13576LL,
122 13974LL, 14383LL, 14805LL, 15239LL, 15685LL, 16145LL,
123 16618LL, 17105LL, 17606LL, 18122LL, 18653LL,
124 19200LL, 19762LL, 20341LL, 20937LL, 21551LL, 22182LL, 22832LL,
125 23501LL, 24190LL, 24899LL, 25628LL, 26379LL, 27152LL,
126 27948LL, 28767LL, 29610LL, 30478LL, 31371LL, 32290LL,
127 33236LL, 34210LL, 35212LL, 36244LL, 37306LL,
128 38400LL, 39525LL, 40683LL, 41875LL, 43102LL, 44365LL, 45665LL,
129 47003LL, 48380LL, 49798LL, 51257LL, 52759LL, 54305LL,
130 55897LL, 57534LL, 59220LL, 60956LL, 62742LL, 64580LL,
131 66473LL, 68421LL, 70425LL, 72489LL, 74613LL,
132 76800LL, 79050LL, 81366LL, 83750LL, 86205LL, 88731LL, 91331LL,
133 94007LL, 96761LL, 99597LL, 102515LL, 105519LL, 108611LL,
134 111794LL, 115069LL, 118441LL, 121912LL, 125484LL,
135 129161LL, 132946LL, 136842LL, 140851LL, 144979LL,
136 149227LL,
137 153600LL, 158100LL, 162733LL, 167501LL, 172410LL, 177462LL,
138 182662LL, 188014LL, 193523LL, 199194LL, 205031LL,
139 211039LL, 217223LL, 223588LL, 230139LL, 236883LL,
140 243824LL, 250969LL, 258323LL, 265892LL, 273684LL,
141 281703LL, 289958LL, 298454LL,
142 307200LL, 316201LL, 325467LL, 335003LL, 344820LL, 354924LL,
143 365324LL, 376029LL, 387047LL, 398389LL, 410062LL,
144 422078LL, 434446LL, 447176LL, 460279LL, 473767LL,
145 487649LL, 501938LL, 516646LL, 531785LL, 547368LL,
146 563407LL, 579916LL, 596909LL,
147 614400LL, 632403LL, 650934LL, 670007LL, 689640LL, 709848LL,
148 730648LL, 752058LL, 774095LL, 796778LL, 820125LL,
149 844157LL, 868892LL, 894353LL, 920559LL, 947534LL,
150 975299LL, 1003877LL, 1033293LL, 1063571LL, 1094736LL,
151 1126814LL, 1159832LL, 1193818LL,
152 1228800LL, 1264806LL, 1301868LL, 1340015LL, 1379281LL,
153 1419697LL, 1461297LL, 1504116LL, 1548190LL, 1593556LL,
154 1640251LL, 1688314LL, 1737785LL, 1788706LL, 1841119LL,
155 1895068LL, 1950598LL, 2007755LL, 2066587LL, 2127142LL,
156 2189472LL, 2253629LL, 2319665LL, 2387636LL,
157 2457600LL, 2529613LL, 2603736LL, 2680031LL, 2758562LL,
158 2839394LL, 2922595LL, 3008233LL, 3096381LL, 3187112LL,
159 3280502LL, 3376628LL, 3475571LL, 3577413LL, 3682239LL,
160 3790137LL, 3901196LL, 4015510LL, 4133174LL, 4254285LL,
161 4378945LL, 4507258LL, 4639331LL, 4775273LL,
162 4915200LL, 5059226LL, 5207473LL, 5360063LL, 5517125LL,
163 5678789LL, 5845190LL, 6016467LL, 6192763LL, 6374225LL,
164 6561004LL, 6753256LL, 6951142LL, 7154826LL, 7364478LL,
165 7580274LL, 7802393LL, 8031021LL, 8266348LL, 8508570LL,
166 8757890LL, 9014516LL, 9278662LL, 9550547LL,
167 9830400LL, 10118452LL, 10414946LL, 10720127LL, 11034250LL,
168 11357579LL, 11690381LL, 12032935LL, 12385527LL,
169 12748451LL, 13122009LL, 13506513LL, 13902285LL,
170 14309653LL, 14728957LL, 15160549LL, 15604787LL,
171 16062042LL, 16532696LL, 17017141LL, 17515781LL,
172 18029033LL, 18557324LL, 19101095LL,
173 19660800LL, 20236905LL, 20829892LL, 21440254LL, 22068501LL,
174 22715158LL, 23380763LL, 24065871LL, 24771055LL,
175 25496903LL, 26244019LL, 27013027LL, 27804570LL,
176 28619306LL, 29457915LL, 30321098LL, 31209574LL,
177 32124084LL, 33065392LL, 34034282LL, 35031563LL,
178 36058066LL, 37114648LL, 38202190LL,
179 39321600LL, 40473810LL, 41659784LL, 42880508LL, 44137003LL,
180 45430316LL, 46761526LL, 48131743LL, 49542111LL,
181 50993806LL, 52488038LL, 54026055LL, 55609140LL,
182 57238612LL, 58915831LL, 60642197LL, 62419149LL,
183 64248169LL, 66130784LL, 68068564LL, 70063126LL,
184 72116132LL, 74229296LL, 76404380LL,
185 78643200LL, 80947621LL, 83319568LL, 85761017LL, 88274007LL,
186 90860633LL, 93523052LL, 96263487LL, 99084223LL,
187 101987612LL, 104976077LL, 108052111LL, 111218280LL,
188 114477224LL, 117831663LL, 121284394LL, 124838298LL,
189 128496339LL, 132261569LL, 136137129LL, 140126252LL,
190 144232264LL, 148458592LL, 152808761LL,
191 157286400LL, 161895243LL, 166639136LL, 171522035LL, 176548014LL,
192 181721266LL, 187046105LL, 192526975LL, 198168446LL,
193 203975224LL, 209952155LL, 216104222LL, 222436560LL,
194 228954448LL, 235663326LL, 242568788LL, 249676596LL,
195 256992679LL, 264523139LL, 272274259LL, 280252504LL,
196 288464529LL, 296917185LL, 305617523LL,
197 314572800LL, 323790486LL, 333278272LL, 343044070LL, 353096029LL,
198 363442532LL, 374092211LL, 385053950LL, 396336892LL,
199 407950449LL, 419904310LL, 432208445LL, 444873120LL,
200 457908897LL, 471326652LL, 485137577LL, 499353193LL,
201 513985359LL, 529046279LL, 544548518LL, 560505008LL,
202 576929058LL, 593834370LL, 611235046LL,
203 629145600LL, 647580973LL, 666556544LL, 686088141LL,
204 706192058LL, 726885065LL, 748184423LL, 770107900LL,
205 792673784LL, 815900899LL, 839808620LL, 864416891LL,
206 889746240LL, 915817795LL, 942653304LL, 970275155LL,
207 998706387LL, 1027970718LL, 1058092559LL, 1089097037LL,
208 1121010017LL, 1153858117LL, 1187668741LL, 1222470092LL,
209 1258291200LL, 1295161946LL, 1333113088LL, 1372176283LL,
210 1412384117LL, 1453770131LL, 1496368847LL, 1540215801LL,
211 1585347569LL, 1631801799LL, 1679617241LL, 1728833783LL,
212 1779492480LL, 1831635590LL, 1885306609LL, 1940550310LL,
213 1997412774LL, 2055941436LL, 2116185118LL, 2178194075LL,
214 2242020034LL, 2307716235LL, 2375337483LL, 2444940184LL,
215 2516582400LL, 2590323893LL, 2666226176LL, 2744352567LL,
216 2824768235LL, 2907540262LL, 2992737695LL, 3080431603LL,
217 3170695139LL, 3263603598LL, 3359234483LL, 3457667567LL,
218 3558984960LL, 3663271180LL, 3770613219LL, 3881100620LL,
219 3994825549LL, 4111882872LL, 4232370237LL, 4356388151LL,
220 4484040068LL, 4615432471LL, 4750674966LL, 4889880368LL,
221 5033164800LL, 5180647786LL, 5332452353LL, 5488705134LL,
222 5649536470LL, 5815080525LL, 5985475391LL, 6160863207LL,
223 6341390279LL, 6527207197LL, 6718468967LL, 6915335135LL,
224 7117969921LL, 7326542360LL, 7541226438LL, 7762201240LL,
225 7989651098LL, 8223765745LL, 8464740475LL, 8712776303LL,
226 8968080137LL, 9230864943LL, 9501349932LL, 9779760736LL,
227 10066329600LL, 10361295572LL, 10664904706LL, 10977410268LL,
228 11299072941LL, 11630161050LL, 11970950782LL,
229 12321726414LL, 12682780558LL, 13054414395LL,
230 13436937935LL, 13830670270LL, 14235939843LL,
231 14653084721LL, 15082452877LL, 15524402481LL,
232 15979302196LL, 16447531490LL, 16929480950LL,
233 17425552607LL, 17936160274LL, 18461729886LL,
234 19002699864LL, 19559521473LL,
235 20132659200LL, 20722591144LL, 21329809413LL, 21954820536LL,
236 22598145883LL, 23260322101LL, 23941901564LL,
237 24643452829LL, 25365561116LL, 26108828790LL,
238 26873875870LL, 27661340540LL, 28471879687LL,
239 29306169443LL, 30164905755LL, 31048804962LL,
240 31958604392LL, 32895062981LL, 33858961901LL,
241 34851105215LL, 35872320548LL, 36923459773LL,
242 38005399729LL, 39119042946LL,
243 40265318400LL, 41445182288LL,42659618827LL,43909641073LL,
244 45196291767LL, 46520644203LL, 47883803129LL,
245 49286905659LL, 50731122232LL, 52217657581LL,
246 53747751741LL, 55322681081LL, 56943759374LL,
247 58612338887LL, 60329811511LL, 62097609924LL,
248 63917208785LL, 65790125963LL, 67717923803LL,
249 69702210431LL, 71744641096LL, 73846919547LL,
250 76010799459LL, 78238085892LL,
251 80530636800LL, 82890364576LL, 85319237654LL, 87819282146LL,
252 90392583534LL, 93041288407LL, 95767606258LL,
253 98573811319LL, 101462244465LL, 104435315163LL,
254 107495503482LL, 110645362163LL, 113887518749LL,
255 117224677774LL, 120659623022LL, 124195219849LL,
256 127834417571LL, 131580251926LL, 135435847606LL,
257 139404420863LL, 143489282192LL, 147693839094LL,
258 152021598918LL, 156476171785LL,
259 161061273600LL, 165780729153LL, 170638475309LL, 175638564293LL,
260 180785167068LL, 186082576815LL, 191535212516LL,
261 197147622639LL, 202924488931LL, 208870630326LL,
262 214991006964LL, 221290724326LL, 227775037498LL,
263 234449355548LL, 241319246045LL, 248390439699LL,
264 255668835143LL, 263160503853LL, 270871695212LL,
265 278808841726LL, 286978564384LL, 295387678189LL,
266 304043197837LL, 312952343570LL,
267 322122547200LL, 331561458306LL, 341276950619LL, 351277128587LL,
268 361570334136LL, 372165153630LL, 383070425033LL,
269 394295245278LL, 405848977863LL, 417741260653LL,
270 429982013929LL, 442581448653LL, 455550074996LL,
271 468898711096LL, 482638492091LL, 496780879399LL,
272 511337670287LL, 526321007707LL, 541743390425LL,
273 557617683452LL, 573957128769LL, 590775356379LL,
274 608086395675LL, 625904687141LL,
275 644245094400LL, 663122916612LL, 682553901238LL, 702554257174LL,
276 723140668273LL, 744330307261LL, 766140850066LL,
277 788590490556LL, 811697955726LL, 835482521307LL,
278 859964027858LL, 885162897307LL, 911100149992LL,
279 937797422193LL, 965276984182LL, 993561758799LL,
280 1022675340575LL, 1052642015414LL, 1083486780851LL,
281 1115235366904LL, 1147914257538LL, 1181550712759LL,
282 1216172791350LL, 1251809374282LL,
283 1288490188800LL, 1326245833225LL, 1365107802477LL,
284 1405108514349LL, 1446281336546LL, 1488660614523LL,
285 1532281700132LL, 1577180981113LL, 1623395911452LL,
286 1670965042615LL, 1719928055717LL, 1770325794615LL,
287 1822200299985LL, 1875594844387LL, 1930553968365LL,
288 1987123517599LL, 2045350681151LL, 2105284030829LL,
289 2166973561703LL, 2230470733808LL, 2295828515076LL,
290 2363101425518LL, 2432345582701LL, 2503618748564LL,
291 2576980377600LL, 2652491666450LL, 2730215604955LL,
292 2810217028699LL, 2892562673093LL, 2977321229047LL,
293 3064563400264LL, 3154361962227LL, 3246791822904LL,
294 3341930085231LL, 3439856111434LL, 3540651589231LL,
295 3644400599971LL, 3751189688775LL, 3861107936730LL,
296 3974247035198LL, 4090701362303LL, 4210568061659LL,
297 4333947123406LL, 4460941467616LL, 4591657030153LL,
298 4726202851036LL, 4864691165402LL, 5007237497128LL,
299 5153960755200LL, 5304983332900LL, 5460431209910LL,
300 5620434057398LL, 5785125346187LL, 5954642458094LL,
301 6129126800528LL, 6308723924455LL, 6493583645808LL,
302 6683860170462LL, 6879712222869LL, 7081303178462LL,
303 7288801199942LL, 7502379377550LL, 7722215873460LL,
304 7948494070397LL, 8181402724607LL, 8421136123319LL,
305 8667894246812LL, 8921882935232LL, 9183314060306LL,
306 9452405702073LL, 9729382330805LL, 10014474994257LL,
307 10307921510400LL, 10609966665800LL, 10920862419821LL,
308 11240868114797LL, 11570250692375LL, 11909284916188LL,
309 12258253601057LL, 12617447848911LL, 12987167291617LL,
310 13367720340924LL, 13759424445738LL, 14162606356925LL,
311 14577602399885LL, 15004758755101LL, 15444431746921LL,
312 15896988140794LL, 16362805449214LL, 16842272246638LL,
313 17335788493624LL, 17843765870465LL, 18366628120613LL,
314 18904811404146LL, 19458764661611LL, 20028949988515LL,
315 20615843020800LL, 21219933331600LL, 21841724839642LL,
316 22481736229595LL, 23140501384751LL, 23818569832376LL,
317 24516507202114LL, 25234895697822LL, 25974334583234LL,
318 26735440681849LL, 27518848891476LL, 28325212713851LL,
319 29155204799770LL, 30009517510202LL, 30888863493843LL,
320 31793976281588LL, 32725610898429LL, 33684544493277LL,
321 34671576987248LL, 35687531740931LL, 36733256241226LL,
322 37809622808292LL, 38917529323222LL, 40057899977031LL,
323 41231686041600LL, 42439866663201LL, 43683449679284LL,
324 44963472459190LL, 46281002769503LL, 47637139664753LL,
325 49033014404229LL, 50469791395645LL, 51948669166468LL,
326 53470881363698LL, 55037697782953LL, 56650425427703LL,
327 58310409599540LL, 60019035020404LL, 61777726987686LL,
328 63587952563177LL, 65451221796858LL, 67369088986555LL,
329 69343153974496LL, 71375063481862LL, 73466512482452LL,
330 75619245616585LL, 77835058646445LL, 80115799954063LL,
331 82463372083200LL, 84879733326402LL, 87366899358568LL,
332 89926944918381LL, 92562005539006LL, 95274279329506LL,
333 98066028808458LL, 100939582791291LL, 103897338332936LL,
334 106941762727397LL, 110075395565907LL, 113300850855406LL,
335 116620819199080LL, 120038070040809LL, 123555453975373LL,
336 127175905126354LL, 130902443593716LL, 134738177973111LL,
337 138686307948992LL, 142750126963725LL, 146933024964905LL,
338 151238491233171LL, 155670117292890LL, 160231599908126LL,
339 164926744166400LL, 169759466652804LL, 174733798717137LL,
340 179853889836763LL, 185124011078013LL, 190548558659013LL,
341 196132057616916LL, 201879165582582LL, 207794676665873LL,
342 213883525454795LL, 220150791131815LL, 226601701710812LL,
343 233241638398160LL, 240076140081619LL, 247110907950747LL,
344 254351810252708LL, 261804887187433LL, 269476355946222LL,
345 277372615897984LL, 285500253927451LL, 293866049929810LL,
346 302476982466342LL, 311340234585780LL, 320463199816253LL,
347 329853488332800LL, 339518933305609LL, 349467597434275LL,
348 359707779673527LL, 370248022156026LL, 381097117318027LL,
349 392264115233832LL, 403758331165164LL, 415589353331747LL,
350 427767050909590LL, 440301582263631LL, 453203403421624LL,
351 466483276796321LL, 480152280163238LL, 494221815901494LL,
352 508703620505416LL, 523609774374866LL, 538952711892444LL,
353 554745231795968LL, 571000507854903LL, 587732099859621LL,
354 604953964932684LL, 622680469171561LL, 640926399632506LL,
355 659706976665600LL, 679037866611218LL, 698935194868551LL,
356 719415559347055LL, 740496044312053LL, 762194234636054LL,
357 784528230467665LL, 807516662330329LL, 831178706663495LL,
358 855534101819180LL, 880603164527263LL, 906406806843249LL,
359 932966553592642LL, 960304560326477LL, 988443631802988LL,
360 1017407241010832LL, 1047219548749733LL,
361 1077905423784889LL, 1109490463591937LL,
362 1142001015709806LL, 1175464199719243LL,
363 1209907929865369LL, 1245360938343122LL,
364 1281852799265013LL,
365 1319413953331200LL, 1358075733222436LL, 1397870389737103LL,
366 1438831118694110LL, 1480992088624106LL,
367 1524388469272109LL, 1569056460935331LL,
368 1615033324660658LL, 1662357413326990LL,
369 1711068203638361LL, 1761206329054527LL,
370 1812813613686499LL, 1865933107185284LL,
371 1920609120652954LL, 1976887263605976LL};
374 __u8 __attribute__((const)) enc_log_64_11(__u32 value)
376 int i;
377 BUG_ON(log_64_11_table[255] != 571789581);
378 for (i=1;i<256;i++) {
379 if (log_64_11_table[i] > value)
380 break;
383 return (__u8)(i-1); /* round down */
386 __u32 __attribute__((const)) dec_log_64_11(__u8 value)
388 BUG_ON(log_64_11_table[255] != 571789581);
389 return log_64_11_table[value];
392 static void check_log_64_11_table(void)
394 int i;
395 BUG_ON(log_64_11_table[0] != 0);
396 for (i=1;i<256;i++) {
397 BUG_ON(log_64_11_table[i] <= log_64_11_table[i-1]);
401 __u16 __attribute__((const)) enc_log_300_24(__u64 value)
403 int i;
404 BUG_ON(log_300_24_table[1023] != 1976887263605976LL);
405 for (i=1;i<1024;i++) {
406 if (log_300_24_table[i] > value)
407 break;
410 return (__u16)(i-1); /* round down */
413 __u64 __attribute__((const)) dec_log_300_24(__u16 value)
415 BUG_ON(value >= 1024);
416 BUG_ON(log_300_24_table[1023] != 1976887263605976LL);
417 return log_300_24_table[value];
420 static void check_log_300_24_table(void)
422 int i;
423 BUG_ON(log_300_24_table[0] != 0);
424 for (i=1;i<1024;i++) {
425 BUG_ON(log_300_24_table[i] <= log_300_24_table[i-1]);
429 static inline __u64 mul_saturated(__u64 a, __u64 b)
431 __u64 res = a*b;
432 if (unlikely(res / a != b))
433 return -1;
434 return res;
437 static inline int numdigits(__u64 value)
439 int digits = 0;
440 for (;value != 0;value = (value >> 1)) {
441 digits++;
443 return digits;
446 /* approximate (a*b) / c without overflowing a*b */
447 __u64 __attribute__((const)) multiply_div(__u64 a, __u64 b, __u64 c)
449 int alen = numdigits(a);
450 int blen = numdigits(b);
451 int clen = numdigits(c);
453 BUG_ON(alen < 0 || alen > 64);
454 BUG_ON(blen < 0 || blen > 64);
455 BUG_ON(clen < 0 || clen > 64);
457 BUG_ON((a == 0 && alen != 0) || (a != 0 && alen == 0));
458 BUG_ON((b == 0 && blen != 0) || (b != 0 && blen == 0));
459 BUG_ON((c == 0 && clen != 0) || (c != 0 && clen == 0));
461 BUG_ON(a >= b && alen < blen);
462 BUG_ON(a >= c && alen < clen);
463 BUG_ON(b >= a && blen < alen);
464 BUG_ON(b >= c && blen < clen);
465 BUG_ON(c >= a && clen < alen);
466 BUG_ON(c >= b && clen < blen);
468 if (alen == 0 || blen == 0)
469 return 0;
471 BUG_ON(c == 0);
473 if (alen + blen <= 64)
474 return (a*b)/c;
476 if (a >= b && alen > clen + 16)
477 return mul_saturated(a/c, b);
478 else if (a < b && blen > clen + 16)
479 return mul_saturated(b/c, a);
481 while (alen + blen > 64) {
482 if (alen > blen || (alen == blen && a > b)) {
483 alen--;
484 a = (a >> 1);
485 } else {
486 blen--;
487 b = (b >> 1);
489 clen--;
490 c = (c >> 1);
493 return (a*b)/c;
496 static inline int hdr_size(void)
498 return ((sizeof(struct cell_hdr) + sizeof(void *) - 1) / sizeof(void *)
499 ) * sizeof(void *);
502 static inline int elements_per_cell(int cell_size)
504 return (cell_size - hdr_size())/sizeof(void *);
507 static inline struct cell_hdr *cell_addr(struct htable *ht, __u32 id)
509 int idx = (id%ht->htable_size) / (elements_per_cell(ht->cell_size));
510 return (struct cell_hdr *) (((char *)ht->htable) + ht->cell_size * idx);
513 static inline char **element_addr(struct htable *ht, __u32 id)
515 int idx = (id%ht->htable_size) % (elements_per_cell(ht->cell_size));
516 return (char **)
517 ( ((char *)cell_addr(ht, id)) +
518 hdr_size() + idx*sizeof(void *) );
522 static inline char **next_element(struct htable *ht, char *element)
524 return (char **)(element + ht->entry_offset);
527 static inline struct kref *element_kref(struct htable *ht, char *element)
529 return (struct kref *)(element + ht->kref_offset);
533 static inline void unlock_element(struct htable *ht, __u32 key)
535 struct cell_hdr *hdr = cell_addr(ht, key);
536 spin_unlock( &(hdr->lock) );
540 static char **get_element_nounlock(struct htable *ht, __u32 key,
541 void *searcheditem)
543 struct cell_hdr *hdr = cell_addr(ht, key);
544 char **element = element_addr(ht, key);
546 BUG_ON(0 == element);
548 spin_lock( &(hdr->lock) );
550 while (1) {
551 if (*element == 0)
552 break;
553 if (searcheditem != 0 && ht->matches(*element, searcheditem))
554 break;
555 element = next_element(ht, *element);
558 return element;
561 char *htable_get(struct htable *ht, __u32 key, void *searcheditem)
563 unsigned long iflags;
564 char *element;
566 if (unlikely(ht->htable == 0))
567 return 0;
569 local_irq_save(iflags);
570 element = *(get_element_nounlock(ht, key, searcheditem));
571 if (likely(element != 0))
572 kref_get(element_kref(ht, element));
573 unlock_element(ht, key);
574 local_irq_restore(iflags);
576 return element;
579 int htable_delete(struct htable *ht, __u32 key,
580 void *searcheditem, void (*free) (struct kref *ref))
582 unsigned long iflags;
583 char **element;
584 char **next;
585 int rc = 0;
587 if (unlikely(ht->htable == 0))
588 return 1;
590 local_irq_save(iflags);
592 element = get_element_nounlock(ht, key, searcheditem);
593 BUG_ON(0 == element);
595 if (unlikely(*element == 0)) {
596 /* key not in table */
597 rc = 1;
598 goto out;
601 next = next_element(ht, *element);
602 kref_put(element_kref(ht, *element), free);
603 *element = *next;
605 out:
606 unlock_element(ht, key);
607 local_irq_restore(iflags);
609 return rc;
612 void htable_insert(struct htable *ht, char *newelement, __u32 key)
614 unsigned long iflags;
615 char **element;
617 if (unlikely(ht->htable == 0))
618 return;
620 BUG_ON(*next_element(ht, newelement) != 0);
621 local_irq_save(iflags);
623 element = get_element_nounlock(ht, key, 0);
625 BUG_ON(element == 0);
626 BUG_ON(*element != 0);
628 *element = newelement;
629 kref_get(element_kref(ht, newelement));
631 unlock_element(ht, key);
632 local_irq_restore(iflags);
636 void htable_init(struct htable *ht, int (*matches)(void *htentry,
637 void *searcheditem), __u32 entry_offset, __u32 kref_offset)
639 int num_cells;
640 int j;
642 BUG_ON(0 == ht);
644 ht->htable = kmalloc(PAGE_SIZE, GFP_KERNEL);
645 if (unlikely(ht->htable == 0)) {
646 printk(KERN_CRIT "cor: error allocating htable (out of "
647 "memory?)");
648 return;
650 memset(ht->htable, 0, PAGE_SIZE);
651 ht->cell_size = 256;
653 num_cells = PAGE_SIZE/ht->cell_size;
655 for (j=0;j<num_cells;j++) {
656 struct cell_hdr *hdr = (struct cell_hdr *)
657 ( ((char *) ht->htable) + j * ht->cell_size );
658 spin_lock_init(&(hdr->lock));
661 ht->htable_size = num_cells * elements_per_cell(ht->cell_size);
662 ht->num_elements = 0;
664 ht->matches = matches;
665 ht->entry_offset = entry_offset;
666 ht->kref_offset = kref_offset;
669 struct reverse_connid_matchparam {
670 struct neighbor *nb;
671 __u32 conn_id;
674 static __u32 rcm_to_key(struct reverse_connid_matchparam *rcm)
676 return (((__u32) rcm->nb) ^ rcm->conn_id);
679 static int matches_reverse_connid(void *htentry, void *searcheditem)
681 struct conn *conn = (struct conn *) htentry;
682 struct reverse_connid_matchparam *rcm =
683 (struct reverse_connid_matchparam *) searcheditem;
684 BUG_ON(conn->targettype != TARGET_OUT);
685 return (conn->target.out.nb == rcm->nb) &&
686 (conn->target.out.conn_id == rcm->conn_id);
689 struct conn *get_conn_reverse(struct neighbor *nb, __u32 conn_id)
691 struct reverse_connid_matchparam rcm;
692 rcm.nb = nb;
693 rcm.conn_id = conn_id;
695 return (struct conn *) htable_get(&reverse_connid_table,
696 rcm_to_key(&rcm), &rcm);
699 void insert_reverse_connid(struct conn *rconn)
701 struct reverse_connid_matchparam rcm;
703 BUG_ON(rconn->targettype != TARGET_OUT);
705 rcm.nb = rconn->target.out.nb;
706 rcm.conn_id = rconn->target.out.conn_id;
707 htable_insert(&reverse_connid_table, (char *) rconn, rcm_to_key(&rcm));
710 struct conn *get_conn(__u32 conn_id)
712 return (struct conn *) htable_get(&connid_table, conn_id, &conn_id);
715 static int connid_alloc(struct conn *sconn)
717 __u32 conn_id;
718 int i;
720 BUG_ON(sconn->sourcetype != SOURCE_IN);
722 mutex_lock(&connid_gen);
723 for(i=0;i<16;i++) {
724 struct conn *tmp;
726 conn_id = 0;
727 get_random_bytes((char *) &conn_id, sizeof(conn_id));
729 if (unlikely(conn_id == 0))
730 continue;
732 tmp = get_conn(conn_id);
733 if (unlikely(tmp != 0)) {
734 kref_put(&(tmp->ref), free_conn);
735 continue;
738 goto found;
740 mutex_unlock(&connid_gen);
742 return 1;
744 found:
745 sconn->source.in.conn_id = conn_id;
746 htable_insert(&connid_table, (char *) sconn, conn_id);
747 mutex_unlock(&connid_gen);
748 return 0;
751 void free_conn(struct kref *ref)
753 unsigned long iflags;
754 struct conn *conn = container_of(ref, struct conn, ref);
755 struct conn *reversedir = 0;
757 BUG_ON(atomic_read(&(conn->isreset)) == 0);
759 spin_lock_irqsave(&conn_free, iflags);
761 if (conn->reversedir != 0)
762 atomic_set(&(conn->reversedir->isreset), 3);
764 if (atomic_read(&(conn->isreset)) != 3)
765 goto out;
767 if (conn->reversedir != 0) {
768 conn->reversedir->reversedir = 0;
769 reversedir = conn->reversedir;
770 conn->reversedir = 0;
773 if (conn->sourcetype == SOURCE_IN) {
774 kref_put(&(conn->source.in.nb->ref), neighbor_free);
775 conn->source.in.nb = 0;
778 if (conn->targettype == TARGET_OUT) {
779 kref_put(&(conn->target.out.nb->ref), neighbor_free);
780 conn->target.out.nb = 0;
783 BUG_ON(conn->data_buf.totalsize != 0);
784 BUG_ON(conn->data_buf.overhead != 0);
786 kmem_cache_free(conn_slab, conn);
788 out:
789 spin_unlock_irqrestore(&conn_free, iflags);
791 if (reversedir != 0)
792 free_conn(&(reversedir->ref));
796 * rconn ==> the connection we received the commend from
797 * ==> init rconn->target.out + rconn->reversedir->source.in
798 * rc == 0 ==> ok
799 * rc == 1 ==> connid allocation failed
801 * NOTE: call to this func *must* be protected by rcv_lock
803 int conn_init_out(struct conn *rconn, struct neighbor *nb)
805 int rc = 0;
806 struct conn *sconn = rconn->reversedir;
808 mutex_lock(&(sconn->rcv_lock));
810 BUG_ON(rconn->targettype != TARGET_UNCONNECTED);
811 BUG_ON(sconn == 0);
812 BUG_ON(sconn->sourcetype != SOURCE_NONE);
814 memset(&(rconn->target.out), 0, sizeof(rconn->target.out));
815 memset(&(sconn->source.in), 0, sizeof(sconn->source.in));
817 rconn->targettype = TARGET_OUT;
818 sconn->sourcetype = SOURCE_IN;
820 rconn->target.out.nb = nb;
821 sconn->source.in.nb = nb;
823 /* neighbor pointer */
824 kref_get(&(nb->ref));
825 kref_get(&(nb->ref));
827 skb_queue_head_init(&(sconn->source.in.reorder_queue));
828 atomic_set(&(sconn->source.in.pong_awaiting), 0);
830 if (unlikely(connid_alloc(sconn))) {
831 rc = 1;
832 goto out;
835 INIT_LIST_HEAD(&(rconn->target.out.retrans_list));
837 reset_seqno(rconn, 0);
838 get_random_bytes((char *) &(sconn->source.in.next_seqno),
839 sizeof(sconn->source.in.next_seqno));
841 get_random_bytes((char *) &(rconn->target.out.decaytime_seqno), 1);
842 rconn->target.out.decaytime_seqno = rconn->target.out.decaytime_seqno %
844 rconn->source.in.decaytime_seqno = 255;
847 mutex_lock(&(nb->conn_list_lock));
848 list_add_tail(&(sconn->source.in.nb_list), &(nb->rcv_conn_list));
849 list_add_tail(&(rconn->target.out.nb_list), &(nb->snd_conn_list));
850 nb->num_send_conns++;
851 mutex_unlock(&(nb->conn_list_lock));
853 /* neighbor lists */
854 kref_get(&(rconn->ref));
855 kref_get(&(sconn->ref));
857 atomic_inc(&num_conns);
860 refresh_conn_credits(rconn, 0, 1);
862 out:
863 mutex_unlock(&(sconn->rcv_lock));
865 return rc;
868 void conn_init_sock_source(struct conn *conn)
870 BUG_ON(conn == 0);
871 conn->sourcetype = SOURCE_SOCK;
872 memset(&(conn->source.sock), 0, sizeof(conn->source.sock));
873 init_waitqueue_head(&(conn->source.sock.wait));
876 void conn_init_sock_target(struct conn *conn)
878 BUG_ON(conn == 0);
879 conn->targettype = TARGET_SOCK;
880 memset(&(conn->target.sock), 0, sizeof(conn->target.sock));
881 init_waitqueue_head(&(conn->target.sock.wait));
882 reset_seqno(conn, 0);
885 struct conn* alloc_conn(gfp_t allocflags)
887 struct conn *rconn = 0;
888 struct conn *sconn = 0;
890 rconn = kmem_cache_alloc(conn_slab, allocflags);
891 if (unlikely(rconn == 0))
892 goto out_err0;
894 sconn = kmem_cache_alloc(conn_slab, allocflags);
895 if (unlikely(sconn == 0))
896 goto out_err1;
898 memset(rconn, 0, sizeof(struct conn));
899 memset(sconn, 0, sizeof(struct conn));
901 rconn->reversedir = sconn;
902 sconn->reversedir = rconn;
904 kref_init(&(rconn->ref));
905 kref_init(&(sconn->ref));
907 rconn->sockstate = SOCKSTATE_CONN;
908 sconn->sockstate = SOCKSTATE_CONN;
910 rconn->sourcetype = SOURCE_NONE;
911 sconn->sourcetype = SOURCE_NONE;
912 rconn->targettype = TARGET_UNCONNECTED;
913 sconn->targettype = TARGET_UNCONNECTED;
915 atomic_set(&(rconn->isreset), 0);
916 atomic_set(&(sconn->isreset), 0);
918 mutex_init(&(rconn->rcv_lock));
919 mutex_init(&(sconn->rcv_lock));
921 rconn->jiffies_credit_update = jiffies;
922 sconn->jiffies_credit_update = rconn->jiffies_credit_update;
924 rconn->crate_forward = ((1 << 31) - (((__u32) 1 << 31) / 10));
925 sconn->crate_forward = ((1 << 31) - (((__u32) 1 << 31) / 10));
927 databuf_init(rconn);
928 databuf_init(sconn);
930 return rconn;
932 out_err1:
933 kmem_cache_free(conn_slab, rconn);
934 out_err0:
935 return 0;
938 static struct connlistener *get_connlistener(__be64 port)
940 struct list_head *curr = openports.next;
942 while (curr != &openports) {
943 struct bindnode *currnode = ((struct bindnode *)
944 (((char *)curr) - offsetof(struct bindnode, lh)));
945 if (currnode->port == port) {
946 BUG_ON(currnode->owner == 0);
947 return currnode->owner;
950 curr = curr->next;
953 return 0;
956 void close_port(struct connlistener *listener)
958 mutex_lock(&cor_bindnodes);
960 if (listener->bn != 0) {
961 list_del(&(listener->bn->lh));
962 kmem_cache_free(bindnode_slab, listener->bn);
963 listener->bn = 0;
966 while (list_empty(&(listener->conn_queue)) == 0) {
967 struct conn *rconn = container_of(listener->conn_queue.next,
968 struct conn, source.sock.cl_list);
969 list_del(&(rconn->source.sock.cl_list));
970 atomic_cmpxchg(&(rconn->reversedir->isreset), 0, 1);
971 reset_conn(rconn);
972 kref_put(&(rconn->ref), free_conn);
975 kmem_cache_free(connlistener_slab, listener);
977 mutex_unlock(&cor_bindnodes);
980 struct connlistener *open_port(__be64 port)
983 struct bindnode *bn = 0;
984 struct connlistener *listener = 0;
986 mutex_lock(&cor_bindnodes);
987 if (get_connlistener(port) != 0)
988 goto out;
991 bn = kmem_cache_alloc(bindnode_slab, GFP_KERNEL);
992 listener = kmem_cache_alloc(connlistener_slab, GFP_KERNEL);
994 memset(bn, 0, sizeof(struct bindnode));
995 memset(listener, 0, sizeof(struct connlistener));
997 bn->owner = listener;
998 bn->port = port;
1000 /* kref is not actually used */
1001 listener->sockstate = SOCKSTATE_LISTENER;
1002 listener->bn = bn;
1003 mutex_init(&(listener->lock));
1004 INIT_LIST_HEAD(&(listener->conn_queue));
1005 init_waitqueue_head(&(listener->wait));
1007 list_add_tail((struct list_head *) &(bn->lh), &openports);
1009 out:
1010 mutex_unlock(&cor_bindnodes);
1012 return listener;
1016 * rc == 0 connected
1017 * rc == 2 port not open
1018 * rc == 3 listener queue full
1020 int connect_port(struct conn *rconn, __be64 port)
1023 struct connlistener *listener;
1024 int rc = 0;
1026 mutex_lock(&cor_bindnodes);
1028 listener = get_connlistener(port);
1029 if (listener == 0) {
1030 rc = 2;
1031 goto out;
1034 mutex_lock(&(listener->lock));
1036 if (unlikely(listener->queue_len >= listener->queue_maxlen)) {
1037 if (listener->queue_maxlen <= 0)
1038 rc = 2;
1039 else
1040 rc = 3;
1042 goto out2;
1045 kref_get(&(rconn->reversedir->ref));
1047 mutex_lock(&(rconn->reversedir->rcv_lock));
1048 conn_init_sock_target(rconn);
1049 conn_init_sock_source(rconn->reversedir);
1050 mutex_unlock(&(rconn->reversedir->rcv_lock));
1052 list_add_tail(&(rconn->reversedir->source.sock.cl_list),
1053 &(listener->conn_queue));
1054 listener->queue_len++;
1055 wake_up_interruptible(&(listener->wait));
1057 out2:
1058 mutex_unlock(&(listener->lock));
1060 out:
1061 mutex_unlock(&cor_bindnodes);
1062 return rc;
1066 * rc == 0 connected
1067 * rc == 2 addrtype not found
1068 * rc == 3 addr not found
1069 * rc == 4 ==> connid allocation failed
1070 * rc == 5 ==> control msg alloc failed
1072 int connect_neigh(struct conn *rconn,
1073 __u16 addrtypelen, __u8 *addrtype,
1074 __u16 addrlen, __u8 *addr)
1076 int rc = 0;
1077 struct control_msg_out *cm;
1078 struct neighbor *nb = find_neigh(addrtypelen, addrtype, addrlen, addr);
1080 if (nb == 0)
1081 return 3;
1082 if (unlikely(conn_init_out(rconn, nb))) {
1083 rc = 4;
1084 goto neigh_kref;
1087 cm = alloc_control_msg(nb, ACM_PRIORITY_HIGH);
1088 if (unlikely(cm == 0)) {
1089 rc = 5;
1090 goto neigh_kref;
1093 send_connect_nb(cm, rconn->reversedir->source.in.conn_id,
1094 rconn->reversedir->source.in.next_seqno,
1095 rconn->reversedir);
1097 neigh_kref:
1098 kref_put(&(nb->ref), neighbor_free);
1100 return rc;
1103 void reset_ping(struct conn *rconn)
1105 struct neighbor *nb = rconn->source.in.nb;
1106 if (atomic_read(&(rconn->source.in.pong_awaiting)) != 0) {
1107 mutex_lock(&(nb->conn_list_lock));
1108 if (atomic_read(&(rconn->source.in.pong_awaiting)) == 0)
1109 goto unlock;
1111 atomic_set(&(rconn->source.in.pong_awaiting), 0);
1112 nb->pong_conns_expected--;
1113 unlock:
1114 mutex_unlock(&(nb->conn_list_lock));
1118 static int _reset_conn(struct conn *conn)
1121 * aktive conns have an additional ref to make sure that they are not
1122 * freed when only one direction is referenced by the connid hashtable
1124 int krefput = 1;
1125 int isreset = atomic_cmpxchg(&(conn->isreset), 0, 2);
1126 if (isreset == 1)
1127 isreset = atomic_cmpxchg(&(conn->isreset), 1, 2);
1129 if (isreset == 2 || isreset == 3)
1130 return 0;
1132 /* lock sourcetype/targettype */
1133 mutex_lock(&(conn->rcv_lock));
1135 if (conn->sourcetype == SOURCE_IN) {
1136 mutex_lock(&(conn->source.in.nb->conn_list_lock));
1137 list_del(&(conn->source.in.nb_list));
1138 mutex_unlock(&(conn->source.in.nb->conn_list_lock));
1140 krefput++;
1142 if (conn->source.in.conn_id != 0) {
1143 if (htable_delete(&connid_table,
1144 conn->source.in.conn_id,
1145 &(conn->source.in.conn_id), free_conn)){
1146 printk(KERN_ERR "error in _reset_conn: "
1147 "htable_delete src_in failed");
1149 conn->source.in.conn_id = 0;
1152 reset_ping(conn);
1154 atomic_dec(&num_conns);
1155 BUG_ON(atomic_read(&num_conns) < 0);
1156 } else if (conn->sourcetype == SOURCE_SOCK) {
1157 wake_up_interruptible(&(conn->source.sock.wait));
1160 if (conn->targettype == TARGET_UNCONNECTED) {
1161 connreset_cpacket_buffer(conn);
1162 } else if (conn->targettype == TARGET_OUT) {
1163 mutex_lock(&(conn->target.out.nb->conn_list_lock));
1164 list_del(&(conn->target.out.nb_list));
1165 conn->target.out.nb->num_send_conns--;
1166 BUG_ON(conn->target.out.nb->num_send_conns < 0);
1167 mutex_unlock(&(conn->target.out.nb->conn_list_lock));
1169 krefput++;
1171 if (conn->target.out.conn_id != 0) {
1172 struct reverse_connid_matchparam rcm;
1173 rcm.nb = conn->target.out.nb;
1174 rcm.conn_id = conn->target.out.conn_id;
1175 if (htable_delete(&reverse_connid_table,
1176 rcm_to_key(&rcm),
1177 &rcm, free_conn)){
1178 printk(KERN_ERR "error in _reset_conn: "
1179 "htable_delete target_out "
1180 "failed");
1184 if (isreset == 0 && conn->target.out.conn_id != 0) {
1185 struct control_msg_out *cm = alloc_control_msg(
1186 conn->target.out.nb, ACM_PRIORITY_HIGH);
1187 if (unlikely(cm == 0))
1188 send_ping_all_conns(conn->target.out.nb);
1189 else
1190 send_reset_conn(cm, conn->target.out.conn_id);
1193 conn->target.out.conn_id = 0;
1195 cancel_retrans(conn);
1196 } else if (conn->targettype == TARGET_SOCK) {
1197 wake_up_interruptible(&(conn->target.sock.wait));
1200 databuf_free(conn);
1202 mutex_unlock(&(conn->rcv_lock));
1204 qos_remove_conn(conn);
1205 reset_bufferusage(conn);
1206 unreserve_sock_buffer(conn);
1207 connreset_credits(conn);
1209 mutex_lock(&(conn->rcv_lock));
1210 if (conn->sourcetype != SOURCE_SOCK) {
1211 mutex_unlock(&(conn->rcv_lock));
1212 goto skipbufferlimits;
1214 mutex_unlock(&(conn->rcv_lock));
1216 mutex_lock(&sock_bufferlimits_lock);
1217 mutex_lock(&(conn->rcv_lock));
1218 if (conn->sourcetype == SOURCE_SOCK) {
1219 BUG_ON(conn->source.sock.in_alwait_list);
1220 conn->source.sock.sbt->usage -= conn->source.sock.alloclimit;
1221 if (conn->source.sock.delay_flush) {
1222 conn->source.sock.delay_flush = 0;
1223 list_del(&(conn->source.sock.delflush_list));
1225 kref_put(&(conn->source.sock.sbt->ref), free_sbt);
1226 conn->source.sock.sbt = 0;
1228 mutex_unlock(&(conn->rcv_lock));
1229 mutex_unlock(&sock_bufferlimits_lock);
1231 skipbufferlimits:
1232 return krefput;
1235 /* warning: do not hold the rcv_lock while calling this! */
1236 void reset_conn(struct conn *conn)
1238 int put1 = _reset_conn(conn);
1239 int put2 = _reset_conn(conn->reversedir);
1241 /* free_conn may not be called, before both _reset_conn have finished */
1242 while (put1 > 0) {
1243 kref_put(&(conn->ref), free_conn);
1244 put1--;
1247 while (put2 > 0) {
1248 kref_put(&(conn->reversedir->ref), free_conn);
1249 put2--;
1253 static int matches_connid_in(void *htentry, void *searcheditem)
1255 struct conn *conn = (struct conn *) htentry;
1256 __u32 conn_id = *((__u32 *) searcheditem);
1257 BUG_ON(conn->sourcetype != SOURCE_IN);
1258 return (conn->source.in.conn_id == conn_id);
1261 static int __init cor_common_init(void)
1263 int rc;
1265 struct conn c;
1267 check_log_64_11_table();
1268 check_log_300_24_table();
1270 printk(KERN_ERR "sizeof conn: %d", sizeof(c));
1271 printk(KERN_ERR " conn.source: %d", sizeof(c.source));
1272 printk(KERN_ERR " conn.target: %d", sizeof(c.target));
1273 printk(KERN_ERR " conn.target.out: %d", sizeof(c.target.out));
1274 printk(KERN_ERR " conn.buf: %d", sizeof(c.data_buf));
1276 printk(KERN_ERR " mutex: %d", sizeof(struct mutex));
1277 printk(KERN_ERR " spinlock: %d", sizeof(spinlock_t));
1278 printk(KERN_ERR " kref: %d", sizeof(struct kref));
1280 conn_slab = kmem_cache_create("cor_conn", sizeof(struct conn), 8, 0, 0);
1281 htable_init(&connid_table, matches_connid_in,
1282 offsetof(struct conn, source.in.htab_entry),
1283 offsetof(struct conn, ref));
1285 htable_init(&reverse_connid_table, matches_reverse_connid,
1286 offsetof(struct conn, target.out.htab_entry),
1287 offsetof(struct conn, ref));
1289 bindnode_slab = kmem_cache_create("cor_bindnode",
1290 sizeof(struct bindnode), 8, 0, 0);
1291 connlistener_slab = kmem_cache_create("cor_connlistener",
1292 sizeof(struct connlistener), 8, 0, 0);
1294 atomic_set(&num_conns, 0);
1296 credits_init();
1297 forward_init();
1298 cor_kgen_init();
1299 cor_cpacket_init();
1301 rc = cor_snd_init();
1302 if (unlikely(rc != 0))
1303 return rc;
1305 rc = cor_neighbor_init();
1306 if (unlikely(rc != 0))
1307 return rc;
1309 rc = cor_rcv_init();
1310 if (unlikely(rc != 0))
1311 return rc;
1313 return 0;
1316 module_init(cor_common_init);
1317 MODULE_LICENSE("GPL");