The XrmIPsecSAReplace function is designed to replace an existing IPsec Security Association (SA) with a new one. This function is part of a system that manages IPsec tunnels and their associated security associations. Here's a detailed explanation of the function:
pIpsecTunnel: A pointer to the IPsec tunnel structure.
pIpsecSA: A pointer to the IPsec SA structure that needs to be replaced.
pCmd: A pointer to the command structure that contains the details for the new SA.
Function Body
Initialization:
c
ERROR_CODE status = SUCCESS;
The status variable is initialized to SUCCESS.
Deleting the Existing SA:
c
if ((status = XrmNPDeleteSA(pIpsecSA)) != SUCCESS)
The function attempts to delete the existing SA using XrmNPDeleteSA.
If the deletion fails, an event is notified to indicate the failure.
The event notification includes details such as the operation type (delete), the tunnel ID, the direction (IN or OUT), the SA key, and the local and remote IP addresses.
If the deletion is successful, the function proceeds to add the new SA using XrmNPAddSA.
The Xid field in the command structure is set to the key2 value from the pIpsecSA structure.
If the addition fails, an event is notified to indicate the failure, similar to the deletion failure notification.
User
660 LOCAL ERROR_CODE XrmNPAddSA(XRM_IPSEC_SA_UPDATE_CMD_STR *pCmd,
661 XRM_IPSEC_SA_STR *pIpsecSA,
662 uint32 *EncId)
663 {
664 ERROR_CODE ret = SUCCESS;
665 CPS_IPSEC_ENC_CREATE_CMD_STR enEncStr;
666 CPS_IPSEC_DEC_CREATE_CMD_STR enDecStr;
667 XRM_IPSEC_TUNNEL_STR *pIpsecTunnel;
668
669 pIpsecTunnel = pIpsecSA->pTunnel;
670
671 // in the standby case, use the already assigned Xid on active side
672 if (pCmd->Xid)
673 {
674 *EncId = pCmd->Xid;
675 }
676 // XRM only allocate the ENC_ID, DEC_ID is the local spi
677 // SAs processed by the standby would already of Xid
678 else if (pCmd->direction == XRM_IPSEC_SA_DIRECTION_IN)
679 {
680 *EncId = pCmd->spi & XRM_IPSEC_IPV4_SPI_ID_BYTE;
681 }
682 else
683 {
684 // EncId is allocated by CPS
685 *EncId = 0;
686 }
687
688 if (pCmd->direction == XRM_IPSEC_SA_DIRECTION_OUT)
689 {
690 // Call CPS to allocate and enable encryption context in NPs
691 MemZero(&enEncStr, sizeof(enEncStr));
692
693 enEncStr.saMode = pCmd->saType;
694 enEncStr.cryptoAlg = pCmd->esp;
695 enEncStr.authAlg = pCmd->auth;
696 enEncStr.spi = pCmd->spi;
697 enEncStr.seqNum = pIpsecSA->seqn;
698 enEncStr.cipherKeySz = (uint8) pCmd->cipherKeylen;
699 enEncStr.authKeySz = (uint8) pCmd->authKeylen;
700
701 MemCpy(enEncStr.cipherKey, pCmd->cipherKey, pCmd->cipherKeylen);
702 MemCpy(enEncStr.authKey, pCmd->authKey, pCmd->authKeylen);
703
704 // If Tunnel mode, call XRM to build tunnel (outer) IP header
705 if (pCmd->saType == XRM_IPSEC_SA_MODE_TUNNEL)
706 {
707 // Determine if V4 or V6
708 if (IPUTIL_IS_V4_ADDRESS(&pIpsecTunnel->remoteIp))
709 {
710 XrmIpv4HdrBuild(&pIpsecTunnel->localIp,
711 &pIpsecTunnel->remoteIp,
712 0, // TOS, should NP replace with original packet TOS??
713 IPPROTO_ESP,
714 0, // packet len, 0 tells NP to calculate it
715 (XRM_IPV4_HDR_STR *)enEncStr.tunnelHdr);
716
717 enEncStr.tunnelHdrSz = sizeof(XRM_IPV4_HDR_STR);
718 }
719 else
720 {
721 XrmIpv6HdrBuild(&pIpsecTunnel->localIp,
722 &pIpsecTunnel->remoteIp,
723 0, // TOS, should NP replace with original packet TOS??
724 IPPROTO_ESP,
725 0, // packet len, 0 tells NP to calculate it
726 (XRM_IPV6_HDR_STR *)enEncStr.tunnelHdr);
727
728 enEncStr.tunnelHdrSz = sizeof(XRM_IPV6_HDR_STR);
729 }
730 }
731
732 //Update async flag based on SLB role only? TODO
733 ret = CpsIpsecEncCreate(XRM_CPS_IPSEC_ENC_HNDL, &enEncStr, EncId ,NULL, false);
734 }
735 else
736 {
737 // Call CPS to allocate and enable decryption context in NPs
738 MemZero(&enDecStr, sizeof(enDecStr));
739
740 enDecStr.decId = *EncId;
741 enDecStr.saMode = pCmd->saType;
742 enDecStr.cryptoAlg = pCmd->esp;
743 enDecStr.authAlg = pCmd->auth;
744 enDecStr.cipherKeySz = (uint8) pCmd->cipherKeylen;
745 enDecStr.authKeySz = (uint8) pCmd->authKeylen;
746
747 MemCpy(enDecStr.cipherKey, pCmd->cipherKey, pCmd->cipherKeylen);
748 MemCpy(enDecStr.authKey, pCmd->authKey, pCmd->authKeylen);
749
750 //Update async flag based on SLB role only? TODO
751 ret = CpsIpsecDecCreate(XRM_CPS_IPSEC_DEC_HNDL, &enDecStr, false);
752 }
753
754 XRM_LOG_INFO(GCID_NULL, NULL, CALL_TRACE_KEY_NULL,
755 "%s: SA spi=0x%08x(%d) %s remote=%-p local=%-p enable %s akaPairSpi=0x%08x(%d)",
756 , pCmd->spi, pCmd->spi,
757 XRM_GET_IPSEC_DIRECTION(pCmd->direction),
758 &pCmd->remoteIp, &pCmd->localIp,
759 ((ret==SUCCESS)?"succeeded":"failed"),pCmd->akaPairSpi, pCmd->akaPairSpi);
760
761 XRM_LOG_INFO(GCID_NULL, NULL, CALL_TRACE_KEY_NULL,
762 "%s: SA mode=%s crypto=%s auth=%s cryptoKeySz=%d authTagSz=%d tunnelHdrSz %d",
763 ,
764 XRM_GET_IPSEC_SA_TYPE(pCmd->saType),
765 XRM_GET_IPSEC_ENCDEC_ALG(pCmd->esp),
766 XRM_GET_IPSEC_AUTH_ALG(pCmd->auth),
767 pCmd->cipherKeylen, pCmd->authKeylen,
768 enEncStr.tunnelHdrSz);
769
770 return (ret);
771 }
772
Assistant
The XrmNPAddSA function is designed to add a new IPsec Security Association (SA) to the system. This function is part of a system that manages IPsec tunnels and their associated security associations. Here's a detailed explanation of the function:
Function Signature
c
LOCAL ERROR_CODE XrmNPAddSA(
XRM_IPSEC_SA_UPDATE_CMD_STR *pCmd,
XRM_IPSEC_SA_STR *pIpsecSA,
uint32 *EncId
);
Parameters:
pCmd: A pointer to the command structure that contains the details for the new SA.
pIpsecSA: A pointer to the IPsec SA structure.
EncId: A pointer to a variable that will hold the encryption ID.
The XRM_FILL_SA macro and the conditional call to it in the XrmIPsecSAReplace function serve a specific purpose in the context of updating the IPsec Security Association (SA) structure. Let's break down why this is done and what it achieves:
This macro copies various fields from the source structure (_src) to the destination structure (_dest). Specifically, it copies:
Authentication algorithm (auth)
Encryption algorithm (esp)
Mode (mode)
SA type (saType)
Authentication key length (authKeylen)
Authentication key (authKey)
Cipher key length (cipherKeylen)
Cipher key (cipherKey)
Conditional Call in XrmIPsecSAReplace
In the XrmIPsecSAReplace function, the macro is called conditionally:
c
if (status == SUCCESS)
{
XRM_FILL_SA(pIpsecSA, pCmd);
}
This conditional call ensures that the structure is updated with the new values from the structure only if the previous operations (deleting the old SA and adding the new SA) were successful.
User
483 XRM_IPSEC_CTRL_STR *XrmIPsecCtrlLookup(USHORT acId)
484 {
485 XRM_GLOBAL_DATA_STR *globalData = XrmGlobalDataGet();
486 XRM_IPSEC_RES_POOL_STR *ipsecRes = &globalData->ipsecRes;
487 XRM_IPSEC_CTRL_STR pIpsecCtrl;
488
489 / while there are still instances on the list */
490 pIpsecCtrl = (XRM_IPSEC_CTRL_STR *)ipsecRes->ipsecCtrl.next;
491 while ( (LIST_STR *)pIpsecCtrl != &ipsecRes->ipsecCtrl)
492 {
493 XRM_IPSEC_CTRL_STR *pNext = (XRM_IPSEC_CTRL_STR *)pIpsecCtrl->listEnt.next;
494
495 if (pIpsecCtrl->acId == acId)
496 return(pIpsecCtrl);
497
498 pIpsecCtrl = pNext;
499 }
500 return(NULL);
501 }
explain in depth with example
Assistant
The XrmIPsecCtrlLookup function is designed to search for and return a pointer to an XRM_IPSEC_CTRL_STR structure based on a given acId (which likely stands for "Association Control ID" or a similar identifier). This function traverses a linked list of IPsec control structures to find the matching entry. Let's break down the function in detail and provide an example to illustrate its operation.
The function initializes pIpsecCtrl to the first element in the linked list of IPsec control structures.
It then enters a loop to traverse the linked list.
User
483 XRM_IPSEC_CTRL_STR *XrmIPsecCtrlLookup(USHORT acId)
484 {
485 XRM_GLOBAL_DATA_STR *globalData = XrmGlobalDataGet();
486 XRM_IPSEC_RES_POOL_STR *ipsecRes = &globalData->ipsecRes;
487 XRM_IPSEC_CTRL_STR pIpsecCtrl;
488
489 / while there are still instances on the list */
490 pIpsecCtrl = (XRM_IPSEC_CTRL_STR *)ipsecRes->ipsecCtrl.next;
491 while ( (LIST_STR *)pIpsecCtrl != &ipsecRes->ipsecCtrl)
492 {
493 XRM_IPSEC_CTRL_STR *pNext = (XRM_IPSEC_CTRL_STR *)pIpsecCtrl->listEnt.next;
494
495 if (pIpsecCtrl->acId == acId)
496 return(pIpsecCtrl);
497
498 pIpsecCtrl = pNext;
499 }
500 return(NULL);
501 }
1424 typedef struct xrm_global_data_str
1425 {
1426 XRM_NIF_STR *nifArray[NP_MEDIA_NUM_NP_MAX]; // per-NP array of mgmt/pkt NIFs
1427 GHASH_TABLE *lifIfIdxHashTbl; // hash table of LIFs, hashed on ifIndex
1428 GHASH_TABLE *lifIdHashTbl; // hash table of LIFs, hashed on LIF Id
1429 GHASH_TABLE *lifGrpNameHashTbl; // hash table of LIF Groups, hashed on name
1430 GHASH_TABLE *lifGrpIdHashTbl; // hash table of LIF Groups, hashed on LIF group Id
1431 LIST_STR sgRegList; // list of registered SIPSG
1432 NP_MEDIA_API_HNDL npIntfHndl;
1433 NP_MEDIA_API_HNDL npNaptPktHndl;
1434 NP_MEDIA_API_HNDL npDtmfPktHndl;
1435 NP_MEDIA_API_HNDL npRtpactPktHndl;
1436 NP_MEDIA_API_HNDL npIceDtlsPktHndl;
1437 CPS_API_HNDL cpsIntfHndl;
1438 CPS_LIF_BYTE_CNT_RD_STR lifCntRsp; // used to get LIF counter stats in-bulk from NP
1439 TIMER_ID lifPollTimerId;
1440 TIMER_ID secPollTimerId;
1441 TIMER_ID pktLossPollTimerId; // poll timer for packet loss clearing condition
1442 TIMER_ID pktLossSuppressTimerId; // timer started after switchover activation
1443 TIMER_ID fprmRateLimitTimerId; // timer used to rate limit fingerprinting
1444 bool suppressPktLossInd; // if true do not send indication to NRMA
1445 NP_MEDIA_RES_STR npResInfo; // NP resource counts/ranges, etc.
1446 XRM_IPSEC_RES_POOL_STR ipsecRes; // ipsec related resources
1447 bool mediaResActivated;
1448 XRM_CPU_NP_CONGESTION_STR cpuNpCongestion;
1449 bool iceStatsEnable;
1450 bool iceResIndMsgEnable; // for SVT load testing
1451 bool hotStandbyMode;
1452 // LSWU support from pre-V05 releases
1453 bool lswuPeerLessThanV04_01; // valid ONLY on standby server!
1454 bool lswuPeerLessThanV05_00; // valid ONLY on standby server!
1455 uint8 mediaXresAllocMode;
1456 uint8 lswuMode;
1457 XRM_LSWU_PRE_V05_STATS_STR lswuStats;
1458 uint32 systemStatsTimeInterval;
1459 bool forceGETSCallEnable;
1460 XRM_RTP_ACTIVITY_STATS_STR rtpActiityStats;
1461 int nifBwStatsUpdateThreadId;
1462 bool mediaDryupMode;
1463 TIMER_ID mediaDryupRtpActivityTImerId; // timer to enable rtp inactivity during dryup mode
1464 TIMER_ID nsTableStatePollTimerId; // poll timer for pod ID mapping
1465 XRM_BW_STATS_THREAD_DATA bwStatsThreadData;
1466 TIMER_ID udpChunkCleanupTimerId;
1467 CPS_API_HNDL cpsIntfIpsecEncHndl;
1468 CPS_API_HNDL cpsIntfIpsecDecHndl;
1469 CPS_API_HNDL cpsIntfMicroflowHndl;
1470 GHASH_TABLE *lifIdGwMacTmpHashTbl; // hash table of GW MAC addresses, hashed on ifIndex
1471 } XRM_GLOBAL_DATA_STR;
1402 // IPsec resources
1403 typedef struct XRM_IPSEC_RES_POOL_STR
1404 {
1405 LIST_STR ipsecCtrl; // ipsec instances, 1 system and 1 for each address context
1406 uint32 ipsecNum; // total number of ipsec control instances
1407 LIST_ITEM_STR mediaSAList; // list of SAs being used for media traffic (e.g. LI)
1408 GHASH_TABLE IPSEC ctrl List entry in currentContext in phase 1, this is a list of the IPSEC tunnel (local IKE, and remote IKE) there could be up to XRM_PNS_IPSEC_TUNNEL_MAX of tunnels */
192 LIST_STR ipsecTunnelList;
193 HASH_CTRL_STR for interface with NP to locate SA based on enc/dec ID */
196 ULONG saNum;
197 HASH_CTRL_STR *ipsecSAHashTbl;
198
199 XRM_IPSEC_CTRL_STATS_STR stats;
200
201 } XRM_IPSEC_CTRL_STR;
202
1827 XRM_GLOBAL_DATA_STR *XrmGlobalDataGet (void)
1828 {
1829 return (&xrmIcb->globalData);
1830 }
82 XRM_ICB_STR *xrmIcb;
1548 typedef struct xrm_icb_str
1549 {
1550 SYS_ICB_STR sysIcb;
1551 XRM_CONFD_CSV_STR *csvCb;
1552 XRM_CONTEXT_STR *currContext;
1553 XRM_CONTEXT_STR *sbyContext[SYS_CONTEXT_MAX];
1554 uint16 currContextId;
1555 XRM_GLOBAL_DATA_STR globalData;
1556 XRM_USER_ADMIN_STR userAdmin; // User-configurable admin data
1557 uint8 redundMode;
1558 CPS_ACL_DBG_STR lastDbgAdmitEntry; //used by debug command
1559 CPS_IP_ACL_RULE_DBG_STR lastDbgIpAclEntry;
1560 CPS_ACL_DBG_STR lastDbgDblEntry;
1561 CPS_ACL_DBG_STR lastDbgDiscardEntry;
1562 uint32 floatingId;
1563 bool isPfeDeployment;// Call Sma API once and store it here.
1564 bool isAsyncCpsCmd;
1565 SmaCnfPodType cnfPodType;// Call Sma API once and store it here.
1566 XRM_ROUTE_STR *pfeDummyRoute; // Create the dummy route once and use it everytime
1567 std::unique_ptr pfeClientInstance;
1568 } XRM_ICB_STR;
Assistant
To fully understand the XrmIPsecCtrlLookup function, we need to link it with the provided data structures and their relationships. Let's break down each component and explain how they interact.
Data Structures
XRM_GLOBAL_DATA_STR
c
typedef struct xrm_global_data_str {
// Other fields...
XRM_IPSEC_RES_POOL_STR ipsecRes; // IPsec related resources
// Other fields...
} XRM_GLOBAL_DATA_STR;
This structure holds global data for the XRM (likely an IPsec management module).
It includes an ipsecRes field, which is an instance of XRM_IPSEC_RES_POOL_STR.
XRM_IPSEC_RES_POOL_STR
c
typedef struct XRM_IPSEC_RES_POOL_STR {
LIST_STR ipsecCtrl; // List of IPsec control structures
uint32 ipsecNum; // Total number of IPsec control instances
// Other fields...
} XRM_IPSEC_RES_POOL_STR;
This structure manages IPsec resources, including a list of IPsec control structures.
The ipsecCtrl field is a linked list of XRM_IPSEC_CTRL_STR structures.
XRM_IPSEC_CTRL_STR
c
typedef struct xrm_ipsec_ctrl_str {
LIST_STR listEnt; // List entry for the IPsec control structure
USHORT acId; // Association Control ID
USHORT tunnelNum; // Number of tunnels
// Other fields...
} XRM_IPSEC_CTRL_STR;
This structure represents an IPsec control instance.
It includes an acId field, which is used to identify the control instance.
The listEnt field is used to link this structure into a list.
XRM_ICB_STR
c
User
explain the linked list traversing concept used here
Assistant
The linked list traversal concept used in the XrmIPsecCtrlLookup function is a fundamental technique for navigating through a linked list data structure. Let's break down the key concepts and how they are applied in this function.
Linked List Basics
A linked list is a linear data structure where each element (called a node) contains data and a reference (or pointer) to the next node in the sequence. The last node typically points to NULL to indicate the end of the list.
Types of Linked Lists
Singly Linked List: Each node contains a single pointer to the next node.
Doubly Linked List: Each node contains two pointers, one to the next node and one to the previous node.
Circular Linked List: The last node points back to the first node, forming a circle.
In the XrmIPsecCtrlLookup function, a singly linked list is used.
Data Structures
The relevant data structures for the linked list traversal are:
This is a generic list structure with a single pointer to the next node.
XRM_IPSEC_CTRL_STR:
c
typedef struct xrm_ipsec_ctrl_str {
LIST_STR listEnt; // List entry for the IPsec control structure
USHORT acId; // Association Control ID
// Other fields...
} XRM_IPSEC_CTRL_STR;
This structure represents an IPsec control instance and includes a listEnt field, which is a LIST_STR used to link this structure into a list.
XRM_IPSEC_RES_POOL_STR:
c
typedef struct XRM_IPSEC_RES_POOL_STR {
LIST_STR ipsecCtrl; // List of IPsec control structures
// Other fields...
} XRM_IPSEC_RES_POOL_STR;
This structure manages IPsec resources and includes an ipsecCtrl field, which is a representing the head of the linked list.
User
The function initializes pIpsecCtrl to the first element in the ipsecCtrl linked list. The ipsecCtrl field is a LIST_STR, and its next field points to the first XRM_IPSEC_CTRL_STR in the list.
I didn't understand this please explain in detail
Assistant
Certainly! Let's break down the initialization process in detail to clarify how pIpsecCtrl is set to the first element in the ipsecCtrl linked list.
This is a generic list structure with a single pointer to the next node.
XRM_IPSEC_CTRL_STR:
c
typedef struct xrm_ipsec_ctrl_str {
LIST_STR listEnt; // List entry for the IPsec control structure
USHORT acId; // Association Control ID
// Other fields...
} XRM_IPSEC_CTRL_STR;
This structure represents an IPsec control instance and includes a listEnt field, which is a LIST_STR used to link this structure into a list.
XRM_IPSEC_RES_POOL_STR:
c
typedef struct XRM_IPSEC_RES_POOL_STR {
LIST_STR ipsecCtrl; // List of IPsec control structures
// Other fields...
} XRM_IPSEC_RES_POOL_STR;
This structure manages IPsec resources and includes an ipsecCtrl field, which is a LIST_STR representing the head of the linked list.
Initialization Process
The initialization process involves setting up a pointer to traverse the linked list of XRM_IPSEC_CTRL_STR structures. Here's the relevant code:
then what is this XRM_IPSEC_CTRL_STR *pNext = (XRM_IPSEC_CTRL_STR *)pIpsecCtrl->listEnt.next;
Assistant
The line XRM_IPSEC_CTRL_STR *pNext = (XRM_IPSEC_CTRL_STR *)pIpsecCtrl->listEnt.next; is used to safely traverse the linked list of XRM_IPSEC_CTRL_STR structures. Let's break down what this line does and why it is necessary.
pIpsecCtrl is a pointer to the current XRM_IPSEC_CTRL_STR structure in the linked list.
listEnt is a LIST_STR field within the XRM_IPSEC_CTRL_STR structure. This field is used to link the current structure to the next structure in the list.
next is a pointer within the LIST_STR structure that points to the next node in the list.
Type Casting:
(XRM_IPSEC_CTRL_STR *) is a type cast that converts the LIST_STR * (which is a generic list pointer) to an XRM_IPSEC_CTRL_STR * (which is a specific pointer to an XRM_IPSEC_CTRL_STR structure).
This cast is necessary because pIpsecCtrl->listEnt.next is of type LIST_STR *, but we want to treat it as an XRM_IPSEC_CTRL_STR * to access the fields of the XRM_IPSEC_CTRL_STR structure.
pNext:
pNext is a new pointer of type XRM_IPSEC_CTRL_STR * that will point to the next node in the linked list.
Why This Line is Necessary
The purpose of this line is to safely advance the traversal of the linked list. By assigning pNext to pIpsecCtrl->listEnt.next, we ensure that we have a pointer to the next node in the list before potentially modifying pIpsecCtrl. This is crucial for maintaining the integrity of the list traversal, especially in a loop where pIpsecCtrl is updated on each iteration.
Example in Context
Let's consider the traversal loop in the XrmIPsecCtrlLookup function:
c
User
now explain in overall what is happenning
Assistant
Certainly! Let's provide an overall explanation of what is happening in the XrmIPsecCtrlLookup function, including the linked list traversal and the purpose of the function.
Overall Purpose
The XrmIPsecCtrlLookup function is designed to search for and return a pointer to an XRM_IPSEC_CTRL_STR structure based on a given acId (Association Control ID). This function is part of a system that manages IPsec (Internet Protocol Security) control structures, which are used to handle secure communication over IP networks.
Data Structures Involved
XRM_GLOBAL_DATA_STR:
This structure holds global data for the XRM (likely an IPsec management module).
It includes an ipsecRes field, which is an instance of XRM_IPSEC_RES_POOL_STR.
XRM_IPSEC_RES_POOL_STR:
This structure manages IPsec resources, including a linked list of XRM_IPSEC_CTRL_STR structures.
The ipsecCtrl field is a LIST_STR that serves as the head of the linked list.
XRM_IPSEC_CTRL_STR:
This structure represents an IPsec control instance.
It includes an acId field, which is used to identify the control instance.
The listEnt field is a LIST_STR used to link this structure into a list.
LIST_STR:
This is a generic list structure with a single pointer to the next node.
The LISTINSTAIL_GUTS macro is designed to insert an element at the tail (end) of a doubly linked list. Let's break down the macro step-by-step to understand what it does and how it works.
Doubly Linked List Basics
A doubly linked list is a data structure where each node contains pointers to both the next and previous nodes in the list. This allows for efficient traversal in both directions.
This check ensures that the list is in a consistent state before performing the insertion.
((LIST_STR *)list)->prev->next should point back to the head of the list (list). If this is not the case, it indicates a potential corruption in the list structure.
User
(pointed to by list->prev
what is this?
Assistant
The phrase "pointed to by list->prev" refers to the node that the prev pointer of the head of the list is currently pointing to. Let's break this down in the context of a doubly linked list.
Doubly Linked List Basics
In a doubly linked list, each node contains two pointers:
next: Points to the next node in the list.
prev: Points to the previous node in the list.
The head of the list is a special node that serves as the entry point to the list. In many implementations, the head node itself does not contain any data but is used to manage the list. The prev pointer of the head node points to the last node in the list, creating a circular reference that helps in efficiently managing the list.
Visual Representation
Let's visualize a doubly linked list:
code
head (list) <-> [Node1] <-> [Node2] <-> [Node3] <-> head (list)
head->next points to Node1.
head->prev points to Node3 (the last node in the list).
Node1->prev points to head.
Node1->next points to Node2.
Node2->prev points to Node1.
Node2->next points to Node3.
Node3->prev points to Node2.
Node3->next points to head.
Explanation of "pointed to by list->prev"
In the context of the LISTINSTAIL_GUTS macro, list is a pointer to the head of the doubly linked list. The prev pointer of the head node (list->prev) points to the last node in the list.
list->prev is the prev pointer of the head node.
list->prev points to the last node in the list.
So, "pointed to by list->prev" means the node that the prev pointer of the head node is currently pointing to, which is the last node in the list.
Example in Context
Let's consider the LISTINSTAIL_GUTS macro again:
c
User
C
((LIST_STR *)elem)->prev = ((LIST_STR *)list)->prev;
The prev pointer of the new element (elem) is set to point to the current last element in the list (which is pointed to by list->prev).
here last element is (which is pointed to by list->prev). how and what
Assistant
Let's clarify the concept of the "last element" in the context of a doubly linked list and how the prev pointer of the head node (list->prev) points to it.
Doubly Linked List Structure
In a doubly linked list, each node contains two pointers:
next: Points to the next node in the list.
prev: Points to the previous node in the list.
The head of the list is a special node that serves as the entry point to the list. In many implementations, the head node itself does not contain any data but is used to manage the list. The prev pointer of the head node points to the last node in the list, creating a circular reference that helps in efficiently managing the list.
Visual Representation
Let's visualize a doubly linked list:
code
head (list) <-> [Node1] <-> [Node2] <-> [Node3] <-> head (list)
head->next points to Node1.
head->prev points to Node3 (the last node in the list).
Node1->prev points to head.
Node1->next points to Node2.
Node2->prev points to Node1.
Node2->next points to Node3.
Node3->prev points to Node2.
Node3->next points to head.
Explanation of list->prev
list is a pointer to the head of the doubly linked list.
list->prev is the prev pointer of the head node.
list->prev points to the last node in the list.
So, "pointed to by list->prev" means the node that the prev pointer of the head node is currently pointing to, which is the last node in the list.
Example in Context
Let's consider the LISTINSTAIL_GUTS macro again:
c
User
now explain the entire code with visual representation like above
Assistant
Certainly! Let's go through the entire LISTINSTAIL_GUTS macro step-by-step with visual representations to illustrate how the insertion of a new element at the tail of a doubly linked list works.
Let's assume we have a doubly linked list with the following nodes:
code
head (list) <-> [Node1] <-> [Node2] <-> [Node3] <-> head (list)
head->next points to Node1.
head->prev points to Node3.
Node1->prev points to head.
Node1->next points to Node2.
Node2->prev points to Node1.
Node2->next points to Node3.
Node3->prev points to Node2.
Node3->next points to head.
Step-by-Step Explanation with Visual Representation
User
if ((lif = XrmLifGet(pstLocal, pIpsecCtrl->acId, 0)) == NULL)
4092 XRM_LIF_STR *XrmLifGet (const SOCKADDR_STR *ipAddr,
4093 uint16 addrCtxt,
4094 uint16 lifGrpId)
4095 {
4096 XRM_LIF_GROUP_STR *lifGrp;
4097 XRM_LIF_STR *lif;
4098
4099 // If LIG group Id is specified, then only search LIFs in
4100 // specified LIF group
4101 if (lifGrpId != 0)
4102 {
4103 // Get LIF group based on Id
4104 if ((lifGrp = XrmLifGroupGet(lifGrpId)) == NULL)
4105 {
4106 return (NULL);
4107 }
4108
4109 // Go thru all LIFs in this LIF group
4110 for (lif = XrmLifGetFirstInLifGrp(lifGrp);
4111 lif != NULL;
4112 lif = XrmLifGetNextInLifGrp(lifGrp, lif))
4113 {
4114 // Check for match
4115 if (XrmLifMatchAddrCtxtIp(addrCtxt, ipAddr, lif) == true)
4116 {
4117 return (lif);
4118 }
4119 }
4120 }
4121 else
4122 {
4123 // LIF group not specified, search all created LIFs
4124 for (lif = XrmLifGetFirst();
4125 lif != NULL;
4126 lif = XrmLifGetNext(lif))
4127 {
4128 // Check for match
4129 if (XrmLifMatchAddrCtxtIp(addrCtxt, ipAddr, lif) == true)
4130 {
4131 return (lif);
4132 }
4133 }
4134 }
4135
4136 return (NULL);
4137 }
4285 XRM_LIF_STR *XrmLifGetFirst (void)
4286 {
4287 XRM_GLOBAL_DATA_STR *globalData = XrmGlobalDataGet();
4288 GHASH_ENTRY *hashEntry;
4289 XRM_LIF_STR *lif;
4290 uint16 ctxtId = XrmIcbGet()->currContextId;
4291
4292 // In N:1 redundancy, hash table on standby contains LIFs from
4293 // multiple contexts. Get first entry
4294 if (((hashEntry = GHashFirst(globalData->lifIfIdxHashTbl)) == NULL) ||
4295 ((lif = (XRM_LIF_STR *)hashEntry->item) == NULL))
4296 {
4297 return (NULL);
4298 }
4299
4300 if (lif->ifIdxHashEnt.ctxtId == ctxtId)
4301 {
4302 // First entry is for this context ID
4303 return (lif);
4304 }
4305
4306 // First entry was not for this context ID. Travserse hash
4307 // table until first entry for this context ID is found
4308 lif = XrmLifGetNext(lif);
4309
4310 return (lif);
4311 }
4319 XRM_LIF_STR *XrmLifGetNext (XRM_LIF_STR *lif)
4320 {
4321 XRM_GLOBAL_DATA_STR *globalData = XrmGlobalDataGet();
4322 GHASH_ENTRY *hashEntry;
4323 uint16 ctxtId = XrmIcbGet()->currContextId;
4324 XRM_LIF_STR *retLif = lif;
4325
4326 // In N:1 redundancy, hash table on standby contains LIFs from
4327 // multiple contexts. Travserse hash table until first entry for
4328 // this context ID is found
4329 if(retLif != NULL)
4330 {
4331 while ((hashEntry = GHashNext(globalData->lifIfIdxHashTbl, &retLif->ifIdxHashEnt.ghashEntry)) != NULL)
4332 {
4333 if ((retLif = (XRM_LIF_STR *)hashEntry->item) == NULL)
4334 {
4335 return (NULL);
4336 }
4337
4338 if (retLif->ifIdxHashEnt.ctxtId == ctxtId)
4339 {
4340 // Entry is for this context ID
4341 return (retLif);
4342 }
4343 }
4344 }
4345 return (NULL);
4346 }
4347
Assistant
To understand the code snippets provided, let's break down each part and explain how they link together. The primary goal is to retrieve a Logical Interface (LIF) structure based on certain criteria. Here's a detailed explanation:
Overview
XrmLifGet Function: This function retrieves a LIF structure based on an IP address, address context, and optionally a LIF group ID.
XrmLifGetFirst Function: This function retrieves the first LIF structure in the global data.
XrmLifGetNext Function: This function retrieves the next LIF structure in the global data.
Detailed Explanation
XrmLifGet Function
c
XRM_LIF_STR *XrmLifGet(const SOCKADDR_STR *ipAddr, uint16 addrCtxt, uint16 lifGrpId)
{
XRM_LIF_GROUP_STR *lifGrp;
XRM_LIF_STR *lif;
// If LIF group Id is specified, then only search LIFs in specified LIF group
if (lifGrpId != 0)
{
// Get LIF group based on Id
if ((lifGrp = XrmLifGroupGet(lifGrpId)) == NULL)
{
return (NULL);
}
// Go thru all LIFs in this LIF group
for (lif = XrmLifGetFirstInLifGrp(lifGrp);
lif != NULL;
lif = XrmLifGetNextInLifGrp(lifGrp, lif))
{
// Check for match
if (XrmLifMatchAddrCtxtIp(addrCtxt, ipAddr, lif) == true)
{
return (lif);
}
}
}
else
{
// LIF group not specified, search all created LIFs
for (lif = XrmLifGetFirst();
lif != NULL;
lif = XrmLifGetNext(lif))
{
// Check for match
if (XrmLifMatchAddrCtxtIp(addrCtxt, ipAddr, lif) == true)
{
return (lif);
}
}
}
return (NULL);
}
if (status == SUCCESS)
{
XRM_FILL_SA(pIpsecSA, pCmd);
}
If both the deletion and addition operations are successful, the XRM_FILL_SA function is called to fill the pIpsecSA structure with the details from the pCmd structure.
Event Notification
The EventNotify function is used to log events when operations fail. It provides detailed information about the failure, including:
The event type (EVENT_XRM_IPSEC_IXP_SA_OP_FAILURE).
The system ID (SYSID_XRM).
The function name (XrmIPsecSAReplace).
The operation type (delete or add).
The tunnel ID.
The direction (IN or OUT).
The SA key.
The local and remote IP addresses.
Summary
The XrmIPsecSAReplace function is responsible for replacing an existing IPsec SA with a new one. It performs the following steps:
Deletes the existing SA.
Adds the new SA.
Notifies events if any operation fails.
Updates the SA structure with the new details if both operations are successful.
The function logs detailed information about the SA creation process, including the SPI, direction, remote and local IP addresses, and the result of the operation.
Return Statement:
c
return (ret);
The function returns the result of the SA creation process.
Summary
The XrmNPAddSA function is responsible for adding a new IPsec SA to the system. It performs the following steps:
Initializes the necessary variables and structures.
Handles the encryption ID based on the command and direction.
Prepares and creates the encryption or decryption context in the NPs based on the direction.
Logs detailed information about the SA creation process.
Returns the result of the operation.
pIpsecSA
pCmd
Why This is Done
Consistency: Ensures that the pIpsecSA structure reflects the current state of the SA after a successful replacement. This is crucial for maintaining consistency in the system's state.
Error Handling: If any step in the replacement process fails (e.g., deleting the old SA or adding the new SA), the pIpsecSA structure will not be updated. This prevents the system from operating with outdated or incorrect SA information.
Atomicity: The update to pIpsecSA is performed atomically with the success of the SA replacement operations. This ensures that the system does not end up in an inconsistent state where the SA structure is updated but the actual SA operations have failed.
Summary
The XRM_FILL_SA macro is used to update the pIpsecSA structure with the new SA details from the pCmd structure. The conditional call to XRM_FILL_SA ensures that this update only happens if the SA replacement operations are successful. This approach maintains consistency, handles errors gracefully, and ensures atomicity in the SA replacement process.
while
For each element in the list, it checks if the acId of the current pIpsecCtrl matches the given acId.
If a match is found, it returns the pointer to the current pIpsecCtrl.
If no match is found, it moves to the next element in the list using pNext.
Return NULL if Not Found:
c
return (NULL);
If the loop completes without finding a match, the function returns NULL.
Example
Let's consider an example to illustrate how the function works.
ipsecRes points to the IPsec resource pool within the global data.
pIpsecCtrl is initialized to the first element in the linked list (XRM_IPSEC_CTRL_STR1).
Traversal:
The while loop starts.
pNext is set to the next element in the list (XRM_IPSEC_CTRL_STR2).
The function checks if pIpsecCtrl->acId matches targetAcId (1001).
If no match is found, pIpsecCtrl is updated to pNext (XRM_IPSEC_CTRL_STR2).
The loop continues to the next element (XRM_IPSEC_CTRL_STR3).
If pIpsecCtrl->acId matches targetAcId (1001), the function returns pIpsecCtrl.
If the loop completes without finding a match, the function returns NULL.
Summary
The XrmIPsecCtrlLookup function traverses a linked list of IPsec control structures to find a match based on the given acId. It returns a pointer to the matching structure or NULL if no match is found. This function is useful for quickly looking up IPsec control structures by their unique identifier, which is essential for managing IPsec associations in a system.
redSAHashTbl; // Media SA redundancy
1409 LIST_ITEM_STR deferXresList; // list of XRES are not added into IPsecSAList
1410 } XRM_IPSEC_RES_POOL_STR;
1411
182 typedef struct xrm_ipsec_ctrl_str
183 {
184 /
globalData points to the globalData field within the xrmIcb structure.
IPsec Resource Pool:
ipsecRes points to the ipsecRes field within the globalData structure.
Initialization and Traversal:
pIpsecCtrl is initialized to the first element in the ipsecCtrl linked list (XRM_IPSEC_CTRL_STR1).
Traversal Loop:
The while loop starts.
pNext is set to the next element in the list (XRM_IPSEC_CTRL_STR2).
The function checks if pIpsecCtrl->acId matches targetAcId (1001).
If no match is found, pIpsecCtrl is updated to pNext (XRM_IPSEC_CTRL_STR2).
The loop continues to the next element (XRM_IPSEC_CTRL_STR3).
If pIpsecCtrl->acId matches targetAcId (1001), the function returns pIpsecCtrl.
If the loop completes without finding a match, the function returns NULL.
Summary
The XrmIPsecCtrlLookup function traverses a linked list of IPsec control structures to find a match based on the given acId. It returns a pointer to the matching structure or NULL if no match is found. This function is essential for managing IPsec associations in a system by quickly looking up control structures based on their unique identifier. The function relies on the global data structure and the IPsec resource pool to access the linked list of control structures.
LIST_STR
Linked List Traversal in XrmIPsecCtrlLookup
The XrmIPsecCtrlLookup function traverses the linked list of XRM_IPSEC_CTRL_STR structures to find a node with a matching acId. Here's a step-by-step explanation of the traversal process:
The function initializes pIpsecCtrl to the first element in the ipsecCtrl linked list. The ipsecCtrl field is a LIST_STR, and its next field points to the first XRM_IPSEC_CTRL_STR in the list.
pIpsecCtrl is initialized to the first element in the ipsecCtrl linked list (XRM_IPSEC_CTRL_STR1).
Traversal Loop:
The while loop starts.
pNext is set to the next element in the list (XRM_IPSEC_CTRL_STR2).
The function checks if pIpsecCtrl->acId matches targetAcId (1001).
If no match is found, pIpsecCtrl is updated to pNext (XRM_IPSEC_CTRL_STR2).
The loop continues to the next element (XRM_IPSEC_CTRL_STR3).
If pIpsecCtrl->acId matches targetAcId (1001), the function returns pIpsecCtrl.
If the loop completes without finding a match, the function returns NULL.
Summary
The linked list traversal in the XrmIPsecCtrlLookup function involves initializing a pointer to the first node in the list and then iterating through the list until a node with a matching acId is found or the end of the list is reached. This technique is essential for efficiently searching and managing linked lists in data structures. The use of a circular linked list ensures that the traversal can detect the end of the list by checking if the current node points back to the head of the list.
The function retrieves the global data structure using XrmGlobalDataGet().
It then gets a pointer to the ipsecRes field within the global data structure.
Understanding ipsecRes->ipsecCtrl:
ipsecRes->ipsecCtrl is a LIST_STR that serves as the head of the linked list.
The next field of this LIST_STR points to the first node in the list.
ipsecRes->ipsecCtrl is the head of the list, and its next field points to the first XRM_IPSEC_CTRL_STR (let's call it XRM_IPSEC_CTRL_STR1).
pIpsecCtrl is initialized to XRM_IPSEC_CTRL_STR1.
Example Code
Here's a simplified example to illustrate the initialization:
c
#include <stdio.h>
#include <stdlib.h>
typedef struct LIST_STR {
struct LIST_STR *next;
} LIST_STR;
typedef struct XRM_IPSEC_CTRL_STR {
LIST_STR listEnt;
unsigned short acId;
// Other fields...
} XRM_IPSEC_CTRL_STR;
typedef struct XRM_IPSEC_RES_POOL_STR {
LIST_STR ipsecCtrl;
// Other fields...
} XRM_IPSEC_RES_POOL_STR;
int main() {
// Create a sample linked list
XRM_IPSEC_CTRL_STR node1 = {{NULL}, 1001};
XRM_IPSEC_CTRL_STR node2 = {{NULL}, 1002};
XRM_IPSEC_CTRL_STR node3 = {{NULL}, 1003};
XRM_IPSEC_RES_POOL_STR ipsecRes;
ipsecRes.ipsecCtrl.next = (LIST_STR *)&node1;
node1.listEnt.next = (LIST_STR *)&node2;
node2.listEnt.next = (LIST_STR *)&node3;
node3.listEnt.next = NULL;
// Initialize pIpsecCtrl to the first element in the list
XRM_IPSEC_CTRL_STR *pIpsecCtrl = (XRM_IPSEC_CTRL_STR *)ipsecRes.ipsecCtrl.next;
// Print the acId of the first node
printf("First node acId: %hu\n", pIpsecCtrl->acId);
return 0;
}
Summary
The initialization process sets pIpsecCtrl to the first element in the ipsecCtrl linked list by following the next pointer of the ipsecCtrl field, which is a LIST_STR. This LIST_STR serves as the head of the list and points to the first XRM_IPSEC_CTRL_STR in the list. The cast (XRM_IPSEC_CTRL_STR *) is used to convert the LIST_STR * to an XRM_IPSEC_CTRL_STR *, allowing pIpsecCtrl to correctly point to the first node in the list.
pIpsecCtrl is initialized to the first element in the ipsecCtrl linked list.
Traversal Loop:
The while loop continues as long as pIpsecCtrl does not point back to the head of the list (ipsecRes->ipsecCtrl).
Inside the loop, pNext is set to the next node in the list using pIpsecCtrl->listEnt.next.
The type cast (XRM_IPSEC_CTRL_STR *) ensures that pNext is treated as an XRM_IPSEC_CTRL_STR *.
Check for Match:
The function checks if the acId of the current pIpsecCtrl matches the given acId.
If a match is found, the function returns pIpsecCtrl.
Advance to Next Node:
If no match is found, pIpsecCtrl is updated to pNext, advancing the traversal to the next node in the list.
Example Code
Here's a simplified example to illustrate the traversal process:
c
#include <stdio.h>
#include <stdlib.h>
typedef struct LIST_STR {
struct LIST_STR *next;
} LIST_STR;
typedef struct XRM_IPSEC_CTRL_STR {
LIST_STR listEnt;
unsigned short acId;
// Other fields...
} XRM_IPSEC_CTRL_STR;
typedef struct XRM_IPSEC_RES_POOL_STR {
LIST_STR ipsecCtrl;
// Other fields...
} XRM_IPSEC_RES_POOL_STR;
int main() {
// Create a sample linked list
XRM_IPSEC_CTRL_STR node1 = {{NULL}, 1001};
XRM_IPSEC_CTRL_STR node2 = {{NULL}, 1002};
XRM_IPSEC_CTRL_STR node3 = {{NULL}, 1003};
XRM_IPSEC_RES_POOL_STR ipsecRes;
ipsecRes.ipsecCtrl.next = (LIST_STR *)&node1;
node1.listEnt.next = (LIST_STR *)&node2;
node2.listEnt.next = (LIST_STR *)&node3;
node3.listEnt.next = (LIST_STR *)&ipsecRes.ipsecCtrl; // Circular link
// Initialize pIpsecCtrl to the first element in the list
XRM_IPSEC_CTRL_STR *pIpsecCtrl = (XRM_IPSEC_CTRL_STR *)ipsecRes.ipsecCtrl.next;
// Traverse the list and print acIds
while ((LIST_STR *)pIpsecCtrl != &ipsecRes.ipsecCtrl) {
XRM_IPSEC_CTRL_STR *pNext = (XRM_IPSEC_CTRL_STR *)pIpsecCtrl->listEnt.next;
printf("Current node acId: %hu\n", pIpsecCtrl->acId);
pIpsecCtrl = pNext;
}
return 0;
}
Summary
The line XRM_IPSEC_CTRL_STR *pNext = (XRM_IPSEC_CTRL_STR *)pIpsecCtrl->listEnt.next; is used to safely advance the traversal of the linked list. It ensures that we have a pointer to the next node in the list before potentially modifying pIpsecCtrl. This is crucial for maintaining the integrity of the list traversal, especially in a loop where pIpsecCtrl is updated on each iteration. The type cast is necessary to convert the generic LIST_STR * to a specific XRM_IPSEC_CTRL_STR *, allowing access to the fields of the XRM_IPSEC_CTRL_STR structure.
The function is called with targetAcId set to 1002.
Traversal:
The function initializes pIpsecCtrl to XRM_IPSEC_CTRL_STR1.
It checks if XRM_IPSEC_CTRL_STR1->acId matches targetAcId (1002). It does not match.
It advances to XRM_IPSEC_CTRL_STR2.
It checks if XRM_IPSEC_CTRL_STR2->acId matches targetAcId (1002). It matches.
The function returns XRM_IPSEC_CTRL_STR2.
Summary
The XrmIPsecCtrlLookup function traverses a linked list of XRM_IPSEC_CTRL_STR structures to find a match based on the given acId. It returns a pointer to the matching structure or NULL if no match is found. This function is essential for managing IPsec associations in a system by quickly looking up control structures based on their unique identifier. The linked list traversal ensures efficient searching and maintenance of the IPsec control structures.
If the check fails, the macro calls SYS_ERR with SE_CRASH and SE_SYS_LIST_INSERT to indicate a critical error.
Setting the next Pointer of the New Element:
c
((LIST_STR *)elem)->next = (LIST_STR *)list;
The next pointer of the new element (elem) is set to point to the head of the list (list). This is because the new element will be the last element in the list.
This check ensures that the list is in a consistent state.
((LIST_STR *)list)->prev->next should point back to the head of the list (list). If this is not the case, it indicates a potential corruption in the list structure.
If the check fails, the macro calls SYS_ERR with SE_CRASH and SE_SYS_LIST_INSERT to indicate a critical error.
Setting the next Pointer of the New Element:
c
((LIST_STR *)elem)->next = (LIST_STR *)list;
The next pointer of the new element (elem) is set to point to the head of the list (list). This is because the new element will be the last element in the list.
The next pointer of the current last element (pointed to by list->prev) is updated to point to the new element (elem).
Updating the prev Pointer of the List Head:
c
((LIST_STR *)list)->prev = (LIST_STR *)elem;
The prev pointer of the list head (list) is updated to point to the new element (elem), making it the new last element in the list.
Summary
"Pointed to by list->prev" refers to the node that the prev pointer of the head node is currently pointing to, which is the last node in the list.
The LISTINSTAIL_GUTS macro inserts a new element at the tail of a doubly linked list by updating the next and prev pointers of the relevant nodes to maintain the list's integrity and consistency.
This check ensures that the list is in a consistent state.
((LIST_STR *)list)->prev->next should point back to the head of the list (list). If this is not the case, it indicates a potential corruption in the list structure.
If the check fails, the macro calls SYS_ERR with SE_CRASH and SE_SYS_LIST_INSERT to indicate a critical error.
Setting the next Pointer of the New Element:
c
((LIST_STR *)elem)->next = (LIST_STR *)list;
The next pointer of the new element (elem) is set to point to the head of the list (list). This is because the new element will be the last element in the list.
The prev pointer of the new element (elem) is set to point to the current last element in the list (which is pointed to by list->prev).
Detailed Explanation of ((LIST_STR *)elem)->prev = ((LIST_STR *)list)->prev;
((LIST_STR *)list)->prev points to the last node in the list.
((LIST_STR *)elem)->prev is set to this last node.
This means that the new element (elem) is being inserted at the tail of the list. The prev pointer of the new element is set to point to the current last node, establishing the backward link in the doubly linked list.
Visual Representation of Insertion
Before Insertion
code
head (list) <-> [Node1] <-> [Node2] <-> [Node3] <-> head (list)
list->prev points to Node3.
Node3->next points to head.
After Insertion of elem
code
head (list) <-> [Node1] <-> [Node2] <-> [Node3] <-> [elem] <-> head (list)
list->prev now points to elem.
elem->next points to head.
elem->prev points to Node3.
Node3->next now points to elem.
Summary
"Pointed to by list->prev" refers to the last node in the doubly linked list.
The LISTINSTAIL_GUTS macro inserts a new element at the tail of the list by updating the next and prev pointers of the relevant nodes.
The line ((LIST_STR *)elem)->prev = ((LIST_STR *)list)->prev; sets the prev pointer of the new element to point to the current last node in the list, establishing the backward link in the doubly linked list.
This check ensures that the list is in a consistent state.
((LIST_STR *)list)->prev->next should point back to the head of the list (list). If this is not the case, it indicates a potential corruption in the list structure.
If the check fails, the macro calls SYS_ERR with SE_CRASH and SE_SYS_LIST_INSERT to indicate a critical error.
Visual Representation (Before Sanity Check):
code
head (list) <-> [Node1] <-> [Node2] <-> [Node3] <-> head (list)
Setting the next Pointer of the New Element:
c
((LIST_STR *)elem)->next = (LIST_STR *)list;
The next pointer of the new element (elem) is set to point to the head of the list (list). This is because the new element will be the last element in the list.
Visual Representation (After Setting next Pointer):
code
head (list) <-> [Node1] <-> [Node2] <-> [Node3] <-> head (list)
elem -> next = head (list)
The next pointer of the current last element (pointed to by list->prev) is updated to point to the new element (elem).
Visual Representation (After Updating next Pointer of Current Last Element):
code
head (list) <-> [Node1] <-> [Node2] <-> [Node3] -> elem -> head (list)
elem -> next = head (list)
elem -> prev = Node3
Updating the prev Pointer of the List Head:
c
((LIST_STR *)list)->prev = (LIST_STR *)elem;
The prev pointer of the list head (list) is updated to point to the new element (elem), making it the new last element in the list.
Visual Representation (After Updating prev Pointer of List Head):
code
head (list) <-> [Node1] <-> [Node2] <-> [Node3] -> elem -> head (list)
head (list) -> prev = elem
elem -> next = head (list)
elem -> prev = Node3
Final State
After all the steps, the new element (elem) is successfully inserted at the tail of the list. The list now looks like this:
code
head (list) <-> [Node1] <-> [Node2] <-> [Node3] <-> [elem] <-> head (list)
head->next points to Node1.
head->prev points to elem.
Node1->prev points to head.
Node1->next points to Node2.
Node2->prev points to Node1.
Node2->next points to Node3.
Node3->prev points to Node2.
Node3->next points to elem.
elem->prev points to Node3.
elem->next points to head.
Summary
The LISTINSTAIL_GUTS macro inserts a new element at the tail of a doubly linked list by performing the following steps:
Ensures the list is in a consistent state.
Sets the next pointer of the new element to point to the head of the list.
Sets the prev pointer of the new element to point to the current last element in the list.
Updates the next pointer of the current last element to point to the new element.
Updates the prev pointer of the list head to point to the new element.
This macro maintains the integrity and consistency of the doubly linked list when inserting new elements at the tail.
link and explain
Parameters:
ipAddr: The IP address to match.
addrCtxt: The address context to match.
lifGrpId: The LIF group ID (optional).
Logic:
If lifGrpId is specified, the function searches within the specified LIF group.
If lifGrpId is not specified, the function searches all created LIFs.
The function uses XrmLifGetFirstInLifGrp and XrmLifGetNextInLifGrp to traverse LIFs within a group.
The function uses XrmLifGetFirst and XrmLifGetNext to traverse all LIFs.
The function checks for a match using XrmLifMatchAddrCtxtIp.
XrmLifGetFirst Function
c
XRM_LIF_STR *XrmLifGetFirst(void)
{
XRM_GLOBAL_DATA_STR *globalData = XrmGlobalDataGet();
GHASH_ENTRY *hashEntry;
XRM_LIF_STR *lif;
uint16 ctxtId = XrmIcbGet()->currContextId;
// In N:1 redundancy, hash table on standby contains LIFs from multiple contexts. Get first entry
if (((hashEntry = GHashFirst(globalData->lifIfIdxHashTbl)) == NULL) ||
((lif = (XRM_LIF_STR *)hashEntry->item) == NULL))
{
return (NULL);
}
if (lif->ifIdxHashEnt.ctxtId == ctxtId)
{
// First entry is for this context ID
return (lif);
}
// First entry was not for this context ID. Traverse hash table until first entry for this context ID is found
lif = XrmLifGetNext(lif);
return (lif);
}
Logic:
The function retrieves the first entry from the lifIfIdxHashTbl hash table.
If the first entry matches the current context ID, it returns the LIF.
If the first entry does not match the current context ID, it traverses the hash table using XrmLifGetNext until it finds a matching entry.
XrmLifGetNext Function
c
XRM_LIF_STR *XrmLifGetNext(XRM_LIF_STR *lif)
{
XRM_GLOBAL_DATA_STR *globalData = XrmGlobalDataGet();
GHASH_ENTRY *hashEntry;
uint16 ctxtId = XrmIcbGet()->currContextId;
XRM_LIF_STR *retLif = lif;
// In N:1 redundancy, hash table on standby contains LIFs from multiple contexts. Traverse hash table until first entry for this context ID is found
if (retLif != NULL)
{
while ((hashEntry = GHashNext(globalData->lifIfIdxHashTbl, &retLif->ifIdxHashEnt.ghashEntry)) != NULL)
{
if ((retLif = (XRM_LIF_STR *)hashEntry->item) == NULL)
{
return (NULL);
}
if (retLif->ifIdxHashEnt.ctxtId == ctxtId)
{
// Entry is for this context ID
return (retLif);
}
}
}
return (NULL);
}
Logic:
The function traverses the lifIfIdxHashTbl hash table starting from the given LIF.
It continues to traverse until it finds an entry that matches the current context ID.
If a matching entry is found, it returns the LIF.
If no matching entry is found, it returns NULL.
Linking the Code Snippets
Let's link the code snippets together to understand how they work in the context of retrieving a LIF structure.
Example Usage
c
if ((lif = XrmLifGet(pstLocal, pIpsecCtrl->acId, 0)) == NULL)
{
// Handle the case where the LIF is not found
}
Explanation:
XrmLifGet is called with pstLocal (assumed to be a SOCKADDR_STR representing the IP address), pIpsecCtrl->acId (assumed to be the address context), and 0 (indicating that no specific LIF group ID is specified).
XrmLifGet will search all created LIFs for a match.
If a match is found, lif will point to the matching LIF structure.
If no match is found, lif will be NULL.
Internal Workings of XrmLifGet
Initialization:
The function initializes lifGrp and lif to NULL.
LIF Group Check:
If lifGrpId is specified, the function retrieves the LIF group using XrmLifGroupGet.
If the LIF group is not found, the function returns NULL.
Traversing LIFs:
If lifGrpId is specified, the function traverses LIFs within the specified group using XrmLifGetFirstInLifGrp and XrmLifGetNextInLifGrp.
If lifGrpId is not specified, the function traverses all LIFs using XrmLifGetFirst and XrmLifGetNext.
Matching LIF:
The function checks for a match using XrmLifMatchAddrCtxtIp.
If a match is found, the function returns the matching LIF.
If no match is found, the function returns NULL.
Internal Workings of XrmLifGetFirst
Retrieving First Entry:
The function retrieves the first entry from the lifIfIdxHashTbl hash table.
If the first entry matches the current context ID, it returns the LIF.
If the first entry does not match the current context ID, it traverses the hash table using XrmLifGetNext until it finds a matching entry.
Internal Workings of XrmLifGetNext
Traversing Hash Table:
The function traverses the lifIfIdxHashTbl hash table starting from the given LIF.
It continues to traverse until it finds an entry that matches the current context ID.
If a matching entry is found, it returns the LIF.
If no matching entry is found, it returns NULL.
Summary
The XrmLifGet function is used to retrieve a LIF structure based on an IP address, address context, and optionally a LIF group ID. It traverses the LIFs using XrmLifGetFirst and XrmLifGetNext to find a matching LIF. The XrmLifGetFirst function retrieves the first LIF in the global data, and the XrmLifGetNext function retrieves the next LIF in the global data. These functions work together to efficiently search and retrieve LIF structures based on the specified criteria.