source: rubycfg_asp/trunk/asp_dcre/tinet/netinet/tcp_timer.c@ 313

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

ソースを追加

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