source: network-demos/ttcp/rtems_ttcp.c @ 9399732

4.11netdemos-4-5-branchnetwork-demos-4-10-branchnetwork-demos-4-6-branchnetwork-demos-4-7-branchnetwork-demos-4-8-branchnetwork-demos-4-9-branchrtems-4-5-branch
Last change on this file since 9399732 was c87143a, checked in by Joel Sherrill <joel.sherrill@…>, on 07/30/98 at 14:42:29

base from Eric Norum -- Demos.30May1998.tar.gz

  • Property mode set to 100644
File size: 6.9 KB
Line 
1/*
2 * A collection of hacks, glue, and patches to
3 * provide a `UNIX-like' environment to ttcp.
4 *
5 * Some of the code here should migrate to the libc
6 * support routines some day.  Some of the more sleazy
7 * hacks should never make it outside this file!
8 *
9 * This program may be distributed and used for any purpose.
10 * I ask only that you:
11 *      1. Leave this author information intact.
12 *      2. Document any changes you make.
13 *
14 * W. Eric Norum
15 * Saskatchewan Accelerator Laboratory
16 * University of Saskatchewan
17 * Saskatoon, Saskatchewan, CANADA
18 * eric@skatter.usask.ca
19 */
20
21#include <stdio.h>
22#include <rtems.h>
23#include <rtems_ka9q.h>
24#include <rtems/error.h>
25#include <socket.h>
26#include <sockaddr.h>
27#include <netuser.h>
28#include <sys/time.h>
29
30/*
31 * Glue between UNIX-style ttcp code and RTEMS
32 */
33int rtems_ttcp_main (int argc, char **argv);
34
35#define ENOBUFS 2000
36
37struct  hostent {
38        char    *h_name;        /* official name of host */
39        char    **h_aliases;    /* alias list */
40        int     h_addrtype;     /* host address type */
41        int     h_length;       /* length of address */
42        char    **h_addr_list;  /* list of addresses from name server */
43        #define h_addr  h_addr_list[0]  /* address, for backward compatiblity */
44};
45
46#define SOL_SOCKET      0
47#define SO_DEBUG        0
48
49static struct hostent *
50gethostbyname (const char *cp)
51{
52        rtems_panic ("gethostbyname()");
53}
54
55static int
56select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
57{
58        rtems_panic ("select()");
59}
60
61static void
62(*signal(int sig, void (*func)()))()
63{
64        return 0;;
65}
66
67static char *
68rtems_inet_ntoa (struct in_addr in)
69{
70        return inet_ntoa (in.s_addr);
71}
72#define inet_ntoa rtems_inet_ntoa
73#define inet_addr(cp)   resolve(cp)
74
75int
76gettimeofday (struct timeval *tp, struct timezone *tzp)
77{
78        rtems_clock_time_value now;
79
80        rtems_clock_get (RTEMS_CLOCK_GET_TIME_VALUE, &now);
81        tp->tv_sec = now.seconds;
82        tp->tv_usec = now.microseconds;
83        return 0;
84}
85
86#define _SYS_RESOURCE_H_
87#define RUSAGE_SELF     0               /* calling process */
88#define RUSAGE_CHILDREN -1              /* terminated child processes */
89struct rusage {
90        struct timeval ru_utime;        /* user time used */
91        struct timeval ru_stime;        /* system time used */
92        int ru_maxrss;          /* maximum resident set size */
93        int ru_ixrss;           /* currently 0 */
94        int ru_idrss;           /* integral resident set size */
95        int ru_isrss;           /* currently 0 */
96        int ru_minflt;          /* page faults not requiring physical I/O */
97        int ru_majflt;          /* page faults requiring physical I/O */
98        int ru_nswap;           /* swaps */
99        int ru_inblock;         /* block input operations */
100        int ru_oublock;         /* block output operations */
101        int ru_msgsnd;          /* messages sent */
102        int ru_msgrcv;          /* messages received */
103        int ru_nsignals;        /* signals received */
104        int ru_nvcsw;           /* voluntary context switches */
105        int ru_nivcsw;          /* involuntary context switches */
106};
107int
108getrusage(int ignored, struct rusage *ru)
109{
110        rtems_clock_time_value now;
111        static struct rusage nullUsage;
112
113        rtems_clock_get (RTEMS_CLOCK_GET_TIME_VALUE, &now);
114        *ru = nullUsage;
115        ru->ru_stime.tv_sec  = now.seconds;
116        ru->ru_stime.tv_usec = now.microseconds;
117        ru->ru_utime.tv_sec  = 0;
118        ru->ru_utime.tv_usec = 0;
119        return 0;
120}
121
122/*
123 * Display the contents of several KA9Q tables
124 */
125static void
126show_ka9q_tables (void)
127{
128        printf ("\n****************** MBUF Statistics ***************\n");
129        mbufstat ();
130        mbufsizes ();
131        printf ("\n****************** Routing Table ***************\n");
132        rtems_ka9q_execute_command ("route");
133        printf ("\n****************** ARP Table ***************\n");
134        rtems_ka9q_execute_command ("arp");
135        printf ("\n****************** Driver Statistics ***************\n");
136        rtems_ka9q_execute_command ("ifconfig rtems");
137        printf ("\n****************** Ip Statistics ***************\n");
138        rtems_ka9q_execute_command ("ip status");
139        printf ("\n****************** ICMP Statistics ***************\n");
140        rtems_ka9q_execute_command ("icmp status");
141        printf ("\n****************** UDP Statistics ***************\n");
142        rtems_ka9q_execute_command ("udp status");
143        printf ("\n****************** TCP Statistics ***************\n");
144        rtems_ka9q_execute_command ("tcp status");
145}
146
147static void
148rtems_ttcp_exit (int code)
149{
150        rtems_interval ticksPerSecond;
151
152        rtems_clock_get (RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticksPerSecond);
153        rtems_task_wake_after (ticksPerSecond * 2);
154        show_ka9q_tables ();
155        exit (code);
156}
157
158/*
159 * Task to run UNIX ttcp command
160 */
161char *__progname;
162static void
163ttcpTask (rtems_task_argument arg)
164{
165        int code;
166        int argc;
167        char arg0[10];
168        char *argv[20];
169        char linebuf[200];
170
171        for (;;) {
172                char *cp;
173
174                /*
175                 * Set up first argument
176                 */
177                argc = 1;
178                strcpy (arg0, "ttcp");
179                argv[0] = __progname = arg0;
180
181                /*
182                 * Read a line
183                 */
184                printf (">>> %s ", argv[0]);
185                fflush (stdout);
186                fgets (linebuf, sizeof linebuf, stdin);
187
188                /*
189                 * Break line into arguments
190                 */
191                cp = linebuf;
192                for (;;) {
193                        while (isspace (*cp))
194                                *cp++ = '\0';
195                        if (*cp == '\0')
196                                break;
197                        if (argc >= ((sizeof argv / sizeof argv[0]) - 1)) {
198                                printf ("Too many arguments.\n");
199                                argc = 0;
200                                break;
201                        }
202                        argv[argc++] = cp;
203                        while (!isspace (*cp)) {
204                                if (*cp == '\0')
205                                        break;
206                                cp++;
207                        }
208                }
209                if (argc > 1) {
210                        argv[argc] = NULL;
211                        break;
212                }
213                printf ("You must give some arguments!\n");
214                printf ("At the very least, you must provide\n");
215                printf ("         -r\n");
216                printf ("or\n");
217                printf ("         -t destination.internet.address\n");
218        }
219        code = rtems_ttcp_main (argc, argv);
220        rtems_ttcp_exit (code);
221}
222
223static int
224rtems_ttcp_bind (int s, struct sockaddr *name, int namelen)
225{
226        struct sockaddr_in *in = (struct sockaddr_in *)name;
227        /*
228         * KA9Q doesn't like 0 port numbers
229         */
230        if (in->sin_port == 0)
231                in->sin_port = 2662;
232        return bind (s, name, namelen);
233}
234
235/*
236 * Test network throughput
237 */
238void
239test_network (void)
240{
241        rtems_id tid;
242        rtems_status_code sc;
243        rtems_time_of_day now;
244        rtems_task_priority my_priority;
245
246        /*
247         * Set up time-of-day clock
248         */
249        now.year = 1997;
250        now.month = 1;
251        now.day = 1;
252        now.hour = 0;
253        now.minute = 0;
254        now.second = 0;
255        now.ticks = 0;
256        sc = rtems_clock_set (&now);
257        if (sc != RTEMS_SUCCESSFUL) {
258                printf ("Can't set date/time; %s\n", rtems_status_text (sc));
259                return;
260        }
261
262        /*
263         * Spawn test task
264         */
265        rtems_task_set_priority (RTEMS_SELF, RTEMS_CURRENT_PRIORITY, &my_priority);
266        sc = rtems_task_create (rtems_build_name ('T', 'T', 'C', 'P'),
267                        my_priority,
268                        32*1024,
269                        RTEMS_PREEMPT|RTEMS_NO_TIMESLICE|RTEMS_NO_ASR|RTEMS_INTERRUPT_LEVEL(0),
270                        RTEMS_NO_FLOATING_POINT|RTEMS_LOCAL,
271                        &tid);
272        if (sc != RTEMS_SUCCESSFUL) {
273                printf ("Can't create task; %s\n", rtems_status_text (sc));
274                return;
275        }
276        sc = rtems_task_start (tid, ttcpTask, 0);
277        if (sc != RTEMS_SUCCESSFUL) {
278                printf ("Can't start task; %s\n", rtems_status_text (sc));
279                return;
280        }
281        rtems_task_suspend (RTEMS_SELF);
282}
283
284#define main            rtems_ttcp_main
285#define exit(code)      close(fd),rtems_ttcp_exit(code)
286#define bind            rtems_ttcp_bind
287#define read_timer      rtems_read_timer
288
289/*
290 * RTEMS/KA9Q code expects port numbers in host byte order!
291 */
292#define htons(x)        (x)
293
294#include "ttcp_orig/ttcp.c"
Note: See TracBrowser for help on using the repository browser.