conn rcv_lock converted to spinlock, struct cor_sock created, kernel_packet skb_clone...
[cor_2_6_31.git] / net / cor / common.c
blobe61280f839b89198d18ce2c1f5f43114ea8fb2a7
1 /**
2 * Connection oriented routing
3 * Copyright (C) 2007-2011 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_SPINLOCK(cor_bindnodes);
26 DEFINE_SPINLOCK(conn_free);
27 DEFINE_SPINLOCK(connid_gen);
29 LIST_HEAD(openports);
32 struct cell_hdr{
33 spinlock_t lock;
37 struct kmem_cache *conn_slab;
38 struct kmem_cache *connid_reuse_slab;
41 struct htable connid_table;
42 struct htable reverse_connid_table;
43 struct htable connid_reuse_table;
45 atomic_t num_conns;
48 /* see cor.h/KP_ACK_CONN */
49 static const __u32 log_64_11_table[] = {0,
50 64, 68, 73, 77, 82, 88, 93, 99, 106, 113, 120,
51 128, 136, 145, 155, 165, 175, 187, 199, 212, 226, 240,
52 256, 273, 290, 309, 329, 351, 374, 398, 424, 451, 481,
53 512, 545, 581, 619, 659, 702, 747, 796, 848, 903, 961,
54 1024, 1091, 1162, 1237, 1318, 1403, 1495, 1592, 1695, 1805,
55 1923,
56 2048, 2181, 2323, 2474, 2635, 2806, 2989, 3183, 3390, 3611,
57 3846,
58 4096, 4362, 4646, 4948, 5270, 5613, 5978, 6367, 6781, 7222,
59 7692,
60 8192, 8725, 9292, 9897, 10540, 11226, 11956, 12734, 13562,
61 14444, 15383,
62 16384, 17450, 18585, 19793, 21081, 22452, 23912, 25467, 27124,
63 28888, 30767,
64 32768, 34899, 37169, 39587, 42161, 44904, 47824, 50935, 54248,
65 57776, 61534,
66 65536, 69799, 74338, 79173, 84323, 89807, 95648, 101870, 108495,
67 115552, 123068,
68 131072, 139597, 148677, 158347, 168646, 179615, 191297, 203739,
69 216991, 231104, 246135,
70 262144, 279194, 297353, 316693, 337291, 359229, 382594, 407478,
71 433981, 462208, 492270,
72 524288, 558388, 594706, 633387, 674583, 718459, 765188, 814957,
73 867962, 924415, 984540,
74 1048576, 1116777, 1189413, 1266774, 1349166, 1436917, 1530376,
75 1629913, 1735924, 1848831, 1969081,
76 2097152, 2233553, 2378826, 2533547, 2698332, 2873834, 3060752,
77 3259826, 3471849, 3697662, 3938162,
78 4194304, 4467106, 4757652, 5067094, 5396664, 5747669, 6121503,
79 6519652, 6943698, 7395323, 7876323,
80 8388608, 8934212, 9515303, 10134189, 10793327, 11495337,
81 12243006, 13039305, 13887396, 14790647,
82 15752647,
83 16777216, 17868424, 19030606, 20268378, 21586655, 22990674,
84 24486013, 26078610, 27774791, 29581294,
85 31505293,
86 33554432, 35736849, 38061212, 40536755, 43173310, 45981349,
87 48972026, 52157220, 55549582, 59162588,
88 63010587,
89 67108864, 71473698, 76122425, 81073510, 86346620, 91962698,
90 97944052, 104314440, 111099165, 118325175,
91 126021174,
92 134217728, 142947395, 152244850, 162147020, 172693239,
93 183925396, 195888104, 208628880, 222198329,
94 236650351, 252042347,
95 268435456, 285894791, 304489699, 324294041, 345386479,
96 367850791, 391776208, 417257759, 444396658,
97 473300701, 504084694,
98 536870912, 571789581};
100 static const __u64 log_300_24_table[] = {0LL,
101 300LL, 308LL, 317LL, 327LL, 336LL, 346LL, 356LL, 367LL, 377LL,
102 389LL, 400LL, 412LL, 424LL, 436LL, 449LL, 462LL, 476LL,
103 490LL, 504LL, 519LL, 534LL, 550LL, 566LL, 582LL,
104 600LL, 617LL, 635LL, 654LL, 673LL, 693LL, 713LL, 734LL, 755LL,
105 778LL, 800LL, 824LL, 848LL, 873LL, 898LL, 925LL, 952LL,
106 980LL, 1009LL, 1038LL, 1069LL, 1100LL, 1132LL, 1165LL,
107 1200LL, 1235LL, 1271LL, 1308LL, 1346LL, 1386LL, 1427LL, 1468LL,
108 1511LL, 1556LL, 1601LL, 1648LL, 1697LL, 1746LL, 1797LL,
109 1850LL, 1904LL, 1960LL, 2018LL, 2077LL, 2138LL, 2200LL,
110 2265LL, 2331LL,
111 2400LL, 2470LL, 2542LL, 2617LL, 2693LL, 2772LL, 2854LL, 2937LL,
112 3023LL, 3112LL, 3203LL, 3297LL, 3394LL, 3493LL, 3595LL,
113 3701LL, 3809LL, 3921LL, 4036LL, 4154LL, 4276LL, 4401LL,
114 4530LL, 4663LL,
115 4800LL, 4940LL, 5085LL, 5234LL, 5387LL, 5545LL, 5708LL, 5875LL,
116 6047LL, 6224LL, 6407LL, 6594LL, 6788LL, 6987LL, 7191LL,
117 7402LL, 7619LL, 7842LL, 8072LL, 8309LL, 8552LL, 8803LL,
118 9061LL, 9326LL,
119 9600LL, 9881LL, 10170LL, 10468LL, 10775LL, 11091LL, 11416LL,
120 11750LL, 12095LL, 12449LL, 12814LL, 13189LL, 13576LL,
121 13974LL, 14383LL, 14805LL, 15239LL, 15685LL, 16145LL,
122 16618LL, 17105LL, 17606LL, 18122LL, 18653LL,
123 19200LL, 19762LL, 20341LL, 20937LL, 21551LL, 22182LL, 22832LL,
124 23501LL, 24190LL, 24899LL, 25628LL, 26379LL, 27152LL,
125 27948LL, 28767LL, 29610LL, 30478LL, 31371LL, 32290LL,
126 33236LL, 34210LL, 35212LL, 36244LL, 37306LL,
127 38400LL, 39525LL, 40683LL, 41875LL, 43102LL, 44365LL, 45665LL,
128 47003LL, 48380LL, 49798LL, 51257LL, 52759LL, 54305LL,
129 55897LL, 57534LL, 59220LL, 60956LL, 62742LL, 64580LL,
130 66473LL, 68421LL, 70425LL, 72489LL, 74613LL,
131 76800LL, 79050LL, 81366LL, 83750LL, 86205LL, 88731LL, 91331LL,
132 94007LL, 96761LL, 99597LL, 102515LL, 105519LL, 108611LL,
133 111794LL, 115069LL, 118441LL, 121912LL, 125484LL,
134 129161LL, 132946LL, 136842LL, 140851LL, 144979LL,
135 149227LL,
136 153600LL, 158100LL, 162733LL, 167501LL, 172410LL, 177462LL,
137 182662LL, 188014LL, 193523LL, 199194LL, 205031LL,
138 211039LL, 217223LL, 223588LL, 230139LL, 236883LL,
139 243824LL, 250969LL, 258323LL, 265892LL, 273684LL,
140 281703LL, 289958LL, 298454LL,
141 307200LL, 316201LL, 325467LL, 335003LL, 344820LL, 354924LL,
142 365324LL, 376029LL, 387047LL, 398389LL, 410062LL,
143 422078LL, 434446LL, 447176LL, 460279LL, 473767LL,
144 487649LL, 501938LL, 516646LL, 531785LL, 547368LL,
145 563407LL, 579916LL, 596909LL,
146 614400LL, 632403LL, 650934LL, 670007LL, 689640LL, 709848LL,
147 730648LL, 752058LL, 774095LL, 796778LL, 820125LL,
148 844157LL, 868892LL, 894353LL, 920559LL, 947534LL,
149 975299LL, 1003877LL, 1033293LL, 1063571LL, 1094736LL,
150 1126814LL, 1159832LL, 1193818LL,
151 1228800LL, 1264806LL, 1301868LL, 1340015LL, 1379281LL,
152 1419697LL, 1461297LL, 1504116LL, 1548190LL, 1593556LL,
153 1640251LL, 1688314LL, 1737785LL, 1788706LL, 1841119LL,
154 1895068LL, 1950598LL, 2007755LL, 2066587LL, 2127142LL,
155 2189472LL, 2253629LL, 2319665LL, 2387636LL,
156 2457600LL, 2529613LL, 2603736LL, 2680031LL, 2758562LL,
157 2839394LL, 2922595LL, 3008233LL, 3096381LL, 3187112LL,
158 3280502LL, 3376628LL, 3475571LL, 3577413LL, 3682239LL,
159 3790137LL, 3901196LL, 4015510LL, 4133174LL, 4254285LL,
160 4378945LL, 4507258LL, 4639331LL, 4775273LL,
161 4915200LL, 5059226LL, 5207473LL, 5360063LL, 5517125LL,
162 5678789LL, 5845190LL, 6016467LL, 6192763LL, 6374225LL,
163 6561004LL, 6753256LL, 6951142LL, 7154826LL, 7364478LL,
164 7580274LL, 7802393LL, 8031021LL, 8266348LL, 8508570LL,
165 8757890LL, 9014516LL, 9278662LL, 9550547LL,
166 9830400LL, 10118452LL, 10414946LL, 10720127LL, 11034250LL,
167 11357579LL, 11690381LL, 12032935LL, 12385527LL,
168 12748451LL, 13122009LL, 13506513LL, 13902285LL,
169 14309653LL, 14728957LL, 15160549LL, 15604787LL,
170 16062042LL, 16532696LL, 17017141LL, 17515781LL,
171 18029033LL, 18557324LL, 19101095LL,
172 19660800LL, 20236905LL, 20829892LL, 21440254LL, 22068501LL,
173 22715158LL, 23380763LL, 24065871LL, 24771055LL,
174 25496903LL, 26244019LL, 27013027LL, 27804570LL,
175 28619306LL, 29457915LL, 30321098LL, 31209574LL,
176 32124084LL, 33065392LL, 34034282LL, 35031563LL,
177 36058066LL, 37114648LL, 38202190LL,
178 39321600LL, 40473810LL, 41659784LL, 42880508LL, 44137003LL,
179 45430316LL, 46761526LL, 48131743LL, 49542111LL,
180 50993806LL, 52488038LL, 54026055LL, 55609140LL,
181 57238612LL, 58915831LL, 60642197LL, 62419149LL,
182 64248169LL, 66130784LL, 68068564LL, 70063126LL,
183 72116132LL, 74229296LL, 76404380LL,
184 78643200LL, 80947621LL, 83319568LL, 85761017LL, 88274007LL,
185 90860633LL, 93523052LL, 96263487LL, 99084223LL,
186 101987612LL, 104976077LL, 108052111LL, 111218280LL,
187 114477224LL, 117831663LL, 121284394LL, 124838298LL,
188 128496339LL, 132261569LL, 136137129LL, 140126252LL,
189 144232264LL, 148458592LL, 152808761LL,
190 157286400LL, 161895243LL, 166639136LL, 171522035LL, 176548014LL,
191 181721266LL, 187046105LL, 192526975LL, 198168446LL,
192 203975224LL, 209952155LL, 216104222LL, 222436560LL,
193 228954448LL, 235663326LL, 242568788LL, 249676596LL,
194 256992679LL, 264523139LL, 272274259LL, 280252504LL,
195 288464529LL, 296917185LL, 305617523LL,
196 314572800LL, 323790486LL, 333278272LL, 343044070LL, 353096029LL,
197 363442532LL, 374092211LL, 385053950LL, 396336892LL,
198 407950449LL, 419904310LL, 432208445LL, 444873120LL,
199 457908897LL, 471326652LL, 485137577LL, 499353193LL,
200 513985359LL, 529046279LL, 544548518LL, 560505008LL,
201 576929058LL, 593834370LL, 611235046LL,
202 629145600LL, 647580973LL, 666556544LL, 686088141LL,
203 706192058LL, 726885065LL, 748184423LL, 770107900LL,
204 792673784LL, 815900899LL, 839808620LL, 864416891LL,
205 889746240LL, 915817795LL, 942653304LL, 970275155LL,
206 998706387LL, 1027970718LL, 1058092559LL, 1089097037LL,
207 1121010017LL, 1153858117LL, 1187668741LL, 1222470092LL,
208 1258291200LL, 1295161946LL, 1333113088LL, 1372176283LL,
209 1412384117LL, 1453770131LL, 1496368847LL, 1540215801LL,
210 1585347569LL, 1631801799LL, 1679617241LL, 1728833783LL,
211 1779492480LL, 1831635590LL, 1885306609LL, 1940550310LL,
212 1997412774LL, 2055941436LL, 2116185118LL, 2178194075LL,
213 2242020034LL, 2307716235LL, 2375337483LL, 2444940184LL,
214 2516582400LL, 2590323893LL, 2666226176LL, 2744352567LL,
215 2824768235LL, 2907540262LL, 2992737695LL, 3080431603LL,
216 3170695139LL, 3263603598LL, 3359234483LL, 3457667567LL,
217 3558984960LL, 3663271180LL, 3770613219LL, 3881100620LL,
218 3994825549LL, 4111882872LL, 4232370237LL, 4356388151LL,
219 4484040068LL, 4615432471LL, 4750674966LL, 4889880368LL,
220 5033164800LL, 5180647786LL, 5332452353LL, 5488705134LL,
221 5649536470LL, 5815080525LL, 5985475391LL, 6160863207LL,
222 6341390279LL, 6527207197LL, 6718468967LL, 6915335135LL,
223 7117969921LL, 7326542360LL, 7541226438LL, 7762201240LL,
224 7989651098LL, 8223765745LL, 8464740475LL, 8712776303LL,
225 8968080137LL, 9230864943LL, 9501349932LL, 9779760736LL,
226 10066329600LL, 10361295572LL, 10664904706LL, 10977410268LL,
227 11299072941LL, 11630161050LL, 11970950782LL,
228 12321726414LL, 12682780558LL, 13054414395LL,
229 13436937935LL, 13830670270LL, 14235939843LL,
230 14653084721LL, 15082452877LL, 15524402481LL,
231 15979302196LL, 16447531490LL, 16929480950LL,
232 17425552607LL, 17936160274LL, 18461729886LL,
233 19002699864LL, 19559521473LL,
234 20132659200LL, 20722591144LL, 21329809413LL, 21954820536LL,
235 22598145883LL, 23260322101LL, 23941901564LL,
236 24643452829LL, 25365561116LL, 26108828790LL,
237 26873875870LL, 27661340540LL, 28471879687LL,
238 29306169443LL, 30164905755LL, 31048804962LL,
239 31958604392LL, 32895062981LL, 33858961901LL,
240 34851105215LL, 35872320548LL, 36923459773LL,
241 38005399729LL, 39119042946LL,
242 40265318400LL, 41445182288LL,42659618827LL,43909641073LL,
243 45196291767LL, 46520644203LL, 47883803129LL,
244 49286905659LL, 50731122232LL, 52217657581LL,
245 53747751741LL, 55322681081LL, 56943759374LL,
246 58612338887LL, 60329811511LL, 62097609924LL,
247 63917208785LL, 65790125963LL, 67717923803LL,
248 69702210431LL, 71744641096LL, 73846919547LL,
249 76010799459LL, 78238085892LL,
250 80530636800LL, 82890364576LL, 85319237654LL, 87819282146LL,
251 90392583534LL, 93041288407LL, 95767606258LL,
252 98573811319LL, 101462244465LL, 104435315163LL,
253 107495503482LL, 110645362163LL, 113887518749LL,
254 117224677774LL, 120659623022LL, 124195219849LL,
255 127834417571LL, 131580251926LL, 135435847606LL,
256 139404420863LL, 143489282192LL, 147693839094LL,
257 152021598918LL, 156476171785LL,
258 161061273600LL, 165780729153LL, 170638475309LL, 175638564293LL,
259 180785167068LL, 186082576815LL, 191535212516LL,
260 197147622639LL, 202924488931LL, 208870630326LL,
261 214991006964LL, 221290724326LL, 227775037498LL,
262 234449355548LL, 241319246045LL, 248390439699LL,
263 255668835143LL, 263160503853LL, 270871695212LL,
264 278808841726LL, 286978564384LL, 295387678189LL,
265 304043197837LL, 312952343570LL,
266 322122547200LL, 331561458306LL, 341276950619LL, 351277128587LL,
267 361570334136LL, 372165153630LL, 383070425033LL,
268 394295245278LL, 405848977863LL, 417741260653LL,
269 429982013929LL, 442581448653LL, 455550074996LL,
270 468898711096LL, 482638492091LL, 496780879399LL,
271 511337670287LL, 526321007707LL, 541743390425LL,
272 557617683452LL, 573957128769LL, 590775356379LL,
273 608086395675LL, 625904687141LL,
274 644245094400LL, 663122916612LL, 682553901238LL, 702554257174LL,
275 723140668273LL, 744330307261LL, 766140850066LL,
276 788590490556LL, 811697955726LL, 835482521307LL,
277 859964027858LL, 885162897307LL, 911100149992LL,
278 937797422193LL, 965276984182LL, 993561758799LL,
279 1022675340575LL, 1052642015414LL, 1083486780851LL,
280 1115235366904LL, 1147914257538LL, 1181550712759LL,
281 1216172791350LL, 1251809374282LL,
282 1288490188800LL, 1326245833225LL, 1365107802477LL,
283 1405108514349LL, 1446281336546LL, 1488660614523LL,
284 1532281700132LL, 1577180981113LL, 1623395911452LL,
285 1670965042615LL, 1719928055717LL, 1770325794615LL,
286 1822200299985LL, 1875594844387LL, 1930553968365LL,
287 1987123517599LL, 2045350681151LL, 2105284030829LL,
288 2166973561703LL, 2230470733808LL, 2295828515076LL,
289 2363101425518LL, 2432345582701LL, 2503618748564LL,
290 2576980377600LL, 2652491666450LL, 2730215604955LL,
291 2810217028699LL, 2892562673093LL, 2977321229047LL,
292 3064563400264LL, 3154361962227LL, 3246791822904LL,
293 3341930085231LL, 3439856111434LL, 3540651589231LL,
294 3644400599971LL, 3751189688775LL, 3861107936730LL,
295 3974247035198LL, 4090701362303LL, 4210568061659LL,
296 4333947123406LL, 4460941467616LL, 4591657030153LL,
297 4726202851036LL, 4864691165402LL, 5007237497128LL,
298 5153960755200LL, 5304983332900LL, 5460431209910LL,
299 5620434057398LL, 5785125346187LL, 5954642458094LL,
300 6129126800528LL, 6308723924455LL, 6493583645808LL,
301 6683860170462LL, 6879712222869LL, 7081303178462LL,
302 7288801199942LL, 7502379377550LL, 7722215873460LL,
303 7948494070397LL, 8181402724607LL, 8421136123319LL,
304 8667894246812LL, 8921882935232LL, 9183314060306LL,
305 9452405702073LL, 9729382330805LL, 10014474994257LL,
306 10307921510400LL, 10609966665800LL, 10920862419821LL,
307 11240868114797LL, 11570250692375LL, 11909284916188LL,
308 12258253601057LL, 12617447848911LL, 12987167291617LL,
309 13367720340924LL, 13759424445738LL, 14162606356925LL,
310 14577602399885LL, 15004758755101LL, 15444431746921LL,
311 15896988140794LL, 16362805449214LL, 16842272246638LL,
312 17335788493624LL, 17843765870465LL, 18366628120613LL,
313 18904811404146LL, 19458764661611LL, 20028949988515LL,
314 20615843020800LL, 21219933331600LL, 21841724839642LL,
315 22481736229595LL, 23140501384751LL, 23818569832376LL,
316 24516507202114LL, 25234895697822LL, 25974334583234LL,
317 26735440681849LL, 27518848891476LL, 28325212713851LL,
318 29155204799770LL, 30009517510202LL, 30888863493843LL,
319 31793976281588LL, 32725610898429LL, 33684544493277LL,
320 34671576987248LL, 35687531740931LL, 36733256241226LL,
321 37809622808292LL, 38917529323222LL, 40057899977031LL,
322 41231686041600LL, 42439866663201LL, 43683449679284LL,
323 44963472459190LL, 46281002769503LL, 47637139664753LL,
324 49033014404229LL, 50469791395645LL, 51948669166468LL,
325 53470881363698LL, 55037697782953LL, 56650425427703LL,
326 58310409599540LL, 60019035020404LL, 61777726987686LL,
327 63587952563177LL, 65451221796858LL, 67369088986555LL,
328 69343153974496LL, 71375063481862LL, 73466512482452LL,
329 75619245616585LL, 77835058646445LL, 80115799954063LL,
330 82463372083200LL, 84879733326402LL, 87366899358568LL,
331 89926944918381LL, 92562005539006LL, 95274279329506LL,
332 98066028808458LL, 100939582791291LL, 103897338332936LL,
333 106941762727397LL, 110075395565907LL, 113300850855406LL,
334 116620819199080LL, 120038070040809LL, 123555453975373LL,
335 127175905126354LL, 130902443593716LL, 134738177973111LL,
336 138686307948992LL, 142750126963725LL, 146933024964905LL,
337 151238491233171LL, 155670117292890LL, 160231599908126LL,
338 164926744166400LL, 169759466652804LL, 174733798717137LL,
339 179853889836763LL, 185124011078013LL, 190548558659013LL,
340 196132057616916LL, 201879165582582LL, 207794676665873LL,
341 213883525454795LL, 220150791131815LL, 226601701710812LL,
342 233241638398160LL, 240076140081619LL, 247110907950747LL,
343 254351810252708LL, 261804887187433LL, 269476355946222LL,
344 277372615897984LL, 285500253927451LL, 293866049929810LL,
345 302476982466342LL, 311340234585780LL, 320463199816253LL,
346 329853488332800LL, 339518933305609LL, 349467597434275LL,
347 359707779673527LL, 370248022156026LL, 381097117318027LL,
348 392264115233832LL, 403758331165164LL, 415589353331747LL,
349 427767050909590LL, 440301582263631LL, 453203403421624LL,
350 466483276796321LL, 480152280163238LL, 494221815901494LL,
351 508703620505416LL, 523609774374866LL, 538952711892444LL,
352 554745231795968LL, 571000507854903LL, 587732099859621LL,
353 604953964932684LL, 622680469171561LL, 640926399632506LL,
354 659706976665600LL, 679037866611218LL, 698935194868551LL,
355 719415559347055LL, 740496044312053LL, 762194234636054LL,
356 784528230467665LL, 807516662330329LL, 831178706663495LL,
357 855534101819180LL, 880603164527263LL, 906406806843249LL,
358 932966553592642LL, 960304560326477LL, 988443631802988LL,
359 1017407241010832LL, 1047219548749733LL,
360 1077905423784889LL, 1109490463591937LL,
361 1142001015709806LL, 1175464199719243LL,
362 1209907929865369LL, 1245360938343122LL,
363 1281852799265013LL,
364 1319413953331200LL, 1358075733222436LL, 1397870389737103LL,
365 1438831118694110LL, 1480992088624106LL,
366 1524388469272109LL, 1569056460935331LL,
367 1615033324660658LL, 1662357413326990LL,
368 1711068203638361LL, 1761206329054527LL,
369 1812813613686499LL, 1865933107185284LL,
370 1920609120652954LL, 1976887263605976LL};
373 __u8 __attribute__((const)) enc_log_64_11(__u32 value)
375 int i;
376 BUG_ON(log_64_11_table[255] != 571789581);
377 for (i=1;i<256;i++) {
378 if (log_64_11_table[i] > value)
379 break;
382 return (__u8)(i-1); /* round down */
385 __u32 __attribute__((const)) dec_log_64_11(__u8 value)
387 BUG_ON(log_64_11_table[255] != 571789581);
388 return log_64_11_table[value];
391 static void check_log_64_11_table(void)
393 int i;
394 BUG_ON(log_64_11_table[0] != 0);
395 for (i=1;i<256;i++) {
396 BUG_ON(log_64_11_table[i] <= log_64_11_table[i-1]);
400 __u16 __attribute__((const)) enc_log_300_24(__u64 value)
402 int i;
403 BUG_ON(log_300_24_table[1023] != 1976887263605976LL);
404 for (i=1;i<1024;i++) {
405 if (log_300_24_table[i] > value)
406 break;
409 return (__u16)(i-1); /* round down */
412 __u64 __attribute__((const)) dec_log_300_24(__u16 value)
414 BUG_ON(value >= 1024);
415 BUG_ON(log_300_24_table[1023] != 1976887263605976LL);
416 return log_300_24_table[value];
419 static void check_log_300_24_table(void)
421 int i;
422 BUG_ON(log_300_24_table[0] != 0);
423 for (i=1;i<1024;i++) {
424 BUG_ON(log_300_24_table[i] <= log_300_24_table[i-1]);
428 static inline __u64 mul_saturated(__u64 a, __u64 b)
430 __u64 res = a*b;
431 if (unlikely(res / a != b))
432 return -1;
433 return res;
436 static inline int numdigits(__u64 value)
438 int digits = 0;
439 for (;value != 0;value = (value >> 1)) {
440 digits++;
442 return digits;
445 static void mul(__u64 a, __u64 b, __u64 *reshigh, __u64 *reslow)
447 __u32 al = a;
448 __u32 ah = (a >> 32);
449 __u32 bl = b;
450 __u32 bh = (b >> 32);
452 __u64 r1 = ((__u64) al) * bl;
453 __u64 r20 = (r1 >> 32);
454 __u64 r21 = ((__u64) ah) * bl;
455 __u64 r22 = ((__u64) al) * bh;
456 __u64 r2 = r20 + ((r21 << 32) >> 32) + ((r22 << 32) >> 32);
457 __u64 r30 = (r21 >> 32) + (r22 >> 32) + (r2 >> 32);
458 __u64 r31 = ((__u64) ah) * bh;
460 BUG_ON(reshigh == 0);
461 BUG_ON(reslow == 0);
463 (*reslow) = ((r1 << 32) >> 32) + (r2 << 32);
464 (*reshigh) = r30 + r31;
467 /* calculate (a*b+rem)/c */
468 __u64 multiply_div2(__u64 a, __u64 b, __s64 rem, __u64 c, __u64 *remainder)
470 __u64 res = 0;
471 __u64 reshigh = 0;
472 __u64 reslow = 0;
473 int u;
475 __u64 high = 0;
476 __u64 low = 0;
478 mul(a, b, &high, &low);
480 if (rem < 0) {
481 __u64 rem0 = (rem == S64_MIN) ? (((__u64) S64_MAX) + 1) :
482 (0 - rem);
483 if (low - rem0 > low)
484 high--;
485 low -= rem0;
486 } else {
487 if (low + ((__u64) rem) < low)
488 high++;
489 low += ((__u64) rem);
493 for(u=63;u>=0;u--) {
494 __u64 tmphigh;
495 __u64 tmplow;
497 __u64 tmpres = res + (1LL << u);
499 mul(c, tmpres, &tmphigh, &tmplow);
501 if (tmphigh > high)
502 continue;
503 if (tmphigh < high)
504 goto apply;
506 if (tmplow > low)
507 continue;
508 if (tmplow < low)
509 goto apply;
511 if (remainder != 0)
512 (*remainder) = 0;
513 return tmpres;
514 apply:
515 res = tmpres;
516 reshigh = tmphigh;
517 reslow = tmplow;
520 if (remainder != 0) {
521 (*remainder) = low - reslow;
522 if ((*remainder) > c)
523 (*remainder) = 0;
527 return res;
530 static inline int hdr_size(void)
532 return ((sizeof(struct cell_hdr) + sizeof(void *) - 1) / sizeof(void *)
533 ) * sizeof(void *);
536 static inline int elements_per_cell(int cell_size)
538 return (cell_size - hdr_size())/sizeof(void *);
541 static inline struct cell_hdr *cell_addr(struct htable *ht, __u32 id)
543 int idx = (id%ht->htable_size) / (elements_per_cell(ht->cell_size));
544 return (struct cell_hdr *) (((char *)ht->htable) + ht->cell_size * idx);
547 static inline char **element_addr(struct htable *ht, __u32 id)
549 int idx = (id%ht->htable_size) % (elements_per_cell(ht->cell_size));
550 return (char **)
551 ( ((char *)cell_addr(ht, id)) +
552 hdr_size() + idx*sizeof(void *));
556 static inline char **next_element(struct htable *ht, char *element)
558 return (char **)(element + ht->entry_offset);
561 static inline struct kref *element_kref(struct htable *ht, char *element)
563 return (struct kref *)(element + ht->kref_offset);
567 static inline void unlock_element(struct htable *ht, __u32 key)
569 struct cell_hdr *hdr = cell_addr(ht, key);
570 spin_unlock(&(hdr->lock));
574 static char **get_element_nounlock(struct htable *ht, __u32 key,
575 void *searcheditem)
577 struct cell_hdr *hdr = cell_addr(ht, key);
578 char **element = element_addr(ht, key);
580 BUG_ON(0 == element);
582 spin_lock(&(hdr->lock));
584 while (1) {
585 if (*element == 0)
586 break;
587 if (searcheditem != 0 && ht->matches(*element, searcheditem))
588 break;
589 element = next_element(ht, *element);
592 return element;
595 char *htable_get(struct htable *ht, __u32 key, void *searcheditem)
597 unsigned long iflags;
598 char *element;
600 if (unlikely(ht->htable == 0))
601 return 0;
603 local_irq_save(iflags);
604 element = *(get_element_nounlock(ht, key, searcheditem));
605 if (likely(element != 0))
606 kref_get(element_kref(ht, element));
607 unlock_element(ht, key);
608 local_irq_restore(iflags);
610 return element;
613 int htable_delete(struct htable *ht, __u32 key,
614 void *searcheditem, void (*free) (struct kref *ref))
616 unsigned long iflags;
617 char **element;
618 char **next;
619 int rc = 0;
621 if (unlikely(ht->htable == 0))
622 return 1;
624 local_irq_save(iflags);
626 element = get_element_nounlock(ht, key, searcheditem);
627 BUG_ON(0 == element);
629 if (unlikely(*element == 0)) {
630 /* key not in table */
631 rc = 1;
632 goto out;
635 next = next_element(ht, *element);
636 kref_put(element_kref(ht, *element), free);
637 *element = *next;
639 out:
640 unlock_element(ht, key);
641 local_irq_restore(iflags);
643 return rc;
646 void htable_insert(struct htable *ht, char *newelement, __u32 key)
648 unsigned long iflags;
649 char **element;
651 if (unlikely(ht->htable == 0))
652 return;
654 BUG_ON(*next_element(ht, newelement) != 0);
655 local_irq_save(iflags);
657 element = get_element_nounlock(ht, key, 0);
659 BUG_ON(element == 0);
660 BUG_ON(*element != 0);
662 *element = newelement;
663 kref_get(element_kref(ht, newelement));
665 unlock_element(ht, key);
666 local_irq_restore(iflags);
670 void htable_init(struct htable *ht, int (*matches)(void *htentry,
671 void *searcheditem), __u32 entry_offset, __u32 kref_offset)
673 int num_cells;
674 int j;
676 BUG_ON(0 == ht);
678 ht->htable = kmalloc(PAGE_SIZE, GFP_KERNEL);
679 if (unlikely(ht->htable == 0)) {
680 printk(KERN_CRIT "cor: error allocating htable (out of "
681 "memory?)");
682 return;
684 memset(ht->htable, 0, PAGE_SIZE);
685 ht->cell_size = 256;
687 num_cells = PAGE_SIZE/ht->cell_size;
689 for (j=0;j<num_cells;j++) {
690 struct cell_hdr *hdr = (struct cell_hdr *)
691 ( ((char *) ht->htable) + j * ht->cell_size);
692 spin_lock_init(&(hdr->lock));
695 ht->htable_size = num_cells * elements_per_cell(ht->cell_size);
696 ht->num_elements = 0;
698 ht->matches = matches;
699 ht->entry_offset = entry_offset;
700 ht->kref_offset = kref_offset;
703 struct reverse_connid_matchparam {
704 struct neighbor *nb;
705 __u32 conn_id;
708 static __u32 rcm_to_key(struct reverse_connid_matchparam *rcm)
710 return (((__u32) rcm->nb) ^ rcm->conn_id);
713 static int matches_reverse_connid(void *htentry, void *searcheditem)
715 struct conn *conn = (struct conn *) htentry;
716 struct reverse_connid_matchparam *rcm =
717 (struct reverse_connid_matchparam *) searcheditem;
718 BUG_ON(conn->targettype != TARGET_OUT);
719 return (conn->target.out.nb == rcm->nb) &&
720 (conn->target.out.conn_id == rcm->conn_id);
723 struct conn *get_conn_reverse(struct neighbor *nb, __u32 conn_id)
725 struct reverse_connid_matchparam rcm;
726 rcm.nb = nb;
727 rcm.conn_id = conn_id;
729 return (struct conn *) htable_get(&reverse_connid_table,
730 rcm_to_key(&rcm), &rcm);
733 void insert_reverse_connid(struct conn *trgt_out_ll)
735 struct reverse_connid_matchparam rcm;
737 BUG_ON(trgt_out_ll->targettype != TARGET_OUT);
739 rcm.nb = trgt_out_ll->target.out.nb;
740 rcm.conn_id = trgt_out_ll->target.out.conn_id;
741 htable_insert(&reverse_connid_table, (char *) trgt_out_ll,
742 rcm_to_key(&rcm));
745 static int matches_connid_in(void *htentry, void *searcheditem)
747 struct conn *conn = (struct conn *) htentry;
748 __u32 conn_id = *((__u32 *) searcheditem);
749 BUG_ON(conn->sourcetype != SOURCE_IN);
750 return (conn->source.in.conn_id == conn_id);
753 struct conn *get_conn(__u32 conn_id)
755 return (struct conn *) htable_get(&connid_table, conn_id, &conn_id);
758 static void free_connid_reuse(struct kref *ref)
760 struct connid_reuse_item *cir = container_of(ref,
761 struct connid_reuse_item, ref);
763 kmem_cache_free(connid_reuse_slab, cir);
766 static int matches_reuse_connid(void *htentry, void *searcheditem)
768 struct connid_reuse_item *cir = (struct connid_reuse_item *) htentry;
769 __u32 conn_id = *((__u32 *) searcheditem);
770 return (cir->conn_id == conn_id);
773 void connid_used_pingsuccess(struct neighbor *nb)
775 unsigned long iflags;
776 struct connid_reuse_item *cri;
778 spin_lock_irqsave(&(nb->connid_reuse_lock), iflags);
780 nb->connid_reuse_pingcnt++;
781 while (list_empty(&(nb->connid_reuse_list)) == 0) {
782 cri = container_of(nb->connid_reuse_list.next,
783 struct connid_reuse_item, lh);
784 if ((cri->pingcnt + CONNID_REUSE_RTTS -
785 nb->connid_reuse_pingcnt) < 32768)
786 break;
788 if (htable_delete(&connid_reuse_table,
789 cri->conn_id,
790 &(cri->conn_id), free_connid_reuse)) {
791 printk(KERN_ERR "error in connid_used_pingsuccess: "
792 "htable_delete failed");
795 list_del(&(cri->lh));
796 kref_put(&(cri->ref), free_connid_reuse);
799 spin_unlock_irqrestore(&(nb->connid_reuse_lock), iflags);
802 static int connid_used(__u32 conn_id)
804 struct conn *cn;
805 struct connid_reuse_item *cir;
807 cn = get_conn(conn_id);
808 if (unlikely(cn != 0)) {
809 kref_put(&(cn->ref), free_conn);
810 return 1;
813 cir = (struct connid_reuse_item *)
814 htable_get(&connid_reuse_table, conn_id, &conn_id);
816 if (unlikely(cir != 0)) {
817 kref_put(&(cir->ref), free_connid_reuse);
818 return 1;
821 return 0;
824 static int connid_alloc(struct conn *src_in_ll)
826 __u32 conn_id;
827 int i;
829 BUG_ON(src_in_ll->sourcetype != SOURCE_IN);
831 spin_lock_bh(&connid_gen);
832 for(i=0;i<16;i++) {
833 conn_id = 0;
834 get_random_bytes((char *) &conn_id, sizeof(conn_id));
836 if (unlikely(conn_id == 0))
837 continue;
839 if (unlikely(connid_used(conn_id)))
840 continue;
842 goto found;
844 spin_unlock_bh(&connid_gen);
846 return 1;
848 found:
849 src_in_ll->source.in.conn_id = conn_id;
850 htable_insert(&connid_table, (char *) src_in_ll, conn_id);
851 spin_unlock_bh(&connid_gen);
852 return 0;
855 void _set_last_act(struct conn *src_in_l)
857 unsigned long iflags;
858 spin_lock_irqsave(&(src_in_l->source.in.nb->conn_list_lock), iflags);
859 list_del(&(src_in_l->source.in.nb_list));
860 list_add_tail(&(src_in_l->source.in.nb_list),
861 &(src_in_l->source.in.nb->rcv_conn_list));
862 spin_unlock_irqrestore(&(src_in_l->source.in.nb->conn_list_lock),
863 iflags);
866 void free_conn(struct kref *ref)
868 unsigned long iflags;
869 struct conn *cn = container_of(ref, struct conn, ref);
870 struct conn *reversedir = 0;
872 spin_lock_irqsave(&conn_free, iflags);
874 BUG_ON(cn->isreset == 0);
876 if (cn->reversedir != 0)
877 cn->reversedir->isreset = 3;
879 if (cn->isreset != 3)
880 goto out;
882 if (cn->reversedir != 0) {
883 cn->reversedir->reversedir = 0;
884 reversedir = cn->reversedir;
885 cn->reversedir = 0;
888 if (cn->sourcetype == SOURCE_IN) {
889 kref_put(&(cn->source.in.nb->ref), neighbor_free);
890 cn->source.in.nb = 0;
893 if (cn->targettype == TARGET_OUT) {
894 kref_put(&(cn->target.out.nb->ref), neighbor_free);
895 cn->target.out.nb = 0;
898 BUG_ON(cn->data_buf.totalsize != 0);
899 BUG_ON(cn->data_buf.overhead != 0);
901 kmem_cache_free(conn_slab, cn);
903 out:
904 spin_unlock_irqrestore(&conn_free, iflags);
906 if (reversedir != 0)
907 free_conn(&(reversedir->ref));
911 * rc == 0 ==> ok
912 * rc == 1 ==> connid_reuse or connid allocation failed
914 int conn_init_out(struct conn *trgt_unconn_ll, struct neighbor *nb)
916 unsigned long iflags;
917 int rc = 0;
918 struct conn *src_none_ll = trgt_unconn_ll->reversedir;
920 BUG_ON(trgt_unconn_ll->targettype != TARGET_UNCONNECTED);
921 BUG_ON(src_none_ll == 0);
922 BUG_ON(src_none_ll->sourcetype != SOURCE_NONE);
924 memset(&(trgt_unconn_ll->target.out), 0,
925 sizeof(trgt_unconn_ll->target.out));
926 memset(&(src_none_ll->source.in), 0, sizeof(src_none_ll->source.in));
928 trgt_unconn_ll->targettype = TARGET_OUT;
929 src_none_ll->sourcetype = SOURCE_IN;
932 src_none_ll->source.in.cir = kmem_cache_alloc(connid_reuse_slab,
933 GFP_ATOMIC);
934 if (unlikely(src_none_ll->source.in.cir == 0)) {
935 rc = 1;
936 goto out;
938 memset(src_none_ll->source.in.cir, 0, sizeof(struct connid_reuse_item));
941 if (unlikely(connid_alloc(src_none_ll))) {
942 rc = 1;
943 goto out_freecir;
947 trgt_unconn_ll->target.out.nb = nb;
948 src_none_ll->source.in.nb = nb;
950 /* neighbor pointer */
951 kref_get(&(nb->ref));
952 kref_get(&(nb->ref));
954 skb_queue_head_init(&(src_none_ll->source.in.reorder_queue));
956 INIT_LIST_HEAD(&(src_none_ll->source.in.acks_pending));
958 INIT_LIST_HEAD(&(trgt_unconn_ll->target.out.retrans_list));
960 reset_seqno(trgt_unconn_ll, 0);
961 get_random_bytes((char *) &(src_none_ll->source.in.next_seqno),
962 sizeof(src_none_ll->source.in.next_seqno));
964 get_random_bytes((char *) &(trgt_unconn_ll->target.out.decaytime_seqno),
966 trgt_unconn_ll->target.out.decaytime_seqno =
967 trgt_unconn_ll->target.out.decaytime_seqno % 64;
968 trgt_unconn_ll->source.in.decaytime_seqno = 255;
970 src_none_ll->source.in.jiffies_last_act = jiffies;
972 spin_lock_irqsave(&(nb->conn_list_lock), iflags);
973 list_add_tail(&(src_none_ll->source.in.nb_list), &(nb->rcv_conn_list));
974 spin_unlock_irqrestore(&(nb->conn_list_lock), iflags);
976 /* neighbor lists */
977 kref_get(&(src_none_ll->ref));
979 atomic_inc(&num_conns);
982 refresh_conn_credits(trgt_unconn_ll, 0, 1);
984 if (0) {
985 out_freecir:
986 kmem_cache_free(connid_reuse_slab, src_none_ll->source.in.cir);
987 src_none_ll->source.in.cir = 0;
989 out:
990 return rc;
993 void conn_init_sock_source(struct conn *cn)
995 BUG_ON(cn == 0);
996 cn->sourcetype = SOURCE_SOCK;
997 memset(&(cn->source.sock), 0, sizeof(cn->source.sock));
998 init_waitqueue_head(&(cn->source.sock.wait));
1001 void conn_init_sock_target(struct conn *cn)
1003 BUG_ON(cn == 0);
1004 cn->targettype = TARGET_SOCK;
1005 memset(&(cn->target.sock), 0, sizeof(cn->target.sock));
1006 init_waitqueue_head(&(cn->target.sock.wait));
1007 reset_seqno(cn, 0);
1010 #warning todo alloclimit (credits)
1011 struct conn* alloc_conn(gfp_t allocflags)
1013 struct conn *cn1 = 0;
1014 struct conn *cn2 = 0;
1016 cn1 = kmem_cache_alloc(conn_slab, allocflags);
1017 if (unlikely(cn1 == 0))
1018 goto out_err0;
1020 cn2 = kmem_cache_alloc(conn_slab, allocflags);
1021 if (unlikely(cn2 == 0))
1022 goto out_err1;
1024 memset(cn1, 0, sizeof(struct conn));
1025 memset(cn2, 0, sizeof(struct conn));
1027 cn1->reversedir = cn2;
1028 cn2->reversedir = cn1;
1030 kref_init(&(cn1->ref));
1031 kref_init(&(cn2->ref));
1033 cn1->sourcetype = SOURCE_NONE;
1034 cn2->sourcetype = SOURCE_NONE;
1035 cn1->targettype = TARGET_UNCONNECTED;
1036 cn2->targettype = TARGET_UNCONNECTED;
1038 cn1->isreset = 0;
1039 cn2->isreset = 0;
1041 spin_lock_init(&(cn1->rcv_lock));
1042 spin_lock_init(&(cn2->rcv_lock));
1044 cn1->ktime_credit_update = ktime_get();
1045 cn2->ktime_credit_update = cn1->ktime_credit_update;
1047 cn1->crate_forward = ((1 << 31) - (((__u32) 1 << 31) / 10));
1048 cn2->crate_forward = ((1 << 31) - (((__u32) 1 << 31) / 10));
1050 databuf_init(cn1);
1051 databuf_init(cn2);
1053 return cn1;
1055 out_err1:
1056 kmem_cache_free(conn_slab, cn1);
1057 out_err0:
1058 return 0;
1061 static struct cor_sock *get_corsock(__be64 port)
1063 struct list_head *curr = openports.next;
1065 while (curr != &openports) {
1066 struct cor_sock *cs = container_of(curr, struct cor_sock,
1067 data.listener.lh);
1068 BUG_ON(cs->type != SOCKTYPE_LISTENER);
1069 if (cs->data.listener.port == port)
1070 return cs;
1072 curr = curr->next;
1075 return 0;
1078 void close_port(struct cor_sock *cs)
1080 spin_lock_bh(&cor_bindnodes);
1082 list_del(&(cs->data.listener.lh));
1084 while (list_empty(&(cs->data.listener.conn_queue)) == 0) {
1085 struct conn *src_sock_o = container_of(
1086 cs->data.listener.conn_queue.next,
1087 struct conn, source.sock.cl_list);
1088 list_del(&(src_sock_o->source.sock.cl_list));
1089 reset_conn(src_sock_o);
1090 kref_put(&(src_sock_o->ref), free_conn);
1093 spin_unlock_bh(&cor_bindnodes);
1096 int open_port(struct cor_sock *cs_l, __be64 port)
1098 int rc = 0;
1100 spin_lock_bh(&cor_bindnodes);
1101 if (get_corsock(port) != 0) {
1102 rc = -EADDRINUSE;
1103 goto out;
1106 BUG_ON(cs_l->type != SOCKTYPE_UNCONNECTED);
1108 cs_l->type = SOCKTYPE_LISTENER;
1109 cs_l->data.listener.port = port;
1111 /* kref is not actually used */
1112 INIT_LIST_HEAD(&(cs_l->data.listener.conn_queue));
1113 init_waitqueue_head(&(cs_l->data.listener.wait));
1115 list_add_tail((struct list_head *) &(cs_l->data.listener.lh),
1116 &openports);
1118 out:
1119 spin_unlock_bh(&cor_bindnodes);
1121 return rc;
1125 * rc == 0 connected
1126 * rc == 2 port not open
1127 * rc == 3 listener queue full
1129 int connect_port(struct conn *trtg_unconn_l, __be64 port)
1131 struct cor_sock *cs;
1132 int rc = 0;
1134 spin_lock_bh(&cor_bindnodes);
1136 cs = get_corsock(port);
1137 if (cs == 0) {
1138 rc = 2;
1139 goto out;
1142 spin_lock_bh(&(cs->lock));
1144 if (unlikely(cs->data.listener.queue_len >=
1145 cs->data.listener.queue_maxlen)) {
1146 if (cs->data.listener.queue_maxlen <= 0)
1147 rc = 2;
1148 else
1149 rc = 3;
1151 goto out2;
1154 kref_get(&(trtg_unconn_l->reversedir->ref));
1156 BUG_ON(trtg_unconn_l->is_client != 1);
1157 spin_lock_bh(&(trtg_unconn_l->reversedir->rcv_lock));
1158 conn_init_sock_target(trtg_unconn_l);
1159 conn_init_sock_source(trtg_unconn_l->reversedir);
1160 spin_unlock_bh(&(trtg_unconn_l->reversedir->rcv_lock));
1162 list_add_tail(&(trtg_unconn_l->reversedir->source.sock.cl_list),
1163 &(cs->data.listener.conn_queue));
1164 cs->data.listener.queue_len++;
1165 wake_up_interruptible(&(cs->data.listener.wait));
1167 out2:
1168 spin_unlock_bh(&(cs->lock));
1170 out:
1171 spin_unlock_bh(&cor_bindnodes);
1172 return rc;
1176 * rc == 0 connected
1177 * rc == 2 addrtype not found
1178 * rc == 3 addr not found
1179 * rc == 4 ==> connid allocation failed
1180 * rc == 4 ==> control msg alloc failed
1182 int connect_neigh(struct conn *trtg_unconn_l,
1183 __u16 addrtypelen, __u8 *addrtype,
1184 __u16 addrlen, __u8 *addr)
1186 int rc = 0;
1187 int ciorc;
1188 struct control_msg_out *cm;
1189 struct neighbor *nb = 0;
1191 if (addrtype_known(addrtypelen, addrtype) == 0) {
1192 rc = 2;
1193 goto discard;
1196 nb = find_neigh(addrtypelen, addrtype, addrlen, addr);
1198 if (nb == 0) {
1199 rc = 3;
1200 goto discard;
1203 cm = alloc_control_msg(nb, ACM_PRIORITY_HIGH);
1204 if (unlikely(cm == 0)) {
1205 rc = 4;
1206 goto discard;
1209 spin_lock_bh(&(trtg_unconn_l->reversedir->rcv_lock));
1210 ciorc = conn_init_out(trtg_unconn_l, nb);
1211 spin_unlock_bh(&(trtg_unconn_l->reversedir->rcv_lock));
1212 if (unlikely(ciorc)) {
1213 rc = 4;
1214 goto freecm;
1217 send_connect_nb(cm, trtg_unconn_l->reversedir->source.in.conn_id,
1218 trtg_unconn_l->reversedir->source.in.next_seqno,
1219 trtg_unconn_l->reversedir);
1221 if (0) {
1222 freecm:
1223 free_control_msg(cm);
1224 discard:
1225 trtg_unconn_l->targettype = TARGET_DISCARD;
1228 if (nb != 0)
1229 kref_put(&(nb->ref), neighbor_free);
1231 return rc;
1234 static int _reset_conn(struct conn *cn, int trgt_out_resetneeded)
1237 * active conns have an additional ref to make sure that they are not
1238 * freed when only one direction is referenced by the connid hashtable
1240 int krefput = 1;
1242 /* lock sourcetype/targettype */
1243 spin_lock_bh(&(cn->rcv_lock));
1245 if (cn->sourcetype == SOURCE_IN) {
1246 unsigned long iflags;
1248 spin_lock_irqsave(&(cn->source.in.nb->conn_list_lock), iflags);
1249 list_del(&(cn->source.in.nb_list));
1250 spin_unlock_irqrestore(&(cn->source.in.nb->conn_list_lock),
1251 iflags);
1253 set_busy_till(cn->source.in.nb, 0);
1255 krefput++;
1257 if (cn->source.in.conn_id != 0) {
1258 BUG_ON(cn->source.in.cir == 0);
1260 kref_init(&(cn->source.in.cir->ref));
1261 cn->source.in.cir->conn_id = cn->source.in.conn_id;
1262 cn->source.in.cir->pingcnt =
1263 cn->source.in.nb->connid_reuse_pingcnt;
1265 spin_lock_irqsave(
1266 &(cn->source.in.nb->connid_reuse_lock),
1267 iflags);
1268 htable_insert(&connid_reuse_table,
1269 (char *) cn->source.in.cir,
1270 cn->source.in.cir->conn_id);
1271 list_add_tail(&(cn->source.in.cir->lh),
1272 &(cn->source.in.nb->connid_reuse_list));
1273 spin_unlock_irqrestore(
1274 &(cn->source.in.nb->connid_reuse_lock),
1275 iflags);
1277 cn->source.in.cir = 0;
1279 if (htable_delete(&connid_table,
1280 cn->source.in.conn_id,
1281 &(cn->source.in.conn_id), free_conn)) {
1282 printk(KERN_ERR "error in _reset_conn: "
1283 "htable_delete src_in failed");
1286 cn->source.in.conn_id = 0;
1288 free_ack_conns(cn);
1291 atomic_dec(&num_conns);
1292 BUG_ON(atomic_read(&num_conns) < 0);
1294 reset_ooo_queue(cn);
1295 } else if (cn->sourcetype == SOURCE_SOCK) {
1296 wake_up_interruptible(&(cn->source.sock.wait));
1299 if (cn->targettype == TARGET_UNCONNECTED) {
1300 connreset_cpacket_buffer(cn);
1301 } else if (cn->targettype == TARGET_OUT) {
1302 if (trgt_out_resetneeded && cn->target.out.conn_id != 0) {
1303 send_reset_conn(cn->target.out.nb,
1304 cn->target.out.conn_id,
1305 cn->reversedir->source.in.conn_id, 0);
1308 if (cn->target.out.conn_id != 0) {
1309 struct reverse_connid_matchparam rcm;
1310 rcm.nb = cn->target.out.nb;
1311 rcm.conn_id = cn->target.out.conn_id;
1312 if (htable_delete(&reverse_connid_table,
1313 rcm_to_key(&rcm),
1314 &rcm, free_conn)){
1315 printk(KERN_ERR "error in _reset_conn: "
1316 "htable_delete target_out "
1317 "failed");
1321 cn->target.out.conn_id = 0;
1323 cancel_retrans(cn);
1325 qos_remove_conn(cn);
1326 } else if (cn->targettype == TARGET_SOCK) {
1327 wake_up_interruptible(&(cn->target.sock.wait));
1330 databuf_ackdiscard(cn);
1332 spin_unlock_bh(&(cn->rcv_lock));
1334 reset_bufferusage(cn); /* source in only */
1335 connreset_credits(cn);
1336 connreset_sbt(cn); /* source sock only */
1338 return krefput;
1341 /* warning: do not hold the rcv_lock while calling this! */
1342 void reset_conn(struct conn *cn)
1344 int put1;
1345 int put2;
1347 int isreset1;
1348 int isreset2;
1350 if (cn->is_client) {
1351 spin_lock_bh(&(cn->rcv_lock));
1352 spin_lock_bh(&(cn->reversedir->rcv_lock));
1353 } else {
1354 spin_lock_bh(&(cn->reversedir->rcv_lock));
1355 spin_lock_bh(&(cn->rcv_lock));
1358 BUG_ON(cn->isreset <= 1 && cn->reversedir->isreset >= 2);
1359 BUG_ON(cn->isreset >= 2 && cn->reversedir->isreset <= 1);
1361 isreset1 = cn->isreset;
1362 if (cn->isreset <= 1)
1363 cn->isreset = 2;
1365 isreset2 = cn->reversedir->isreset;
1366 if (cn->reversedir->isreset <= 1)
1367 cn->reversedir->isreset = 2;
1369 if (cn->is_client) {
1370 spin_unlock_bh(&(cn->rcv_lock));
1371 spin_unlock_bh(&(cn->reversedir->rcv_lock));
1372 } else {
1373 spin_unlock_bh(&(cn->reversedir->rcv_lock));
1374 spin_unlock_bh(&(cn->rcv_lock));
1377 if (isreset1 >= 2)
1378 return;
1380 put1 = _reset_conn(cn, isreset1 == 0);
1381 put2 = _reset_conn(cn->reversedir, isreset2 == 0);
1383 /* free_conn may not be called, before both _reset_conn have finished */
1384 while (put1 > 0) {
1385 kref_put(&(cn->ref), free_conn);
1386 put1--;
1389 while (put2 > 0) {
1390 kref_put(&(cn->reversedir->ref), free_conn);
1391 put2--;
1395 static int __init cor_common_init(void)
1397 int rc;
1399 struct conn c;
1401 check_log_64_11_table();
1402 check_log_300_24_table();
1404 printk(KERN_ERR "sizeof conn: %d", sizeof(c));
1405 printk(KERN_ERR " conn.source: %d", sizeof(c.source));
1406 printk(KERN_ERR " conn.target: %d", sizeof(c.target));
1407 printk(KERN_ERR " conn.target.out: %d", sizeof(c.target.out));
1408 printk(KERN_ERR " conn.buf: %d", sizeof(c.data_buf));
1410 printk(KERN_ERR " mutex: %d", sizeof(struct mutex));
1411 printk(KERN_ERR " spinlock: %d", sizeof(spinlock_t));
1412 printk(KERN_ERR " kref: %d", sizeof(struct kref));
1414 conn_slab = kmem_cache_create("cor_conn", sizeof(struct conn), 8, 0, 0);
1415 connid_reuse_slab = kmem_cache_create("cor_connid_reuse",
1416 sizeof(struct connid_reuse_item), 8, 0, 0);
1418 htable_init(&connid_table, matches_connid_in,
1419 offsetof(struct conn, source.in.htab_entry),
1420 offsetof(struct conn, ref));
1422 htable_init(&reverse_connid_table, matches_reverse_connid,
1423 offsetof(struct conn, target.out.htab_entry),
1424 offsetof(struct conn, ref));
1426 htable_init(&connid_reuse_table, matches_reuse_connid,
1427 offsetof(struct connid_reuse_item, htab_entry),
1428 offsetof(struct connid_reuse_item, ref));
1430 atomic_set(&num_conns, 0);
1432 credits_init();
1433 forward_init();
1434 cor_kgen_init();
1435 cor_cpacket_init();
1437 rc = cor_snd_init();
1438 if (unlikely(rc != 0))
1439 return rc;
1441 rc = cor_neighbor_init();
1442 if (unlikely(rc != 0))
1443 return rc;
1445 rc = cor_rcv_init();
1446 if (unlikely(rc != 0))
1447 return rc;
1449 return 0;
1452 module_init(cor_common_init);
1453 MODULE_LICENSE("GPL");