blob: 9dfbf9d89189167d8d97f42381f2a8f2c676cd2d [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.com84bd3342020-03-16 18:04:57 +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
116#ifdef __TRACING__
117typedef const std::unique_ptr<opentracing::Span> otSpan;
118#else
119typedef const int otSpan;
120#endif
121
122#define VOLUME_URL_SIZE 256
123
124typedef struct sctp_params {
125 uint16_t rmrPort = 0;
126 int epoll_fd = 0;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200127 int listenFD = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300128 int rmrListenFd = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200129 int inotifyFD = 0;
130 int inotifyWD = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300131 void *rmrCtx = nullptr;
132 Sctp_Map_t *sctpMap = nullptr;
ss412g3bac2da2020-01-05 11:52:19 +0200133 char ka_message[4096] {};
134 int ka_message_length = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300135 char rmrAddress[256] {}; // "tcp:portnumber" "tcp:5566" listen to all address on port 5566
136 mdclog_severity_t logLevel = MDCLOG_INFO;
137 char volume[VOLUME_URL_SIZE];
ss412g3bac2da2020-01-05 11:52:19 +0200138 string myIP {};
139 string fqdn {};
aa7133@att.com342843e2020-01-26 12:53:34 +0200140 string podName {};
ss412g3bac2da2020-01-05 11:52:19 +0200141 string configFilePath {};
142 string configFileName {};
143 bool trace = true;
ss412g1a79bdf2019-10-24 12:03:05 +0300144 //shared_timed_mutex fence; // moved to mapWrapper
145} sctp_params_t;
146
147typedef struct ConnectedCU {
148 int fileDescriptor = 0;
149 char hostName[NI_MAXHOST] {};
150 char portNumber[NI_MAXSERV] {};
151 char enodbName[MAX_ENODB_NAME_SIZE] {};
152 char asnData[RECEIVE_SCTP_BUFFER_SIZE] {};
153 size_t asnLength = 0;
154 int mtype = 0;
155 bool isConnected = false;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200156 bool gotSetup = false;
157 sctp_params_t *sctpParams = nullptr;
ss412g1a79bdf2019-10-24 12:03:05 +0300158} ConnectedCU_t ;
159
160#define MAX_RMR_BUFF_ARRY 32
161typedef struct RmrMessagesBuffer {
ss412g3bac2da2020-01-05 11:52:19 +0200162 char ka_message[4096] {};
163 int ka_message_len = 0;
164 void *rmrCtx = nullptr;
165 rmr_mbuf_t *sendMessage= nullptr;
166 rmr_mbuf_t *sendBufferedMessages[MAX_RMR_BUFF_ARRY] {};
167 rmr_mbuf_t *rcvMessage= nullptr;
168 rmr_mbuf_t *rcvBufferedMessages[MAX_RMR_BUFF_ARRY] {};
ss412g1a79bdf2019-10-24 12:03:05 +0300169} RmrMessagesBuffer_t;
170
171typedef struct formatedMessage {
172 char enodbName[MAX_ENODB_NAME_SIZE];
173 struct timespec time;
174 int messageType;
175 char direction;
176 ssize_t asnLength;
177 unsigned char *asndata;
178} FormatedMessage_t;
179
180typedef struct ReportingMessages {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200181 FormatedMessage_t message {};
182 ConnectedCU_t *peerInfo = nullptr;
183 long outLen = 0;
184 unsigned char base64Data[RECEIVE_SCTP_BUFFER_SIZE * 2] {};
185 char buffer[RECEIVE_SCTP_BUFFER_SIZE * 8] {};
ss412g1a79bdf2019-10-24 12:03:05 +0300186} ReportingMessages_t;
187
ss412g3bac2da2020-01-05 11:52:19 +0200188cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &pSctpParams);
189
190int buildInotify(sctp_params_t &sctpParams);
191
192void handleTermInit(sctp_params_t &sctpParams);
193
194void handleConfigChange(sctp_params_t *sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300195
196void listener(sctp_params_t *params);
197
ss412g3bac2da2020-01-05 11:52:19 +0200198void sendTermInit(sctp_params_t &sctpParams);
199
ss412g1a79bdf2019-10-24 12:03:05 +0300200int setSocketNoBlocking(int socket);
201
ss412g3bac2da2020-01-05 11:52:19 +0200202void handleEinprogressMessages(struct epoll_event &event,
203 ReportingMessages_t &message,
204 RmrMessagesBuffer_t &rmrMessageBuffer,
205 sctp_params_t *params,
206 otSpan *pSpan);
207
208void handlepoll_error(struct epoll_event &event,
209 ReportingMessages_t &message,
210 RmrMessagesBuffer_t &rmrMessageBuffer,
211 sctp_params_t *params,
212 otSpan *pSpan);
213
214
ss412g1a79bdf2019-10-24 12:03:05 +0300215void cleanHashEntry(ConnectedCU_t *peerInfo, Sctp_Map_t *m, otSpan *pSpan);
216
217int getSetupRequestMetaData(ReportingMessages_t &message, char *data, char *host, uint16_t &port, otSpan *pSpan);
218
219/**
220 *
221 * @param message
222 * @param rmrMessageBuffer
223 * @param pSpan
224 */
225void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer, otSpan *pSpan);
226
227/**
228 *
229 * @param sctpMap
230 * @param messagBuffer
231 * @param message
232 * @param failedMesgId
233 * @param pSpan
234 * @return
235 */
236int sendMessagetoCu(Sctp_Map_t *sctpMap,
237 RmrMessagesBuffer_t &messagBuffer,
238 ReportingMessages_t &message,
239 int failedMesgId, otSpan *pSpan);
240
241void sendFailedSendingMessagetoXapp(RmrMessagesBuffer_t &rmrMessageBuffer,
242 ReportingMessages_t &message,
243 int failedMesgId,
244 otSpan *pSpan);
245
246int sendRequestToXapp(ReportingMessages_t &message,
247 int requestId,
248 RmrMessagesBuffer_t &rmrMmessageBuffer,
249 otSpan *pSpan);
250
251/**
252 *
253 * @param message
254 * @param msgType
255 * @param requestType
256 * @param rmrMessageBuffer
257 * @param sctpMap
258 * @param pSpan
259 * @return
260 */
aa7133@att.com84bd3342020-03-16 18:04:57 +0200261/*
ss412g1a79bdf2019-10-24 12:03:05 +0300262int sendResponseToXapp(ReportingMessages_t &message,
263 int msgType,
264 int requestType,
265 RmrMessagesBuffer_t &rmrMessageBuffer,
266 Sctp_Map_t *sctpMap,
267 otSpan *pSpan);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200268*/
ss412g1a79bdf2019-10-24 12:03:05 +0300269
270/**
271 *
272 * @param peerInfo
273 * @param message
274 * @param m
275 * @param pSpan
276 * @return
277 */
278int sendSctpMsg(ConnectedCU_t *peerInfo,
279 ReportingMessages_t &message,
280 Sctp_Map_t *m,
281 otSpan *pSpan);
282
283/**
284 *
285 * @param events
286 * @param sctpMap
287 * @param numOfMessages
288 * @param rmrMessageBuffer
289 * @param ts
290 * @param pSpan
291 * @return
292 */
293int receiveDataFromSctp(struct epoll_event *events,
294 Sctp_Map_t *sctpMap,
295 int &numOfMessages,
296 RmrMessagesBuffer_t &rmrMessageBuffer,
297 struct timespec &ts,
298 otSpan *pSpan);
299
300/**
301 *
302 * @param rmrAddress
303 * @param pSpan
304 * @return
305 */
ss412g3bac2da2020-01-05 11:52:19 +0200306void getRmrContext(sctp_params_t &pSctpParams, otSpan *pSpan);
ss412g1a79bdf2019-10-24 12:03:05 +0300307
308/**
309 *
310 * @param epoll_fd
311 * @param rmrCtx
312 * @param sctpMap
313 * @param messagBuffer
314 * @param pSpan
315 * @return
316 */
317int receiveXappMessages(int epoll_fd,
318 Sctp_Map_t *sctpMap,
319 RmrMessagesBuffer_t &rmrMessageBuffer,
320 struct timespec &ts,
321 otSpan *pSpan);
322
323/**
324 *
325 * @param rmrMessageBuffer
326 * @param message
327 * @param epoll_fd
328 * @param sctpMap
329 * @param pSpan
330 * @return
331 */
332int connectToCUandSetUp(RmrMessagesBuffer_t &rmrMessageBuffer,
333 ReportingMessages_t &message,
334 int epoll_fd,
335 Sctp_Map_t *sctpMap,
336 otSpan *pSpan);
337
338/**
339 *
340 * @param messagBuffer
341 * @param failedMsgId
342 * @param sctpMap
343 * @param pSpan
344 * @return
345 */
346int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messagBuffer,
347 ReportingMessages_t &message,
348 int failedMsgId,
349 Sctp_Map_t *sctpMap,
350 otSpan *pSpan);
351/**
352 *
353 * @param pdu
354 * @param message
355 * @param rmrMessageBuffer
356 * @param pSpan
357 */
358void asnInitiatingRequest(E2AP_PDU_t *pdu,
359 ReportingMessages_t &message,
360 RmrMessagesBuffer_t &rmrMessageBuffer,
361 otSpan *pSpan);
362/**
363 *
364 * @param pdu
365 * @param message
366 * @param sctpMap
367 * @param rmrMessageBuffer
368 * @param pSpan
369 */
370void asnSuccsesfulMsg(E2AP_PDU_t *pdu,
371 ReportingMessages_t &message,
372 Sctp_Map_t *sctpMap,
373 RmrMessagesBuffer_t &rmrMessageBuffer,
374 otSpan *pSpan);
375/**
376 *
377 * @param pdu
378 * @param message
379 * @param sctpMap
380 * @param rmrMessageBuffer
381 * @param pSpan
382 */
383void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
384 ReportingMessages_t &message,
385 Sctp_Map_t *sctpMap,
386 RmrMessagesBuffer_t &rmrMessageBuffer,
387 otSpan *pSpan);
388
389/**
390 *
391 * @param rmrMessageBuffer
392 * @param message
393 * @param pSpan
394 * @return
395 */
396int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message, otSpan *pSpan);
ss412g1a79bdf2019-10-24 12:03:05 +0300397/**
398 *
399 * @param epoll_fd
400 * @param peerInfo
401 * @param events
402 * @param sctpMap
403 * @param enodbName
404 * @param msgType
405 * @param pSpan
406 * @returnsrc::logger_mt& lg = my_logger::get();
407 */
408int addToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType, otSpan *pSpan);
409/**
410 *
411 * @param epoll_fd
412 * @param peerInfo
413 * @param events
414 * @param sctpMap
415 * @param enodbName
416 * @param msgType
417 * @param pSpan
418 * @return
419 */
420int modifyToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType, otSpan *pSpan);
421
422/**
423 *
424 * @param message
425 */
426void buildJsonMessage(ReportingMessages_t &message);
427
428/**
429 *
430 *
431 * @param state
432 * @return
433 */
434string translateRmrErrorMessages(int state);
435
ss412g3bac2da2020-01-05 11:52:19 +0200436
437static inline uint64_t rdtscp(uint32_t &aux) {
438 uint64_t rax,rdx;
439 asm volatile ("rdtscp\n" : "=a" (rax), "=d" (rdx), "=c" (aux) : :);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200440 return (rdx << (unsigned)32) + rax;
ss412g3bac2da2020-01-05 11:52:19 +0200441}
ss412g1a79bdf2019-10-24 12:03:05 +0300442#ifndef RIC_SCTP_CONNECTION_FAILURE
443#define RIC_SCTP_CONNECTION_FAILURE 10080
444#endif
445
446#endif //X2_SCTP_THREAD_H