version 4.0.1 remove unsupported messages
[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
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>
68 #include <boost/filesystem.hpp>
69
70 #include <mdclog/mdclog.h>
71
72 #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"
83
84 #include "cxxopts.hpp"
85 //#include "config-cpp/include/config-cpp/config-cpp.h"
86
87 #ifdef __TRACING__
88 #include "openTracing.h"
89 #endif
90
91 #include "mapWrapper.h"
92
93 #include "base64.h"
94
95 #include "ReadConfigFile.h"
96
97 using namespace std;
98 namespace logging = boost::log;
99 namespace src = boost::log::sources;
100 namespace keywords = boost::log::keywords;
101 namespace sinks = boost::log::sinks;
102 namespace posix_time = boost::posix_time;
103 namespace 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
111 #define RECEIVE_SCTP_BUFFER_SIZE (8 * 1024)
112 #define RECEIVE_XAPP_BUFFER_SIZE RECEIVE_SCTP_BUFFER_SIZE 
113
114 typedef mapWrapper Sctp_Map_t;
115
116
117 #define VOLUME_URL_SIZE 256
118 #define KA_MESSAGE_SIZE 2048
119
120 typedef struct sctp_params {
121     uint16_t rmrPort = 0;
122     uint16_t sctpPort = SRC_PORT;
123     int      epoll_fd = 0;
124     int      listenFD = 0;
125     int      rmrListenFd = 0;
126     int      inotifyFD = 0;
127     int      inotifyWD = 0;
128     void     *rmrCtx = nullptr;
129     Sctp_Map_t *sctpMap = nullptr;
130     char      ka_message[KA_MESSAGE_SIZE] {};
131     int       ka_message_length = 0;
132     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];
135     string myIP {};
136     string fqdn {};
137     string podName {};
138     string configFilePath {};
139     string configFileName {};
140     bool trace = true;
141     //shared_timed_mutex fence; // moved to mapWrapper
142 } sctp_params_t;
143
144 typedef 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] {};
150     size_t asnLength = 0;
151     int mtype = 0;
152     bool isConnected = false;
153     bool gotSetup = false;
154     sctp_params_t *sctpParams = nullptr;
155 } ConnectedCU_t ;
156
157 #define MAX_RMR_BUFF_ARRY 32
158 typedef struct RmrMessagesBuffer {
159     char ka_message[KA_MESSAGE_SIZE] {};
160     int  ka_message_len = 0;
161     void *rmrCtx = nullptr;
162     rmr_mbuf_t *sendMessage= nullptr;
163     rmr_mbuf_t *sendBufferedMessages[MAX_RMR_BUFF_ARRY] {};
164     rmr_mbuf_t *rcvMessage= nullptr;
165     rmr_mbuf_t *rcvBufferedMessages[MAX_RMR_BUFF_ARRY] {};
166 } RmrMessagesBuffer_t;
167
168 typedef struct formatedMessage {
169     char enodbName[MAX_ENODB_NAME_SIZE];
170     struct timespec time;
171     int messageType;
172     char direction;
173     ssize_t asnLength;
174     unsigned char *asndata;
175 } FormatedMessage_t;
176
177 typedef struct ReportingMessages {
178     FormatedMessage_t message {};
179     ConnectedCU_t *peerInfo = nullptr;
180     long outLen = 0;
181     unsigned char base64Data[RECEIVE_SCTP_BUFFER_SIZE * 2] {};
182     char buffer[RECEIVE_SCTP_BUFFER_SIZE * 8] {};
183 } ReportingMessages_t;
184
185 cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &pSctpParams);
186
187 int buildInotify(sctp_params_t &sctpParams);
188
189 void handleTermInit(sctp_params_t &sctpParams);
190
191 void handleConfigChange(sctp_params_t *sctpParams);
192
193 void listener(sctp_params_t *params);
194
195 void sendTermInit(sctp_params_t &sctpParams);
196
197 int setSocketNoBlocking(int socket);
198
199 void handleEinprogressMessages(struct epoll_event &event,
200                                ReportingMessages_t &message,
201                                RmrMessagesBuffer_t &rmrMessageBuffer,
202                                sctp_params_t *params);
203
204 void handlepoll_error(struct epoll_event &event,
205                       ReportingMessages_t &message,
206                       RmrMessagesBuffer_t &rmrMessageBuffer,
207                       sctp_params_t *params);
208
209
210 void cleanHashEntry(ConnectedCU_t *peerInfo, Sctp_Map_t *m);
211
212 int getSetupRequestMetaData(ReportingMessages_t &message, char *data, char *host, uint16_t &port);
213
214 /**
215  *
216  * @param message
217  * @param rmrMessageBuffer
218  */
219 void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer);
220
221 /**
222  *
223  * @param sctpMap
224  * @param messagBuffer
225  * @param message
226  * @param failedMesgId
227  * @return
228  */
229 int sendMessagetoCu(Sctp_Map_t *sctpMap,
230                     RmrMessagesBuffer_t &messagBuffer,
231                     ReportingMessages_t &message,
232                     int failedMesgId);
233
234 void sendFailedSendingMessagetoXapp(RmrMessagesBuffer_t &rmrMessageBuffer,
235                                     ReportingMessages_t &message,
236                                     int failedMesgId);
237
238 int sendRequestToXapp(ReportingMessages_t &message,
239                       int requestId,
240                       RmrMessagesBuffer_t &rmrMmessageBuffer);
241
242 /**
243  *
244  * @param message
245  * @param msgType
246  * @param requestType
247  * @param rmrMessageBuffer
248  * @param sctpMap
249  * @return
250  */
251 /*
252 int sendResponseToXapp(ReportingMessages_t &message,
253                        int msgType,
254                        int requestType,
255                        RmrMessagesBuffer_t &rmrMessageBuffer,
256                        Sctp_Map_t *sctpMap);
257 */
258
259 /**
260  *
261  * @param peerInfo
262  * @param message
263  * @param m
264  * @return
265  */
266 int sendSctpMsg(ConnectedCU_t *peerInfo,
267                 ReportingMessages_t &message,
268                 Sctp_Map_t *m);
269
270 /**
271  *
272  * @param events
273  * @param sctpMap
274  * @param numOfMessages
275  * @param rmrMessageBuffer
276  * @param ts
277  * @return
278  */
279 int receiveDataFromSctp(struct epoll_event *events,
280                         Sctp_Map_t *sctpMap,
281                         int &numOfMessages,
282                         RmrMessagesBuffer_t &rmrMessageBuffer,
283                         struct timespec &ts);
284
285 /**
286  *
287  * @param rmrAddress
288  * @return
289  */
290 void getRmrContext(sctp_params_t &pSctpParams);
291
292 /**
293  *
294  * @param epoll_fd
295  * @param rmrCtx
296  * @param sctpMap
297  * @param messagBuffer
298  * @return
299  */
300 int receiveXappMessages(int epoll_fd,
301                         Sctp_Map_t *sctpMap,
302                         RmrMessagesBuffer_t &rmrMessageBuffer,
303                         struct timespec &ts);
304
305 /**
306  *
307  * @param rmrMessageBuffer
308  * @param message
309  * @param epoll_fd
310  * @param sctpMap
311  * @return
312  */
313 int connectToCUandSetUp(RmrMessagesBuffer_t &rmrMessageBuffer,
314                            ReportingMessages_t &message,
315                            int epoll_fd,
316                            Sctp_Map_t *sctpMap);
317
318 /**
319  *
320  * @param messagBuffer
321  * @param failedMsgId
322  * @param sctpMap
323  * @return
324  */
325 int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messagBuffer,
326                            ReportingMessages_t &message,
327                            int failedMsgId,
328                            Sctp_Map_t *sctpMap);
329 /**
330  *
331  * @param pdu
332  * @param message
333  * @param rmrMessageBuffer
334  */
335 void asnInitiatingRequest(E2AP_PDU_t *pdu,
336                           ReportingMessages_t &message,
337                           RmrMessagesBuffer_t &rmrMessageBuffer);
338 /**
339  *
340  * @param pdu
341  * @param message
342  * @param sctpMap
343  * @param rmrMessageBuffer
344  */
345 void asnSuccsesfulMsg(E2AP_PDU_t *pdu,
346                       ReportingMessages_t &message,
347                       Sctp_Map_t *sctpMap,
348                       RmrMessagesBuffer_t &rmrMessageBuffer);
349 /**
350  *
351  * @param pdu
352  * @param message
353  * @param sctpMap
354  * @param rmrMessageBuffer
355  */
356 void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
357                         ReportingMessages_t &message,
358                         Sctp_Map_t *sctpMap,
359                         RmrMessagesBuffer_t &rmrMessageBuffer);
360
361 /**
362  *
363  * @param rmrMessageBuffer
364  * @param message
365  * @return
366  */
367 int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message);
368 /**
369  *
370  * @param epoll_fd
371  * @param peerInfo
372  * @param events
373  * @param sctpMap
374  * @param enodbName
375  * @param msgType
376  * @returnsrc::logger_mt& lg = my_logger::get();
377  */
378 int addToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType);
379 /**
380  *
381  * @param epoll_fd
382  * @param peerInfo
383  * @param events
384  * @param sctpMap
385  * @param enodbName
386  * @param msgType
387  * @return
388  */
389 int modifyToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType);
390
391 /**
392  *
393  * @param message
394  */
395 void buildJsonMessage(ReportingMessages_t &message);
396
397 /**
398  *
399  *
400  * @param state
401  * @return
402  */
403 string translateRmrErrorMessages(int state);
404
405
406 static inline uint64_t rdtscp(uint32_t &aux) {
407     uint64_t rax,rdx;
408     asm volatile ("rdtscp\n" : "=a" (rax), "=d" (rdx), "=c" (aux) : :);
409     return (rdx << (unsigned)32) + rax;
410 }
411 #ifndef RIC_SCTP_CONNECTION_FAILURE
412 #define RIC_SCTP_CONNECTION_FAILURE  10080
413 #endif
414
415 #endif //X2_SCTP_THREAD_H