1 /******************************************
2 * File: ifdma-test.c
3 * Purpose: test of infrastructure dma mode
4 **************************************************************
5 * FILE: ifdma-test.c
6 *
7 * DESCRIPTION: netapi user space transport
8 * library test application
9 *
10 * REVISION HISTORY: rev 0.0.1
11 *
12 * Copyright (c) Texas Instruments Incorporated 2010-2011
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *
18 * Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the
24 * distribution.
25 *
26 * Neither the name of Texas Instruments Incorporated nor the names of
27 * its contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 *****************************************/
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <string.h>
48 #include <signal.h>
49 #include <pthread.h>
50 #include <sched.h>
52 #include "trie.h"
53 #include "string.h"
54 #include "netapi.h"
55 #include "pktio.h"
56 #include "net_test.h"
57 #include <ti/drv/sa/salld.h>
60 void benchmarks(void);
61 static inline unsigned long netapi_timing_start(void)
62 {
63 volatile int vval;
64 //read clock
65 asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(vval));
66 return vval;
67 }
69 //turn this off to use packets received from Network instead of self generated ones
70 #define INTERNAL_PACKETS
71 static int scnt=0;
72 static int QUIT=0;
73 __thread int our_core;
76 #define IFDMA_FLOW_INDEX 33
77 #define IFMDA_HEAP_SIZE 200
78 #define IFDMA_MAX_NUM_HEAPS 2
79 #define IFDMA_MAX_HEAP_PKTS 128
80 //sig handler
81 void netTest_utilMySig(int x)
82 {
83 QUIT=1;
84 scnt+=1;
85 printf(">ifdma-test: recv'd signal %d cnt=%d\n",x,scnt);
86 if (scnt > 10) {printf(">ifdma-test: WARNING EXITING WITH PROPER SHUTDOWN, LUTS LEFT ACTIVE\n");exit(1);}
88 }
89 void recv_cb_net(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
90 PKTIO_METADATA_T meta[], int n_pkts,
91 uint64_t ts );
92 void recv_cb_consumer(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
93 PKTIO_METADATA_T meta[], int n_pkts,
94 uint64_t ts );
97 /*************debug********************/
98 void netTest_utilDumpDescr(unsigned long *p, int n)
99 {
100 printf("--------dump of descriptor %d %x\n", n, (int) p);
101 printf("> %x %x %x %x %x %x %x %x\n",p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7]);
102 printf("> %x %x %x %x %x %x %x %x\n",p[8],p[9],p[10],p[11],p[12],p[13],p[14],p[15]);
103 printf("-----------------------------\n");
104 }
105 void netTest_utilDumpHeader(unsigned long *p, int n, int a, int r)
106 {
107 printf("--------dump of header %d %x appID=%x flag1=%x\n", n, (int) p,a,r);
108 printf("> %x %x %x %x %x %x %x %x\n",p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7]);
109 printf("> %x %x %x %x %x %x %x %x\n",p[8],p[9],p[10],p[11],p[12],p[13],p[14],p[15]);
110 printf("> %x %x %x %x %x %x %x %x\n",p[16],p[17],p[18],p[19],p[20],p[21],p[22],p[23]);
111 printf("> %x %x %x %x %x %x %x %x\n",p[24],p[25],p[26],p[27],p[28],p[29],p[30],p[31]);
112 printf("-----------------------------\n");
113 }
114 /*****************************************/
122 /*******************************************
123 *************NETAPI OBJECTS***************
124 *****************************************/
125 static NETAPI_CFG_T our_netapi_default_cfg=
126 {
127 TUNE_NETAPI_PERM_MEM_SZ,
128 128, //start of packet offset for hw to place data on rx for default flow
129 TUNE_NETAPI_QM_CONFIG_MAX_DESC_NUM, //max number of descriptors in system
130 TUNE_NETAPI_NUM_GLOBAL_DESC, //total we will use
131 TUNE_NETAPI_DEFAULT_NUM_BUFFERS, //#descriptors+buffers in default heap
132 64, //#descriptors w/o buffers in default heap
133 TUNE_NETAPI_DEFAULT_BUFFER_SIZE+128+128, //size of buffers in default heap
134 128 , //tail room
135 256, //extra room
136 0,
137 NULL,
138 18,
139 0x2000
140 };
142 Pktlib_HeapHandle OurHeap; //default heap, used by producer
143 Pktlib_HeapHandle consumerHeap; //for consumer
144 PKTIO_HANDLE_T * rx_chan; //for consumer
145 PKTIO_HANDLE_T * tx_chan; // for producer
146 PKTIO_CFG_T rx_chan_cfg={PKTIO_RX|PKTIO_TX, PKTIO_GLOBAL, PKTIO_Q_ANY, 8};
147 PKTIO_CFG_T tx_chan_cfg={PKTIO_TX, PKTIO_GLOBAL|PKTIO_IFDMA, /*PKTIO_Q_ANY*/ 820, 8};
148 PKTIO_CFG_T netcp_rx_cfg={PKTIO_RX, PKTIO_NA, PKTIO_NA, 8};
149 PKTIO_HANDLE_T * netcp_rx_chan;
150 NETAPI_T netapi_handle;
151 NETCP_CFG_FLOW_HANDLE_T specialFlow; //for consumer. Producer uses the "flowid" in this handle as meta data when he sends data
153 PKTIO_CONTROL_T zap_channel_control={PKTIO_CLEAR, NULL};
155 NETCP_CFG_ROUTE_T test_route=
156 {
157 0,
158 NULL,
159 NULL,
160 0,
161 0,
162 0,
163 1
164 };
167 /*************************END NETAPI OBJECTS***********************/
169 static unsigned char all_mac[]={0,0,0,0,0,0};
172 #define IFDMA_PKT_LEN 100
174 //stats
175 int pkt_rx=0;
176 int pkt_tx=0;
177 int pkt_stall=0;
179 //***************************
180 //consumer thread
181 //**************************
182 void consumer_thread(int coreid)
183 {
184 int err;
185 int np;
186 cpu_set_t cpu_set;
188 CPU_ZERO( &cpu_set);
189 CPU_SET( 1, &cpu_set);
190 hplib_utilSetupThread(1, &cpu_set,hplib_spinLock_Type_LOL);
192 for(;!((volatile int)QUIT);)
193 {
194 np = netapi_pktioPoll(rx_chan,NULL,&err);
195 pkt_rx+=np;
196 }
197 printf("IFDMA-TEST: CONSUMER DONE %d packets received\n", pkt_rx);
198 }
201 //**********************************
202 //producer thread
203 //*********************************
204 void producer_thread(int coreid)
205 {
206 int err;
207 int i;
208 Ti_Pkt * tip;
209 unsigned char * pData;
210 int len;
211 PKTIO_METADATA_T meta = {PKTIO_META_IFDMA_TX,{0},0};
212 int np;
213 cpu_set_t cpu_set;
216 CPU_ZERO( &cpu_set);
217 CPU_SET( 2, &cpu_set);
218 hplib_utilSetupThread(2, &cpu_set,hplib_spinLock_Type_LOL);
220 #ifdef INTERNAL_PACKETS
221 //generate packets internally by allocating from OurHeap (the NETAPI
222 //default) and send to receiver via ifdma pktio channel
223 sleep(5);
224 for(i=0;!((volatile int) QUIT);i++)
225 {
226 tip=Pktlib_allocPacket(OurHeap,IFDMA_PKT_LEN);
227 if (!tip)
228 {
229 pkt_stall+=1;
230 sleep(1); //out of buffers, let consumer catch up
231 continue;
232 }
233 Pktlib_getDataBuffer(tip,&pData,&len);
234 sprintf(pData,"this is packet %d", pkt_tx);
235 Cppi_setData (Cppi_DescType_HOST, (Cppi_Desc *) tip, pData,IFDMA_PKT_LEN);
236 Pktlib_setPacketLen(tip,IFDMA_PKT_LEN);
237 meta.u.tx_ifdma_dest=((NETCP_CFG_FLOW_T*)specialFlow)->flowid;
238 netapi_pktioSend(tx_chan,tip,&meta,&err);
239 pkt_tx+=1;
240 if(!(pkt_tx % 64)) sched_yield(); //give consumer a chance
241 }
242 #else
243 //relay packets from network. recv_cb registered when we created
244 //netcp_rx_chan will do this relay via the ifdma pktio channel
245 //so we poll the default pktio channel for pkts from net
246 for(i=0;!((volatile) QUIT);i++)
247 {
248 np = netapi_pktioPoll(netcp_rx_chan,NULL,&err);
249 if (!np) sched_yield();
251 }
252 #endif
253 printf("IFDMA-TEST: PRODUCER DONE %d pkts sent (stalls=%d)\n", pkt_tx,pkt_stall);
254 }
257 //******************************
258 // main program
259 //*****************************
260 int main(int argc, char **argv)
261 {
262 int err,i;
263 int32_t errCode;
264 Pktlib_HeapIfTable* pPktifTable;
265 Pktlib_HeapCfg heapCfg;
266 long t1, t2 ;
267 cpu_set_t cpu_set;
270 //install signal handler for ^c
271 signal(SIGINT,netTest_utilMySig);
272 CPU_ZERO( &cpu_set);
273 CPU_SET( 0, &cpu_set);
274 hplib_utilSetupThread(0, &cpu_set, hplib_spinLock_Type_LOL);
277 /*******************************************/
278 /*************NETAPI STARTUP****************/
279 /*******************************************/
281 /* create netapi */
282 netapi_handle = netapi_init(NETAPI_SYS_MASTER, &our_netapi_default_cfg);
283 printf("main: returned from netapi_init\n");
285 /* Un-configure rules for execption packet handling */
286 /* open the main heap */
287 OurHeap = Pktlib_findHeapByName("netapi");
288 if (!OurHeap) {printf("findheapbyname fail\n"); exit(1);}
290 //create a receive queue for consumer
291 rx_chan=netapi_pktioCreate(netapi_handle,"ourrxq",(PKTIO_CB) recv_cb_consumer, &rx_chan_cfg,&err);
292 if (!rx_chan) {printf("pktio create failed err=%d\n",err); exit(1);}
295 #ifndef INTERNAL_PACKETS
296 //if we want to relay network packets, we create a handle to the
297 //default netcp receive queue here
298 netcp_rx_chan= netapi_pktioOpen(netapi_handle, NETCP_RX, (PKTIO_CB) recv_cb_net, &netcp_rx_cfg, &err);
299 if (!netcp_rx_chan) {printf("pktio open RX failed err=%d\n",err); exit(1);}
300 #endif
302 //********************************
303 //create a consumer heap
304 //**********************************
306 /* Initialize the heap configuration. */
307 memset ((void *)&heapCfg, 0, sizeof(Pktlib_HeapCfg));
309 pPktifTable = netapi_getPktlibIfTable();
311 /* Populate the heap configuration */
312 heapCfg.name = "netapi-consumer";
313 heapCfg.memRegion = netapi_getMemoryRegionId(netapi_handle);
314 heapCfg.sharedHeap = 1;
315 heapCfg.useStarvationQueue = 0;
316 heapCfg.dataBufferSize = TUNE_NETAPI_DEFAULT_BUFFER_SIZE;
317 heapCfg.numPkts = IFDMA_MAX_HEAP_PKTS;
318 heapCfg.numZeroBufferPackets= 0;
319 heapCfg.heapInterfaceTable.data_malloc = pPktifTable->data_malloc;
320 heapCfg.heapInterfaceTable.data_free = pPktifTable->data_free;
321 heapCfg.dataBufferPktThreshold = 0;
322 heapCfg.zeroBufferPktThreshold = 0;
323 consumerHeap = Pktlib_createHeap(&heapCfg, &errCode);
325 //by registering the heap, netapi will take care of
326 //cleaning it up @ shutdown..
327 netapi_registerHeap(netapi_handle, consumerHeap); //register heap.
329 //**************************************************
330 //create a FLOW for consumer RX. Note this is created
331 //in the QMSS (INFRASTRUCTURE) CPPI DMA ENGINE
332 // todo: flowindex should be passed in or got
333 // from resource manager
334 //*************************************************
335 {
336 Pktlib_HeapHandle heaps[2];
337 int sizes[2];
338 #define SPECIAL_SOP_OFF 0
339 NETCP_CFG_FLOW_CONFIG_T flow_config={IFDMA_FLOW_INDEX,
340 NETAPI_DMA_INFRASTRUCTURE,
341 SPECIAL_SOP_OFF,
342 NETAPI_FLOW_BLOCK };
343 heaps[0]= consumerHeap;
344 sizes[0]=IFMDA_HEAP_SIZE;
345 heaps[1]= consumerHeap;
346 sizes[1]=TUNE_NETAPI_DEFAULT_BUFFER_SIZE - SPECIAL_SOP_OFF;
347 flow_config.p_dest_q = rx_chan; // send pkts to rx_chan
349 //specialFlow handle will hold "magic" flowid that producer needs to
350 //include in his pkto_send meta data
351 specialFlow = netapi_netcpCfgAddFlow( netapi_handle,
352 IFDMA_MAX_NUM_HEAPS, //1 heap defined
353 heaps,
354 sizes,
355 &flow_config, //offset to start rx is 128
356 &err);
357 if (err) {printf("add flow failed\n", err); exit(1);}
358 }
360 //************************************************************
361 //create the IDMA channel: this is used by producer to either
362 //send generated packets or relay received packets from net
363 //***************************************************************
364 tx_chan=netapi_pktioCreate(netapi_handle,"ourtxq",NULL, &tx_chan_cfg,&err);
365 if (!tx_chan) {printf("pktio create failed err=%d\n",err); exit(1);}
367 #ifndef INTERNAL_PACKETS
368 //if we want to relay packets, creaate a simple netcp rule
369 //to get a lot of packets
370 netapi_netcpCfgCreateMacInterface(
371 netapi_handle,
372 &all_mac[0],
373 NULL,
374 0,
375 0,
376 (NETCP_CFG_ROUTE_HANDLE_T) NULL,
377 (NETCP_CFG_VLAN_T ) NULL , //future
378 1,
379 &err);
380 #endif
382 /*********************************************/
383 /*****************end NETAPI STARTUP**********/
384 /*********************************************/
386 #if 1
387 //sonme benchmarks
388 benchmarks();
389 #endif
390 //**************************************
391 //Create a consumer and producer thread
392 //***************************************
393 {
394 pthread_t *thrs;
395 int procs =2;
396 int c;
397 thrs = malloc( sizeof( pthread_t ) * procs );
398 if (thrs == NULL)
399 {
400 perror( "malloc" );
401 return -1;
402 }
403 printf( "ifdma-test: Starting %d threads...\n", procs );
405 if (pthread_create( &thrs[0], NULL, (void*)consumer_thread,
406 (void *)0 ))
407 {
408 perror( "pthread_create" );
409 exit(1);
410 }
411 if (pthread_create( &thrs[1], NULL, (void*)producer_thread,
412 (void *)1 ))
413 {
414 perror( "pthread_create" );
415 exit(1);
416 }
417 //this thread of execution (main) now just waits on user input
418 for(;;)
419 {
420 printf(">");
421 c=getchar();
422 if (c=='q') {QUIT=1;break;}
423 else if (c=='s') printf(">IFDMA-TEST STATS: %d sent, %d received stall=%d \n",pkt_tx, pkt_rx,pkt_stall);
424 else if (c=='h') printf("> 'q' to quit, 's' for stats, 'h' for help\n");
425 }
427 //wait for completion
428 printf("main task now pending on thread completion\n");
429 for (i = 0; i < procs; i++)
430 pthread_join( thrs[i], NULL );
432 free( thrs );
434 }
436 /*************************************************
437 ************CLEAN UP****************************
438 ************************************************/
439 #ifndef INTERNAL_PACKETS
440 //get rid of rule, in the case that we are relaying packets
441 //also close our netcp rx channel
442 netapi_netcpCfgDelMac(netapi_handle,0,&err);
443 netapi_pktioClose(netcp_rx_chan,&err);
444 #endif
446 //close pktio channels we opened
447 netapi_pktioDelete(tx_chan ,&err);
448 netapi_pktioDelete(rx_chan ,&err);
450 //close flow
451 netapi_netcpCfgDelFlow(netapi_handle, specialFlow, &err);
453 //done
454 netapi_shutdown(netapi_handle);
457 //!finished!
458 }
461 //receive callback for packets from net (for consumer)
462 // this is used for case where we want to relay packets from
463 // network, instead of internally generating them
464 void recv_cb_net(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
465 PKTIO_METADATA_T meta[], int n_pkts,
466 uint64_t ts )
467 {
468 int i;
469 PKTIO_METADATA_T meta2 = {PKTIO_META_TX,{0},0};
470 Ti_Pkt * tip;
471 int err;
473 for(i=0;i<n_pkts;i++)
474 {
475 tip = p_recv[i];
476 meta2.u.tx_ifdma_dest=((NETCP_CFG_FLOW_T*)specialFlow)->flowid;
477 netapi_pktioSend(tx_chan,tip,&meta2,&err);
478 pkt_tx+=1;
479 if(!(pkt_tx % 128)) sched_yield(); //give consumer a chance
480 }
483 }
486 //receive callback for consumer (registered when we create pktio channel)
487 void recv_cb_consumer(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
488 PKTIO_METADATA_T meta[], int n_pkts,
489 uint64_t ts )
490 {
491 int i;
492 int len;
493 Ti_Pkt * tip;
494 unsigned int templen;
495 char * p_pkt;
497 for(i=0;i<n_pkts;i++)
498 {
499 tip = p_recv[i];
500 Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
501 len = Pktlib_getPacketLen(tip);
502 Pktlib_freePacket((Ti_Pkt*)tip);
503 }
504 return;
505 }
510 //SOME BENCHMARKS
511 //sonme benchmarks
512 void benchmarks(void)
513 {
514 int i,j;
515 unsigned long v1pop;
516 unsigned long v2pop;
517 unsigned long v1push;
518 unsigned long v2push;
519 unsigned long v1read;
520 unsigned long v2read;
521 unsigned long v1write;
522 unsigned long v2write;
523 unsigned long v1read2;
524 unsigned long v2read2;
525 #define N 100
526 Ti_Pkt pkts[N];
527 unsigned char * p_pkt;
528 int len;
529 int sum=0;
530 int sum2=0;
531 char *p=(char *) malloc(1000);
533 //queue pop
534 v1pop=netapi_timing_start();
535 for(i=0;i<N;i++) pkts[i]= Pktlib_allocPacket(OurHeap,1000);
536 v2pop = netapi_timing_start();
538 //write access
539 Pktlib_getDataBuffer(pkts[0],(uint8_t**)&p_pkt,&len);
540 v1write=netapi_timing_start();
541 for(i=0;i<1000;i++) p_pkt[i]=i;
542 v2write=netapi_timing_start();
544 // access
546 v1read=netapi_timing_start();
547 for(j=0;j<10;j++)
548 for(i=0;i<1000;i++) sum+=p_pkt[i];
549 v2read=netapi_timing_start();
551 // access (from malloc)
552 v1read2=netapi_timing_start();
553 for(i=0;i<1000;i++) sum2+=p[i];
554 v2read2=netapi_timing_start();
556 //queue push
557 v1push=netapi_timing_start();
558 for(i=0;i<N;i++) Pktlib_freePacket(pkts[i]);
559 v2push = netapi_timing_start();
561 //resutls
562 printf("allocs= %d free=%d write=%d read=%d read-malloc=%d (sum=%d %d)\n",
563 (v2pop-v1pop)/N, (v2push-v1push)/N, (v2write-v1write)/1000, (v2read-v1read)/10000,
564 (v2read2-v1read2)/1000,sum,sum2);
566 }