blob: 9dfbf9d89189167d8d97f42381f2a8f2c676cd2d [file] [log] [blame]
/*
* Copyright 2019 AT&T Intellectual Property
* Copyright 2019 Nokia
*
* 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.
*/
/*
* This source code is part of the near-RT RIC (RAN Intelligent Controller)
* platform project (RICP).
*/
#ifndef X2_SCTP_THREAD_H
#define X2_SCTP_THREAD_H
#include <algorithm>
#include <cstdio>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <random>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/sctp.h>
#include <thread>
#include <atomic>
#include <sys/param.h>
#include <sys/file.h>
#include <ctime>
#include <netdb.h>
#include <sys/epoll.h>
#include <mutex>
#include <shared_mutex>
#include <iterator>
#include <map>
#include <sys/inotify.h>
#include <csignal>
#include <rmr/rmr.h>
#include <rmr/RIC_message_types.h>
#include <mdclog/mdclog.h>
#include <functional>
#include <iostream>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/move/utility.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/filesystem.hpp>
#include <mdclog/mdclog.h>
#include "oranE2/E2AP-PDU.h"
#include "oranE2/ProtocolIE-Container.h"
#include "oranE2/InitiatingMessage.h"
#include "oranE2/SuccessfulOutcome.h"
#include "oranE2/UnsuccessfulOutcome.h"
#include "oranE2/ProtocolIE-Container.h"
#include "oranE2/ProtocolIE-Field.h"
#include "oranE2/GlobalE2node-gNB-ID.h"
#include "oranE2/GlobalE2node-en-gNB-ID.h"
#include "oranE2/GlobalE2node-ng-eNB-ID.h"
#include "oranE2/GlobalE2node-eNB-ID.h"
#include "cxxopts.hpp"
//#include "config-cpp/include/config-cpp/config-cpp.h"
#ifdef __TRACING__
#include "openTracing.h"
#endif
#include "mapWrapper.h"
#include "base64.h"
#include "ReadConfigFile.h"
using namespace std;
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace keywords = boost::log::keywords;
namespace sinks = boost::log::sinks;
namespace posix_time = boost::posix_time;
namespace expr = boost::log::expressions;
#define SRC_PORT 36422
#define SA struct sockaddr
#define MAX_ENODB_NAME_SIZE 64
#define MAXEVENTS 128
#define RECEIVE_SCTP_BUFFER_SIZE (8*1024)
#define RECEIVE_XAPP_BUFFER_SIZE RECEIVE_SCTP_BUFFER_SIZE
typedef mapWrapper Sctp_Map_t;
#ifdef __TRACING__
typedef const std::unique_ptr<opentracing::Span> otSpan;
#else
typedef const int otSpan;
#endif
#define VOLUME_URL_SIZE 256
typedef struct sctp_params {
uint16_t rmrPort = 0;
int epoll_fd = 0;
int listenFD = 0;
int rmrListenFd = 0;
int inotifyFD = 0;
int inotifyWD = 0;
void *rmrCtx = nullptr;
Sctp_Map_t *sctpMap = nullptr;
char ka_message[4096] {};
int ka_message_length = 0;
char rmrAddress[256] {}; // "tcp:portnumber" "tcp:5566" listen to all address on port 5566
mdclog_severity_t logLevel = MDCLOG_INFO;
char volume[VOLUME_URL_SIZE];
string myIP {};
string fqdn {};
string podName {};
string configFilePath {};
string configFileName {};
bool trace = true;
//shared_timed_mutex fence; // moved to mapWrapper
} sctp_params_t;
typedef struct ConnectedCU {
int fileDescriptor = 0;
char hostName[NI_MAXHOST] {};
char portNumber[NI_MAXSERV] {};
char enodbName[MAX_ENODB_NAME_SIZE] {};
char asnData[RECEIVE_SCTP_BUFFER_SIZE] {};
size_t asnLength = 0;
int mtype = 0;
bool isConnected = false;
bool gotSetup = false;
sctp_params_t *sctpParams = nullptr;
} ConnectedCU_t ;
#define MAX_RMR_BUFF_ARRY 32
typedef struct RmrMessagesBuffer {
char ka_message[4096] {};
int ka_message_len = 0;
void *rmrCtx = nullptr;
rmr_mbuf_t *sendMessage= nullptr;
rmr_mbuf_t *sendBufferedMessages[MAX_RMR_BUFF_ARRY] {};
rmr_mbuf_t *rcvMessage= nullptr;
rmr_mbuf_t *rcvBufferedMessages[MAX_RMR_BUFF_ARRY] {};
} RmrMessagesBuffer_t;
typedef struct formatedMessage {
char enodbName[MAX_ENODB_NAME_SIZE];
struct timespec time;
int messageType;
char direction;
ssize_t asnLength;
unsigned char *asndata;
} FormatedMessage_t;
typedef struct ReportingMessages {
FormatedMessage_t message {};
ConnectedCU_t *peerInfo = nullptr;
long outLen = 0;
unsigned char base64Data[RECEIVE_SCTP_BUFFER_SIZE * 2] {};
char buffer[RECEIVE_SCTP_BUFFER_SIZE * 8] {};
} ReportingMessages_t;
cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &pSctpParams);
int buildInotify(sctp_params_t &sctpParams);
void handleTermInit(sctp_params_t &sctpParams);
void handleConfigChange(sctp_params_t *sctpParams);
void listener(sctp_params_t *params);
void sendTermInit(sctp_params_t &sctpParams);
int setSocketNoBlocking(int socket);
void handleEinprogressMessages(struct epoll_event &event,
ReportingMessages_t &message,
RmrMessagesBuffer_t &rmrMessageBuffer,
sctp_params_t *params,
otSpan *pSpan);
void handlepoll_error(struct epoll_event &event,
ReportingMessages_t &message,
RmrMessagesBuffer_t &rmrMessageBuffer,
sctp_params_t *params,
otSpan *pSpan);
void cleanHashEntry(ConnectedCU_t *peerInfo, Sctp_Map_t *m, otSpan *pSpan);
int getSetupRequestMetaData(ReportingMessages_t &message, char *data, char *host, uint16_t &port, otSpan *pSpan);
/**
*
* @param message
* @param rmrMessageBuffer
* @param pSpan
*/
void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer, otSpan *pSpan);
/**
*
* @param sctpMap
* @param messagBuffer
* @param message
* @param failedMesgId
* @param pSpan
* @return
*/
int sendMessagetoCu(Sctp_Map_t *sctpMap,
RmrMessagesBuffer_t &messagBuffer,
ReportingMessages_t &message,
int failedMesgId, otSpan *pSpan);
void sendFailedSendingMessagetoXapp(RmrMessagesBuffer_t &rmrMessageBuffer,
ReportingMessages_t &message,
int failedMesgId,
otSpan *pSpan);
int sendRequestToXapp(ReportingMessages_t &message,
int requestId,
RmrMessagesBuffer_t &rmrMmessageBuffer,
otSpan *pSpan);
/**
*
* @param message
* @param msgType
* @param requestType
* @param rmrMessageBuffer
* @param sctpMap
* @param pSpan
* @return
*/
/*
int sendResponseToXapp(ReportingMessages_t &message,
int msgType,
int requestType,
RmrMessagesBuffer_t &rmrMessageBuffer,
Sctp_Map_t *sctpMap,
otSpan *pSpan);
*/
/**
*
* @param peerInfo
* @param message
* @param m
* @param pSpan
* @return
*/
int sendSctpMsg(ConnectedCU_t *peerInfo,
ReportingMessages_t &message,
Sctp_Map_t *m,
otSpan *pSpan);
/**
*
* @param events
* @param sctpMap
* @param numOfMessages
* @param rmrMessageBuffer
* @param ts
* @param pSpan
* @return
*/
int receiveDataFromSctp(struct epoll_event *events,
Sctp_Map_t *sctpMap,
int &numOfMessages,
RmrMessagesBuffer_t &rmrMessageBuffer,
struct timespec &ts,
otSpan *pSpan);
/**
*
* @param rmrAddress
* @param pSpan
* @return
*/
void getRmrContext(sctp_params_t &pSctpParams, otSpan *pSpan);
/**
*
* @param epoll_fd
* @param rmrCtx
* @param sctpMap
* @param messagBuffer
* @param pSpan
* @return
*/
int receiveXappMessages(int epoll_fd,
Sctp_Map_t *sctpMap,
RmrMessagesBuffer_t &rmrMessageBuffer,
struct timespec &ts,
otSpan *pSpan);
/**
*
* @param rmrMessageBuffer
* @param message
* @param epoll_fd
* @param sctpMap
* @param pSpan
* @return
*/
int connectToCUandSetUp(RmrMessagesBuffer_t &rmrMessageBuffer,
ReportingMessages_t &message,
int epoll_fd,
Sctp_Map_t *sctpMap,
otSpan *pSpan);
/**
*
* @param messagBuffer
* @param failedMsgId
* @param sctpMap
* @param pSpan
* @return
*/
int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messagBuffer,
ReportingMessages_t &message,
int failedMsgId,
Sctp_Map_t *sctpMap,
otSpan *pSpan);
/**
*
* @param pdu
* @param message
* @param rmrMessageBuffer
* @param pSpan
*/
void asnInitiatingRequest(E2AP_PDU_t *pdu,
ReportingMessages_t &message,
RmrMessagesBuffer_t &rmrMessageBuffer,
otSpan *pSpan);
/**
*
* @param pdu
* @param message
* @param sctpMap
* @param rmrMessageBuffer
* @param pSpan
*/
void asnSuccsesfulMsg(E2AP_PDU_t *pdu,
ReportingMessages_t &message,
Sctp_Map_t *sctpMap,
RmrMessagesBuffer_t &rmrMessageBuffer,
otSpan *pSpan);
/**
*
* @param pdu
* @param message
* @param sctpMap
* @param rmrMessageBuffer
* @param pSpan
*/
void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
ReportingMessages_t &message,
Sctp_Map_t *sctpMap,
RmrMessagesBuffer_t &rmrMessageBuffer,
otSpan *pSpan);
/**
*
* @param rmrMessageBuffer
* @param message
* @param pSpan
* @return
*/
int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message, otSpan *pSpan);
/**
*
* @param epoll_fd
* @param peerInfo
* @param events
* @param sctpMap
* @param enodbName
* @param msgType
* @param pSpan
* @returnsrc::logger_mt& lg = my_logger::get();
*/
int addToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType, otSpan *pSpan);
/**
*
* @param epoll_fd
* @param peerInfo
* @param events
* @param sctpMap
* @param enodbName
* @param msgType
* @param pSpan
* @return
*/
int modifyToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType, otSpan *pSpan);
/**
*
* @param message
*/
void buildJsonMessage(ReportingMessages_t &message);
/**
*
*
* @param state
* @return
*/
string translateRmrErrorMessages(int state);
static inline uint64_t rdtscp(uint32_t &aux) {
uint64_t rax,rdx;
asm volatile ("rdtscp\n" : "=a" (rax), "=d" (rdx), "=c" (aux) : :);
return (rdx << (unsigned)32) + rax;
}
#ifndef RIC_SCTP_CONNECTION_FAILURE
#define RIC_SCTP_CONNECTION_FAILURE 10080
#endif
#endif //X2_SCTP_THREAD_H