blob: c5ba2295442ebae90d4e6d3e5125ff113f0c7fb2 [file] [log] [blame]
ss412g1a79bdf2019-10-24 12:03:05 +03001// Copyright 2019 AT&T Intellectual Property
2// Copyright 2019 Nokia
3//
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
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
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.
15
nm755n2e268142019-11-28 16:40:23 +000016// This source code is part of the near-RT RIC (RAN Intelligent Controller)
17// platform project (RICP).
18
ss412g1a79bdf2019-10-24 12:03:05 +030019// TODO: High-level file comment.
20
21
aa7133@att.com84bd3342020-03-16 18:04:57 +020022
aa7133@att.com7b437f72020-03-30 13:25:54 +030023#include <3rdparty/oranE2/RANfunctions-List.h>
ss412g1a79bdf2019-10-24 12:03:05 +030024#include "sctpThread.h"
aa7133@att.com84bd3342020-03-16 18:04:57 +020025#include "BuildRunName.h"
ss412g1a79bdf2019-10-24 12:03:05 +030026
aa7133@att.com7b437f72020-03-30 13:25:54 +030027//#define asn_DEF_E2SM_gNB_NRT_RANfunction_Definition e2sm_asn_DEF_E2SM_gNB_NRT_RANfunction_Definition
28//#define __gcov_init e2sm___gcov_init
29//#define __gcov_merge_add e2sm___gcov_merge_add
30//#define calloc e2sm_calloc
31
32#include "3rdparty/oranE2SM/E2SM-gNB-NRT-RANfunction-Definition.h"
33
34//#undef calloc
35//#undef __gcov_merge_add
36//#undef __gcov_init
37//#undef asn_DEF_E2SM_gNB_NRT_RANfunction_Definition
38
aa7133@att.com84bd3342020-03-16 18:04:57 +020039using namespace std;
40//using namespace std::placeholders;
ss412g3bac2da2020-01-05 11:52:19 +020041using namespace boost::filesystem;
42
ss412g1a79bdf2019-10-24 12:03:05 +030043//#ifdef __cplusplus
44//extern "C"
45//{
46//#endif
47
ss412g3bac2da2020-01-05 11:52:19 +020048// need to expose without the include of gcov
49extern "C" void __gcov_flush(void);
50
51static void catch_function(int signal) {
52 __gcov_flush();
53 exit(signal);
54}
55
56
ss412g1a79bdf2019-10-24 12:03:05 +030057BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
58
59boost::shared_ptr<sinks::synchronous_sink<sinks::text_file_backend>> boostLogger;
ss412g3bac2da2020-01-05 11:52:19 +020060double cpuClock = 0.0;
61bool jsonTrace = true;
ss412g1a79bdf2019-10-24 12:03:05 +030062
63void init_log() {
64 mdclog_attr_t *attr;
65 mdclog_attr_init(&attr);
66 mdclog_attr_set_ident(attr, "E2Terminator");
67 mdclog_init(attr);
68 mdclog_attr_destroy(attr);
69}
ss412g3bac2da2020-01-05 11:52:19 +020070auto start_time = std::chrono::high_resolution_clock::now();
71typedef std::chrono::duration<double, std::ratio<1,1>> seconds_t;
ss412g1a79bdf2019-10-24 12:03:05 +030072
ss412g3bac2da2020-01-05 11:52:19 +020073double age() {
74 return seconds_t(std::chrono::high_resolution_clock::now() - start_time).count();
75}
76
77double approx_CPU_MHz(unsigned sleeptime) {
78 using namespace std::chrono_literals;
79 uint32_t aux = 0;
80 uint64_t cycles_start = rdtscp(aux);
81 double time_start = age();
82 std::this_thread::sleep_for(sleeptime * 1ms);
83 uint64_t elapsed_cycles = rdtscp(aux) - cycles_start;
84 double elapsed_time = age() - time_start;
85 return elapsed_cycles / elapsed_time;
86}
ss412g1a79bdf2019-10-24 12:03:05 +030087
88//std::atomic<int64_t> rmrCounter{0};
89std::atomic<int64_t> num_of_messages{0};
ss412g3bac2da2020-01-05 11:52:19 +020090std::atomic<int64_t> num_of_XAPP_messages{0};
ss412g1a79bdf2019-10-24 12:03:05 +030091static long transactionCounter = 0;
92
aa7133@att.com84bd3342020-03-16 18:04:57 +020093int buildListeningPort(sctp_params_t &sctpParams) {
94 sctpParams.listenFD = socket (AF_INET6, SOCK_STREAM, IPPROTO_SCTP);
95 struct sockaddr_in6 servaddr {};
96 servaddr.sin6_family = AF_INET6;
97 servaddr.sin6_addr = in6addr_any;
aa7133@att.combe92d712020-03-18 17:30:39 +020098 servaddr.sin6_port = htons(sctpParams.sctpPort);
aa7133@att.com84bd3342020-03-16 18:04:57 +020099 if (bind(sctpParams.listenFD, (SA *)&servaddr, sizeof(servaddr)) < 0 ) {
100 mdclog_write(MDCLOG_ERR, "Error binding. %s\n", strerror(errno));
101 return -1;
102 }
103 if (setSocketNoBlocking(sctpParams.listenFD) == -1) {
104 //mdclog_write(MDCLOG_ERR, "Error binding. %s", strerror(errno));
105 return -1;
106 }
107 if (mdclog_level_get() >= MDCLOG_DEBUG) {
108 struct sockaddr_in6 cliaddr {};
109 socklen_t len = sizeof(cliaddr);
110 getsockname(sctpParams.listenFD, (SA *)&cliaddr, &len);
111 char buff[1024] {};
112 inet_ntop(AF_INET6, &cliaddr.sin6_addr, buff, sizeof(buff));
113 mdclog_write(MDCLOG_DEBUG, "My address: %s, port %d\n", buff, htons(cliaddr.sin6_port));
ss412g1a79bdf2019-10-24 12:03:05 +0300114 }
115
aa7133@att.com84bd3342020-03-16 18:04:57 +0200116 if (listen(sctpParams.listenFD, SOMAXCONN) < 0) {
117 mdclog_write(MDCLOG_ERR, "Error listening. %s\n", strerror(errno));
118 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200119 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200120 struct epoll_event event {};
121 event.events = EPOLLIN | EPOLLET;
122 event.data.fd = sctpParams.listenFD;
123
124 // add listening port to epoll
125 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.listenFD, &event)) {
126 printf("Failed to add descriptor to epoll\n");
127 mdclog_write(MDCLOG_ERR, "Failed to add descriptor to epoll. %s\n", strerror(errno));
128 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200129 }
130
aa7133@att.com84bd3342020-03-16 18:04:57 +0200131 return 0;
132}
ss412g3bac2da2020-01-05 11:52:19 +0200133
aa7133@att.com84bd3342020-03-16 18:04:57 +0200134int buildConfiguration(sctp_params_t &sctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +0200135 path p = (sctpParams.configFilePath + "/" + sctpParams.configFileName).c_str();
136 if (exists(p)) {
137 const int size = 2048;
138 auto fileSize = file_size(p);
139 if (fileSize > size) {
140 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200141 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200142 }
143 } else {
144 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
aa7133@att.com84bd3342020-03-16 18:04:57 +0200145 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200146 }
147
ss412g3bac2da2020-01-05 11:52:19 +0200148 ReadConfigFile conf;
149 if (conf.openConfigFile(p.string()) == -1) {
150 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
151 p.string().c_str(), strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200152 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200153 }
154 int rmrPort = conf.getIntValue("nano");
155 if (rmrPort == -1) {
156 mdclog_write(MDCLOG_ERR, "illigal RMR port ");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200157 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200158 }
159 sctpParams.rmrPort = (uint16_t)rmrPort;
160 snprintf(sctpParams.rmrAddress, sizeof(sctpParams.rmrAddress), "%d", (int) (sctpParams.rmrPort));
161
162 auto tmpStr = conf.getStringValue("loglevel");
163 if (tmpStr.length() == 0) {
164 mdclog_write(MDCLOG_ERR, "illigal loglevel. Set loglevel to MDCLOG_INFO");
165 tmpStr = "info";
166 }
167 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
168
169 if ((tmpStr.compare("debug")) == 0) {
170 sctpParams.logLevel = MDCLOG_DEBUG;
171 } else if ((tmpStr.compare("info")) == 0) {
172 sctpParams.logLevel = MDCLOG_INFO;
173 } else if ((tmpStr.compare("warning")) == 0) {
174 sctpParams.logLevel = MDCLOG_WARN;
175 } else if ((tmpStr.compare("error")) == 0) {
176 sctpParams.logLevel = MDCLOG_ERR;
177 } else {
178 mdclog_write(MDCLOG_ERR, "illigal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
179 sctpParams.logLevel = MDCLOG_INFO;
180 }
181 mdclog_level_set(sctpParams.logLevel);
182
183 tmpStr = conf.getStringValue("volume");
184 if (tmpStr.length() == 0) {
185 mdclog_write(MDCLOG_ERR, "illigal volume.");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200186 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200187 }
188
ss412g1a79bdf2019-10-24 12:03:05 +0300189 char tmpLogFilespec[VOLUME_URL_SIZE];
190 tmpLogFilespec[0] = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200191 sctpParams.volume[0] = 0;
192 snprintf(sctpParams.volume, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
193 // copy the name to temp file as well
194 snprintf(tmpLogFilespec, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
195
196
197 // define the file name in the tmp directory under the volume
198 strcat(tmpLogFilespec,"/tmp/E2Term_%Y-%m-%d_%H-%M-%S.%N.tmpStr");
199
200 sctpParams.myIP = conf.getStringValue("local-ip");
201 if (sctpParams.myIP.length() == 0) {
202 mdclog_write(MDCLOG_ERR, "illigal local-ip.");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200203 return -1;
ss412g1a79bdf2019-10-24 12:03:05 +0300204 }
205
aa7133@att.combe92d712020-03-18 17:30:39 +0200206 int sctpPort = conf.getIntValue("sctp-port");
207 if (sctpPort == -1) {
208 mdclog_write(MDCLOG_ERR, "illigal SCTP port ");
209 return -1;
210 }
211 sctpParams.sctpPort = (uint16_t)sctpPort;
212
aa7133@att.com84bd3342020-03-16 18:04:57 +0200213 sctpParams.fqdn = conf.getStringValue("external-fqdn");
214 if (sctpParams.fqdn.length() == 0) {
215 mdclog_write(MDCLOG_ERR, "illigal external-fqdn");
216 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200217 }
ss412g1a79bdf2019-10-24 12:03:05 +0300218
aa7133@att.com342843e2020-01-26 12:53:34 +0200219 std::string pod = conf.getStringValue("pod_name");
220 if (pod.length() == 0) {
221 mdclog_write(MDCLOG_ERR, "illigal pod_name in config file");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200222 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200223 }
224 auto *podName = getenv(pod.c_str());
225 if (podName == nullptr) {
226 mdclog_write(MDCLOG_ERR, "illigal pod_name or environment varible not exists : %s", pod.c_str());
aa7133@att.com84bd3342020-03-16 18:04:57 +0200227 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200228
229 } else {
230 sctpParams.podName.assign(podName);
231 if (sctpParams.podName.length() == 0) {
232 mdclog_write(MDCLOG_ERR, "illigal pod_name");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200233 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200234 }
235 }
236
ss412g3bac2da2020-01-05 11:52:19 +0200237 tmpStr = conf.getStringValue("trace");
238 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
239 if ((tmpStr.compare("start")) == 0) {
240 mdclog_write(MDCLOG_INFO, "Trace set to: start");
241 sctpParams.trace = true;
242 } else if ((tmpStr.compare("stop")) == 0) {
243 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
244 sctpParams.trace = false;
245 }
246 jsonTrace = sctpParams.trace;
ss412g1a79bdf2019-10-24 12:03:05 +0300247
aa7133@att.combe92d712020-03-18 17:30:39 +0200248 sctpParams.ka_message_length = snprintf(sctpParams.ka_message, KA_MESSAGE_SIZE, "{\"address\": \"%s:%d\","
aa7133@att.com96c9b882020-03-25 14:50:40 +0200249 "\"fqdn\": \"%s\","
250 "\"pod_name\": \"%s\"}",
aa7133@att.com342843e2020-01-26 12:53:34 +0200251 (const char *)sctpParams.myIP.c_str(),
252 sctpParams.rmrPort,
253 sctpParams.fqdn.c_str(),
254 sctpParams.podName.c_str());
255
ss412g1a79bdf2019-10-24 12:03:05 +0300256 if (mdclog_level_get() >= MDCLOG_INFO) {
ss412g3bac2da2020-01-05 11:52:19 +0200257 mdclog_mdc_add("RMR Port", to_string(sctpParams.rmrPort).c_str());
258 mdclog_mdc_add("LogLevel", to_string(sctpParams.logLevel).c_str());
259 mdclog_mdc_add("volume", sctpParams.volume);
ss412g1a79bdf2019-10-24 12:03:05 +0300260 mdclog_mdc_add("tmpLogFilespec", tmpLogFilespec);
ss412g3bac2da2020-01-05 11:52:19 +0200261 mdclog_mdc_add("my ip", sctpParams.myIP.c_str());
aa7133@att.com342843e2020-01-26 12:53:34 +0200262 mdclog_mdc_add("pod name", sctpParams.podName.c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300263
aa7133@att.com342843e2020-01-26 12:53:34 +0200264 mdclog_write(MDCLOG_INFO, "running parameters for instance : %s", sctpParams.ka_message);
ss412g1a79bdf2019-10-24 12:03:05 +0300265 }
266 mdclog_mdc_clean();
267
268 // Files written to the current working directory
269 boostLogger = logging::add_file_log(
ss412g3bac2da2020-01-05 11:52:19 +0200270 keywords::file_name = tmpLogFilespec, // to temp directory
ss412g1a79bdf2019-10-24 12:03:05 +0300271 keywords::rotation_size = 10 * 1024 * 1024,
272 keywords::time_based_rotation = sinks::file::rotation_at_time_interval(posix_time::hours(1)),
273 keywords::format = "%Message%"
ss412g3bac2da2020-01-05 11:52:19 +0200274 //keywords::format = "[%TimeStamp%]: %Message%" // use each tmpStr with time stamp
ss412g1a79bdf2019-10-24 12:03:05 +0300275 );
276
277 // Setup a destination folder for collecting rotated (closed) files --since the same volumn can use rename()
278 boostLogger->locked_backend()->set_file_collector(sinks::file::make_collector(
ss412g3bac2da2020-01-05 11:52:19 +0200279 keywords::target = sctpParams.volume
ss412g1a79bdf2019-10-24 12:03:05 +0300280 ));
281
282 // Upon restart, scan the directory for files matching the file_name pattern
283 boostLogger->locked_backend()->scan_for_files();
284
ss412g3bac2da2020-01-05 11:52:19 +0200285 // Enable auto-flushing after each tmpStr record written
ss412g1a79bdf2019-10-24 12:03:05 +0300286 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200287 boostLogger->locked_backend()->auto_flush(true);
288 }
289
290 return 0;
291}
292
aa7133@att.com16c799b2020-03-24 18:01:51 +0200293
294
aa7133@att.com84bd3342020-03-16 18:04:57 +0200295int main(const int argc, char **argv) {
296 sctp_params_t sctpParams;
297
aa7133@att.com84bd3342020-03-16 18:04:57 +0200298 {
299 std::random_device device{};
300 std::mt19937 generator(device());
301 std::uniform_int_distribution<long> distribution(1, (long) 1e12);
302 transactionCounter = distribution(generator);
303 }
304
305// uint64_t st = 0;
306// uint32_t aux1 = 0;
307// st = rdtscp(aux1);
308
309 unsigned num_cpus = std::thread::hardware_concurrency();
310 init_log();
311 mdclog_level_set(MDCLOG_INFO);
312
313 if (std::signal(SIGINT, catch_function) == SIG_ERR) {
314 mdclog_write(MDCLOG_ERR, "Error initializing SIGINT");
315 exit(1);
316 }
317 if (std::signal(SIGABRT, catch_function)== SIG_ERR) {
318 mdclog_write(MDCLOG_ERR, "Error initializing SIGABRT");
319 exit(1);
320 }
321 if (std::signal(SIGTERM, catch_function)== SIG_ERR) {
322 mdclog_write(MDCLOG_ERR, "Error initializing SIGTERM");
323 exit(1);
324 }
325
326 cpuClock = approx_CPU_MHz(100);
327
328 mdclog_write(MDCLOG_DEBUG, "CPU speed %11.11f", cpuClock);
329
330 auto result = parse(argc, argv, sctpParams);
331
332 if (buildConfiguration(sctpParams) != 0) {
333 exit(-1);
ss412g1a79bdf2019-10-24 12:03:05 +0300334 }
335
336 // start epoll
ss412g3bac2da2020-01-05 11:52:19 +0200337 sctpParams.epoll_fd = epoll_create1(0);
338 if (sctpParams.epoll_fd == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +0300339 mdclog_write(MDCLOG_ERR, "failed to open epoll descriptor");
340 exit(-1);
341 }
342
aa7133@att.combe92d712020-03-18 17:30:39 +0200343 getRmrContext(sctpParams);
ss412g3bac2da2020-01-05 11:52:19 +0200344 if (sctpParams.rmrCtx == nullptr) {
345 close(sctpParams.epoll_fd);
ss412g1a79bdf2019-10-24 12:03:05 +0300346 exit(-1);
347 }
348
ss412g3bac2da2020-01-05 11:52:19 +0200349 if (buildInotify(sctpParams) == -1) {
350 close(sctpParams.rmrListenFd);
351 rmr_close(sctpParams.rmrCtx);
352 close(sctpParams.epoll_fd);
353 exit(-1);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200354 }
355
356 if (buildListeningPort(sctpParams) != 0) {
357 close(sctpParams.rmrListenFd);
358 rmr_close(sctpParams.rmrCtx);
359 close(sctpParams.epoll_fd);
360 exit(-1);
361 }
ss412g3bac2da2020-01-05 11:52:19 +0200362
363 sctpParams.sctpMap = new mapWrapper();
ss412g1a79bdf2019-10-24 12:03:05 +0300364
365 std::vector<std::thread> threads(num_cpus);
366// std::vector<std::thread> threads;
367
368 num_cpus = 1;
369 for (unsigned int i = 0; i < num_cpus; i++) {
ss412g3bac2da2020-01-05 11:52:19 +0200370 threads[i] = std::thread(listener, &sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300371
372 cpu_set_t cpuset;
373 CPU_ZERO(&cpuset);
374 CPU_SET(i, &cpuset);
375 int rc = pthread_setaffinity_np(threads[i].native_handle(), sizeof(cpu_set_t), &cpuset);
376 if (rc != 0) {
377 mdclog_write(MDCLOG_ERR, "Error calling pthread_setaffinity_np: %d", rc);
378 }
ss412g1a79bdf2019-10-24 12:03:05 +0300379 }
380
aa7133@att.com16c799b2020-03-24 18:01:51 +0200381 auto statFlag = false;
382 auto statThread = std::thread(statColectorThread, (void *)&statFlag);
383
ss412g3bac2da2020-01-05 11:52:19 +0200384 //loop over term_init until first message from xApp
385 handleTermInit(sctpParams);
386
387 for (auto &t : threads) {
388 t.join();
389 }
390
aa7133@att.com16c799b2020-03-24 18:01:51 +0200391 statFlag = true;
392 statThread.join();
393
ss412g3bac2da2020-01-05 11:52:19 +0200394 return 0;
395}
396
397void handleTermInit(sctp_params_t &sctpParams) {
398 sendTermInit(sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300399 //send to e2 manager init of e2 term
400 //E2_TERM_INIT
ss412g1a79bdf2019-10-24 12:03:05 +0300401
ss412g3bac2da2020-01-05 11:52:19 +0200402 int count = 0;
403 while (true) {
404 auto xappMessages = num_of_XAPP_messages.load(std::memory_order_acquire);
405 if (xappMessages > 0) {
406 if (mdclog_level_get() >= MDCLOG_INFO) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200407 mdclog_write(MDCLOG_INFO, "Got a message from some appliction, stop sending E2_TERM_INIT");
ss412g3bac2da2020-01-05 11:52:19 +0200408 }
409 return;
410 }
411 usleep(100000);
412 count++;
413 if (count % 1000 == 0) {
414 mdclog_write(MDCLOG_ERR, "GOT No messages from any xApp");
415 sendTermInit(sctpParams);
416 }
417 }
418}
419
420void sendTermInit(sctp_params_t &sctpParams) {
421 rmr_mbuf_t *msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300422 auto count = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200423 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +0300424 msg->mtype = E2_TERM_INIT;
425 msg->state = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200426 rmr_bytes2payload(msg, (unsigned char *)sctpParams.ka_message, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300427 static unsigned char tx[32];
428 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
429 rmr_bytes2xact(msg, tx, txLen);
ss412g3bac2da2020-01-05 11:52:19 +0200430 msg = rmr_send_msg(sctpParams.rmrCtx, msg);
ss412g1a79bdf2019-10-24 12:03:05 +0300431 if (msg == nullptr) {
ss412g3bac2da2020-01-05 11:52:19 +0200432 msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.myIP.length());
ss412g1a79bdf2019-10-24 12:03:05 +0300433 } else if (msg->state == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300434 rmr_free_msg(msg);
ss412g3bac2da2020-01-05 11:52:19 +0200435 if (mdclog_level_get() >= MDCLOG_INFO) {
436 mdclog_write(MDCLOG_INFO, "E2_TERM_INIT succsesfuly sent ");
437 }
438 return;
ss412g1a79bdf2019-10-24 12:03:05 +0300439 } else {
440 if (count % 100 == 0) {
aa7133@att.com11b17d42020-03-22 11:35:16 +0200441 mdclog_write(MDCLOG_ERR, "Error sending E2_TERM_INIT cause : %s ", translateRmrErrorMessages(msg->state).c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300442 }
443 sleep(1);
444 }
445 count++;
446 }
ss412g3bac2da2020-01-05 11:52:19 +0200447}
448
449/**
450 *
451 * @param argc
452 * @param argv
453 * @param sctpParams
454 * @return
455 */
456cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &sctpParams) {
457 cxxopts::Options options(argv[0], "e2 term help");
458 options.positional_help("[optional args]").show_positional_help();
459 options.allow_unrecognised_options().add_options()
460 ("p,path", "config file path", cxxopts::value<std::string>(sctpParams.configFilePath)->default_value("config"))
461 ("f,file", "config file name", cxxopts::value<std::string>(sctpParams.configFileName)->default_value("config.conf"))
462 ("h,help", "Print help");
463
464 auto result = options.parse(argc, argv);
465
466 if (result.count("help")) {
467 std::cout << options.help({""}) << std::endl;
468 exit(0);
469 }
470 return result;
471}
472
473/**
474 *
475 * @param sctpParams
476 * @return -1 failed 0 success
477 */
478int buildInotify(sctp_params_t &sctpParams) {
479 sctpParams.inotifyFD = inotify_init1(IN_NONBLOCK);
480 if (sctpParams.inotifyFD == -1) {
481 mdclog_write(MDCLOG_ERR, "Failed to init inotify (inotify_init1) %s", strerror(errno));
482 close(sctpParams.rmrListenFd);
483 rmr_close(sctpParams.rmrCtx);
484 close(sctpParams.epoll_fd);
485 return -1;
ss412g1a79bdf2019-10-24 12:03:05 +0300486 }
487
ss412g3bac2da2020-01-05 11:52:19 +0200488 sctpParams.inotifyWD = inotify_add_watch(sctpParams.inotifyFD,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200489 (const char *)sctpParams.configFilePath.c_str(),
aa7133@att.com84bd3342020-03-16 18:04:57 +0200490 (unsigned)IN_OPEN | (unsigned)IN_CLOSE_WRITE | (unsigned)IN_CLOSE_NOWRITE); //IN_CLOSE = (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
ss412g3bac2da2020-01-05 11:52:19 +0200491 if (sctpParams.inotifyWD == -1) {
492 mdclog_write(MDCLOG_ERR, "Failed to add directory : %s to inotify (inotify_add_watch) %s",
aa7133@att.com96c9b882020-03-25 14:50:40 +0200493 sctpParams.configFilePath.c_str(),
494 strerror(errno));
ss412g3bac2da2020-01-05 11:52:19 +0200495 close(sctpParams.inotifyFD);
496 return -1;
497 }
498
499 struct epoll_event event{};
500 event.events = (EPOLLIN);
501 event.data.fd = sctpParams.inotifyFD;
502 // add listening RMR FD to epoll
503 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.inotifyFD, &event)) {
504 mdclog_write(MDCLOG_ERR, "Failed to add inotify FD to epoll");
505 close(sctpParams.inotifyFD);
506 return -1;
507 }
ss412g1a79bdf2019-10-24 12:03:05 +0300508 return 0;
509}
510
511/**
512 *
513 * @param args
514 * @return
515 */
516void listener(sctp_params_t *params) {
ss412g1a79bdf2019-10-24 12:03:05 +0300517 int num_of_SCTP_messages = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300518 auto totalTime = 0.0;
519 mdclog_mdc_clean();
520 mdclog_level_set(params->logLevel);
521
522 std::thread::id this_id = std::this_thread::get_id();
523 //save cout
524 streambuf *oldCout = cout.rdbuf();
525 ostringstream memCout;
526 // create new cout
527 cout.rdbuf(memCout.rdbuf());
528 cout << this_id;
529 //return to the normal cout
530 cout.rdbuf(oldCout);
531
532 char tid[32];
533 memcpy(tid, memCout.str().c_str(), memCout.str().length() < 32 ? memCout.str().length() : 31);
534 tid[memCout.str().length()] = 0;
535 mdclog_mdc_add("thread id", tid);
536
537 if (mdclog_level_get() >= MDCLOG_DEBUG) {
538 mdclog_write(MDCLOG_DEBUG, "started thread number %s", tid);
539 }
540
aa7133@att.com342843e2020-01-26 12:53:34 +0200541
ss412g1a79bdf2019-10-24 12:03:05 +0300542 RmrMessagesBuffer_t rmrMessageBuffer{};
543 //create and init RMR
544 rmrMessageBuffer.rmrCtx = params->rmrCtx;
545
546 auto *events = (struct epoll_event *) calloc(MAXEVENTS, sizeof(struct epoll_event));
547 struct timespec end{0, 0};
548 struct timespec start{0, 0};
549
550 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
551 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
552
ss412g3bac2da2020-01-05 11:52:19 +0200553 memcpy(rmrMessageBuffer.ka_message, params->ka_message, params->ka_message_length);
554 rmrMessageBuffer.ka_message_len = params->ka_message_length;
555 rmrMessageBuffer.ka_message[rmrMessageBuffer.ka_message_len] = 0;
556
557 if (mdclog_level_get() >= MDCLOG_DEBUG) {
558 mdclog_write(MDCLOG_DEBUG, "keep alive message is : %s", rmrMessageBuffer.ka_message);
559 }
560
ss412g1a79bdf2019-10-24 12:03:05 +0300561 ReportingMessages_t message {};
562
aa7133@att.com7c3e4c02020-03-25 16:37:19 +0200563// for (int i = 0; i < MAX_RMR_BUFF_ARRY; i++) {
564// rmrMessageBuffer.rcvBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
565// rmrMessageBuffer.sendBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
566// }
ss412g1a79bdf2019-10-24 12:03:05 +0300567
aa7133@att.com16c799b2020-03-24 18:01:51 +0200568 message.statCollector = StatCollector::GetInstance();
569
ss412g1a79bdf2019-10-24 12:03:05 +0300570 while (true) {
571 if (mdclog_level_get() >= MDCLOG_DEBUG) {
572 mdclog_write(MDCLOG_DEBUG, "Start EPOLL Wait");
573 }
574 auto numOfEvents = epoll_wait(params->epoll_fd, events, MAXEVENTS, -1);
575 if (numOfEvents < 0 && errno == EINTR) {
576 if (mdclog_level_get() >= MDCLOG_DEBUG) {
577 mdclog_write(MDCLOG_DEBUG, "got EINTR : %s", strerror(errno));
578 }
579 continue;
580 }
581 if (numOfEvents < 0) {
582 mdclog_write(MDCLOG_ERR, "Epoll wait failed, errno = %s", strerror(errno));
583 return;
584 }
585 for (auto i = 0; i < numOfEvents; i++) {
586 if (mdclog_level_get() >= MDCLOG_DEBUG) {
587 mdclog_write(MDCLOG_DEBUG, "handling epoll event %d out of %d", i + 1, numOfEvents);
588 }
589 clock_gettime(CLOCK_MONOTONIC, &message.message.time);
590 start.tv_sec = message.message.time.tv_sec;
591 start.tv_nsec = message.message.time.tv_nsec;
ss412g3bac2da2020-01-05 11:52:19 +0200592
593
ss412g1a79bdf2019-10-24 12:03:05 +0300594 if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200595 handlepoll_error(events[i], message, rmrMessageBuffer, params);
ss412g1a79bdf2019-10-24 12:03:05 +0300596 } else if (events[i].events & EPOLLOUT) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200597 handleEinprogressMessages(events[i], message, rmrMessageBuffer, params);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200598 } else if (params->listenFD == events[i].data.fd) {
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200599 if (mdclog_level_get() >= MDCLOG_INFO) {
600 mdclog_write(MDCLOG_INFO, "New connection request from sctp network\n");
601 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200602 // new connection is requested from RAN start build connection
603 while (true) {
604 struct sockaddr in_addr {};
605 socklen_t in_len;
606 char hostBuff[NI_MAXHOST];
607 char portBuff[NI_MAXSERV];
608
609 in_len = sizeof(in_addr);
610 auto *peerInfo = (ConnectedCU_t *)calloc(1, sizeof(ConnectedCU_t));
611 peerInfo->sctpParams = params;
612 peerInfo->fileDescriptor = accept(params->listenFD, &in_addr, &in_len);
613 if (peerInfo->fileDescriptor == -1) {
614 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
615 /* We have processed all incoming connections. */
616 break;
617 } else {
618 mdclog_write(MDCLOG_ERR, "Accept error, errno = %s", strerror(errno));
619 break;
620 }
621 }
622 if (setSocketNoBlocking(peerInfo->fileDescriptor) == -1) {
623 mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on port %s\n", hostBuff, portBuff);
624 close(peerInfo->fileDescriptor);
625 break;
626 }
627 auto ans = getnameinfo(&in_addr, in_len,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200628 peerInfo->hostName, NI_MAXHOST,
629 peerInfo->portNumber, NI_MAXSERV, (unsigned )((unsigned int)NI_NUMERICHOST | (unsigned int)NI_NUMERICSERV));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200630 if (ans < 0) {
631 mdclog_write(MDCLOG_ERR, "Failed to get info on connection request. %s\n", strerror(errno));
632 close(peerInfo->fileDescriptor);
633 break;
634 }
635 if (mdclog_level_get() >= MDCLOG_DEBUG) {
636 mdclog_write(MDCLOG_DEBUG, "Accepted connection on descriptor %d (host=%s, port=%s)\n", peerInfo->fileDescriptor, peerInfo->hostName, peerInfo->portNumber);
637 }
638 peerInfo->isConnected = false;
639 peerInfo->gotSetup = false;
640 if (addToEpoll(params->epoll_fd,
641 peerInfo,
642 (EPOLLIN | EPOLLET),
643 params->sctpMap, nullptr,
aa7133@att.combe92d712020-03-18 17:30:39 +0200644 0) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200645 break;
646 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200647 break;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200648 }
ss412g1a79bdf2019-10-24 12:03:05 +0300649 } else if (params->rmrListenFd == events[i].data.fd) {
650 // got message from XAPP
ss412g3bac2da2020-01-05 11:52:19 +0200651 num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
ss412g1a79bdf2019-10-24 12:03:05 +0300652 num_of_messages.fetch_add(1, std::memory_order_release);
653 if (mdclog_level_get() >= MDCLOG_DEBUG) {
654 mdclog_write(MDCLOG_DEBUG, "new message from RMR");
655 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200656 if (receiveXappMessages(params->sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +0300657 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200658 message.message.time) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300659 mdclog_write(MDCLOG_ERR, "Error handling Xapp message");
660 }
ss412g3bac2da2020-01-05 11:52:19 +0200661 } else if (params->inotifyFD == events[i].data.fd) {
662 mdclog_write(MDCLOG_INFO, "Got event from inotify (configuration update)");
663 handleConfigChange(params);
ss412g1a79bdf2019-10-24 12:03:05 +0300664 } else {
665 /* We RMR_ERR_RETRY have data on the fd waiting to be read. Read and display it.
666 * We must read whatever data is available completely, as we are running
667 * in edge-triggered mode and won't get a notification again for the same data. */
668 num_of_messages.fetch_add(1, std::memory_order_release);
669 if (mdclog_level_get() >= MDCLOG_DEBUG) {
670 mdclog_write(MDCLOG_DEBUG, "new message from SCTP, epoll flags are : %0x", events[i].events);
671 }
672 receiveDataFromSctp(&events[i],
673 params->sctpMap,
674 num_of_SCTP_messages,
675 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200676 message.message.time);
ss412g1a79bdf2019-10-24 12:03:05 +0300677 }
678
679 clock_gettime(CLOCK_MONOTONIC, &end);
680 if (mdclog_level_get() >= MDCLOG_INFO) {
681 totalTime += ((end.tv_sec + 1.0e-9 * end.tv_nsec) -
682 ((double) start.tv_sec + 1.0e-9 * start.tv_nsec));
683 }
684 if (mdclog_level_get() >= MDCLOG_DEBUG) {
685 mdclog_write(MDCLOG_DEBUG, "message handling is %ld seconds %ld nanoseconds",
686 end.tv_sec - start.tv_sec,
687 end.tv_nsec - start.tv_nsec);
688 }
689 }
690 }
ss412g1a79bdf2019-10-24 12:03:05 +0300691}
692
693/**
694 *
ss412g3bac2da2020-01-05 11:52:19 +0200695 * @param sctpParams
696 */
697void handleConfigChange(sctp_params_t *sctpParams) {
698 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
699 const struct inotify_event *event;
700 char *ptr;
701
702 path p = (sctpParams->configFilePath + "/" + sctpParams->configFileName).c_str();
703 auto endlessLoop = true;
704 while (endlessLoop) {
705 auto len = read(sctpParams->inotifyFD, buf, sizeof buf);
706 if (len == -1) {
707 if (errno != EAGAIN) {
708 mdclog_write(MDCLOG_ERR, "read %s ", strerror(errno));
709 endlessLoop = false;
710 continue;
711 }
712 else {
713 endlessLoop = false;
714 continue;
715 }
716 }
717
718 for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) {
719 event = (const struct inotify_event *)ptr;
720 if (event->mask & (uint32_t)IN_ISDIR) {
721 continue;
722 }
723
724 // the directory name
725 if (sctpParams->inotifyWD == event->wd) {
726 // not the directory
727 }
728 if (event->len) {
729 if (!(sctpParams->configFileName.compare(event->name))) {
730 continue;
731 }
732 }
733 // only the file we want
734 if (event->mask & (uint32_t)IN_CLOSE_WRITE) {
735 if (exists(p)) {
736 const int size = 2048;
737 auto fileSize = file_size(p);
738 if (fileSize > size) {
739 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
740 return;
741 }
742 } else {
743 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
744 return;
745 }
746
747 ReadConfigFile conf;
748 if (conf.openConfigFile(p.string()) == -1) {
749 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
750 p.string().c_str(), strerror(errno));
751 return;
752 }
753
754 auto tmpStr = conf.getStringValue("loglevel");
755 if (tmpStr.length() == 0) {
756 mdclog_write(MDCLOG_ERR, "illigal loglevel. Set loglevel to MDCLOG_INFO");
757 tmpStr = "info";
758 }
759 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
760
761 if ((tmpStr.compare("debug")) == 0) {
762 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_DEBUG");
763 sctpParams->logLevel = MDCLOG_DEBUG;
764 } else if ((tmpStr.compare("info")) == 0) {
765 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_INFO");
766 sctpParams->logLevel = MDCLOG_INFO;
767 } else if ((tmpStr.compare("warning")) == 0) {
768 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_WARN");
769 sctpParams->logLevel = MDCLOG_WARN;
770 } else if ((tmpStr.compare("error")) == 0) {
771 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_ERR");
772 sctpParams->logLevel = MDCLOG_ERR;
773 } else {
774 mdclog_write(MDCLOG_ERR, "illigal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
775 sctpParams->logLevel = MDCLOG_INFO;
776 }
777 mdclog_level_set(sctpParams->logLevel);
778
779
780 tmpStr = conf.getStringValue("trace");
781 if (tmpStr.length() == 0) {
782 mdclog_write(MDCLOG_ERR, "illigal trace. Set trace to stop");
783 tmpStr = "stop";
784 }
785
786 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
787 if ((tmpStr.compare("start")) == 0) {
788 mdclog_write(MDCLOG_INFO, "Trace set to: start");
789 sctpParams->trace = true;
790 } else if ((tmpStr.compare("stop")) == 0) {
791 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
792 sctpParams->trace = false;
793 } else {
794 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
795 sctpParams->trace = false;
796 }
797 jsonTrace = sctpParams->trace;
798 endlessLoop = false;
799 }
800 }
801 }
802}
803
804/**
805 *
806 * @param event
807 * @param message
808 * @param rmrMessageBuffer
809 * @param params
ss412g3bac2da2020-01-05 11:52:19 +0200810 */
811void handleEinprogressMessages(struct epoll_event &event,
812 ReportingMessages_t &message,
813 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200814 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +0200815 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
816 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
817
818 mdclog_write(MDCLOG_INFO, "file descriptor %d got EPOLLOUT", peerInfo->fileDescriptor);
819 auto retVal = 0;
820 socklen_t retValLen = 0;
821 auto rc = getsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_ERROR, &retVal, &retValLen);
822 if (rc != 0 || retVal != 0) {
823 if (rc != 0) {
824 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
825 "%s|Failed SCTP Connection, after EINPROGRESS the getsockopt%s",
826 peerInfo->enodbName, strerror(errno));
827 } else if (retVal != 0) {
828 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
829 "%s|Failed SCTP Connection after EINPROGRESS, SO_ERROR",
830 peerInfo->enodbName);
831 }
832
833 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
834 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
835 mdclog_write(MDCLOG_ERR, "%s", rmrMessageBuffer.sendMessage->payload);
836 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +0200837 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200838 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
839 }
840 memset(peerInfo->asnData, 0, peerInfo->asnLength);
841 peerInfo->asnLength = 0;
842 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200843 return;
844 }
845
846 peerInfo->isConnected = true;
847
848 if (modifyToEpoll(params->epoll_fd, peerInfo, (EPOLLIN | EPOLLET), params->sctpMap, peerInfo->enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +0200849 peerInfo->mtype) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200850 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_MOD");
ss412g3bac2da2020-01-05 11:52:19 +0200851 return;
852 }
853
854 message.message.asndata = (unsigned char *)peerInfo->asnData;
855 message.message.asnLength = peerInfo->asnLength;
856 message.message.messageType = peerInfo->mtype;
857 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
858 num_of_messages.fetch_add(1, std::memory_order_release);
859 if (mdclog_level_get() >= MDCLOG_DEBUG) {
860 mdclog_write(MDCLOG_DEBUG, "send the delayed SETUP/ENDC SETUP to sctp for %s",
861 message.message.enodbName);
862 }
aa7133@att.combe92d712020-03-18 17:30:39 +0200863 if (sendSctpMsg(peerInfo, message, params->sctpMap) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200864 if (mdclog_level_get() >= MDCLOG_DEBUG) {
865 mdclog_write(MDCLOG_DEBUG, "Error write to SCTP %s %d", __func__, __LINE__);
866 }
ss412g3bac2da2020-01-05 11:52:19 +0200867 return;
868 }
869
870 memset(peerInfo->asnData, 0, peerInfo->asnLength);
871 peerInfo->asnLength = 0;
872 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200873}
874
875
876void handlepoll_error(struct epoll_event &event,
877 ReportingMessages_t &message,
878 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200879 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +0200880 if (event.data.fd != params->rmrListenFd) {
881 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
882 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on fd %d, RAN NAME : %s",
883 event.events, peerInfo->fileDescriptor, peerInfo->enodbName);
884
885 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
886 "%s|Failed SCTP Connection",
887 peerInfo->enodbName);
888 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
889 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
890
891 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
892 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +0200893 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200894 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
895 }
896
897 close(peerInfo->fileDescriptor);
aa7133@att.com7b437f72020-03-30 13:25:54 +0300898 params->sctpMap->erase(peerInfo->enodbName);
aa7133@att.combe92d712020-03-18 17:30:39 +0200899 cleanHashEntry((ConnectedCU_t *) event.data.ptr, params->sctpMap);
ss412g3bac2da2020-01-05 11:52:19 +0200900 } else {
901 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on RMR FD", event.events);
902 }
ss412g3bac2da2020-01-05 11:52:19 +0200903}
904/**
905 *
ss412g1a79bdf2019-10-24 12:03:05 +0300906 * @param socket
907 * @return
908 */
909int setSocketNoBlocking(int socket) {
910 auto flags = fcntl(socket, F_GETFL, 0);
911
912 if (flags == -1) {
913 mdclog_mdc_add("func", "fcntl");
914 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
915 mdclog_mdc_clean();
916 return -1;
917 }
918
919 flags = (unsigned) flags | (unsigned) O_NONBLOCK;
920 if (fcntl(socket, F_SETFL, flags) == -1) {
921 mdclog_mdc_add("func", "fcntl");
922 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
923 mdclog_mdc_clean();
924 return -1;
925 }
926
927 return 0;
928}
929
930/**
931 *
932 * @param val
933 * @param m
ss412g1a79bdf2019-10-24 12:03:05 +0300934 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200935void cleanHashEntry(ConnectedCU_t *val, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +0300936 char *dummy;
937 auto port = (uint16_t) strtol(val->portNumber, &dummy, 10);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200938 char searchBuff[2048]{};
ss412g1a79bdf2019-10-24 12:03:05 +0300939
940 snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", val->hostName, port);
941 m->erase(searchBuff);
942
943 m->erase(val->enodbName);
944 free(val);
ss412g1a79bdf2019-10-24 12:03:05 +0300945}
946
947/**
948 *
949 * @param fd file discriptor
950 * @param data the asn data to send
951 * @param len length of the data
952 * @param enodbName the enodbName as in the map for printing purpose
953 * @param m map host information
954 * @param mtype message number
955 * @return 0 success, anegative number on fail
956 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200957int sendSctpMsg(ConnectedCU_t *peerInfo, ReportingMessages_t &message, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +0300958 auto loglevel = mdclog_level_get();
959 int fd = peerInfo->fileDescriptor;
960 if (loglevel >= MDCLOG_DEBUG) {
961 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for CU %s, %s",
962 message.message.enodbName, __FUNCTION__);
963 }
964
965 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +0300966 if (send(fd,message.message.asndata, message.message.asnLength,MSG_NOSIGNAL) < 0) {
967 if (errno == EINTR) {
968 continue;
969 }
970 mdclog_write(MDCLOG_ERR, "error writing to CU a message, %s ", strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200971 if (!peerInfo->isConnected) {
972 mdclog_write(MDCLOG_ERR, "connection to CU %s is still in progress.", message.message.enodbName);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200973 return -1;
974 }
aa7133@att.combe92d712020-03-18 17:30:39 +0200975 cleanHashEntry(peerInfo, m);
ss412g1a79bdf2019-10-24 12:03:05 +0300976 close(fd);
977 char key[MAX_ENODB_NAME_SIZE * 2];
978 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName,
979 message.message.messageType);
980 if (loglevel >= MDCLOG_DEBUG) {
981 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
982 }
aa7133@att.com96c9b882020-03-25 14:50:40 +0200983 auto tmp = m->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +0300984 if (tmp) {
985 free(tmp);
986 }
987 m->erase(key);
ss412g1a79bdf2019-10-24 12:03:05 +0300988 return -1;
989 }
aa7133@att.com16c799b2020-03-24 18:01:51 +0200990 message.statCollector->incSentMessage(string(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +0300991 message.message.direction = 'D';
992 // send report.buffer of size
993 buildJsonMessage(message);
994
995 if (loglevel >= MDCLOG_DEBUG) {
996 mdclog_write(MDCLOG_DEBUG,
997 "SCTP message for CU %s sent from %s",
998 message.message.enodbName,
999 __FUNCTION__);
1000 }
ss412g1a79bdf2019-10-24 12:03:05 +03001001 return 0;
1002 }
1003}
1004
1005/**
1006 *
1007 * @param message
1008 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001009 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001010void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1011 rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *) (message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001012
1013 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
1014 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
1015
1016 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1017 mdclog_write(MDCLOG_DEBUG, "Message from Xapp RAN name = %s message length = %ld",
1018 message.message.enodbName, (unsigned long) message.message.asnLength);
1019 }
ss412g1a79bdf2019-10-24 12:03:05 +03001020}
1021
1022
ss412g1a79bdf2019-10-24 12:03:05 +03001023
1024/**
1025 *
1026 * @param events
1027 * @param sctpMap
1028 * @param numOfMessages
1029 * @param rmrMessageBuffer
1030 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03001031 * @return
1032 */
1033int receiveDataFromSctp(struct epoll_event *events,
1034 Sctp_Map_t *sctpMap,
1035 int &numOfMessages,
1036 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001037 struct timespec &ts) {
ss412g1a79bdf2019-10-24 12:03:05 +03001038 /* We have data on the fd waiting to be read. Read and display it.
1039 * We must read whatever data is available completely, as we are running
1040 * in edge-triggered mode and won't get a notification again for the same data. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001041 ReportingMessages_t message {};
1042 auto done = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001043 auto loglevel = mdclog_level_get();
aa7133@att.com84bd3342020-03-16 18:04:57 +02001044
ss412g1a79bdf2019-10-24 12:03:05 +03001045 // get the identity of the interface
aa7133@att.com84bd3342020-03-16 18:04:57 +02001046 message.peerInfo = (ConnectedCU_t *)events->data.ptr;
1047
aa7133@att.com16c799b2020-03-24 18:01:51 +02001048 message.statCollector = StatCollector::GetInstance();
ss412g1a79bdf2019-10-24 12:03:05 +03001049 struct timespec start{0, 0};
1050 struct timespec decodestart{0, 0};
1051 struct timespec end{0, 0};
1052
1053 E2AP_PDU_t *pdu = nullptr;
1054
ss412g1a79bdf2019-10-24 12:03:05 +03001055
1056 while (true) {
1057 if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001058 mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP %d fd", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001059 clock_gettime(CLOCK_MONOTONIC, &start);
1060 }
1061 // read the buffer directly to rmr payload
1062 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1063 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
aa7133@att.com84bd3342020-03-16 18:04:57 +02001064 read(message.peerInfo->fileDescriptor, rmrMessageBuffer.sendMessage->payload, RECEIVE_SCTP_BUFFER_SIZE);
1065
ss412g1a79bdf2019-10-24 12:03:05 +03001066 if (loglevel >= MDCLOG_DEBUG) {
1067 mdclog_write(MDCLOG_DEBUG, "Finish Read from SCTP %d fd message length = %ld",
aa7133@att.com96c9b882020-03-25 14:50:40 +02001068 message.peerInfo->fileDescriptor, message.message.asnLength);
ss412g1a79bdf2019-10-24 12:03:05 +03001069 }
aa7133@att.com16c799b2020-03-24 18:01:51 +02001070
aa7133@att.com84bd3342020-03-16 18:04:57 +02001071 memcpy(message.message.enodbName, message.peerInfo->enodbName, sizeof(message.peerInfo->enodbName));
aa7133@att.com16c799b2020-03-24 18:01:51 +02001072 message.statCollector->incRecvMessage(string(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001073 message.message.direction = 'U';
1074 message.message.time.tv_nsec = ts.tv_nsec;
1075 message.message.time.tv_sec = ts.tv_sec;
1076
1077 if (message.message.asnLength < 0) {
1078 if (errno == EINTR) {
1079 continue;
1080 }
1081 /* If errno == EAGAIN, that means we have read all
aa7133@att.com84bd3342020-03-16 18:04:57 +02001082 data. So goReportingMessages_t back to the main loop. */
ss412g1a79bdf2019-10-24 12:03:05 +03001083 if (errno != EAGAIN) {
1084 mdclog_write(MDCLOG_ERR, "Read error, %s ", strerror(errno));
1085 done = 1;
1086 } else if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001087 mdclog_write(MDCLOG_DEBUG, "EAGAIN - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001088 }
1089 break;
1090 } else if (message.message.asnLength == 0) {
1091 /* End of file. The remote has closed the connection. */
1092 if (loglevel >= MDCLOG_INFO) {
1093 mdclog_write(MDCLOG_INFO, "END of File Closed connection - descriptor = %d",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001094 message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001095 }
1096 done = 1;
1097 break;
1098 }
1099
ss412g1a79bdf2019-10-24 12:03:05 +03001100 if (loglevel >= MDCLOG_DEBUG) {
1101 char printBuffer[4096]{};
1102 char *tmp = printBuffer;
1103 for (size_t i = 0; i < (size_t)message.message.asnLength; ++i) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001104 snprintf(tmp, 3, "%02x", message.message.asndata[i]);
ss412g1a79bdf2019-10-24 12:03:05 +03001105 tmp += 2;
1106 }
1107 printBuffer[message.message.asnLength] = 0;
1108 clock_gettime(CLOCK_MONOTONIC, &end);
1109 mdclog_write(MDCLOG_DEBUG, "Before Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001110 message.peerInfo->enodbName, end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001111 mdclog_write(MDCLOG_DEBUG, "PDU buffer length = %ld, data = : %s", message.message.asnLength,
1112 printBuffer);
1113 clock_gettime(CLOCK_MONOTONIC, &decodestart);
1114 }
1115
aa7133@att.com7b437f72020-03-30 13:25:54 +03001116 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
ss412g1a79bdf2019-10-24 12:03:05 +03001117 message.message.asndata, message.message.asnLength);
1118 if (rval.code != RC_OK) {
1119 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from RAN : %s", rval.code,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001120 message.peerInfo->enodbName);
aa7133@att.com9dff4f02020-03-29 09:55:22 +03001121 //todo may need reset to pdu
ss412g1a79bdf2019-10-24 12:03:05 +03001122 break;
1123 }
1124
1125 if (loglevel >= MDCLOG_DEBUG) {
1126 clock_gettime(CLOCK_MONOTONIC, &end);
1127 mdclog_write(MDCLOG_DEBUG, "After Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001128 message.peerInfo->enodbName, end.tv_sec - decodestart.tv_sec, end.tv_nsec - decodestart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001129 char *printBuffer;
1130 size_t size;
1131 FILE *stream = open_memstream(&printBuffer, &size);
1132 asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu);
1133 mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer);
1134 clock_gettime(CLOCK_MONOTONIC, &decodestart);
1135 }
1136
1137 switch (pdu->present) {
1138 case E2AP_PDU_PR_initiatingMessage: {//initiating message
aa7133@att.com7b437f72020-03-30 13:25:54 +03001139 asnInitiatingRequest(pdu, sctpMap,message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001140 break;
1141 }
1142 case E2AP_PDU_PR_successfulOutcome: { //successful outcome
aa7133@att.com7b437f72020-03-30 13:25:54 +03001143 asnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001144 break;
1145 }
1146 case E2AP_PDU_PR_unsuccessfulOutcome: { //Unsuccessful Outcome
aa7133@att.com7b437f72020-03-30 13:25:54 +03001147 asnUnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001148 break;
1149 }
1150 default:
1151 mdclog_write(MDCLOG_ERR, "Unknown index %d in E2AP PDU", pdu->present);
1152 break;
1153 }
1154 if (loglevel >= MDCLOG_DEBUG) {
1155 clock_gettime(CLOCK_MONOTONIC, &end);
1156 mdclog_write(MDCLOG_DEBUG,
1157 "After processing message and sent to rmr for : %s, Read time is : %ld seconds, %ld nanoseconds",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001158 message.peerInfo->enodbName, end.tv_sec - decodestart.tv_sec, end.tv_nsec - decodestart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001159 }
1160 numOfMessages++;
1161 // remove the break for EAGAIN
1162 //break;
1163 if (pdu != nullptr) {
1164 //TODO need to test ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu); to get better performance
aa7133@att.com1fecb052020-03-29 09:44:09 +03001165 ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
1166 //ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
aa7133@att.com9dff4f02020-03-29 09:55:22 +03001167 //pdu = nullptr;
ss412g1a79bdf2019-10-24 12:03:05 +03001168 }
1169 //clock_gettime(CLOCK_MONOTONIC, &start);
1170 }
1171 // in case of break to avoid memory leak
aa7133@att.com9dff4f02020-03-29 09:55:22 +03001172// if (pdu != nullptr) {
1173// //ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1174// ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
1175// //pdu = nullptr;
1176// }
ss412g1a79bdf2019-10-24 12:03:05 +03001177
1178 if (done) {
1179 if (loglevel >= MDCLOG_INFO) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001180 mdclog_write(MDCLOG_INFO, "Closed connection - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001181 }
1182 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1183 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02001184 256,
1185 "%s|CU disconnected unexpectedly",
1186 message.peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001187 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1188
1189 if (sendRequestToXapp(message,
1190 RIC_SCTP_CONNECTION_FAILURE,
aa7133@att.combe92d712020-03-18 17:30:39 +02001191 rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001192 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1193 }
1194
1195 /* Closing descriptor make epoll remove it from the set of descriptors which are monitored. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001196 close(message.peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02001197 cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03001198 }
1199 if (loglevel >= MDCLOG_DEBUG) {
1200 clock_gettime(CLOCK_MONOTONIC, &end);
1201 mdclog_write(MDCLOG_DEBUG, "from receive SCTP to send RMR time is %ld seconds and %ld nanoseconds",
1202 end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1203
1204 }
ss412g1a79bdf2019-10-24 12:03:05 +03001205 return 0;
1206}
1207
aa7133@att.com84bd3342020-03-16 18:04:57 +02001208static void buildAndsendSetupRequest(ReportingMessages_t &message,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001209 RmrMessagesBuffer_t &rmrMessageBuffer,
1210 E2AP_PDU_t *pdu) {
1211 auto logLevel = mdclog_level_get();
1212
aa7133@att.com84bd3342020-03-16 18:04:57 +02001213 // now we can send the data to e2Mgr
aa7133@att.com96c9b882020-03-25 14:50:40 +02001214 auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001215
1216 auto *rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size);
1217 // add addrees to message
aa7133@att.com84bd3342020-03-16 18:04:57 +02001218
1219
aa7133@att.combc110772020-03-25 09:45:14 +02001220 // unsigned char *buffer = &rmrMsg->payload[j];
aa7133@att.com96c9b882020-03-25 14:50:40 +02001221 unsigned char buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
aa7133@att.com84bd3342020-03-16 18:04:57 +02001222 // encode to xml
aa7133@att.com1fecb052020-03-29 09:44:09 +03001223 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001224 if (er.encoded == -1) {
1225 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1226 } else if (er.encoded > (ssize_t) buffer_size) {
1227 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s",
1228 (int) buffer_size,
1229 asn_DEF_E2AP_PDU.name);
1230 } else {
aa7133@att.com96c9b882020-03-25 14:50:40 +02001231 rmrMsg->len = snprintf((char *)rmrMsg->payload, RECEIVE_SCTP_BUFFER_SIZE * 2, "%s:%d|%s",
1232 message.peerInfo->sctpParams->myIP.c_str(),
1233 message.peerInfo->sctpParams->rmrPort,
1234 buffer);
1235 if (logLevel >= MDCLOG_DEBUG) {
1236 mdclog_write(MDCLOG_DEBUG, "Setup request of size %d :\n %s\n", rmrMsg->len, rmrMsg->payload);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001237 }
aa7133@att.com11b17d42020-03-22 11:35:16 +02001238 // send to RMR
aa7133@att.combe92d712020-03-18 17:30:39 +02001239 message.message.messageType = rmrMsg->mtype = RIC_E2_SETUP_REQ;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001240 rmrMsg->state = 0;
1241 rmr_bytes2meid(rmrMsg, (unsigned char *) message.message.enodbName, strlen(message.message.enodbName));
1242
1243 static unsigned char tx[32];
1244 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1245 rmr_bytes2xact(rmrMsg, tx, strlen((const char *) tx));
1246
1247 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1248 if (rmrMsg == nullptr) {
1249 mdclog_write(MDCLOG_ERR, "RMR failed to send returned nullptr");
1250 } else if (rmrMsg->state != 0) {
1251 char meid[RMR_MAX_MEID]{};
1252 if (rmrMsg->state == RMR_ERR_RETRY) {
1253 usleep(5);
1254 rmrMsg->state = 0;
1255 mdclog_write(MDCLOG_INFO, "RETRY sending Message %d to Xapp from %s",
1256 rmrMsg->mtype, rmr_get_meid(rmrMsg, (unsigned char *) meid));
1257 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1258 if (rmrMsg == nullptr) {
1259 mdclog_write(MDCLOG_ERR, "RMR failed send returned nullptr");
1260 } else if (rmrMsg->state != 0) {
1261 mdclog_write(MDCLOG_ERR,
1262 "RMR Retry failed %s sending request %d to Xapp from %s",
1263 translateRmrErrorMessages(rmrMsg->state).c_str(),
1264 rmrMsg->mtype,
1265 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1266 }
1267 } else {
1268 mdclog_write(MDCLOG_ERR, "RMR failed: %s. sending request %d to Xapp from %s",
1269 translateRmrErrorMessages(rmrMsg->state).c_str(),
1270 rmrMsg->mtype,
1271 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1272 }
1273 }
1274 message.peerInfo->gotSetup = true;
1275 buildJsonMessage(message);
aa7133@att.combc110772020-03-25 09:45:14 +02001276 if (rmrMsg != nullptr) {
1277 rmr_free_msg(rmrMsg);
1278 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001279 }
1280
1281}
ss412g1a79bdf2019-10-24 12:03:05 +03001282/**
1283 *
1284 * @param pdu
1285 * @param message
1286 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001287 */
1288void asnInitiatingRequest(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001289 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03001290 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02001291 RmrMessagesBuffer_t &rmrMessageBuffer) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001292 auto logLevel = mdclog_level_get();
ss412g1a79bdf2019-10-24 12:03:05 +03001293 auto procedureCode = ((InitiatingMessage_t *) pdu->choice.initiatingMessage)->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001294 if (logLevel >= MDCLOG_DEBUG) {
1295 mdclog_write(MDCLOG_DEBUG, "Initiating message %ld\n", procedureCode);
ss412g1a79bdf2019-10-24 12:03:05 +03001296 }
1297 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001298 case ProcedureCode_id_E2setup: {
1299 if (logLevel >= MDCLOG_DEBUG) {
1300 mdclog_write(MDCLOG_DEBUG, "Got E2setup\n");
1301 }
1302
aa7133@att.com7b437f72020-03-30 13:25:54 +03001303 // first get the message as XML buffer
1304 auto setup_xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1305 unsigned char setup_xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
1306 //unsigned char *tmp_buff_cursor = setup_xml_buffer;
1307
1308 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, setup_xml_buffer, setup_xml_buffer_size);
1309 if (er.encoded == -1) {
1310 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1311 } else if (er.encoded > (ssize_t) setup_xml_buffer_size) {
1312 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s",
1313 (int)setup_xml_buffer_size,
1314 asn_DEF_E2AP_PDU.name);
1315 }
1316 std::string xmlString(setup_xml_buffer_size, setup_xml_buffer_size + er.encoded);
1317
1318 auto failed = false;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001319 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1320 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
1321 auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i];
1322 if (ie->id == ProtocolIE_ID_id_GlobalE2node_ID) {
aa7133@att.com7b437f72020-03-30 13:25:54 +03001323 // get the ran name for meid
aa7133@att.com84bd3342020-03-16 18:04:57 +02001324 if (ie->value.present == E2setupRequestIEs__value_PR_GlobalE2node_ID) {
aa7133@att.com7b437f72020-03-30 13:25:54 +03001325 if (buildRanName(message.peerInfo->enodbName, ie) < 0) {
1326 mdclog_write(MDCLOG_ERR, "Bad param in E2setupRequestIEs GlobalE2node_ID.\n");
1327 // no mesage will be sent
1328 break;
1329 }
1330 memcpy(message.message.enodbName, message.peerInfo->enodbName, strlen(message.peerInfo->enodbName));
1331 sctpMap->setkey(message.message.enodbName, message.peerInfo);
1332 }
1333 }
1334 // reformat RANFUNCTION Definition to XML
1335 if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
1336 if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
1337 for (auto j = 0; i < ie->value.choice.RANfunctions_List.list.count; i++) {
1338 auto *raNfunctionItemIEs = (RANfunction_ItemIEs_t *)ie->value.choice.RANfunctions_List.list.array[j];
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03001339 if (raNfunctionItemIEs->id == ProtocolIE_ID_id_RANfunction_Item &&
1340 (raNfunctionItemIEs->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item)) {
aa7133@att.com7b437f72020-03-30 13:25:54 +03001341 // encode to xml
1342 E2SM_gNB_NRT_RANfunction_Definition_t *ranFunDef = nullptr;
1343 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER,
1344 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1345 (void **)&ranFunDef,
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03001346 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.buf,
1347 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.size);
aa7133@att.com7b437f72020-03-30 13:25:54 +03001348 if (rval.code != RC_OK) {
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03001349 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2SM message from : %s",
aa7133@att.com7b437f72020-03-30 13:25:54 +03001350 rval.code,
1351 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name);
1352 failed = true;
1353 break;
1354 }
1355
1356 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1357 char *printBuffer;
1358 size_t size;
1359 FILE *stream = open_memstream(&printBuffer, &size);
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03001360 asn_fprint(stream, &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition, pdu);
aa7133@att.com7b437f72020-03-30 13:25:54 +03001361 mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer);
1362 }
1363 auto xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1364 unsigned char xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
1365 // encode to xml
1366 er = asn_encode_to_buffer(nullptr,
1367 ATS_BASIC_XER,
1368 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1369 ranFunDef,
1370 xml_buffer,
1371 xml_buffer_size);
1372 if (er.encoded == -1) {
1373 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s",
1374 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1375 strerror(errno));
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03001376 } else if (er.encoded > (ssize_t)xml_buffer_size) {
aa7133@att.com7b437f72020-03-30 13:25:54 +03001377 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s",
1378 (int) xml_buffer_size,
1379 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name);
1380 } else {
1381 // we have the xml
1382 }
1383
1384 }
1385 }
1386 if (failed) {
1387 break;
1388 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001389 }
1390 }
ss412g1a79bdf2019-10-24 12:03:05 +03001391 }
aa7133@att.com7b437f72020-03-30 13:25:54 +03001392 if (failed) {
1393 break;
1394 }
1395
1396 //build all parts and send the XML (need to copy the XML with the header to the rmrMessageBuffer payload
1397 //TODO replace with new function
1398 buildAndsendSetupRequest(message, rmrMessageBuffer, pdu);
ss412g1a79bdf2019-10-24 12:03:05 +03001399 break;
1400 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001401 case ProcedureCode_id_ErrorIndication: {
1402 if (logLevel >= MDCLOG_DEBUG) {
1403 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
1404 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001405 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001406 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001407 }
1408 break;
1409 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001410 case ProcedureCode_id_Reset: {
1411 if (logLevel >= MDCLOG_DEBUG) {
1412 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001413 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001414 if (sendRequestToXapp(message, RIC_X2_RESET, rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001415 mdclog_write(MDCLOG_ERR, "RIC_X2_RESET message failed to send to xAPP");
1416 }
1417 break;
1418 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001419 case ProcedureCode_id_RICcontrol: {
1420 if (logLevel >= MDCLOG_DEBUG) {
1421 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
1422 }
1423 break;
1424 }
1425 case ProcedureCode_id_RICindication: {
1426 if (logLevel >= MDCLOG_DEBUG) {
1427 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1428 }
1429 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
ss412g1a79bdf2019-10-24 12:03:05 +03001430 auto messageSent = false;
1431 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02001432 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001433 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1434 }
1435 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001436 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001437 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1438 }
1439 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1440 static unsigned char tx[32];
1441 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1442 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1443 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02001444 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001445 (unsigned char *)message.message.enodbName,
1446 strlen(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001447 rmrMessageBuffer.sendMessage->state = 0;
1448 rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
ss412g3bac2da2020-01-05 11:52:19 +02001449 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1450 mdclog_write(MDCLOG_DEBUG, "RIC sub id = %d, message type = %d",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001451 rmrMessageBuffer.sendMessage->sub_id,
1452 rmrMessageBuffer.sendMessage->mtype);
ss412g3bac2da2020-01-05 11:52:19 +02001453 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001454 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001455 messageSent = true;
1456 } else {
1457 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1458 }
1459 }
1460 if (messageSent) {
1461 break;
1462 }
1463 }
1464 break;
1465 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001466 case ProcedureCode_id_RICserviceQuery: {
1467 if (logLevel >= MDCLOG_DEBUG) {
1468 mdclog_write(MDCLOG_DEBUG, "Got RICserviceQuery %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001469 }
1470 break;
1471 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001472 case ProcedureCode_id_RICserviceUpdate: {
1473 if (logLevel >= MDCLOG_DEBUG) {
1474 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1475 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001476 if (sendRequestToXapp(message, RIC_SERVICE_UPDATE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001477 mdclog_write(MDCLOG_ERR, "RIC_SERVICE_UPDATE message failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001478 }
1479 break;
1480 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001481 case ProcedureCode_id_RICsubscription: {
1482 if (logLevel >= MDCLOG_DEBUG) {
1483 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1484 }
1485 break;
1486 }
1487 case ProcedureCode_id_RICsubscriptionDelete: {
1488 if (logLevel >= MDCLOG_DEBUG) {
1489 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001490 }
1491 break;
1492 }
1493 default: {
1494 mdclog_write(MDCLOG_ERR, "Undefined or not supported message = %ld", procedureCode);
1495 message.message.messageType = 0; // no RMR message type yet
1496
1497 buildJsonMessage(message);
1498
1499 break;
1500 }
1501 }
ss412g1a79bdf2019-10-24 12:03:05 +03001502}
1503
1504/**
1505 *
1506 * @param pdu
1507 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03001508 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001509 */
aa7133@att.com7b437f72020-03-30 13:25:54 +03001510void asnSuccsesfulMsg(E2AP_PDU_t *pdu,
1511 Sctp_Map_t *sctpMap,
1512 ReportingMessages_t &message,
1513 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001514 auto procedureCode = pdu->choice.successfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001515 auto logLevel = mdclog_level_get();
1516 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03001517 mdclog_write(MDCLOG_INFO, "Successful Outcome %ld", procedureCode);
1518 }
1519 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001520 case ProcedureCode_id_E2setup: {
1521 if (logLevel >= MDCLOG_DEBUG) {
1522 mdclog_write(MDCLOG_DEBUG, "Got E2setup\n");
ss412g1a79bdf2019-10-24 12:03:05 +03001523 }
1524 break;
1525 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001526 case ProcedureCode_id_ErrorIndication: {
1527 if (logLevel >= MDCLOG_DEBUG) {
1528 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001529 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001530 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001531 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001532 }
1533 break;
1534 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001535 case ProcedureCode_id_Reset: {
1536 if (logLevel >= MDCLOG_DEBUG) {
1537 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001538 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001539 if (sendRequestToXapp(message, RIC_X2_RESET, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001540 mdclog_write(MDCLOG_ERR, "RIC_X2_RESET message failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001541 }
1542 break;
1543 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001544 case ProcedureCode_id_RICcontrol: {
1545 if (logLevel >= MDCLOG_DEBUG) {
1546 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001547 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001548 for (auto i = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001549 i < pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.count; i++) {
1550 auto messageSent = false;
1551 RICcontrolAcknowledge_IEs_t *ie = pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.array[i];
1552 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1553 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1554 }
1555 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1556 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1557 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1558 }
1559 if (ie->value.present == RICcontrolAcknowledge_IEs__value_PR_RICrequestID) {
1560 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_ACK;
1561 rmrMessageBuffer.sendMessage->state = 0;
1562 rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1563 static unsigned char tx[32];
1564 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1565 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02001566 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001567 (unsigned char *)message.message.enodbName,
1568 strlen(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001569
aa7133@att.combe92d712020-03-18 17:30:39 +02001570 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001571 messageSent = true;
1572 } else {
1573 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1574 }
1575 }
1576 if (messageSent) {
1577 break;
1578 }
1579 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001580
1581 break;
1582 }
1583 case ProcedureCode_id_RICindication: {
1584 if (logLevel >= MDCLOG_DEBUG) {
1585 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1586 }
1587 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
1588 auto messageSent = false;
1589 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
1590 if (logLevel >= MDCLOG_DEBUG) {
1591 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1592 }
1593 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1594 if (logLevel >= MDCLOG_DEBUG) {
1595 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1596 }
1597 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1598 static unsigned char tx[32];
1599 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1600 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1601 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
1602 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
1603 (unsigned char *)message.message.enodbName,
1604 strlen(message.message.enodbName));
1605 rmrMessageBuffer.sendMessage->state = 0;
1606 rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1607 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1608 mdclog_write(MDCLOG_DEBUG, "RIC sub id = %d, message type = %d",
1609 rmrMessageBuffer.sendMessage->sub_id,
1610 rmrMessageBuffer.sendMessage->mtype);
1611 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001612 sendRmrMessage(rmrMessageBuffer, message);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001613 messageSent = true;
1614 } else {
1615 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1616 }
1617 }
1618 if (messageSent) {
1619 break;
1620 }
1621 }
1622 break;
1623 }
1624 case ProcedureCode_id_RICserviceQuery: {
1625 if (logLevel >= MDCLOG_DEBUG) {
1626 mdclog_write(MDCLOG_DEBUG, "Got RICserviceQuery %s", message.message.enodbName);
1627 }
1628 break;
1629 }
1630 case ProcedureCode_id_RICserviceUpdate: {
1631 if (logLevel >= MDCLOG_DEBUG) {
1632 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1633 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001634 if (sendRequestToXapp(message, RIC_SERVICE_UPDATE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001635 mdclog_write(MDCLOG_ERR, "RIC_SERVICE_UPDATE message failed to send to xAPP");
1636 }
1637 break;
1638 }
1639 case ProcedureCode_id_RICsubscription: {
1640 if (logLevel >= MDCLOG_DEBUG) {
1641 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1642 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001643 if (sendRequestToXapp(message, RIC_SUB_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001644 mdclog_write(MDCLOG_ERR, "Subscription successful message failed to send to xAPP");
1645 }
1646 break;
1647 }
1648 case ProcedureCode_id_RICsubscriptionDelete: {
1649 if (logLevel >= MDCLOG_DEBUG) {
1650 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1651 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001652 if (sendRequestToXapp(message, RIC_SUB_DEL_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001653 mdclog_write(MDCLOG_ERR, "Subscription delete successful message failed to send to xAPP");
1654 }
ss412g1a79bdf2019-10-24 12:03:05 +03001655 break;
1656 }
1657 default: {
1658 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1659 message.message.messageType = 0; // no RMR message type yet
1660 buildJsonMessage(message);
1661
1662 break;
1663 }
1664 }
ss412g1a79bdf2019-10-24 12:03:05 +03001665}
1666
1667/**
1668 *
1669 * @param pdu
1670 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03001671 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001672 */
1673void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001674 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03001675 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02001676 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001677 auto procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001678 auto logLevel = mdclog_level_get();
1679 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03001680 mdclog_write(MDCLOG_INFO, "Unsuccessful Outcome %ld", procedureCode);
1681 }
1682 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001683 case ProcedureCode_id_E2setup: {
1684 if (logLevel >= MDCLOG_DEBUG) {
1685 mdclog_write(MDCLOG_DEBUG, "Got E2setup\n");
ss412g1a79bdf2019-10-24 12:03:05 +03001686 }
1687 break;
1688 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001689 case ProcedureCode_id_ErrorIndication: {
1690 if (logLevel >= MDCLOG_DEBUG) {
1691 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001692 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001693 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001694 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001695 }
1696 break;
1697 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001698 case ProcedureCode_id_Reset: {
1699 if (logLevel >= MDCLOG_DEBUG) {
1700 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
1701 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001702 if (sendRequestToXapp(message, RIC_X2_RESET, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001703 mdclog_write(MDCLOG_ERR, "RIC_X2_RESET message failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001704 }
1705 break;
1706 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001707 case ProcedureCode_id_RICcontrol: {
1708 if (logLevel >= MDCLOG_DEBUG) {
1709 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001710 }
1711 for (int i = 0;
1712 i < pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.count; i++) {
1713 auto messageSent = false;
1714 RICcontrolFailure_IEs_t *ie = pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02001715 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001716 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1717 }
1718 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001719 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001720 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1721 }
1722 if (ie->value.present == RICcontrolFailure_IEs__value_PR_RICrequestID) {
1723 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_FAILURE;
1724 rmrMessageBuffer.sendMessage->state = 0;
1725 rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1726 static unsigned char tx[32];
1727 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1728 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001729 rmr_bytes2meid(rmrMessageBuffer.sendMessage, (unsigned char *) message.message.enodbName,
1730 strlen(message.message.enodbName));
aa7133@att.combe92d712020-03-18 17:30:39 +02001731 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001732 messageSent = true;
1733 } else {
1734 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1735 }
1736 }
1737 if (messageSent) {
1738 break;
1739 }
1740 }
1741 break;
1742 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001743 case ProcedureCode_id_RICindication: {
1744 if (logLevel >= MDCLOG_DEBUG) {
1745 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1746 }
1747 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
1748 auto messageSent = false;
1749 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
1750 if (logLevel >= MDCLOG_DEBUG) {
1751 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1752 }
1753 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1754 if (logLevel >= MDCLOG_DEBUG) {
1755 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1756 }
1757 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1758 static unsigned char tx[32];
1759 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1760 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1761 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
1762 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
1763 (unsigned char *)message.message.enodbName,
1764 strlen(message.message.enodbName));
1765 rmrMessageBuffer.sendMessage->state = 0;
1766 rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1767 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1768 mdclog_write(MDCLOG_DEBUG, "RIC sub id = %d, message type = %d",
1769 rmrMessageBuffer.sendMessage->sub_id,
1770 rmrMessageBuffer.sendMessage->mtype);
1771 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001772 sendRmrMessage(rmrMessageBuffer, message);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001773 messageSent = true;
1774 } else {
1775 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1776 }
1777 }
1778 if (messageSent) {
1779 break;
1780 }
1781 }
1782 break;
1783 }
1784 case ProcedureCode_id_RICserviceQuery: {
1785 if (logLevel >= MDCLOG_DEBUG) {
1786 mdclog_write(MDCLOG_DEBUG, "Got RICserviceQuery %s", message.message.enodbName);
1787 }
1788 break;
1789 }
1790 case ProcedureCode_id_RICserviceUpdate: {
1791 if (logLevel >= MDCLOG_DEBUG) {
1792 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1793 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001794 if (sendRequestToXapp(message, RIC_SERVICE_UPDATE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001795 mdclog_write(MDCLOG_ERR, "RIC_SERVICE_UPDATE message failed to send to xAPP");
1796 }
1797 break;
1798 }
1799 case ProcedureCode_id_RICsubscription: {
1800 if (logLevel >= MDCLOG_DEBUG) {
1801 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1802 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001803 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001804 mdclog_write(MDCLOG_ERR, "Subscription unsuccessful message failed to send to xAPP");
1805 }
1806 break;
1807 }
1808 case ProcedureCode_id_RICsubscriptionDelete: {
1809 if (logLevel >= MDCLOG_DEBUG) {
1810 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1811 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001812 if (sendRequestToXapp(message, RIC_SUB_DEL_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001813 mdclog_write(MDCLOG_ERR, "Subscription Delete unsuccessful message failed to send to xAPP");
1814 }
1815 break;
1816 }
ss412g1a79bdf2019-10-24 12:03:05 +03001817 default: {
1818 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1819 message.message.messageType = 0; // no RMR message type yet
1820
1821 buildJsonMessage(message);
1822
1823 break;
1824 }
1825 }
ss412g1a79bdf2019-10-24 12:03:05 +03001826}
1827
1828/**
1829 *
1830 * @param message
1831 * @param requestId
1832 * @param rmrMmessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001833 * @return
1834 */
1835int sendRequestToXapp(ReportingMessages_t &message,
1836 int requestId,
aa7133@att.combe92d712020-03-18 17:30:39 +02001837 RmrMessagesBuffer_t &rmrMmessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001838 rmr_bytes2meid(rmrMmessageBuffer.sendMessage,
1839 (unsigned char *)message.message.enodbName,
1840 strlen(message.message.enodbName));
1841 message.message.messageType = rmrMmessageBuffer.sendMessage->mtype = requestId;
1842 rmrMmessageBuffer.sendMessage->state = 0;
1843 static unsigned char tx[32];
1844 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1845 rmr_bytes2xact(rmrMmessageBuffer.sendMessage, tx, strlen((const char *) tx));
1846
aa7133@att.combe92d712020-03-18 17:30:39 +02001847 auto rc = sendRmrMessage(rmrMmessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001848 return rc;
1849}
1850
1851
aa7133@att.combe92d712020-03-18 17:30:39 +02001852void getRmrContext(sctp_params_t &pSctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +02001853 pSctpParams.rmrCtx = nullptr;
1854 pSctpParams.rmrCtx = rmr_init(pSctpParams.rmrAddress, RMR_MAX_RCV_BYTES, RMRFL_NONE);
1855 if (pSctpParams.rmrCtx == nullptr) {
1856 mdclog_write(MDCLOG_ERR, "Failed to initialize RMR");
ss412g3bac2da2020-01-05 11:52:19 +02001857 return;
ss412g1a79bdf2019-10-24 12:03:05 +03001858 }
1859
ss412g3bac2da2020-01-05 11:52:19 +02001860 rmr_set_stimeout(pSctpParams.rmrCtx, 0); // disable retries for any send operation
ss412g1a79bdf2019-10-24 12:03:05 +03001861 // we need to find that routing table exist and we can run
1862 if (mdclog_level_get() >= MDCLOG_INFO) {
1863 mdclog_write(MDCLOG_INFO, "We are after RMR INIT wait for RMR_Ready");
1864 }
1865 int rmrReady = 0;
1866 int count = 0;
1867 while (!rmrReady) {
ss412g3bac2da2020-01-05 11:52:19 +02001868 if ((rmrReady = rmr_ready(pSctpParams.rmrCtx)) == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001869 sleep(1);
1870 }
1871 count++;
1872 if (count % 60 == 0) {
1873 mdclog_write(MDCLOG_INFO, "waiting to RMR ready state for %d seconds", count);
1874 }
1875 }
1876 if (mdclog_level_get() >= MDCLOG_INFO) {
1877 mdclog_write(MDCLOG_INFO, "RMR running");
1878 }
ss412g3bac2da2020-01-05 11:52:19 +02001879 rmr_init_trace(pSctpParams.rmrCtx, 200);
1880 // get the RMR fd for the epoll
1881 pSctpParams.rmrListenFd = rmr_get_rcvfd(pSctpParams.rmrCtx);
1882 struct epoll_event event{};
1883 // add RMR fd to epoll
1884 event.events = (EPOLLIN);
1885 event.data.fd = pSctpParams.rmrListenFd;
1886 // add listening RMR FD to epoll
1887 if (epoll_ctl(pSctpParams.epoll_fd, EPOLL_CTL_ADD, pSctpParams.rmrListenFd, &event)) {
1888 mdclog_write(MDCLOG_ERR, "Failed to add RMR descriptor to epoll");
1889 close(pSctpParams.rmrListenFd);
1890 rmr_close(pSctpParams.rmrCtx);
1891 pSctpParams.rmrCtx = nullptr;
1892 }
ss412g1a79bdf2019-10-24 12:03:05 +03001893}
1894
aa7133@att.com1fecb052020-03-29 09:44:09 +03001895int BuildPERSetupResponseMessaeFromXML(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1896 E2AP_PDU_t *pdu;
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03001897
1898 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1899 mdclog_write(MDCLOG_DEBUG, "got xml setup response \n %s\n", rmrMessageBuffer.rcvMessage->payload);
1900 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03001901 auto rval = asn_decode(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1902 rmrMessageBuffer.rcvMessage->payload, rmrMessageBuffer.rcvMessage->len);
1903 if (rval.code != RC_OK) {
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03001904 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
aa7133@att.com1fecb052020-03-29 09:44:09 +03001905 rval.code,
1906 message.message.enodbName);
1907 return -1;
1908 }
1909
1910 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu,
1911 rmrMessageBuffer.rcvMessage->payload, rmrMessageBuffer.rcvMessage->len);
1912 if (er.encoded == -1) {
1913 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1914 return -1;
1915 } else if (er.encoded > (ssize_t)rmrMessageBuffer.rcvMessage->len) {
1916 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s",
1917 (int)rmrMessageBuffer.rcvMessage->len,
1918 asn_DEF_E2AP_PDU.name);
1919 return -1;
1920 }
1921 return 0;
1922}
1923
ss412g1a79bdf2019-10-24 12:03:05 +03001924/**
1925 *
ss412g1a79bdf2019-10-24 12:03:05 +03001926 * @param sctpMap
1927 * @param rmrMessageBuffer
1928 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03001929 * @return
1930 */
aa7133@att.com6680c3b2020-03-22 12:06:58 +02001931int receiveXappMessages(Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03001932 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001933 struct timespec &ts) {
ss412g1a79bdf2019-10-24 12:03:05 +03001934 if (rmrMessageBuffer.rcvMessage == nullptr) {
1935 //we have error
1936 mdclog_write(MDCLOG_ERR, "RMR Allocation message, %s", strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +03001937 return -1;
1938 }
1939
1940 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1941 mdclog_write(MDCLOG_DEBUG, "Call to rmr_rcv_msg");
1942 }
1943 rmrMessageBuffer.rcvMessage = rmr_rcv_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
1944 if (rmrMessageBuffer.rcvMessage == nullptr) {
1945 mdclog_write(MDCLOG_ERR, "RMR Receving message with null pointer, Realloc rmr mesage buffer");
1946 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03001947 return -2;
1948 }
1949 ReportingMessages_t message;
1950 message.message.direction = 'D';
1951 message.message.time.tv_nsec = ts.tv_nsec;
1952 message.message.time.tv_sec = ts.tv_sec;
1953
1954 // get message payload
1955 //auto msgData = msg->payload;
1956 if (rmrMessageBuffer.rcvMessage->state != 0) {
1957 mdclog_write(MDCLOG_ERR, "RMR Receving message with stat = %d", rmrMessageBuffer.rcvMessage->state);
ss412g1a79bdf2019-10-24 12:03:05 +03001958 return -1;
1959 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03001960 rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *)message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001961 switch (rmrMessageBuffer.rcvMessage->mtype) {
aa7133@att.combe92d712020-03-18 17:30:39 +02001962 case RIC_E2_SETUP_RESP : {
aa7133@att.com1fecb052020-03-29 09:44:09 +03001963 if (BuildPERSetupResponseMessaeFromXML(message, rmrMessageBuffer) != 0) {
1964 break;
1965 }
1966
aa7133@att.combe92d712020-03-18 17:30:39 +02001967 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
1968 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_RESP");
1969 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03001970 }
1971 break;
1972 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001973 case RIC_E2_SETUP_FAILURE : {
aa7133@att.com1fecb052020-03-29 09:44:09 +03001974 if (BuildPERSetupResponseMessaeFromXML(message, rmrMessageBuffer) != 0) {
1975 break;
1976 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001977 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
1978 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_FAILURE");
1979 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03001980 }
1981 break;
1982 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001983 case RIC_ERROR_INDICATION: {
1984 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
1985 mdclog_write(MDCLOG_ERR, "Failed to send RIC_ERROR_INDICATION");
ss412g1a79bdf2019-10-24 12:03:05 +03001986 return -6;
1987 }
1988 break;
1989 }
1990 case RIC_SUB_REQ: {
aa7133@att.combe92d712020-03-18 17:30:39 +02001991 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001992 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03001993 return -6;
1994 }
1995 break;
1996 }
1997 case RIC_SUB_DEL_REQ: {
aa7133@att.combe92d712020-03-18 17:30:39 +02001998 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001999 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_DEL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002000 return -6;
2001 }
2002 break;
2003 }
2004 case RIC_CONTROL_REQ: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002005 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002006 mdclog_write(MDCLOG_ERR, "Failed to send RIC_CONTROL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002007 return -6;
2008 }
2009 break;
2010 }
2011 case RIC_SERVICE_QUERY: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002012 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002013 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_QUERY");
ss412g1a79bdf2019-10-24 12:03:05 +03002014 return -6;
2015 }
2016 break;
2017 }
2018 case RIC_SERVICE_UPDATE_ACK: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002019 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002020 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_ACK");
ss412g1a79bdf2019-10-24 12:03:05 +03002021 return -6;
2022 }
2023 break;
2024 }
2025 case RIC_SERVICE_UPDATE_FAILURE: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002026 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002027 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_FAILURE");
ss412g1a79bdf2019-10-24 12:03:05 +03002028 return -6;
2029 }
2030 break;
2031 }
2032 case RIC_X2_RESET: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002033 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002034 mdclog_write(MDCLOG_ERR, "Failed to send RIC_X2_RESET");
ss412g1a79bdf2019-10-24 12:03:05 +03002035 return -6;
2036 }
2037 break;
2038 }
2039 case RIC_X2_RESET_RESP: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002040 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002041 mdclog_write(MDCLOG_ERR, "Failed to send RIC_X2_RESET_RESP");
ss412g1a79bdf2019-10-24 12:03:05 +03002042 return -6;
2043 }
2044 break;
2045 }
2046 case RIC_SCTP_CLEAR_ALL: {
2047 mdclog_write(MDCLOG_INFO, "RIC_SCTP_CLEAR_ALL");
2048 // loop on all keys and close socket and then erase all map.
2049 vector<char *> v;
2050 sctpMap->getKeys(v);
2051 for (auto const &iter : v) { //}; iter != sctpMap.end(); iter++) {
2052 if (!boost::starts_with((string) (iter), "host:") && !boost::starts_with((string) (iter), "msg:")) {
2053 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(iter);
2054 if (peerInfo == nullptr) {
2055 continue;
2056 }
2057 close(peerInfo->fileDescriptor);
2058 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
2059 message.message.direction = 'D';
2060 message.message.time.tv_nsec = ts.tv_nsec;
2061 message.message.time.tv_sec = ts.tv_sec;
2062
2063 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
2064 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02002065 256,
2066 "%s|RIC_SCTP_CLEAR_ALL",
2067 peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03002068 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
2069 mdclog_write(MDCLOG_INFO, "%s", message.message.asndata);
aa7133@att.combe92d712020-03-18 17:30:39 +02002070 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002071 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
2072 }
2073 free(peerInfo);
2074 }
2075 }
2076
2077 sleep(1);
2078 sctpMap->clear();
2079 break;
2080 }
ss412g3bac2da2020-01-05 11:52:19 +02002081 case E2_TERM_KEEP_ALIVE_REQ: {
2082 // send message back
ss412g3bac2da2020-01-05 11:52:19 +02002083 rmr_bytes2payload(rmrMessageBuffer.sendMessage,
aa7133@att.com96c9b882020-03-25 14:50:40 +02002084 (unsigned char *)rmrMessageBuffer.ka_message,
2085 rmrMessageBuffer.ka_message_len);
ss412g3bac2da2020-01-05 11:52:19 +02002086 rmrMessageBuffer.sendMessage->mtype = E2_TERM_KEEP_ALIVE_RESP;
2087 rmrMessageBuffer.sendMessage->state = 0;
2088 static unsigned char tx[32];
2089 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2090 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, txLen);
2091 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2092 if (rmrMessageBuffer.sendMessage == nullptr) {
2093 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
aa7133@att.com77fff432020-01-27 12:01:20 +02002094 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP RMR message returned NULL");
ss412g3bac2da2020-01-05 11:52:19 +02002095 } else if (rmrMessageBuffer.sendMessage->state != 0) {
aa7133@att.com77fff432020-01-27 12:01:20 +02002096 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP, on RMR state = %d ( %s)",
aa7133@att.com96c9b882020-03-25 14:50:40 +02002097 rmrMessageBuffer.sendMessage->state, translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str());
aa7133@att.combc110772020-03-25 09:45:14 +02002098 } else if (mdclog_level_get() >= MDCLOG_DEBUG) {
2099 mdclog_write(MDCLOG_DEBUG, "Got Keep Alive Request send : %s", rmrMessageBuffer.ka_message);
ss412g3bac2da2020-01-05 11:52:19 +02002100 }
aa7133@att.com342843e2020-01-26 12:53:34 +02002101
ss412g3bac2da2020-01-05 11:52:19 +02002102 break;
2103 }
ss412g1a79bdf2019-10-24 12:03:05 +03002104 default:
2105 mdclog_write(MDCLOG_WARN, "Message Type : %d is not seported", rmrMessageBuffer.rcvMessage->mtype);
2106 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
2107 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
2108 message.message.time.tv_nsec = ts.tv_nsec;
2109 message.message.time.tv_sec = ts.tv_sec;
2110 message.message.messageType = rmrMessageBuffer.rcvMessage->mtype;
2111
2112 buildJsonMessage(message);
2113
2114
ss412g1a79bdf2019-10-24 12:03:05 +03002115 return -7;
2116 }
2117 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2118 mdclog_write(MDCLOG_DEBUG, "EXIT OK from %s", __FUNCTION__);
2119 }
ss412g1a79bdf2019-10-24 12:03:05 +03002120 return 0;
2121}
2122
2123/**
2124 * Send message to the CU that is not expecting for successful or unsuccessful results
2125 * @param messageBuffer
2126 * @param message
2127 * @param failedMsgId
2128 * @param sctpMap
ss412g1a79bdf2019-10-24 12:03:05 +03002129 * @return
2130 */
2131int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messageBuffer,
2132 ReportingMessages_t &message,
2133 int failedMsgId,
aa7133@att.combe92d712020-03-18 17:30:39 +02002134 Sctp_Map_t *sctpMap) {
ss412g1a79bdf2019-10-24 12:03:05 +03002135
aa7133@att.combe92d712020-03-18 17:30:39 +02002136 getRequestMetaData(message, messageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03002137 if (mdclog_level_get() >= MDCLOG_INFO) {
2138 mdclog_write(MDCLOG_INFO, "send message to %s address", message.message.enodbName);
2139 }
2140
aa7133@att.combe92d712020-03-18 17:30:39 +02002141 auto rc = sendMessagetoCu(sctpMap, messageBuffer, message, failedMsgId);
ss412g1a79bdf2019-10-24 12:03:05 +03002142 return rc;
2143}
2144
2145/**
2146 *
2147 * @param sctpMap
2148 * @param messageBuffer
2149 * @param message
2150 * @param failedMesgId
ss412g1a79bdf2019-10-24 12:03:05 +03002151 * @return
2152 */
2153int sendMessagetoCu(Sctp_Map_t *sctpMap,
2154 RmrMessagesBuffer_t &messageBuffer,
2155 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02002156 int failedMesgId) {
ss412g1a79bdf2019-10-24 12:03:05 +03002157 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(message.message.enodbName);
2158 if (peerInfo == nullptr) {
2159 if (failedMesgId != 0) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002160 sendFailedSendingMessagetoXapp(messageBuffer, message, failedMesgId);
ss412g1a79bdf2019-10-24 12:03:05 +03002161 } else {
2162 mdclog_write(MDCLOG_ERR, "Failed to send message no CU entry %s", message.message.enodbName);
2163 }
ss412g1a79bdf2019-10-24 12:03:05 +03002164 return -1;
2165 }
2166
2167 // get the FD
2168 message.message.messageType = messageBuffer.rcvMessage->mtype;
aa7133@att.combe92d712020-03-18 17:30:39 +02002169 auto rc = sendSctpMsg(peerInfo, message, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002170 return rc;
2171}
2172
2173/**
2174 *
2175 * @param rmrCtx the rmr context to send and receive
2176 * @param msg the msg we got fromxApp
2177 * @param metaData data from xApp in ordered struct
2178 * @param failedMesgId the return message type error
2179 */
2180void
aa7133@att.combe92d712020-03-18 17:30:39 +02002181sendFailedSendingMessagetoXapp(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message, int failedMesgId) {
ss412g1a79bdf2019-10-24 12:03:05 +03002182 rmr_mbuf_t *msg = rmrMessageBuffer.sendMessage;
2183 msg->len = snprintf((char *) msg->payload, 200, "the gNb/eNode name %s not found",
2184 message.message.enodbName);
2185 if (mdclog_level_get() >= MDCLOG_INFO) {
2186 mdclog_write(MDCLOG_INFO, "%s", msg->payload);
2187 }
2188 msg->mtype = failedMesgId;
2189 msg->state = 0;
2190
2191 static unsigned char tx[32];
2192 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2193 rmr_bytes2xact(msg, tx, strlen((const char *) tx));
2194
aa7133@att.combe92d712020-03-18 17:30:39 +02002195 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03002196}
2197
ss412g1a79bdf2019-10-24 12:03:05 +03002198
ss412g1a79bdf2019-10-24 12:03:05 +03002199
2200/**
2201 *
2202 * @param epoll_fd
2203 * @param peerInfo
2204 * @param events
2205 * @param sctpMap
2206 * @param enodbName
2207 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002208 * @return
2209 */
2210int addToEpoll(int epoll_fd,
2211 ConnectedCU_t *peerInfo,
2212 uint32_t events,
2213 Sctp_Map_t *sctpMap,
2214 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002215 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002216 // Add to Epol
2217 struct epoll_event event{};
2218 event.data.ptr = peerInfo;
2219 event.events = events;
2220 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peerInfo->fileDescriptor, &event) < 0) {
2221 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2222 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here), %s, %s %d",
2223 strerror(errno), __func__, __LINE__);
2224 }
2225 close(peerInfo->fileDescriptor);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002226 if (enodbName != nullptr) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002227 cleanHashEntry(peerInfo, sctpMap);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002228 char key[MAX_ENODB_NAME_SIZE * 2];
2229 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2230 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2231 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2232 }
2233 auto tmp = sctpMap->find(key);
2234 if (tmp) {
2235 free(tmp);
2236 sctpMap->erase(key);
2237 }
2238 } else {
2239 peerInfo->enodbName[0] = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03002240 }
ss412g1a79bdf2019-10-24 12:03:05 +03002241 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002242 return -1;
2243 }
ss412g1a79bdf2019-10-24 12:03:05 +03002244 return 0;
2245}
2246
2247/**
2248 *
2249 * @param epoll_fd
2250 * @param peerInfo
2251 * @param events
2252 * @param sctpMap
2253 * @param enodbName
2254 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002255 * @return
2256 */
2257int modifyToEpoll(int epoll_fd,
2258 ConnectedCU_t *peerInfo,
2259 uint32_t events,
2260 Sctp_Map_t *sctpMap,
2261 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002262 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002263 // Add to Epol
2264 struct epoll_event event{};
2265 event.data.ptr = peerInfo;
2266 event.events = events;
2267 if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, peerInfo->fileDescriptor, &event) < 0) {
2268 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2269 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_MOD (may chack not to quit here), %s, %s %d",
2270 strerror(errno), __func__, __LINE__);
2271 }
2272 close(peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02002273 cleanHashEntry(peerInfo, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002274 char key[MAX_ENODB_NAME_SIZE * 2];
2275 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2276 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2277 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2278 }
aa7133@att.com96c9b882020-03-25 14:50:40 +02002279 auto tmp = sctpMap->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +03002280 if (tmp) {
2281 free(tmp);
2282 }
2283 sctpMap->erase(key);
2284 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002285 return -1;
2286 }
ss412g1a79bdf2019-10-24 12:03:05 +03002287 return 0;
2288}
2289
2290
aa7133@att.combe92d712020-03-18 17:30:39 +02002291int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message) {
ss412g1a79bdf2019-10-24 12:03:05 +03002292 buildJsonMessage(message);
2293
2294 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2295
2296 if (rmrMessageBuffer.sendMessage == nullptr) {
2297 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2298 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
ss412g1a79bdf2019-10-24 12:03:05 +03002299 return -1;
2300 }
2301
2302 if (rmrMessageBuffer.sendMessage->state != 0) {
2303 char meid[RMR_MAX_MEID]{};
2304 if (rmrMessageBuffer.sendMessage->state == RMR_ERR_RETRY) {
2305 usleep(5);
2306 rmrMessageBuffer.sendMessage->state = 0;
2307 mdclog_write(MDCLOG_INFO, "RETRY sending Message type %d to Xapp from %s",
2308 rmrMessageBuffer.sendMessage->mtype,
2309 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2310 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2311 if (rmrMessageBuffer.sendMessage == nullptr) {
2312 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
2313 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03002314 return -1;
2315 } else if (rmrMessageBuffer.sendMessage->state != 0) {
2316 mdclog_write(MDCLOG_ERR,
2317 "Message state %s while sending request %d to Xapp from %s after retry of 10 microseconds",
2318 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2319 rmrMessageBuffer.sendMessage->mtype,
2320 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2321 auto rc = rmrMessageBuffer.sendMessage->state;
ss412g1a79bdf2019-10-24 12:03:05 +03002322 return rc;
2323 }
2324 } else {
2325 mdclog_write(MDCLOG_ERR, "Message state %s while sending request %d to Xapp from %s",
2326 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2327 rmrMessageBuffer.sendMessage->mtype,
2328 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
ss412g1a79bdf2019-10-24 12:03:05 +03002329 return rmrMessageBuffer.sendMessage->state;
2330 }
2331 }
2332 return 0;
2333}
2334
2335void buildJsonMessage(ReportingMessages_t &message) {
ss412g3bac2da2020-01-05 11:52:19 +02002336 if (jsonTrace) {
2337 message.outLen = sizeof(message.base64Data);
2338 base64::encode((const unsigned char *) message.message.asndata,
2339 (const int) message.message.asnLength,
2340 message.base64Data,
2341 message.outLen);
2342 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2343 mdclog_write(MDCLOG_DEBUG, "asn data length = %d, base64 message length = %d ",
2344 (int) message.message.asnLength,
2345 (int) message.outLen);
2346 }
2347
2348 snprintf(message.buffer, sizeof(message.buffer),
aa7133@att.com96c9b882020-03-25 14:50:40 +02002349 "{\"header\": {\"ts\": \"%ld.%09ld\","
2350 "\"ranName\": \"%s\","
2351 "\"messageType\": %d,"
2352 "\"direction\": \"%c\"},"
2353 "\"base64Length\": %d,"
2354 "\"asnBase64\": \"%s\"}",
2355 message.message.time.tv_sec,
2356 message.message.time.tv_nsec,
2357 message.message.enodbName,
2358 message.message.messageType,
2359 message.message.direction,
2360 (int) message.outLen,
2361 message.base64Data);
ss412g3bac2da2020-01-05 11:52:19 +02002362 static src::logger_mt &lg = my_logger::get();
2363
2364 BOOST_LOG(lg) << message.buffer;
ss412g1a79bdf2019-10-24 12:03:05 +03002365 }
ss412g1a79bdf2019-10-24 12:03:05 +03002366}
2367
2368
2369/**
2370 * take RMR error code to string
2371 * @param state
2372 * @return
2373 */
2374string translateRmrErrorMessages(int state) {
2375 string str = {};
2376 switch (state) {
2377 case RMR_OK:
2378 str = "RMR_OK - state is good";
2379 break;
2380 case RMR_ERR_BADARG:
2381 str = "RMR_ERR_BADARG - argument passd to function was unusable";
2382 break;
2383 case RMR_ERR_NOENDPT:
2384 str = "RMR_ERR_NOENDPT - send//call could not find an endpoint based on msg type";
2385 break;
2386 case RMR_ERR_EMPTY:
2387 str = "RMR_ERR_EMPTY - msg received had no payload; attempt to send an empty message";
2388 break;
2389 case RMR_ERR_NOHDR:
2390 str = "RMR_ERR_NOHDR - message didn't contain a valid header";
2391 break;
2392 case RMR_ERR_SENDFAILED:
2393 str = "RMR_ERR_SENDFAILED - send failed; errno has nano reason";
2394 break;
2395 case RMR_ERR_CALLFAILED:
2396 str = "RMR_ERR_CALLFAILED - unable to send call() message";
2397 break;
2398 case RMR_ERR_NOWHOPEN:
2399 str = "RMR_ERR_NOWHOPEN - no wormholes are open";
2400 break;
2401 case RMR_ERR_WHID:
2402 str = "RMR_ERR_WHID - wormhole id was invalid";
2403 break;
2404 case RMR_ERR_OVERFLOW:
2405 str = "RMR_ERR_OVERFLOW - operation would have busted through a buffer/field size";
2406 break;
2407 case RMR_ERR_RETRY:
2408 str = "RMR_ERR_RETRY - request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)";
2409 break;
2410 case RMR_ERR_RCVFAILED:
2411 str = "RMR_ERR_RCVFAILED - receive failed (hard error)";
2412 break;
2413 case RMR_ERR_TIMEOUT:
2414 str = "RMR_ERR_TIMEOUT - message processing call timed out";
2415 break;
2416 case RMR_ERR_UNSET:
2417 str = "RMR_ERR_UNSET - the message hasn't been populated with a transport buffer";
2418 break;
2419 case RMR_ERR_TRUNC:
2420 str = "RMR_ERR_TRUNC - received message likely truncated";
2421 break;
2422 case RMR_ERR_INITFAILED:
2423 str = "RMR_ERR_INITFAILED - initialisation of something (probably message) failed";
2424 break;
2425 case RMR_ERR_NOTSUPP:
2426 str = "RMR_ERR_NOTSUPP - the request is not supported, or RMr was not initialised for the request";
2427 break;
2428 default:
2429 char buf[128]{};
2430 snprintf(buf, sizeof buf, "UNDOCUMENTED RMR_ERR : %d", state);
2431 str = buf;
2432 break;
2433 }
2434 return str;
2435}
2436
2437