E2AP code changes
[o-du/l2.git] / src / cu_stub / cu_stub_sctp.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18
19 /* This file contains all SCTP related functionality */
20
21 #include <stdio.h> 
22 #include "cu_f1ap_msg_hdl.h"
23 #include "cu_stub_sctp.h"
24 #include "cu_stub_egtp.h"
25
26 CuSctpDestCb f1Params;
27 /**************************************************************************
28  * @brief Task Initiation callback function. 
29  *
30  * @details
31  *
32  *     Function : sctpActvInit 
33  *    
34  *     Functionality:
35  *             This function is supplied as one of parameters during SCTP's 
36  *             task registration. SSI will invoke this function once, after
37  *             it creates and attaches this TAPA Task to a system task.
38  *     
39  * @param[in]  Ent entity, the entity ID of this task.     
40  * @param[in]  Inst inst, the instance ID of this task.
41  * @param[in]  Region region, the region ID registered for memory 
42  *              usage of this task.
43  * @param[in]  Reason reason.
44  * @return ROK     - success
45  *         RFAILED - failure
46  ***************************************************************************/
47 S16 sctpActvInit()
48 {
49    DU_LOG("\n\nSCTP : Initializing");
50    connUp = FALSE;
51    assocId = 0;
52    nonblocking = FALSE;
53    sctpCfg = cuCfgParams.sctpParams;
54    return ROK;
55
56 }
57
58 /**************************************************************************
59  * @brief Task Activation callback function. 
60  *
61  * @details
62  *
63  *      Function : sctpActvTsk 
64  * 
65  *      Functionality:
66  *           This function handles all SCTP messages received
67  *           This API is registered with SSI during the 
68  *           Task Registration of DU APP.
69  *     
70  * @param[in]  Pst     *pst, Post structure of the primitive.     
71  * @param[in]  Buffer *mBuf, Packed primitive parameters in the
72  *  buffer.
73  * @return ROK     - success
74  *         RFAILED - failure
75  *
76  ***************************************************************************/
77 S16 sctpActvTsk(Pst *pst, Buffer *mBuf)
78 {
79
80 //TODO: TBD
81    return ROK;
82 }
83
84 /**************************************************************************
85  * @brief Function to configure the Sctp Params during config Request
86  *
87  * @details
88  *
89  *      Function : duSctpCfgReq
90  * 
91  *      Functionality:
92  *           This function configures SCTP Params during the config Request
93  *     
94  * @return ROK     - success
95  *         RFAILED - failure
96  *
97  ***************************************************************************/
98
99 S16 sctpCfgReq()
100 {
101
102 /* Fill F1 Params */
103    f1Params.destPort             = sctpCfg.duPort;
104    f1Params.srcPort              = sctpCfg.cuPort;
105    f1Params.bReadFdSet           = ROK;
106    cmMemset ((U8 *)&f1Params.sockFd, -1, sizeof(CmInetFd));
107    cmMemset ((U8 *)&f1Params.lstnSockFd, -1, sizeof(CmInetFd));
108    fillDestNetAddr(&f1Params.destIpNetAddr, &sctpCfg.duIpAddr);
109
110 /* Set polling to FALSE */
111    pollingState = FALSE;  
112
113  RETVALUE(ROK);
114 }
115
116
117 /*******************************************************************
118  *
119  * @brief Fills the address List of the source Ip Address
120  *
121  * @details
122  *
123  *    Function : fillAddrLst
124  *
125  *    Functionality:
126  *       Fills the address List of source Ip Address
127  *
128  * @params[in] CmInetNetAddrLst *addrLstPtr, Address List pointer
129  * @params[in] F1IpAddr *srcIpAddr, src Ip Adrress to be filled in the Address List
130  * @return ROK     - success
131  *         RFAILED - failure
132  *
133  ******************************************************************/
134
135 S16 fillAddrLst(CmInetNetAddrLst *addrLstPtr, SctpIpAddr *ipAddr)
136
137    addrLstPtr->count++;
138    addrLstPtr->addrs[(addrLstPtr->count - 1)].type = CM_INET_IPV4ADDR_TYPE;
139    addrLstPtr->addrs[(addrLstPtr->count - 1)].u.ipv4NetAddr = CM_INET_NTOH_U32(ipAddr->ipV4Addr);
140
141    RETVALUE(ROK);
142 }
143
144 /******************************************************************************
145  *
146  * @brief Fills the address List of the source Ip Address
147  *
148  * @details
149  *
150  *    Function : fillDestNetAddr
151  *
152  *    Functionality:
153  *       Fills the address List of destinatoion Ip Address
154  *
155  * @params[in] CmInetNetAddr *destAddrPtr, Address List pointer
156  * @params[in] F1IpAddr *dstIpAddr, destIp Address to be filled in the Address List
157  * @return ROK     - success
158  *         RFAILED - failure
159  *
160  *******************************************************************************/
161 S16 fillDestNetAddr(CmInetNetAddr *destAddrPtr, SctpIpAddr *dstIpPtr)
162 {
163    /* Filling destination address */
164    destAddrPtr->type = CM_INET_IPV4ADDR_TYPE;
165    destAddrPtr->u.ipv4NetAddr = CM_INET_NTOH_U32(dstIpPtr->ipV4Addr);
166    RETVALUE(ROK);
167 }
168
169 /******************************************************************************
170  *
171  * @brief Eastablishes the Assoc Req for the received interface type
172  *
173  * @details
174  *
175  *    Function : sctpStartReq
176  *
177  *    Functionality:
178  *       Eastablishes the Assoc Req for the received interface type
179  *
180  * @params[in] DuSctpDestCb *paramPtr
181  * @return ROK     - success
182  *         RFAILED - failure
183  *
184  *******************************************************************************/
185
186 S16 sctpStartReq()
187 {
188    S16 ret = ROK;
189    socket_type = CM_INET_STREAM;
190    fillAddrLst(&f1Params.localAddrLst, &sctpCfg.cuIpAddr);
191
192    if((ret = cmInetSocket(socket_type, &f1Params.lstnSockFd, IPPROTO_SCTP) != ROK))
193    {
194       DU_LOG("\nSCTP : Socket[%d] coudnt open for listening", f1Params.lstnSockFd.fd);
195    } 
196    else if((ret = cmInetSctpBindx(&f1Params.lstnSockFd, &f1Params.localAddrLst, f1Params.srcPort)) != ROK)
197    {
198       DU_LOG("\nSCTP: Binding failed at CU");
199    }
200    else if((ret = sctpAccept(&f1Params.lstnSockFd, &f1Params.peerAddr, &f1Params.sockFd)) != ROK)
201    {
202       DU_LOG("\nSCTP: Unable to accept the connection at CU");
203    }
204    else if(sctpSockPoll() != ROK)
205    {
206       DU_LOG("\nSCTP: Polling failed to start at CU");
207    }
208    RETVALUE(ret);
209 }
210 /*******************************************************************
211  *
212  * @brief Sets socket options as per requirement
213  *
214  * @details
215  *
216  *    Function : sctpSetSockOpts
217  *
218  *    Functionality: 
219  *       Sets socket options as per requirement
220  *
221  * @params[in] 
222  * @return ROK     - success
223  *         RFAILED - failure
224  *
225  * ****************************************************************/
226 S16 sctpSetSockOpts(CmInetFd *sock_Fd)
227 {
228    S16  ret = ROK;
229    CmSctpEvent sctpEvent;
230   
231    sctpEvent.dataIoEvent          = TRUE;
232    sctpEvent.associationEvent     = TRUE;
233    sctpEvent.addressEvent         = TRUE;
234    sctpEvent.sendFailureEvent     = TRUE;
235    sctpEvent.peerErrorEvent       = TRUE;
236    sctpEvent.shutdownEvent        = TRUE;
237    sctpEvent.partialDeliveryEvent = TRUE;
238    sctpEvent.adaptationLayerEvent = TRUE;
239
240    if((ret = cmInetSetOpt(sock_Fd, CM_SOCKOPT_LEVEL_SCTP, CM_SOCKOPT_OPT_SCTP_EVENTS, &sctpEvent)) != ROK)
241    {
242      ret = RFAILED;
243    }
244   
245    RETVALUE(ret);
246 }
247
248 /*******************************************************************
249  *
250  * @brief Initiates connection with peer SCTP
251  *
252  * @details
253  *
254  *    Function : sctpAccept
255  *
256  *    Functionality:
257  *       Establishes SCTP connection with peer.
258  *       Here, DU-SCTP will initate connection towards CU-SCTP
259  *
260  * @params[in] 
261  * @return ROK     - success
262  *         RFAILED - failure
263  *
264  * ****************************************************************/
265 S16 sctpAccept(CmInetFd *lstnSock_Fd, CmInetAddr *peerAddr, CmInetFd *sock_Fd)
266 {
267    U8  ret;
268    ret = cmInetListen(lstnSock_Fd, 1);
269    if (ret != ROK)
270    {
271       DU_LOG("\nSCTP : Listening on socket failed");
272       cmInetClose(lstnSock_Fd);
273       RETVALUE(RFAILED);
274    }
275    
276    DU_LOG("\nSCTP : Connecting");
277
278    while(!connUp)
279    {
280       ret = cmInetAccept(lstnSock_Fd, peerAddr, sock_Fd);
281       if (ret == ROKDNA)
282       {
283          continue;
284       }
285       else if(ret != ROK)
286       {
287          DU_LOG("\nSCTP : Failed to accept connection");
288          RETVALUE(RFAILED);
289       }
290       else
291       {
292          connUp = TRUE;
293          sctpSetSockOpts(sock_Fd);
294          break;
295       }
296    }
297    DU_LOG("\nSCTP : Connection established");
298
299    RETVALUE(ROK);
300 }
301
302 /*******************************************************************
303  *
304  * @brief Handles an SCTP notification message
305  *
306  * @details
307  *
308  *    Function : sctpNtfyHdlr
309  *
310  *    Functionality:
311  *      Handles an SCTP notification message
312  *
313  * @params[in] Notify message
314  *
315  * @return ROK     - success
316  *         RFAILED - failure
317  *
318  * ****************************************************************/
319 S16 sctpNtfyHdlr(CmInetSctpNotification *ntfy)
320 {
321    switch(ntfy->header.nType)
322    {
323       case CM_INET_SCTP_ASSOC_CHANGE :
324          DU_LOG("\nSCTP : Assoc change notification received");
325          switch(ntfy->u.assocChange.state)
326          {
327             case CM_INET_SCTP_COMM_UP:
328                DU_LOG("Event : COMMUNICATION UP");
329                connUp = TRUE;
330                break;
331             case CM_INET_SCTP_COMM_LOST:
332                DU_LOG("Event : COMMUNICATION LOST");
333                connUp = FALSE;
334                break;
335             case CM_INET_SCTP_RESTART:
336                DU_LOG("Event : SCTP RESTART");
337                connUp = FALSE;
338                break;
339             case CM_INET_SCTP_SHUTDOWN_COMP: /* association gracefully shutdown */
340                DU_LOG("Event : SHUTDOWN COMPLETE");
341                connUp = FALSE;
342                break;
343             case CM_INET_SCTP_CANT_STR_ASSOC:
344                DU_LOG("Event : CANT START ASSOC");
345                connUp = FALSE;
346                break;
347             default:
348                DU_LOG("\nInvalid event");
349                break;
350          }
351          break;
352       case CM_INET_SCTP_PEER_ADDR_CHANGE :
353          DU_LOG("\nSCTP : Peer Address Change notificarion received");
354          /* Need to add handler */
355          break;
356       case CM_INET_SCTP_REMOTE_ERROR :
357          DU_LOG("\nSCTP : Remote Error notification received");
358          break;
359       case CM_INET_SCTP_SEND_FAILED :
360          DU_LOG("\nSCTP : Send Failed notification received\n");
361          break;
362       case CM_INET_SCTP_SHUTDOWN_EVENT : /* peer socket gracefully closed */
363          DU_LOG("\nSCTP : Shutdown Event notification received\n");
364          connUp = FALSE;
365          exit(0);
366          break;
367       case CM_INET_SCTP_ADAPTATION_INDICATION :
368          DU_LOG("\nSCTP : Adaptation Indication received\n");
369          break;
370       case CM_INET_SCTP_PARTIAL_DELIVERY_EVENT:
371          DU_LOG("\nSCTP : Partial Delivery Event received\n");
372          break;
373       default:
374          DU_LOG("\nSCTP : Invalid notification type\n");
375          break;
376    }
377
378    sctpNtfyInd(ntfy);
379    RETVALUE(ROK);
380 }/* End of sctpNtfyHdlr */
381
382 /*******************************************************************
383  *
384  * @brief Receives message on the socket
385  *
386  * @details
387  *
388  *    Function : sctpSockPoll
389  *
390  *    Functionality:
391  *      Receives message on the socket
392  *
393  * @params[in] 
394  * @return ROK     - success
395  *         RFAILED - failure
396  *
397  * ****************************************************************/
398 S16 sctpSockPoll()
399 {
400    U16 ret = ROK;
401    U32           timeout;
402    U32           *timeoutPtr;
403    Buffer        *egtpBuf;
404    MsgLen        egtpBufLen;
405    CmInetAddr    egtpFromAddr;
406    CmInetMemInfo memInfo;
407    sctpSockPollParams f1PollParams;
408
409    memset(&f1PollParams, 0, sizeof(sctpSockPollParams));
410     
411    egtpFromAddr.port = egtpCb.dstCb.dstPort;
412    egtpFromAddr.address = egtpCb.dstCb.dstIp;
413
414    if(f1Params.sockFd.blocking)
415    {
416       /* blocking */
417       timeoutPtr = NULLP;
418    }
419    else
420    {
421       /* non-blocking */
422       timeout = 0;
423       timeoutPtr = &timeout;
424    }
425    memInfo.region = CU_APP_MEM_REG;
426    memInfo.pool   = CU_POOL;
427    
428    CM_INET_FD_ZERO(&f1PollParams.readFd);
429
430    while(1)
431    {
432       if((ret = processPolling(&f1PollParams, &f1Params.sockFd, timeoutPtr, &memInfo)) != ROK)
433       {
434          DU_LOG("\nSCTP : Failed to RecvMsg for F1 at CU\n");
435       }
436
437       /* Receiving EGTP data */
438       egtpBufLen = -1;
439       ret = cmInetRecvMsg(&(egtpCb.recvTptSrvr.sockFd), &egtpFromAddr, &memInfo, &egtpBuf, &egtpBufLen, CM_INET_NO_FLAG);
440       if(ret == ROK && egtpBuf != NULLP)
441       {
442          DU_LOG("\nEGTP : Received message \n");
443          SPrntMsg(egtpBuf, 0 ,0);
444          cuEgtpHdlRecvMsg(egtpBuf);
445
446       }
447    };
448    RETVALUE(ret);
449 }/* End of sctpSockPoll() */
450
451 /*******************************************************************
452  *
453  * @brief checks for valid readFd and process the InetSctpRecvMsg
454  * during polling 
455  *
456  * @details
457  *
458  *    Function : processPolling
459  *
460  *    Functionality:
461  *         checks for valid readFd and process the InetSctpRecvMsg
462  *         during polling
463  *
464  * @params[in]  Params required for polling
465  * @params[in]  SockFd for file descriptor
466  * @params[in]  timeoutPtr indicates the timeout value
467  * @params[in]  MemInfo indicates memory region
468  *
469  * @return ROK     - success
470  *         RFAILED - failure
471  *
472  * ****************************************************************/
473   
474 S16 processPolling(sctpSockPollParams *pollParams, CmInetFd *sockFd, U32 *timeoutPtr, CmInetMemInfo *memInfo)
475 {
476    U16 ret = ROK;
477    CM_INET_FD_SET(sockFd, &pollParams->readFd);
478    ret = cmInetSelect(&pollParams->readFd, NULLP, timeoutPtr, &pollParams->numFds);
479    if(CM_INET_FD_ISSET(sockFd, &pollParams->readFd))
480    {
481       CM_INET_FD_CLR(sockFd, &pollParams->readFd);
482       ret = cmInetSctpRecvMsg(sockFd, &pollParams->addr, &pollParams->port, memInfo, &pollParams->mBuf, &pollParams->bufLen, &pollParams->info, &pollParams->flag, &pollParams->ntfy);
483       if(connUp & (ret != ROK))
484       {
485          f1Params.bReadFdSet = RFAILED;
486       }
487       else
488       {
489          if(((pollParams->flag & CM_INET_SCTP_MSG_NOTIFICATION) != 0) && (ret == ROK))
490          {
491             ret = sctpNtfyHdlr(&pollParams->ntfy);
492             if(ret != ROK)
493             {
494                DU_LOG("\nSCTP : Failed to process sctp notify msg\n");
495             }
496          }
497          else if(connUp & (pollParams->port == f1Params.destPort))
498          {  
499             F1APMsgHdlr(pollParams->mBuf);
500             SPutMsg(pollParams->mBuf);
501          }
502          else
503          {
504             SPutMsg(pollParams->mBuf);
505          }
506       } 
507   }
508   RETVALUE(ROK);
509 }/* End of sctpSockPoll() */
510
511 /*******************************************************************
512  *
513  * @brief Send message on SCTP socket
514  *
515  * @details
516  *
517  *    Function : sctpSend 
518  *
519  *    Functionality:
520  *        Send message on SCTP socket
521  *
522  * @params[in] 
523  * @return ROK     - success
524  *         RFAILED - failure
525  *
526  * ****************************************************************/
527 S16 sctpSend(Buffer *mBuf)
528 {
529    U8               ret;
530    MsgLen           len;          /* number of actually sent octets */
531    CmInetMemInfo    memInfo;                        
532    
533    memInfo.region = CU_APP_MEM_REG;               
534    memInfo.pool   = CU_POOL;
535
536    ret = cmInetSctpSendMsg(&f1Params.sockFd, &f1Params.destIpNetAddr, f1Params.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
537
538    if(ret != ROK && ret != RWOULDBLOCK)
539    {
540       DU_LOG("\nSCTP : Send message failed");
541       RETVALUE(RFAILED);
542    }
543
544    RETVALUE(ROK);
545 } /* End of sctpSend */
546
547 /**********************************************************************
548          End of file
549 **********************************************************************/