source: asp3_tinet_ecnl_rx/trunk/asp3_dcre/tinet/netapp/tcp_discard_cli.c@ 337

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

ASP3版ECNLを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 13.3 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 * 上記著作権者は,以下の (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 * TCP DISCARD クライアント
36 *
37 * ・ノンブロッキングコール
38 * ・IPv4
39 * ・IPv6
40 */
41
42#include <stdlib.h>
43
44#ifdef TARGET_KERNEL_ASP
45
46#include <kernel.h>
47#include <sil.h>
48#include <t_syslog.h>
49#include "kernel_cfg.h"
50
51#endif /* of #ifdef TARGET_KERNEL_ASP */
52
53#ifdef TARGET_KERNEL_JSP
54
55#include <t_services.h>
56#include "kernel_id.h"
57
58#endif /* of #ifdef TARGET_KERNEL_JSP */
59
60#include <netinet/in.h>
61#include <netinet/in_itron.h>
62
63#include <netapp/netapp.h>
64#include <netapp/netapp_var.h>
65#include <netapp/tcp_discard_cli.h>
66
67#ifdef USE_TCP_DISCARD_CLI
68
69/* discard サーバのポート番号 */
70
71#define DISCARD_SRV_PORTNO UINT_C(9)
72
73/* 表示 */
74
75/*#define SHOW_RCV_RANGE*/
76
77/* 送信間隔 */
78
79#ifdef TOPPERS_S810_CLG3_85
80
81#define START_DLY (10*SYSTIM_HZ)
82#define SND_ITV (5*SYSTIM_HZ)
83#define SLP_ITV (30*SYSTIM_HZ)
84
85#else /* of #ifdef TOPPERS_S810_CLG3_85 */
86
87#define START_DLY (10*SYSTIM_HZ)
88#define SND_ITV (5*SYSTIM_HZ)
89#define SLP_ITV (60*SYSTIM_HZ)
90
91#endif /* of #ifdef TOPPERS_S810_CLG3_85 */
92
93/* 自動実行 */
94
95#if defined(TCP_DISCARD_CLI_AUTO_RUN_STR)
96#else
97#if 0
98#if defined(SUPPORT_INET6)
99#define TCP_DISCARD_CLI_AUTO_RUN_STR "fd90:cce5:25f6:ff81:201:2ff:fe81:e7c9 - 0"
100#else
101#define TCP_DISCARD_CLI_AUTO_RUN_STR "172.25.129.142 - 0"
102#endif
103#endif
104#endif
105
106/*
107 * バッファサイズの定義
108 */
109
110#define NUM_DISCARD 5
111#define NUM_REP_PAT 40
112#define PAT_BEGIN ' '
113#define PAT_END '~'
114#define SND_BUF_SIZE ((PAT_END - PAT_BEGIN + 1) * NUM_REP_PAT)
115
116/*
117 * 全域変数
118 */
119
120bool_t tcp_discard_cli_valid;
121
122/* TCP 送受信ウィンドバッファ */
123
124#ifndef TCP_CFG_SWBUF_CSAVE_ONLY
125uint8_t tcp_discard_cli_swbuf[TCP_DISCARD_CLI_SWBUF_SIZE];
126#endif
127
128#ifdef USE_TCP_NON_BLOCKING
129
130/*
131 * 変数
132 */
133
134static T_IPEP nblk_src = {
135 IP_ADDRANY,
136 TCP_PORTANY,
137 };
138static T_IPEP nblk_dst;
139static ER_UINT nblk_error;
140
141/*
142 * ノンブロッキングコールのコールバック関数
143 */
144
145ER
146callback_nblk_tcp_discard_cli (ID cepid, FN fncd, void *p_parblk)
147{
148 ER error = E_OK;
149
150 nblk_error = *(ER*)p_parblk;
151
152 switch (fncd) {
153
154 case TFN_TCP_CON_CEP:
155 syscall(sig_sem(SEM_TCP_DISCARD_CLI_NBLK_READY));
156 break;
157
158 case TFN_TCP_CLS_CEP:
159 if (nblk_error < 0)
160 syslog(LOG_NOTICE, "[TDCn:%02u CBN] close error: %s", cepid, itron_strerror(nblk_error));
161 syscall(sig_sem(SEM_TCP_DISCARD_CLI_NBLK_READY));
162 break;
163
164 case TFN_TCP_ACP_CEP:
165 case TFN_TCP_SND_DAT:
166 case TFN_TCP_GET_BUF:
167 case TFN_TCP_SND_OOB:
168 default:
169 error = E_PAR;
170 break;
171 }
172 return error;
173 }
174
175/*
176 * send_tcp_discard -- DISCARD/TCP サーバにメッセージを送信する。
177 */
178
179static ER
180send_tcp_discard (ID cepid, T_IN_ADDR *ipaddr, uint16_t portno)
181{
182 static char smsg[SND_BUF_SIZE];
183
184 ER_UINT slen;
185 ER error;
186 SYSTIM time;
187 uint32_t total;
188 uint16_t soff, discard, rep, scount;
189 uint8_t pat, *p;
190
191 nblk_dst.ipaddr = *ipaddr;
192 nblk_dst.portno = portno;
193
194 p = smsg;
195 for (rep = NUM_REP_PAT; rep -- > 0; )
196 for (pat = PAT_BEGIN; pat <= PAT_END; pat ++)
197 *p ++ = pat;
198
199 if ((error = TCP_CON_CEP(cepid, &nblk_src, &nblk_dst, TMO_NBLK)) != E_WBLK) {
200 syslog(LOG_NOTICE, "[TDCn:%02u SND] connect error: %s", cepid, itron_strerror(error));
201 return error;
202 }
203
204 /* 接続が完了するまで待つ。*/
205 syscall(wai_sem(SEM_TCP_DISCARD_CLI_NBLK_READY));
206
207 if (nblk_error != E_OK)
208 syslog(LOG_NOTICE, "[TDCn:%02u SND] connect error: %s", cepid, itron_strerror(nblk_error));
209 else {
210 get_tim(&time);
211 syslog(LOG_NOTICE, "[TDCn:%02u SND] conct: %7lu, to: %s.%d",
212 cepid, time / SYSTIM_HZ, IP2STR(NULL, ipaddr), nblk_dst.portno);
213
214 scount = total = 0;
215 for (discard = NUM_DISCARD; discard -- > 0; ) {
216 soff = 0;
217 while (soff < SND_BUF_SIZE) {
218 if ((slen = tcp_snd_dat(cepid, smsg + soff, sizeof(smsg) - soff, TMO_FEVR)) < 0) {
219 syslog(LOG_NOTICE, "[TDCn:%02u SND] send error: %s",
220 cepid, itron_strerror(slen));
221 goto cls_ret;
222 }
223 soff += (uint16_t)slen;
224 total += slen;
225 scount ++;
226
227#ifdef SHOW_RCV_RANGE
228 syslog(LOG_NOTICE, "[TDCn:%02u SND] send: %7lu, len: %4u, off: %4u",
229 cepid, scount, (uint16_t)slen, soff);
230#endif /* of #ifdef SHOW_RCV_RANGE */
231
232 syscall(dly_tsk(500 + netapp_rand() % SYSTIM_HZ));
233 }
234 }
235
236 cls_ret:
237 get_tim(&time);
238 syslog(LOG_NOTICE, "[TDCn:%02u SND] finsh: %7lu, ttl: %lu",
239 cepid, time / SYSTIM_HZ, scount, total);
240
241 if ((error = tcp_sht_cep(cepid)) < 0)
242 syslog(LOG_NOTICE, "[TDCn:%02u SND] shutdown error: %s", cepid, itron_strerror(error));
243
244 if ((error = tcp_cls_cep(cepid, TMO_NBLK)) != E_WBLK)
245 syslog(LOG_NOTICE, "[TDCn:%02u SND] close error: %s", cepid, itron_strerror(error));
246
247 /* 開放が完了するまで待つ。*/
248 syscall(wai_sem(SEM_TCP_DISCARD_CLI_NBLK_READY));
249 }
250
251 return E_OK;
252 }
253
254#else /* of #ifdef USE_TCP_NON_BLOCKING */
255
256/*
257 * send_tcp_discard -- DISCARD/TCP サーバにメッセージを送信する。
258 */
259
260static ER
261send_tcp_discard (ID cepid, T_IN_ADDR *ipaddr, uint16_t portno)
262{
263 static char smsg[SND_BUF_SIZE];
264 static T_IPEP src = {
265 IP_ADDRANY,
266 TCP_PORTANY,
267 };
268
269 T_IPEP dst;
270 ER_UINT slen;
271 ER error;
272 SYSTIM time;
273 uint16_t pat, soff, discard, rep, scount, total;
274 uint8_t *p;
275
276 dst.ipaddr = *ipaddr;
277 dst.portno = portno;
278
279 p = smsg;
280 for (rep = NUM_REP_PAT; rep -- > 0; )
281 for (pat = PAT_BEGIN; pat <= PAT_END; pat ++)
282 *p ++ = pat;
283
284 if ((error = TCP_CON_CEP(cepid, &src, &dst, 60 * 1000)) != E_OK) {
285 syslog(LOG_NOTICE, "[TDCn:%02u SND] connect error: %s", cepid, itron_strerror(error));
286 return error;
287 }
288
289 get_tim(&time);
290 syslog(LOG_NOTICE, "[TDCn:%02u SND] conct: %7lu, to: %s.%d",
291 cepid, time / SYSTIM_HZ, IP2STR(NULL, &dst.ipaddr), dst.portno);
292
293 scount = total = 0;
294 for (discard = NUM_DISCARD; discard -- > 0; ) {
295 soff = 0;
296 while (soff < SND_BUF_SIZE) {
297 if ((slen = tcp_snd_dat(cepid, smsg + soff, sizeof(smsg) - soff, TMO_FEVR)) < 0) {
298 syslog(LOG_NOTICE, "[TDCn:%02u SND] send error: %s", cepid, itron_strerror(slen));
299 goto cls_ret;
300 }
301 soff += (uint16_t)slen;
302 total += slen;
303 scount ++;
304
305#ifdef SHOW_RCV_RANGE
306 syslog(LOG_NOTICE, "[TDCn:%02u SND] send: %7lu, len: %4u, off: %4u",
307 cepid, scount, (uint16_t)slen, soff);
308#endif /* of #ifdef SHOW_RCV_RANGE */
309
310 syscall(dly_tsk(500 + netapp_rand() % SYSTIM_HZ));
311 }
312 }
313
314cls_ret:
315 get_tim(&time);
316 syslog(LOG_NOTICE, "[TDCn:%02u SND] finsh: %7lu, ttl: %lu",
317 cepid, time / SYSTIM_HZ, scount, total);
318
319 if ((error = tcp_sht_cep(cepid)) < 0)
320 syslog(LOG_NOTICE, "[TDCn:%02u SND] shutdown error: %s", cepid, itron_strerror(error));
321
322 if ((error = tcp_cls_cep(cepid, TMO_FEVR)) < 0)
323 syslog(LOG_NOTICE, "[TDCn:%02u SND] close error: %s", cepid, itron_strerror(error));
324
325 return E_OK;
326 }
327
328#endif /* of #ifdef USE_TCP_NON_BLOCKING */
329
330/*
331 * getcomd -- コマンドを得る。
332 */
333
334#ifdef TCP_DISCARD_CLI_AUTO_RUN_STR
335
336static char *
337getcomd (ID cepid, bool_t retry)
338{
339 ER error;
340 char *line = NULL;
341 static char auto_run_str[] = TCP_DISCARD_CLI_AUTO_RUN_STR;
342 static int_t count = 0;
343
344 if (retry || (count == 0)) {
345 line = auto_run_str;
346 dly_tsk(3 * 1000);
347 }
348 else {
349 while ((error = rcv_dtq(DTQ_TCP_DISCARD_CLI, (intptr_t*)&line)) != E_OK) {
350 syslog(LOG_NOTICE, "[TDCn:%02u TSK] error: %s", cepid, itron_strerror(error));
351 dly_tsk(SLP_ITV);
352 }
353 }
354
355 count ++;
356 return line;
357 }
358
359#else /* of #ifdef TCP_DISCARD_CLI_AUTO_RUN_STR */
360
361static char *
362getcomd (ID cepid, bool_t retry)
363{
364 ER error;
365 char *line = NULL;
366
367 while ((error = rcv_dtq(DTQ_TCP_DISCARD_CLI, (intptr_t*)&line)) != E_OK) {
368 syslog(LOG_NOTICE, "[TDCn:%02u TSK] error: %s", cepid, itron_strerror(error));
369 dly_tsk(SLP_ITV);
370 }
371 return line;
372 }
373
374#endif /* of #ifdef TCP_DISCARD_CLI_AUTO_RUN_STR */
375
376void
377tcp_discard_cli_task (intptr_t exinf)
378{
379 ID tskid, cepid;
380 T_IN_ADDR addr;
381 ER error;
382 uint8_t *line;
383 int_t rep, count;
384 bool_t retry = false;
385 uint16_t portno;
386 char apip;
387
388#ifdef USE_TCP_EXTENTIONS
389
390 T_TCP_CCEP ccep;
391
392#endif /* of #ifdef USE_TCP_EXTENTIONS */
393
394#ifdef TCP_ECHO_CLI_AUTO_RUN_STR
395 dly_tsk(START_DLY);
396#endif
397 get_tid(&tskid);
398
399#if defined(SUPPORT_INET6)
400 apip = API_PROTO_IPV6;
401#if defined(SUPPORT_INET4)
402 syslog(LOG_NOTICE, "[TCPn DISCARD CLI:%d,%d] started.", tskid, (ID)exinf);
403#else
404 syslog(LOG_NOTICE, "[TCP6 DISCARD CLI:%d,%d] started.", tskid, (ID)exinf);
405#endif
406#else
407 apip = API_PROTO_IPV4;
408 syslog(LOG_NOTICE, "[TCP4 DISCARD CLI:%d,%d] started.", tskid, (ID)exinf);
409#endif
410
411 while (true) {
412 line = skip_blanks(getcomd((ID)exinf, retry));
413
414 if ((line = lookup_ipaddr(&addr, line, apip)) == NULL) {
415 syslog(LOG_NOTICE, "[TDCn:%02u TSK] sleep %d.%03u[s], unknown host.",
416 (ID)exinf, SLP_ITV / SYSTIM_HZ, SLP_ITV % SYSTIM_HZ);
417 dly_tsk(SLP_ITV);
418 syslog(LOG_NOTICE, "[TDCn:%02u TSK] resume.", (ID)exinf);
419 retry = true;
420 continue;
421 }
422 else
423 retry = false;
424
425 line = skip_blanks(line);
426 if ('0' <= *line && *line <= '9') { /* Port No */
427 line = get_int(&rep, line);
428 portno = (uint16_t)rep;
429 }
430 else {
431 line ++;
432 portno = DISCARD_SRV_PORTNO;
433 }
434
435 line = skip_blanks(line);
436 if ('0' <= *line && *line <= '9') /* Repeat */
437 line = get_int(&rep, line);
438 else
439 rep = 1;
440
441#ifdef USE_TCP_EXTENTIONS
442
443 ccep.cepatr = 0;
444 ccep.sbufsz = TCP_DISCARD_CLI_SWBUF_SIZE;
445 ccep.rbufsz = 0;
446 ccep.rbuf = NADR;
447
448#ifdef TCP_CFG_SWBUF_CSAVE
449 ccep.sbuf = NADR;
450#else
451 ccep.sbuf = tcp_discard_cli_swbuf;
452#endif
453#ifdef USE_TCP_NON_BLOCKING
454 ccep.callback = (FP)callback_nblk_tcp_discard_cli;
455#else
456 ccep.callback = NULL;
457#endif
458
459 if ((error = ALLOC_TCP_CEP(&cepid, tskid, &ccep)) != E_OK) {
460 syslog(LOG_NOTICE, "[TDCn:%02u TSK] CEP create error: %s", cepid, itron_strerror(error));
461 continue;
462 }
463
464#else /* of #ifdef USE_TCP_EXTENTIONS */
465
466 cepid = (ID)exinf;
467
468#endif /* of #ifdef USE_TCP_EXTENTIONS */
469
470 tcp_discard_cli_valid = true;
471 count = 0;
472 while (rep == 0 || count < rep) {
473 if (!tcp_discard_cli_valid) {
474 syslog(LOG_NOTICE, "[TDCn:%02u TSK] canceled.", cepid);
475 break;
476 }
477
478 count ++;
479 if (rep == 0) {
480 syslog(LOG_NOTICE, "[TDCn:%02u TSK] start: repeat: %d", cepid, count);
481 error = send_tcp_discard(cepid, &addr, portno);
482 syslog(LOG_NOTICE, "[TDCn:%02u TSK] finsh: repeat: %d", cepid, count);
483 }
484 else {
485 syslog(LOG_NOTICE, "[TDCn:%02u TSK] start: repeat: %d/%d", cepid, count, rep);
486 error = send_tcp_discard(cepid, &addr, portno);
487 syslog(LOG_NOTICE, "[TDCn:%02u TSK] finsh: repeat: %d/%d", cepid, count, rep);
488 }
489
490 if (error != E_OK) {
491 syslog(LOG_NOTICE, "[TDCn:%02u TSK] sleep %d.%03u[s], error: %s",
492 cepid, SLP_ITV / SYSTIM_HZ, SLP_ITV % SYSTIM_HZ, itron_strerror(error));
493 tslp_tsk(SLP_ITV);
494 syslog(LOG_NOTICE, "[TDCn:%02u TSK] resume.", cepid);
495 }
496
497#if defined(SND_ITV)
498#if SND_ITV > 0
499 if (count < rep) {
500 uint_t itv;
501
502 itv = SND_ITV;
503 syslog(LOG_NOTICE, "[TDCn:%02u TSK] interval: %d[s].", cepid, itv / SYSTIM_HZ);
504 syscall(dly_tsk(SND_ITV));
505 }
506#endif
507#endif
508 }
509
510#ifdef USE_TCP_EXTENTIONS
511
512 if ((error = FREE_TCP_CEP(cepid)) != E_OK)
513 syslog(LOG_NOTICE, "[TDCn:%02u TSK] CEP delete error: %s", cepid, itron_strerror(error));
514
515#endif /* of #ifdef USE_TCP_EXTENTIONS */
516
517 }
518 }
519
520#endif /* of #ifdef USE_TCP_DISCARD_CLI */
Note: See TracBrowser for help on using the repository browser.