blob: 10c6d90c4a0970ff5dd4dbdfe11696ef0f26a74e [file] [log] [blame]
ss412g1a79bdf2019-10-24 12:03:05 +03001/*
2 * Copyright 2019 AT&T Intellectual Property
3 * Copyright 2019 Nokia
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.
ss412g3bac2da2020-01-05 11:52:19 +020016*/
nm755n2e268142019-11-28 16:40:23 +000017
18/*
19 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 * platform project (RICP).
21 */
22
ss412g1a79bdf2019-10-24 12:03:05 +030023#ifndef X2_SCTP_THREAD_H
24#define X2_SCTP_THREAD_H
25
26#include <algorithm>
27
28#include <cstdio>
29#include <cerrno>
30#include <cstdlib>
31#include <cstring>
32#include <random>
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>
40#include <thread>
41#include <atomic>
42#include <sys/param.h>
43#include <sys/file.h>
44#include <ctime>
45#include <netdb.h>
46#include <sys/epoll.h>
47#include <mutex>
48#include <shared_mutex>
49#include <iterator>
50#include <map>
ss412g3bac2da2020-01-05 11:52:19 +020051#include <sys/inotify.h>
52#include <csignal>
ss412g1a79bdf2019-10-24 12:03:05 +030053
54#include <rmr/rmr.h>
55#include <rmr/RIC_message_types.h>
56#include <mdclog/mdclog.h>
57#include <functional>
58#include <iostream>
59
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>
ss412g3bac2da2020-01-05 11:52:19 +020068#include <boost/filesystem.hpp>
ss412g1a79bdf2019-10-24 12:03:05 +030069
70#include <mdclog/mdclog.h>
71
aa7133@att.com84bd3342020-03-16 18:04:57 +020072#include "oranE2/E2AP-PDU.h"
73#include "oranE2/ProtocolIE-Container.h"
74#include "oranE2/InitiatingMessage.h"
75#include "oranE2/SuccessfulOutcome.h"
76#include "oranE2/UnsuccessfulOutcome.h"
77#include "oranE2/ProtocolIE-Container.h"
78#include "oranE2/ProtocolIE-Field.h"
79#include "oranE2/GlobalE2node-gNB-ID.h"
80#include "oranE2/GlobalE2node-en-gNB-ID.h"
81#include "oranE2/GlobalE2node-ng-eNB-ID.h"
82#include "oranE2/GlobalE2node-eNB-ID.h"
ss412g3bac2da2020-01-05 11:52:19 +020083
84#include "cxxopts.hpp"
85//#include "config-cpp/include/config-cpp/config-cpp.h"
ss412g1a79bdf2019-10-24 12:03:05 +030086
ss412g1a79bdf2019-10-24 12:03:05 +030087
88#include "mapWrapper.h"
aa7133@att.com16c799b2020-03-24 18:01:51 +020089#include "statCollector.h"
ss412g1a79bdf2019-10-24 12:03:05 +030090
91#include "base64.h"
92
ss412g3bac2da2020-01-05 11:52:19 +020093#include "ReadConfigFile.h"
94
ss412g1a79bdf2019-10-24 12:03:05 +030095using namespace std;
96namespace logging = boost::log;
97namespace src = boost::log::sources;
98namespace keywords = boost::log::keywords;
99namespace sinks = boost::log::sinks;
100namespace posix_time = boost::posix_time;
101namespace expr = boost::log::expressions;
102
103#define SRC_PORT 36422
104#define SA struct sockaddr
105#define MAX_ENODB_NAME_SIZE 64
106
107#define MAXEVENTS 128
108
aa7133@att.com7c3e4c02020-03-25 16:37:19 +0200109#define RECEIVE_SCTP_BUFFER_SIZE (128 * 1024)
ss412g1a79bdf2019-10-24 12:03:05 +0300110#define RECEIVE_XAPP_BUFFER_SIZE RECEIVE_SCTP_BUFFER_SIZE
111
112typedef mapWrapper Sctp_Map_t;
113
ss412g1a79bdf2019-10-24 12:03:05 +0300114
aa7133@att.com16c799b2020-03-24 18:01:51 +0200115
ss412g1a79bdf2019-10-24 12:03:05 +0300116#define VOLUME_URL_SIZE 256
aa7133@att.combe92d712020-03-18 17:30:39 +0200117#define KA_MESSAGE_SIZE 2048
ss412g1a79bdf2019-10-24 12:03:05 +0300118
119typedef struct sctp_params {
120 uint16_t rmrPort = 0;
aa7133@att.combe92d712020-03-18 17:30:39 +0200121 uint16_t sctpPort = SRC_PORT;
ss412g1a79bdf2019-10-24 12:03:05 +0300122 int epoll_fd = 0;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200123 int listenFD = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300124 int rmrListenFd = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200125 int inotifyFD = 0;
126 int inotifyWD = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300127 void *rmrCtx = nullptr;
128 Sctp_Map_t *sctpMap = nullptr;
aa7133@att.combe92d712020-03-18 17:30:39 +0200129 char ka_message[KA_MESSAGE_SIZE] {};
ss412g3bac2da2020-01-05 11:52:19 +0200130 int ka_message_length = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300131 char rmrAddress[256] {}; // "tcp:portnumber" "tcp:5566" listen to all address on port 5566
132 mdclog_severity_t logLevel = MDCLOG_INFO;
133 char volume[VOLUME_URL_SIZE];
ss412g3bac2da2020-01-05 11:52:19 +0200134 string myIP {};
135 string fqdn {};
aa7133@att.com342843e2020-01-26 12:53:34 +0200136 string podName {};
ss412g3bac2da2020-01-05 11:52:19 +0200137 string configFilePath {};
138 string configFileName {};
139 bool trace = true;
ss412g1a79bdf2019-10-24 12:03:05 +0300140 //shared_timed_mutex fence; // moved to mapWrapper
141} sctp_params_t;
142
143typedef struct ConnectedCU {
144 int fileDescriptor = 0;
145 char hostName[NI_MAXHOST] {};
146 char portNumber[NI_MAXSERV] {};
147 char enodbName[MAX_ENODB_NAME_SIZE] {};
148 char asnData[RECEIVE_SCTP_BUFFER_SIZE] {};
149 size_t asnLength = 0;
150 int mtype = 0;
151 bool isConnected = false;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200152 bool gotSetup = false;
153 sctp_params_t *sctpParams = nullptr;
ss412g1a79bdf2019-10-24 12:03:05 +0300154} ConnectedCU_t ;
155
156#define MAX_RMR_BUFF_ARRY 32
157typedef struct RmrMessagesBuffer {
aa7133@att.combe92d712020-03-18 17:30:39 +0200158 char ka_message[KA_MESSAGE_SIZE] {};
ss412g3bac2da2020-01-05 11:52:19 +0200159 int ka_message_len = 0;
160 void *rmrCtx = nullptr;
161 rmr_mbuf_t *sendMessage= nullptr;
aa7133@att.com7c3e4c02020-03-25 16:37:19 +0200162 //rmr_mbuf_t *sendBufferedMessages[MAX_RMR_BUFF_ARRY] {};
ss412g3bac2da2020-01-05 11:52:19 +0200163 rmr_mbuf_t *rcvMessage= nullptr;
aa7133@att.com7c3e4c02020-03-25 16:37:19 +0200164 //rmr_mbuf_t *rcvBufferedMessages[MAX_RMR_BUFF_ARRY] {};
ss412g1a79bdf2019-10-24 12:03:05 +0300165} RmrMessagesBuffer_t;
166
167typedef struct formatedMessage {
168 char enodbName[MAX_ENODB_NAME_SIZE];
169 struct timespec time;
170 int messageType;
171 char direction;
172 ssize_t asnLength;
173 unsigned char *asndata;
174} FormatedMessage_t;
175
176typedef struct ReportingMessages {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200177 FormatedMessage_t message {};
178 ConnectedCU_t *peerInfo = nullptr;
179 long outLen = 0;
180 unsigned char base64Data[RECEIVE_SCTP_BUFFER_SIZE * 2] {};
181 char buffer[RECEIVE_SCTP_BUFFER_SIZE * 8] {};
aa7133@att.com16c799b2020-03-24 18:01:51 +0200182 StatCollector *statCollector = nullptr;
ss412g1a79bdf2019-10-24 12:03:05 +0300183} ReportingMessages_t;
184
ss412g3bac2da2020-01-05 11:52:19 +0200185cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &pSctpParams);
186
187int buildInotify(sctp_params_t &sctpParams);
188
189void handleTermInit(sctp_params_t &sctpParams);
190
191void handleConfigChange(sctp_params_t *sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300192
193void listener(sctp_params_t *params);
194
ss412g3bac2da2020-01-05 11:52:19 +0200195void sendTermInit(sctp_params_t &sctpParams);
196
ss412g1a79bdf2019-10-24 12:03:05 +0300197int setSocketNoBlocking(int socket);
198
ss412g3bac2da2020-01-05 11:52:19 +0200199void handleEinprogressMessages(struct epoll_event &event,
200 ReportingMessages_t &message,
201 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200202 sctp_params_t *params);
ss412g3bac2da2020-01-05 11:52:19 +0200203
204void handlepoll_error(struct epoll_event &event,
205 ReportingMessages_t &message,
206 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200207 sctp_params_t *params);
ss412g3bac2da2020-01-05 11:52:19 +0200208
209
aa7133@att.combe92d712020-03-18 17:30:39 +0200210void cleanHashEntry(ConnectedCU_t *peerInfo, Sctp_Map_t *m);
ss412g1a79bdf2019-10-24 12:03:05 +0300211
ss412g1a79bdf2019-10-24 12:03:05 +0300212
213/**
214 *
215 * @param message
216 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +0300217 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200218void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +0300219
220/**
221 *
222 * @param sctpMap
223 * @param messagBuffer
224 * @param message
225 * @param failedMesgId
ss412g1a79bdf2019-10-24 12:03:05 +0300226 * @return
227 */
228int sendMessagetoCu(Sctp_Map_t *sctpMap,
229 RmrMessagesBuffer_t &messagBuffer,
230 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +0200231 int failedMesgId);
ss412g1a79bdf2019-10-24 12:03:05 +0300232
233void sendFailedSendingMessagetoXapp(RmrMessagesBuffer_t &rmrMessageBuffer,
234 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +0200235 int failedMesgId);
ss412g1a79bdf2019-10-24 12:03:05 +0300236
237int sendRequestToXapp(ReportingMessages_t &message,
238 int requestId,
aa7133@att.combe92d712020-03-18 17:30:39 +0200239 RmrMessagesBuffer_t &rmrMmessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +0300240
241/**
242 *
243 * @param message
244 * @param msgType
245 * @param requestType
246 * @param rmrMessageBuffer
247 * @param sctpMap
ss412g1a79bdf2019-10-24 12:03:05 +0300248 * @return
249 */
aa7133@att.com84bd3342020-03-16 18:04:57 +0200250/*
ss412g1a79bdf2019-10-24 12:03:05 +0300251int sendResponseToXapp(ReportingMessages_t &message,
252 int msgType,
253 int requestType,
254 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200255 Sctp_Map_t *sctpMap);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200256*/
ss412g1a79bdf2019-10-24 12:03:05 +0300257
258/**
259 *
260 * @param peerInfo
261 * @param message
262 * @param m
ss412g1a79bdf2019-10-24 12:03:05 +0300263 * @return
264 */
265int sendSctpMsg(ConnectedCU_t *peerInfo,
266 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +0200267 Sctp_Map_t *m);
ss412g1a79bdf2019-10-24 12:03:05 +0300268
269/**
270 *
271 * @param events
272 * @param sctpMap
273 * @param numOfMessages
274 * @param rmrMessageBuffer
275 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +0300276 * @return
277 */
278int receiveDataFromSctp(struct epoll_event *events,
279 Sctp_Map_t *sctpMap,
280 int &numOfMessages,
281 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200282 struct timespec &ts);
ss412g1a79bdf2019-10-24 12:03:05 +0300283
284/**
285 *
286 * @param rmrAddress
ss412g1a79bdf2019-10-24 12:03:05 +0300287 * @return
288 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200289void getRmrContext(sctp_params_t &pSctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300290
291/**
292 *
293 * @param epoll_fd
294 * @param rmrCtx
295 * @param sctpMap
296 * @param messagBuffer
ss412g1a79bdf2019-10-24 12:03:05 +0300297 * @return
298 */
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200299int receiveXappMessages(Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +0300300 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200301 struct timespec &ts);
ss412g1a79bdf2019-10-24 12:03:05 +0300302
303/**
304 *
ss412g1a79bdf2019-10-24 12:03:05 +0300305 * @param messagBuffer
306 * @param failedMsgId
307 * @param sctpMap
ss412g1a79bdf2019-10-24 12:03:05 +0300308 * @return
309 */
310int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messagBuffer,
311 ReportingMessages_t &message,
312 int failedMsgId,
aa7133@att.combe92d712020-03-18 17:30:39 +0200313 Sctp_Map_t *sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +0300314/**
315 *
316 * @param pdu
317 * @param message
318 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +0300319 */
320void asnInitiatingRequest(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +0300321 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +0300322 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +0200323 RmrMessagesBuffer_t &rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +0300324/**
325 *
326 * @param pdu
327 * @param message
328 * @param sctpMap
329 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +0300330 */
331void asnSuccsesfulMsg(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +0300332 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +0300333 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +0200334 RmrMessagesBuffer_t &rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +0300335/**
336 *
337 * @param pdu
338 * @param message
339 * @param sctpMap
340 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +0300341 */
342void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +0300343 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +0300344 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +0200345 RmrMessagesBuffer_t &rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +0300346
347/**
348 *
349 * @param rmrMessageBuffer
350 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +0300351 * @return
352 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200353int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message);
ss412g1a79bdf2019-10-24 12:03:05 +0300354/**
355 *
356 * @param epoll_fd
357 * @param peerInfo
358 * @param events
359 * @param sctpMap
360 * @param enodbName
361 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +0300362 * @returnsrc::logger_mt& lg = my_logger::get();
363 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200364int addToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType);
ss412g1a79bdf2019-10-24 12:03:05 +0300365/**
366 *
367 * @param epoll_fd
368 * @param peerInfo
369 * @param events
370 * @param sctpMap
371 * @param enodbName
372 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +0300373 * @return
374 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200375int modifyToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType);
ss412g1a79bdf2019-10-24 12:03:05 +0300376
377/**
378 *
379 * @param message
380 */
381void buildJsonMessage(ReportingMessages_t &message);
382
383/**
384 *
385 *
386 * @param state
387 * @return
388 */
389string translateRmrErrorMessages(int state);
390
ss412g3bac2da2020-01-05 11:52:19 +0200391
392static inline uint64_t rdtscp(uint32_t &aux) {
393 uint64_t rax,rdx;
394 asm volatile ("rdtscp\n" : "=a" (rax), "=d" (rdx), "=c" (aux) : :);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200395 return (rdx << (unsigned)32) + rax;
ss412g3bac2da2020-01-05 11:52:19 +0200396}
ss412g1a79bdf2019-10-24 12:03:05 +0300397#ifndef RIC_SCTP_CONNECTION_FAILURE
398#define RIC_SCTP_CONNECTION_FAILURE 10080
399#endif
400
401#endif //X2_SCTP_THREAD_H