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