1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX IPerf stack Component                                            */
16 /**                                                                       */
17 /**   This is a small demo of the high-performance NetX IPerf stack.      */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 #include   "tx_api.h"
22 #include   "nx_api.h"
23 
24 /* Define demo stack size.   */
25 #define                 NX_PACKET_POOL_SIZE     ((1536 + sizeof(NX_PACKET)) * 30)
26 #define                 DEMO_STACK_SIZE         2048
27 #define                 HTTP_STACK_SIZE         2048
28 #define                 IPERF_STACK_SIZE        2048
29 
30 /* Define the ThreadX and NetX object control blocks...  */
31 TX_THREAD               thread_0;
32 NX_PACKET_POOL          pool_0;
33 NX_IP                   ip_0;
34 
35 UCHAR                   *pointer;
36 UCHAR                   *http_stack;
37 UCHAR                   *iperf_stack;
38 #ifdef FEATURE_NX_IPV6
39 NXD_ADDRESS             ipv6_address;
40 #endif
41 
42 /* Define the counters used in the demo application...  */
43 ULONG                   error_counter;
44 
45 /* Define thread prototypes.  */
46 VOID    thread_0_entry(ULONG thread_input);
47 extern  VOID nx_iperf_entry(NX_PACKET_POOL *pool_ptr, NX_IP *ip_ptr, UCHAR* http_stack, ULONG http_stack_size, UCHAR *iperf_stack, ULONG iperf_stack_size);
48 
49 /***** Substitute your ethernet driver entry function here *********/
50 extern  VOID _nx_ram_network_driver(NX_IP_DRIVER*);
51 
52 
53 /* Define main entry point.  */
main()54 int main()
55 {
56 
57     /* Enter the ThreadX kernel.  */
58     tx_kernel_enter();
59 }
60 
61 
62 /* Define what the initial system looks like.  */
tx_application_define(void * first_unused_memory)63 void    tx_application_define(void *first_unused_memory)
64 {
65 
66 UINT    status;
67 
68     /* Setup the working pointer.  */
69     pointer = (UCHAR *) first_unused_memory;
70 
71     /* Initialize the NetX system.  */
72     nx_system_initialize();
73 
74     /* Create a packet pool.  */
75     status = nx_packet_pool_create(&pool_0, "NetX Main Packet Pool",
76                                    (1536 + sizeof(NX_PACKET)),
77                                    pointer, NX_PACKET_POOL_SIZE);
78     pointer = pointer + NX_PACKET_POOL_SIZE;
79 
80     /* Check for packet pool create errors.  */
81     if (status)
82         error_counter++;
83 
84     /* Create an IP instance.  */
85     status = nx_ip_create(&ip_0, "NetX IP Instance 0", IP_ADDRESS(192, 168, 100, 18), 0xFFFFFF00UL, &pool_0, _nx_ram_network_driver,
86                           pointer, 2048, 1);
87     pointer =  pointer + 2048;
88 
89     /* Check for IP create errors.  */
90     if (status)
91         error_counter++;
92 
93     /* Enable ARP and supply ARP cache memory for IP Instance 0.  */
94     status =  nx_arp_enable(&ip_0, (void *) pointer, 1024);
95     pointer = pointer + 1024;
96 
97     /* Check for ARP enable errors.  */
98     if (status)
99         error_counter++;
100 
101     /* Enable ICMP */
102     status = nx_icmp_enable(&ip_0);
103 
104     /* Check for ICMP enable errors.  */
105     if(status)
106         error_counter++;
107 
108     /* Enable UDP traffic.  */
109     status =  nx_udp_enable(&ip_0);
110 
111     /* Check for UDP enable errors.  */
112     if (status)
113         error_counter++;
114 
115     /* Enable TCP traffic.  */
116     status =  nx_tcp_enable(&ip_0);
117 
118     /* Check for TCP enable errors.  */
119     if (status)
120         error_counter++;
121 
122     /* Create the main thread.  */
123     tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
124                      pointer, DEMO_STACK_SIZE,
125                      4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
126     pointer =  pointer + DEMO_STACK_SIZE;
127 
128 #ifdef FEATURE_NX_IPV6
129     /* Set up the IPv6 address here. */
130     ipv6_address.nxd_ip_address.v6[3] = 0x3;
131     ipv6_address.nxd_ip_address.v6[2] = 0x0;
132     ipv6_address.nxd_ip_address.v6[1] = 0x0;
133     ipv6_address.nxd_ip_address.v6[0] = 0xfe800000;
134     ipv6_address.nxd_ip_version = NX_IP_VERSION_V6;
135 
136     /* Enable ICMPv6 services. */
137     status = nxd_icmp_enable(&ip_0);
138     if (status)
139         error_counter++;
140 
141     /* Enable IPv6 services. */
142     status = nxd_ipv6_enable(&ip_0);
143     if (status)
144         error_counter++;
145 
146     status = nxd_ipv6_address_set(&ip_0, 0, &ipv6_address, 10, NX_NULL);
147     if (status)
148         error_counter++;
149 #endif
150 }
151 
152 /* Define the test threads.  */
thread_0_entry(ULONG thread_input)153 void    thread_0_entry(ULONG thread_input)
154 {
155 #ifdef FEATURE_NX_IPV6
156     tx_thread_sleep(5 * NX_IP_PERIODIC_RATE);
157 #endif
158 
159     /* Set the HTTP stack and IPerf stack.  */
160     http_stack = pointer;
161     pointer += HTTP_STACK_SIZE;
162     iperf_stack = pointer;
163     pointer += IPERF_STACK_SIZE;
164 
165     /* Call entry function to start iperf test.  */
166     nx_iperf_entry(&pool_0, &ip_0, http_stack, HTTP_STACK_SIZE, iperf_stack, IPERF_STACK_SIZE);
167 }