Add R5 content to master
[ric-plt/e2.git] / RIC-E2-TERMINATION / sctpThread.h
1 /*
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.
16 */
17
18 /*
19  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20  * platform project (RICP).
21  */
22
23 #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>
51 #include <sys/inotify.h>
52 #include <csignal>
53 #include <future>
54
55 #include <rmr/rmr.h>
56 #include <rmr/RIC_message_types.h>
57 #include <mdclog/mdclog.h>
58 #include <functional>
59 #include <iostream>
60
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>
70
71 #include <mdclog/mdclog.h>
72
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"
84
85 #include "cxxopts.hpp"
86 //#include "config-cpp/include/config-cpp/config-cpp.h"
87 #include <zlib.h>
88 #include <prometheus/counter.h>
89 #include <prometheus/exposer.h>
90 #include <prometheus/gateway.h>
91 #include <prometheus/registry.h>
92
93 using namespace prometheus;
94
95 #include "mapWrapper.h"
96
97 #include "base64.h"
98
99 #include "ReadConfigFile.h"
100
101 using namespace std;
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;
108
109 #define SRC_PORT 36422
110 #define SA      struct sockaddr
111 #define MAX_ENODB_NAME_SIZE 64
112
113 #define MAXEVENTS 128
114
115 #define RECEIVE_SCTP_BUFFER_SIZE (256 * 1024)
116 #define RECEIVE_XAPP_BUFFER_SIZE RECEIVE_SCTP_BUFFER_SIZE
117
118 typedef mapWrapper Sctp_Map_t;
119
120
121
122 #define VOLUME_URL_SIZE 256
123 #define KA_MESSAGE_SIZE 2048
124
125 typedef struct sctp_params {
126     int      epollTimeOut = -1;
127     uint16_t rmrPort = 0;
128     uint16_t sctpPort = SRC_PORT;
129     int      epoll_fd = 0;
130     int      listenFD = 0;
131     int      rmrListenFd = 0;
132     int      inotifyFD = 0;
133     int      inotifyWD = 0;
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:portnumber" "tcp:5566" listen to all address on port 5566
139     mdclog_severity_t logLevel = MDCLOG_INFO;
140     char volume[VOLUME_URL_SIZE];
141     string myIP {};
142     string fqdn {};
143     string podName {};
144     string configFilePath {};
145     string configFileName {};
146     bool trace = true;
147     string prometheusMode {"pull"};
148     string prometheusPushAddress {"127.0.0.1:7676"};
149     shared_ptr<prometheus::Registry> prometheusRegistry;
150     string prometheusPort {"8088"};
151     Family<Counter> *prometheusFamily;
152     Gateway *prometheusGateway = nullptr;
153     Exposer *prometheusExposer = nullptr;
154 } sctp_params_t;
155
156 // RAN to RIC
157 #define IN_INITI 0 //INITIATING
158 #define IN_SUCC 1 //SUCCESSFULL
159 #define IN_UN_SUCC 2 //UN-Successfull
160
161 // RIC To RAN
162 #define OUT_INITI 3 //INITIATING
163 #define OUT_SUCC 4 //SUCCESSFULL
164 #define OUT_UN_SUCC 5 //UN-Successfull
165
166 #define MSG_COUNTER 0
167 #define BYTES_COUNTER 1
168
169 typedef struct ConnectedCU {
170     int fileDescriptor = 0;
171     char hostName[NI_MAXHOST] {};
172     char portNumber[NI_MAXSERV] {};
173     char enodbName[MAX_ENODB_NAME_SIZE] {};
174     char asnData[RECEIVE_SCTP_BUFFER_SIZE] {};
175     size_t asnLength = 0;
176     int mtype = 0;
177     bool isConnected = false;
178     bool gotSetup = false;
179     sctp_params_t *sctpParams = nullptr;
180     Counter *counters[6][2][ProcedureCode_id_RICsubscriptionDelete] {};
181 } ConnectedCU_t ;
182
183
184 #define MAX_RMR_BUFF_ARRY 32
185 typedef struct RmrMessagesBuffer {
186     char ka_message[KA_MESSAGE_SIZE] {};
187     int  ka_message_len = 0;
188     void *rmrCtx = nullptr;
189     rmr_mbuf_t *sendMessage= nullptr;
190     //rmr_mbuf_t *sendBufferedMessages[MAX_RMR_BUFF_ARRY] {};
191     rmr_mbuf_t *rcvMessage= nullptr;
192     //rmr_mbuf_t *rcvBufferedMessages[MAX_RMR_BUFF_ARRY] {};
193 } RmrMessagesBuffer_t;
194
195 typedef struct formatedMessage {
196     char enodbName[MAX_ENODB_NAME_SIZE];
197     struct timespec time;
198     int messageType;
199     char direction;
200     ssize_t asnLength;
201     unsigned char *asndata;
202 } FormatedMessage_t;
203
204 typedef struct ReportingMessages {
205     FormatedMessage_t message {};
206     ConnectedCU_t *peerInfo = nullptr;
207     long outLen = 0;
208     unsigned char base64Data[RECEIVE_SCTP_BUFFER_SIZE * 2] {};
209     char buffer[RECEIVE_SCTP_BUFFER_SIZE * 8] {};
210 } ReportingMessages_t;
211
212 cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &pSctpParams);
213
214 int buildInotify(sctp_params_t &sctpParams);
215
216 void handleTermInit(sctp_params_t &sctpParams);
217
218 void handleConfigChange(sctp_params_t *sctpParams);
219
220 void listener(sctp_params_t *params);
221
222 void sendTermInit(sctp_params_t &sctpParams);
223
224 int setSocketNoBlocking(int socket);
225
226 void handleEinprogressMessages(struct epoll_event &event,
227                                ReportingMessages_t &message,
228                                RmrMessagesBuffer_t &rmrMessageBuffer,
229                                sctp_params_t *params);
230
231 void handlepoll_error(struct epoll_event &event,
232                       ReportingMessages_t &message,
233                       RmrMessagesBuffer_t &rmrMessageBuffer,
234                       sctp_params_t *params);
235
236
237 void cleanHashEntry(ConnectedCU_t *peerInfo, Sctp_Map_t *m);
238
239
240 /**
241  *
242  * @param message
243  * @param rmrMessageBuffer
244  */
245 void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer);
246
247 /**
248  *
249  * @param sctpMap
250  * @param messagBuffer
251  * @param message
252  * @param failedMesgId
253  * @return
254  */
255 int sendMessagetoCu(Sctp_Map_t *sctpMap,
256                     RmrMessagesBuffer_t &messagBuffer,
257                     ReportingMessages_t &message,
258                     int failedMesgId);
259
260 void sendFailedSendingMessagetoXapp(RmrMessagesBuffer_t &rmrMessageBuffer,
261                                     ReportingMessages_t &message,
262                                     int failedMesgId);
263
264 int sendRequestToXapp(ReportingMessages_t &message,
265                       int requestId,
266                       RmrMessagesBuffer_t &rmrMmessageBuffer);
267
268 /**
269  *
270  * @param message
271  * @param msgType
272  * @param requestType
273  * @param rmrMessageBuffer
274  * @param sctpMap
275  * @return
276  */
277 /*
278 int sendResponseToXapp(ReportingMessages_t &message,
279                        int msgType,
280                        int requestType,
281                        RmrMessagesBuffer_t &rmrMessageBuffer,
282                        Sctp_Map_t *sctpMap);
283 */
284
285 /**
286  *
287  * @param peerInfo
288  * @param message
289  * @param m
290  * @return
291  */
292 int sendSctpMsg(ConnectedCU_t *peerInfo,
293                 ReportingMessages_t &message,
294                 Sctp_Map_t *m);
295
296 /**
297  *
298  * @param events
299  * @param sctpMap
300  * @param numOfMessages
301  * @param rmrMessageBuffer
302  * @param ts
303  * @return
304  */
305 int receiveDataFromSctp(struct epoll_event *events,
306                         Sctp_Map_t *sctpMap,
307                         int &numOfMessages,
308                         RmrMessagesBuffer_t &rmrMessageBuffer,
309                         struct timespec &ts);
310
311 /**
312  *
313  * @param rmrAddress
314  * @return
315  */
316 void getRmrContext(sctp_params_t &pSctpParams);
317
318 /**
319  *
320  * @param epoll_fd
321  * @param rmrCtx
322  * @param sctpMap
323  * @param messagBuffer
324  * @return
325  */
326 int receiveXappMessages(Sctp_Map_t *sctpMap,
327                         RmrMessagesBuffer_t &rmrMessageBuffer,
328                         struct timespec &ts);
329
330 /**
331  *
332  * @param messagBuffer
333  * @param failedMsgId
334  * @param sctpMap
335  * @return
336  */
337 int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messagBuffer,
338                            ReportingMessages_t &message,
339                            int failedMsgId,
340                            Sctp_Map_t *sctpMap);
341 /**
342  *
343  * @param pdu
344  * @param message
345  * @param rmrMessageBuffer
346  */
347 void asnInitiatingRequest(E2AP_PDU_t *pdu,
348                           Sctp_Map_t *sctpMap,
349                           ReportingMessages_t &message,
350                           RmrMessagesBuffer_t &rmrMessageBuffer);
351 /**
352  *
353  * @param pdu
354  * @param message
355  * @param sctpMap
356  * @param rmrMessageBuffer
357  */
358 void asnSuccsesfulMsg(E2AP_PDU_t *pdu,
359                       Sctp_Map_t *sctpMap,
360                       ReportingMessages_t &message,
361                       RmrMessagesBuffer_t &rmrMessageBuffer);
362 /**
363  *
364  * @param pdu
365  * @param message
366  * @param sctpMap
367  * @param rmrMessageBuffer
368  */
369 void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
370                         Sctp_Map_t *sctpMap,
371                         ReportingMessages_t &message,
372                         RmrMessagesBuffer_t &rmrMessageBuffer);
373
374 /**
375  *
376  * @param rmrMessageBuffer
377  * @param message
378  * @return
379  */
380 int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message);
381 /**
382  *
383  * @param epoll_fd
384  * @param peerInfo
385  * @param events
386  * @param sctpMap
387  * @param enodbName
388  * @param msgType
389  * @returnsrc::logger_mt& lg = my_logger::get();
390  */
391 int addToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType);
392 /**
393  *
394  * @param epoll_fd
395  * @param peerInfo
396  * @param events
397  * @param sctpMap
398  * @param enodbName
399  * @param msgType
400  * @return
401  */
402 int modifyToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType);
403
404 /**
405  *
406  * @param message
407  */
408 void buildJsonMessage(ReportingMessages_t &message);
409
410 /**
411  *
412  *
413  * @param state
414  * @return
415  */
416 string translateRmrErrorMessages(int state);
417
418
419 static inline uint64_t rdtscp(uint32_t &aux) {
420     uint64_t rax,rdx;
421     asm volatile ("rdtscp\n" : "=a" (rax), "=d" (rdx), "=c" (aux) : :);
422     return (rdx << (unsigned)32) + rax;
423 }
424 #ifndef RIC_SCTP_CONNECTION_FAILURE
425 #define RIC_SCTP_CONNECTION_FAILURE  10080
426 #endif
427
428 #endif //X2_SCTP_THREAD_H