1 /* This NetX test concentrates on the RARP dynamic entry operation.  */
2 
3 #include   "tx_api.h"
4 #include   "nx_api.h"
5 #include   "nx_rarp.h"
6 #include   "nx_ram_network_driver_test_1500.h"
7 
8 extern void    test_control_return(UINT status);
9 
10 #if !defined(NX_DISABLE_RARP_INFO) && (NX_MAX_PHYSICAL_INTERFACES > 1) && !defined(NX_DISABLE_IPV4)
11 
12 #define     DEMO_STACK_SIZE    2048
13 #define     TEST_INTERFACE     0
14 
15 
16 /* Define the ThreadX and NetX object control blocks...  */
17 
18 static TX_THREAD               ntest_0;
19 
20 static NX_PACKET_POOL          pool_0;
21 static NX_IP                   ip_0;
22 
23 
24 
25 /* Define the counters used in the test application...  */
26 
27 static ULONG                   error_counter;
28 static UINT                    response_type;
29 
30 
31 /* Define thread prototypes.  */
32 
33 static void    ntest_0_entry(ULONG thread_input);
34 extern void    test_control_return(UINT status);
35 extern UINT    (*advanced_packet_process_callback)(NX_IP *ip_ptr, NX_PACKET *packet_ptr, UINT *operation_ptr, UINT *delay_ptr);
36 static UINT    my_rarp_packet_process(NX_IP *ip_ptr, NX_PACKET *packet_ptr, UINT *operation_ptr, UINT *delay_ptr);
37 static VOID    ip_address_change_notify(NX_IP *ip_ptr, VOID *additional_info);
38 static VOID    fake_rarp_response_packet(NX_PACKET **my_packet, UINT type);
39 extern void    _nx_ram_network_driver_256(struct NX_IP_DRIVER_STRUCT *driver_req);
40 void           _nx_ram_network_driver_receive(NX_IP *ip_ptr, NX_PACKET *packet_ptr, UINT interface_instance_id);
41 
42 #define NX_ETHERNET_RARP    0x8035
43 #define NX_ETHERNET_SIZE    14
44 
45 /* Define what the initial system looks like.  */
46 
47 #ifdef CTEST
test_application_define(void * first_unused_memory)48 VOID test_application_define(void *first_unused_memory)
49 #else
50 void    netx_rarp_basic_processing_test_application_define(void *first_unused_memory)
51 #endif
52 {
53 
54 CHAR    *pointer;
55 UINT    status;
56 
57 
58     /* Setup the working pointer.  */
59     pointer =  (CHAR *) first_unused_memory;
60 
61     error_counter =  0;
62 
63     /* Create the main thread.  */
64     tx_thread_create(&ntest_0, "thread 0", ntest_0_entry, 0,
65             pointer, DEMO_STACK_SIZE,
66             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
67     pointer =  pointer + DEMO_STACK_SIZE;
68 
69     /* Initialize the NetX system.  */
70     nx_system_initialize();
71 
72     /* Create a packet pool.  */
73     status =  nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", 256, pointer, 8192);
74     pointer = pointer + 8192;
75 
76     if (status)
77         error_counter++;
78 
79     /* Create an IP instance.  */
80     status = nx_ip_create(&ip_0, "NetX IP Instance 0", 0, 0xFFFFFF00UL, &pool_0, _nx_ram_network_driver_256,
81                     pointer, 2048, 1);
82     pointer =  pointer + 2048;
83 
84     if (status)
85         error_counter++;
86 
87     /* Enable ARP and supply ARP cache memory for IP Instance 0.  */
88     status =  nx_arp_enable(&ip_0, (void *) pointer, 1024);
89     pointer = pointer + 1024;
90     if (status)
91         error_counter++;
92 
93     /* Attach the 2nd interface IP address so it dosn't need RARP service. */
94     status = nx_ip_interface_attach(&ip_0, "2nd interface", IP_ADDRESS(4, 3, 2, 111), 0xFF000000, _nx_ram_network_driver_256);
95     if(status != NX_SUCCESS)
96     {
97         error_counter++;
98     }
99 
100 #if (NX_MAX_PHYSICAL_INTERFACES > 2) && defined(__PRODUCT_NETXDUO__)
101     /* Attach the 3rd interface IP address so it dosn't need RARP service. */
102     status = nx_ip_interface_attach(&ip_0, "3rd interface", 0, 0xFFFFFF00, _nx_ram_network_driver_256);
103     if(status != NX_SUCCESS)
104     {
105         error_counter++;
106     }
107 #endif
108 }
109 
110 
111 /* Define the test threads.  */
112 
ntest_0_entry(ULONG thread_input)113 static void    ntest_0_entry(ULONG thread_input)
114 {
115 
116 UINT        status;
117 ULONG       actual_status;
118 ULONG       ip_address, network_mask;
119 ULONG       rarp_requests_sent, rarp_responses_received, rarp_invalid_messages;
120 
121 
122     /* Print out some test information banners.  */
123     printf("NetX Test:   RARP Basic Processing Test................................");
124 
125     /* Check for earlier error.  */
126     if (error_counter)
127     {
128 
129         printf("ERROR!\n");
130         test_control_return(1);
131     }
132 
133 #if (NX_MAX_PHYSICAL_INTERFACES > 2) && defined(__PRODUCT_NETXDUO__)
134     /* Disable address mapping. */
135     nx_ip_interface_address_mapping_configure(&ip_0, 2, NX_FALSE);
136 #endif
137 
138    /* Enable RARP for IP Instance 0.  */
139     status  =  nx_rarp_enable(&ip_0);
140     if (status)
141     {
142 
143         printf("ERROR!\n");
144         test_control_return(1);
145     }
146 
147     /* Enable the rarp feature again.  */
148     status = nx_rarp_enable(&ip_0);
149 
150     /* Check status...  */
151     if (status != NX_ALREADY_ENABLED)
152     {
153 
154         printf("ERROR!\n");
155         test_control_return(1);
156     }
157 
158     /* Register IP address change callback. */
159     status = nx_ip_address_change_notify(&ip_0, ip_address_change_notify, NX_NULL);
160 
161     /* Check for error */
162     if (status)
163     {
164 
165         printf("ERROR!\n");
166         test_control_return(1);
167     }
168 
169     /* Deal the packet with my routing.  */
170     advanced_packet_process_callback = my_rarp_packet_process;
171 
172     /* Set the response type as 0 .  */
173     response_type = 0;
174 
175     /* Waiting NetX sending the RARP request to get the address.  */
176     tx_thread_sleep(2 * NX_IP_PERIODIC_RATE);
177 
178     /* Ensure the IP instance has been initialized.  */
179     status =  nx_ip_status_check(&ip_0, NX_IP_ADDRESS_RESOLVED, &actual_status, NX_IP_PERIODIC_RATE);
180 
181     /* Check status...  */
182     if (status == NX_SUCCESS)
183     {
184 
185         printf("ERROR!\n");
186         test_control_return(1);
187     }
188 
189    /* Enable RARP for IP Instance 0.  */
190     status  =  nx_rarp_enable(&ip_0);
191     if (status)
192     {
193         printf("ERROR!\n");
194         test_control_return(1);
195     }
196 
197     /* Set the response type as 0 .  */
198     response_type = 1;
199 
200     /* Waiting NetX sending the RARP request to get the address.  */
201     tx_thread_sleep(2 * NX_IP_PERIODIC_RATE);
202 
203     /* Ensure the IP instance has been initialized.  */
204     status =  nx_ip_status_check(&ip_0, NX_IP_ADDRESS_RESOLVED, &actual_status, NX_IP_PERIODIC_RATE);
205 
206     /* Check status...  */
207     if (status != NX_SUCCESS)
208     {
209 
210         printf("ERROR!\n");
211         test_control_return(1);
212     }
213 
214     /* Get the IP instance address.  */
215     status = nx_ip_interface_address_get(&ip_0, 0, &ip_address, &network_mask);
216 
217     /* Check status...  */
218     if((status != NX_SUCCESS) || (ip_address != IP_ADDRESS(1,2,3,4)) || (network_mask != 0xFFFFFF00UL))
219     {
220 
221         printf("ERROR!\n");
222         test_control_return(1);
223     }
224 
225     /* Get information about RARP.  */
226     status =  nx_rarp_info_get(&ip_0, NX_NULL, NX_NULL, NX_NULL);
227 
228     /* Check status...  */
229     if (status != NX_SUCCESS)
230     {
231 
232         printf("ERROR!\n");
233         test_control_return(1);
234     }
235 
236     status =  nx_rarp_info_get(&ip_0, &rarp_requests_sent, &rarp_responses_received, &rarp_invalid_messages);
237     /* Check status...  */
238     if (status != NX_SUCCESS)
239     {
240 
241         printf("ERROR!\n");
242         test_control_return(1);
243     }
244 
245 #ifndef NX_DISABLE_ARP_INFO
246     if((rarp_requests_sent != 2) || (rarp_responses_received != 1) || (rarp_invalid_messages != 3))
247     {
248         printf("ERROR!\n");
249         test_control_return(1);
250     }
251 #endif
252 
253 #ifdef __PRODUCT_NETXDUO__
254     /* Tested the nx_rarp_enable feature after IP address resloved.  */
255 
256     /* Enable the rarp feature again.  */
257     status = nx_rarp_enable(&ip_0);
258 
259     /* Check status...  */
260     if (status != NX_IP_ADDRESS_ERROR)
261     {
262 
263         printf("ERROR!\n");
264         test_control_return(1);
265     }
266 
267     /* Disable the rarp feature.  */
268     status =  nx_rarp_disable(&ip_0);
269 
270     if (status != NX_NOT_ENABLED)
271     {
272 
273         printf("ERROR!\n");
274         test_control_return(1);
275     }
276 #endif
277 
278     /* Check status.  */
279     if (error_counter)
280     {
281 
282         printf("ERROR!\n");
283         test_control_return(1);
284     }
285     else
286     {
287 
288         printf("SUCCESS!\n");
289         test_control_return(0);
290     }
291 }
292 
my_rarp_packet_process(NX_IP * ip_ptr,NX_PACKET * packet_ptr,UINT * operation_ptr,UINT * delay_ptr)293 static UINT    my_rarp_packet_process(NX_IP *ip_ptr, NX_PACKET *packet_ptr, UINT *operation_ptr, UINT *delay_ptr)
294 {
295 UINT        status;
296 NX_PACKET   *fake_packet_1;
297 NX_PACKET   *fake_packet_2;
298 NX_PACKET   *fake_packet_3;
299 
300     /* Drop the packet. */
301     *operation_ptr = NX_RAMDRIVER_OP_DROP;
302 
303     /* Check the response type.  */
304     if (response_type == 0)
305     {
306 
307         /* Disable the rarp feature.  */
308         status =  nx_rarp_disable(&ip_0);
309 
310         /* Check status.  */
311         if (status)
312             error_counter++;
313 
314         /* Fake the response packet.  */
315         fake_rarp_response_packet(&fake_packet_1, NX_RARP_OPTION_RESPONSE);
316 
317         /* Call the driver to receive this packet.  */
318         _nx_ram_network_driver_receive(&ip_0, fake_packet_1, 0);
319     }
320     else if (response_type == 1)
321     {
322 
323         /* Fake a request packet.  */
324         fake_rarp_response_packet(&fake_packet_1, NX_RARP_OPTION_REQUEST);
325 
326         /* Call the driver to receive this packet.  */
327         _nx_ram_network_driver_receive(&ip_0, fake_packet_1, 0);
328 
329         /* Fake a invalid packet.  */
330         fake_rarp_response_packet(&fake_packet_2, 0);
331 
332         /* Call the driver to receive this packet.  */
333         _nx_ram_network_driver_receive(&ip_0, fake_packet_2, 0);
334 
335         /* Fake the response packet.  */
336         fake_rarp_response_packet(&fake_packet_3, NX_RARP_OPTION_RESPONSE);
337 
338         /* Call the driver to receive this packet.  */
339         _nx_ram_network_driver_receive(&ip_0, fake_packet_3, 0);
340     }
341 
342     return NX_TRUE;
343 }
fake_rarp_response_packet(NX_PACKET ** packet_ptr,UINT type)344 static VOID fake_rarp_response_packet(NX_PACKET **packet_ptr, UINT type)
345 {
346 UINT        status;
347 NX_PACKET   *my_packet;
348 ULONG       *ethernet_frame_ptr;
349 
350     /* Allocate a packet.  */
351     status =  nx_packet_allocate(&pool_0, &my_packet, NX_PHYSICAL_HEADER, NX_WAIT_FOREVER);
352 
353     /* Check status.  */
354     if (status != NX_SUCCESS)
355         error_counter++;
356 
357     /* Create a fake RARP response packet to assign address(1.2.3.4)!  */
358     my_packet -> nx_packet_prepend_ptr[0] =   0x00;     /* Ethernet header  */
359     my_packet -> nx_packet_prepend_ptr[1] =   0x01;
360     my_packet -> nx_packet_prepend_ptr[2] =   0x80;     /* IP address  */
361     my_packet -> nx_packet_prepend_ptr[3] =   0x00;
362     my_packet -> nx_packet_prepend_ptr[4] =   0x06;     /* Hardware address size */
363     my_packet -> nx_packet_prepend_ptr[5] =   0x04;     /* IP address size  */
364 
365     /* Check the type.  */
366     if (type == NX_RARP_OPTION_REQUEST)
367     {
368         my_packet -> nx_packet_prepend_ptr[6] =   0x00;     /* RARP request  */
369         my_packet -> nx_packet_prepend_ptr[7] =   0x03;
370     }
371     else if (type == NX_RARP_OPTION_RESPONSE)
372     {
373         my_packet -> nx_packet_prepend_ptr[6] =   0x00;     /* RARP response  */
374         my_packet -> nx_packet_prepend_ptr[7] =   0x04;
375     }
376     else
377     {
378         my_packet -> nx_packet_prepend_ptr[6] =   0x00;     /* Invalid RARP type  */
379         my_packet -> nx_packet_prepend_ptr[7] =   0x00;
380     }
381     my_packet -> nx_packet_prepend_ptr[8] =   0x11;     /* Sender Ethernet hardware address  */
382     my_packet -> nx_packet_prepend_ptr[9] =   0x22;
383     my_packet -> nx_packet_prepend_ptr[10] =  0x33;
384     my_packet -> nx_packet_prepend_ptr[11] =  0x44;
385     my_packet -> nx_packet_prepend_ptr[12] =  0x55;
386     my_packet -> nx_packet_prepend_ptr[13] =  0x67;
387     my_packet -> nx_packet_prepend_ptr[14] =  0x01;     /* Sender IP address  */
388     my_packet -> nx_packet_prepend_ptr[15] =  0x02;
389     my_packet -> nx_packet_prepend_ptr[16] =  0x03;
390     my_packet -> nx_packet_prepend_ptr[17] =  0x87;
391     my_packet -> nx_packet_prepend_ptr[18] =  0x11;     /* Target hardware address  */
392     my_packet -> nx_packet_prepend_ptr[19] =  0x22;
393     my_packet -> nx_packet_prepend_ptr[20] =  0x33;
394     my_packet -> nx_packet_prepend_ptr[21] =  0x44;
395     my_packet -> nx_packet_prepend_ptr[22] =  0x55;
396     my_packet -> nx_packet_prepend_ptr[23] =  0x66;
397     my_packet -> nx_packet_prepend_ptr[24] =  0x01;     /* Target IP address  */
398     my_packet -> nx_packet_prepend_ptr[25] =  0x02;
399     my_packet -> nx_packet_prepend_ptr[26] =  0x03;
400     my_packet -> nx_packet_prepend_ptr[27] =  0x04;
401 
402     /* Adjust the write pointer.  */
403     my_packet -> nx_packet_length =  28;
404     my_packet -> nx_packet_append_ptr =  my_packet -> nx_packet_prepend_ptr + 28;
405 
406     /* Fake a receive RARP packet.  */
407     my_packet -> nx_packet_ip_interface = &ip_0.nx_ip_interface[TEST_INTERFACE];
408 
409     /* Adjust the prepend pointer.  */
410     my_packet -> nx_packet_prepend_ptr =  my_packet -> nx_packet_prepend_ptr - NX_ETHERNET_SIZE;
411 
412     /* Adjust the packet length.  */
413     my_packet -> nx_packet_length =  my_packet -> nx_packet_length + NX_ETHERNET_SIZE;
414 
415     /* Setup the ethernet frame pointer to build the ethernet frame.  Backup another 2
416     bytes to get 32-bit word alignment.  */
417     ethernet_frame_ptr =  (ULONG *) (my_packet -> nx_packet_prepend_ptr - 2);
418 
419     /* Build the ethernet frame.  */
420     *ethernet_frame_ptr     =  0x00000011;
421     *(ethernet_frame_ptr+1) = 0x22334456;
422     *(ethernet_frame_ptr+2) =  (0x00000011 << 16) | (0x22334457 >> 16);
423     *(ethernet_frame_ptr+3) =  ((0x22334457 & 0xFFFF) << 16);
424     *(ethernet_frame_ptr+3) |= NX_ETHERNET_RARP;
425 
426     /* Endian swapping if NX_LITTLE_ENDIAN is defined.  */
427     NX_CHANGE_ULONG_ENDIAN(*(ethernet_frame_ptr));
428     NX_CHANGE_ULONG_ENDIAN(*(ethernet_frame_ptr+1));
429     NX_CHANGE_ULONG_ENDIAN(*(ethernet_frame_ptr+2));
430     NX_CHANGE_ULONG_ENDIAN(*(ethernet_frame_ptr+3));
431 
432     /* Set the packet pointer.  */
433     *packet_ptr = my_packet;
434 }
ip_address_change_notify(NX_IP * ip_ptr,VOID * additional_info)435 static VOID ip_address_change_notify(NX_IP *ip_ptr, VOID *additional_info)
436 {
437 }
438 #else
439 
440 #ifdef CTEST
test_application_define(void * first_unused_memory)441 VOID test_application_define(void *first_unused_memory)
442 #else
443 void           netx_rarp_basic_processing_test_application_define(void *first_unused_memory)
444 #endif
445 {
446 
447     /* Print out some test information banners.  */
448     printf("NetX Test:   RARP Basic Processing Test................................N/A\n");
449 
450     test_control_return(3);
451 
452 }
453 #endif
454