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

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

mruby版ECNLプロトタイピング・ツールを追加

  • 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.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 * 上記著作権者
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 270 2017-02-09 04:03:47Z 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.h>
106#include <net/net_var.h>
107#include <net/net_buf.h>
108#include <net/net_timer.h>
109
110#include <netinet/in.h>
111#include <netinet/in_var.h>
112#include <netinet6/in6.h>
113#include <netinet6/in6_var.h>
114#include <netinet/ip.h>
115#include <netinet/ip6.h>
116#include <netinet/tcp.h>
117#include <netinet/tcp_timer.h>
118#include <netinet/tcp_var.h>
119#include <netinet/tcp_fsm.h>
120#include <netinet/tcp_seq.h>
121
122#ifdef SUPPORT_TCP
123
124/*
125 * 局所関数
126 */
127
128static T_TCP_CEP *tcp_timers (T_TCP_CEP *cep, int_t tix);
129
130#if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0
131
132static void tcp_tw_timo (void);
133
134#endif /* of #if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0 */
135
136/*
137 * バックオフ時間
138 *
139 * 再送を行うたびに、タイムアウトの時間を延長する。
140 */
141
142const static uint8_t tcp_back_off[] = {
143 UINT_C(1), UINT_C(2), UINT_C(4), UINT_C(8),
144 UINT_C(16), UINT_C(32), UINT_C(64), UINT_C(64),
145 UINT_C(64), UINT_C(64), UINT_C(64), UINT_C(64),
146 UINT_C(64)
147 };
148
149#define TCP_TOTAL_BACK_OFF 511 /* バックオフ時間の合計 */
150
151/*
152 * tcp_timers -- タイムアウト処理
153 */
154
155static T_TCP_CEP *
156tcp_timers (T_TCP_CEP *cep, int_t tix)
157{
158 uint16_t win;
159
160
161 switch (tix) {
162
163 /*
164 * 再送タイマ
165 */
166 case TCP_TIM_REXMT:
167
168 /*
169 * 最大再送回数 (TCP_MAX_REXMT_SHIFT、標準 12 回) になったときは、
170 * コネクションを切断する。
171 */
172 if (++ cep->rxtshift > TCP_MAX_REXMT_SHIFT) {
173 cep->rxtshift = TCP_MAX_REXMT_SHIFT;
174 cep->net_error = EV_REXMTMO;
175 cep = tcp_drop(cep, E_CLS);
176 break;
177 }
178
179 /*
180 * 再送タイムアウトを計算する。
181 */
182 cep->rxtcur = tcp_range_set((T_TCP_TIME)(tcp_rexmt_val(cep) * tcp_back_off[cep->rxtshift]),
183 (T_TCP_TIME)TCP_TVAL_MIN,
184 (T_TCP_TIME)TCP_TVAL_MAX_REXMT);
185 cep->timer[TCP_TIM_REXMT] = cep->rxtcur;
186
187 /*
188 * srtt: 平滑化された RTT
189 * rttvar: 平滑化された分散
190 *
191 * 再送回数が最大再送回数の 1/4 になったときは、
192 * 平滑化された分散 (rttvar) に srtt を加算し、
193 * 平滑化された RTT を 0 にする。
194 *
195 */
196 if (cep->rxtshift > TCP_MAX_REXMT_SHIFT / 4) {
197 cep->rttvar += (cep->srtt >> TCP_SRTT_SHIFT);
198 cep->srtt = 0;
199 }
200
201 /*
202 * snd_nxt: 次に送信する SEQ、この時点では、前回送信した SEQ
203 * snd_una: 未確認の最小送信 SEQ または、確認された最大送信 SEQ
204 *
205 * 前回送信した SEQ (snd_nxt) を
206 * 確認された最大送信 SEQ (snd_una) まで戻す。
207 */
208 cep->snd_nxt = cep->snd_una;
209 cep->flags |= TCP_CEP_FLG_ACK_NOW;
210
211 /*
212 * rtt: 往復時間の計測を中止する。
213 */
214 cep->rtt = 0;
215
216 /*
217 * 送信ウインドの設定
218 *
219 * snd_wnd: 相手の受信可能ウィンドサイズ
220 * snd_cwnd: 輻輳ウィンドサイズ
221 * maxseg : 相手の最大受信セグメントサイズ
222 *
223 * 相手の受信可能ウィンドサイズ (snd_wnd) か、
224 * 輻輳ウィンドサイズ (snd_cwnd) の
225 * どちらか小さいサイズの 1/2 を、更に
226 * 相手の最大受信セグメントサイズ (maxseg) で割った値。
227 * ただし、2 以上
228 */
229 if (cep->snd_wnd < cep->snd_cwnd)
230 win = cep->snd_wnd / 2 / cep->maxseg;
231 else
232 win = cep->snd_cwnd / 2 / cep->maxseg;
233
234 if (win < 2)
235 win = 2;
236
237 /*
238 * 輻輳ウィンドサイズ (snd_cwnd) は
239 * 相手の受信可能ウィンドサイズ (snd_wnd) に、
240 * 輻輳ウィンドサイズのしきい値 (snd_ssthresh) は
241 * 相手の受信可能ウィンドサイズ (snd_wnd) の win 倍に
242 * 設定する。
243 */
244 cep->snd_cwnd = cep->maxseg;
245 cep->snd_ssthresh = win * cep->maxseg;
246 cep->dupacks = 0;
247
248 /* 出力をポストする。*/
249 cep->flags |= TCP_CEP_FLG_POST_OUTPUT;
250 sig_sem(SEM_TCP_POST_OUTPUT);
251 break;
252
253 /*
254 * 持続タイマ
255 */
256 case TCP_TIM_PERSIST:
257
258 /*
259 * 最大再送回数 (TCP_MAX_REXMT_SHIFT、標準 12 回) を超
260えていて、
261 * アイドル時間が、保留タイマの標準値 (TCP_TVAL_KEEP_IDLE、
262 * 標準 2 * 60 * 60 秒) 以上か、
263 * 再送タイムアウト値 * バックオフ時間の合計以上なら
264 * コネクションを切断する。
265 */
266 if (cep->rxtshift > TCP_MAX_REXMT_SHIFT &&
267 (cep->idle >= TCP_TVAL_KEEP_IDLE ||
268 cep->idle >= tcp_rexmt_val(cep) * TCP_TOTAL_BACK_OFF)) {
269 cep->net_error = EV_REXMTMO;
270 cep = tcp_drop(cep, E_CLS);
271 break;
272 }
273
274 /* 持続タイマを再設定し、出力をポストする。*/
275 tcp_set_persist_timer(cep);
276
277 cep->flags |= TCP_CEP_FLG_FORCE | TCP_CEP_FLG_FORCE_CLEAR | TCP_CEP_FLG_POST_OUTPUT;
278 sig_sem(SEM_TCP_POST_OUTPUT);
279 break;
280
281 /*
282 * 保留 (keep alive) タイマ
283 */
284 case TCP_TIM_KEEP:
285
286 /*
287 * コネクションが開設されるまでにタイムアウトしたら
288 * コネクションの開設を中止する。
289 */
290 if (cep->fsm_state < TCP_FSM_ESTABLISHED) {
291 cep->net_error = EV_REXMTMO;
292 cep = tcp_drop(cep, E_CLS);
293 break;
294 }
295
296#ifdef TCP_CFG_ALWAYS_KEEP
297
298 else if (cep->fsm_state < TCP_FSM_CLOSING) {
299 if (cep->idle >= TCP_TVAL_KEEP_IDLE +
300 TCP_TVAL_KEEP_COUNT * TCP_TVAL_KEEP_INTERVAL) {
301 cep->net_error = EV_REXMTMO;
302 cep = tcp_drop(cep, E_CLS);
303 break;
304 }
305 else
306 tcp_respond(NULL, cep, cep->rcv_nxt, cep->snd_una - 1,
307 cep->rbufsz - cep->rwbuf_count, 0);
308 cep->timer[TCP_TIM_KEEP] = TCP_TVAL_KEEP_INTERVAL;
309 }
310 else
311 cep->timer[TCP_TIM_KEEP] = TCP_TVAL_KEEP_IDLE;
312
313#else /* of #ifdef TCP_CFG_ALWAYS_KEEP */
314
315 cep->timer[TCP_TIM_KEEP] = TCP_TVAL_KEEP_IDLE;
316
317#endif /* of #ifdef TCP_CFG_ALWAYS_KEEP */
318
319 break;
320
321 /*
322 * 2MSL タイマ
323 */
324 case TCP_TIM_2MSL:
325
326 if (cep->fsm_state != TCP_FSM_TIME_WAIT &&
327 cep->idle <= TCP_TVAL_KEEP_COUNT * TCP_TVAL_KEEP_INTERVAL)
328 cep->timer[TCP_TIM_2MSL] = TCP_TVAL_KEEP_INTERVAL;
329 else
330 cep = tcp_close(cep);
331 break;
332 }
333
334 return cep;
335 }
336
337/*
338 * tcp_cancel_timers -- å…
339¨ã¦ã®ã‚¿ã‚¤ãƒžã‚’キャンセルする。
340 */
341
342void
343tcp_cancel_timers (T_TCP_CEP *cep)
344{
345 int_t ix;
346
347 for (ix = NUM_TCP_TIMERS; ix -- > 0; )
348 cep->timer[ix] = 0;
349 }
350
351#if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0
352
353/*
354 * tcp_tw_timo -- Time Wait 用 TCP 通信端点のタイムアウト処理
355 */
356
357void
358tcp_tw_timo (void)
359{
360 T_TCP_CEP *cep, *lcep;
361 T_TCP_TWCEP* twcep;
362 T_TCP_TIME longer;
363 int_t frees, waits;
364
365 /*
366 * Time Wait 用 TCP 通信端点でタイムアウトのエントリを探索し、
367 * 探索したエントリを空きにする。
368 */
369 frees = 0;
370 for (twcep = &tcp_twcep[NUM_TCP_TW_CEP_ENTRY]; twcep -- != tcp_twcep; )
371 if (twcep->fsm_state == TCP_FSM_TIME_WAIT && -- twcep->timer_2msl == 0) {
372 twcep->fsm_state = TCP_FSM_CLOSED;
373 frees ++;
374 }
375
376 /*
377 * Time Wait 用 TCP 通信端点の空きエントリが発生したら
378 * 標準 TCP 通信端点で Time Wait 中の TCP 通信端点を探索し、
379 * 必
380要な情
381報を Time Wait 用 TCP 通信端点に移して、
382 * 標準の TCP 通信端点を開放する。
383 */
384 if (frees > 0) {
385 waits = 0;
386 for (cep = &tcp_cep[tmax_tcp_cepid]; cep -- != tcp_cep; )
387 if (cep->fsm_state == TCP_FSM_TIME_WAIT)
388 waits ++;
389 if (waits > frees)
390 waits = frees;
391
392 /*
393 * もっとも長い間 Time Wait している TCP 通信端点を開放する。
394 */
395 while (waits > 0) {
396 longer = TCP_TVAL_MSL * 2;
397 lcep = NULL;
398 for (cep = &tcp_cep[tmax_tcp_cepid]; cep -- != tcp_cep; )
399 if (cep->fsm_state == TCP_FSM_TIME_WAIT && cep->timer[TCP_TIM_2MSL] < longer) {
400 longer = cep->timer[TCP_TIM_2MSL];
401 lcep = cep;
402 }
403 if (lcep != NULL) {
404 tcp_move_twcep(lcep);
405 waits --;
406 }
407 else
408 break;
409 }
410 }
411 }
412
413#endif /* of #if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0 */
414
415/*
416 * tcp_slow_timo -- 500 [ms] 毎に呼出される TCP タイムアウト関数
417 */
418
419void
420tcp_slow_timo (void *ignore)
421{
422 T_TCP_CEP *cep;
423 int_t cix, tix;
424
425 for (cix = tmax_tcp_cepid; cix -- > 0; ) {
426 cep = &tcp_cep[cix];
427 if (!(cep->fsm_state == TCP_FSM_CLOSED || cep->fsm_state == TCP_FSM_LISTEN)) {
428 for (tix = NUM_TCP_TIMERS; cep != NULL && tix -- > 0; ) {
429 if (cep->timer[tix] != 0 && -- cep->timer[tix] == 0) {
430 cep = tcp_timers(cep, tix);
431 }
432 }
433 if (cep != NULL) {
434 cep->idle ++;
435 if (cep->rtt) {
436 cep->rtt ++;
437 }
438 }
439 }
440 }
441
442#if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0
443
444 tcp_tw_timo();
445
446#endif /* of #if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0 */
447
448 if (tcp_iss != 0) {
449 tcp_iss += TCP_ISS_INCR() / TCP_SLOW_HZ;
450 }
451
452 timeout(tcp_slow_timo, NULL, TCP_SLOW_TMO);
453 }
454
455#ifdef TCP_CFG_DELAY_ACK
456
457/*
458 * tcp_fast_timo -- 200 [ms] 毎に呼出される TCP タイムアウト関数
459 */
460
461void
462tcp_fast_timo (void *ignore)
463{
464 T_TCP_CEP *cep;
465 int_t cix;
466
467 for (cix = tmax_tcp_cepid; cix -- > 0; ) {
468 cep = &tcp_cep[cix];
469 if (cep->flags & TCP_CEP_FLG_DEL_ACK) {
470 cep->flags &= ~TCP_CEP_FLG_DEL_ACK;
471 cep->flags |= TCP_CEP_FLG_ACK_NOW | TCP_CEP_FLG_POST_OUTPUT;
472 sig_sem(SEM_TCP_POST_OUTPUT);
473 }
474 }
475
476 timeout(tcp_fast_timo, NULL, TCP_FAST_TMO);
477 }
478
479#endif/* of #ifdef TCP_CFG_DELAY_ACK */
480
481/*
482 * tcp_range_set -- 時間を指定した範囲に設定する。
483 */
484
485T_TCP_TIME
486tcp_range_set (T_TCP_TIME value, T_TCP_TIME tvmin, T_TCP_TIME tvmax)
487{
488 if ((uint32_t)value < (uint32_t)tvmin)
489 return tvmin;
490 else if ((uint32_t)value > (uint32_t)tvmax)
491 return tvmax;
492 else
493 return value;
494 }
495
496/*
497 * tcp_set_persist_timer -- 持続タイマの設定
498 */
499
500void
501tcp_set_persist_timer (T_TCP_CEP *cep)
502{
503 T_TCP_TIME time;
504
505 /*
506 * srtt: 平滑化された RTT
507 * rttvar: 平滑化された分散
508 */
509 time = ((cep->srtt >> 2) + cep->rttvar) >> 1;
510
511 /*
512 * 再送タイマも設定されていれば回復不能エラー
513 */
514 if (cep->timer[TCP_TIM_REXMT])
515 /* %%% panic("tcp_output REXMT"); %%% */;
516
517 /*
518 * 持続タイマを設定する。
519 */
520 cep->timer[TCP_TIM_PERSIST] = tcp_range_set((T_TCP_TIME)(time * tcp_back_off[cep->rxtshift]),
521 (T_TCP_TIME)TCP_TVAL_MIN_PERSIST,
522 (T_TCP_TIME)TCP_TVAL_MAX_PERSIST);
523
524 /*
525 * 再送回数を更新する。
526 */
527 if (cep->rxtshift < TCP_MAX_REXMT_SHIFT)
528 cep->rxtshift ++;
529 }
530
531#endif /* of #ifdef SUPPORT_TCP */
Note: See TracBrowser for help on using the repository browser.