ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame^] | 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 | #ifndef X2_SCTP_THREAD_H |
| 19 | #define X2_SCTP_THREAD_H |
| 20 | |
| 21 | #include <algorithm> |
| 22 | |
| 23 | #include <cstdio> |
| 24 | #include <cerrno> |
| 25 | #include <cstdlib> |
| 26 | #include <cstring> |
| 27 | #include <random> |
| 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> |
| 35 | #include <thread> |
| 36 | #include <atomic> |
| 37 | #include <sys/param.h> |
| 38 | #include <sys/file.h> |
| 39 | #include <ctime> |
| 40 | #include <netdb.h> |
| 41 | #include <sys/epoll.h> |
| 42 | #include <mutex> |
| 43 | #include <shared_mutex> |
| 44 | #include <iterator> |
| 45 | #include <map> |
| 46 | |
| 47 | #include <rmr/rmr.h> |
| 48 | #include <rmr/RIC_message_types.h> |
| 49 | #include <mdclog/mdclog.h> |
| 50 | #include <functional> |
| 51 | #include <iostream> |
| 52 | |
| 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> |
| 61 | |
| 62 | |
| 63 | #include <mdclog/mdclog.h> |
| 64 | |
| 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" |
| 72 | |
| 73 | #ifdef __TRACING__ |
| 74 | #include "openTracing.h" |
| 75 | #endif |
| 76 | |
| 77 | #include "mapWrapper.h" |
| 78 | |
| 79 | #include "base64.h" |
| 80 | |
| 81 | using namespace std; |
| 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; |
| 88 | |
| 89 | #define SRC_PORT 36422 |
| 90 | #define SA struct sockaddr |
| 91 | #define MAX_ENODB_NAME_SIZE 64 |
| 92 | |
| 93 | #define MAXEVENTS 128 |
| 94 | |
| 95 | #define RECEIVE_SCTP_BUFFER_SIZE (64*1024) |
| 96 | #define RECEIVE_XAPP_BUFFER_SIZE RECEIVE_SCTP_BUFFER_SIZE |
| 97 | |
| 98 | typedef mapWrapper Sctp_Map_t; |
| 99 | |
| 100 | #ifdef __TRACING__ |
| 101 | typedef const std::unique_ptr<opentracing::Span> otSpan; |
| 102 | #else |
| 103 | typedef const int otSpan; |
| 104 | #endif |
| 105 | |
| 106 | #define VOLUME_URL_SIZE 256 |
| 107 | |
| 108 | typedef struct sctp_params { |
| 109 | uint16_t rmrPort = 0; |
| 110 | int epoll_fd = 0; |
| 111 | int rmrListenFd = 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 |
| 118 | } sctp_params_t; |
| 119 | |
| 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; |
| 127 | int mtype = 0; |
| 128 | bool isConnected = false; |
| 129 | } ConnectedCU_t ; |
| 130 | |
| 131 | #define MAX_RMR_BUFF_ARRY 32 |
| 132 | typedef struct RmrMessagesBuffer { |
| 133 | void *rmrCtx; |
| 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; |
| 139 | |
| 140 | typedef struct formatedMessage { |
| 141 | char enodbName[MAX_ENODB_NAME_SIZE]; |
| 142 | struct timespec time; |
| 143 | int messageType; |
| 144 | char direction; |
| 145 | ssize_t asnLength; |
| 146 | unsigned char *asndata; |
| 147 | } FormatedMessage_t; |
| 148 | |
| 149 | typedef struct ReportingMessages { |
| 150 | FormatedMessage_t message; |
| 151 | int outLen; |
| 152 | unsigned char base64Data[RECEIVE_SCTP_BUFFER_SIZE * 2]; |
| 153 | char buffer[RECEIVE_SCTP_BUFFER_SIZE * 8]; |
| 154 | size_t bufferLen; |
| 155 | } ReportingMessages_t; |
| 156 | |
| 157 | |
| 158 | void listener(sctp_params_t *params); |
| 159 | |
| 160 | int setSocketNoBlocking(int socket); |
| 161 | |
| 162 | void cleanHashEntry(ConnectedCU_t *peerInfo, Sctp_Map_t *m, otSpan *pSpan); |
| 163 | |
| 164 | int getSetupRequestMetaData(ReportingMessages_t &message, char *data, char *host, uint16_t &port, otSpan *pSpan); |
| 165 | |
| 166 | /** |
| 167 | * |
| 168 | * @param message |
| 169 | * @param rmrMessageBuffer |
| 170 | * @param pSpan |
| 171 | */ |
| 172 | void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer, otSpan *pSpan); |
| 173 | |
| 174 | /** |
| 175 | * |
| 176 | * @param sctpMap |
| 177 | * @param messagBuffer |
| 178 | * @param message |
| 179 | * @param failedMesgId |
| 180 | * @param pSpan |
| 181 | * @return |
| 182 | */ |
| 183 | int sendMessagetoCu(Sctp_Map_t *sctpMap, |
| 184 | RmrMessagesBuffer_t &messagBuffer, |
| 185 | ReportingMessages_t &message, |
| 186 | int failedMesgId, otSpan *pSpan); |
| 187 | |
| 188 | void sendFailedSendingMessagetoXapp(RmrMessagesBuffer_t &rmrMessageBuffer, |
| 189 | ReportingMessages_t &message, |
| 190 | int failedMesgId, |
| 191 | otSpan *pSpan); |
| 192 | |
| 193 | int sendRequestToXapp(ReportingMessages_t &message, |
| 194 | int requestId, |
| 195 | RmrMessagesBuffer_t &rmrMmessageBuffer, |
| 196 | otSpan *pSpan); |
| 197 | |
| 198 | /** |
| 199 | * |
| 200 | * @param message |
| 201 | * @param msgType |
| 202 | * @param requestType |
| 203 | * @param rmrMessageBuffer |
| 204 | * @param sctpMap |
| 205 | * @param pSpan |
| 206 | * @return |
| 207 | */ |
| 208 | int sendResponseToXapp(ReportingMessages_t &message, |
| 209 | int msgType, |
| 210 | int requestType, |
| 211 | RmrMessagesBuffer_t &rmrMessageBuffer, |
| 212 | Sctp_Map_t *sctpMap, |
| 213 | otSpan *pSpan); |
| 214 | |
| 215 | /** |
| 216 | * |
| 217 | * @param peerInfo |
| 218 | * @param message |
| 219 | * @param m |
| 220 | * @param pSpan |
| 221 | * @return |
| 222 | */ |
| 223 | int sendSctpMsg(ConnectedCU_t *peerInfo, |
| 224 | ReportingMessages_t &message, |
| 225 | Sctp_Map_t *m, |
| 226 | otSpan *pSpan); |
| 227 | |
| 228 | /** |
| 229 | * |
| 230 | * @param events |
| 231 | * @param sctpMap |
| 232 | * @param numOfMessages |
| 233 | * @param rmrMessageBuffer |
| 234 | * @param ts |
| 235 | * @param pSpan |
| 236 | * @return |
| 237 | */ |
| 238 | int receiveDataFromSctp(struct epoll_event *events, |
| 239 | Sctp_Map_t *sctpMap, |
| 240 | int &numOfMessages, |
| 241 | RmrMessagesBuffer_t &rmrMessageBuffer, |
| 242 | struct timespec &ts, |
| 243 | otSpan *pSpan); |
| 244 | |
| 245 | /** |
| 246 | * |
| 247 | * @param rmrAddress |
| 248 | * @param pSpan |
| 249 | * @return |
| 250 | */ |
| 251 | void *getRmrContext(char *rmrAddress, otSpan *pSpan); |
| 252 | |
| 253 | /** |
| 254 | * |
| 255 | * @param epoll_fd |
| 256 | * @param rmrCtx |
| 257 | * @param sctpMap |
| 258 | * @param messagBuffer |
| 259 | * @param pSpan |
| 260 | * @return |
| 261 | */ |
| 262 | int receiveXappMessages(int epoll_fd, |
| 263 | Sctp_Map_t *sctpMap, |
| 264 | RmrMessagesBuffer_t &rmrMessageBuffer, |
| 265 | struct timespec &ts, |
| 266 | otSpan *pSpan); |
| 267 | |
| 268 | /** |
| 269 | * |
| 270 | * @param rmrMessageBuffer |
| 271 | * @param message |
| 272 | * @param epoll_fd |
| 273 | * @param sctpMap |
| 274 | * @param pSpan |
| 275 | * @return |
| 276 | */ |
| 277 | int connectToCUandSetUp(RmrMessagesBuffer_t &rmrMessageBuffer, |
| 278 | ReportingMessages_t &message, |
| 279 | int epoll_fd, |
| 280 | Sctp_Map_t *sctpMap, |
| 281 | otSpan *pSpan); |
| 282 | |
| 283 | /** |
| 284 | * |
| 285 | * @param messagBuffer |
| 286 | * @param failedMsgId |
| 287 | * @param sctpMap |
| 288 | * @param pSpan |
| 289 | * @return |
| 290 | */ |
| 291 | int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messagBuffer, |
| 292 | ReportingMessages_t &message, |
| 293 | int failedMsgId, |
| 294 | Sctp_Map_t *sctpMap, |
| 295 | otSpan *pSpan); |
| 296 | /** |
| 297 | * |
| 298 | * @param pdu |
| 299 | * @param message |
| 300 | * @param rmrMessageBuffer |
| 301 | * @param pSpan |
| 302 | */ |
| 303 | void asnInitiatingRequest(E2AP_PDU_t *pdu, |
| 304 | ReportingMessages_t &message, |
| 305 | RmrMessagesBuffer_t &rmrMessageBuffer, |
| 306 | otSpan *pSpan); |
| 307 | /** |
| 308 | * |
| 309 | * @param pdu |
| 310 | * @param message |
| 311 | * @param sctpMap |
| 312 | * @param rmrMessageBuffer |
| 313 | * @param pSpan |
| 314 | */ |
| 315 | void asnSuccsesfulMsg(E2AP_PDU_t *pdu, |
| 316 | ReportingMessages_t &message, |
| 317 | Sctp_Map_t *sctpMap, |
| 318 | RmrMessagesBuffer_t &rmrMessageBuffer, |
| 319 | otSpan *pSpan); |
| 320 | /** |
| 321 | * |
| 322 | * @param pdu |
| 323 | * @param message |
| 324 | * @param sctpMap |
| 325 | * @param rmrMessageBuffer |
| 326 | * @param pSpan |
| 327 | */ |
| 328 | void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu, |
| 329 | ReportingMessages_t &message, |
| 330 | Sctp_Map_t *sctpMap, |
| 331 | RmrMessagesBuffer_t &rmrMessageBuffer, |
| 332 | otSpan *pSpan); |
| 333 | |
| 334 | /** |
| 335 | * |
| 336 | * @param rmrMessageBuffer |
| 337 | * @param message |
| 338 | * @param pSpan |
| 339 | * @return |
| 340 | */ |
| 341 | int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message, otSpan *pSpan); |
| 342 | |
| 343 | /** |
| 344 | * |
| 345 | * @param epoll_fd |
| 346 | * @param peerInfo |
| 347 | * @param events |
| 348 | * @param sctpMap |
| 349 | * @param enodbName |
| 350 | * @param msgType |
| 351 | * @param pSpan |
| 352 | * @returnsrc::logger_mt& lg = my_logger::get(); |
| 353 | */ |
| 354 | int addToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType, otSpan *pSpan); |
| 355 | /** |
| 356 | * |
| 357 | * @param epoll_fd |
| 358 | * @param peerInfo |
| 359 | * @param events |
| 360 | * @param sctpMap |
| 361 | * @param enodbName |
| 362 | * @param msgType |
| 363 | * @param pSpan |
| 364 | * @return |
| 365 | */ |
| 366 | int modifyToEpoll(int epoll_fd, ConnectedCU_t *peerInfo, uint32_t events, Sctp_Map_t *sctpMap, char *enodbName, int msgType, otSpan *pSpan); |
| 367 | |
| 368 | /** |
| 369 | * |
| 370 | * @param message |
| 371 | */ |
| 372 | void buildJsonMessage(ReportingMessages_t &message); |
| 373 | |
| 374 | /** |
| 375 | * |
| 376 | * |
| 377 | * @param state |
| 378 | * @return |
| 379 | */ |
| 380 | string translateRmrErrorMessages(int state); |
| 381 | |
| 382 | #ifndef RIC_SCTP_CONNECTION_FAILURE |
| 383 | #define RIC_SCTP_CONNECTION_FAILURE 10080 |
| 384 | #endif |
| 385 | |
| 386 | #endif //X2_SCTP_THREAD_H |