b5fbb071296d725b907a899a60d66aa4072009db
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 };
140 Pktlib_HeapHandle OurHeap; //default heap, used by producer
141 Pktlib_HeapHandle consumerHeap; //for consumer
142 PKTIO_HANDLE_T * rx_chan; //for consumer
143 PKTIO_HANDLE_T * tx_chan; // for producer
144 PKTIO_CFG_T rx_chan_cfg={PKTIO_RX|PKTIO_TX, PKTIO_GLOBAL, PKTIO_Q_ANY, 8};
145 PKTIO_CFG_T tx_chan_cfg={PKTIO_TX, PKTIO_GLOBAL|PKTIO_IFDMA, /*PKTIO_Q_ANY*/ 820, 8};
146 PKTIO_CFG_T netcp_rx_cfg={PKTIO_RX, PKTIO_NA, PKTIO_NA, 8};
147 PKTIO_HANDLE_T * netcp_rx_chan;
148 NETAPI_T netapi_handle;
149 NETCP_CFG_FLOW_HANDLE_T specialFlow; //for consumer. Producer uses the "flowid" in this handle as meta data when he sends data
151 PKTIO_CONTROL_T zap_channel_control={PKTIO_CLEAR, NULL};
153 NETCP_CFG_ROUTE_T test_route=
154 {
155 0,
156 NULL,
157 NULL,
158 0//* to be filled in
159 };
162 /*************************END NETAPI OBJECTS***********************/
164 static unsigned char all_mac[]={0,0,0,0,0,0};
167 #define IFDMA_PKT_LEN 100
169 //stats
170 int pkt_rx=0;
171 int pkt_tx=0;
172 int pkt_stall=0;
174 //***************************
175 //consumer thread
176 //**************************
177 void consumer_thread(int coreid)
178 {
179 int err;
180 int np;
181 cpu_set_t cpu_set;
183 CPU_ZERO( &cpu_set);
184 CPU_SET( 1, &cpu_set);
185 hplib_utilSetupThread(1, &cpu_set,hplib_spinLock_Type_LOL);
187 for(;!((volatile)QUIT);)
188 {
189 np = netapi_pktioPoll(rx_chan,NULL,&err);
190 pkt_rx+=np;
191 }
192 printf("IFDMA-TEST: CONSUMER DONE %d packets received\n", pkt_rx);
193 }
196 //**********************************
197 //producer thread
198 //*********************************
199 void producer_thread(int coreid)
200 {
201 int err;
202 int i;
203 Ti_Pkt * tip;
204 unsigned char * pData;
205 int len;
206 PKTIO_METADATA_T meta = {PKTIO_META_IFDMA_TX,{0},0};
207 int np;
208 cpu_set_t cpu_set;
211 CPU_ZERO( &cpu_set);
212 CPU_SET( 2, &cpu_set);
213 hplib_utilSetupThread(2, &cpu_set,hplib_spinLock_Type_LOL);
215 #ifdef INTERNAL_PACKETS
216 //generate packets internally by allocating from OurHeap (the NETAPI
217 //default) and send to receiver via ifdma pktio channel
218 sleep(5);
219 for(i=0;!((volatile) QUIT);i++)
220 {
221 tip=Pktlib_allocPacket(OurHeap,IFDMA_PKT_LEN);
222 if (!tip)
223 {
224 pkt_stall+=1;
225 sleep(1); //out of buffers, let consumer catch up
226 continue;
227 }
228 Pktlib_getDataBuffer(tip,&pData,&len);
229 sprintf(pData,"this is packet %d", pkt_tx);
230 Cppi_setData (Cppi_DescType_HOST, (Cppi_Desc *) tip, pData,IFDMA_PKT_LEN);
231 Pktlib_setPacketLen(tip,IFDMA_PKT_LEN);
232 meta.u.tx_ifdma_dest=((NETCP_CFG_FLOW_T*)specialFlow)->flowid;
233 netapi_pktioSend(tx_chan,tip,&meta,&err);
234 pkt_tx+=1;
235 if(!(pkt_tx % 64)) sched_yield(); //give consumer a chance
236 }
237 #else
238 //relay packets from network. recv_cb registered when we created
239 //netcp_rx_chan will do this relay via the ifdma pktio channel
240 //so we poll the default pktio channel for pkts from net
241 for(i=0;!((volatile) QUIT);i++)
242 {
243 np = netapi_pktioPoll(netcp_rx_chan,NULL,&err);
244 if (!np) sched_yield();
246 }
247 #endif
248 printf("IFDMA-TEST: PRODUCER DONE %d pkts sent (stalls=%d)\n", pkt_tx,pkt_stall);
249 }
252 //******************************
253 // main program
254 //*****************************
255 int main(int argc, char **argv)
256 {
257 int err,i;
258 int32_t errCode;
259 Pktlib_HeapIfTable* pPktifTable;
260 Pktlib_HeapCfg heapCfg;
261 long t1, t2 ;
262 cpu_set_t cpu_set;
265 //install signal handler for ^c
266 signal(SIGINT,netTest_utilMySig);
267 CPU_ZERO( &cpu_set);
268 CPU_SET( 0, &cpu_set);
269 hplib_utilSetupThread(0, &cpu_set, hplib_spinLock_Type_LOL);
272 /*******************************************/
273 /*************NETAPI STARTUP****************/
274 /*******************************************/
276 /* create netapi */
277 netapi_handle = netapi_init(NETAPI_SYS_MASTER, &our_netapi_default_cfg);
278 printf("main: returned from netapi_init\n");
280 /* Un-configure rules for execption packet handling */
281 netapi_netcpCfgExceptions(netapi_handle,
282 NETCP_CFG_ALL_EXCEPTIONS,
283 NETCP_CFG_ACTION_DISCARD,
284 (NETCP_CFG_ROUTE_HANDLE_T) NULL);
285 /* open the main heap */
286 OurHeap = Pktlib_findHeapByName("netapi");
287 if (!OurHeap) {printf("findheapbyname fail\n"); exit(1);}
289 //create a receive queue for consumer
290 rx_chan=netapi_pktioCreate(netapi_handle,"ourrxq",(PKTIO_CB) recv_cb_consumer, &rx_chan_cfg,&err);
291 if (!rx_chan) {printf("pktio create failed err=%d\n",err); exit(1);}
294 #ifndef INTERNAL_PACKETS
295 //if we want to relay network packets, we create a handle to the
296 //default netcp receive queue here
297 netcp_rx_chan= netapi_pktioOpen(netapi_handle, NETCP_RX, (PKTIO_CB) recv_cb_net, &netcp_rx_cfg, &err);
298 if (!netcp_rx_chan) {printf("pktio open RX failed err=%d\n",err); exit(1);}
299 #endif
301 //********************************
302 //create a consumer heap
303 //**********************************
305 /* Initialize the heap configuration. */
306 memset ((void *)&heapCfg, 0, sizeof(Pktlib_HeapCfg));
308 pPktifTable = netapi_getPktlibIfTable();
310 /* Populate the heap configuration */
311 heapCfg.name = "netapi-consumer";
312 heapCfg.memRegion = NETAPI_GLOBAL_REGION;
313 heapCfg.sharedHeap = 1;
314 heapCfg.useStarvationQueue = 0;
315 heapCfg.dataBufferSize = TUNE_NETAPI_DEFAULT_BUFFER_SIZE;
316 heapCfg.numPkts = IFDMA_MAX_HEAP_PKTS;
317 heapCfg.numZeroBufferPackets= 0;
318 heapCfg.heapInterfaceTable.data_malloc = pPktifTable->data_malloc;
319 heapCfg.heapInterfaceTable.data_free = pPktifTable->data_free;
320 heapCfg.dataBufferPktThreshold = 0;
321 heapCfg.zeroBufferPktThreshold = 0;
322 consumerHeap = Pktlib_createHeap(&heapCfg, &errCode);
324 //by registering the heap, netapi will take care of
325 //cleaning it up @ shutdown..
326 netapi_registerHeap(netapi_handle, consumerHeap); //register heap.
328 //**************************************************
329 //create a FLOW for consumer RX. Note this is created
330 //in the QMSS (INFRASTRUCTURE) CPPI DMA ENGINE
331 // todo: flowindex should be passed in or got
332 // from resource manager
333 //*************************************************
334 {
335 Pktlib_HeapHandle heaps[2];
336 int sizes[2];
337 #define SPECIAL_SOP_OFF 0
338 NETCP_CFG_FLOW_CONFIG_T flow_config={IFDMA_FLOW_INDEX,
339 NETAPI_DMA_INFRASTRUCTURE,
340 SPECIAL_SOP_OFF,
341 NETAPI_FLOW_BLOCK };
342 heaps[0]= consumerHeap;
343 sizes[0]=IFMDA_HEAP_SIZE;
344 heaps[1]= consumerHeap;
345 sizes[1]=TUNE_NETAPI_DEFAULT_BUFFER_SIZE - SPECIAL_SOP_OFF;
346 flow_config.p_dest_q = rx_chan; // send pkts to rx_chan
348 //specialFlow handle will hold "magic" flowid that producer needs to
349 //include in his pkto_send meta data
350 specialFlow = netapi_netcpCfgAddFlow( netapi_handle,
351 IFDMA_MAX_NUM_HEAPS, //1 heap defined
352 heaps,
353 sizes,
354 &flow_config, //offset to start rx is 128
355 &err);
356 if (err) {printf("add flow failed\n", err); exit(1);}
357 }
359 //************************************************************
360 //create the IDMA channel: this is used by producer to either
361 //send generated packets or relay received packets from net
362 //***************************************************************
363 tx_chan=netapi_pktioCreate(netapi_handle,"ourtxq",NULL, &tx_chan_cfg,&err);
364 if (!tx_chan) {printf("pktio create failed err=%d\n",err); exit(1);}
366 #ifndef INTERNAL_PACKETS
367 //if we want to relay packets, creaate a simple netcp rule
368 //to get a lot of packets
369 netapi_netcpCfgCreateMacInterface(
370 netapi_handle,
371 &all_mac[0],
372 NULL,
373 0,
374 0,
375 (NETCP_CFG_ROUTE_HANDLE_T) NULL,
376 (NETCP_CFG_VLAN_T ) NULL , //future
377 1,
378 &err);
379 #endif
381 /*********************************************/
382 /*****************end NETAPI STARTUP**********/
383 /*********************************************/
385 #if 1
386 //sonme benchmarks
387 benchmarks();
388 #endif
389 //**************************************
390 //Create a consumer and producer thread
391 //***************************************
392 {
393 pthread_t *thrs;
394 int procs =2;
395 char c;
396 thrs = malloc( sizeof( pthread_t ) * procs );
397 if (thrs == NULL)
398 {
399 perror( "malloc" );
400 return -1;
401 }
402 printf( "ifdma-test: Starting %d threads...\n", procs );
404 if (pthread_create( &thrs[0], NULL, (void*)consumer_thread,
405 (void *)0 ))
406 {
407 perror( "pthread_create" );
408 exit(1);
409 }
410 if (pthread_create( &thrs[1], NULL, (void*)producer_thread,
411 (void *)1 ))
412 {
413 perror( "pthread_create" );
414 exit(1);
415 }
416 //this thread of execution (main) now just waits on user input
417 for(;;)
418 {
419 printf(">");
420 c=getchar();
421 if (c=='q') {QUIT=1;break;}
422 else if (c=='s') printf(">IFDMA-TEST STATS: %d sent, %d received stall=%d \n",pkt_tx, pkt_rx,pkt_stall);
423 else if (c=='h') printf("> 'q' to quit, 's' for stats, 'h' for help\n");
424 }
426 //wait for completion
427 printf("main task now pending on thread completion\n");
428 for (i = 0; i < procs; i++)
429 pthread_join( thrs[i], NULL );
431 free( thrs );
433 }
435 /*************************************************
436 ************CLEAN UP****************************
437 ************************************************/
438 #ifndef INTERNAL_PACKETS
439 //get rid of rule, in the case that we are relaying packets
440 //also close our netcp rx channel
441 netapi_netcpCfgDelMac(netapi_handle,0,&err);
442 netapi_pktioClose(netcp_rx_chan,&err);
443 #endif
445 //close pktio channels we opened
446 netapi_pktioDelete(tx_chan ,&err);
447 netapi_pktioDelete(rx_chan ,&err);
449 //close flow
450 netapi_netcpCfgDelFlow(netapi_handle, specialFlow, &err);
452 //done
453 netapi_shutdown(netapi_handle);
456 //!finished!
457 }
460 //receive callback for packets from net (for consumer)
461 // this is used for case where we want to relay packets from
462 // network, instead of internally generating them
463 void recv_cb_net(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
464 PKTIO_METADATA_T meta[], int n_pkts,
465 uint64_t ts )
466 {
467 int i;
468 PKTIO_METADATA_T meta2 = {PKTIO_META_TX,{0},0};
469 Ti_Pkt * tip;
470 int err;
472 for(i=0;i<n_pkts;i++)
473 {
474 tip = p_recv[i];
475 meta2.u.tx_ifdma_dest=((NETCP_CFG_FLOW_T*)specialFlow)->flowid;
476 netapi_pktioSend(tx_chan,tip,&meta2,&err);
477 pkt_tx+=1;
478 if(!(pkt_tx % 128)) sched_yield(); //give consumer a chance
479 }
482 }
485 //receive callback for consumer (registered when we create pktio channel)
486 void recv_cb_consumer(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
487 PKTIO_METADATA_T meta[], int n_pkts,
488 uint64_t ts )
489 {
490 int i;
491 int len;
492 Ti_Pkt * tip;
493 unsigned int templen;
494 char * p_pkt;
496 for(i=0;i<n_pkts;i++)
497 {
498 tip = p_recv[i];
499 Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
500 len = Pktlib_getPacketLen(tip);
501 Pktlib_freePacket((Ti_Pkt*)tip);
502 }
503 return;
504 }
509 //SOME BENCHMARKS
510 //sonme benchmarks
511 void benchmarks(void)
512 {
513 int i,j;
514 unsigned long v1pop;
515 unsigned long v2pop;
516 unsigned long v1push;
517 unsigned long v2push;
518 unsigned long v1read;
519 unsigned long v2read;
520 unsigned long v1write;
521 unsigned long v2write;
522 unsigned long v1read2;
523 unsigned long v2read2;
524 #define N 100
525 Ti_Pkt pkts[N];
526 unsigned char * p_pkt;
527 int len;
528 int sum=0;
529 int sum2=0;
530 char *p=(char *) malloc(1000);
532 //queue pop
533 v1pop=netapi_timing_start();
534 for(i=0;i<N;i++) pkts[i]= Pktlib_allocPacket(OurHeap,1000);
535 v2pop = netapi_timing_start();
537 //write access
538 Pktlib_getDataBuffer(pkts[0],(uint8_t**)&p_pkt,&len);
539 v1write=netapi_timing_start();
540 for(i=0;i<1000;i++) p_pkt[i]=i;
541 v2write=netapi_timing_start();
543 // access
545 v1read=netapi_timing_start();
546 for(j=0;j<10;j++)
547 for(i=0;i<1000;i++) sum+=p_pkt[i];
548 v2read=netapi_timing_start();
550 // access (from malloc)
551 v1read2=netapi_timing_start();
552 for(i=0;i<1000;i++) sum2+=p[i];
553 v2read2=netapi_timing_start();
555 //queue push
556 v1push=netapi_timing_start();
557 for(i=0;i<N;i++) Pktlib_freePacket(pkts[i]);
558 v2push = netapi_timing_start();
560 //resutls
561 printf("allocs= %d free=%d write=%d read=%d read-malloc=%d (sum=%d %d)\n",
562 (v2pop-v1pop)/N, (v2push-v1push)/N, (v2write-v1write)/1000, (v2read-v1read)/10000,
563 (v2read2-v1read2)/1000,sum,sum2);
565 }