blob: f6b97527e719435a18d20b934cd9559c4a948f5c [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
87#ifdef __TRACING__
88#include "openTracing.h"
89#endif
90
91#include "mapWrapper.h"
92
93#include "base64.h"
94
ss412g3bac2da2020-01-05 11:52:19 +020095#include "ReadConfigFile.h"
96
ss412g1a79bdf2019-10-24 12:03:05 +030097using namespace std;
98namespace logging = boost::log;
99namespace src = boost::log::sources;
100namespace keywords = boost::log::keywords;
101namespace sinks = boost::log::sinks;
102namespace posix_time = boost::posix_time;
103namespace expr = boost::log::expressions;
104
105#define SRC_PORT 36422
106#define SA struct sockaddr
107#define MAX_ENODB_NAME_SIZE 64
108
109#define MAXEVENTS 128
110
aa7133@att.combe92d712020-03-18 17:30:39 +0200111#define RECEIVE_SCTP_BUFFER_SIZE (8 * 1024)
ss412g1a79bdf2019-10-24 12:03:05 +0300112#define RECEIVE_XAPP_BUFFER_SIZE RECEIVE_SCTP_BUFFER_SIZE
113
114typedef mapWrapper Sctp_Map_t;
115
ss412g1a79bdf2019-10-24 12:03:05 +0300116
117#define VOLUME_URL_SIZE 256
aa7133@att.combe92d712020-03-18 17:30:39 +0200118#define KA_MESSAGE_SIZE 2048
ss412g1a79bdf2019-10-24 12:03:05 +0300119
120typedef struct sctp_params {
121 uint16_t rmrPort = 0;
aa7133@att.combe92d712020-03-18 17:30:39 +0200122 uint16_t sctpPort = SRC_PORT;
ss412g1a79bdf2019-10-24 12:03:05 +0300123 int epoll_fd = 0;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200124 int listenFD = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300125 int rmrListenFd = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200126 int inotifyFD = 0;
127 int inotifyWD = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300128 void *rmrCtx = nullptr;
129 Sctp_Map_t *sctpMap = nullptr;
aa7133@att.combe92d712020-03-18 17:30:39 +0200130 char ka_message[KA_MESSAGE_SIZE] {};
ss412g3bac2da2020-01-05 11:52:19 +0200131 int ka_message_length = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300132 char rmrAddress[256] {}; // "tcp:portnumber" "tcp:5566" listen to all address on port 5566
133 mdclog_severity_t logLevel = MDCLOG_INFO;
134 char volume[VOLUME_URL_SIZE];
ss412g3bac2da2020-01-05 11:52:19 +0200135 string myIP {};
136 string fqdn {};
aa7133@att.com342843e2020-01-26 12:53:34 +0200137 string podName {};
ss412g3bac2da2020-01-05 11:52:19 +0200138 string configFilePath {};
139 string configFileName {};
140 bool trace = true;
ss412g1a79bdf2019-10-24 12:03:05 +0300141 //shared_timed_mutex fence; // moved to mapWrapper
142} sctp_params_t;
143
144typedef struct ConnectedCU {
145 int fileDescriptor = 0;
146 char hostName[NI_MAXHOST] {};
147 char portNumber[NI_MAXSERV] {};
148 char enodbName[MAX_ENODB_NAME_SIZE] {};
149 char asnData[RECEIVE_SCTP_BUFFER_SIZE] {};
aa7133@att.com11b17d42020-03-22 11:35:16 +0200150 int rcvMsgs = 0;
151 int sentMesgs = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300152 size_t asnLength = 0;
153 int mtype = 0;
154 bool isConnected = false;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200155 bool gotSetup = false;
156 sctp_params_t *sctpParams = nullptr;
ss412g1a79bdf2019-10-24 12:03:05 +0300157} ConnectedCU_t ;
158
159#define MAX_RMR_BUFF_ARRY 32
160typedef struct RmrMessagesBuffer {
aa7133@att.combe92d712020-03-18 17:30:39 +0200161 char ka_message[KA_MESSAGE_SIZE] {};
ss412g3bac2da2020-01-05 11:52:19 +0200162 int ka_message_len = 0;
163 void *rmrCtx = nullptr;
164 rmr_mbuf_t *sendMessage= nullptr;
165 rmr_mbuf_t *sendBufferedMessages[MAX_RMR_BUFF_ARRY] {};
166 rmr_mbuf_t *rcvMessage= nullptr;
167 rmr_mbuf_t *rcvBufferedMessages[MAX_RMR_BUFF_ARRY] {};
ss412g1a79bdf2019-10-24 12:03:05 +0300168} RmrMessagesBuffer_t;
169
170typedef struct formatedMessage {
171 char enodbName[MAX_ENODB_NAME_SIZE];
172 struct timespec time;
173 int messageType;
174 char direction;
175 ssize_t asnLength;
176 unsigned char *asndata;
177} FormatedMessage_t;
178
179typedef struct ReportingMessages {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200180 FormatedMessage_t message {};
181 ConnectedCU_t *peerInfo = nullptr;
182 long outLen = 0;
183 unsigned char base64Data[RECEIVE_SCTP_BUFFER_SIZE * 2] {};
184 char buffer[RECEIVE_SCTP_BUFFER_SIZE * 8] {};
ss412g1a79bdf2019-10-24 12:03:05 +0300185} ReportingMessages_t;
186
ss412g3bac2da2020-01-05 11:52:19 +0200187cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &pSctpParams);
188
189int buildInotify(sctp_params_t &sctpParams);
190
191void handleTermInit(sctp_params_t &sctpParams);
192
193void handleConfigChange(sctp_params_t *sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300194
195void listener(sctp_params_t *params);
196
ss412g3bac2da2020-01-05 11:52:19 +0200197void sendTermInit(sctp_params_t &sctpParams);
198
ss412g1a79bdf2019-10-24 12:03:05 +0300199int setSocketNoBlocking(int socket);
200
ss412g3bac2da2020-01-05 11:52:19 +0200201void handleEinprogressMessages(struct epoll_event &event,
202 ReportingMessages_t &message,
203 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200204 sctp_params_t *params);
ss412g3bac2da2020-01-05 11:52:19 +0200205
206void handlepoll_error(struct epoll_event &event,
207 ReportingMessages_t &message,
208 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200209 sctp_params_t *params);
ss412g3bac2da2020-01-05 11:52:19 +0200210
211
aa7133@att.combe92d712020-03-18 17:30:39 +0200212void cleanHashEntry(ConnectedCU_t *peerInfo, Sctp_Map_t *m);
ss412g1a79bdf2019-10-24 12:03:05 +0300213
ss412g1a79bdf2019-10-24 12:03:05 +0300214
215/**
216 *
217 * @param message
218 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +0300219 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200220void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +0300221
222/**
223 *
224 * @param sctpMap
225 * @param messagBuffer
226 * @param message
227 * @param failedMesgId
ss412g1a79bdf2019-10-24 12:03:05 +0300228 * @return
229 */
230int sendMessagetoCu(Sctp_Map_t *sctpMap,
231 RmrMessagesBuffer_t &messagBuffer,
232 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +0200233 int failedMesgId);
ss412g1a79bdf2019-10-24 12:03:05 +0300234
235void sendFailedSendingMessagetoXapp(RmrMessagesBuffer_t &rmrMessageBuffer,
236 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +0200237 int failedMesgId);
ss412g1a79bdf2019-10-24 12:03:05 +0300238
239int sendRequestToXapp(ReportingMessages_t &message,
240 int requestId,
aa7133@att.combe92d712020-03-18 17:30:39 +0200241 RmrMessagesBuffer_t &rmrMmessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +0300242
243/**
244 *
245 * @param message
246 * @param msgType
247 * @param requestType
248 * @param rmrMessageBuffer
249 * @param sctpMap
ss412g1a79bdf2019-10-24 12:03:05 +0300250 * @return
251 */
aa7133@att.com84bd3342020-03-16 18:04:57 +0200252/*
ss412g1a79bdf2019-10-24 12:03:05 +0300253int sendResponseToXapp(ReportingMessages_t &message,
254 int msgType,
255 int requestType,
256 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200257 Sctp_Map_t *sctpMap);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200258*/
ss412g1a79bdf2019-10-24 12:03:05 +0300259
260/**
261 *
262 * @param peerInfo
263 * @param message
264 * @param m
ss412g1a79bdf2019-10-24 12:03:05 +0300265 * @return
266 */
267int sendSctpMsg(ConnectedCU_t *peerInfo,
268 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +0200269 Sctp_Map_t *m);
ss412g1a79bdf2019-10-24 12:03:05 +0300270
271/**
272 *
273 * @param events
274 * @param sctpMap
275 * @param numOfMessages
276 * @param rmrMessageBuffer
277 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +0300278 * @return
279 */
280int receiveDataFromSctp(struct epoll_event *events,
281 Sctp_Map_t *sctpMap,
282 int &numOfMessages,
283 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200284 struct timespec &ts);
ss412g1a79bdf2019-10-24 12:03:05 +0300285
286/**
287 *
288 * @param rmrAddress
ss412g1a79bdf2019-10-24 12:03:05 +0300289 * @return
290 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200291void getRmrContext(sctp_params_t &pSctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300292
293/**
294 *
295 * @param epoll_fd
296 * @param rmrCtx
297 * @param sctpMap
298 * @param messagBuffer
ss412g1a79bdf2019-10-24 12:03:05 +0300299 * @return
300 */
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200301int receiveXappMessages(Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +0300302 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200303 struct timespec &ts);
ss412g1a79bdf2019-10-24 12:03:05 +0300304
305/**
306 *
ss412g1a79bdf2019-10-24 12:03:05 +0300307 * @param messagBuffer
308 * @param failedMsgId
309 * @param sctpMap
ss412g1a79bdf2019-10-24 12:03:05 +0300310 * @return
311 */
312int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messagBuffer,
313 ReportingMessages_t &message,
314 int failedMsgId,
aa7133@att.combe92d712020-03-18 17:30:39 +0200315 Sctp_Map_t *sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +0300316/**
317 *
318 * @param pdu
319 * @param message
320 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +0300321 */
322void asnInitiatingRequest(E2AP_PDU_t *pdu,
323 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +0200324 RmrMessagesBuffer_t &rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +0300325/**
326 *
327 * @param pdu
328 * @param message
329 * @param sctpMap
330 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +0300331 */
332void asnSuccsesfulMsg(E2AP_PDU_t *pdu,
333 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,
343 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +0200344 RmrMessagesBuffer_t &rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +0300345
346/**
347 *
348 * @param rmrMessageBuffer
349 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +0300350 * @return
351 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200352int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message);
ss412g1a79bdf2019-10-24 12:03:05 +0300353/**
354 *
355 * @param epoll_fd
356 * @param peerInfo
357 * @param events
358 * @param sctpMap
359 * @param enodbName
360 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +0300361 * @returnsrc::logger_mt& lg = my_logger::get();
362 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200363int addToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType);
ss412g1a79bdf2019-10-24 12:03:05 +0300364/**
365 *
366 * @param epoll_fd
367 * @param peerInfo
368 * @param events
369 * @param sctpMap
370 * @param enodbName
371 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +0300372 * @return
373 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200374int modifyToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType);
ss412g1a79bdf2019-10-24 12:03:05 +0300375
376/**
377 *
378 * @param message
379 */
380void buildJsonMessage(ReportingMessages_t &message);
381
382/**
383 *
384 *
385 * @param state
386 * @return
387 */
388string translateRmrErrorMessages(int state);
389
ss412g3bac2da2020-01-05 11:52:19 +0200390
391static inline uint64_t rdtscp(uint32_t &aux) {
392 uint64_t rax,rdx;
393 asm volatile ("rdtscp\n" : "=a" (rax), "=d" (rdx), "=c" (aux) : :);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200394 return (rdx << (unsigned)32) + rax;
ss412g3bac2da2020-01-05 11:52:19 +0200395}
ss412g1a79bdf2019-10-24 12:03:05 +0300396#ifndef RIC_SCTP_CONNECTION_FAILURE
397#define RIC_SCTP_CONNECTION_FAILURE 10080
398#endif
399
400#endif //X2_SCTP_THREAD_H