2 * Copyright 2019 AT&T Intellectual Property
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef X2_SCTP_THREAD_H
19 #define X2_SCTP_THREAD_H
28 #include <sys/socket.h>
29 #include <arpa/inet.h>
30 #include <netinet/in_systm.h>
31 #include <netinet/in.h>
32 #include <netinet/ip.h>
33 #include <netinet/ip_icmp.h>
34 #include <netinet/sctp.h>
37 #include <sys/param.h>
41 #include <sys/epoll.h>
43 #include <shared_mutex>
48 #include <rmr/RIC_message_types.h>
49 #include <mdclog/mdclog.h>
53 #include <boost/algorithm/string/predicate.hpp>
54 #include <boost/lexical_cast.hpp>
55 #include <boost/move/utility.hpp>
56 #include <boost/log/sources/logger.hpp>
57 #include <boost/log/sources/record_ostream.hpp>
58 #include <boost/log/sources/global_logger_storage.hpp>
59 #include <boost/log/utility/setup/file.hpp>
60 #include <boost/log/utility/setup/common_attributes.hpp>
63 #include <mdclog/mdclog.h>
65 #include "asn1cFiles/E2AP-PDU.h"
66 #include <asn1cFiles/ProtocolIE-Container.h>
67 #include "asn1cFiles/InitiatingMessage.h"
68 #include "asn1cFiles/SuccessfulOutcome.h"
69 #include "asn1cFiles/UnsuccessfulOutcome.h"
70 #include "asn1cFiles/ProtocolIE-Container.h"
71 #include "asn1cFiles/ProtocolIE-Field.h"
74 #include "openTracing.h"
77 #include "mapWrapper.h"
82 namespace logging = boost::log;
83 namespace src = boost::log::sources;
84 namespace keywords = boost::log::keywords;
85 namespace sinks = boost::log::sinks;
86 namespace posix_time = boost::posix_time;
87 namespace expr = boost::log::expressions;
89 #define SRC_PORT 36422
90 #define SA struct sockaddr
91 #define MAX_ENODB_NAME_SIZE 64
95 #define RECEIVE_SCTP_BUFFER_SIZE (64*1024)
96 #define RECEIVE_XAPP_BUFFER_SIZE RECEIVE_SCTP_BUFFER_SIZE
98 typedef mapWrapper Sctp_Map_t;
101 typedef const std::unique_ptr<opentracing::Span> otSpan;
103 typedef const int otSpan;
106 #define VOLUME_URL_SIZE 256
108 typedef struct sctp_params {
109 uint16_t rmrPort = 0;
112 void *rmrCtx = nullptr;
113 Sctp_Map_t *sctpMap = nullptr;
114 char rmrAddress[256] {}; // "tcp:portnumber" "tcp:5566" listen to all address on port 5566
115 mdclog_severity_t logLevel = MDCLOG_INFO;
116 char volume[VOLUME_URL_SIZE];
117 //shared_timed_mutex fence; // moved to mapWrapper
120 typedef struct ConnectedCU {
121 int fileDescriptor = 0;
122 char hostName[NI_MAXHOST] {};
123 char portNumber[NI_MAXSERV] {};
124 char enodbName[MAX_ENODB_NAME_SIZE] {};
125 char asnData[RECEIVE_SCTP_BUFFER_SIZE] {};
126 size_t asnLength = 0;
128 bool isConnected = false;
131 #define MAX_RMR_BUFF_ARRY 32
132 typedef struct RmrMessagesBuffer {
134 rmr_mbuf_t *sendMessage;
135 rmr_mbuf_t *sendBufferedMessages[MAX_RMR_BUFF_ARRY];
136 rmr_mbuf_t *rcvMessage;
137 rmr_mbuf_t *rcvBufferedMessages[MAX_RMR_BUFF_ARRY];
138 } RmrMessagesBuffer_t;
140 typedef struct formatedMessage {
141 char enodbName[MAX_ENODB_NAME_SIZE];
142 struct timespec time;
146 unsigned char *asndata;
149 typedef struct ReportingMessages {
150 FormatedMessage_t message;
152 unsigned char base64Data[RECEIVE_SCTP_BUFFER_SIZE * 2];
153 char buffer[RECEIVE_SCTP_BUFFER_SIZE * 8];
155 } ReportingMessages_t;
158 void listener(sctp_params_t *params);
160 int setSocketNoBlocking(int socket);
162 void cleanHashEntry(ConnectedCU_t *peerInfo, Sctp_Map_t *m, otSpan *pSpan);
164 int getSetupRequestMetaData(ReportingMessages_t &message, char *data, char *host, uint16_t &port, otSpan *pSpan);
169 * @param rmrMessageBuffer
172 void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer, otSpan *pSpan);
177 * @param messagBuffer
179 * @param failedMesgId
183 int sendMessagetoCu(Sctp_Map_t *sctpMap,
184 RmrMessagesBuffer_t &messagBuffer,
185 ReportingMessages_t &message,
186 int failedMesgId, otSpan *pSpan);
188 void sendFailedSendingMessagetoXapp(RmrMessagesBuffer_t &rmrMessageBuffer,
189 ReportingMessages_t &message,
193 int sendRequestToXapp(ReportingMessages_t &message,
195 RmrMessagesBuffer_t &rmrMmessageBuffer,
203 * @param rmrMessageBuffer
208 int sendResponseToXapp(ReportingMessages_t &message,
211 RmrMessagesBuffer_t &rmrMessageBuffer,
223 int sendSctpMsg(ConnectedCU_t *peerInfo,
224 ReportingMessages_t &message,
232 * @param numOfMessages
233 * @param rmrMessageBuffer
238 int receiveDataFromSctp(struct epoll_event *events,
241 RmrMessagesBuffer_t &rmrMessageBuffer,
251 void *getRmrContext(char *rmrAddress, otSpan *pSpan);
258 * @param messagBuffer
262 int receiveXappMessages(int epoll_fd,
264 RmrMessagesBuffer_t &rmrMessageBuffer,
270 * @param rmrMessageBuffer
277 int connectToCUandSetUp(RmrMessagesBuffer_t &rmrMessageBuffer,
278 ReportingMessages_t &message,
285 * @param messagBuffer
291 int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messagBuffer,
292 ReportingMessages_t &message,
300 * @param rmrMessageBuffer
303 void asnInitiatingRequest(E2AP_PDU_t *pdu,
304 ReportingMessages_t &message,
305 RmrMessagesBuffer_t &rmrMessageBuffer,
312 * @param rmrMessageBuffer
315 void asnSuccsesfulMsg(E2AP_PDU_t *pdu,
316 ReportingMessages_t &message,
318 RmrMessagesBuffer_t &rmrMessageBuffer,
325 * @param rmrMessageBuffer
328 void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
329 ReportingMessages_t &message,
331 RmrMessagesBuffer_t &rmrMessageBuffer,
336 * @param rmrMessageBuffer
341 int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message, otSpan *pSpan);
352 * @returnsrc::logger_mt& lg = my_logger::get();
354 int addToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType, otSpan *pSpan);
366 int modifyToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType, otSpan *pSpan);
372 void buildJsonMessage(ReportingMessages_t &message);
380 string translateRmrErrorMessages(int state);
382 #ifndef RIC_SCTP_CONNECTION_FAILURE
383 #define RIC_SCTP_CONNECTION_FAILURE 10080
386 #endif //X2_SCTP_THREAD_H