source: EcnlProtoTool/trunk/asp3_dcre/tinet/netinet/tcp_timer.c@ 321

Last change on this file since 321 was 321, checked in by coas-nagasima, 7 years ago

文字コードを設定

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 14.8 KB
Line 
1/*
2 * TINET (TCP/IP Protocol Stack)
3 *
4 * Copyright (C) 2001-2009 by Dep. of Computer Science and Engineering
5 * Tomakomai National College of Technology, JAPAN
6 *
7 * 上記著作権者は,以下の (1)~(4) の条件か,Free Software Foundation
8 * によって公表されている GNU General Public License の Version 2 に記
9 * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
10 * を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
11 * 利用と呼ぶ)することを無償で許諾する.
12 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
13 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
14 * スコード中に含まれていること.
15 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
16 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
17 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
18 * の無保証規定を掲載すること.
19 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
20 * 用できない形で再配布する場合には,次の条件を満たすこと.
21 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
22 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
23 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
24 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
25 *
26 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
27 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
28 * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
29 * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
30 *
31 * @(#) $Id$
32 */
33
34/*
35 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
36 * The Regents of the University of California. All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95
67 * $FreeBSD: src/sys/netinet/tcp_timer.c,v 1.28.2.1 1999/08/29 16:29:56 peter Exp $
68 */
69
70#ifdef TARGET_KERNEL_ASP
71
72#include <kernel.h>
73#include <sil.h>
74#include "kernel_cfg.h"
75
76#endif /* of #ifdef TARGET_KERNEL_ASP */
77
78#ifdef TARGET_KERNEL_JSP
79
80#include <s_services.h>
81#include <t_services.h>
82#include "kernel_id.h"
83
84#endif /* of #ifdef TARGET_KERNEL_JSP */
85
86#include <tinet_defs.h>
87#include <tinet_config.h>
88
89#include <net/if.h>
90#include <net/if_ppp.h>
91#include <net/if_loop.h>
92#include <net/ethernet.h>
93#include <net/net.h>
94#include <net/net_var.h>
95#include <net/net_buf.h>
96#include <net/net_timer.h>
97
98#include <netinet/in.h>
99#include <netinet/in_var.h>
100#include <netinet6/in6.h>
101#include <netinet6/in6_var.h>
102#include <netinet/ip.h>
103#include <netinet/ip6.h>
104#include <netinet/tcp.h>
105#include <netinet/tcp_timer.h>
106#include <netinet/tcp_var.h>
107#include <netinet/tcp_fsm.h>
108#include <netinet/tcp_seq.h>
109
110#ifdef SUPPORT_TCP
111
112/*
113 * 局所関数
114 */
115
116static T_TCP_CEP *tcp_timers (T_TCP_CEP *cep, int_t tix);
117
118#if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0
119
120static void tcp_tw_timo (void);
121
122#endif /* of #if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0 */
123
124/*
125 * バックオフ時間
126 *
127 * 再送を行うたびに、タイムアウトの時間を延長する。
128 */
129
130const static uint8_t tcp_back_off[] = {
131 UINT_C(1), UINT_C(2), UINT_C(4), UINT_C(8),
132 UINT_C(16), UINT_C(32), UINT_C(64), UINT_C(64),
133 UINT_C(64), UINT_C(64), UINT_C(64), UINT_C(64),
134 UINT_C(64)
135 };
136
137#define TCP_TOTAL_BACK_OFF 511 /* バックオフ時間の合計 */
138
139/*
140 * tcp_timers -- タイムアウト処理
141 */
142
143static T_TCP_CEP *
144tcp_timers (T_TCP_CEP *cep, int_t tix)
145{
146 uint16_t win;
147
148
149 switch (tix) {
150
151 /*
152 * 再送タイマ
153 */
154 case TCP_TIM_REXMT:
155
156 /*
157 * 最大再送回数 (TCP_MAX_REXMT_SHIFT、標準 12 回) になったときは、
158 * コネクションを切断する。
159 */
160 if (++ cep->rxtshift > TCP_MAX_REXMT_SHIFT) {
161 cep->rxtshift = TCP_MAX_REXMT_SHIFT;
162 cep->net_error = EV_REXMTMO;
163 cep = tcp_drop(cep, E_CLS);
164 break;
165 }
166
167 /*
168 * 再送タイムアウトを計算する。
169 */
170 cep->rxtcur = tcp_range_set((T_TCP_TIME)(tcp_rexmt_val(cep) * tcp_back_off[cep->rxtshift]),
171 (T_TCP_TIME)TCP_TVAL_MIN,
172 (T_TCP_TIME)TCP_TVAL_MAX_REXMT);
173 cep->timer[TCP_TIM_REXMT] = cep->rxtcur;
174
175 /*
176 * srtt: 平滑化された RTT
177 * rttvar: 平滑化された分散
178 *
179 * 再送回数が最大再送回数の 1/4 になったときは、
180 * 平滑化された分散 (rttvar) に srtt を加算し、
181 * 平滑化された RTT を 0 にする。
182 *
183 */
184 if (cep->rxtshift > TCP_MAX_REXMT_SHIFT / 4) {
185 cep->rttvar += (cep->srtt >> TCP_SRTT_SHIFT);
186 cep->srtt = 0;
187 }
188
189 /*
190 * snd_nxt: 次に送信する SEQ、この時点では、前回送信した SEQ
191 * snd_una: 未確認の最小送信 SEQ または、確認された最大送信 SEQ
192 *
193 * 前回送信した SEQ (snd_nxt) を
194 * 確認された最大送信 SEQ (snd_una) まで戻す。
195 */
196 cep->snd_nxt = cep->snd_una;
197 cep->flags |= TCP_CEP_FLG_ACK_NOW;
198
199 /*
200 * rtt: 往復時間の計測を中止する。
201 */
202 cep->rtt = 0;
203
204 /*
205 * 送信ウインドの設定
206 *
207 * snd_wnd: 相手の受信可能ウィンドサイズ
208 * snd_cwnd: 輻輳ウィンドサイズ
209 * maxseg : 相手の最大受信セグメントサイズ
210 *
211 * 相手の受信可能ウィンドサイズ (snd_wnd) か、
212 * 輻輳ウィンドサイズ (snd_cwnd) の
213 * どちらか小さいサイズの 1/2 を、更に
214 * 相手の最大受信セグメントサイズ (maxseg) で割った値。
215 * ただし、2 以上
216 */
217 if (cep->snd_wnd < cep->snd_cwnd)
218 win = cep->snd_wnd / 2 / cep->maxseg;
219 else
220 win = cep->snd_cwnd / 2 / cep->maxseg;
221
222 if (win < 2)
223 win = 2;
224
225 /*
226 * 輻輳ウィンドサイズ (snd_cwnd) は
227 * 相手の受信可能ウィンドサイズ (snd_wnd) に、
228 * 輻輳ウィンドサイズのしきい値 (snd_ssthresh) は
229 * 相手の受信可能ウィンドサイズ (snd_wnd) の win 倍に
230 * 設定する。
231 */
232 cep->snd_cwnd = cep->maxseg;
233 cep->snd_ssthresh = win * cep->maxseg;
234 cep->dupacks = 0;
235
236 /* 出力をポストする。*/
237 cep->flags |= TCP_CEP_FLG_POST_OUTPUT;
238 sig_sem(SEM_TCP_POST_OUTPUT);
239 break;
240
241 /*
242 * 持続タイマ
243 */
244 case TCP_TIM_PERSIST:
245
246 /*
247 * 最大再送回数 (TCP_MAX_REXMT_SHIFT、標準 12 回) を超えていて、
248 * アイドル時間が、保留タイマの標準値 (TCP_TVAL_KEEP_IDLE、
249 * 標準 2 * 60 * 60 秒) 以上か、
250 * 再送タイムアウト値 * バックオフ時間の合計以上なら
251 * コネクションを切断する。
252 */
253 if (cep->rxtshift > TCP_MAX_REXMT_SHIFT &&
254 (cep->idle >= TCP_TVAL_KEEP_IDLE ||
255 cep->idle >= tcp_rexmt_val(cep) * TCP_TOTAL_BACK_OFF)) {
256 cep->net_error = EV_REXMTMO;
257 cep = tcp_drop(cep, E_CLS);
258 break;
259 }
260
261 /* 持続タイマを再設定し、出力をポストする。*/
262 tcp_set_persist_timer(cep);
263
264 cep->flags |= TCP_CEP_FLG_FORCE | TCP_CEP_FLG_FORCE_CLEAR | TCP_CEP_FLG_POST_OUTPUT;
265 sig_sem(SEM_TCP_POST_OUTPUT);
266 break;
267
268 /*
269 * 保留 (keep alive) タイマ
270 */
271 case TCP_TIM_KEEP:
272
273 /*
274 * コネクションが開設されるまでにタイムアウトしたら
275 * コネクションの開設を中止する。
276 */
277 if (cep->fsm_state < TCP_FSM_ESTABLISHED) {
278 cep->net_error = EV_REXMTMO;
279 cep = tcp_drop(cep, E_CLS);
280 break;
281 }
282
283#ifdef TCP_CFG_ALWAYS_KEEP
284
285 else if (cep->fsm_state < TCP_FSM_CLOSING) {
286 if (cep->idle >= TCP_TVAL_KEEP_IDLE +
287 TCP_TVAL_KEEP_COUNT * TCP_TVAL_KEEP_INTERVAL) {
288 cep->net_error = EV_REXMTMO;
289 cep = tcp_drop(cep, E_CLS);
290 break;
291 }
292 else
293 tcp_respond(NULL, cep, cep->rcv_nxt, cep->snd_una - 1,
294 cep->rbufsz - cep->rwbuf_count, 0);
295 cep->timer[TCP_TIM_KEEP] = TCP_TVAL_KEEP_INTERVAL;
296 }
297 else
298 cep->timer[TCP_TIM_KEEP] = TCP_TVAL_KEEP_IDLE;
299
300#else /* of #ifdef TCP_CFG_ALWAYS_KEEP */
301
302 cep->timer[TCP_TIM_KEEP] = TCP_TVAL_KEEP_IDLE;
303
304#endif /* of #ifdef TCP_CFG_ALWAYS_KEEP */
305
306 break;
307
308 /*
309 * 2MSL タイマ
310 */
311 case TCP_TIM_2MSL:
312
313 if (cep->fsm_state != TCP_FSM_TIME_WAIT &&
314 cep->idle <= TCP_TVAL_KEEP_COUNT * TCP_TVAL_KEEP_INTERVAL)
315 cep->timer[TCP_TIM_2MSL] = TCP_TVAL_KEEP_INTERVAL;
316 else
317 cep = tcp_close(cep);
318 break;
319 }
320
321 return cep;
322 }
323
324/*
325 * tcp_cancel_timers -- 全てのタイマをキャンセルする。
326 */
327
328void
329tcp_cancel_timers (T_TCP_CEP *cep)
330{
331 int_t ix;
332
333 for (ix = NUM_TCP_TIMERS; ix -- > 0; )
334 cep->timer[ix] = 0;
335 }
336
337#if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0
338
339/*
340 * tcp_tw_timo -- Time Wait 用 TCP 通信端点のタイムアウト処理
341 */
342
343void
344tcp_tw_timo (void)
345{
346 T_TCP_CEP *cep, *lcep;
347 T_TCP_TWCEP* twcep;
348 T_TCP_TIME longer;
349 int_t frees, waits;
350
351 /*
352 * Time Wait 用 TCP 通信端点でタイムアウトのエントリを探索し、
353 * 探索したエントリを空きにする。
354 */
355 frees = 0;
356 for (twcep = &tcp_twcep[NUM_TCP_TW_CEP_ENTRY]; twcep -- != tcp_twcep; )
357 if (twcep->fsm_state == TCP_FSM_TIME_WAIT && -- twcep->timer_2msl == 0) {
358 twcep->fsm_state = TCP_FSM_CLOSED;
359 frees ++;
360 }
361
362 /*
363 * Time Wait 用 TCP 通信端点の空きエントリが発生したら
364 * 標準 TCP 通信端点で Time Wait 中の TCP 通信端点を探索し、
365 * 必要な情報を Time Wait 用 TCP 通信端点に移して、
366 * 標準の TCP 通信端点を開放する。
367 */
368 if (frees > 0) {
369 waits = 0;
370 for (cep = &tcp_cep[tmax_tcp_cepid]; cep -- != tcp_cep; )
371 if (cep->fsm_state == TCP_FSM_TIME_WAIT)
372 waits ++;
373 if (waits > frees)
374 waits = frees;
375
376 /*
377 * もっとも長い間 Time Wait している TCP 通信端点を開放する。
378 */
379 while (waits > 0) {
380 longer = TCP_TVAL_MSL * 2;
381 lcep = NULL;
382 for (cep = &tcp_cep[tmax_tcp_cepid]; cep -- != tcp_cep; )
383 if (cep->fsm_state == TCP_FSM_TIME_WAIT && cep->timer[TCP_TIM_2MSL] < longer) {
384 longer = cep->timer[TCP_TIM_2MSL];
385 lcep = cep;
386 }
387 if (lcep != NULL) {
388 tcp_move_twcep(lcep);
389 waits --;
390 }
391 else
392 break;
393 }
394 }
395 }
396
397#endif /* of #if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0 */
398
399/*
400 * tcp_slow_timo -- 500 [ms] 毎に呼出される TCP タイムアウト関数
401 */
402
403void
404tcp_slow_timo (void *ignore)
405{
406 T_TCP_CEP *cep;
407 int_t cix, tix;
408
409 for (cix = tmax_tcp_cepid; cix -- > 0; ) {
410 cep = &tcp_cep[cix];
411 if (!(cep->fsm_state == TCP_FSM_CLOSED || cep->fsm_state == TCP_FSM_LISTEN)) {
412 for (tix = NUM_TCP_TIMERS; cep != NULL && tix -- > 0; ) {
413 if (cep->timer[tix] != 0 && -- cep->timer[tix] == 0) {
414 cep = tcp_timers(cep, tix);
415 }
416 }
417 if (cep != NULL) {
418 cep->idle ++;
419 if (cep->rtt) {
420 cep->rtt ++;
421 }
422 }
423 }
424 }
425
426#if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0
427
428 tcp_tw_timo();
429
430#endif /* of #if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0 */
431
432 if (tcp_iss != 0) {
433 tcp_iss += TCP_ISS_INCR() / TCP_SLOW_HZ;
434 }
435
436 timeout(tcp_slow_timo, NULL, TCP_SLOW_TMO);
437 }
438
439#ifdef TCP_CFG_DELAY_ACK
440
441/*
442 * tcp_fast_timo -- 200 [ms] 毎に呼出される TCP タイムアウト関数
443 */
444
445void
446tcp_fast_timo (void *ignore)
447{
448 T_TCP_CEP *cep;
449 int_t cix;
450
451 for (cix = tmax_tcp_cepid; cix -- > 0; ) {
452 cep = &tcp_cep[cix];
453 if (cep->flags & TCP_CEP_FLG_DEL_ACK) {
454 cep->flags &= ~TCP_CEP_FLG_DEL_ACK;
455 cep->flags |= TCP_CEP_FLG_ACK_NOW | TCP_CEP_FLG_POST_OUTPUT;
456 sig_sem(SEM_TCP_POST_OUTPUT);
457 }
458 }
459
460 timeout(tcp_fast_timo, NULL, TCP_FAST_TMO);
461 }
462
463#endif/* of #ifdef TCP_CFG_DELAY_ACK */
464
465/*
466 * tcp_range_set -- 時間を指定した範囲に設定する。
467 */
468
469T_TCP_TIME
470tcp_range_set (T_TCP_TIME value, T_TCP_TIME tvmin, T_TCP_TIME tvmax)
471{
472 if ((uint32_t)value < (uint32_t)tvmin)
473 return tvmin;
474 else if ((uint32_t)value > (uint32_t)tvmax)
475 return tvmax;
476 else
477 return value;
478 }
479
480/*
481 * tcp_set_persist_timer -- 持続タイマの設定
482 */
483
484void
485tcp_set_persist_timer (T_TCP_CEP *cep)
486{
487 T_TCP_TIME time;
488
489 /*
490 * srtt: 平滑化された RTT
491 * rttvar: 平滑化された分散
492 */
493 time = ((cep->srtt >> 2) + cep->rttvar) >> 1;
494
495 /*
496 * 再送タイマも設定されていれば回復不能エラー
497 */
498 if (cep->timer[TCP_TIM_REXMT])
499 /* %%% panic("tcp_output REXMT"); %%% */;
500
501 /*
502 * 持続タイマを設定する。
503 */
504 cep->timer[TCP_TIM_PERSIST] = tcp_range_set((T_TCP_TIME)(time * tcp_back_off[cep->rxtshift]),
505 (T_TCP_TIME)TCP_TVAL_MIN_PERSIST,
506 (T_TCP_TIME)TCP_TVAL_MAX_PERSIST);
507
508 /*
509 * 再送回数を更新する。
510 */
511 if (cep->rxtshift < TCP_MAX_REXMT_SHIFT)
512 cep->rxtshift ++;
513 }
514
515#endif /* of #ifdef SUPPORT_TCP */
Note: See TracBrowser for help on using the repository browser.