1 // Copyright 2019 AT&T Intellectual Property
2 // Copyright 2019 Nokia
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
16 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 // platform project (RICP).
19 // TODO: High-level file comment.
23 #include <3rdparty/oranE2/RANfunctions-List.h>
24 #include "sctpThread.h"
25 #include "BuildRunName.h"
27 //#include "3rdparty/oranE2SM/E2SM-gNB-NRT-RANfunction-Definition.h"
28 //#include "BuildXml.h"
29 //#include "pugixml/src/pugixml.hpp"
32 //using namespace std::placeholders;
33 using namespace boost::filesystem;
34 using namespace prometheus;
42 // need to expose without the include of gcov
43 extern "C" void __gcov_flush(void);
45 static void catch_function(int signal) {
51 BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
53 boost::shared_ptr<sinks::synchronous_sink<sinks::text_file_backend>> boostLogger;
54 double cpuClock = 0.0;
55 bool jsonTrace = false;
59 mdclog_attr_init(&attr);
60 mdclog_attr_set_ident(attr, "E2Terminator");
62 mdclog_attr_destroy(attr);
64 auto start_time = std::chrono::high_resolution_clock::now();
65 typedef std::chrono::duration<double, std::ratio<1,1>> seconds_t;
68 return seconds_t(std::chrono::high_resolution_clock::now() - start_time).count();
71 double approx_CPU_MHz(unsigned sleepTime) {
72 using namespace std::chrono_literals;
74 uint64_t cycles_start = rdtscp(aux);
75 double time_start = age();
76 std::this_thread::sleep_for(sleepTime * 1ms);
77 uint64_t elapsed_cycles = rdtscp(aux) - cycles_start;
78 double elapsed_time = age() - time_start;
79 return elapsed_cycles / elapsed_time;
82 //std::atomic<int64_t> rmrCounter{0};
83 std::atomic<int64_t> num_of_messages{0};
84 std::atomic<int64_t> num_of_XAPP_messages{0};
85 static long transactionCounter = 0;
87 int buildListeningPort(sctp_params_t &sctpParams) {
88 sctpParams.listenFD = socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP);
89 if (sctpParams.listenFD <= 0) {
90 mdclog_write(MDCLOG_ERR, "Error Opening socket, %s", strerror(errno));
94 struct sockaddr_in6 serverAddress {};
95 serverAddress.sin6_family = AF_INET6;
96 serverAddress.sin6_addr = in6addr_any;
97 serverAddress.sin6_port = htons(sctpParams.sctpPort);
98 if (bind(sctpParams.listenFD, (SA *)&serverAddress, sizeof(serverAddress)) < 0 ) {
99 mdclog_write(MDCLOG_ERR, "Error binding port %d. %s", sctpParams.sctpPort, strerror(errno));
102 if (setSocketNoBlocking(sctpParams.listenFD) == -1) {
103 //mdclog_write(MDCLOG_ERR, "Error binding. %s", strerror(errno));
106 if (mdclog_level_get() >= MDCLOG_DEBUG) {
107 struct sockaddr_in6 clientAddress {};
108 socklen_t len = sizeof(clientAddress);
109 getsockname(sctpParams.listenFD, (SA *)&clientAddress, &len);
111 inet_ntop(AF_INET6, &clientAddress.sin6_addr, buff, sizeof(buff));
112 mdclog_write(MDCLOG_DEBUG, "My address: %s, port %d\n", buff, htons(clientAddress.sin6_port));
115 if (listen(sctpParams.listenFD, SOMAXCONN) < 0) {
116 mdclog_write(MDCLOG_ERR, "Error listening. %s\n", strerror(errno));
119 struct epoll_event event {};
120 event.events = EPOLLIN | EPOLLET;
121 event.data.fd = sctpParams.listenFD;
123 // add listening port to epoll
124 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.listenFD, &event)) {
125 printf("Failed to add descriptor to epoll\n");
126 mdclog_write(MDCLOG_ERR, "Failed to add descriptor to epoll. %s\n", strerror(errno));
133 int buildConfiguration(sctp_params_t &sctpParams) {
134 path p = (sctpParams.configFilePath + "/" + sctpParams.configFileName).c_str();
136 const int size = 2048;
137 auto fileSize = file_size(p);
138 if (fileSize > size) {
139 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
143 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
148 if (conf.openConfigFile(p.string()) == -1) {
149 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
150 p.string().c_str(), strerror(errno));
153 int rmrPort = conf.getIntValue("nano");
155 mdclog_write(MDCLOG_ERR, "illegal RMR port ");
158 sctpParams.rmrPort = (uint16_t)rmrPort;
159 snprintf(sctpParams.rmrAddress, sizeof(sctpParams.rmrAddress), "%d", (int) (sctpParams.rmrPort));
161 auto tmpStr = conf.getStringValue("loglevel");
162 if (tmpStr.length() == 0) {
163 mdclog_write(MDCLOG_ERR, "illegal loglevel. Set loglevel to MDCLOG_INFO");
166 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
168 if ((tmpStr.compare("debug")) == 0) {
169 sctpParams.logLevel = MDCLOG_DEBUG;
170 } else if ((tmpStr.compare("info")) == 0) {
171 sctpParams.logLevel = MDCLOG_INFO;
172 } else if ((tmpStr.compare("warning")) == 0) {
173 sctpParams.logLevel = MDCLOG_WARN;
174 } else if ((tmpStr.compare("error")) == 0) {
175 sctpParams.logLevel = MDCLOG_ERR;
177 mdclog_write(MDCLOG_ERR, "illegal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
178 sctpParams.logLevel = MDCLOG_INFO;
180 mdclog_level_set(sctpParams.logLevel);
182 tmpStr = conf.getStringValue("volume");
183 if (tmpStr.length() == 0) {
184 mdclog_write(MDCLOG_ERR, "illegal volume.");
188 char tmpLogFilespec[VOLUME_URL_SIZE];
189 tmpLogFilespec[0] = 0;
190 sctpParams.volume[0] = 0;
191 snprintf(sctpParams.volume, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
192 // copy the name to temp file as well
193 snprintf(tmpLogFilespec, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
196 // define the file name in the tmp directory under the volume
197 strcat(tmpLogFilespec,"/tmp/E2Term_%Y-%m-%d_%H-%M-%S.%N.tmpStr");
199 sctpParams.myIP = conf.getStringValue("local-ip");
200 if (sctpParams.myIP.length() == 0) {
201 mdclog_write(MDCLOG_ERR, "illegal local-ip.");
205 int sctpPort = conf.getIntValue("sctp-port");
206 if (sctpPort == -1) {
207 mdclog_write(MDCLOG_ERR, "illegal SCTP port ");
210 sctpParams.sctpPort = (uint16_t)sctpPort;
212 sctpParams.fqdn = conf.getStringValue("external-fqdn");
213 if (sctpParams.fqdn.length() == 0) {
214 mdclog_write(MDCLOG_ERR, "illegal external-fqdn");
218 std::string pod = conf.getStringValue("pod_name");
219 if (pod.length() == 0) {
220 mdclog_write(MDCLOG_ERR, "illegal pod_name in config file");
223 auto *podName = getenv(pod.c_str());
224 if (podName == nullptr) {
225 mdclog_write(MDCLOG_ERR, "illegal pod_name or environment variable not exists : %s", pod.c_str());
229 sctpParams.podName.assign(podName);
230 if (sctpParams.podName.length() == 0) {
231 mdclog_write(MDCLOG_ERR, "illegal pod_name");
236 tmpStr = conf.getStringValue("trace");
237 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
238 if ((tmpStr.compare("start")) == 0) {
239 mdclog_write(MDCLOG_INFO, "Trace set to: start");
240 sctpParams.trace = true;
241 } else if ((tmpStr.compare("stop")) == 0) {
242 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
243 sctpParams.trace = false;
245 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
246 sctpParams.trace = false;
248 jsonTrace = sctpParams.trace;
250 sctpParams.epollTimeOut = -1;
252 tmpStr = conf.getStringValue("prometheusPort");
253 if (tmpStr.length() != 0) {
254 sctpParams.prometheusPort = tmpStr;
257 sctpParams.ka_message_length = snprintf(sctpParams.ka_message, KA_MESSAGE_SIZE, "{\"address\": \"%s:%d\","
259 "\"pod_name\": \"%s\"}",
260 (const char *)sctpParams.myIP.c_str(),
262 sctpParams.fqdn.c_str(),
263 sctpParams.podName.c_str());
265 if (mdclog_level_get() >= MDCLOG_INFO) {
266 mdclog_mdc_add("RMR Port", to_string(sctpParams.rmrPort).c_str());
267 mdclog_mdc_add("LogLevel", to_string(sctpParams.logLevel).c_str());
268 mdclog_mdc_add("volume", sctpParams.volume);
269 mdclog_mdc_add("tmpLogFilespec", tmpLogFilespec);
270 mdclog_mdc_add("my ip", sctpParams.myIP.c_str());
271 mdclog_mdc_add("pod name", sctpParams.podName.c_str());
273 mdclog_write(MDCLOG_INFO, "running parameters for instance : %s", sctpParams.ka_message);
277 // Files written to the current working directory
278 boostLogger = logging::add_file_log(
279 keywords::file_name = tmpLogFilespec, // to temp directory
280 keywords::rotation_size = 10 * 1024 * 1024,
281 keywords::time_based_rotation = sinks::file::rotation_at_time_interval(posix_time::hours(1)),
282 keywords::format = "%Message%"
283 //keywords::format = "[%TimeStamp%]: %Message%" // use each tmpStr with time stamp
286 // Setup a destination folder for collecting rotated (closed) files --since the same volume can use rename()
287 boostLogger->locked_backend()->set_file_collector(sinks::file::make_collector(
288 keywords::target = sctpParams.volume
291 // Upon restart, scan the directory for files matching the file_name pattern
292 boostLogger->locked_backend()->scan_for_files();
294 // Enable auto-flushing after each tmpStr record written
295 if (mdclog_level_get() >= MDCLOG_DEBUG) {
296 boostLogger->locked_backend()->auto_flush(true);
302 void startPrometheus(sctp_params_t &sctpParams) {
303 sctpParams.prometheusFamily = &BuildCounter()
305 .Help("E2T message counter")
306 .Labels({{"POD_NAME", sctpParams.podName}})
307 .Register(*sctpParams.prometheusRegistry);
309 string prometheusPath = sctpParams.prometheusPort + "," + "[::]:" + sctpParams.prometheusPort;
310 if (mdclog_level_get() >= MDCLOG_DEBUG) {
311 mdclog_write(MDCLOG_DEBUG, "Start Prometheus Pull mode on %s", prometheusPath.c_str());
313 sctpParams.prometheusExposer = new Exposer(prometheusPath, 1);
314 sctpParams.prometheusExposer->RegisterCollectable(sctpParams.prometheusRegistry);
317 int main(const int argc, char **argv) {
318 sctp_params_t sctpParams;
321 std::random_device device{};
322 std::mt19937 generator(device());
323 std::uniform_int_distribution<long> distribution(1, (long) 1e12);
324 transactionCounter = distribution(generator);
328 // uint32_t aux1 = 0;
329 // st = rdtscp(aux1);
331 unsigned num_cpus = std::thread::hardware_concurrency();
333 mdclog_level_set(MDCLOG_INFO);
335 if (std::signal(SIGINT, catch_function) == SIG_ERR) {
336 mdclog_write(MDCLOG_ERR, "Error initializing SIGINT");
339 if (std::signal(SIGABRT, catch_function)== SIG_ERR) {
340 mdclog_write(MDCLOG_ERR, "Error initializing SIGABRT");
343 if (std::signal(SIGTERM, catch_function)== SIG_ERR) {
344 mdclog_write(MDCLOG_ERR, "Error initializing SIGTERM");
348 cpuClock = approx_CPU_MHz(100);
350 mdclog_write(MDCLOG_DEBUG, "CPU speed %11.11f", cpuClock);
352 auto result = parse(argc, argv, sctpParams);
354 if (buildConfiguration(sctpParams) != 0) {
358 //auto registry = std::make_shared<Registry>();
359 sctpParams.prometheusRegistry = std::make_shared<Registry>();
361 //sctpParams.prometheusFamily = new Family<Counter>("E2T", "E2T message counter", {{"E", sctpParams.podName}});
363 startPrometheus(sctpParams);
366 sctpParams.epoll_fd = epoll_create1(0);
367 if (sctpParams.epoll_fd == -1) {
368 mdclog_write(MDCLOG_ERR, "failed to open epoll descriptor");
372 getRmrContext(sctpParams);
373 if (sctpParams.rmrCtx == nullptr) {
374 close(sctpParams.epoll_fd);
378 if (buildInotify(sctpParams) == -1) {
379 close(sctpParams.rmrListenFd);
380 rmr_close(sctpParams.rmrCtx);
381 close(sctpParams.epoll_fd);
385 if (buildListeningPort(sctpParams) != 0) {
386 close(sctpParams.rmrListenFd);
387 rmr_close(sctpParams.rmrCtx);
388 close(sctpParams.epoll_fd);
392 sctpParams.sctpMap = new mapWrapper();
394 std::vector<std::thread> threads(num_cpus);
395 // std::vector<std::thread> threads;
398 for (unsigned int i = 0; i < num_cpus; i++) {
399 threads[i] = std::thread(listener, &sctpParams);
404 int rc = pthread_setaffinity_np(threads[i].native_handle(), sizeof(cpu_set_t), &cpuset);
406 mdclog_write(MDCLOG_ERR, "Error calling pthread_setaffinity_np: %d", rc);
411 //loop over term_init until first message from xApp
412 handleTermInit(sctpParams);
414 for (auto &t : threads) {
421 void handleTermInit(sctp_params_t &sctpParams) {
422 sendTermInit(sctpParams);
423 //send to e2 manager init of e2 term
428 auto xappMessages = num_of_XAPP_messages.load(std::memory_order_acquire);
429 if (xappMessages > 0) {
430 if (mdclog_level_get() >= MDCLOG_INFO) {
431 mdclog_write(MDCLOG_INFO, "Got a message from some application, stop sending E2_TERM_INIT");
437 if (count % 1000 == 0) {
438 mdclog_write(MDCLOG_ERR, "GOT No messages from any xApp");
439 sendTermInit(sctpParams);
444 void sendTermInit(sctp_params_t &sctpParams) {
445 rmr_mbuf_t *msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
448 msg->mtype = E2_TERM_INIT;
450 rmr_bytes2payload(msg, (unsigned char *)sctpParams.ka_message, sctpParams.ka_message_length);
451 static unsigned char tx[32];
452 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
453 rmr_bytes2xact(msg, tx, txLen);
454 msg = rmr_send_msg(sctpParams.rmrCtx, msg);
455 if (msg == nullptr) {
456 msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
457 } else if (msg->state == 0) {
459 if (mdclog_level_get() >= MDCLOG_INFO) {
460 mdclog_write(MDCLOG_INFO, "E2_TERM_INIT successfully sent ");
464 if (count % 100 == 0) {
465 mdclog_write(MDCLOG_ERR, "Error sending E2_TERM_INIT cause : %s ", translateRmrErrorMessages(msg->state).c_str());
480 cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &sctpParams) {
481 cxxopts::Options options(argv[0], "e2 term help");
482 options.positional_help("[optional args]").show_positional_help();
483 options.allow_unrecognised_options().add_options()
484 ("p,path", "config file path", cxxopts::value<std::string>(sctpParams.configFilePath)->default_value("config"))
485 ("f,file", "config file name", cxxopts::value<std::string>(sctpParams.configFileName)->default_value("config.conf"))
486 ("h,help", "Print help");
488 auto result = options.parse(argc, (const char **&)argv);
490 if (result.count("help")) {
491 std::cout << options.help({""}) << std::endl;
500 * @return -1 failed 0 success
502 int buildInotify(sctp_params_t &sctpParams) {
503 sctpParams.inotifyFD = inotify_init1(IN_NONBLOCK);
504 if (sctpParams.inotifyFD == -1) {
505 mdclog_write(MDCLOG_ERR, "Failed to init inotify (inotify_init1) %s", strerror(errno));
509 sctpParams.inotifyWD = inotify_add_watch(sctpParams.inotifyFD,
510 (const char *)sctpParams.configFilePath.c_str(),
511 (unsigned)IN_OPEN | (unsigned)IN_CLOSE_WRITE | (unsigned)IN_CLOSE_NOWRITE); //IN_CLOSE = (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
512 if (sctpParams.inotifyWD == -1) {
513 mdclog_write(MDCLOG_ERR, "Failed to add directory : %s to inotify (inotify_add_watch) %s",
514 sctpParams.configFilePath.c_str(),
516 close(sctpParams.inotifyFD);
520 struct epoll_event event{};
521 event.events = (EPOLLIN);
522 event.data.fd = sctpParams.inotifyFD;
523 // add listening RMR FD to epoll
524 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.inotifyFD, &event)) {
525 mdclog_write(MDCLOG_ERR, "Failed to add inotify FD to epoll");
526 close(sctpParams.inotifyFD);
537 void listener(sctp_params_t *params) {
538 int num_of_SCTP_messages = 0;
539 auto totalTime = 0.0;
541 mdclog_level_set(params->logLevel);
543 std::thread::id this_id = std::this_thread::get_id();
545 streambuf *oldCout = cout.rdbuf();
546 ostringstream memCout;
548 cout.rdbuf(memCout.rdbuf());
550 //return to the normal cout
554 memcpy(tid, memCout.str().c_str(), memCout.str().length() < 32 ? memCout.str().length() : 31);
555 tid[memCout.str().length()] = 0;
556 mdclog_mdc_add("thread id", tid);
558 if (mdclog_level_get() >= MDCLOG_DEBUG) {
559 mdclog_write(MDCLOG_DEBUG, "started thread number %s", tid);
562 RmrMessagesBuffer_t rmrMessageBuffer{};
563 //create and init RMR
564 rmrMessageBuffer.rmrCtx = params->rmrCtx;
566 auto *events = (struct epoll_event *) calloc(MAXEVENTS, sizeof(struct epoll_event));
567 struct timespec end{0, 0};
568 struct timespec start{0, 0};
570 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
571 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
573 memcpy(rmrMessageBuffer.ka_message, params->ka_message, params->ka_message_length);
574 rmrMessageBuffer.ka_message_len = params->ka_message_length;
575 rmrMessageBuffer.ka_message[rmrMessageBuffer.ka_message_len] = 0;
577 if (mdclog_level_get() >= MDCLOG_DEBUG) {
578 mdclog_write(MDCLOG_DEBUG, "keep alive message is : %s", rmrMessageBuffer.ka_message);
581 ReportingMessages_t message {};
583 // for (int i = 0; i < MAX_RMR_BUFF_ARRAY; i++) {
584 // rmrMessageBuffer.rcvBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
585 // rmrMessageBuffer.sendBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
589 if (mdclog_level_get() >= MDCLOG_DEBUG) {
590 mdclog_write(MDCLOG_DEBUG, "Start EPOLL Wait. Timeout = %d", params->epollTimeOut);
592 auto numOfEvents = epoll_wait(params->epoll_fd, events, MAXEVENTS, params->epollTimeOut);
593 if (numOfEvents == 0) { // time out
594 if (mdclog_level_get() >= MDCLOG_DEBUG) {
595 mdclog_write(MDCLOG_DEBUG, "got epoll timeout");
598 } else if (numOfEvents < 0) {
599 if (errno == EINTR) {
600 if (mdclog_level_get() >= MDCLOG_DEBUG) {
601 mdclog_write(MDCLOG_DEBUG, "got EINTR : %s", strerror(errno));
605 mdclog_write(MDCLOG_ERR, "Epoll wait failed, errno = %s", strerror(errno));
608 for (auto i = 0; i < numOfEvents; i++) {
609 if (mdclog_level_get() >= MDCLOG_DEBUG) {
610 mdclog_write(MDCLOG_DEBUG, "handling epoll event %d out of %d", i + 1, numOfEvents);
612 clock_gettime(CLOCK_MONOTONIC, &message.message.time);
613 start.tv_sec = message.message.time.tv_sec;
614 start.tv_nsec = message.message.time.tv_nsec;
617 if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)) {
618 handlepoll_error(events[i], message, rmrMessageBuffer, params);
619 } else if (events[i].events & EPOLLOUT) {
620 handleEinprogressMessages(events[i], message, rmrMessageBuffer, params);
621 } else if (params->listenFD == events[i].data.fd) {
622 if (mdclog_level_get() >= MDCLOG_INFO) {
623 mdclog_write(MDCLOG_INFO, "New connection request from sctp network\n");
625 // new connection is requested from RAN start build connection
627 struct sockaddr in_addr {};
629 char hostBuff[NI_MAXHOST];
630 char portBuff[NI_MAXSERV];
632 in_len = sizeof(in_addr);
633 auto *peerInfo = (ConnectedCU_t *)calloc(1, sizeof(ConnectedCU_t));
634 if(peerInfo == nullptr){
635 mdclog_write(MDCLOG_ERR, "calloc failed");
638 peerInfo->sctpParams = params;
639 peerInfo->fileDescriptor = accept(params->listenFD, &in_addr, &in_len);
640 if (peerInfo->fileDescriptor == -1) {
641 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
642 /* We have processed all incoming connections. */
645 mdclog_write(MDCLOG_ERR, "Accept error, errno = %s", strerror(errno));
649 if (setSocketNoBlocking(peerInfo->fileDescriptor) == -1) {
650 mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on port %s\n", hostBuff, portBuff);
651 close(peerInfo->fileDescriptor);
654 auto ans = getnameinfo(&in_addr, in_len,
655 peerInfo->hostName, NI_MAXHOST,
656 peerInfo->portNumber, NI_MAXSERV, (unsigned )((unsigned int)NI_NUMERICHOST | (unsigned int)NI_NUMERICSERV));
658 mdclog_write(MDCLOG_ERR, "Failed to get info on connection request. %s\n", strerror(errno));
659 close(peerInfo->fileDescriptor);
662 if (mdclog_level_get() >= MDCLOG_DEBUG) {
663 mdclog_write(MDCLOG_DEBUG, "Accepted connection on descriptor %d (host=%s, port=%s)\n", peerInfo->fileDescriptor, peerInfo->hostName, peerInfo->portNumber);
665 peerInfo->isConnected = false;
666 peerInfo->gotSetup = false;
667 if (addToEpoll(params->epoll_fd,
670 params->sctpMap, nullptr,
676 } else if (params->rmrListenFd == events[i].data.fd) {
677 // got message from XAPP
678 //num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
679 num_of_messages.fetch_add(1, std::memory_order_release);
680 if (mdclog_level_get() >= MDCLOG_DEBUG) {
681 mdclog_write(MDCLOG_DEBUG, "new RMR message");
683 if (receiveXappMessages(params->sctpMap,
685 message.message.time) != 0) {
686 mdclog_write(MDCLOG_ERR, "Error handling Xapp message");
688 } else if (params->inotifyFD == events[i].data.fd) {
689 mdclog_write(MDCLOG_INFO, "Got event from inotify (configuration update)");
690 handleConfigChange(params);
692 /* We RMR_ERR_RETRY have data on the fd waiting to be read. Read and display it.
693 * We must read whatever data is available completely, as we are running
694 * in edge-triggered mode and won't get a notification again for the same data. */
695 num_of_messages.fetch_add(1, std::memory_order_release);
696 if (mdclog_level_get() >= MDCLOG_DEBUG) {
697 mdclog_write(MDCLOG_DEBUG, "new message from SCTP, epoll flags are : %0x", events[i].events);
699 receiveDataFromSctp(&events[i],
701 num_of_SCTP_messages,
703 message.message.time);
706 clock_gettime(CLOCK_MONOTONIC, &end);
707 if (mdclog_level_get() >= MDCLOG_INFO) {
708 totalTime += ((end.tv_sec + 1.0e-9 * end.tv_nsec) -
709 ((double) start.tv_sec + 1.0e-9 * start.tv_nsec));
711 if (mdclog_level_get() >= MDCLOG_DEBUG) {
712 mdclog_write(MDCLOG_DEBUG, "message handling is %ld seconds %ld nanoseconds",
713 end.tv_sec - start.tv_sec,
714 end.tv_nsec - start.tv_nsec);
724 void handleConfigChange(sctp_params_t *sctpParams) {
725 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
726 const struct inotify_event *event;
729 path p = (sctpParams->configFilePath + "/" + sctpParams->configFileName).c_str();
730 auto endlessLoop = true;
731 while (endlessLoop) {
732 auto len = read(sctpParams->inotifyFD, buf, sizeof buf);
734 if (errno != EAGAIN) {
735 mdclog_write(MDCLOG_ERR, "read %s ", strerror(errno));
745 for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) {
746 event = (const struct inotify_event *)ptr;
747 if (event->mask & (uint32_t)IN_ISDIR) {
751 // the directory name
752 if (sctpParams->inotifyWD == event->wd) {
756 auto retVal = strcmp(sctpParams->configFileName.c_str(), event->name);
761 // only the file we want
762 if (event->mask & (uint32_t)IN_CLOSE_WRITE) {
763 if (mdclog_level_get() >= MDCLOG_INFO) {
764 mdclog_write(MDCLOG_INFO, "Configuration file changed");
767 const int size = 2048;
768 auto fileSize = file_size(p);
769 if (fileSize > size) {
770 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
774 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
779 if (conf.openConfigFile(p.string()) == -1) {
780 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
781 p.string().c_str(), strerror(errno));
785 auto tmpStr = conf.getStringValue("loglevel");
786 if (tmpStr.length() == 0) {
787 mdclog_write(MDCLOG_ERR, "illegal loglevel. Set loglevel to MDCLOG_INFO");
790 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
792 if ((tmpStr.compare("debug")) == 0) {
793 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_DEBUG");
794 sctpParams->logLevel = MDCLOG_DEBUG;
795 } else if ((tmpStr.compare("info")) == 0) {
796 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_INFO");
797 sctpParams->logLevel = MDCLOG_INFO;
798 } else if ((tmpStr.compare("warning")) == 0) {
799 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_WARN");
800 sctpParams->logLevel = MDCLOG_WARN;
801 } else if ((tmpStr.compare("error")) == 0) {
802 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_ERR");
803 sctpParams->logLevel = MDCLOG_ERR;
805 mdclog_write(MDCLOG_ERR, "illegal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
806 sctpParams->logLevel = MDCLOG_INFO;
808 mdclog_level_set(sctpParams->logLevel);
811 tmpStr = conf.getStringValue("trace");
812 if (tmpStr.length() == 0) {
813 mdclog_write(MDCLOG_ERR, "illegal trace. Set trace to stop");
817 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
818 if ((tmpStr.compare("start")) == 0) {
819 mdclog_write(MDCLOG_INFO, "Trace set to: start");
820 sctpParams->trace = true;
821 } else if ((tmpStr.compare("stop")) == 0) {
822 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
823 sctpParams->trace = false;
825 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
826 sctpParams->trace = false;
828 jsonTrace = sctpParams->trace;
841 * @param rmrMessageBuffer
844 void handleEinprogressMessages(struct epoll_event &event,
845 ReportingMessages_t &message,
846 RmrMessagesBuffer_t &rmrMessageBuffer,
847 sctp_params_t *params) {
848 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
849 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
851 mdclog_write(MDCLOG_INFO, "file descriptor %d got EPOLLOUT", peerInfo->fileDescriptor);
853 socklen_t retValLen = 0;
854 auto rc = getsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_ERROR, &retVal, &retValLen);
855 if (rc != 0 || retVal != 0) {
857 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
858 "%s|Failed SCTP Connection, after EINPROGRESS the getsockopt%s",
859 peerInfo->enodbName, strerror(errno));
860 } else if (retVal != 0) {
861 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
862 "%s|Failed SCTP Connection after EINPROGRESS, SO_ERROR",
863 peerInfo->enodbName);
866 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
867 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
868 mdclog_write(MDCLOG_ERR, "%s", rmrMessageBuffer.sendMessage->payload);
869 message.message.direction = 'N';
870 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
871 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
873 memset(peerInfo->asnData, 0, peerInfo->asnLength);
874 peerInfo->asnLength = 0;
879 peerInfo->isConnected = true;
881 if (modifyToEpoll(params->epoll_fd, peerInfo, (EPOLLIN | EPOLLET), params->sctpMap, peerInfo->enodbName,
882 peerInfo->mtype) != 0) {
883 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_MOD");
887 message.message.asndata = (unsigned char *)peerInfo->asnData;
888 message.message.asnLength = peerInfo->asnLength;
889 message.message.messageType = peerInfo->mtype;
890 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
891 num_of_messages.fetch_add(1, std::memory_order_release);
892 if (mdclog_level_get() >= MDCLOG_DEBUG) {
893 mdclog_write(MDCLOG_DEBUG, "send the delayed SETUP/ENDC SETUP to sctp for %s",
894 message.message.enodbName);
896 if (sendSctpMsg(peerInfo, message, params->sctpMap) != 0) {
897 if (mdclog_level_get() >= MDCLOG_DEBUG) {
898 mdclog_write(MDCLOG_DEBUG, "Error write to SCTP %s %d", __func__, __LINE__);
903 memset(peerInfo->asnData, 0, peerInfo->asnLength);
904 peerInfo->asnLength = 0;
909 void handlepoll_error(struct epoll_event &event,
910 ReportingMessages_t &message,
911 RmrMessagesBuffer_t &rmrMessageBuffer,
912 sctp_params_t *params) {
913 if (event.data.fd != params->rmrListenFd) {
914 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
915 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on fd %d, RAN NAME : %s",
916 event.events, peerInfo->fileDescriptor, peerInfo->enodbName);
918 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
919 "%s|Failed SCTP Connection",
920 peerInfo->enodbName);
921 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
922 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
924 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
925 message.message.direction = 'N';
926 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
927 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
930 close(peerInfo->fileDescriptor);
931 params->sctpMap->erase(peerInfo->enodbName);
932 cleanHashEntry((ConnectedCU_t *) event.data.ptr, params->sctpMap);
934 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on RMR FD", event.events);
942 int setSocketNoBlocking(int socket) {
943 auto flags = fcntl(socket, F_GETFL, 0);
946 mdclog_mdc_add("func", "fcntl");
947 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
952 flags = (unsigned) flags | (unsigned) O_NONBLOCK;
953 if (fcntl(socket, F_SETFL, flags) == -1) {
954 mdclog_mdc_add("func", "fcntl");
955 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
968 void cleanHashEntry(ConnectedCU_t *val, Sctp_Map_t *m) {
970 auto port = (uint16_t) strtol(val->portNumber, &dummy, 10);
971 char searchBuff[2048]{};
973 snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", val->hostName, port);
974 m->erase(searchBuff);
976 m->erase(val->enodbName);
982 * @param fd file descriptor
983 * @param data the asn data to send
984 * @param len length of the data
985 * @param enodbName the enodbName as in the map for printing purpose
986 * @param m map host information
987 * @param mtype message number
988 * @return 0 success, a negative number on fail
990 int sendSctpMsg(ConnectedCU_t *peerInfo, ReportingMessages_t &message, Sctp_Map_t *m) {
991 auto loglevel = mdclog_level_get();
992 int fd = peerInfo->fileDescriptor;
993 if (loglevel >= MDCLOG_DEBUG) {
994 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for CU %s, %s",
995 message.message.enodbName, __FUNCTION__);
999 if (send(fd,message.message.asndata, message.message.asnLength,MSG_NOSIGNAL) < 0) {
1000 if (errno == EINTR) {
1003 mdclog_write(MDCLOG_ERR, "error writing to CU a message, %s ", strerror(errno));
1004 if (!peerInfo->isConnected) {
1005 mdclog_write(MDCLOG_ERR, "connection to CU %s is still in progress.", message.message.enodbName);
1008 cleanHashEntry(peerInfo, m);
1010 char key[MAX_ENODB_NAME_SIZE * 2];
1011 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName,
1012 message.message.messageType);
1013 if (loglevel >= MDCLOG_DEBUG) {
1014 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
1016 auto tmp = m->find(key);
1023 message.message.direction = 'D';
1024 // send report.buffer of size
1025 buildJsonMessage(message);
1027 if (loglevel >= MDCLOG_DEBUG) {
1028 mdclog_write(MDCLOG_DEBUG,
1029 "SCTP message for CU %s sent from %s",
1030 message.message.enodbName,
1040 * @param rmrMessageBuffer
1042 void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1043 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
1044 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
1046 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1047 mdclog_write(MDCLOG_DEBUG, "Message from Xapp RAN name = %s message length = %ld",
1048 message.message.enodbName, (unsigned long) message.message.asnLength);
1058 * @param numOfMessages
1059 * @param rmrMessageBuffer
1063 int receiveDataFromSctp(struct epoll_event *events,
1064 Sctp_Map_t *sctpMap,
1066 RmrMessagesBuffer_t &rmrMessageBuffer,
1067 struct timespec &ts) {
1068 /* We have data on the fd waiting to be read. Read and display it.
1069 * We must read whatever data is available completely, as we are running
1070 * in edge-triggered mode and won't get a notification again for the same data. */
1071 ReportingMessages_t message {};
1073 auto loglevel = mdclog_level_get();
1075 // get the identity of the interface
1076 message.peerInfo = (ConnectedCU_t *)events->data.ptr;
1078 struct timespec start{0, 0};
1079 struct timespec decodeStart{0, 0};
1080 struct timespec end{0, 0};
1082 E2AP_PDU_t *pdu = nullptr;
1085 if (loglevel >= MDCLOG_DEBUG) {
1086 mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP %d fd", message.peerInfo->fileDescriptor);
1087 clock_gettime(CLOCK_MONOTONIC, &start);
1089 // read the buffer directly to rmr payload
1090 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1091 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1092 read(message.peerInfo->fileDescriptor, rmrMessageBuffer.sendMessage->payload, RECEIVE_SCTP_BUFFER_SIZE);
1094 if (loglevel >= MDCLOG_DEBUG) {
1095 mdclog_write(MDCLOG_DEBUG, "Finish Read from SCTP %d fd message length = %ld",
1096 message.peerInfo->fileDescriptor, message.message.asnLength);
1099 memcpy(message.message.enodbName, message.peerInfo->enodbName, sizeof(message.peerInfo->enodbName));
1100 message.message.direction = 'U';
1101 message.message.time.tv_nsec = ts.tv_nsec;
1102 message.message.time.tv_sec = ts.tv_sec;
1104 if (message.message.asnLength < 0) {
1105 if (errno == EINTR) {
1108 /* If errno == EAGAIN, that means we have read all
1109 data. So goReportingMessages_t back to the main loop. */
1110 if (errno != EAGAIN) {
1111 mdclog_write(MDCLOG_ERR, "Read error, %s ", strerror(errno));
1113 } else if (loglevel >= MDCLOG_DEBUG) {
1114 mdclog_write(MDCLOG_DEBUG, "EAGAIN - descriptor = %d", message.peerInfo->fileDescriptor);
1117 } else if (message.message.asnLength == 0) {
1118 /* End of file. The remote has closed the connection. */
1119 if (loglevel >= MDCLOG_INFO) {
1120 mdclog_write(MDCLOG_INFO, "END of File Closed connection - descriptor = %d",
1121 message.peerInfo->fileDescriptor);
1127 if (loglevel >= MDCLOG_DEBUG) {
1128 char printBuffer[RECEIVE_SCTP_BUFFER_SIZE]{};
1129 char *tmp = printBuffer;
1130 for (size_t i = 0; i < (size_t)message.message.asnLength; ++i) {
1131 snprintf(tmp, 3, "%02x", message.message.asndata[i]);
1134 printBuffer[message.message.asnLength] = 0;
1135 clock_gettime(CLOCK_MONOTONIC, &end);
1136 mdclog_write(MDCLOG_DEBUG, "Before Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds",
1137 message.peerInfo->enodbName, end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1138 mdclog_write(MDCLOG_DEBUG, "PDU buffer length = %ld, data = : %s", message.message.asnLength,
1140 clock_gettime(CLOCK_MONOTONIC, &decodeStart);
1143 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1144 message.message.asndata, message.message.asnLength);
1145 if (rval.code != RC_OK) {
1146 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from RAN : %s", rval.code,
1147 message.peerInfo->enodbName);
1151 if (loglevel >= MDCLOG_DEBUG) {
1152 clock_gettime(CLOCK_MONOTONIC, &end);
1153 mdclog_write(MDCLOG_DEBUG, "After Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds",
1154 message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
1157 FILE *stream = open_memstream(&printBuffer, &size);
1158 asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu);
1159 mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer);
1160 clock_gettime(CLOCK_MONOTONIC, &decodeStart);
1166 switch (pdu->present) {
1167 case E2AP_PDU_PR_initiatingMessage: {//initiating message
1168 asnInitiatingRequest(pdu, sctpMap,message, rmrMessageBuffer);
1171 case E2AP_PDU_PR_successfulOutcome: { //successful outcome
1172 asnSuccessfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
1175 case E2AP_PDU_PR_unsuccessfulOutcome: { //Unsuccessful Outcome
1176 asnUnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
1180 mdclog_write(MDCLOG_ERR, "Unknown index %d in E2AP PDU", pdu->present);
1183 if (loglevel >= MDCLOG_DEBUG) {
1184 clock_gettime(CLOCK_MONOTONIC, &end);
1185 mdclog_write(MDCLOG_DEBUG,
1186 "After processing message and sent to rmr for : %s, Read time is : %ld seconds, %ld nanoseconds",
1187 message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
1190 if (pdu != nullptr) {
1191 ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
1192 //ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1198 if (loglevel >= MDCLOG_INFO) {
1199 mdclog_write(MDCLOG_INFO, "Closed connection - descriptor = %d", message.peerInfo->fileDescriptor);
1201 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1202 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
1204 "%s|CU disconnected unexpectedly",
1205 message.peerInfo->enodbName);
1206 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1208 if (sendRequestToXapp(message,
1209 RIC_SCTP_CONNECTION_FAILURE,
1210 rmrMessageBuffer) != 0) {
1211 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1214 /* Closing descriptor make epoll remove it from the set of descriptors which are monitored. */
1215 close(message.peerInfo->fileDescriptor);
1216 cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
1218 if (loglevel >= MDCLOG_DEBUG) {
1219 clock_gettime(CLOCK_MONOTONIC, &end);
1220 mdclog_write(MDCLOG_DEBUG, "from receive SCTP to send RMR time is %ld seconds and %ld nanoseconds",
1221 end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1227 static void buildAndSendSetupRequest(ReportingMessages_t &message,
1228 RmrMessagesBuffer_t &rmrMessageBuffer,
1230 string const &messageName,
1231 string const &ieName,
1232 vector<string> &functionsToAdd_v,
1233 vector<string> &functionsToModified_v*/) {
1234 auto logLevel = mdclog_level_get();
1235 // now we can send the data to e2Mgr
1238 auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1239 unsigned char *buffer = nullptr;
1240 buffer = (unsigned char *) calloc(buffer_size, sizeof(unsigned char));
1243 mdclog_write(MDCLOG_ERR, "Allocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1247 er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size);
1248 if (er.encoded == -1) {
1249 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1251 } else if (er.encoded > (ssize_t) buffer_size) {
1252 buffer_size = er.encoded + 128;
1253 mdclog_write(MDCLOG_WARN, "Buffer of size %d is to small for %s. Reallocate buffer of size %d",
1255 asn_DEF_E2AP_PDU.name, buffer_size);
1256 buffer_size = er.encoded + 128;
1258 unsigned char *newBuffer = nullptr;
1259 newBuffer = (unsigned char *) realloc(buffer, buffer_size);
1263 mdclog_write(MDCLOG_ERR, "Reallocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1270 buffer[er.encoded] = '\0';
1275 string res((char *)buffer);
1276 res.erase(std::remove(res.begin(), res.end(), '\n'), res.end());
1277 res.erase(std::remove(res.begin(), res.end(), '\t'), res.end());
1278 res.erase(std::remove(res.begin(), res.end(), ' '), res.end());
1281 // if (!functionsToAdd_v.empty() || !functionsToModified_v.empty()) {
1282 // res = buildXmlData(messageName, ieName, functionsToAdd_v, functionsToModified_v, buffer, (size_t) er.encoded);
1285 // if (res.length() == 0) {
1286 // rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size + 256);
1287 // rmrMsg->len = snprintf((char *) rmrMsg->payload, RECEIVE_SCTP_BUFFER_SIZE * 2, "%s:%d|%s",
1288 // message.peerInfo->sctpParams->myIP.c_str(),
1289 // message.peerInfo->sctpParams->rmrPort,
1292 rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, (int)res.length() + 256);
1293 rmrMsg->len = snprintf((char *) rmrMsg->payload, res.length() + 256, "%s:%d|%s",
1294 message.peerInfo->sctpParams->myIP.c_str(),
1295 message.peerInfo->sctpParams->rmrPort,
1299 if (logLevel >= MDCLOG_DEBUG) {
1300 mdclog_write(MDCLOG_DEBUG, "Setup request of size %d :\n %s\n", rmrMsg->len, rmrMsg->payload);
1303 rmrMsg->mtype = message.message.messageType;
1305 rmr_bytes2meid(rmrMsg, (unsigned char *) message.message.enodbName, strlen(message.message.enodbName));
1307 static unsigned char tx[32];
1308 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1309 rmr_bytes2xact(rmrMsg, tx, strlen((const char *) tx));
1311 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1312 if (rmrMsg == nullptr) {
1313 mdclog_write(MDCLOG_ERR, "RMR failed to send returned nullptr");
1314 } else if (rmrMsg->state != 0) {
1315 char meid[RMR_MAX_MEID]{};
1316 if (rmrMsg->state == RMR_ERR_RETRY) {
1319 mdclog_write(MDCLOG_INFO, "RETRY sending Message %d to Xapp from %s",
1320 rmrMsg->mtype, rmr_get_meid(rmrMsg, (unsigned char *) meid));
1321 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1322 if (rmrMsg == nullptr) {
1323 mdclog_write(MDCLOG_ERR, "RMR failed send returned nullptr");
1324 } else if (rmrMsg->state != 0) {
1325 mdclog_write(MDCLOG_ERR,
1326 "RMR Retry failed %s sending request %d to Xapp from %s",
1327 translateRmrErrorMessages(rmrMsg->state).c_str(),
1329 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1332 mdclog_write(MDCLOG_ERR, "RMR failed: %s. sending request %d to Xapp from %s",
1333 translateRmrErrorMessages(rmrMsg->state).c_str(),
1335 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1338 message.peerInfo->gotSetup = true;
1339 buildJsonMessage(message);
1341 if (rmrMsg != nullptr) {
1342 rmr_free_msg(rmrMsg);
1350 int RAN_Function_list_To_Vector(RANfunctions_List_t& list, vector <string> &runFunXML_v) {
1352 runFunXML_v.clear();
1353 for (auto j = 0; j < list.list.count; j++) {
1354 auto *raNfunctionItemIEs = (RANfunction_ItemIEs_t *)list.list.array[j];
1355 if (raNfunctionItemIEs->id == ProtocolIE_ID_id_RANfunction_Item &&
1356 (raNfunctionItemIEs->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item)) {
1358 E2SM_gNB_NRT_RANfunction_Definition_t *ranFunDef = nullptr;
1359 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER,
1360 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1361 (void **)&ranFunDef,
1362 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.buf,
1363 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.size);
1364 if (rval.code != RC_OK) {
1365 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2SM message from : %s",
1367 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name);
1371 auto xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1372 unsigned char xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
1373 memset(xml_buffer, 0, RECEIVE_SCTP_BUFFER_SIZE * 2);
1375 auto er = asn_encode_to_buffer(nullptr,
1377 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1381 if (er.encoded == -1) {
1382 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s",
1383 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1385 } else if (er.encoded > (ssize_t)xml_buffer_size) {
1386 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1387 (int) xml_buffer_size,
1388 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name, __func__, __LINE__);
1390 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1391 mdclog_write(MDCLOG_DEBUG, "Encoding E2SM %s PDU number %d : %s",
1392 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1397 string runFuncs = (char *)(xml_buffer);
1398 runFunXML_v.emplace_back(runFuncs);
1405 int collectServiceUpdate_RequestData(E2AP_PDU_t *pdu,
1406 Sctp_Map_t *sctpMap,
1407 ReportingMessages_t &message,
1408 vector <string> &RANfunctionsAdded_v,
1409 vector <string> &RANfunctionsModified_v) {
1410 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1411 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.count; i++) {
1412 auto *ie = pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.array[i];
1413 if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
1414 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctionsID_List) {
1415 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1416 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1417 ie->value.choice.RANfunctions_List.list.count);
1419 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1423 } else if (ie->id == ProtocolIE_ID_id_RANfunctionsModified) {
1424 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctions_List) {
1425 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1426 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1427 ie->value.choice.RANfunctions_List.list.count);
1429 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsModified_v) != 0 ) {
1435 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1436 mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
1437 RANfunctionsAdded_v.size());
1445 void buildPrometheusList(ConnectedCU_t *peerInfo, Family<Counter> *prometheusFamily) {
1446 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Messages"}});
1447 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Bytes"}});
1449 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Messages"}});
1450 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Bytes"}});
1452 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Messages"}});
1453 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Bytes"}});
1455 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Messages"}});
1456 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Bytes"}});
1458 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Messages"}});
1459 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Bytes"}});
1460 // ---------------------------------------------
1461 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Messages"}});
1462 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Bytes"}});
1464 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Messages"}});
1465 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Bytes"}});
1467 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Messages"}});
1468 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Bytes"}});
1470 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Messages"}});
1471 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Bytes"}});
1472 //-------------------------------------------------------------
1474 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Messages"}});
1475 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Bytes"}});
1477 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Messages"}});
1478 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Bytes"}});
1480 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Messages"}});
1481 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Bytes"}});
1483 //====================================================================================
1484 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Messages"}});
1485 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Bytes"}});
1487 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Messages"}});
1488 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Bytes"}});
1490 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Messages"}});
1491 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Bytes"}});
1493 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Messages"}});
1494 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Bytes"}});
1496 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Messages"}});
1497 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Bytes"}});
1499 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Messages"}});
1500 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Bytes"}});
1501 //---------------------------------------------------------------------------------------------------------
1502 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Messages"}});
1503 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Bytes"}});
1505 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Messages"}});
1506 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Bytes"}});
1508 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Messages"}});
1509 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Bytes"}});
1510 //----------------------------------------------------------------------------------------------------------------
1511 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Messages"}});
1512 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Bytes"}});
1514 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Messages"}});
1515 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Bytes"}});
1522 * @param RANfunctionsAdded_v
1525 int collectSetupRequestData(E2AP_PDU_t *pdu,
1526 Sctp_Map_t *sctpMap,
1527 ReportingMessages_t &message /*, vector <string> &RANfunctionsAdded_v*/) {
1528 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1529 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
1530 auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i];
1531 if (ie->id == ProtocolIE_ID_id_GlobalE2node_ID) {
1532 // get the ran name for meid
1533 if (ie->value.present == E2setupRequestIEs__value_PR_GlobalE2node_ID) {
1534 if (buildRanName(message.peerInfo->enodbName, ie) < 0) {
1535 mdclog_write(MDCLOG_ERR, "Bad param in E2setupRequestIEs GlobalE2node_ID.\n");
1536 // no message will be sent
1540 memcpy(message.message.enodbName, message.peerInfo->enodbName, strlen(message.peerInfo->enodbName));
1541 sctpMap->setkey(message.message.enodbName, message.peerInfo);
1543 } /*else if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
1544 if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
1545 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1546 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1547 ie->value.choice.RANfunctions_List.list.count);
1549 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1555 // if (mdclog_level_get() >= MDCLOG_DEBUG) {
1556 // mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
1557 // RANfunctionsAdded_v.size());
1562 int XML_From_PER(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1563 E2AP_PDU_t *pdu = nullptr;
1565 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1566 mdclog_write(MDCLOG_DEBUG, "got PER message of size %d is:%s",
1567 rmrMessageBuffer.sendMessage->len, rmrMessageBuffer.sendMessage->payload);
1569 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1570 rmrMessageBuffer.sendMessage->payload, rmrMessageBuffer.sendMessage->len);
1571 if (rval.code != RC_OK) {
1572 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
1574 message.message.enodbName);
1578 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
1579 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu,
1580 rmrMessageBuffer.sendMessage->payload, buff_size);
1581 if (er.encoded == -1) {
1582 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1584 } else if (er.encoded > (ssize_t)buff_size) {
1585 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1586 (int)rmrMessageBuffer.sendMessage->len,
1587 asn_DEF_E2AP_PDU.name,
1592 rmrMessageBuffer.sendMessage->len = er.encoded;
1601 * @param rmrMessageBuffer
1603 void asnInitiatingRequest(E2AP_PDU_t *pdu,
1604 Sctp_Map_t *sctpMap,
1605 ReportingMessages_t &message,
1606 RmrMessagesBuffer_t &rmrMessageBuffer) {
1607 auto logLevel = mdclog_level_get();
1608 auto procedureCode = ((InitiatingMessage_t *) pdu->choice.initiatingMessage)->procedureCode;
1609 if (logLevel >= MDCLOG_DEBUG) {
1610 mdclog_write(MDCLOG_DEBUG, "Initiating message %ld\n", procedureCode);
1612 switch (procedureCode) {
1613 case ProcedureCode_id_E2setup: {
1614 if (logLevel >= MDCLOG_DEBUG) {
1615 mdclog_write(MDCLOG_DEBUG, "Got E2setup");
1618 // vector <string> RANfunctionsAdded_v;
1619 // vector <string> RANfunctionsModified_v;
1620 // RANfunctionsAdded_v.clear();
1621 // RANfunctionsModified_v.clear();
1622 if (collectSetupRequestData(pdu, sctpMap, message) != 0) {
1626 buildPrometheusList(message.peerInfo, message.peerInfo->sctpParams->prometheusFamily);
1628 string messageName("E2setupRequest");
1629 string ieName("E2setupRequestIEs");
1630 message.message.messageType = RIC_E2_SETUP_REQ;
1631 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
1632 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment((double)message.message.asnLength);
1633 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
1636 case ProcedureCode_id_RICserviceUpdate: {
1637 if (logLevel >= MDCLOG_DEBUG) {
1638 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1640 // vector <string> RANfunctionsAdded_v;
1641 // vector <string> RANfunctionsModified_v;
1642 // RANfunctionsAdded_v.clear();
1643 // RANfunctionsModified_v.clear();
1644 // if (collectServiceUpdate_RequestData(pdu, sctpMap, message,
1645 // RANfunctionsAdded_v, RANfunctionsModified_v) != 0) {
1649 string messageName("RICserviceUpdate");
1650 string ieName("RICserviceUpdateIEs");
1651 message.message.messageType = RIC_SERVICE_UPDATE;
1652 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
1653 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment((double)message.message.asnLength);
1655 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
1658 case ProcedureCode_id_ErrorIndication: {
1659 if (logLevel >= MDCLOG_DEBUG) {
1660 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
1662 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
1663 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment((double)message.message.asnLength);
1664 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
1665 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
1669 case ProcedureCode_id_Reset: {
1670 if (logLevel >= MDCLOG_DEBUG) {
1671 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
1674 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
1675 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
1676 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
1680 if (sendRequestToXapp(message, RIC_E2_RESET_REQ, rmrMessageBuffer) != 0) {
1681 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_REQ message failed to send to xAPP");
1685 case ProcedureCode_id_RICindication: {
1686 if (logLevel >= MDCLOG_DEBUG) {
1687 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1689 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
1690 auto messageSent = false;
1691 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
1692 if (logLevel >= MDCLOG_DEBUG) {
1693 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1695 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1696 if (logLevel >= MDCLOG_DEBUG) {
1697 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1699 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1700 static unsigned char tx[32];
1701 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1702 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1703 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
1704 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
1705 (unsigned char *)message.message.enodbName,
1706 strlen(message.message.enodbName));
1707 rmrMessageBuffer.sendMessage->state = 0;
1708 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
1710 //ie->value.choice.RICrequestID.ricInstanceID;
1711 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1712 mdclog_write(MDCLOG_DEBUG, "sub id = %d, mtype = %d, ric instance id %ld, requestor id = %ld",
1713 rmrMessageBuffer.sendMessage->sub_id,
1714 rmrMessageBuffer.sendMessage->mtype,
1715 ie->value.choice.RICrequestID.ricInstanceID,
1716 ie->value.choice.RICrequestID.ricRequestorID);
1718 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICindication]->Increment();
1719 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICindication]->Increment((double)message.message.asnLength);
1720 sendRmrMessage(rmrMessageBuffer, message);
1723 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
1733 mdclog_write(MDCLOG_ERR, "Undefined or not supported message = %ld", procedureCode);
1734 message.message.messageType = 0; // no RMR message type yet
1736 buildJsonMessage(message);
1747 * @param rmrMessageBuffer
1749 void asnSuccessfulMsg(E2AP_PDU_t *pdu,
1750 Sctp_Map_t *sctpMap,
1751 ReportingMessages_t &message,
1752 RmrMessagesBuffer_t &rmrMessageBuffer) {
1753 auto procedureCode = pdu->choice.successfulOutcome->procedureCode;
1754 auto logLevel = mdclog_level_get();
1755 if (logLevel >= MDCLOG_INFO) {
1756 mdclog_write(MDCLOG_INFO, "Successful Outcome %ld", procedureCode);
1758 switch (procedureCode) {
1759 case ProcedureCode_id_Reset: {
1760 if (logLevel >= MDCLOG_DEBUG) {
1761 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
1763 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
1764 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
1765 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
1768 if (sendRequestToXapp(message, RIC_E2_RESET_RESP, rmrMessageBuffer) != 0) {
1769 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_RESP message failed to send to xAPP");
1773 case ProcedureCode_id_RICcontrol: {
1774 if (logLevel >= MDCLOG_DEBUG) {
1775 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
1778 i < pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.count; i++) {
1779 auto messageSent = false;
1780 RICcontrolAcknowledge_IEs_t *ie = pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.array[i];
1781 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1782 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1784 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1785 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1786 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1788 if (ie->value.present == RICcontrolAcknowledge_IEs__value_PR_RICrequestID) {
1789 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_ACK;
1790 rmrMessageBuffer.sendMessage->state = 0;
1791 // rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1792 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
1794 static unsigned char tx[32];
1795 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1796 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
1797 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
1798 (unsigned char *)message.message.enodbName,
1799 strlen(message.message.enodbName));
1801 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
1802 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
1803 sendRmrMessage(rmrMessageBuffer, message);
1806 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
1816 case ProcedureCode_id_RICsubscription: {
1817 if (logLevel >= MDCLOG_DEBUG) {
1818 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1820 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
1821 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
1822 if (sendRequestToXapp(message, RIC_SUB_RESP, rmrMessageBuffer) != 0) {
1823 mdclog_write(MDCLOG_ERR, "Subscription successful message failed to send to xAPP");
1827 case ProcedureCode_id_RICsubscriptionDelete: {
1828 if (logLevel >= MDCLOG_DEBUG) {
1829 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1831 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
1832 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
1833 if (sendRequestToXapp(message, RIC_SUB_DEL_RESP, rmrMessageBuffer) != 0) {
1834 mdclog_write(MDCLOG_ERR, "Subscription delete successful message failed to send to xAPP");
1839 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1840 message.message.messageType = 0; // no RMR message type yet
1841 buildJsonMessage(message);
1852 * @param rmrMessageBuffer
1854 void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
1855 Sctp_Map_t *sctpMap,
1856 ReportingMessages_t &message,
1857 RmrMessagesBuffer_t &rmrMessageBuffer) {
1858 auto procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
1859 auto logLevel = mdclog_level_get();
1860 if (logLevel >= MDCLOG_INFO) {
1861 mdclog_write(MDCLOG_INFO, "Unsuccessful Outcome %ld", procedureCode);
1863 switch (procedureCode) {
1864 case ProcedureCode_id_RICcontrol: {
1865 if (logLevel >= MDCLOG_DEBUG) {
1866 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
1869 i < pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.count; i++) {
1870 auto messageSent = false;
1871 RICcontrolFailure_IEs_t *ie = pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.array[i];
1872 if (logLevel >= MDCLOG_DEBUG) {
1873 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1875 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1876 if (logLevel >= MDCLOG_DEBUG) {
1877 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1879 if (ie->value.present == RICcontrolFailure_IEs__value_PR_RICrequestID) {
1880 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_FAILURE;
1881 rmrMessageBuffer.sendMessage->state = 0;
1882 // rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricRequestorID;
1883 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
1884 static unsigned char tx[32];
1885 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1886 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
1887 rmr_bytes2meid(rmrMessageBuffer.sendMessage, (unsigned char *) message.message.enodbName,
1888 strlen(message.message.enodbName));
1889 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
1890 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
1891 sendRmrMessage(rmrMessageBuffer, message);
1894 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
1903 case ProcedureCode_id_RICsubscription: {
1904 if (logLevel >= MDCLOG_DEBUG) {
1905 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1907 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
1908 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
1909 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
1910 mdclog_write(MDCLOG_ERR, "Subscription unsuccessful message failed to send to xAPP");
1914 case ProcedureCode_id_RICsubscriptionDelete: {
1915 if (logLevel >= MDCLOG_DEBUG) {
1916 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1918 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
1919 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
1920 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
1921 mdclog_write(MDCLOG_ERR, "Subscription Delete unsuccessful message failed to send to xAPP");
1926 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1927 message.message.messageType = 0; // no RMR message type yet
1929 buildJsonMessage(message);
1940 * @param rmrMmessageBuffer
1943 int sendRequestToXapp(ReportingMessages_t &message,
1945 RmrMessagesBuffer_t &rmrMmessageBuffer) {
1946 rmr_bytes2meid(rmrMmessageBuffer.sendMessage,
1947 (unsigned char *)message.message.enodbName,
1948 strlen(message.message.enodbName));
1949 message.message.messageType = rmrMmessageBuffer.sendMessage->mtype = requestId;
1950 rmrMmessageBuffer.sendMessage->state = 0;
1951 static unsigned char tx[32];
1952 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1953 rmr_bytes2xact(rmrMmessageBuffer.sendMessage, tx, strlen((const char *) tx));
1955 auto rc = sendRmrMessage(rmrMmessageBuffer, message);
1961 * @param pSctpParams
1963 void getRmrContext(sctp_params_t &pSctpParams) {
1964 pSctpParams.rmrCtx = nullptr;
1965 pSctpParams.rmrCtx = rmr_init(pSctpParams.rmrAddress, RECEIVE_XAPP_BUFFER_SIZE, RMRFL_NONE);
1966 if (pSctpParams.rmrCtx == nullptr) {
1967 mdclog_write(MDCLOG_ERR, "Failed to initialize RMR");
1971 rmr_set_stimeout(pSctpParams.rmrCtx, 0); // disable retries for any send operation
1972 // we need to find that routing table exist and we can run
1973 if (mdclog_level_get() >= MDCLOG_INFO) {
1974 mdclog_write(MDCLOG_INFO, "We are after RMR INIT wait for RMR_Ready");
1979 if ((rmrReady = rmr_ready(pSctpParams.rmrCtx)) == 0) {
1983 if (count % 60 == 0) {
1984 mdclog_write(MDCLOG_INFO, "waiting to RMR ready state for %d seconds", count);
1987 if (mdclog_level_get() >= MDCLOG_INFO) {
1988 mdclog_write(MDCLOG_INFO, "RMR running");
1990 rmr_init_trace(pSctpParams.rmrCtx, 200);
1991 // get the RMR fd for the epoll
1992 pSctpParams.rmrListenFd = rmr_get_rcvfd(pSctpParams.rmrCtx);
1993 struct epoll_event event{};
1994 // add RMR fd to epoll
1995 event.events = (EPOLLIN);
1996 event.data.fd = pSctpParams.rmrListenFd;
1997 // add listening RMR FD to epoll
1998 if (epoll_ctl(pSctpParams.epoll_fd, EPOLL_CTL_ADD, pSctpParams.rmrListenFd, &event)) {
1999 mdclog_write(MDCLOG_ERR, "Failed to add RMR descriptor to epoll");
2000 close(pSctpParams.rmrListenFd);
2001 rmr_close(pSctpParams.rmrCtx);
2002 pSctpParams.rmrCtx = nullptr;
2009 * @param rmrMessageBuffer
2012 int PER_FromXML(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
2013 E2AP_PDU_t *pdu = nullptr;
2015 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2016 mdclog_write(MDCLOG_DEBUG, "got xml Format data from xApp of size %d is:%s",
2017 rmrMessageBuffer.rcvMessage->len, rmrMessageBuffer.rcvMessage->payload);
2019 auto rval = asn_decode(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, (void **) &pdu,
2020 rmrMessageBuffer.rcvMessage->payload, rmrMessageBuffer.rcvMessage->len);
2021 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2022 mdclog_write(MDCLOG_DEBUG, "%s After decoding the XML to PDU", __func__ );
2024 if (rval.code != RC_OK) {
2025 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
2027 message.message.enodbName);
2031 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
2032 auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu,
2033 rmrMessageBuffer.rcvMessage->payload, buff_size);
2034 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2035 mdclog_write(MDCLOG_DEBUG, "%s After encoding PDU to PER", __func__ );
2037 if (er.encoded == -1) {
2038 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
2040 } else if (er.encoded > (ssize_t)buff_size) {
2041 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
2042 (int)rmrMessageBuffer.rcvMessage->len,
2043 asn_DEF_E2AP_PDU.name,
2048 rmrMessageBuffer.rcvMessage->len = er.encoded;
2055 * @param rmrMessageBuffer
2059 int receiveXappMessages(Sctp_Map_t *sctpMap,
2060 RmrMessagesBuffer_t &rmrMessageBuffer,
2061 struct timespec &ts) {
2062 int loglevel = mdclog_level_get();
2063 if (rmrMessageBuffer.rcvMessage == nullptr) {
2065 mdclog_write(MDCLOG_ERR, "RMR Allocation message, %s", strerror(errno));
2069 // if (loglevel >= MDCLOG_DEBUG) {
2070 // mdclog_write(MDCLOG_DEBUG, "Call to rmr_rcv_msg");
2072 rmrMessageBuffer.rcvMessage = rmr_rcv_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2073 if (rmrMessageBuffer.rcvMessage == nullptr) {
2074 mdclog_write(MDCLOG_ERR, "RMR Receiving message with null pointer, Reallocated rmr message buffer");
2075 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2078 ReportingMessages_t message;
2079 message.message.direction = 'D';
2080 message.message.time.tv_nsec = ts.tv_nsec;
2081 message.message.time.tv_sec = ts.tv_sec;
2083 // get message payload
2084 //auto msgData = msg->payload;
2085 if (rmrMessageBuffer.rcvMessage->state != 0) {
2086 mdclog_write(MDCLOG_ERR, "RMR Receiving message with stat = %d", rmrMessageBuffer.rcvMessage->state);
2089 rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *)message.message.enodbName);
2090 message.peerInfo = (ConnectedCU_t *) sctpMap->find(message.message.enodbName);
2091 if (message.peerInfo == nullptr) {
2092 auto type = rmrMessageBuffer.rcvMessage->mtype;
2094 case RIC_SCTP_CLEAR_ALL:
2095 case E2_TERM_KEEP_ALIVE_REQ:
2096 case RIC_HEALTH_CHECK_REQ:
2099 mdclog_write(MDCLOG_ERR, "Failed to send message no CU entry %s", message.message.enodbName);
2104 if (rmrMessageBuffer.rcvMessage->mtype != RIC_HEALTH_CHECK_REQ) {
2105 num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
2108 switch (rmrMessageBuffer.rcvMessage->mtype) {
2109 case RIC_E2_SETUP_RESP : {
2110 if (loglevel >= MDCLOG_DEBUG) {
2111 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_RESP");
2113 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2116 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2117 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
2118 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2119 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_RESP");
2124 case RIC_E2_SETUP_FAILURE : {
2125 if (loglevel >= MDCLOG_DEBUG) {
2126 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_FAILURE");
2128 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2131 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2132 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
2133 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2134 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_FAILURE");
2139 case RIC_ERROR_INDICATION: {
2140 if (loglevel >= MDCLOG_DEBUG) {
2141 mdclog_write(MDCLOG_DEBUG, "RIC_ERROR_INDICATION");
2143 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2144 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment(rmrMessageBuffer.rcvMessage->len);
2145 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2146 mdclog_write(MDCLOG_ERR, "Failed to send RIC_ERROR_INDICATION");
2152 if (loglevel >= MDCLOG_DEBUG) {
2153 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_REQ");
2155 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2156 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment(rmrMessageBuffer.rcvMessage->len);
2157 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2158 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_REQ");
2163 case RIC_SUB_DEL_REQ: {
2164 if (loglevel >= MDCLOG_DEBUG) {
2165 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_DEL_REQ");
2167 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2168 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment(rmrMessageBuffer.rcvMessage->len);
2169 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2170 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_DEL_REQ");
2175 case RIC_CONTROL_REQ: {
2176 if (loglevel >= MDCLOG_DEBUG) {
2177 mdclog_write(MDCLOG_DEBUG, "RIC_CONTROL_REQ");
2179 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2180 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment(rmrMessageBuffer.rcvMessage->len);
2181 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2182 mdclog_write(MDCLOG_ERR, "Failed to send RIC_CONTROL_REQ");
2187 case RIC_SERVICE_QUERY: {
2188 if (loglevel >= MDCLOG_DEBUG) {
2189 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_QUERY");
2191 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2194 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment();
2195 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
2196 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2197 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_QUERY");
2202 case RIC_SERVICE_UPDATE_ACK: {
2203 if (loglevel >= MDCLOG_DEBUG) {
2204 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_ACK");
2206 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2207 mdclog_write(MDCLOG_ERR, "error in PER_FromXML");
2210 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2211 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2212 if (loglevel >= MDCLOG_DEBUG) {
2213 mdclog_write(MDCLOG_DEBUG, "Before sending to CU");
2215 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2216 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_ACK");
2221 case RIC_SERVICE_UPDATE_FAILURE: {
2222 if (loglevel >= MDCLOG_DEBUG) {
2223 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_FAILURE");
2225 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2228 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2229 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2230 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2231 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_FAILURE");
2236 case RIC_E2_RESET_REQ: {
2237 if (loglevel >= MDCLOG_DEBUG) {
2238 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_REQ");
2240 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2243 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2244 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
2245 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2246 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET");
2251 case RIC_E2_RESET_RESP: {
2252 if (loglevel >= MDCLOG_DEBUG) {
2253 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_RESP");
2255 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2258 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2259 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
2260 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2261 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET_RESP");
2266 case RIC_SCTP_CLEAR_ALL: {
2267 mdclog_write(MDCLOG_INFO, "RIC_SCTP_CLEAR_ALL");
2268 // loop on all keys and close socket and then erase all map.
2270 sctpMap->getKeys(v);
2271 for (auto const &iter : v) { //}; iter != sctpMap.end(); iter++) {
2272 if (!boost::starts_with((string) (iter), "host:") && !boost::starts_with((string) (iter), "msg:")) {
2273 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(iter);
2274 if (peerInfo == nullptr) {
2277 close(peerInfo->fileDescriptor);
2278 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
2279 message.message.direction = 'D';
2280 message.message.time.tv_nsec = ts.tv_nsec;
2281 message.message.time.tv_sec = ts.tv_sec;
2283 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
2284 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
2286 "%s|RIC_SCTP_CLEAR_ALL",
2287 peerInfo->enodbName);
2288 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
2289 mdclog_write(MDCLOG_INFO, "%s", message.message.asndata);
2290 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
2291 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
2301 case E2_TERM_KEEP_ALIVE_REQ: {
2302 // send message back
2303 rmr_bytes2payload(rmrMessageBuffer.sendMessage,
2304 (unsigned char *)rmrMessageBuffer.ka_message,
2305 rmrMessageBuffer.ka_message_len);
2306 rmrMessageBuffer.sendMessage->mtype = E2_TERM_KEEP_ALIVE_RESP;
2307 rmrMessageBuffer.sendMessage->state = 0;
2308 static unsigned char tx[32];
2309 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2310 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, txLen);
2311 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2312 if (rmrMessageBuffer.sendMessage == nullptr) {
2313 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2314 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP RMR message returned NULL");
2315 } else if (rmrMessageBuffer.sendMessage->state != 0) {
2316 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP, on RMR state = %d ( %s)",
2317 rmrMessageBuffer.sendMessage->state, translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str());
2318 } else if (loglevel >= MDCLOG_DEBUG) {
2319 mdclog_write(MDCLOG_DEBUG, "Got Keep Alive Request send : %s", rmrMessageBuffer.ka_message);
2324 case RIC_HEALTH_CHECK_REQ: {
2325 static int counter = 0;
2326 // send message back
2327 rmr_bytes2payload(rmrMessageBuffer.rcvMessage,
2328 (unsigned char *)"OK",
2330 rmrMessageBuffer.rcvMessage->mtype = RIC_HEALTH_CHECK_RESP;
2331 rmrMessageBuffer.rcvMessage->state = 0;
2332 static unsigned char tx[32];
2333 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2334 rmr_bytes2xact(rmrMessageBuffer.rcvMessage, tx, txLen);
2335 rmrMessageBuffer.rcvMessage = rmr_rts_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2336 //rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2337 if (rmrMessageBuffer.rcvMessage == nullptr) {
2338 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2339 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP RMR message returned NULL");
2340 } else if (rmrMessageBuffer.rcvMessage->state != 0) {
2341 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP, on RMR state = %d ( %s)",
2342 rmrMessageBuffer.rcvMessage->state, translateRmrErrorMessages(rmrMessageBuffer.rcvMessage->state).c_str());
2343 } else if (loglevel >= MDCLOG_DEBUG && ++counter % 100 == 0) {
2344 mdclog_write(MDCLOG_DEBUG, "Got %d RIC_HEALTH_CHECK_REQ Request send : OK", counter);
2351 mdclog_write(MDCLOG_WARN, "Message Type : %d is not supported", rmrMessageBuffer.rcvMessage->mtype);
2352 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
2353 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
2354 message.message.time.tv_nsec = ts.tv_nsec;
2355 message.message.time.tv_sec = ts.tv_sec;
2356 message.message.messageType = rmrMessageBuffer.rcvMessage->mtype;
2358 buildJsonMessage(message);
2363 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2364 mdclog_write(MDCLOG_DEBUG, "EXIT OK from %s", __FUNCTION__);
2370 * Send message to the CU that is not expecting for successful or unsuccessful results
2371 * @param messageBuffer
2373 * @param failedMsgId
2377 int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messageBuffer,
2378 ReportingMessages_t &message,
2380 Sctp_Map_t *sctpMap) {
2381 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2382 mdclog_write(MDCLOG_DEBUG, "send message: %d to %s address", message.message.messageType, message.message.enodbName);
2385 getRequestMetaData(message, messageBuffer);
2386 if (mdclog_level_get() >= MDCLOG_INFO) {
2387 mdclog_write(MDCLOG_INFO, "send message to %s address", message.message.enodbName);
2390 auto rc = sendMessagetoCu(sctpMap, messageBuffer, message, failedMsgId);
2397 * @param messageBuffer
2399 * @param failedMesgId
2402 int sendMessagetoCu(Sctp_Map_t *sctpMap,
2403 RmrMessagesBuffer_t &messageBuffer,
2404 ReportingMessages_t &message,
2407 message.message.messageType = messageBuffer.rcvMessage->mtype;
2408 auto rc = sendSctpMsg(message.peerInfo, message, sctpMap);
2423 int addToEpoll(int epoll_fd,
2424 ConnectedCU_t *peerInfo,
2426 Sctp_Map_t *sctpMap,
2430 struct epoll_event event{};
2431 event.data.ptr = peerInfo;
2432 event.events = events;
2433 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peerInfo->fileDescriptor, &event) < 0) {
2434 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2435 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here), %s, %s %d",
2436 strerror(errno), __func__, __LINE__);
2438 close(peerInfo->fileDescriptor);
2439 if (enodbName != nullptr) {
2440 cleanHashEntry(peerInfo, sctpMap);
2441 char key[MAX_ENODB_NAME_SIZE * 2];
2442 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2443 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2444 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2446 auto tmp = sctpMap->find(key);
2449 sctpMap->erase(key);
2452 peerInfo->enodbName[0] = 0;
2454 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
2470 int modifyToEpoll(int epoll_fd,
2471 ConnectedCU_t *peerInfo,
2473 Sctp_Map_t *sctpMap,
2477 struct epoll_event event{};
2478 event.data.ptr = peerInfo;
2479 event.events = events;
2480 if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, peerInfo->fileDescriptor, &event) < 0) {
2481 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2482 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_MOD (may check not to quit here), %s, %s %d",
2483 strerror(errno), __func__, __LINE__);
2485 close(peerInfo->fileDescriptor);
2486 cleanHashEntry(peerInfo, sctpMap);
2487 char key[MAX_ENODB_NAME_SIZE * 2];
2488 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2489 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2490 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2492 auto tmp = sctpMap->find(key);
2496 sctpMap->erase(key);
2497 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
2504 int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message) {
2505 buildJsonMessage(message);
2507 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2509 if (rmrMessageBuffer.sendMessage == nullptr) {
2510 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2511 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
2515 if (rmrMessageBuffer.sendMessage->state != 0) {
2516 char meid[RMR_MAX_MEID]{};
2517 if (rmrMessageBuffer.sendMessage->state == RMR_ERR_RETRY) {
2519 rmrMessageBuffer.sendMessage->state = 0;
2520 mdclog_write(MDCLOG_INFO, "RETRY sending Message type %d to Xapp from %s",
2521 rmrMessageBuffer.sendMessage->mtype,
2522 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2523 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2524 if (rmrMessageBuffer.sendMessage == nullptr) {
2525 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
2526 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2528 } else if (rmrMessageBuffer.sendMessage->state != 0) {
2529 mdclog_write(MDCLOG_ERR,
2530 "Message state %s while sending request %d to Xapp from %s after retry of 10 microseconds",
2531 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2532 rmrMessageBuffer.sendMessage->mtype,
2533 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2534 auto rc = rmrMessageBuffer.sendMessage->state;
2538 mdclog_write(MDCLOG_ERR, "Message state %s while sending request %d to Xapp from %s",
2539 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2540 rmrMessageBuffer.sendMessage->mtype,
2541 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2542 return rmrMessageBuffer.sendMessage->state;
2548 void buildJsonMessage(ReportingMessages_t &message) {
2550 message.outLen = sizeof(message.base64Data);
2551 base64::encode((const unsigned char *) message.message.asndata,
2552 (const int) message.message.asnLength,
2555 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2556 mdclog_write(MDCLOG_DEBUG, "Tracing: ASN length = %d, base64 message length = %d ",
2557 (int) message.message.asnLength,
2558 (int) message.outLen);
2561 snprintf(message.buffer, sizeof(message.buffer),
2562 "{\"header\": {\"ts\": \"%ld.%09ld\","
2563 "\"ranName\": \"%s\","
2564 "\"messageType\": %d,"
2565 "\"direction\": \"%c\"},"
2566 "\"base64Length\": %d,"
2567 "\"asnBase64\": \"%s\"}",
2568 message.message.time.tv_sec,
2569 message.message.time.tv_nsec,
2570 message.message.enodbName,
2571 message.message.messageType,
2572 message.message.direction,
2573 (int) message.outLen,
2574 message.base64Data);
2575 static src::logger_mt &lg = my_logger::get();
2577 BOOST_LOG(lg) << message.buffer;
2583 * take RMR error code to string
2587 string translateRmrErrorMessages(int state) {
2591 str = "RMR_OK - state is good";
2593 case RMR_ERR_BADARG:
2594 str = "RMR_ERR_BADARG - argument passed to function was unusable";
2596 case RMR_ERR_NOENDPT:
2597 str = "RMR_ERR_NOENDPT - send//call could not find an endpoint based on msg type";
2600 str = "RMR_ERR_EMPTY - msg received had no payload; attempt to send an empty message";
2603 str = "RMR_ERR_NOHDR - message didn't contain a valid header";
2605 case RMR_ERR_SENDFAILED:
2606 str = "RMR_ERR_SENDFAILED - send failed; errno has nano reason";
2608 case RMR_ERR_CALLFAILED:
2609 str = "RMR_ERR_CALLFAILED - unable to send call() message";
2611 case RMR_ERR_NOWHOPEN:
2612 str = "RMR_ERR_NOWHOPEN - no wormholes are open";
2615 str = "RMR_ERR_WHID - wormhole id was invalid";
2617 case RMR_ERR_OVERFLOW:
2618 str = "RMR_ERR_OVERFLOW - operation would have busted through a buffer/field size";
2621 str = "RMR_ERR_RETRY - request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)";
2623 case RMR_ERR_RCVFAILED:
2624 str = "RMR_ERR_RCVFAILED - receive failed (hard error)";
2626 case RMR_ERR_TIMEOUT:
2627 str = "RMR_ERR_TIMEOUT - message processing call timed out";
2630 str = "RMR_ERR_UNSET - the message hasn't been populated with a transport buffer";
2633 str = "RMR_ERR_TRUNC - received message likely truncated";
2635 case RMR_ERR_INITFAILED:
2636 str = "RMR_ERR_INITFAILED - initialisation of something (probably message) failed";
2638 case RMR_ERR_NOTSUPP:
2639 str = "RMR_ERR_NOTSUPP - the request is not supported, or RMr was not initialised for the request";
2643 snprintf(buf, sizeof buf, "UNDOCUMENTED RMR_ERR : %d", state);