2 * Copyright 2019 AT&T Intellectual Property
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 * platform project (RICP).
23 #ifndef X2_SCTP_THREAD_H
24 #define X2_SCTP_THREAD_H
33 #include <sys/socket.h>
34 #include <arpa/inet.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/in.h>
37 #include <netinet/ip.h>
38 #include <netinet/ip_icmp.h>
39 #include <netinet/sctp.h>
42 #include <sys/param.h>
46 #include <sys/epoll.h>
48 #include <shared_mutex>
51 #include <sys/inotify.h>
55 #include <rmr/RIC_message_types.h>
56 #include <mdclog/mdclog.h>
60 #include <boost/algorithm/string/predicate.hpp>
61 #include <boost/lexical_cast.hpp>
62 #include <boost/move/utility.hpp>
63 #include <boost/log/sources/logger.hpp>
64 #include <boost/log/sources/record_ostream.hpp>
65 #include <boost/log/sources/global_logger_storage.hpp>
66 #include <boost/log/utility/setup/file.hpp>
67 #include <boost/log/utility/setup/common_attributes.hpp>
68 #include <boost/filesystem.hpp>
70 #include <mdclog/mdclog.h>
72 #include "asn1cFiles/E2AP-PDU.h"
73 #include "asn1cFiles/ProtocolIE-Container.h"
74 #include "asn1cFiles/InitiatingMessage.h"
75 #include "asn1cFiles/SuccessfulOutcome.h"
76 #include "asn1cFiles/UnsuccessfulOutcome.h"
77 #include "asn1cFiles/ProtocolIE-Container.h"
78 #include "asn1cFiles/ProtocolIE-Field.h"
80 #include "cxxopts.hpp"
81 //#include "config-cpp/include/config-cpp/config-cpp.h"
84 #include "openTracing.h"
87 #include "mapWrapper.h"
91 #include "ReadConfigFile.h"
94 namespace logging = boost::log;
95 namespace src = boost::log::sources;
96 namespace keywords = boost::log::keywords;
97 namespace sinks = boost::log::sinks;
98 namespace posix_time = boost::posix_time;
99 namespace expr = boost::log::expressions;
101 #define SRC_PORT 36422
102 #define SA struct sockaddr
103 #define MAX_ENODB_NAME_SIZE 64
105 #define MAXEVENTS 128
107 #define RECEIVE_SCTP_BUFFER_SIZE (64*1024)
108 #define RECEIVE_XAPP_BUFFER_SIZE RECEIVE_SCTP_BUFFER_SIZE
110 typedef mapWrapper Sctp_Map_t;
113 typedef const std::unique_ptr<opentracing::Span> otSpan;
115 typedef const int otSpan;
118 #define VOLUME_URL_SIZE 256
120 typedef struct sctp_params {
121 uint16_t rmrPort = 0;
126 void *rmrCtx = nullptr;
127 Sctp_Map_t *sctpMap = nullptr;
128 char ka_message[4096] {};
129 int ka_message_length = 0;
130 char rmrAddress[256] {}; // "tcp:portnumber" "tcp:5566" listen to all address on port 5566
131 mdclog_severity_t logLevel = MDCLOG_INFO;
132 char volume[VOLUME_URL_SIZE];
135 string configFilePath {};
136 string configFileName {};
138 //shared_timed_mutex fence; // moved to mapWrapper
141 typedef struct ConnectedCU {
142 int fileDescriptor = 0;
143 char hostName[NI_MAXHOST] {};
144 char portNumber[NI_MAXSERV] {};
145 char enodbName[MAX_ENODB_NAME_SIZE] {};
146 char asnData[RECEIVE_SCTP_BUFFER_SIZE] {};
147 size_t asnLength = 0;
149 bool isConnected = false;
152 #define MAX_RMR_BUFF_ARRY 32
153 typedef struct RmrMessagesBuffer {
154 char ka_message[4096] {};
155 int ka_message_len = 0;
156 void *rmrCtx = nullptr;
157 rmr_mbuf_t *sendMessage= nullptr;
158 rmr_mbuf_t *sendBufferedMessages[MAX_RMR_BUFF_ARRY] {};
159 rmr_mbuf_t *rcvMessage= nullptr;
160 rmr_mbuf_t *rcvBufferedMessages[MAX_RMR_BUFF_ARRY] {};
161 } RmrMessagesBuffer_t;
163 typedef struct formatedMessage {
164 char enodbName[MAX_ENODB_NAME_SIZE];
165 struct timespec time;
169 unsigned char *asndata;
172 typedef struct ReportingMessages {
173 FormatedMessage_t message;
175 unsigned char base64Data[RECEIVE_SCTP_BUFFER_SIZE * 2];
176 char buffer[RECEIVE_SCTP_BUFFER_SIZE * 8];
177 } ReportingMessages_t;
179 cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &pSctpParams);
181 int buildInotify(sctp_params_t &sctpParams);
183 void handleTermInit(sctp_params_t &sctpParams);
185 void handleConfigChange(sctp_params_t *sctpParams);
187 void listener(sctp_params_t *params);
189 void sendTermInit(sctp_params_t &sctpParams);
191 int setSocketNoBlocking(int socket);
193 void handleEinprogressMessages(struct epoll_event &event,
194 ReportingMessages_t &message,
195 RmrMessagesBuffer_t &rmrMessageBuffer,
196 sctp_params_t *params,
199 void handlepoll_error(struct epoll_event &event,
200 ReportingMessages_t &message,
201 RmrMessagesBuffer_t &rmrMessageBuffer,
202 sctp_params_t *params,
206 void cleanHashEntry(ConnectedCU_t *peerInfo, Sctp_Map_t *m, otSpan *pSpan);
208 int getSetupRequestMetaData(ReportingMessages_t &message, char *data, char *host, uint16_t &port, otSpan *pSpan);
213 * @param rmrMessageBuffer
216 void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer, otSpan *pSpan);
221 * @param messagBuffer
223 * @param failedMesgId
227 int sendMessagetoCu(Sctp_Map_t *sctpMap,
228 RmrMessagesBuffer_t &messagBuffer,
229 ReportingMessages_t &message,
230 int failedMesgId, otSpan *pSpan);
232 void sendFailedSendingMessagetoXapp(RmrMessagesBuffer_t &rmrMessageBuffer,
233 ReportingMessages_t &message,
237 int sendRequestToXapp(ReportingMessages_t &message,
239 RmrMessagesBuffer_t &rmrMmessageBuffer,
247 * @param rmrMessageBuffer
252 int sendResponseToXapp(ReportingMessages_t &message,
255 RmrMessagesBuffer_t &rmrMessageBuffer,
267 int sendSctpMsg(ConnectedCU_t *peerInfo,
268 ReportingMessages_t &message,
276 * @param numOfMessages
277 * @param rmrMessageBuffer
282 int receiveDataFromSctp(struct epoll_event *events,
285 RmrMessagesBuffer_t &rmrMessageBuffer,
295 void getRmrContext(sctp_params_t &pSctpParams, otSpan *pSpan);
302 * @param messagBuffer
306 int receiveXappMessages(int epoll_fd,
308 RmrMessagesBuffer_t &rmrMessageBuffer,
314 * @param rmrMessageBuffer
321 int connectToCUandSetUp(RmrMessagesBuffer_t &rmrMessageBuffer,
322 ReportingMessages_t &message,
329 * @param messagBuffer
335 int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messagBuffer,
336 ReportingMessages_t &message,
344 * @param rmrMessageBuffer
347 void asnInitiatingRequest(E2AP_PDU_t *pdu,
348 ReportingMessages_t &message,
349 RmrMessagesBuffer_t &rmrMessageBuffer,
356 * @param rmrMessageBuffer
359 void asnSuccsesfulMsg(E2AP_PDU_t *pdu,
360 ReportingMessages_t &message,
362 RmrMessagesBuffer_t &rmrMessageBuffer,
369 * @param rmrMessageBuffer
372 void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
373 ReportingMessages_t &message,
375 RmrMessagesBuffer_t &rmrMessageBuffer,
380 * @param rmrMessageBuffer
385 int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message, otSpan *pSpan);
396 * @returnsrc::logger_mt& lg = my_logger::get();
398 int addToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType, otSpan *pSpan);
410 int modifyToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType, otSpan *pSpan);
416 void buildJsonMessage(ReportingMessages_t &message);
424 string translateRmrErrorMessages(int state);
427 static inline uint64_t rdtscp(uint32_t &aux) {
429 asm volatile ("rdtscp\n" : "=a" (rax), "=d" (rdx), "=c" (aux) : :);
430 return (rdx << 32) + rax;
432 #ifndef RIC_SCTP_CONNECTION_FAILURE
433 #define RIC_SCTP_CONNECTION_FAILURE 10080
436 #endif //X2_SCTP_THREAD_H