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>
56 #include <rmr/RIC_message_types.h>
57 #include <mdclog/mdclog.h>
61 #include <boost/algorithm/string/predicate.hpp>
62 #include <boost/lexical_cast.hpp>
63 #include <boost/move/utility.hpp>
64 #include <boost/log/sources/logger.hpp>
65 #include <boost/log/sources/record_ostream.hpp>
66 #include <boost/log/sources/global_logger_storage.hpp>
67 #include <boost/log/utility/setup/file.hpp>
68 #include <boost/log/utility/setup/common_attributes.hpp>
69 #include <boost/filesystem.hpp>
71 #include <mdclog/mdclog.h>
73 #include "oranE2/E2AP-PDU.h"
74 #include "oranE2/ProtocolIE-Container.h"
75 #include "oranE2/InitiatingMessage.h"
76 #include "oranE2/SuccessfulOutcome.h"
77 #include "oranE2/UnsuccessfulOutcome.h"
78 #include "oranE2/ProtocolIE-Container.h"
79 #include "oranE2/ProtocolIE-Field.h"
80 #include "oranE2/GlobalE2node-gNB-ID.h"
81 #include "oranE2/GlobalE2node-en-gNB-ID.h"
82 #include "oranE2/GlobalE2node-ng-eNB-ID.h"
83 #include "oranE2/GlobalE2node-eNB-ID.h"
85 #include "cxxopts.hpp"
86 //#include "config-cpp/include/config-cpp/config-cpp.h"
88 #include <prometheus/counter.h>
89 #include <prometheus/exposer.h>
90 #include <prometheus/gateway.h>
91 #include <prometheus/registry.h>
93 using namespace prometheus;
95 #include "mapWrapper.h"
99 #include "ReadConfigFile.h"
102 namespace logging = boost::log;
103 namespace src = boost::log::sources;
104 namespace keywords = boost::log::keywords;
105 namespace sinks = boost::log::sinks;
106 namespace posix_time = boost::posix_time;
107 namespace expr = boost::log::expressions;
109 #define SRC_PORT 36422
110 #define SA struct sockaddr
111 #define MAX_ENODB_NAME_SIZE 64
113 #define MAXEVENTS 128
115 #define RECEIVE_SCTP_BUFFER_SIZE (256 * 1024)
116 #define RECEIVE_XAPP_BUFFER_SIZE RECEIVE_SCTP_BUFFER_SIZE
118 typedef mapWrapper Sctp_Map_t;
122 #define VOLUME_URL_SIZE 256
123 #define KA_MESSAGE_SIZE 2048
125 typedef struct sctp_params {
126 int epollTimeOut = -1;
127 uint16_t rmrPort = 0;
128 uint16_t sctpPort = SRC_PORT;
134 void *rmrCtx = nullptr;
135 Sctp_Map_t *sctpMap = nullptr;
136 char ka_message[KA_MESSAGE_SIZE] {};
137 int ka_message_length = 0;
138 char rmrAddress[256] {}; // "tcp:port number" "tcp:5566" listen to all address on port 5566
139 mdclog_severity_t logLevel = MDCLOG_INFO;
140 char volume[VOLUME_URL_SIZE];
144 string configFilePath {};
145 string configFileName {};
147 shared_ptr<prometheus::Registry> prometheusRegistry;
148 string prometheusPort {"8088"};
149 Family<Counter> *prometheusFamily;
150 Exposer *prometheusExposer = nullptr;
154 #define IN_INITI 0 //INITIATING
155 #define IN_SUCC 1 //SUCCESSFUL
156 #define IN_UN_SUCC 2 //UN-Successful
159 #define OUT_INITI 3 //INITIATING
160 #define OUT_SUCC 4 //SUCCESSFUL
161 #define OUT_UN_SUCC 5 //UN-Successful
163 #define MSG_COUNTER 0
164 #define BYTES_COUNTER 1
166 typedef struct ConnectedCU {
167 int fileDescriptor = 0;
168 char hostName[NI_MAXHOST] {};
169 char portNumber[NI_MAXSERV] {};
170 char enodbName[MAX_ENODB_NAME_SIZE] {};
171 char asnData[RECEIVE_SCTP_BUFFER_SIZE] {};
172 size_t asnLength = 0;
174 bool isConnected = false;
175 bool gotSetup = false;
176 sctp_params_t *sctpParams = nullptr;
177 Counter *counters[6][2][ProcedureCode_id_RICsubscriptionDelete + 1] {};
181 #define MAX_RMR_BUFF_ARRAY 32
182 typedef struct RmrMessagesBuffer {
183 char ka_message[KA_MESSAGE_SIZE] {};
184 int ka_message_len = 0;
185 void *rmrCtx = nullptr;
186 rmr_mbuf_t *sendMessage= nullptr;
187 //rmr_mbuf_t *sendBufferedMessages[MAX_RMR_BUFF_ARRAY] {};
188 rmr_mbuf_t *rcvMessage= nullptr;
189 //rmr_mbuf_t *rcvBufferedMessages[MAX_RMR_BUFF_ARRAY] {};
190 } RmrMessagesBuffer_t;
192 typedef struct formatedMessage {
193 char enodbName[MAX_ENODB_NAME_SIZE];
194 struct timespec time;
198 unsigned char *asndata;
201 typedef struct ReportingMessages {
202 FormatedMessage_t message {};
203 ConnectedCU_t *peerInfo = nullptr;
205 unsigned char base64Data[RECEIVE_SCTP_BUFFER_SIZE * 2] {};
206 char buffer[RECEIVE_SCTP_BUFFER_SIZE * 8] {};
207 } ReportingMessages_t;
209 cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &pSctpParams);
211 int buildInotify(sctp_params_t &sctpParams);
213 void handleTermInit(sctp_params_t &sctpParams);
215 void handleConfigChange(sctp_params_t *sctpParams);
217 void listener(sctp_params_t *params);
219 void sendTermInit(sctp_params_t &sctpParams);
221 int setSocketNoBlocking(int socket);
223 void handleEinprogressMessages(struct epoll_event &event,
224 ReportingMessages_t &message,
225 RmrMessagesBuffer_t &rmrMessageBuffer,
226 sctp_params_t *params);
228 void handlepoll_error(struct epoll_event &event,
229 ReportingMessages_t &message,
230 RmrMessagesBuffer_t &rmrMessageBuffer,
231 sctp_params_t *params);
234 void cleanHashEntry(ConnectedCU_t *peerInfo, Sctp_Map_t *m);
240 * @param rmrMessageBuffer
242 void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer);
247 * @param messageBuffer
249 * @param failedMesgId
252 int sendMessagetoCu(Sctp_Map_t *sctpMap,
253 RmrMessagesBuffer_t &messageBuffer,
254 ReportingMessages_t &message,
257 void sendFailedSendingMessagetoXapp(RmrMessagesBuffer_t &rmrMessageBuffer,
258 ReportingMessages_t &message,
261 int sendRequestToXapp(ReportingMessages_t &message,
263 RmrMessagesBuffer_t &rmrMmessageBuffer);
270 * @param rmrMessageBuffer
275 int sendResponseToXapp(ReportingMessages_t &message,
278 RmrMessagesBuffer_t &rmrMessageBuffer,
279 Sctp_Map_t *sctpMap);
289 int sendSctpMsg(ConnectedCU_t *peerInfo,
290 ReportingMessages_t &message,
297 * @param numOfMessages
298 * @param rmrMessageBuffer
302 int receiveDataFromSctp(struct epoll_event *events,
305 RmrMessagesBuffer_t &rmrMessageBuffer,
306 struct timespec &ts);
313 void getRmrContext(sctp_params_t &pSctpParams);
318 * @param rmrMessageBuffer
322 int receiveXappMessages(Sctp_Map_t *sctpMap,
323 RmrMessagesBuffer_t &rmrMessageBuffer,
324 struct timespec &ts);
328 * @param messageBuffer
333 int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messageBuffer,
334 ReportingMessages_t &message,
336 Sctp_Map_t *sctpMap);
341 * @param rmrMessageBuffer
343 void asnInitiatingRequest(E2AP_PDU_t *pdu,
345 ReportingMessages_t &message,
346 RmrMessagesBuffer_t &rmrMessageBuffer);
352 * @param rmrMessageBuffer
354 void asnSuccessfulMsg(E2AP_PDU_t *pdu,
356 ReportingMessages_t &message,
357 RmrMessagesBuffer_t &rmrMessageBuffer);
363 * @param rmrMessageBuffer
365 void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
367 ReportingMessages_t &message,
368 RmrMessagesBuffer_t &rmrMessageBuffer);
372 * @param rmrMessageBuffer
376 int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message);
385 * @returnsrc::logger_mt& lg = my_logger::get();
387 int addToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType);
398 int modifyToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType);
404 void buildJsonMessage(ReportingMessages_t &message);
412 string translateRmrErrorMessages(int state);
414 int buildConfiguration(sctp_params_t &sctpParams);
415 void startPrometheus(sctp_params_t &sctpParams);
417 static inline uint64_t rdtscp(uint32_t &aux) {
419 asm volatile ("rdtscp\n" : "=a" (rax), "=d" (rdx), "=c" (aux) : :);
420 return (rdx << (unsigned)32) + rax;
422 #ifndef RIC_SCTP_CONNECTION_FAILURE
423 #define RIC_SCTP_CONNECTION_FAILURE 10080
427 int e2_test_main(const int argc, char **argv, sctp_params_t &sctpParams);
428 int buildListeningPort(sctp_params_t &sctpParams);
431 #endif //X2_SCTP_THREAD_H