blob: 407421484a7fa7a06dbbd63fd8282e8c05d37421 [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
ss412g1a79bdf2019-10-24 12:03:05 +030023#include "sctpThread.h"
aa7133@att.com84bd3342020-03-16 18:04:57 +020024#include "BuildRunName.h"
ss412g1a79bdf2019-10-24 12:03:05 +030025
aa7133@att.com84bd3342020-03-16 18:04:57 +020026using namespace std;
27//using namespace std::placeholders;
ss412g3bac2da2020-01-05 11:52:19 +020028using namespace boost::filesystem;
29
ss412g1a79bdf2019-10-24 12:03:05 +030030//#ifdef __cplusplus
31//extern "C"
32//{
33//#endif
34
ss412g3bac2da2020-01-05 11:52:19 +020035// need to expose without the include of gcov
36extern "C" void __gcov_flush(void);
37
38static void catch_function(int signal) {
39 __gcov_flush();
40 exit(signal);
41}
42
43
ss412g1a79bdf2019-10-24 12:03:05 +030044BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
45
46boost::shared_ptr<sinks::synchronous_sink<sinks::text_file_backend>> boostLogger;
ss412g3bac2da2020-01-05 11:52:19 +020047double cpuClock = 0.0;
48bool jsonTrace = true;
ss412g1a79bdf2019-10-24 12:03:05 +030049
50void init_log() {
51 mdclog_attr_t *attr;
52 mdclog_attr_init(&attr);
53 mdclog_attr_set_ident(attr, "E2Terminator");
54 mdclog_init(attr);
55 mdclog_attr_destroy(attr);
56}
ss412g3bac2da2020-01-05 11:52:19 +020057auto start_time = std::chrono::high_resolution_clock::now();
58typedef std::chrono::duration<double, std::ratio<1,1>> seconds_t;
ss412g1a79bdf2019-10-24 12:03:05 +030059
ss412g3bac2da2020-01-05 11:52:19 +020060double age() {
61 return seconds_t(std::chrono::high_resolution_clock::now() - start_time).count();
62}
63
64double approx_CPU_MHz(unsigned sleeptime) {
65 using namespace std::chrono_literals;
66 uint32_t aux = 0;
67 uint64_t cycles_start = rdtscp(aux);
68 double time_start = age();
69 std::this_thread::sleep_for(sleeptime * 1ms);
70 uint64_t elapsed_cycles = rdtscp(aux) - cycles_start;
71 double elapsed_time = age() - time_start;
72 return elapsed_cycles / elapsed_time;
73}
ss412g1a79bdf2019-10-24 12:03:05 +030074
75//std::atomic<int64_t> rmrCounter{0};
76std::atomic<int64_t> num_of_messages{0};
ss412g3bac2da2020-01-05 11:52:19 +020077std::atomic<int64_t> num_of_XAPP_messages{0};
ss412g1a79bdf2019-10-24 12:03:05 +030078static long transactionCounter = 0;
79
aa7133@att.com84bd3342020-03-16 18:04:57 +020080int buildListeningPort(sctp_params_t &sctpParams) {
81 sctpParams.listenFD = socket (AF_INET6, SOCK_STREAM, IPPROTO_SCTP);
82 struct sockaddr_in6 servaddr {};
83 servaddr.sin6_family = AF_INET6;
84 servaddr.sin6_addr = in6addr_any;
aa7133@att.combe92d712020-03-18 17:30:39 +020085 servaddr.sin6_port = htons(sctpParams.sctpPort);
aa7133@att.com84bd3342020-03-16 18:04:57 +020086 if (bind(sctpParams.listenFD, (SA *)&servaddr, sizeof(servaddr)) < 0 ) {
87 mdclog_write(MDCLOG_ERR, "Error binding. %s\n", strerror(errno));
88 return -1;
89 }
90 if (setSocketNoBlocking(sctpParams.listenFD) == -1) {
91 //mdclog_write(MDCLOG_ERR, "Error binding. %s", strerror(errno));
92 return -1;
93 }
94 if (mdclog_level_get() >= MDCLOG_DEBUG) {
95 struct sockaddr_in6 cliaddr {};
96 socklen_t len = sizeof(cliaddr);
97 getsockname(sctpParams.listenFD, (SA *)&cliaddr, &len);
98 char buff[1024] {};
99 inet_ntop(AF_INET6, &cliaddr.sin6_addr, buff, sizeof(buff));
100 mdclog_write(MDCLOG_DEBUG, "My address: %s, port %d\n", buff, htons(cliaddr.sin6_port));
ss412g1a79bdf2019-10-24 12:03:05 +0300101 }
102
aa7133@att.com84bd3342020-03-16 18:04:57 +0200103 if (listen(sctpParams.listenFD, SOMAXCONN) < 0) {
104 mdclog_write(MDCLOG_ERR, "Error listening. %s\n", strerror(errno));
105 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200106 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200107 struct epoll_event event {};
108 event.events = EPOLLIN | EPOLLET;
109 event.data.fd = sctpParams.listenFD;
110
111 // add listening port to epoll
112 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.listenFD, &event)) {
113 printf("Failed to add descriptor to epoll\n");
114 mdclog_write(MDCLOG_ERR, "Failed to add descriptor to epoll. %s\n", strerror(errno));
115 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200116 }
117
aa7133@att.com84bd3342020-03-16 18:04:57 +0200118 return 0;
119}
ss412g3bac2da2020-01-05 11:52:19 +0200120
aa7133@att.com84bd3342020-03-16 18:04:57 +0200121int buildConfiguration(sctp_params_t &sctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +0200122 path p = (sctpParams.configFilePath + "/" + sctpParams.configFileName).c_str();
123 if (exists(p)) {
124 const int size = 2048;
125 auto fileSize = file_size(p);
126 if (fileSize > size) {
127 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200128 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200129 }
130 } else {
131 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
aa7133@att.com84bd3342020-03-16 18:04:57 +0200132 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200133 }
134
ss412g3bac2da2020-01-05 11:52:19 +0200135 ReadConfigFile conf;
136 if (conf.openConfigFile(p.string()) == -1) {
137 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
138 p.string().c_str(), strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200139 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200140 }
141 int rmrPort = conf.getIntValue("nano");
142 if (rmrPort == -1) {
143 mdclog_write(MDCLOG_ERR, "illigal RMR port ");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200144 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200145 }
146 sctpParams.rmrPort = (uint16_t)rmrPort;
147 snprintf(sctpParams.rmrAddress, sizeof(sctpParams.rmrAddress), "%d", (int) (sctpParams.rmrPort));
148
149 auto tmpStr = conf.getStringValue("loglevel");
150 if (tmpStr.length() == 0) {
151 mdclog_write(MDCLOG_ERR, "illigal loglevel. Set loglevel to MDCLOG_INFO");
152 tmpStr = "info";
153 }
154 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
155
156 if ((tmpStr.compare("debug")) == 0) {
157 sctpParams.logLevel = MDCLOG_DEBUG;
158 } else if ((tmpStr.compare("info")) == 0) {
159 sctpParams.logLevel = MDCLOG_INFO;
160 } else if ((tmpStr.compare("warning")) == 0) {
161 sctpParams.logLevel = MDCLOG_WARN;
162 } else if ((tmpStr.compare("error")) == 0) {
163 sctpParams.logLevel = MDCLOG_ERR;
164 } else {
165 mdclog_write(MDCLOG_ERR, "illigal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
166 sctpParams.logLevel = MDCLOG_INFO;
167 }
168 mdclog_level_set(sctpParams.logLevel);
169
170 tmpStr = conf.getStringValue("volume");
171 if (tmpStr.length() == 0) {
172 mdclog_write(MDCLOG_ERR, "illigal volume.");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200173 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200174 }
175
ss412g1a79bdf2019-10-24 12:03:05 +0300176 char tmpLogFilespec[VOLUME_URL_SIZE];
177 tmpLogFilespec[0] = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200178 sctpParams.volume[0] = 0;
179 snprintf(sctpParams.volume, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
180 // copy the name to temp file as well
181 snprintf(tmpLogFilespec, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
182
183
184 // define the file name in the tmp directory under the volume
185 strcat(tmpLogFilespec,"/tmp/E2Term_%Y-%m-%d_%H-%M-%S.%N.tmpStr");
186
aa7133@att.com342843e2020-01-26 12:53:34 +0200187// std::string localIP = conf.getStringValue("local-ip");
188// if (localIP.length() == 0) {
189// mdclog_write(MDCLOG_ERR, "illigal local-ip. environment variable");
190// exit(-1);
191// }
192
193 //sctpParams.myIP.assign(getenv(localIP.c_str()));
ss412g3bac2da2020-01-05 11:52:19 +0200194 sctpParams.myIP = conf.getStringValue("local-ip");
195 if (sctpParams.myIP.length() == 0) {
196 mdclog_write(MDCLOG_ERR, "illigal local-ip.");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200197 return -1;
ss412g1a79bdf2019-10-24 12:03:05 +0300198 }
199
aa7133@att.combe92d712020-03-18 17:30:39 +0200200 int sctpPort = conf.getIntValue("sctp-port");
201 if (sctpPort == -1) {
202 mdclog_write(MDCLOG_ERR, "illigal SCTP port ");
203 return -1;
204 }
205 sctpParams.sctpPort = (uint16_t)sctpPort;
206
aa7133@att.com84bd3342020-03-16 18:04:57 +0200207 sctpParams.fqdn = conf.getStringValue("external-fqdn");
208 if (sctpParams.fqdn.length() == 0) {
209 mdclog_write(MDCLOG_ERR, "illigal external-fqdn");
210 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200211 }
ss412g1a79bdf2019-10-24 12:03:05 +0300212
aa7133@att.com342843e2020-01-26 12:53:34 +0200213 std::string pod = conf.getStringValue("pod_name");
214 if (pod.length() == 0) {
215 mdclog_write(MDCLOG_ERR, "illigal pod_name in config file");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200216 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200217 }
218 auto *podName = getenv(pod.c_str());
219 if (podName == nullptr) {
220 mdclog_write(MDCLOG_ERR, "illigal pod_name or environment varible not exists : %s", pod.c_str());
aa7133@att.com84bd3342020-03-16 18:04:57 +0200221 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200222
223 } else {
224 sctpParams.podName.assign(podName);
225 if (sctpParams.podName.length() == 0) {
226 mdclog_write(MDCLOG_ERR, "illigal pod_name");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200227 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200228 }
229 }
230
ss412g3bac2da2020-01-05 11:52:19 +0200231 tmpStr = conf.getStringValue("trace");
232 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
233 if ((tmpStr.compare("start")) == 0) {
234 mdclog_write(MDCLOG_INFO, "Trace set to: start");
235 sctpParams.trace = true;
236 } else if ((tmpStr.compare("stop")) == 0) {
237 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
238 sctpParams.trace = false;
239 }
240 jsonTrace = sctpParams.trace;
ss412g1a79bdf2019-10-24 12:03:05 +0300241
aa7133@att.combe92d712020-03-18 17:30:39 +0200242 sctpParams.ka_message_length = snprintf(sctpParams.ka_message, KA_MESSAGE_SIZE, "{\"address\": \"%s:%d\","
aa7133@att.com342843e2020-01-26 12:53:34 +0200243 "\"fqdn\": \"%s\","
244 "\"pod_name\": \"%s\"}",
245 (const char *)sctpParams.myIP.c_str(),
246 sctpParams.rmrPort,
247 sctpParams.fqdn.c_str(),
248 sctpParams.podName.c_str());
249
ss412g1a79bdf2019-10-24 12:03:05 +0300250 if (mdclog_level_get() >= MDCLOG_INFO) {
ss412g3bac2da2020-01-05 11:52:19 +0200251 mdclog_mdc_add("RMR Port", to_string(sctpParams.rmrPort).c_str());
252 mdclog_mdc_add("LogLevel", to_string(sctpParams.logLevel).c_str());
253 mdclog_mdc_add("volume", sctpParams.volume);
ss412g1a79bdf2019-10-24 12:03:05 +0300254 mdclog_mdc_add("tmpLogFilespec", tmpLogFilespec);
ss412g3bac2da2020-01-05 11:52:19 +0200255 mdclog_mdc_add("my ip", sctpParams.myIP.c_str());
aa7133@att.com342843e2020-01-26 12:53:34 +0200256 mdclog_mdc_add("pod name", sctpParams.podName.c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300257
aa7133@att.com342843e2020-01-26 12:53:34 +0200258 mdclog_write(MDCLOG_INFO, "running parameters for instance : %s", sctpParams.ka_message);
ss412g1a79bdf2019-10-24 12:03:05 +0300259 }
260 mdclog_mdc_clean();
261
262 // Files written to the current working directory
263 boostLogger = logging::add_file_log(
ss412g3bac2da2020-01-05 11:52:19 +0200264 keywords::file_name = tmpLogFilespec, // to temp directory
ss412g1a79bdf2019-10-24 12:03:05 +0300265 keywords::rotation_size = 10 * 1024 * 1024,
266 keywords::time_based_rotation = sinks::file::rotation_at_time_interval(posix_time::hours(1)),
267 keywords::format = "%Message%"
ss412g3bac2da2020-01-05 11:52:19 +0200268 //keywords::format = "[%TimeStamp%]: %Message%" // use each tmpStr with time stamp
ss412g1a79bdf2019-10-24 12:03:05 +0300269 );
270
271 // Setup a destination folder for collecting rotated (closed) files --since the same volumn can use rename()
272 boostLogger->locked_backend()->set_file_collector(sinks::file::make_collector(
ss412g3bac2da2020-01-05 11:52:19 +0200273 keywords::target = sctpParams.volume
ss412g1a79bdf2019-10-24 12:03:05 +0300274 ));
275
276 // Upon restart, scan the directory for files matching the file_name pattern
277 boostLogger->locked_backend()->scan_for_files();
278
ss412g3bac2da2020-01-05 11:52:19 +0200279 // Enable auto-flushing after each tmpStr record written
ss412g1a79bdf2019-10-24 12:03:05 +0300280 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200281 boostLogger->locked_backend()->auto_flush(true);
282 }
283
284 return 0;
285}
286
287int main(const int argc, char **argv) {
288 sctp_params_t sctpParams;
289
aa7133@att.com84bd3342020-03-16 18:04:57 +0200290 {
291 std::random_device device{};
292 std::mt19937 generator(device());
293 std::uniform_int_distribution<long> distribution(1, (long) 1e12);
294 transactionCounter = distribution(generator);
295 }
296
297// uint64_t st = 0;
298// uint32_t aux1 = 0;
299// st = rdtscp(aux1);
300
301 unsigned num_cpus = std::thread::hardware_concurrency();
302 init_log();
303 mdclog_level_set(MDCLOG_INFO);
304
305 if (std::signal(SIGINT, catch_function) == SIG_ERR) {
306 mdclog_write(MDCLOG_ERR, "Error initializing SIGINT");
307 exit(1);
308 }
309 if (std::signal(SIGABRT, catch_function)== SIG_ERR) {
310 mdclog_write(MDCLOG_ERR, "Error initializing SIGABRT");
311 exit(1);
312 }
313 if (std::signal(SIGTERM, catch_function)== SIG_ERR) {
314 mdclog_write(MDCLOG_ERR, "Error initializing SIGTERM");
315 exit(1);
316 }
317
318 cpuClock = approx_CPU_MHz(100);
319
320 mdclog_write(MDCLOG_DEBUG, "CPU speed %11.11f", cpuClock);
321
322 auto result = parse(argc, argv, sctpParams);
323
324 if (buildConfiguration(sctpParams) != 0) {
325 exit(-1);
ss412g1a79bdf2019-10-24 12:03:05 +0300326 }
327
328 // start epoll
ss412g3bac2da2020-01-05 11:52:19 +0200329 sctpParams.epoll_fd = epoll_create1(0);
330 if (sctpParams.epoll_fd == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +0300331 mdclog_write(MDCLOG_ERR, "failed to open epoll descriptor");
332 exit(-1);
333 }
334
aa7133@att.combe92d712020-03-18 17:30:39 +0200335 getRmrContext(sctpParams);
ss412g3bac2da2020-01-05 11:52:19 +0200336 if (sctpParams.rmrCtx == nullptr) {
337 close(sctpParams.epoll_fd);
ss412g1a79bdf2019-10-24 12:03:05 +0300338 exit(-1);
339 }
340
ss412g3bac2da2020-01-05 11:52:19 +0200341 if (buildInotify(sctpParams) == -1) {
342 close(sctpParams.rmrListenFd);
343 rmr_close(sctpParams.rmrCtx);
344 close(sctpParams.epoll_fd);
345 exit(-1);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200346 }
347
348 if (buildListeningPort(sctpParams) != 0) {
349 close(sctpParams.rmrListenFd);
350 rmr_close(sctpParams.rmrCtx);
351 close(sctpParams.epoll_fd);
352 exit(-1);
353 }
ss412g3bac2da2020-01-05 11:52:19 +0200354
355 sctpParams.sctpMap = new mapWrapper();
ss412g1a79bdf2019-10-24 12:03:05 +0300356
357 std::vector<std::thread> threads(num_cpus);
358// std::vector<std::thread> threads;
359
360 num_cpus = 1;
361 for (unsigned int i = 0; i < num_cpus; i++) {
ss412g3bac2da2020-01-05 11:52:19 +0200362 threads[i] = std::thread(listener, &sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300363
364 cpu_set_t cpuset;
365 CPU_ZERO(&cpuset);
366 CPU_SET(i, &cpuset);
367 int rc = pthread_setaffinity_np(threads[i].native_handle(), sizeof(cpu_set_t), &cpuset);
368 if (rc != 0) {
369 mdclog_write(MDCLOG_ERR, "Error calling pthread_setaffinity_np: %d", rc);
370 }
ss412g1a79bdf2019-10-24 12:03:05 +0300371 }
372
ss412g3bac2da2020-01-05 11:52:19 +0200373 //loop over term_init until first message from xApp
374 handleTermInit(sctpParams);
375
376 for (auto &t : threads) {
377 t.join();
378 }
379
ss412g3bac2da2020-01-05 11:52:19 +0200380 return 0;
381}
382
383void handleTermInit(sctp_params_t &sctpParams) {
384 sendTermInit(sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300385 //send to e2 manager init of e2 term
386 //E2_TERM_INIT
ss412g1a79bdf2019-10-24 12:03:05 +0300387
ss412g3bac2da2020-01-05 11:52:19 +0200388 int count = 0;
389 while (true) {
390 auto xappMessages = num_of_XAPP_messages.load(std::memory_order_acquire);
391 if (xappMessages > 0) {
392 if (mdclog_level_get() >= MDCLOG_INFO) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200393 mdclog_write(MDCLOG_INFO, "Got a message from some appliction, stop sending E2_TERM_INIT");
ss412g3bac2da2020-01-05 11:52:19 +0200394 }
395 return;
396 }
397 usleep(100000);
398 count++;
399 if (count % 1000 == 0) {
400 mdclog_write(MDCLOG_ERR, "GOT No messages from any xApp");
401 sendTermInit(sctpParams);
402 }
403 }
404}
405
406void sendTermInit(sctp_params_t &sctpParams) {
407 rmr_mbuf_t *msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300408 auto count = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200409 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +0300410 msg->mtype = E2_TERM_INIT;
411 msg->state = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200412 rmr_bytes2payload(msg, (unsigned char *)sctpParams.ka_message, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300413 static unsigned char tx[32];
414 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
415 rmr_bytes2xact(msg, tx, txLen);
ss412g3bac2da2020-01-05 11:52:19 +0200416 msg = rmr_send_msg(sctpParams.rmrCtx, msg);
ss412g1a79bdf2019-10-24 12:03:05 +0300417 if (msg == nullptr) {
ss412g3bac2da2020-01-05 11:52:19 +0200418 msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.myIP.length());
ss412g1a79bdf2019-10-24 12:03:05 +0300419 } else if (msg->state == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300420 rmr_free_msg(msg);
ss412g3bac2da2020-01-05 11:52:19 +0200421 if (mdclog_level_get() >= MDCLOG_INFO) {
422 mdclog_write(MDCLOG_INFO, "E2_TERM_INIT succsesfuly sent ");
423 }
424 return;
ss412g1a79bdf2019-10-24 12:03:05 +0300425 } else {
426 if (count % 100 == 0) {
427 mdclog_write(MDCLOG_ERR, "Error sending E2_TERM_INIT cause : %d ", msg->state);
428 }
429 sleep(1);
430 }
431 count++;
432 }
ss412g3bac2da2020-01-05 11:52:19 +0200433}
434
435/**
436 *
437 * @param argc
438 * @param argv
439 * @param sctpParams
440 * @return
441 */
442cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &sctpParams) {
443 cxxopts::Options options(argv[0], "e2 term help");
444 options.positional_help("[optional args]").show_positional_help();
445 options.allow_unrecognised_options().add_options()
446 ("p,path", "config file path", cxxopts::value<std::string>(sctpParams.configFilePath)->default_value("config"))
447 ("f,file", "config file name", cxxopts::value<std::string>(sctpParams.configFileName)->default_value("config.conf"))
448 ("h,help", "Print help");
449
450 auto result = options.parse(argc, argv);
451
452 if (result.count("help")) {
453 std::cout << options.help({""}) << std::endl;
454 exit(0);
455 }
456 return result;
457}
458
459/**
460 *
461 * @param sctpParams
462 * @return -1 failed 0 success
463 */
464int buildInotify(sctp_params_t &sctpParams) {
465 sctpParams.inotifyFD = inotify_init1(IN_NONBLOCK);
466 if (sctpParams.inotifyFD == -1) {
467 mdclog_write(MDCLOG_ERR, "Failed to init inotify (inotify_init1) %s", strerror(errno));
468 close(sctpParams.rmrListenFd);
469 rmr_close(sctpParams.rmrCtx);
470 close(sctpParams.epoll_fd);
471 return -1;
ss412g1a79bdf2019-10-24 12:03:05 +0300472 }
473
ss412g3bac2da2020-01-05 11:52:19 +0200474 sctpParams.inotifyWD = inotify_add_watch(sctpParams.inotifyFD,
475 (const char *)sctpParams.configFilePath.c_str(),
aa7133@att.com84bd3342020-03-16 18:04:57 +0200476 (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 +0200477 if (sctpParams.inotifyWD == -1) {
478 mdclog_write(MDCLOG_ERR, "Failed to add directory : %s to inotify (inotify_add_watch) %s",
479 sctpParams.configFilePath.c_str(),
480 strerror(errno));
481 close(sctpParams.inotifyFD);
482 return -1;
483 }
484
485 struct epoll_event event{};
486 event.events = (EPOLLIN);
487 event.data.fd = sctpParams.inotifyFD;
488 // add listening RMR FD to epoll
489 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.inotifyFD, &event)) {
490 mdclog_write(MDCLOG_ERR, "Failed to add inotify FD to epoll");
491 close(sctpParams.inotifyFD);
492 return -1;
493 }
ss412g1a79bdf2019-10-24 12:03:05 +0300494 return 0;
495}
496
497/**
498 *
499 * @param args
500 * @return
501 */
502void listener(sctp_params_t *params) {
ss412g1a79bdf2019-10-24 12:03:05 +0300503 int num_of_SCTP_messages = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300504 auto totalTime = 0.0;
505 mdclog_mdc_clean();
506 mdclog_level_set(params->logLevel);
507
508 std::thread::id this_id = std::this_thread::get_id();
509 //save cout
510 streambuf *oldCout = cout.rdbuf();
511 ostringstream memCout;
512 // create new cout
513 cout.rdbuf(memCout.rdbuf());
514 cout << this_id;
515 //return to the normal cout
516 cout.rdbuf(oldCout);
517
518 char tid[32];
519 memcpy(tid, memCout.str().c_str(), memCout.str().length() < 32 ? memCout.str().length() : 31);
520 tid[memCout.str().length()] = 0;
521 mdclog_mdc_add("thread id", tid);
522
523 if (mdclog_level_get() >= MDCLOG_DEBUG) {
524 mdclog_write(MDCLOG_DEBUG, "started thread number %s", tid);
525 }
526
aa7133@att.com342843e2020-01-26 12:53:34 +0200527
ss412g1a79bdf2019-10-24 12:03:05 +0300528 RmrMessagesBuffer_t rmrMessageBuffer{};
529 //create and init RMR
530 rmrMessageBuffer.rmrCtx = params->rmrCtx;
531
532 auto *events = (struct epoll_event *) calloc(MAXEVENTS, sizeof(struct epoll_event));
533 struct timespec end{0, 0};
534 struct timespec start{0, 0};
535
536 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
537 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
538
ss412g3bac2da2020-01-05 11:52:19 +0200539 memcpy(rmrMessageBuffer.ka_message, params->ka_message, params->ka_message_length);
540 rmrMessageBuffer.ka_message_len = params->ka_message_length;
541 rmrMessageBuffer.ka_message[rmrMessageBuffer.ka_message_len] = 0;
542
543 if (mdclog_level_get() >= MDCLOG_DEBUG) {
544 mdclog_write(MDCLOG_DEBUG, "keep alive message is : %s", rmrMessageBuffer.ka_message);
545 }
546
ss412g1a79bdf2019-10-24 12:03:05 +0300547 ReportingMessages_t message {};
548
549 for (int i = 0; i < MAX_RMR_BUFF_ARRY; i++) {
550 rmrMessageBuffer.rcvBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
551 rmrMessageBuffer.sendBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
552 }
553
554 while (true) {
555 if (mdclog_level_get() >= MDCLOG_DEBUG) {
556 mdclog_write(MDCLOG_DEBUG, "Start EPOLL Wait");
557 }
558 auto numOfEvents = epoll_wait(params->epoll_fd, events, MAXEVENTS, -1);
559 if (numOfEvents < 0 && errno == EINTR) {
560 if (mdclog_level_get() >= MDCLOG_DEBUG) {
561 mdclog_write(MDCLOG_DEBUG, "got EINTR : %s", strerror(errno));
562 }
563 continue;
564 }
565 if (numOfEvents < 0) {
566 mdclog_write(MDCLOG_ERR, "Epoll wait failed, errno = %s", strerror(errno));
567 return;
568 }
569 for (auto i = 0; i < numOfEvents; i++) {
570 if (mdclog_level_get() >= MDCLOG_DEBUG) {
571 mdclog_write(MDCLOG_DEBUG, "handling epoll event %d out of %d", i + 1, numOfEvents);
572 }
573 clock_gettime(CLOCK_MONOTONIC, &message.message.time);
574 start.tv_sec = message.message.time.tv_sec;
575 start.tv_nsec = message.message.time.tv_nsec;
ss412g3bac2da2020-01-05 11:52:19 +0200576
577
ss412g1a79bdf2019-10-24 12:03:05 +0300578 if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200579 handlepoll_error(events[i], message, rmrMessageBuffer, params);
ss412g1a79bdf2019-10-24 12:03:05 +0300580 } else if (events[i].events & EPOLLOUT) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200581 handleEinprogressMessages(events[i], message, rmrMessageBuffer, params);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200582 } else if (params->listenFD == events[i].data.fd) {
583 // new connection is requested from RAN start build connection
584 while (true) {
585 struct sockaddr in_addr {};
586 socklen_t in_len;
587 char hostBuff[NI_MAXHOST];
588 char portBuff[NI_MAXSERV];
589
590 in_len = sizeof(in_addr);
591 auto *peerInfo = (ConnectedCU_t *)calloc(1, sizeof(ConnectedCU_t));
592 peerInfo->sctpParams = params;
593 peerInfo->fileDescriptor = accept(params->listenFD, &in_addr, &in_len);
594 if (peerInfo->fileDescriptor == -1) {
595 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
596 /* We have processed all incoming connections. */
597 break;
598 } else {
599 mdclog_write(MDCLOG_ERR, "Accept error, errno = %s", strerror(errno));
600 break;
601 }
602 }
603 if (setSocketNoBlocking(peerInfo->fileDescriptor) == -1) {
604 mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on port %s\n", hostBuff, portBuff);
605 close(peerInfo->fileDescriptor);
606 break;
607 }
608 auto ans = getnameinfo(&in_addr, in_len,
609 peerInfo->hostName, NI_MAXHOST,
610 peerInfo->portNumber, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV);
611 if (ans < 0) {
612 mdclog_write(MDCLOG_ERR, "Failed to get info on connection request. %s\n", strerror(errno));
613 close(peerInfo->fileDescriptor);
614 break;
615 }
616 if (mdclog_level_get() >= MDCLOG_DEBUG) {
617 mdclog_write(MDCLOG_DEBUG, "Accepted connection on descriptor %d (host=%s, port=%s)\n", peerInfo->fileDescriptor, peerInfo->hostName, peerInfo->portNumber);
618 }
619 peerInfo->isConnected = false;
620 peerInfo->gotSetup = false;
621 if (addToEpoll(params->epoll_fd,
622 peerInfo,
623 (EPOLLIN | EPOLLET),
624 params->sctpMap, nullptr,
aa7133@att.combe92d712020-03-18 17:30:39 +0200625 0) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200626 break;
627 }
628 }
ss412g1a79bdf2019-10-24 12:03:05 +0300629 } else if (params->rmrListenFd == events[i].data.fd) {
630 // got message from XAPP
ss412g3bac2da2020-01-05 11:52:19 +0200631 num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
ss412g1a79bdf2019-10-24 12:03:05 +0300632 num_of_messages.fetch_add(1, std::memory_order_release);
633 if (mdclog_level_get() >= MDCLOG_DEBUG) {
634 mdclog_write(MDCLOG_DEBUG, "new message from RMR");
635 }
636 if (receiveXappMessages(params->epoll_fd,
637 params->sctpMap,
638 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200639 message.message.time) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300640 mdclog_write(MDCLOG_ERR, "Error handling Xapp message");
641 }
ss412g3bac2da2020-01-05 11:52:19 +0200642 } else if (params->inotifyFD == events[i].data.fd) {
643 mdclog_write(MDCLOG_INFO, "Got event from inotify (configuration update)");
644 handleConfigChange(params);
ss412g1a79bdf2019-10-24 12:03:05 +0300645 } else {
646 /* We RMR_ERR_RETRY have data on the fd waiting to be read. Read and display it.
647 * We must read whatever data is available completely, as we are running
648 * in edge-triggered mode and won't get a notification again for the same data. */
649 num_of_messages.fetch_add(1, std::memory_order_release);
650 if (mdclog_level_get() >= MDCLOG_DEBUG) {
651 mdclog_write(MDCLOG_DEBUG, "new message from SCTP, epoll flags are : %0x", events[i].events);
652 }
653 receiveDataFromSctp(&events[i],
654 params->sctpMap,
655 num_of_SCTP_messages,
656 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200657 message.message.time);
ss412g1a79bdf2019-10-24 12:03:05 +0300658 }
659
660 clock_gettime(CLOCK_MONOTONIC, &end);
661 if (mdclog_level_get() >= MDCLOG_INFO) {
662 totalTime += ((end.tv_sec + 1.0e-9 * end.tv_nsec) -
663 ((double) start.tv_sec + 1.0e-9 * start.tv_nsec));
664 }
665 if (mdclog_level_get() >= MDCLOG_DEBUG) {
666 mdclog_write(MDCLOG_DEBUG, "message handling is %ld seconds %ld nanoseconds",
667 end.tv_sec - start.tv_sec,
668 end.tv_nsec - start.tv_nsec);
669 }
670 }
671 }
ss412g1a79bdf2019-10-24 12:03:05 +0300672}
673
674/**
675 *
ss412g3bac2da2020-01-05 11:52:19 +0200676 * @param sctpParams
677 */
678void handleConfigChange(sctp_params_t *sctpParams) {
679 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
680 const struct inotify_event *event;
681 char *ptr;
682
683 path p = (sctpParams->configFilePath + "/" + sctpParams->configFileName).c_str();
684 auto endlessLoop = true;
685 while (endlessLoop) {
686 auto len = read(sctpParams->inotifyFD, buf, sizeof buf);
687 if (len == -1) {
688 if (errno != EAGAIN) {
689 mdclog_write(MDCLOG_ERR, "read %s ", strerror(errno));
690 endlessLoop = false;
691 continue;
692 }
693 else {
694 endlessLoop = false;
695 continue;
696 }
697 }
698
699 for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) {
700 event = (const struct inotify_event *)ptr;
701 if (event->mask & (uint32_t)IN_ISDIR) {
702 continue;
703 }
704
705 // the directory name
706 if (sctpParams->inotifyWD == event->wd) {
707 // not the directory
708 }
709 if (event->len) {
710 if (!(sctpParams->configFileName.compare(event->name))) {
711 continue;
712 }
713 }
714 // only the file we want
715 if (event->mask & (uint32_t)IN_CLOSE_WRITE) {
716 if (exists(p)) {
717 const int size = 2048;
718 auto fileSize = file_size(p);
719 if (fileSize > size) {
720 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
721 return;
722 }
723 } else {
724 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
725 return;
726 }
727
728 ReadConfigFile conf;
729 if (conf.openConfigFile(p.string()) == -1) {
730 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
731 p.string().c_str(), strerror(errno));
732 return;
733 }
734
735 auto tmpStr = conf.getStringValue("loglevel");
736 if (tmpStr.length() == 0) {
737 mdclog_write(MDCLOG_ERR, "illigal loglevel. Set loglevel to MDCLOG_INFO");
738 tmpStr = "info";
739 }
740 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
741
742 if ((tmpStr.compare("debug")) == 0) {
743 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_DEBUG");
744 sctpParams->logLevel = MDCLOG_DEBUG;
745 } else if ((tmpStr.compare("info")) == 0) {
746 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_INFO");
747 sctpParams->logLevel = MDCLOG_INFO;
748 } else if ((tmpStr.compare("warning")) == 0) {
749 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_WARN");
750 sctpParams->logLevel = MDCLOG_WARN;
751 } else if ((tmpStr.compare("error")) == 0) {
752 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_ERR");
753 sctpParams->logLevel = MDCLOG_ERR;
754 } else {
755 mdclog_write(MDCLOG_ERR, "illigal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
756 sctpParams->logLevel = MDCLOG_INFO;
757 }
758 mdclog_level_set(sctpParams->logLevel);
759
760
761 tmpStr = conf.getStringValue("trace");
762 if (tmpStr.length() == 0) {
763 mdclog_write(MDCLOG_ERR, "illigal trace. Set trace to stop");
764 tmpStr = "stop";
765 }
766
767 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
768 if ((tmpStr.compare("start")) == 0) {
769 mdclog_write(MDCLOG_INFO, "Trace set to: start");
770 sctpParams->trace = true;
771 } else if ((tmpStr.compare("stop")) == 0) {
772 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
773 sctpParams->trace = false;
774 } else {
775 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
776 sctpParams->trace = false;
777 }
778 jsonTrace = sctpParams->trace;
779 endlessLoop = false;
780 }
781 }
782 }
783}
784
785/**
786 *
787 * @param event
788 * @param message
789 * @param rmrMessageBuffer
790 * @param params
ss412g3bac2da2020-01-05 11:52:19 +0200791 */
792void handleEinprogressMessages(struct epoll_event &event,
793 ReportingMessages_t &message,
794 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200795 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +0200796 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
797 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
798
799 mdclog_write(MDCLOG_INFO, "file descriptor %d got EPOLLOUT", peerInfo->fileDescriptor);
800 auto retVal = 0;
801 socklen_t retValLen = 0;
802 auto rc = getsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_ERROR, &retVal, &retValLen);
803 if (rc != 0 || retVal != 0) {
804 if (rc != 0) {
805 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
806 "%s|Failed SCTP Connection, after EINPROGRESS the getsockopt%s",
807 peerInfo->enodbName, strerror(errno));
808 } else if (retVal != 0) {
809 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
810 "%s|Failed SCTP Connection after EINPROGRESS, SO_ERROR",
811 peerInfo->enodbName);
812 }
813
814 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
815 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
816 mdclog_write(MDCLOG_ERR, "%s", rmrMessageBuffer.sendMessage->payload);
817 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +0200818 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200819 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
820 }
821 memset(peerInfo->asnData, 0, peerInfo->asnLength);
822 peerInfo->asnLength = 0;
823 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200824 return;
825 }
826
827 peerInfo->isConnected = true;
828
829 if (modifyToEpoll(params->epoll_fd, peerInfo, (EPOLLIN | EPOLLET), params->sctpMap, peerInfo->enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +0200830 peerInfo->mtype) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200831 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_MOD");
ss412g3bac2da2020-01-05 11:52:19 +0200832 return;
833 }
834
835 message.message.asndata = (unsigned char *)peerInfo->asnData;
836 message.message.asnLength = peerInfo->asnLength;
837 message.message.messageType = peerInfo->mtype;
838 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
839 num_of_messages.fetch_add(1, std::memory_order_release);
840 if (mdclog_level_get() >= MDCLOG_DEBUG) {
841 mdclog_write(MDCLOG_DEBUG, "send the delayed SETUP/ENDC SETUP to sctp for %s",
842 message.message.enodbName);
843 }
aa7133@att.combe92d712020-03-18 17:30:39 +0200844 if (sendSctpMsg(peerInfo, message, params->sctpMap) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200845 if (mdclog_level_get() >= MDCLOG_DEBUG) {
846 mdclog_write(MDCLOG_DEBUG, "Error write to SCTP %s %d", __func__, __LINE__);
847 }
ss412g3bac2da2020-01-05 11:52:19 +0200848 return;
849 }
850
851 memset(peerInfo->asnData, 0, peerInfo->asnLength);
852 peerInfo->asnLength = 0;
853 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200854}
855
856
857void handlepoll_error(struct epoll_event &event,
858 ReportingMessages_t &message,
859 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200860 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +0200861 if (event.data.fd != params->rmrListenFd) {
862 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
863 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on fd %d, RAN NAME : %s",
864 event.events, peerInfo->fileDescriptor, peerInfo->enodbName);
865
866 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
867 "%s|Failed SCTP Connection",
868 peerInfo->enodbName);
869 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
870 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
871
872 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
873 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +0200874 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200875 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
876 }
877
878 close(peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +0200879 cleanHashEntry((ConnectedCU_t *) event.data.ptr, params->sctpMap);
ss412g3bac2da2020-01-05 11:52:19 +0200880 } else {
881 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on RMR FD", event.events);
882 }
ss412g3bac2da2020-01-05 11:52:19 +0200883}
884/**
885 *
ss412g1a79bdf2019-10-24 12:03:05 +0300886 * @param socket
887 * @return
888 */
889int setSocketNoBlocking(int socket) {
890 auto flags = fcntl(socket, F_GETFL, 0);
891
892 if (flags == -1) {
893 mdclog_mdc_add("func", "fcntl");
894 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
895 mdclog_mdc_clean();
896 return -1;
897 }
898
899 flags = (unsigned) flags | (unsigned) O_NONBLOCK;
900 if (fcntl(socket, F_SETFL, flags) == -1) {
901 mdclog_mdc_add("func", "fcntl");
902 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
903 mdclog_mdc_clean();
904 return -1;
905 }
906
907 return 0;
908}
909
910/**
911 *
912 * @param val
913 * @param m
ss412g1a79bdf2019-10-24 12:03:05 +0300914 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200915void cleanHashEntry(ConnectedCU_t *val, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +0300916 char *dummy;
917 auto port = (uint16_t) strtol(val->portNumber, &dummy, 10);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200918 char searchBuff[2048]{};
ss412g1a79bdf2019-10-24 12:03:05 +0300919
920 snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", val->hostName, port);
921 m->erase(searchBuff);
922
923 m->erase(val->enodbName);
924 free(val);
ss412g1a79bdf2019-10-24 12:03:05 +0300925}
926
927/**
928 *
929 * @param fd file discriptor
930 * @param data the asn data to send
931 * @param len length of the data
932 * @param enodbName the enodbName as in the map for printing purpose
933 * @param m map host information
934 * @param mtype message number
935 * @return 0 success, anegative number on fail
936 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200937int sendSctpMsg(ConnectedCU_t *peerInfo, ReportingMessages_t &message, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +0300938 auto loglevel = mdclog_level_get();
939 int fd = peerInfo->fileDescriptor;
940 if (loglevel >= MDCLOG_DEBUG) {
941 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for CU %s, %s",
942 message.message.enodbName, __FUNCTION__);
943 }
944
945 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +0300946 if (send(fd,message.message.asndata, message.message.asnLength,MSG_NOSIGNAL) < 0) {
947 if (errno == EINTR) {
948 continue;
949 }
950 mdclog_write(MDCLOG_ERR, "error writing to CU a message, %s ", strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200951 if (!peerInfo->isConnected) {
952 mdclog_write(MDCLOG_ERR, "connection to CU %s is still in progress.", message.message.enodbName);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200953 return -1;
954 }
aa7133@att.combe92d712020-03-18 17:30:39 +0200955 cleanHashEntry(peerInfo, m);
ss412g1a79bdf2019-10-24 12:03:05 +0300956 close(fd);
957 char key[MAX_ENODB_NAME_SIZE * 2];
958 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName,
959 message.message.messageType);
960 if (loglevel >= MDCLOG_DEBUG) {
961 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
962 }
963 auto tmp = m->find(key);
964 if (tmp) {
965 free(tmp);
966 }
967 m->erase(key);
ss412g1a79bdf2019-10-24 12:03:05 +0300968 return -1;
969 }
970 message.message.direction = 'D';
971 // send report.buffer of size
972 buildJsonMessage(message);
973
974 if (loglevel >= MDCLOG_DEBUG) {
975 mdclog_write(MDCLOG_DEBUG,
976 "SCTP message for CU %s sent from %s",
977 message.message.enodbName,
978 __FUNCTION__);
979 }
ss412g1a79bdf2019-10-24 12:03:05 +0300980 return 0;
981 }
982}
983
984/**
985 *
986 * @param message
987 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +0300988 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200989void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
990 rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *) (message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +0300991
992 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
993 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
994
995 if (mdclog_level_get() >= MDCLOG_DEBUG) {
996 mdclog_write(MDCLOG_DEBUG, "Message from Xapp RAN name = %s message length = %ld",
997 message.message.enodbName, (unsigned long) message.message.asnLength);
998 }
ss412g1a79bdf2019-10-24 12:03:05 +0300999}
1000
1001
1002/**
1003 *
1004 * @param metaData all the data strip to structure
1005 * @param data the data recived from xAPP
1006 * @return 0 success all other values are fault
1007 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001008int getSetupRequestMetaData(ReportingMessages_t &message, char *data, char *host, uint16_t &port) {
ss412g1a79bdf2019-10-24 12:03:05 +03001009 auto loglevel = mdclog_level_get();
1010
1011 char delimiter[4] {};
1012 memset(delimiter, 0, (size_t)4);
1013 delimiter[0] = '|';
1014 char *tmp;
1015
1016 char *val = strtok_r(data, delimiter, &tmp);
1017 if (val != nullptr) {
1018 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1019 mdclog_write(MDCLOG_DEBUG, "SCTP ADDRESS parameter from message = %s", val);
1020 }
1021 memcpy(host, val, tmp - val );
1022 } else {
1023 mdclog_write(MDCLOG_ERR, "wrong Host Name for setup request %s", data);
ss412g1a79bdf2019-10-24 12:03:05 +03001024 return -1;
1025 }
1026
1027 val = strtok_r(nullptr, delimiter, &tmp);
1028 if (val != nullptr) {
1029 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1030 mdclog_write(MDCLOG_DEBUG, "PORT parameter from message = %s", val);
1031 }
1032 char *dummy;
1033 port = (uint16_t)strtol(val, &dummy, 10);
1034 } else {
1035 mdclog_write(MDCLOG_ERR, "wrong Port for setup request %s", data);
ss412g1a79bdf2019-10-24 12:03:05 +03001036 return -2;
1037 }
1038
1039 val = strtok_r(nullptr, delimiter, &tmp);
1040 if (val != nullptr) {
1041 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1042 mdclog_write(MDCLOG_DEBUG, "RAN NAME parameter from message = %s", val);
1043 }
1044 memcpy(message.message.enodbName, val, tmp - val);
1045 } else {
1046 mdclog_write(MDCLOG_ERR, "wrong gNb/Enodeb name for setup request %s", data);
ss412g1a79bdf2019-10-24 12:03:05 +03001047 return -3;
1048 }
1049 val = strtok_r(nullptr, delimiter, &tmp);
1050 if (val != nullptr) {
1051 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1052 mdclog_write(MDCLOG_DEBUG, "ASN length parameter from message = %s", val);
1053 }
1054 char *dummy;
1055 message.message.asnLength = (uint16_t) strtol(val, &dummy, 10);
1056 } else {
1057 mdclog_write(MDCLOG_ERR, "wrong ASN length for setup request %s", data);
ss412g1a79bdf2019-10-24 12:03:05 +03001058 return -4;
1059 }
1060
1061 message.message.asndata = (unsigned char *)tmp; // tmp is local but point to the location in data
1062
1063 if (loglevel >= MDCLOG_INFO) {
1064 mdclog_write(MDCLOG_INFO, "Message from Xapp RAN name = %s host address = %s port = %d",
1065 message.message.enodbName, host, port);
1066 }
ss412g1a79bdf2019-10-24 12:03:05 +03001067 return 0;
1068}
1069
1070/**
1071 *
1072 * @param events
1073 * @param sctpMap
1074 * @param numOfMessages
1075 * @param rmrMessageBuffer
1076 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03001077 * @return
1078 */
1079int receiveDataFromSctp(struct epoll_event *events,
1080 Sctp_Map_t *sctpMap,
1081 int &numOfMessages,
1082 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001083 struct timespec &ts) {
ss412g1a79bdf2019-10-24 12:03:05 +03001084 /* We have data on the fd waiting to be read. Read and display it.
1085 * We must read whatever data is available completely, as we are running
1086 * in edge-triggered mode and won't get a notification again for the same data. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001087 ReportingMessages_t message {};
1088 auto done = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001089 auto loglevel = mdclog_level_get();
aa7133@att.com84bd3342020-03-16 18:04:57 +02001090
ss412g1a79bdf2019-10-24 12:03:05 +03001091 // get the identity of the interface
aa7133@att.com84bd3342020-03-16 18:04:57 +02001092 message.peerInfo = (ConnectedCU_t *)events->data.ptr;
1093
1094
ss412g1a79bdf2019-10-24 12:03:05 +03001095 struct timespec start{0, 0};
1096 struct timespec decodestart{0, 0};
1097 struct timespec end{0, 0};
1098
1099 E2AP_PDU_t *pdu = nullptr;
1100
ss412g1a79bdf2019-10-24 12:03:05 +03001101
1102 while (true) {
1103 if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001104 mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP %d fd", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001105 clock_gettime(CLOCK_MONOTONIC, &start);
1106 }
1107 // read the buffer directly to rmr payload
1108 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1109 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
aa7133@att.com84bd3342020-03-16 18:04:57 +02001110 read(message.peerInfo->fileDescriptor, rmrMessageBuffer.sendMessage->payload, RECEIVE_SCTP_BUFFER_SIZE);
1111
ss412g1a79bdf2019-10-24 12:03:05 +03001112 if (loglevel >= MDCLOG_DEBUG) {
1113 mdclog_write(MDCLOG_DEBUG, "Finish Read from SCTP %d fd message length = %ld",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001114 message.peerInfo->fileDescriptor, message.message.asnLength);
ss412g1a79bdf2019-10-24 12:03:05 +03001115 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001116 memcpy(message.message.enodbName, message.peerInfo->enodbName, sizeof(message.peerInfo->enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001117 message.message.direction = 'U';
1118 message.message.time.tv_nsec = ts.tv_nsec;
1119 message.message.time.tv_sec = ts.tv_sec;
1120
1121 if (message.message.asnLength < 0) {
1122 if (errno == EINTR) {
1123 continue;
1124 }
1125 /* If errno == EAGAIN, that means we have read all
aa7133@att.com84bd3342020-03-16 18:04:57 +02001126 data. So goReportingMessages_t back to the main loop. */
ss412g1a79bdf2019-10-24 12:03:05 +03001127 if (errno != EAGAIN) {
1128 mdclog_write(MDCLOG_ERR, "Read error, %s ", strerror(errno));
1129 done = 1;
1130 } else if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001131 mdclog_write(MDCLOG_DEBUG, "EAGAIN - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001132 }
1133 break;
1134 } else if (message.message.asnLength == 0) {
1135 /* End of file. The remote has closed the connection. */
1136 if (loglevel >= MDCLOG_INFO) {
1137 mdclog_write(MDCLOG_INFO, "END of File Closed connection - descriptor = %d",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001138 message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001139 }
1140 done = 1;
1141 break;
1142 }
1143
1144 asn_dec_rval_t rval;
1145 if (loglevel >= MDCLOG_DEBUG) {
1146 char printBuffer[4096]{};
1147 char *tmp = printBuffer;
1148 for (size_t i = 0; i < (size_t)message.message.asnLength; ++i) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001149 snprintf(tmp, 3, "%02x", message.message.asndata[i]);
ss412g1a79bdf2019-10-24 12:03:05 +03001150 tmp += 2;
1151 }
1152 printBuffer[message.message.asnLength] = 0;
1153 clock_gettime(CLOCK_MONOTONIC, &end);
1154 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 +02001155 message.peerInfo->enodbName, end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001156 mdclog_write(MDCLOG_DEBUG, "PDU buffer length = %ld, data = : %s", message.message.asnLength,
1157 printBuffer);
1158 clock_gettime(CLOCK_MONOTONIC, &decodestart);
1159 }
1160
1161 rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1162 message.message.asndata, message.message.asnLength);
1163 if (rval.code != RC_OK) {
1164 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from RAN : %s", rval.code,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001165 message.peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001166 break;
1167 }
1168
1169 if (loglevel >= MDCLOG_DEBUG) {
1170 clock_gettime(CLOCK_MONOTONIC, &end);
1171 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 +02001172 message.peerInfo->enodbName, end.tv_sec - decodestart.tv_sec, end.tv_nsec - decodestart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001173 char *printBuffer;
1174 size_t size;
1175 FILE *stream = open_memstream(&printBuffer, &size);
1176 asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu);
1177 mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer);
1178 clock_gettime(CLOCK_MONOTONIC, &decodestart);
1179 }
1180
1181 switch (pdu->present) {
1182 case E2AP_PDU_PR_initiatingMessage: {//initiating message
aa7133@att.combe92d712020-03-18 17:30:39 +02001183 asnInitiatingRequest(pdu, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001184 break;
1185 }
1186 case E2AP_PDU_PR_successfulOutcome: { //successful outcome
aa7133@att.combe92d712020-03-18 17:30:39 +02001187 asnSuccsesfulMsg(pdu, message, sctpMap, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001188 break;
1189 }
1190 case E2AP_PDU_PR_unsuccessfulOutcome: { //Unsuccessful Outcome
aa7133@att.combe92d712020-03-18 17:30:39 +02001191 asnUnSuccsesfulMsg(pdu, message, sctpMap, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001192 break;
1193 }
1194 default:
1195 mdclog_write(MDCLOG_ERR, "Unknown index %d in E2AP PDU", pdu->present);
1196 break;
1197 }
1198 if (loglevel >= MDCLOG_DEBUG) {
1199 clock_gettime(CLOCK_MONOTONIC, &end);
1200 mdclog_write(MDCLOG_DEBUG,
1201 "After processing message and sent to rmr for : %s, Read time is : %ld seconds, %ld nanoseconds",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001202 message.peerInfo->enodbName, end.tv_sec - decodestart.tv_sec, end.tv_nsec - decodestart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001203 }
1204 numOfMessages++;
1205 // remove the break for EAGAIN
1206 //break;
1207 if (pdu != nullptr) {
1208 //TODO need to test ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu); to get better performance
1209 //ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
1210 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1211 pdu = nullptr;
1212 }
1213 //clock_gettime(CLOCK_MONOTONIC, &start);
1214 }
1215 // in case of break to avoid memory leak
1216 if (pdu != nullptr) {
1217 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1218 pdu = nullptr;
1219 }
1220
1221 if (done) {
1222 if (loglevel >= MDCLOG_INFO) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001223 mdclog_write(MDCLOG_INFO, "Closed connection - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001224 }
1225 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1226 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
1227 256,
1228 "%s|CU disconnected unexpectedly",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001229 message.peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001230 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1231
1232 if (sendRequestToXapp(message,
1233 RIC_SCTP_CONNECTION_FAILURE,
aa7133@att.combe92d712020-03-18 17:30:39 +02001234 rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001235 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1236 }
1237
1238 /* Closing descriptor make epoll remove it from the set of descriptors which are monitored. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001239 close(message.peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02001240 cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03001241 }
1242 if (loglevel >= MDCLOG_DEBUG) {
1243 clock_gettime(CLOCK_MONOTONIC, &end);
1244 mdclog_write(MDCLOG_DEBUG, "from receive SCTP to send RMR time is %ld seconds and %ld nanoseconds",
1245 end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1246
1247 }
ss412g1a79bdf2019-10-24 12:03:05 +03001248 return 0;
1249}
1250
aa7133@att.com84bd3342020-03-16 18:04:57 +02001251static void buildAndsendSetupRequest(ReportingMessages_t &message,
1252 E2setupRequestIEs_t *ie,
1253 RmrMessagesBuffer_t &rmrMessageBuffer,
1254 E2AP_PDU_t *pdu) {
1255 auto logLevel = mdclog_level_get();
1256
1257
1258 if (buildRanName(message.peerInfo->enodbName, ie) < 0) {
1259 mdclog_write(MDCLOG_ERR, "Bad param in E2setupRequestIEs GlobalE2node_ID.\n");
1260 } else {
1261 memcpy(message.message.enodbName, message.peerInfo->enodbName, strlen(message.peerInfo->enodbName));
1262 }
1263 // now we can send the data to e2Mgr
1264 auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE;
1265
1266 auto *rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size);
1267 // add addrees to message
1268 auto j = snprintf((char *)rmrMsg->payload, 256, "%s:%d|", message.peerInfo->sctpParams->myIP.c_str(), message.peerInfo->sctpParams->rmrPort);
1269
1270
1271 unsigned char *buffer = &rmrMsg->payload[j];
1272 // encode to xml
1273 asn_enc_rval_t er;
1274 er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size - j);
1275 if (er.encoded == -1) {
1276 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1277 } else if (er.encoded > (ssize_t) buffer_size) {
1278 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s",
1279 (int) buffer_size,
1280 asn_DEF_E2AP_PDU.name);
1281 } else {
1282 if (logLevel >= MDCLOG_DEBUG) {
1283 mdclog_write(MDCLOG_DEBUG, "Buffer of size %d, data = %s", (int) er.encoded, buffer);
1284 }
1285 // TODO send to RMR
aa7133@att.combe92d712020-03-18 17:30:39 +02001286 message.message.messageType = rmrMsg->mtype = RIC_E2_SETUP_REQ;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001287 rmrMsg->state = 0;
1288 rmr_bytes2meid(rmrMsg, (unsigned char *) message.message.enodbName, strlen(message.message.enodbName));
1289
1290 static unsigned char tx[32];
1291 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1292 rmr_bytes2xact(rmrMsg, tx, strlen((const char *) tx));
1293
1294 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1295 if (rmrMsg == nullptr) {
1296 mdclog_write(MDCLOG_ERR, "RMR failed to send returned nullptr");
1297 } else if (rmrMsg->state != 0) {
1298 char meid[RMR_MAX_MEID]{};
1299 if (rmrMsg->state == RMR_ERR_RETRY) {
1300 usleep(5);
1301 rmrMsg->state = 0;
1302 mdclog_write(MDCLOG_INFO, "RETRY sending Message %d to Xapp from %s",
1303 rmrMsg->mtype, rmr_get_meid(rmrMsg, (unsigned char *) meid));
1304 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1305 if (rmrMsg == nullptr) {
1306 mdclog_write(MDCLOG_ERR, "RMR failed send returned nullptr");
1307 } else if (rmrMsg->state != 0) {
1308 mdclog_write(MDCLOG_ERR,
1309 "RMR Retry failed %s sending request %d to Xapp from %s",
1310 translateRmrErrorMessages(rmrMsg->state).c_str(),
1311 rmrMsg->mtype,
1312 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1313 }
1314 } else {
1315 mdclog_write(MDCLOG_ERR, "RMR failed: %s. sending request %d to Xapp from %s",
1316 translateRmrErrorMessages(rmrMsg->state).c_str(),
1317 rmrMsg->mtype,
1318 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1319 }
1320 }
1321 message.peerInfo->gotSetup = true;
1322 buildJsonMessage(message);
1323 }
1324
1325}
ss412g1a79bdf2019-10-24 12:03:05 +03001326/**
1327 *
1328 * @param pdu
1329 * @param message
1330 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001331 */
1332void asnInitiatingRequest(E2AP_PDU_t *pdu,
1333 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02001334 RmrMessagesBuffer_t &rmrMessageBuffer) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001335 auto logLevel = mdclog_level_get();
ss412g1a79bdf2019-10-24 12:03:05 +03001336 auto procedureCode = ((InitiatingMessage_t *) pdu->choice.initiatingMessage)->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001337 if (logLevel >= MDCLOG_DEBUG) {
1338 mdclog_write(MDCLOG_DEBUG, "Initiating message %ld\n", procedureCode);
ss412g1a79bdf2019-10-24 12:03:05 +03001339 }
1340 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001341 case ProcedureCode_id_E2setup: {
1342 if (logLevel >= MDCLOG_DEBUG) {
1343 mdclog_write(MDCLOG_DEBUG, "Got E2setup\n");
1344 }
1345
1346 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1347 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
1348 auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i];
1349 if (ie->id == ProtocolIE_ID_id_GlobalE2node_ID) {
1350 if (ie->value.present == E2setupRequestIEs__value_PR_GlobalE2node_ID) {
1351 buildAndsendSetupRequest(message, ie, rmrMessageBuffer, pdu);
1352 break;
1353 }
1354 }
ss412g1a79bdf2019-10-24 12:03:05 +03001355 }
1356 break;
1357 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001358 case ProcedureCode_id_ErrorIndication: {
1359 if (logLevel >= MDCLOG_DEBUG) {
1360 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
1361 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001362 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001363 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001364 }
1365 break;
1366 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001367 case ProcedureCode_id_Reset: {
1368 if (logLevel >= MDCLOG_DEBUG) {
1369 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001370 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001371 if (sendRequestToXapp(message, RIC_X2_RESET, rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001372 mdclog_write(MDCLOG_ERR, "RIC_X2_RESET message failed to send to xAPP");
1373 }
1374 break;
1375 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001376 case ProcedureCode_id_RICcontrol: {
1377 if (logLevel >= MDCLOG_DEBUG) {
1378 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
1379 }
1380 break;
1381 }
1382 case ProcedureCode_id_RICindication: {
1383 if (logLevel >= MDCLOG_DEBUG) {
1384 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1385 }
1386 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
ss412g1a79bdf2019-10-24 12:03:05 +03001387 auto messageSent = false;
1388 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02001389 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001390 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1391 }
1392 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001393 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001394 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1395 }
1396 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1397 static unsigned char tx[32];
1398 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1399 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1400 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02001401 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001402 (unsigned char *)message.message.enodbName,
1403 strlen(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001404 rmrMessageBuffer.sendMessage->state = 0;
1405 rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
ss412g3bac2da2020-01-05 11:52:19 +02001406 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1407 mdclog_write(MDCLOG_DEBUG, "RIC sub id = %d, message type = %d",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001408 rmrMessageBuffer.sendMessage->sub_id,
1409 rmrMessageBuffer.sendMessage->mtype);
ss412g3bac2da2020-01-05 11:52:19 +02001410 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001411 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001412 messageSent = true;
1413 } else {
1414 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1415 }
1416 }
1417 if (messageSent) {
1418 break;
1419 }
1420 }
1421 break;
1422 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001423 case ProcedureCode_id_RICserviceQuery: {
1424 if (logLevel >= MDCLOG_DEBUG) {
1425 mdclog_write(MDCLOG_DEBUG, "Got RICserviceQuery %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001426 }
1427 break;
1428 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001429 case ProcedureCode_id_RICserviceUpdate: {
1430 if (logLevel >= MDCLOG_DEBUG) {
1431 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1432 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001433 if (sendRequestToXapp(message, RIC_SERVICE_UPDATE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001434 mdclog_write(MDCLOG_ERR, "RIC_SERVICE_UPDATE message failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001435 }
1436 break;
1437 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001438 case ProcedureCode_id_RICsubscription: {
1439 if (logLevel >= MDCLOG_DEBUG) {
1440 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1441 }
1442 break;
1443 }
1444 case ProcedureCode_id_RICsubscriptionDelete: {
1445 if (logLevel >= MDCLOG_DEBUG) {
1446 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001447 }
1448 break;
1449 }
1450 default: {
1451 mdclog_write(MDCLOG_ERR, "Undefined or not supported message = %ld", procedureCode);
1452 message.message.messageType = 0; // no RMR message type yet
1453
1454 buildJsonMessage(message);
1455
1456 break;
1457 }
1458 }
ss412g1a79bdf2019-10-24 12:03:05 +03001459}
1460
1461/**
1462 *
1463 * @param pdu
1464 * @param message
1465 * @param sctpMap
1466 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001467 */
1468void asnSuccsesfulMsg(E2AP_PDU_t *pdu, ReportingMessages_t &message, Sctp_Map_t *sctpMap,
aa7133@att.combe92d712020-03-18 17:30:39 +02001469 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001470 auto procedureCode = pdu->choice.successfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001471 auto logLevel = mdclog_level_get();
1472 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03001473 mdclog_write(MDCLOG_INFO, "Successful Outcome %ld", procedureCode);
1474 }
1475 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001476 case ProcedureCode_id_E2setup: {
1477 if (logLevel >= MDCLOG_DEBUG) {
1478 mdclog_write(MDCLOG_DEBUG, "Got E2setup\n");
ss412g1a79bdf2019-10-24 12:03:05 +03001479 }
1480 break;
1481 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001482 case ProcedureCode_id_ErrorIndication: {
1483 if (logLevel >= MDCLOG_DEBUG) {
1484 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001485 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001486 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001487 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001488 }
1489 break;
1490 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001491 case ProcedureCode_id_Reset: {
1492 if (logLevel >= MDCLOG_DEBUG) {
1493 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001494 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001495 if (sendRequestToXapp(message, RIC_X2_RESET, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001496 mdclog_write(MDCLOG_ERR, "RIC_X2_RESET message failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001497 }
1498 break;
1499 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001500 case ProcedureCode_id_RICcontrol: {
1501 if (logLevel >= MDCLOG_DEBUG) {
1502 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001503 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001504 for (auto i = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001505 i < pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.count; i++) {
1506 auto messageSent = false;
1507 RICcontrolAcknowledge_IEs_t *ie = pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.array[i];
1508 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1509 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1510 }
1511 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1512 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1513 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1514 }
1515 if (ie->value.present == RICcontrolAcknowledge_IEs__value_PR_RICrequestID) {
1516 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_ACK;
1517 rmrMessageBuffer.sendMessage->state = 0;
1518 rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1519 static unsigned char tx[32];
1520 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1521 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02001522 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001523 (unsigned char *)message.message.enodbName,
1524 strlen(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001525
aa7133@att.combe92d712020-03-18 17:30:39 +02001526 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001527 messageSent = true;
1528 } else {
1529 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1530 }
1531 }
1532 if (messageSent) {
1533 break;
1534 }
1535 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001536
1537 break;
1538 }
1539 case ProcedureCode_id_RICindication: {
1540 if (logLevel >= MDCLOG_DEBUG) {
1541 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1542 }
1543 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
1544 auto messageSent = false;
1545 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
1546 if (logLevel >= MDCLOG_DEBUG) {
1547 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1548 }
1549 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1550 if (logLevel >= MDCLOG_DEBUG) {
1551 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1552 }
1553 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1554 static unsigned char tx[32];
1555 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1556 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1557 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
1558 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
1559 (unsigned char *)message.message.enodbName,
1560 strlen(message.message.enodbName));
1561 rmrMessageBuffer.sendMessage->state = 0;
1562 rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1563 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1564 mdclog_write(MDCLOG_DEBUG, "RIC sub id = %d, message type = %d",
1565 rmrMessageBuffer.sendMessage->sub_id,
1566 rmrMessageBuffer.sendMessage->mtype);
1567 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001568 sendRmrMessage(rmrMessageBuffer, message);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001569 messageSent = true;
1570 } else {
1571 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1572 }
1573 }
1574 if (messageSent) {
1575 break;
1576 }
1577 }
1578 break;
1579 }
1580 case ProcedureCode_id_RICserviceQuery: {
1581 if (logLevel >= MDCLOG_DEBUG) {
1582 mdclog_write(MDCLOG_DEBUG, "Got RICserviceQuery %s", message.message.enodbName);
1583 }
1584 break;
1585 }
1586 case ProcedureCode_id_RICserviceUpdate: {
1587 if (logLevel >= MDCLOG_DEBUG) {
1588 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1589 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001590 if (sendRequestToXapp(message, RIC_SERVICE_UPDATE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001591 mdclog_write(MDCLOG_ERR, "RIC_SERVICE_UPDATE message failed to send to xAPP");
1592 }
1593 break;
1594 }
1595 case ProcedureCode_id_RICsubscription: {
1596 if (logLevel >= MDCLOG_DEBUG) {
1597 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1598 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001599 if (sendRequestToXapp(message, RIC_SUB_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001600 mdclog_write(MDCLOG_ERR, "Subscription successful message failed to send to xAPP");
1601 }
1602 break;
1603 }
1604 case ProcedureCode_id_RICsubscriptionDelete: {
1605 if (logLevel >= MDCLOG_DEBUG) {
1606 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1607 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001608 if (sendRequestToXapp(message, RIC_SUB_DEL_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001609 mdclog_write(MDCLOG_ERR, "Subscription delete successful message failed to send to xAPP");
1610 }
ss412g1a79bdf2019-10-24 12:03:05 +03001611 break;
1612 }
1613 default: {
1614 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1615 message.message.messageType = 0; // no RMR message type yet
1616 buildJsonMessage(message);
1617
1618 break;
1619 }
1620 }
ss412g1a79bdf2019-10-24 12:03:05 +03001621}
1622
1623/**
1624 *
1625 * @param pdu
1626 * @param message
1627 * @param sctpMap
1628 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001629 */
1630void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
1631 ReportingMessages_t &message,
1632 Sctp_Map_t *sctpMap,
aa7133@att.combe92d712020-03-18 17:30:39 +02001633 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001634 auto procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001635 auto logLevel = mdclog_level_get();
1636 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03001637 mdclog_write(MDCLOG_INFO, "Unsuccessful Outcome %ld", procedureCode);
1638 }
1639 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001640 case ProcedureCode_id_E2setup: {
1641 if (logLevel >= MDCLOG_DEBUG) {
1642 mdclog_write(MDCLOG_DEBUG, "Got E2setup\n");
ss412g1a79bdf2019-10-24 12:03:05 +03001643 }
1644 break;
1645 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001646 case ProcedureCode_id_ErrorIndication: {
1647 if (logLevel >= MDCLOG_DEBUG) {
1648 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001649 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001650 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001651 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001652 }
1653 break;
1654 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001655 case ProcedureCode_id_Reset: {
1656 if (logLevel >= MDCLOG_DEBUG) {
1657 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
1658 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001659 if (sendRequestToXapp(message, RIC_X2_RESET, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001660 mdclog_write(MDCLOG_ERR, "RIC_X2_RESET message failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001661 }
1662 break;
1663 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001664 case ProcedureCode_id_RICcontrol: {
1665 if (logLevel >= MDCLOG_DEBUG) {
1666 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001667 }
1668 for (int i = 0;
1669 i < pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.count; i++) {
1670 auto messageSent = false;
1671 RICcontrolFailure_IEs_t *ie = pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02001672 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001673 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1674 }
1675 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001676 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001677 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1678 }
1679 if (ie->value.present == RICcontrolFailure_IEs__value_PR_RICrequestID) {
1680 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_FAILURE;
1681 rmrMessageBuffer.sendMessage->state = 0;
1682 rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1683 static unsigned char tx[32];
1684 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1685 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001686 rmr_bytes2meid(rmrMessageBuffer.sendMessage, (unsigned char *) message.message.enodbName,
1687 strlen(message.message.enodbName));
aa7133@att.combe92d712020-03-18 17:30:39 +02001688 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001689 messageSent = true;
1690 } else {
1691 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1692 }
1693 }
1694 if (messageSent) {
1695 break;
1696 }
1697 }
1698 break;
1699 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001700 case ProcedureCode_id_RICindication: {
1701 if (logLevel >= MDCLOG_DEBUG) {
1702 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1703 }
1704 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
1705 auto messageSent = false;
1706 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
1707 if (logLevel >= MDCLOG_DEBUG) {
1708 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1709 }
1710 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1711 if (logLevel >= MDCLOG_DEBUG) {
1712 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1713 }
1714 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1715 static unsigned char tx[32];
1716 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1717 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1718 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
1719 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
1720 (unsigned char *)message.message.enodbName,
1721 strlen(message.message.enodbName));
1722 rmrMessageBuffer.sendMessage->state = 0;
1723 rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1724 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1725 mdclog_write(MDCLOG_DEBUG, "RIC sub id = %d, message type = %d",
1726 rmrMessageBuffer.sendMessage->sub_id,
1727 rmrMessageBuffer.sendMessage->mtype);
1728 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001729 sendRmrMessage(rmrMessageBuffer, message);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001730 messageSent = true;
1731 } else {
1732 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1733 }
1734 }
1735 if (messageSent) {
1736 break;
1737 }
1738 }
1739 break;
1740 }
1741 case ProcedureCode_id_RICserviceQuery: {
1742 if (logLevel >= MDCLOG_DEBUG) {
1743 mdclog_write(MDCLOG_DEBUG, "Got RICserviceQuery %s", message.message.enodbName);
1744 }
1745 break;
1746 }
1747 case ProcedureCode_id_RICserviceUpdate: {
1748 if (logLevel >= MDCLOG_DEBUG) {
1749 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1750 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001751 if (sendRequestToXapp(message, RIC_SERVICE_UPDATE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001752 mdclog_write(MDCLOG_ERR, "RIC_SERVICE_UPDATE message failed to send to xAPP");
1753 }
1754 break;
1755 }
1756 case ProcedureCode_id_RICsubscription: {
1757 if (logLevel >= MDCLOG_DEBUG) {
1758 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1759 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001760 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001761 mdclog_write(MDCLOG_ERR, "Subscription unsuccessful message failed to send to xAPP");
1762 }
1763 break;
1764 }
1765 case ProcedureCode_id_RICsubscriptionDelete: {
1766 if (logLevel >= MDCLOG_DEBUG) {
1767 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1768 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001769 if (sendRequestToXapp(message, RIC_SUB_DEL_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001770 mdclog_write(MDCLOG_ERR, "Subscription Delete unsuccessful message failed to send to xAPP");
1771 }
1772 break;
1773 }
ss412g1a79bdf2019-10-24 12:03:05 +03001774 default: {
1775 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1776 message.message.messageType = 0; // no RMR message type yet
1777
1778 buildJsonMessage(message);
1779
1780 break;
1781 }
1782 }
ss412g1a79bdf2019-10-24 12:03:05 +03001783}
1784
1785/**
1786 *
1787 * @param message
1788 * @param requestId
1789 * @param rmrMmessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001790 * @return
1791 */
1792int sendRequestToXapp(ReportingMessages_t &message,
1793 int requestId,
aa7133@att.combe92d712020-03-18 17:30:39 +02001794 RmrMessagesBuffer_t &rmrMmessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001795 rmr_bytes2meid(rmrMmessageBuffer.sendMessage,
1796 (unsigned char *)message.message.enodbName,
1797 strlen(message.message.enodbName));
1798 message.message.messageType = rmrMmessageBuffer.sendMessage->mtype = requestId;
1799 rmrMmessageBuffer.sendMessage->state = 0;
1800 static unsigned char tx[32];
1801 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1802 rmr_bytes2xact(rmrMmessageBuffer.sendMessage, tx, strlen((const char *) tx));
1803
aa7133@att.combe92d712020-03-18 17:30:39 +02001804 auto rc = sendRmrMessage(rmrMmessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001805 return rc;
1806}
1807
1808
aa7133@att.combe92d712020-03-18 17:30:39 +02001809void getRmrContext(sctp_params_t &pSctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +02001810 pSctpParams.rmrCtx = nullptr;
1811 pSctpParams.rmrCtx = rmr_init(pSctpParams.rmrAddress, RMR_MAX_RCV_BYTES, RMRFL_NONE);
1812 if (pSctpParams.rmrCtx == nullptr) {
1813 mdclog_write(MDCLOG_ERR, "Failed to initialize RMR");
ss412g3bac2da2020-01-05 11:52:19 +02001814 return;
ss412g1a79bdf2019-10-24 12:03:05 +03001815 }
1816
ss412g3bac2da2020-01-05 11:52:19 +02001817 rmr_set_stimeout(pSctpParams.rmrCtx, 0); // disable retries for any send operation
ss412g1a79bdf2019-10-24 12:03:05 +03001818 // we need to find that routing table exist and we can run
1819 if (mdclog_level_get() >= MDCLOG_INFO) {
1820 mdclog_write(MDCLOG_INFO, "We are after RMR INIT wait for RMR_Ready");
1821 }
1822 int rmrReady = 0;
1823 int count = 0;
1824 while (!rmrReady) {
ss412g3bac2da2020-01-05 11:52:19 +02001825 if ((rmrReady = rmr_ready(pSctpParams.rmrCtx)) == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001826 sleep(1);
1827 }
1828 count++;
1829 if (count % 60 == 0) {
1830 mdclog_write(MDCLOG_INFO, "waiting to RMR ready state for %d seconds", count);
1831 }
1832 }
1833 if (mdclog_level_get() >= MDCLOG_INFO) {
1834 mdclog_write(MDCLOG_INFO, "RMR running");
1835 }
ss412g3bac2da2020-01-05 11:52:19 +02001836 rmr_init_trace(pSctpParams.rmrCtx, 200);
1837 // get the RMR fd for the epoll
1838 pSctpParams.rmrListenFd = rmr_get_rcvfd(pSctpParams.rmrCtx);
1839 struct epoll_event event{};
1840 // add RMR fd to epoll
1841 event.events = (EPOLLIN);
1842 event.data.fd = pSctpParams.rmrListenFd;
1843 // add listening RMR FD to epoll
1844 if (epoll_ctl(pSctpParams.epoll_fd, EPOLL_CTL_ADD, pSctpParams.rmrListenFd, &event)) {
1845 mdclog_write(MDCLOG_ERR, "Failed to add RMR descriptor to epoll");
1846 close(pSctpParams.rmrListenFd);
1847 rmr_close(pSctpParams.rmrCtx);
1848 pSctpParams.rmrCtx = nullptr;
1849 }
ss412g1a79bdf2019-10-24 12:03:05 +03001850}
1851
1852/**
1853 *
1854 * @param epoll_fd
1855 * @param sctpMap
1856 * @param rmrMessageBuffer
1857 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03001858 * @return
1859 */
1860int receiveXappMessages(int epoll_fd,
1861 Sctp_Map_t *sctpMap,
1862 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001863 struct timespec &ts) {
ss412g1a79bdf2019-10-24 12:03:05 +03001864 if (rmrMessageBuffer.rcvMessage == nullptr) {
1865 //we have error
1866 mdclog_write(MDCLOG_ERR, "RMR Allocation message, %s", strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +03001867 return -1;
1868 }
1869
1870 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1871 mdclog_write(MDCLOG_DEBUG, "Call to rmr_rcv_msg");
1872 }
1873 rmrMessageBuffer.rcvMessage = rmr_rcv_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
1874 if (rmrMessageBuffer.rcvMessage == nullptr) {
1875 mdclog_write(MDCLOG_ERR, "RMR Receving message with null pointer, Realloc rmr mesage buffer");
1876 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03001877 return -2;
1878 }
1879 ReportingMessages_t message;
1880 message.message.direction = 'D';
1881 message.message.time.tv_nsec = ts.tv_nsec;
1882 message.message.time.tv_sec = ts.tv_sec;
1883
1884 // get message payload
1885 //auto msgData = msg->payload;
1886 if (rmrMessageBuffer.rcvMessage->state != 0) {
1887 mdclog_write(MDCLOG_ERR, "RMR Receving message with stat = %d", rmrMessageBuffer.rcvMessage->state);
ss412g1a79bdf2019-10-24 12:03:05 +03001888 return -1;
1889 }
1890 switch (rmrMessageBuffer.rcvMessage->mtype) {
aa7133@att.combe92d712020-03-18 17:30:39 +02001891// case RIC_X2_SETUP_REQ: {
1892// if (connectToCUandSetUp(rmrMessageBuffer, message, epoll_fd, sctpMap) != 0) {
1893// mdclog_write(MDCLOG_ERR, "ERROR in connectToCUandSetUp on RIC_X2_SETUP_REQ");
1894// message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_SCTP_CONNECTION_FAILURE;
1895// message.message.direction = 'N';
1896// message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1897// snprintf((char *)rmrMessageBuffer.sendMessage->payload,
1898// 256,
1899// "ERROR in connectToCUandSetUp on RIC_X2_SETUP_REQ");
1900// rmrMessageBuffer.sendMessage->state = 0;
1901// message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1902//
1903// if (mdclog_level_get() >= MDCLOG_DEBUG) {
1904// mdclog_write(MDCLOG_DEBUG, "start writing to rmr buffer");
1905// }
1906// rmr_bytes2xact(rmrMessageBuffer.sendMessage, rmrMessageBuffer.rcvMessage->xaction, RMR_MAX_XID);
1907// rmr_str2meid(rmrMessageBuffer.sendMessage, (unsigned char *)message.message.enodbName);
1908//
1909// sendRmrMessage(rmrMessageBuffer, message);
1910// return -3;
1911// }
1912// break;
1913// }
1914// case RIC_ENDC_X2_SETUP_REQ: {
1915// if (connectToCUandSetUp(rmrMessageBuffer, message, epoll_fd, sctpMap) != 0) {
1916// mdclog_write(MDCLOG_ERR, "ERROR in connectToCUandSetUp on RIC_ENDC_X2_SETUP_REQ");
1917// message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_SCTP_CONNECTION_FAILURE;
1918// message.message.direction = 'N';
1919// message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1920// snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1921// "ERROR in connectToCUandSetUp on RIC_ENDC_X2_SETUP_REQ");
1922// rmrMessageBuffer.sendMessage->state = 0;
1923// message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1924//
1925// if (mdclog_level_get() >= MDCLOG_DEBUG) {
1926// mdclog_write(MDCLOG_DEBUG, "start writing to rmr buffer");
1927// }
1928//
1929// rmr_bytes2xact(rmrMessageBuffer.sendMessage, rmrMessageBuffer.rcvMessage->xaction, RMR_MAX_XID);
1930// rmr_str2meid(rmrMessageBuffer.sendMessage, (unsigned char *) message.message.enodbName);
1931//
1932// sendRmrMessage(rmrMessageBuffer, message);
1933// return -3;
1934// }
1935// break;
1936// }
1937// case RIC_ENDC_CONF_UPDATE: {
1938// if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
1939// mdclog_write(MDCLOG_ERR, "Failed to send RIC_ENDC_CONF_UPDATE");
1940// return -4;
1941// }
1942// break;
1943// }
1944// case RIC_ENDC_CONF_UPDATE_ACK: {
1945// if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
1946// mdclog_write(MDCLOG_ERR, "Failed to send RIC_ENDC_CONF_UPDATE_ACK");
1947// return -4;
1948// }
1949// break;
1950// }
1951// case RIC_ENDC_CONF_UPDATE_FAILURE: {
1952// if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
1953// mdclog_write(MDCLOG_ERR, "Failed to send RIC_ENDC_CONF_UPDATE_FAILURE");
1954// return -4;
1955// }
1956// break;
1957// }
1958// case RIC_ENB_CONF_UPDATE: {
1959// if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
1960// mdclog_write(MDCLOG_ERR, "Failed to send RIC_ENDC_CONF_UPDATE");
1961// return -4;
1962// }
1963// break;
1964// }
1965// case RIC_ENB_CONF_UPDATE_ACK: {
1966// if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
1967// mdclog_write(MDCLOG_ERR, "Failed to send RIC_ENB_CONF_UPDATE_ACK");
1968// return -4;
1969// }
1970// break;
1971// }
1972// case RIC_ENB_CONF_UPDATE_FAILURE: {
1973// if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
1974// mdclog_write(MDCLOG_ERR, "Failed to send RIC_ENB_CONF_UPDATE_FAILURE");
1975// return -4;
1976// }
1977// break;
1978// }
1979// case RIC_RES_STATUS_REQ: {
1980// if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
1981// mdclog_write(MDCLOG_ERR, "Failed to send RIC_RES_STATUS_REQ");
1982// return -6;
1983// }
1984// break;
1985// }
ss412g1a79bdf2019-10-24 12:03:05 +03001986
aa7133@att.combe92d712020-03-18 17:30:39 +02001987 case RIC_E2_SETUP_RESP : {
1988 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
1989 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_RESP");
1990 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03001991 }
1992 break;
1993 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001994 case RIC_E2_SETUP_FAILURE : {
1995 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
1996 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_FAILURE");
1997 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03001998 }
1999 break;
2000 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002001 case RIC_ERROR_INDICATION: {
2002 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2003 mdclog_write(MDCLOG_ERR, "Failed to send RIC_ERROR_INDICATION");
ss412g1a79bdf2019-10-24 12:03:05 +03002004 return -6;
2005 }
2006 break;
2007 }
2008 case RIC_SUB_REQ: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002009 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002010 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002011 return -6;
2012 }
2013 break;
2014 }
2015 case RIC_SUB_DEL_REQ: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002016 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002017 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_DEL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002018 return -6;
2019 }
2020 break;
2021 }
2022 case RIC_CONTROL_REQ: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002023 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002024 mdclog_write(MDCLOG_ERR, "Failed to send RIC_CONTROL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002025 return -6;
2026 }
2027 break;
2028 }
2029 case RIC_SERVICE_QUERY: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002030 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002031 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_QUERY");
ss412g1a79bdf2019-10-24 12:03:05 +03002032 return -6;
2033 }
2034 break;
2035 }
2036 case RIC_SERVICE_UPDATE_ACK: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002037 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002038 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_ACK");
ss412g1a79bdf2019-10-24 12:03:05 +03002039 return -6;
2040 }
2041 break;
2042 }
2043 case RIC_SERVICE_UPDATE_FAILURE: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002044 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002045 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_FAILURE");
ss412g1a79bdf2019-10-24 12:03:05 +03002046 return -6;
2047 }
2048 break;
2049 }
2050 case RIC_X2_RESET: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002051 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002052 mdclog_write(MDCLOG_ERR, "Failed to send RIC_X2_RESET");
ss412g1a79bdf2019-10-24 12:03:05 +03002053 return -6;
2054 }
2055 break;
2056 }
2057 case RIC_X2_RESET_RESP: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002058 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002059 mdclog_write(MDCLOG_ERR, "Failed to send RIC_X2_RESET_RESP");
ss412g1a79bdf2019-10-24 12:03:05 +03002060 return -6;
2061 }
2062 break;
2063 }
2064 case RIC_SCTP_CLEAR_ALL: {
2065 mdclog_write(MDCLOG_INFO, "RIC_SCTP_CLEAR_ALL");
2066 // loop on all keys and close socket and then erase all map.
2067 vector<char *> v;
2068 sctpMap->getKeys(v);
2069 for (auto const &iter : v) { //}; iter != sctpMap.end(); iter++) {
2070 if (!boost::starts_with((string) (iter), "host:") && !boost::starts_with((string) (iter), "msg:")) {
2071 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(iter);
2072 if (peerInfo == nullptr) {
2073 continue;
2074 }
2075 close(peerInfo->fileDescriptor);
2076 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
2077 message.message.direction = 'D';
2078 message.message.time.tv_nsec = ts.tv_nsec;
2079 message.message.time.tv_sec = ts.tv_sec;
2080
2081 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
2082 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
2083 256,
2084 "%s|RIC_SCTP_CLEAR_ALL",
2085 peerInfo->enodbName);
2086 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
2087 mdclog_write(MDCLOG_INFO, "%s", message.message.asndata);
aa7133@att.combe92d712020-03-18 17:30:39 +02002088 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002089 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
2090 }
2091 free(peerInfo);
2092 }
2093 }
2094
2095 sleep(1);
2096 sctpMap->clear();
2097 break;
2098 }
ss412g3bac2da2020-01-05 11:52:19 +02002099 case E2_TERM_KEEP_ALIVE_REQ: {
2100 // send message back
ss412g3bac2da2020-01-05 11:52:19 +02002101 rmr_bytes2payload(rmrMessageBuffer.sendMessage,
2102 (unsigned char *)rmrMessageBuffer.ka_message,
2103 rmrMessageBuffer.ka_message_len);
2104 rmrMessageBuffer.sendMessage->mtype = E2_TERM_KEEP_ALIVE_RESP;
2105 rmrMessageBuffer.sendMessage->state = 0;
2106 static unsigned char tx[32];
2107 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2108 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, txLen);
2109 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2110 if (rmrMessageBuffer.sendMessage == nullptr) {
2111 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
aa7133@att.com77fff432020-01-27 12:01:20 +02002112 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP RMR message returned NULL");
ss412g3bac2da2020-01-05 11:52:19 +02002113 } else if (rmrMessageBuffer.sendMessage->state != 0) {
aa7133@att.com77fff432020-01-27 12:01:20 +02002114 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP, on RMR state = %d ( %s)",
2115 rmrMessageBuffer.sendMessage->state, translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str());
aa7133@att.com342843e2020-01-26 12:53:34 +02002116 } else if (mdclog_level_get() >= MDCLOG_INFO) {
2117 mdclog_write(MDCLOG_INFO, "Got Keep Alive Request send : %s", rmrMessageBuffer.ka_message);
ss412g3bac2da2020-01-05 11:52:19 +02002118 }
aa7133@att.com342843e2020-01-26 12:53:34 +02002119
ss412g3bac2da2020-01-05 11:52:19 +02002120 break;
2121 }
ss412g1a79bdf2019-10-24 12:03:05 +03002122 default:
2123 mdclog_write(MDCLOG_WARN, "Message Type : %d is not seported", rmrMessageBuffer.rcvMessage->mtype);
2124 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
2125 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
2126 message.message.time.tv_nsec = ts.tv_nsec;
2127 message.message.time.tv_sec = ts.tv_sec;
2128 message.message.messageType = rmrMessageBuffer.rcvMessage->mtype;
2129
2130 buildJsonMessage(message);
2131
2132
ss412g1a79bdf2019-10-24 12:03:05 +03002133 return -7;
2134 }
2135 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2136 mdclog_write(MDCLOG_DEBUG, "EXIT OK from %s", __FUNCTION__);
2137 }
ss412g1a79bdf2019-10-24 12:03:05 +03002138 return 0;
2139}
2140
2141/**
2142 * Send message to the CU that is not expecting for successful or unsuccessful results
2143 * @param messageBuffer
2144 * @param message
2145 * @param failedMsgId
2146 * @param sctpMap
ss412g1a79bdf2019-10-24 12:03:05 +03002147 * @return
2148 */
2149int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messageBuffer,
2150 ReportingMessages_t &message,
2151 int failedMsgId,
aa7133@att.combe92d712020-03-18 17:30:39 +02002152 Sctp_Map_t *sctpMap) {
ss412g1a79bdf2019-10-24 12:03:05 +03002153
aa7133@att.combe92d712020-03-18 17:30:39 +02002154 getRequestMetaData(message, messageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03002155 if (mdclog_level_get() >= MDCLOG_INFO) {
2156 mdclog_write(MDCLOG_INFO, "send message to %s address", message.message.enodbName);
2157 }
2158
aa7133@att.combe92d712020-03-18 17:30:39 +02002159 auto rc = sendMessagetoCu(sctpMap, messageBuffer, message, failedMsgId);
ss412g1a79bdf2019-10-24 12:03:05 +03002160 return rc;
2161}
2162
2163/**
2164 *
2165 * @param sctpMap
2166 * @param messageBuffer
2167 * @param message
2168 * @param failedMesgId
ss412g1a79bdf2019-10-24 12:03:05 +03002169 * @return
2170 */
2171int sendMessagetoCu(Sctp_Map_t *sctpMap,
2172 RmrMessagesBuffer_t &messageBuffer,
2173 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02002174 int failedMesgId) {
ss412g1a79bdf2019-10-24 12:03:05 +03002175 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(message.message.enodbName);
2176 if (peerInfo == nullptr) {
2177 if (failedMesgId != 0) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002178 sendFailedSendingMessagetoXapp(messageBuffer, message, failedMesgId);
ss412g1a79bdf2019-10-24 12:03:05 +03002179 } else {
2180 mdclog_write(MDCLOG_ERR, "Failed to send message no CU entry %s", message.message.enodbName);
2181 }
ss412g1a79bdf2019-10-24 12:03:05 +03002182 return -1;
2183 }
2184
2185 // get the FD
2186 message.message.messageType = messageBuffer.rcvMessage->mtype;
aa7133@att.combe92d712020-03-18 17:30:39 +02002187 auto rc = sendSctpMsg(peerInfo, message, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002188 return rc;
2189}
2190
2191/**
2192 *
2193 * @param rmrCtx the rmr context to send and receive
2194 * @param msg the msg we got fromxApp
2195 * @param metaData data from xApp in ordered struct
2196 * @param failedMesgId the return message type error
2197 */
2198void
aa7133@att.combe92d712020-03-18 17:30:39 +02002199sendFailedSendingMessagetoXapp(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message, int failedMesgId) {
ss412g1a79bdf2019-10-24 12:03:05 +03002200 rmr_mbuf_t *msg = rmrMessageBuffer.sendMessage;
2201 msg->len = snprintf((char *) msg->payload, 200, "the gNb/eNode name %s not found",
2202 message.message.enodbName);
2203 if (mdclog_level_get() >= MDCLOG_INFO) {
2204 mdclog_write(MDCLOG_INFO, "%s", msg->payload);
2205 }
2206 msg->mtype = failedMesgId;
2207 msg->state = 0;
2208
2209 static unsigned char tx[32];
2210 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2211 rmr_bytes2xact(msg, tx, strlen((const char *) tx));
2212
aa7133@att.combe92d712020-03-18 17:30:39 +02002213 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03002214}
2215
ss412g1a79bdf2019-10-24 12:03:05 +03002216
2217/**
2218 * build the SCTP connection to eNodB or gNb
2219 * @param rmrMessageBuffer
2220 * @param message
2221 * @param epoll_fd
2222 * @param sctpMap
ss412g1a79bdf2019-10-24 12:03:05 +03002223 * @return
2224 */
2225int connectToCUandSetUp(RmrMessagesBuffer_t &rmrMessageBuffer,
2226 ReportingMessages_t &message,
2227 int epoll_fd,
aa7133@att.combe92d712020-03-18 17:30:39 +02002228 Sctp_Map_t *sctpMap) {
ss412g1a79bdf2019-10-24 12:03:05 +03002229 struct sockaddr_in6 servaddr{};
2230 struct addrinfo hints{}, *result;
2231 auto msgData = rmrMessageBuffer.rcvMessage->payload;
2232 unsigned char meid[RMR_MAX_MEID]{};
2233 char host[256]{};
2234 uint16_t port = 0;
2235
2236 message.message.messageType = rmrMessageBuffer.rcvMessage->mtype;
2237 rmr_mbuf_t *msg = rmrMessageBuffer.rcvMessage;
2238 rmr_get_meid(msg, meid);
2239
2240 if (mdclog_level_get() >= MDCLOG_INFO) {
2241 mdclog_write(MDCLOG_INFO, "message %d Received for MEID :%s. SETUP/EN-DC Setup Request from xApp, Message = %s",
2242 msg->mtype, meid, msgData);
2243 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002244 if (getSetupRequestMetaData(message, (char *)msgData, host, port) < 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002245 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2246 mdclog_write(MDCLOG_DEBUG, "Error in setup parameters %s, %d", __func__, __LINE__);
2247 }
ss412g1a79bdf2019-10-24 12:03:05 +03002248 return -1;
2249 }
2250
2251 //// message asndata points to the start of the asndata of the message and not to start of payload
2252 // search if the same host:port but not the same enodbname
2253 char searchBuff[256]{};
2254 snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", host, port);
2255 auto e = (char *)sctpMap->find(searchBuff);
2256 if (e != nullptr) {
2257 // found one compare if not the same
2258 if (strcmp(message.message.enodbName, e) != 0) {
2259 mdclog_write(MDCLOG_ERR,
2260 "Try to connect CU %s to Host %s but %s already connected",
2261 message.message.enodbName, host, e);
ss412g1a79bdf2019-10-24 12:03:05 +03002262 return -1;
2263 }
2264 }
2265
aa7133@att.com84bd3342020-03-16 18:04:57 +02002266 // check if not connected. if connected send the request and return
ss412g1a79bdf2019-10-24 12:03:05 +03002267 auto *peerInfo = (ConnectedCU_t *)sctpMap->find(message.message.enodbName);
2268 if (peerInfo != nullptr) {
ss412g1a79bdf2019-10-24 12:03:05 +03002269 if (mdclog_level_get() >= MDCLOG_INFO) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002270 mdclog_write(MDCLOG_INFO, "Device already connected to %s",
ss412g1a79bdf2019-10-24 12:03:05 +03002271 message.message.enodbName);
2272 }
2273 message.message.messageType = msg->mtype;
aa7133@att.combe92d712020-03-18 17:30:39 +02002274 auto rc = sendSctpMsg(peerInfo, message, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002275 if (rc != 0) {
2276 mdclog_write(MDCLOG_ERR, "failed write to SCTP %s, %d", __func__, __LINE__);
ss412g1a79bdf2019-10-24 12:03:05 +03002277 return -1;
2278 }
2279
2280 char key[MAX_ENODB_NAME_SIZE * 2];
2281 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName, msg->mtype);
2282 int xaction_len = strlen((const char *) msg->xaction);
2283 auto *xaction = (unsigned char *) calloc(1, xaction_len);
2284 memcpy(xaction, msg->xaction, xaction_len);
2285 sctpMap->setkey(key, xaction);
2286 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2287 mdclog_write(MDCLOG_DEBUG, "set key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2288 }
ss412g1a79bdf2019-10-24 12:03:05 +03002289 return 0;
2290 }
2291
2292 peerInfo = (ConnectedCU_t *) calloc(1, sizeof(ConnectedCU_t));
2293 memcpy(peerInfo->enodbName, message.message.enodbName, sizeof(message.message.enodbName));
2294
2295 // new connection
2296 if ((peerInfo->fileDescriptor = socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP)) < 0) {
2297 mdclog_write(MDCLOG_ERR, "Socket Error, %s %s, %d", strerror(errno), __func__, __LINE__);
ss412g1a79bdf2019-10-24 12:03:05 +03002298 return -1;
2299 }
2300
2301 auto optval = 1;
2302 if (setsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof optval) != 0) {
2303 mdclog_write(MDCLOG_ERR, "setsockopt SO_REUSEPORT Error, %s %s, %d", strerror(errno), __func__, __LINE__);
ss412g1a79bdf2019-10-24 12:03:05 +03002304 return -1;
2305 }
2306 optval = 1;
2307 if (setsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval) != 0) {
2308 mdclog_write(MDCLOG_ERR, "setsockopt SO_REUSEADDR Error, %s %s, %d", strerror(errno), __func__, __LINE__);
ss412g1a79bdf2019-10-24 12:03:05 +03002309 return -1;
2310 }
2311 servaddr.sin6_family = AF_INET6;
2312
2313 struct sockaddr_in6 localAddr {};
2314 localAddr.sin6_family = AF_INET6;
2315 localAddr.sin6_addr = in6addr_any;
2316 localAddr.sin6_port = htons(SRC_PORT);
2317
2318 if (bind(peerInfo->fileDescriptor, (struct sockaddr*)&localAddr , sizeof(struct sockaddr_in6)) < 0) {
2319 mdclog_write(MDCLOG_ERR, "bind Socket Error, %s %s, %d", strerror(errno), __func__, __LINE__);
ss412g1a79bdf2019-10-24 12:03:05 +03002320 return -1;
2321 }//Ends the binding.
2322
2323 memset(&hints, 0, sizeof hints);
2324 hints.ai_flags = AI_NUMERICHOST;
2325 if (getaddrinfo(host, nullptr, &hints, &result) < 0) {
2326 close(peerInfo->fileDescriptor);
2327 mdclog_write(MDCLOG_ERR, "getaddrinfo error for %s, Error = %s", host, strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +03002328 return -1;
2329 }
2330 memcpy(&servaddr, result->ai_addr, sizeof(struct sockaddr_in6));
2331 freeaddrinfo(result);
2332
2333 servaddr.sin6_port = htons(port); /* daytime server */
2334 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2335 mdclog_write(MDCLOG_DEBUG, "Send Connect FD = %d host : %s port %d",
2336 peerInfo->fileDescriptor,
2337 host,
2338 port);
2339 }
2340
2341 // Add to Epol
2342 if (addToEpoll(epoll_fd, peerInfo, (EPOLLOUT | EPOLLIN | EPOLLET), sctpMap, message.message.enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002343 msg->mtype) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002344 return -1;
2345 }
2346
2347 char hostBuff[NI_MAXHOST];
2348 char portBuff[NI_MAXHOST];
2349
2350 if (getnameinfo((SA *) &servaddr, sizeof(servaddr),
2351 hostBuff, sizeof(hostBuff),
2352 portBuff, sizeof(portBuff),
2353 (uint) (NI_NUMERICHOST) | (uint) (NI_NUMERICSERV)) != 0) {
2354 mdclog_write(MDCLOG_ERR, "getnameinfo() Error, %s %s %d", strerror(errno), __func__, __LINE__);
ss412g1a79bdf2019-10-24 12:03:05 +03002355 return -1;
2356 }
2357
2358 if (setSocketNoBlocking(peerInfo->fileDescriptor) != 0) {
2359 mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on sctpPort %s", hostBuff,
2360 portBuff);
2361 close(peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03002362 return -1;
2363 }
2364
2365 memcpy(peerInfo->hostName, hostBuff, strlen(hostBuff));
2366 peerInfo->hostName[strlen(hostBuff)] = 0;
2367 memcpy(peerInfo->portNumber, portBuff, strlen(portBuff));
2368 peerInfo->portNumber[strlen(portBuff)] = 0;
2369
2370 // map by enoodb/gnb name
2371 sctpMap->setkey(message.message.enodbName, peerInfo);
2372 //map host and port to enodeb
2373 sctpMap->setkey(searchBuff, message.message.enodbName);
2374
2375 // save message for the return values
2376 char key[MAX_ENODB_NAME_SIZE * 2];
2377 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName, msg->mtype);
2378 int xaction_len = strlen((const char *) msg->xaction);
2379 auto *xaction = (unsigned char *) calloc(1, xaction_len);
2380 memcpy(xaction, msg->xaction, xaction_len);
2381 sctpMap->setkey(key, xaction);
2382 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2383 mdclog_write(MDCLOG_DEBUG, "End building peerinfo: %s for CU %s", key, message.message.enodbName);
2384 }
2385
2386 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2387 mdclog_write(MDCLOG_DEBUG, "Send connect to FD %d, %s, %d",
2388 peerInfo->fileDescriptor, __func__, __LINE__);
2389 }
2390 if (connect(peerInfo->fileDescriptor, (SA *) &servaddr, sizeof(servaddr)) < 0) {
2391 if (errno != EINPROGRESS) {
2392 mdclog_write(MDCLOG_ERR, "connect FD %d to host : %s port %d, %s",
2393 peerInfo->fileDescriptor, host, port, strerror(errno));
2394 close(peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03002395 return -1;
2396 }
2397 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2398 mdclog_write(MDCLOG_DEBUG,
2399 "Connect to FD %d returned with EINPROGRESS : %s",
2400 peerInfo->fileDescriptor, strerror(errno));
2401 }
2402 // since message.message.asndata is pointing to the asndata in the rmr message payload we copy it like this
2403 memcpy(peerInfo->asnData, message.message.asndata, message.message.asnLength);
2404 peerInfo->asnLength = message.message.asnLength;
2405 peerInfo->mtype = msg->mtype;
ss412g1a79bdf2019-10-24 12:03:05 +03002406 return 0;
2407 }
2408
2409 if (mdclog_level_get() >= MDCLOG_INFO) {
2410 mdclog_write(MDCLOG_INFO, "Connect to FD %d returned OK without EINPROGRESS", peerInfo->fileDescriptor);
2411 }
2412
2413 peerInfo->isConnected = true;
2414
aa7133@att.combe92d712020-03-18 17:30:39 +02002415 if (modifyToEpoll(epoll_fd, peerInfo, (EPOLLIN | EPOLLET), sctpMap, message.message.enodbName, msg->mtype) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002416 return -1;
2417 }
2418
2419 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2420 mdclog_write(MDCLOG_DEBUG, "Connected to host : %s port %d", host, port);
2421 }
2422
2423 message.message.messageType = msg->mtype;
2424 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2425 mdclog_write(MDCLOG_DEBUG, "Send SCTP message to FD %d", peerInfo->fileDescriptor);
2426 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002427 if (sendSctpMsg(peerInfo, message, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002428 mdclog_write(MDCLOG_ERR, "Error write to SCTP %s %d", __func__, __LINE__);
ss412g1a79bdf2019-10-24 12:03:05 +03002429 return -1;
2430 }
2431 memset(peerInfo->asnData, 0, message.message.asnLength);
2432 peerInfo->asnLength = 0;
2433 peerInfo->mtype = 0;
2434
2435 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2436 mdclog_write(MDCLOG_DEBUG, "Sent message to SCTP for %s", message.message.enodbName);
2437 }
ss412g1a79bdf2019-10-24 12:03:05 +03002438 return 0;
2439}
2440
2441/**
2442 *
2443 * @param epoll_fd
2444 * @param peerInfo
2445 * @param events
2446 * @param sctpMap
2447 * @param enodbName
2448 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002449 * @return
2450 */
2451int addToEpoll(int epoll_fd,
2452 ConnectedCU_t *peerInfo,
2453 uint32_t events,
2454 Sctp_Map_t *sctpMap,
2455 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002456 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002457 // Add to Epol
2458 struct epoll_event event{};
2459 event.data.ptr = peerInfo;
2460 event.events = events;
2461 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peerInfo->fileDescriptor, &event) < 0) {
2462 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2463 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here), %s, %s %d",
2464 strerror(errno), __func__, __LINE__);
2465 }
2466 close(peerInfo->fileDescriptor);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002467 if (enodbName != nullptr) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002468 cleanHashEntry(peerInfo, sctpMap);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002469 char key[MAX_ENODB_NAME_SIZE * 2];
2470 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2471 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2472 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2473 }
2474 auto tmp = sctpMap->find(key);
2475 if (tmp) {
2476 free(tmp);
2477 sctpMap->erase(key);
2478 }
2479 } else {
2480 peerInfo->enodbName[0] = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03002481 }
ss412g1a79bdf2019-10-24 12:03:05 +03002482 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002483 return -1;
2484 }
ss412g1a79bdf2019-10-24 12:03:05 +03002485 return 0;
2486}
2487
2488/**
2489 *
2490 * @param epoll_fd
2491 * @param peerInfo
2492 * @param events
2493 * @param sctpMap
2494 * @param enodbName
2495 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002496 * @return
2497 */
2498int modifyToEpoll(int epoll_fd,
2499 ConnectedCU_t *peerInfo,
2500 uint32_t events,
2501 Sctp_Map_t *sctpMap,
2502 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002503 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002504 // Add to Epol
2505 struct epoll_event event{};
2506 event.data.ptr = peerInfo;
2507 event.events = events;
2508 if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, peerInfo->fileDescriptor, &event) < 0) {
2509 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2510 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_MOD (may chack not to quit here), %s, %s %d",
2511 strerror(errno), __func__, __LINE__);
2512 }
2513 close(peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02002514 cleanHashEntry(peerInfo, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002515 char key[MAX_ENODB_NAME_SIZE * 2];
2516 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2517 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2518 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2519 }
2520 auto tmp = sctpMap->find(key);
2521 if (tmp) {
2522 free(tmp);
2523 }
2524 sctpMap->erase(key);
2525 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002526 return -1;
2527 }
ss412g1a79bdf2019-10-24 12:03:05 +03002528 return 0;
2529}
2530
2531
aa7133@att.combe92d712020-03-18 17:30:39 +02002532int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message) {
ss412g1a79bdf2019-10-24 12:03:05 +03002533 buildJsonMessage(message);
2534
2535 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2536
2537 if (rmrMessageBuffer.sendMessage == nullptr) {
2538 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2539 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
ss412g1a79bdf2019-10-24 12:03:05 +03002540 return -1;
2541 }
2542
2543 if (rmrMessageBuffer.sendMessage->state != 0) {
2544 char meid[RMR_MAX_MEID]{};
2545 if (rmrMessageBuffer.sendMessage->state == RMR_ERR_RETRY) {
2546 usleep(5);
2547 rmrMessageBuffer.sendMessage->state = 0;
2548 mdclog_write(MDCLOG_INFO, "RETRY sending Message type %d to Xapp from %s",
2549 rmrMessageBuffer.sendMessage->mtype,
2550 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2551 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2552 if (rmrMessageBuffer.sendMessage == nullptr) {
2553 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
2554 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03002555 return -1;
2556 } else if (rmrMessageBuffer.sendMessage->state != 0) {
2557 mdclog_write(MDCLOG_ERR,
2558 "Message state %s while sending request %d to Xapp from %s after retry of 10 microseconds",
2559 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2560 rmrMessageBuffer.sendMessage->mtype,
2561 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2562 auto rc = rmrMessageBuffer.sendMessage->state;
ss412g1a79bdf2019-10-24 12:03:05 +03002563 return rc;
2564 }
2565 } else {
2566 mdclog_write(MDCLOG_ERR, "Message state %s while sending request %d to Xapp from %s",
2567 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2568 rmrMessageBuffer.sendMessage->mtype,
2569 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
ss412g1a79bdf2019-10-24 12:03:05 +03002570 return rmrMessageBuffer.sendMessage->state;
2571 }
2572 }
2573 return 0;
2574}
2575
2576void buildJsonMessage(ReportingMessages_t &message) {
ss412g3bac2da2020-01-05 11:52:19 +02002577 if (jsonTrace) {
2578 message.outLen = sizeof(message.base64Data);
2579 base64::encode((const unsigned char *) message.message.asndata,
2580 (const int) message.message.asnLength,
2581 message.base64Data,
2582 message.outLen);
2583 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2584 mdclog_write(MDCLOG_DEBUG, "asn data length = %d, base64 message length = %d ",
2585 (int) message.message.asnLength,
2586 (int) message.outLen);
2587 }
2588
2589 snprintf(message.buffer, sizeof(message.buffer),
2590 "{\"header\": {\"ts\": \"%ld.%09ld\","
2591 "\"ranName\": \"%s\","
2592 "\"messageType\": %d,"
2593 "\"direction\": \"%c\"},"
2594 "\"base64Length\": %d,"
2595 "\"asnBase64\": \"%s\"}",
2596 message.message.time.tv_sec,
2597 message.message.time.tv_nsec,
2598 message.message.enodbName,
2599 message.message.messageType,
2600 message.message.direction,
2601 (int) message.outLen,
2602 message.base64Data);
2603 static src::logger_mt &lg = my_logger::get();
2604
2605 BOOST_LOG(lg) << message.buffer;
ss412g1a79bdf2019-10-24 12:03:05 +03002606 }
ss412g1a79bdf2019-10-24 12:03:05 +03002607}
2608
2609
2610/**
2611 * take RMR error code to string
2612 * @param state
2613 * @return
2614 */
2615string translateRmrErrorMessages(int state) {
2616 string str = {};
2617 switch (state) {
2618 case RMR_OK:
2619 str = "RMR_OK - state is good";
2620 break;
2621 case RMR_ERR_BADARG:
2622 str = "RMR_ERR_BADARG - argument passd to function was unusable";
2623 break;
2624 case RMR_ERR_NOENDPT:
2625 str = "RMR_ERR_NOENDPT - send//call could not find an endpoint based on msg type";
2626 break;
2627 case RMR_ERR_EMPTY:
2628 str = "RMR_ERR_EMPTY - msg received had no payload; attempt to send an empty message";
2629 break;
2630 case RMR_ERR_NOHDR:
2631 str = "RMR_ERR_NOHDR - message didn't contain a valid header";
2632 break;
2633 case RMR_ERR_SENDFAILED:
2634 str = "RMR_ERR_SENDFAILED - send failed; errno has nano reason";
2635 break;
2636 case RMR_ERR_CALLFAILED:
2637 str = "RMR_ERR_CALLFAILED - unable to send call() message";
2638 break;
2639 case RMR_ERR_NOWHOPEN:
2640 str = "RMR_ERR_NOWHOPEN - no wormholes are open";
2641 break;
2642 case RMR_ERR_WHID:
2643 str = "RMR_ERR_WHID - wormhole id was invalid";
2644 break;
2645 case RMR_ERR_OVERFLOW:
2646 str = "RMR_ERR_OVERFLOW - operation would have busted through a buffer/field size";
2647 break;
2648 case RMR_ERR_RETRY:
2649 str = "RMR_ERR_RETRY - request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)";
2650 break;
2651 case RMR_ERR_RCVFAILED:
2652 str = "RMR_ERR_RCVFAILED - receive failed (hard error)";
2653 break;
2654 case RMR_ERR_TIMEOUT:
2655 str = "RMR_ERR_TIMEOUT - message processing call timed out";
2656 break;
2657 case RMR_ERR_UNSET:
2658 str = "RMR_ERR_UNSET - the message hasn't been populated with a transport buffer";
2659 break;
2660 case RMR_ERR_TRUNC:
2661 str = "RMR_ERR_TRUNC - received message likely truncated";
2662 break;
2663 case RMR_ERR_INITFAILED:
2664 str = "RMR_ERR_INITFAILED - initialisation of something (probably message) failed";
2665 break;
2666 case RMR_ERR_NOTSUPP:
2667 str = "RMR_ERR_NOTSUPP - the request is not supported, or RMr was not initialised for the request";
2668 break;
2669 default:
2670 char buf[128]{};
2671 snprintf(buf, sizeof buf, "UNDOCUMENTED RMR_ERR : %d", state);
2672 str = buf;
2673 break;
2674 }
2675 return str;
2676}
2677
2678