From 41385c9928be9f56ce403e8c3656be2ddd0258c3 Mon Sep 17 00:00:00 2001 From: Vijaya Kumar Abbaraju Date: Tue, 8 Oct 2024 03:05:12 +0530 Subject: [PATCH] PAC infra sysapi files (#18646) --- src/sonic-pac/fpinfra/sysapi/sysapi.c | 137 +++++++ src/sonic-pac/fpinfra/sysapi/sysapi_hpc.c | 45 +++ src/sonic-pac/fpinfra/sysapi/sysapi_if_net.c | 389 +++++++++++++++++++ 3 files changed, 571 insertions(+) create mode 100644 src/sonic-pac/fpinfra/sysapi/sysapi.c create mode 100644 src/sonic-pac/fpinfra/sysapi/sysapi_hpc.c create mode 100644 src/sonic-pac/fpinfra/sysapi/sysapi_if_net.c diff --git a/src/sonic-pac/fpinfra/sysapi/sysapi.c b/src/sonic-pac/fpinfra/sysapi/sysapi.c new file mode 100644 index 000000000000..642a9e62ca53 --- /dev/null +++ b/src/sonic-pac/fpinfra/sysapi/sysapi.c @@ -0,0 +1,137 @@ +/* + * Copyright 2024 Broadcom Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "pacinfra_common.h" +#include "product.h" +#include "resources.h" +#include "osapi.h" +#include "sysapi.h" +#include "defaultconfig.h" + +static void * sysapiTimerTaskID = 0; + +/************************************* + * Mbuf Queue declarations + *************************************/ +void **pMbufQTop; /* top of queue */ +void **pMbufQBot; /* bottom of queue */ +void **MbufQHead; +void **MbufQTail; +uint32 MbufsFree; +uint32 MbufsRxUsed; +uint32 MbufsMaxFree; +void *pMbufPool; + + +/************************************************************************** + * + * @purpose Task that creates the application timer function + * + * @param none + * + * @returns none. + * + * @notes If the task is already created, just return. + * + * @end + * + *************************************************************************/ +void sysapiTimerTaskStart(void) +{ + if ( sysapiTimerTaskID != 0 ) + { + return; + } + sysapiTimerTaskID = osapiTaskCreate( "osapiTimer", + (void *)osapiTimerHandler, + 0, + NULLPTR, + DEFAULT_STACK_SIZE, + MEDIUM_TASK_PRIORITY, + DEFAULT_TASK_SLICE); + + /* Wait for osapiTimer task to finish initialization.*/ + if (osapiWaitForTaskInit( OSAPI_TIMER_TASK_SYNC, WAIT_FOREVER) != SUCCESS) + { + //SYSAPI_PRINTF(SYSAPI_LOGGING_ALWAYS, "In routine %s line %d, osapiWaitForTaskInit failed\n", + // __FUNCTION__, __LINE__); + //LOG_ERROR(0); + } + return; +} + +/************************************************************************** + * @purpose Initialize the sysapi component + * + * @param none + * + * @returns SUCCESS + * @returns ERROR + * + * @notes + * + * @end + * + *************************************************************************/ +RC_t sysapiSystemInit(void) +{ + int32 i; + uint32 temp32; + uint32 phy_size = 0; + uint32 mtu_size = FD_NIM_DEFAULT_MTU_SIZE; + + /* initialize system timers */ + sysapiTimerTaskStart(); + + /* Reserve extra space for control overhead. */ + phy_size += (sizeof(SYSAPI_NET_MBUF_HEADER_t) + NET_MBUF_START_OFFSET + 64); + + temp32 = phy_size + mtu_size + SYSAPI_PKT_BUF_ALIGN_LEN; + + pMbufPool = osapiMalloc ( SIM_COMPONENT_ID, MAX_NETWORK_BUFF_PER_BOX * ( temp32 ) ); + if ( pMbufPool == NULLPTR ) + return( ERROR); + + + /******************************************************** + * Allocate the "mbuf" Queue. Each entry is a 32 bit ptr + *********************************************************/ + pMbufQTop = ( void ** )osapiMalloc ( SIM_COMPONENT_ID, MAX_NETWORK_BUFF_PER_BOX * sizeof (void *)); + if ( pMbufQTop == NULLPTR ) + return( ERROR); + + /*************************************************** + * Initialize the "mbuf" Queue and counter. + ****************************************************/ + MbufQHead = pMbufQTop; + MbufQTail = pMbufQTop; + MbufsMaxFree = MAX_NETWORK_BUFF_PER_BOX; + MbufsFree = MbufsMaxFree; + MbufsRxUsed = 0; + memset(&mbuf_stats, 0, sizeof(mbuf_stats_t)); + + for ( i=0;i<( int32)MbufsFree;i++ ) + { + *MbufQHead = ( void * ) ( ( uchar8 *)pMbufPool + i * ( temp32 )); + MbufQHead++; + } + pMbufQBot = --MbufQHead; /* set bottom of queue ptr */ + MbufQHead = pMbufQTop; /* reset head ptr to top */ + + + return( SUCCESS); +} diff --git a/src/sonic-pac/fpinfra/sysapi/sysapi_hpc.c b/src/sonic-pac/fpinfra/sysapi/sysapi_hpc.c new file mode 100644 index 000000000000..1401c9662f38 --- /dev/null +++ b/src/sonic-pac/fpinfra/sysapi/sysapi_hpc.c @@ -0,0 +1,45 @@ +/* + * Copyright 2024 Broadcom Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "pacinfra_common.h" + +typedef struct +{ + IANA_INTF_TYPE_t type; + PORT_SPEEDS_t defaultSpeed; + uint64 phyCapabilities; /* combination of all applicable PHY_CAPABILITIES_t */ + /* CONNECTOR_TYPES_t connectorType;*/ + fec_mode_t defaultFEC; + uint32 fecCapabilities; /* combination of all applicable FEC_CAPABILITY_t */ +} SYSAPI_HPC_PORT_DESCRIPTOR_t; + +/************************************************************************** +* +* @purpose Return the number of physical ports, given a slot number. +* +* @param slotNum slot number for requested card ID. +* +* @returns portCount Number of physical ports in slot number +* +* @notes +* +* @end +* +*************************************************************************/ +uint32 sysapiHpcPhysPortsInSlotGet(int slotNum) +{ + return 255; //needs to work on it +} diff --git a/src/sonic-pac/fpinfra/sysapi/sysapi_if_net.c b/src/sonic-pac/fpinfra/sysapi/sysapi_if_net.c new file mode 100644 index 000000000000..778184fa11a7 --- /dev/null +++ b/src/sonic-pac/fpinfra/sysapi/sysapi_if_net.c @@ -0,0 +1,389 @@ +/* + * Copyright 2024 Broadcom Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include +#include "pacinfra_common.h" +#include "product.h" +#include "resources.h" +#include "osapi.h" +#include "sysapi.h" +#include "system_exports.h" +#include "log.h" + +/************************************* +* Mbuf Queue declarations +*************************************/ +extern void **pMbufQTop; /* top of queue */ +extern void **pMbufQBot; /* bottom of queue */ +extern void **MbufQHead; +extern void **MbufQTail; +extern uint32 MbufsFree; +extern uint32 MbufsRxUsed; +extern uint32 MbufsMaxFree; +extern void *pMbufPool; + +mbuf_stats_t mbuf_stats; + +#define __USE_GNU +#include +#include +static pthread_mutex_t sysapiMbufMutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; +#undef __USE_GNU +#define SYSAPI_MBUF_LOCK() pthread_mutex_lock(&sysapiMbufMutex) +#define SYSAPI_MBUF_UNLOCK() pthread_mutex_unlock(&sysapiMbufMutex) + +/************************************************************************** +* @purpose Function used to track the Mbuf with the file name and the line num. +* This can be used by the individual components for further tracking of Mbuf. +* +* @param netMbufHandle - Ptr to network mbuf handle +* @param file - file name +* @param line - line number +* +* @returns void +* +* @comments +* +* @end +*************************************************************************/ +void sysapiNetMbufTrack( netBufHandle netMbufHandle, uchar8 *file, uint32 line) +{ + SYSAPI_NET_MBUF_HEADER_t *bufHandle = (SYSAPI_NET_MBUF_HEADER_t *)netMbufHandle; + + osapiStrncpySafe(bufHandle->last_file, file, sizeof(bufHandle->last_file)); + bufHandle->last_line = line; +} + + +/************************************************************************** +* +* @purpose Retrieve a network mbuf to the caller (and track the caller) +* +* @param none. +* +* @returns A ptr to a network mbuf handle +* @returns 0 if none are available +* +* @notes Delegates to sysapiNetMbufGet +* +* @end +* +*************************************************************************/ + netBufHandle sysapiNetMbufGetTrack(const char8 *file, uint32 line) +{ + SYSAPI_NET_MBUF_HEADER_t *netMbufHandle = 0; + + /* get the MBUF */ + netMbufHandle = (SYSAPI_NET_MBUF_HEADER_t *)sysapiNetMbufGet(); + + /* store tracking information */ + if(netMbufHandle) + { + osapiStrncpySafe(netMbufHandle->last_file, file, sizeof(netMbufHandle->last_file)); + netMbufHandle->last_line = line; + netMbufHandle->mbufLoc = MBUF_LOC_ALLOC; + + } + + return(( netBufHandle)netMbufHandle); +} + + +/************************************************************************** +* @purpose Retrieve an mbuf to the caller +* +* @param none. +* +* @returns A ptr to an mbuf +* @returns NULL if none are available +* +* @comments Use the Mutex semaphore to inhibit global variable corruption +* +* @end +*************************************************************************/ +static uint32 *sysapiMbufGet( void ) +{ + void * buffer; + uint32 mbufUsed = 0; + + if ( MbufsFree != 0 ) + { + buffer = *MbufQHead; + if ( MbufQHead >= pMbufQBot ) + MbufQHead = pMbufQTop; /* wrap the Q head ptr */ + else + MbufQHead++; /* move the Q head ptr */ + MbufsFree--; /* keep track... */ + + ((SYSAPI_NET_MBUF_HEADER_t *)buffer)->in_use = TRUE; + } + else + { + buffer = NULL; + } + + mbufUsed = MbufsMaxFree - MbufsFree; + /* (void) sysapiMbufUsedNotify(mbufUsed); */ + + return( ( uint32 * )buffer ); +} + +/************************************************************************** +* @purpose Retrieve a network mbuf to the caller +* +* @param none. +* +* @returns A ptr to a network mbuf handle +* @returns 0 if none are available +* +* @comments +* +* @end +*************************************************************************/ + netBufHandle sysapiNetMbufGet( void ) +{ + SYSAPI_NET_MBUF_HEADER_t *netMbufHandle; + + SYSAPI_MBUF_LOCK(); + mbuf_stats.alloc_tx_alloc_attempts++; + + netMbufHandle = (SYSAPI_NET_MBUF_HEADER_t *)sysapiMbufGet(); + if (netMbufHandle != NULL) + { + netMbufHandle->bufStart = ( uchar8 *)netMbufHandle + sizeof(SYSAPI_NET_MBUF_HEADER_t) + + NET_MBUF_START_OFFSET; + + netMbufHandle->bufStart = ( char8 *)SYSAPI_BUF_ALIGN(netMbufHandle->bufStart, MBUF_ALIGN_BOUND); + + netMbufHandle->bufLength = 0; + netMbufHandle->taskId = osapiTaskIdSelf(); + netMbufHandle->timeStamp = osapiUpTimeRaw(); + netMbufHandle->rxBuffer = FALSE; + + /* wipe out tracking information */ + netMbufHandle->last_file[0] = 0; + netMbufHandle->last_line = 0; + netMbufHandle->mbufLoc = MBUF_LOC_ALLOC; + } + else + { + mbuf_stats.alloc_tx_failures++; + } + + SYSAPI_MBUF_UNLOCK(); + + return(( netBufHandle)netMbufHandle); +} + + +/************************************************************************** +* @purpose Retrieve an aligned network mbuf to the caller +* +* @param align @b{(input)} Alignment indicator, for IP or frame +* +* @returns A ptr to a network mbuf handle +* @returns 0 if none are available +* +* @note All mbufs are 4 byte aligned +* +* @end +*************************************************************************/ + netBufHandle sysapiNetMbufAlignGet( uchar8 *file, uint32 line, + MBUF_ALIGNMENT alignType) +{ + SYSAPI_NET_MBUF_HEADER_t *netMbufHandle = 0; + + /* get the MBUF */ + netMbufHandle = (SYSAPI_NET_MBUF_HEADER_t *)sysapiNetMbufGet(); + + /* store tracking information */ + if(netMbufHandle) + { + if ( MBUF_IP_ALIGNED == alignType) + { + /* Compensate for ipheader offset */ + netMbufHandle->bufStart += MBUF_IP_CORRECTION; + } + osapiStrncpySafe(netMbufHandle->last_file, file, sizeof(netMbufHandle->last_file)); + netMbufHandle->last_line = line; + netMbufHandle->mbufLoc = MBUF_LOC_ALLOC; + } + + return(( netBufHandle)netMbufHandle); +} + +/************************************************************************** +* @purpose Free a network mbuf with debug information. +* +* @param Ptr to network mbuf handle +* +* @returns void +* +* @comments +* +* @end +*************************************************************************/ +void sysapiNetMbufFreeTrack( netBufHandle netMbufHandle, const char8 *file, uint32 line) +{ + SYSAPI_NET_MBUF_HEADER_t *header; +#if FEAT_CPUSTATS + uint32 headNext = NULL; + uint32 traceIndex = NULL; + uint32 currentStep = NULL; +#endif /* FEAT_CPUSTATS */ + + header = (SYSAPI_NET_MBUF_HEADER_t *) netMbufHandle; + + if (header->in_use == FALSE) + { + LOG_ERROR ((uint32) ((unsigned long) netMbufHandle)); + } + + osapiStrncpySafe(header->last_file, file, sizeof(header->last_file)); + header->last_line = line; + header->mbufLoc = MBUF_LOC_FREE; + + sysapiNetMbufFree (netMbufHandle); + +} + + +/************************************************************************** +* @purpose Return an mbuf to the mbuf pool +* +* @param *mbuf ptr to the mbuf to return +* +* @returns none. +* +* @comments Use the Mutex semaphore to inhibit global variable corruption +* +* @end +*************************************************************************/ +static void sysapiMbufFree( uint32 *mbuf ) +{ + uint32 mbufUsed = 0; + + + *MbufQTail = ( void * )mbuf; + if ( MbufQTail >= pMbufQBot ) + MbufQTail = pMbufQTop; /* wrap the Q tail ptr */ + else + MbufQTail++; /* move the Q tail ptr */ + + MbufsFree++; /* keep track... */ + + /* If we have extra buffers then somebody must have freed a buffer twice. + ** This is a fatal error. + */ + if (MbufsFree > MbufsMaxFree) + { + LOG_ERROR ((unsigned long) mbuf); + } + + mbufUsed = MbufsMaxFree - MbufsFree; + /*(void) sysapiMbufUsedNotify(mbufUsed);*/ + + + return; +} + +/************************************************************************** +* @purpose Free a network mbuf +* +* @param Ptr to network mbuf handle +* +* @returns void +* +* @comments +* +* @end +*************************************************************************/ +void sysapiNetMbufFree( netBufHandle netMbufHandle ) +{ + SYSAPI_MBUF_LOCK(); + if (netMbufHandle != NULL) + { + if (((SYSAPI_NET_MBUF_HEADER_t *)netMbufHandle)->in_use == TRUE) + { + sysapiMbufRxusedStatsUpdate(((SYSAPI_NET_MBUF_HEADER_t *)netMbufHandle)->priorityPool, + FALSE); + } + ((SYSAPI_NET_MBUF_HEADER_t *)netMbufHandle)->bufStart = NULL; + ((SYSAPI_NET_MBUF_HEADER_t *)netMbufHandle)->bufLength = 0; + ((SYSAPI_NET_MBUF_HEADER_t *)netMbufHandle)->taskId = 0; + ((SYSAPI_NET_MBUF_HEADER_t *)netMbufHandle)->timeStamp = 0; + ((SYSAPI_NET_MBUF_HEADER_t *)netMbufHandle)->in_use = FALSE; + ((SYSAPI_NET_MBUF_HEADER_t *)netMbufHandle)->mbufLoc = MBUF_LOC_FREE; + ((SYSAPI_NET_MBUF_HEADER_t *)netMbufHandle)->priorityPool = MBUF_RX_PRIORITY_NULL; + ((SYSAPI_NET_MBUF_HEADER_t *)netMbufHandle)->rxCode = MBUF_RX_REASON_NONE; + + if (((SYSAPI_NET_MBUF_HEADER_t *)netMbufHandle)->rxBuffer == TRUE) + { + ((SYSAPI_NET_MBUF_HEADER_t *)netMbufHandle)->rxBuffer = FALSE; + + MbufsRxUsed--; + } + + sysapiMbufFree ((uint32 *)netMbufHandle); + } + SYSAPI_MBUF_UNLOCK(); +} + + +/********************************************************************** +* @pupose To increment or decrement Mbuf stats per RX priority as in +* MBUF_RX_PRIORITY +* +* @param priority @b{(input)} Mbuf priority to increment/decrement +* corresponding stats +* operation @b{(input)} Bool value to increment/decrement +* TRUE - to increment +* FALSE - to decrement +* +* @returns SUCCESS, +* +* @notes +* +* @end +* +*********************************************************************/ +RC_t sysapiMbufRxusedStatsUpdate( MBUF_RX_PRIORITY priority, + BOOL operation) +{ + switch(priority) + { + case MBUF_RX_PRIORITY_HIGH: + operation ? mbuf_stats.alloc_rx_high++ : mbuf_stats.alloc_rx_high--; + break; + case MBUF_RX_PRIORITY_MID0: + operation ? mbuf_stats.alloc_rx_mid0++ : mbuf_stats.alloc_rx_mid0--; + break; + case MBUF_RX_PRIORITY_MID1: + operation ? mbuf_stats.alloc_rx_mid1++ : mbuf_stats.alloc_rx_mid1--; + break; + case MBUF_RX_PRIORITY_MID2: + operation ? mbuf_stats.alloc_rx_mid2++ : mbuf_stats.alloc_rx_mid2--; + break; + case MBUF_RX_PRIORITY_NORMAL: + operation ? mbuf_stats.alloc_rx_norm++ : mbuf_stats.alloc_rx_norm--; + break; + default : + break; + } + return SUCCESS; +} +