summaryrefslogtreecommitdiff
path: root/ttcp/rtems_ttcp.c
blob: d266b4277c57776e010afd104f65dfc47cb44af3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * A collection of hacks, glue, and patches to
 * provide a `UNIX-like' environment to ttcp.
 *
 * Some of the code here should migrate to the libc
 * support routines some day.  Some of the more sleazy
 * hacks should never make it outside this file!
 * 
 * This program may be distributed and used for any purpose.
 * I ask only that you:
 *      1. Leave this author information intact.
 *      2. Document any changes you make.
 *
 * W. Eric Norum
 * Saskatchewan Accelerator Laboratory
 * University of Saskatchewan
 * Saskatoon, Saskatchewan, CANADA
 * eric@skatter.usask.ca
 */

#include <stdio.h>
#include <rtems.h>
#include <rtems_ka9q.h>
#include <rtems/error.h>
#include <socket.h>
#include <sockaddr.h>
#include <netuser.h>
#include <sys/time.h>

/*
 * Glue between UNIX-style ttcp code and RTEMS
 */
int rtems_ttcp_main (int argc, char **argv);

#define ENOBUFS	2000

struct  hostent {
	char    *h_name;        /* official name of host */
	char    **h_aliases;    /* alias list */
	int     h_addrtype;     /* host address type */
	int     h_length;       /* length of address */
	char    **h_addr_list;  /* list of addresses from name server */
	#define h_addr  h_addr_list[0]  /* address, for backward compatiblity */
};

#define SOL_SOCKET	0
#define SO_DEBUG	0

static struct hostent *
gethostbyname (const char *cp)
{
	rtems_panic ("gethostbyname()");
}

static int
select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{
	rtems_panic ("select()");
}

static void
(*signal(int sig, void (*func)()))()
{
	return 0;;
}

static char *
rtems_inet_ntoa (struct in_addr in)
{
	return inet_ntoa (in.s_addr);
}
#define inet_ntoa rtems_inet_ntoa
#define inet_addr(cp)	resolve(cp)

int
gettimeofday (struct timeval *tp, struct timezone *tzp)
{
	rtems_clock_time_value now;

	rtems_clock_get (RTEMS_CLOCK_GET_TIME_VALUE, &now);
	tp->tv_sec = now.seconds;
	tp->tv_usec = now.microseconds;
	return 0;
}

#define _SYS_RESOURCE_H_
#define	RUSAGE_SELF	0		/* calling process */
#define	RUSAGE_CHILDREN	-1		/* terminated child processes */
struct rusage {
	struct timeval ru_utime;	/* user time used */
	struct timeval ru_stime;	/* system time used */
	int ru_maxrss;		/* maximum resident set size */
	int ru_ixrss;		/* currently 0 */
	int ru_idrss;		/* integral resident set size */
	int ru_isrss;		/* currently 0 */
	int ru_minflt;		/* page faults not requiring physical I/O */
	int ru_majflt;		/* page faults requiring physical I/O */
	int ru_nswap;		/* swaps */
	int ru_inblock;		/* block input operations */
	int ru_oublock;		/* block output operations */
	int ru_msgsnd;		/* messages sent */
	int ru_msgrcv;		/* messages received */
	int ru_nsignals;	/* signals received */
	int ru_nvcsw;		/* voluntary context switches */
	int ru_nivcsw;		/* involuntary context switches */
};
int
getrusage(int ignored, struct rusage *ru)
{
	rtems_clock_time_value now;
	static struct rusage nullUsage;

	rtems_clock_get (RTEMS_CLOCK_GET_TIME_VALUE, &now);
	*ru = nullUsage;
	ru->ru_stime.tv_sec  = now.seconds;
	ru->ru_stime.tv_usec = now.microseconds;
	ru->ru_utime.tv_sec  = 0;
	ru->ru_utime.tv_usec = 0;
	return 0;
}

/*
 * Display the contents of several KA9Q tables
 */
static void
show_ka9q_tables (void)
{
	printf ("\n****************** MBUF Statistics ***************\n");
	mbufstat ();
	mbufsizes ();
	printf ("\n****************** Routing Table ***************\n");
	rtems_ka9q_execute_command ("route");
	printf ("\n****************** ARP Table ***************\n");
	rtems_ka9q_execute_command ("arp");
	printf ("\n****************** Driver Statistics ***************\n");
	rtems_ka9q_execute_command ("ifconfig rtems");
	printf ("\n****************** Ip Statistics ***************\n");
	rtems_ka9q_execute_command ("ip status");
	printf ("\n****************** ICMP Statistics ***************\n");
	rtems_ka9q_execute_command ("icmp status");
	printf ("\n****************** UDP Statistics ***************\n");
	rtems_ka9q_execute_command ("udp status");
	printf ("\n****************** TCP Statistics ***************\n");
	rtems_ka9q_execute_command ("tcp status");
}

static void
rtems_ttcp_exit (int code)
{
	rtems_interval ticksPerSecond;

	rtems_clock_get (RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticksPerSecond);
	rtems_task_wake_after (ticksPerSecond * 2);
	show_ka9q_tables ();
	exit (code);
}

/*
 * Task to run UNIX ttcp command
 */
char *__progname;
static void
ttcpTask (rtems_task_argument arg)
{
	int code;
	int argc;
	char arg0[10];
	char *argv[20];
	char linebuf[200];

	for (;;) {
		char *cp;

		/*
		 * Set up first argument
		 */
		argc = 1;
		strcpy (arg0, "ttcp");
		argv[0] = __progname = arg0;

		/*
		 * Read a line
		 */
		printf (">>> %s ", argv[0]);
		fflush (stdout);
		fgets (linebuf, sizeof linebuf, stdin);

		/*
		 * Break line into arguments
		 */
		cp = linebuf;
		for (;;) {
			while (isspace (*cp))
				*cp++ = '\0';
			if (*cp == '\0')
				break;
			if (argc >= ((sizeof argv / sizeof argv[0]) - 1)) {
				printf ("Too many arguments.\n");
				argc = 0;
				break;
			}
			argv[argc++] = cp;
			while (!isspace (*cp)) {
				if (*cp == '\0')
					break;
				cp++;
			}
		}
		if (argc > 1) {
			argv[argc] = NULL;
			break;
		}
		printf ("You must give some arguments!\n");
		printf ("At the very least, you must provide\n");
		printf ("         -r\n");
		printf ("or\n");
		printf ("         -t destination.internet.address\n");
	}
	code = rtems_ttcp_main (argc, argv);
	rtems_ttcp_exit (code);
}

static int
rtems_ttcp_bind (int s, struct sockaddr *name, int namelen)
{
	struct sockaddr_in *in = (struct sockaddr_in *)name;
	/*
	 * KA9Q doesn't like 0 port numbers
	 */
	if (in->sin_port == 0)
		in->sin_port = 2662;
	return bind (s, name, namelen);
}

/*
 * Test network throughput
 */
void
test_network (void)
{
	rtems_id tid;
	rtems_status_code sc;
	rtems_time_of_day now;
	rtems_task_priority my_priority;

	/*
	 * Set up time-of-day clock
	 */
	now.year = 1997;
	now.month = 1;
	now.day = 1;
	now.hour = 0;
	now.minute = 0;
	now.second = 0;
	now.ticks = 0;
	sc = rtems_clock_set (&now);
	if (sc != RTEMS_SUCCESSFUL) {
		printf ("Can't set date/time; %s\n", rtems_status_text (sc));
		return;
	}

	/*
	 * Spawn test task
	 */
	rtems_task_set_priority (RTEMS_SELF, RTEMS_CURRENT_PRIORITY, &my_priority);
	sc = rtems_task_create (rtems_build_name ('T', 'T', 'C', 'P'),
			my_priority,
			32*1024,
			RTEMS_PREEMPT|RTEMS_NO_TIMESLICE|RTEMS_NO_ASR|RTEMS_INTERRUPT_LEVEL(0),
			RTEMS_NO_FLOATING_POINT|RTEMS_LOCAL,
			&tid);
	if (sc != RTEMS_SUCCESSFUL) {
		printf ("Can't create task; %s\n", rtems_status_text (sc));
		return;
	}
	sc = rtems_task_start (tid, ttcpTask, 0);
	if (sc != RTEMS_SUCCESSFUL) {
		printf ("Can't start task; %s\n", rtems_status_text (sc));
		return;
	}
	rtems_task_suspend (RTEMS_SELF);
}

#define main		rtems_ttcp_main
#define exit(code)	close(fd),rtems_ttcp_exit(code)
#define bind		rtems_ttcp_bind
#define read_timer	rtems_read_timer

/*
 * RTEMS/KA9Q code expects port numbers in host byte order!
 */
#define htons(x)	(x)

#include "ttcp_orig/ttcp.c"