ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1 | // 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 | |
nm755n | 2e26814 | 2019-11-28 16:40:23 +0000 | [diff] [blame] | 16 | // This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 17 | // platform project (RICP). |
| 18 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 19 | // TODO: High-level file comment. |
| 20 | |
| 21 | |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 22 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 23 | #include "sctpThread.h" |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 24 | #include "BuildRunName.h" |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 25 | |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 26 | using namespace std; |
| 27 | //using namespace std::placeholders; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 28 | using namespace boost::filesystem; |
| 29 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 30 | //#ifdef __cplusplus |
| 31 | //extern "C" |
| 32 | //{ |
| 33 | //#endif |
| 34 | |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 35 | // need to expose without the include of gcov |
| 36 | extern "C" void __gcov_flush(void); |
| 37 | |
| 38 | static void catch_function(int signal) { |
| 39 | __gcov_flush(); |
| 40 | exit(signal); |
| 41 | } |
| 42 | |
| 43 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 44 | BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt) |
| 45 | |
| 46 | boost::shared_ptr<sinks::synchronous_sink<sinks::text_file_backend>> boostLogger; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 47 | double cpuClock = 0.0; |
| 48 | bool jsonTrace = true; |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 49 | |
| 50 | void 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 | } |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 57 | auto start_time = std::chrono::high_resolution_clock::now(); |
| 58 | typedef std::chrono::duration<double, std::ratio<1,1>> seconds_t; |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 59 | |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 60 | double age() { |
| 61 | return seconds_t(std::chrono::high_resolution_clock::now() - start_time).count(); |
| 62 | } |
| 63 | |
| 64 | double 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 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 74 | |
| 75 | //std::atomic<int64_t> rmrCounter{0}; |
| 76 | std::atomic<int64_t> num_of_messages{0}; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 77 | std::atomic<int64_t> num_of_XAPP_messages{0}; |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 78 | static long transactionCounter = 0; |
| 79 | |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 80 | int 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.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 85 | servaddr.sin6_port = htons(sctpParams.sctpPort); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 86 | 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)); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 101 | } |
| 102 | |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 103 | if (listen(sctpParams.listenFD, SOMAXCONN) < 0) { |
| 104 | mdclog_write(MDCLOG_ERR, "Error listening. %s\n", strerror(errno)); |
| 105 | return -1; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 106 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 107 | 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; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 116 | } |
| 117 | |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 118 | return 0; |
| 119 | } |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 120 | |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 121 | int buildConfiguration(sctp_params_t &sctpParams) { |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 122 | 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.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 128 | return -1; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 129 | } |
| 130 | } else { |
| 131 | mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str()); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 132 | return -1; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 133 | } |
| 134 | |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 135 | 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.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 139 | return -1; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 140 | } |
| 141 | int rmrPort = conf.getIntValue("nano"); |
| 142 | if (rmrPort == -1) { |
| 143 | mdclog_write(MDCLOG_ERR, "illigal RMR port "); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 144 | return -1; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 145 | } |
| 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.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 173 | return -1; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 174 | } |
| 175 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 176 | char tmpLogFilespec[VOLUME_URL_SIZE]; |
| 177 | tmpLogFilespec[0] = 0; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 178 | 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 | |
| 187 | sctpParams.myIP = conf.getStringValue("local-ip"); |
| 188 | if (sctpParams.myIP.length() == 0) { |
| 189 | mdclog_write(MDCLOG_ERR, "illigal local-ip."); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 190 | return -1; |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 191 | } |
| 192 | |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 193 | int sctpPort = conf.getIntValue("sctp-port"); |
| 194 | if (sctpPort == -1) { |
| 195 | mdclog_write(MDCLOG_ERR, "illigal SCTP port "); |
| 196 | return -1; |
| 197 | } |
| 198 | sctpParams.sctpPort = (uint16_t)sctpPort; |
| 199 | |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 200 | sctpParams.fqdn = conf.getStringValue("external-fqdn"); |
| 201 | if (sctpParams.fqdn.length() == 0) { |
| 202 | mdclog_write(MDCLOG_ERR, "illigal external-fqdn"); |
| 203 | return -1; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 204 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 205 | |
aa7133@att.com | 342843e | 2020-01-26 12:53:34 +0200 | [diff] [blame] | 206 | std::string pod = conf.getStringValue("pod_name"); |
| 207 | if (pod.length() == 0) { |
| 208 | mdclog_write(MDCLOG_ERR, "illigal pod_name in config file"); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 209 | return -1; |
aa7133@att.com | 342843e | 2020-01-26 12:53:34 +0200 | [diff] [blame] | 210 | } |
| 211 | auto *podName = getenv(pod.c_str()); |
| 212 | if (podName == nullptr) { |
| 213 | mdclog_write(MDCLOG_ERR, "illigal pod_name or environment varible not exists : %s", pod.c_str()); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 214 | return -1; |
aa7133@att.com | 342843e | 2020-01-26 12:53:34 +0200 | [diff] [blame] | 215 | |
| 216 | } else { |
| 217 | sctpParams.podName.assign(podName); |
| 218 | if (sctpParams.podName.length() == 0) { |
| 219 | mdclog_write(MDCLOG_ERR, "illigal pod_name"); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 220 | return -1; |
aa7133@att.com | 342843e | 2020-01-26 12:53:34 +0200 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 224 | tmpStr = conf.getStringValue("trace"); |
| 225 | transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower); |
| 226 | if ((tmpStr.compare("start")) == 0) { |
| 227 | mdclog_write(MDCLOG_INFO, "Trace set to: start"); |
| 228 | sctpParams.trace = true; |
| 229 | } else if ((tmpStr.compare("stop")) == 0) { |
| 230 | mdclog_write(MDCLOG_INFO, "Trace set to: stop"); |
| 231 | sctpParams.trace = false; |
| 232 | } |
| 233 | jsonTrace = sctpParams.trace; |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 234 | |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 235 | sctpParams.ka_message_length = snprintf(sctpParams.ka_message, KA_MESSAGE_SIZE, "{\"address\": \"%s:%d\"," |
aa7133@att.com | 342843e | 2020-01-26 12:53:34 +0200 | [diff] [blame] | 236 | "\"fqdn\": \"%s\"," |
| 237 | "\"pod_name\": \"%s\"}", |
| 238 | (const char *)sctpParams.myIP.c_str(), |
| 239 | sctpParams.rmrPort, |
| 240 | sctpParams.fqdn.c_str(), |
| 241 | sctpParams.podName.c_str()); |
| 242 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 243 | if (mdclog_level_get() >= MDCLOG_INFO) { |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 244 | mdclog_mdc_add("RMR Port", to_string(sctpParams.rmrPort).c_str()); |
| 245 | mdclog_mdc_add("LogLevel", to_string(sctpParams.logLevel).c_str()); |
| 246 | mdclog_mdc_add("volume", sctpParams.volume); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 247 | mdclog_mdc_add("tmpLogFilespec", tmpLogFilespec); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 248 | mdclog_mdc_add("my ip", sctpParams.myIP.c_str()); |
aa7133@att.com | 342843e | 2020-01-26 12:53:34 +0200 | [diff] [blame] | 249 | mdclog_mdc_add("pod name", sctpParams.podName.c_str()); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 250 | |
aa7133@att.com | 342843e | 2020-01-26 12:53:34 +0200 | [diff] [blame] | 251 | mdclog_write(MDCLOG_INFO, "running parameters for instance : %s", sctpParams.ka_message); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 252 | } |
| 253 | mdclog_mdc_clean(); |
| 254 | |
| 255 | // Files written to the current working directory |
| 256 | boostLogger = logging::add_file_log( |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 257 | keywords::file_name = tmpLogFilespec, // to temp directory |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 258 | keywords::rotation_size = 10 * 1024 * 1024, |
| 259 | keywords::time_based_rotation = sinks::file::rotation_at_time_interval(posix_time::hours(1)), |
| 260 | keywords::format = "%Message%" |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 261 | //keywords::format = "[%TimeStamp%]: %Message%" // use each tmpStr with time stamp |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 262 | ); |
| 263 | |
| 264 | // Setup a destination folder for collecting rotated (closed) files --since the same volumn can use rename() |
| 265 | boostLogger->locked_backend()->set_file_collector(sinks::file::make_collector( |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 266 | keywords::target = sctpParams.volume |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 267 | )); |
| 268 | |
| 269 | // Upon restart, scan the directory for files matching the file_name pattern |
| 270 | boostLogger->locked_backend()->scan_for_files(); |
| 271 | |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 272 | // Enable auto-flushing after each tmpStr record written |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 273 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 274 | boostLogger->locked_backend()->auto_flush(true); |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
aa7133@att.com | 16c799b | 2020-03-24 18:01:51 +0200 | [diff] [blame^] | 280 | |
| 281 | |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 282 | int main(const int argc, char **argv) { |
| 283 | sctp_params_t sctpParams; |
| 284 | |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 285 | { |
| 286 | std::random_device device{}; |
| 287 | std::mt19937 generator(device()); |
| 288 | std::uniform_int_distribution<long> distribution(1, (long) 1e12); |
| 289 | transactionCounter = distribution(generator); |
| 290 | } |
| 291 | |
| 292 | // uint64_t st = 0; |
| 293 | // uint32_t aux1 = 0; |
| 294 | // st = rdtscp(aux1); |
| 295 | |
| 296 | unsigned num_cpus = std::thread::hardware_concurrency(); |
| 297 | init_log(); |
| 298 | mdclog_level_set(MDCLOG_INFO); |
| 299 | |
| 300 | if (std::signal(SIGINT, catch_function) == SIG_ERR) { |
| 301 | mdclog_write(MDCLOG_ERR, "Error initializing SIGINT"); |
| 302 | exit(1); |
| 303 | } |
| 304 | if (std::signal(SIGABRT, catch_function)== SIG_ERR) { |
| 305 | mdclog_write(MDCLOG_ERR, "Error initializing SIGABRT"); |
| 306 | exit(1); |
| 307 | } |
| 308 | if (std::signal(SIGTERM, catch_function)== SIG_ERR) { |
| 309 | mdclog_write(MDCLOG_ERR, "Error initializing SIGTERM"); |
| 310 | exit(1); |
| 311 | } |
| 312 | |
| 313 | cpuClock = approx_CPU_MHz(100); |
| 314 | |
| 315 | mdclog_write(MDCLOG_DEBUG, "CPU speed %11.11f", cpuClock); |
| 316 | |
| 317 | auto result = parse(argc, argv, sctpParams); |
| 318 | |
| 319 | if (buildConfiguration(sctpParams) != 0) { |
| 320 | exit(-1); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | // start epoll |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 324 | sctpParams.epoll_fd = epoll_create1(0); |
| 325 | if (sctpParams.epoll_fd == -1) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 326 | mdclog_write(MDCLOG_ERR, "failed to open epoll descriptor"); |
| 327 | exit(-1); |
| 328 | } |
| 329 | |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 330 | getRmrContext(sctpParams); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 331 | if (sctpParams.rmrCtx == nullptr) { |
| 332 | close(sctpParams.epoll_fd); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 333 | exit(-1); |
| 334 | } |
| 335 | |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 336 | if (buildInotify(sctpParams) == -1) { |
| 337 | close(sctpParams.rmrListenFd); |
| 338 | rmr_close(sctpParams.rmrCtx); |
| 339 | close(sctpParams.epoll_fd); |
| 340 | exit(-1); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | if (buildListeningPort(sctpParams) != 0) { |
| 344 | close(sctpParams.rmrListenFd); |
| 345 | rmr_close(sctpParams.rmrCtx); |
| 346 | close(sctpParams.epoll_fd); |
| 347 | exit(-1); |
| 348 | } |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 349 | |
| 350 | sctpParams.sctpMap = new mapWrapper(); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 351 | |
| 352 | std::vector<std::thread> threads(num_cpus); |
| 353 | // std::vector<std::thread> threads; |
| 354 | |
| 355 | num_cpus = 1; |
| 356 | for (unsigned int i = 0; i < num_cpus; i++) { |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 357 | threads[i] = std::thread(listener, &sctpParams); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 358 | |
| 359 | cpu_set_t cpuset; |
| 360 | CPU_ZERO(&cpuset); |
| 361 | CPU_SET(i, &cpuset); |
| 362 | int rc = pthread_setaffinity_np(threads[i].native_handle(), sizeof(cpu_set_t), &cpuset); |
| 363 | if (rc != 0) { |
| 364 | mdclog_write(MDCLOG_ERR, "Error calling pthread_setaffinity_np: %d", rc); |
| 365 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 366 | } |
| 367 | |
aa7133@att.com | 16c799b | 2020-03-24 18:01:51 +0200 | [diff] [blame^] | 368 | auto statFlag = false; |
| 369 | auto statThread = std::thread(statColectorThread, (void *)&statFlag); |
| 370 | |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 371 | //loop over term_init until first message from xApp |
| 372 | handleTermInit(sctpParams); |
| 373 | |
| 374 | for (auto &t : threads) { |
| 375 | t.join(); |
| 376 | } |
| 377 | |
aa7133@att.com | 16c799b | 2020-03-24 18:01:51 +0200 | [diff] [blame^] | 378 | statFlag = true; |
| 379 | statThread.join(); |
| 380 | |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | void handleTermInit(sctp_params_t &sctpParams) { |
| 385 | sendTermInit(sctpParams); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 386 | //send to e2 manager init of e2 term |
| 387 | //E2_TERM_INIT |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 388 | |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 389 | int count = 0; |
| 390 | while (true) { |
| 391 | auto xappMessages = num_of_XAPP_messages.load(std::memory_order_acquire); |
| 392 | if (xappMessages > 0) { |
| 393 | if (mdclog_level_get() >= MDCLOG_INFO) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 394 | mdclog_write(MDCLOG_INFO, "Got a message from some appliction, stop sending E2_TERM_INIT"); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 395 | } |
| 396 | return; |
| 397 | } |
| 398 | usleep(100000); |
| 399 | count++; |
| 400 | if (count % 1000 == 0) { |
| 401 | mdclog_write(MDCLOG_ERR, "GOT No messages from any xApp"); |
| 402 | sendTermInit(sctpParams); |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | void sendTermInit(sctp_params_t &sctpParams) { |
| 408 | rmr_mbuf_t *msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 409 | auto count = 0; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 410 | while (true) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 411 | msg->mtype = E2_TERM_INIT; |
| 412 | msg->state = 0; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 413 | rmr_bytes2payload(msg, (unsigned char *)sctpParams.ka_message, sctpParams.ka_message_length); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 414 | static unsigned char tx[32]; |
| 415 | auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++); |
| 416 | rmr_bytes2xact(msg, tx, txLen); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 417 | msg = rmr_send_msg(sctpParams.rmrCtx, msg); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 418 | if (msg == nullptr) { |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 419 | msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.myIP.length()); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 420 | } else if (msg->state == 0) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 421 | rmr_free_msg(msg); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 422 | if (mdclog_level_get() >= MDCLOG_INFO) { |
| 423 | mdclog_write(MDCLOG_INFO, "E2_TERM_INIT succsesfuly sent "); |
| 424 | } |
| 425 | return; |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 426 | } else { |
| 427 | if (count % 100 == 0) { |
aa7133@att.com | 11b17d4 | 2020-03-22 11:35:16 +0200 | [diff] [blame] | 428 | mdclog_write(MDCLOG_ERR, "Error sending E2_TERM_INIT cause : %s ", translateRmrErrorMessages(msg->state).c_str()); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 429 | } |
| 430 | sleep(1); |
| 431 | } |
| 432 | count++; |
| 433 | } |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /** |
| 437 | * |
| 438 | * @param argc |
| 439 | * @param argv |
| 440 | * @param sctpParams |
| 441 | * @return |
| 442 | */ |
| 443 | cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &sctpParams) { |
| 444 | cxxopts::Options options(argv[0], "e2 term help"); |
| 445 | options.positional_help("[optional args]").show_positional_help(); |
| 446 | options.allow_unrecognised_options().add_options() |
| 447 | ("p,path", "config file path", cxxopts::value<std::string>(sctpParams.configFilePath)->default_value("config")) |
| 448 | ("f,file", "config file name", cxxopts::value<std::string>(sctpParams.configFileName)->default_value("config.conf")) |
| 449 | ("h,help", "Print help"); |
| 450 | |
| 451 | auto result = options.parse(argc, argv); |
| 452 | |
| 453 | if (result.count("help")) { |
| 454 | std::cout << options.help({""}) << std::endl; |
| 455 | exit(0); |
| 456 | } |
| 457 | return result; |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * |
| 462 | * @param sctpParams |
| 463 | * @return -1 failed 0 success |
| 464 | */ |
| 465 | int buildInotify(sctp_params_t &sctpParams) { |
| 466 | sctpParams.inotifyFD = inotify_init1(IN_NONBLOCK); |
| 467 | if (sctpParams.inotifyFD == -1) { |
| 468 | mdclog_write(MDCLOG_ERR, "Failed to init inotify (inotify_init1) %s", strerror(errno)); |
| 469 | close(sctpParams.rmrListenFd); |
| 470 | rmr_close(sctpParams.rmrCtx); |
| 471 | close(sctpParams.epoll_fd); |
| 472 | return -1; |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 473 | } |
| 474 | |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 475 | sctpParams.inotifyWD = inotify_add_watch(sctpParams.inotifyFD, |
| 476 | (const char *)sctpParams.configFilePath.c_str(), |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 477 | (unsigned)IN_OPEN | (unsigned)IN_CLOSE_WRITE | (unsigned)IN_CLOSE_NOWRITE); //IN_CLOSE = (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 478 | if (sctpParams.inotifyWD == -1) { |
| 479 | mdclog_write(MDCLOG_ERR, "Failed to add directory : %s to inotify (inotify_add_watch) %s", |
| 480 | sctpParams.configFilePath.c_str(), |
| 481 | strerror(errno)); |
| 482 | close(sctpParams.inotifyFD); |
| 483 | return -1; |
| 484 | } |
| 485 | |
| 486 | struct epoll_event event{}; |
| 487 | event.events = (EPOLLIN); |
| 488 | event.data.fd = sctpParams.inotifyFD; |
| 489 | // add listening RMR FD to epoll |
| 490 | if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.inotifyFD, &event)) { |
| 491 | mdclog_write(MDCLOG_ERR, "Failed to add inotify FD to epoll"); |
| 492 | close(sctpParams.inotifyFD); |
| 493 | return -1; |
| 494 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * |
| 500 | * @param args |
| 501 | * @return |
| 502 | */ |
| 503 | void listener(sctp_params_t *params) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 504 | int num_of_SCTP_messages = 0; |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 505 | auto totalTime = 0.0; |
| 506 | mdclog_mdc_clean(); |
| 507 | mdclog_level_set(params->logLevel); |
| 508 | |
| 509 | std::thread::id this_id = std::this_thread::get_id(); |
| 510 | //save cout |
| 511 | streambuf *oldCout = cout.rdbuf(); |
| 512 | ostringstream memCout; |
| 513 | // create new cout |
| 514 | cout.rdbuf(memCout.rdbuf()); |
| 515 | cout << this_id; |
| 516 | //return to the normal cout |
| 517 | cout.rdbuf(oldCout); |
| 518 | |
| 519 | char tid[32]; |
| 520 | memcpy(tid, memCout.str().c_str(), memCout.str().length() < 32 ? memCout.str().length() : 31); |
| 521 | tid[memCout.str().length()] = 0; |
| 522 | mdclog_mdc_add("thread id", tid); |
| 523 | |
| 524 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 525 | mdclog_write(MDCLOG_DEBUG, "started thread number %s", tid); |
| 526 | } |
| 527 | |
aa7133@att.com | 342843e | 2020-01-26 12:53:34 +0200 | [diff] [blame] | 528 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 529 | RmrMessagesBuffer_t rmrMessageBuffer{}; |
| 530 | //create and init RMR |
| 531 | rmrMessageBuffer.rmrCtx = params->rmrCtx; |
| 532 | |
| 533 | auto *events = (struct epoll_event *) calloc(MAXEVENTS, sizeof(struct epoll_event)); |
| 534 | struct timespec end{0, 0}; |
| 535 | struct timespec start{0, 0}; |
| 536 | |
| 537 | rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE); |
| 538 | rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE); |
| 539 | |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 540 | memcpy(rmrMessageBuffer.ka_message, params->ka_message, params->ka_message_length); |
| 541 | rmrMessageBuffer.ka_message_len = params->ka_message_length; |
| 542 | rmrMessageBuffer.ka_message[rmrMessageBuffer.ka_message_len] = 0; |
| 543 | |
| 544 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 545 | mdclog_write(MDCLOG_DEBUG, "keep alive message is : %s", rmrMessageBuffer.ka_message); |
| 546 | } |
| 547 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 548 | ReportingMessages_t message {}; |
| 549 | |
| 550 | for (int i = 0; i < MAX_RMR_BUFF_ARRY; i++) { |
| 551 | rmrMessageBuffer.rcvBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE); |
| 552 | rmrMessageBuffer.sendBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE); |
| 553 | } |
| 554 | |
aa7133@att.com | 16c799b | 2020-03-24 18:01:51 +0200 | [diff] [blame^] | 555 | message.statCollector = StatCollector::GetInstance(); |
| 556 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 557 | while (true) { |
| 558 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 559 | mdclog_write(MDCLOG_DEBUG, "Start EPOLL Wait"); |
| 560 | } |
| 561 | auto numOfEvents = epoll_wait(params->epoll_fd, events, MAXEVENTS, -1); |
| 562 | if (numOfEvents < 0 && errno == EINTR) { |
| 563 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 564 | mdclog_write(MDCLOG_DEBUG, "got EINTR : %s", strerror(errno)); |
| 565 | } |
| 566 | continue; |
| 567 | } |
| 568 | if (numOfEvents < 0) { |
| 569 | mdclog_write(MDCLOG_ERR, "Epoll wait failed, errno = %s", strerror(errno)); |
| 570 | return; |
| 571 | } |
| 572 | for (auto i = 0; i < numOfEvents; i++) { |
| 573 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 574 | mdclog_write(MDCLOG_DEBUG, "handling epoll event %d out of %d", i + 1, numOfEvents); |
| 575 | } |
| 576 | clock_gettime(CLOCK_MONOTONIC, &message.message.time); |
| 577 | start.tv_sec = message.message.time.tv_sec; |
| 578 | start.tv_nsec = message.message.time.tv_nsec; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 579 | |
| 580 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 581 | if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)) { |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 582 | handlepoll_error(events[i], message, rmrMessageBuffer, params); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 583 | } else if (events[i].events & EPOLLOUT) { |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 584 | handleEinprogressMessages(events[i], message, rmrMessageBuffer, params); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 585 | } else if (params->listenFD == events[i].data.fd) { |
aa7133@att.com | 6680c3b | 2020-03-22 12:06:58 +0200 | [diff] [blame] | 586 | if (mdclog_level_get() >= MDCLOG_INFO) { |
| 587 | mdclog_write(MDCLOG_INFO, "New connection request from sctp network\n"); |
| 588 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 589 | // new connection is requested from RAN start build connection |
| 590 | while (true) { |
| 591 | struct sockaddr in_addr {}; |
| 592 | socklen_t in_len; |
| 593 | char hostBuff[NI_MAXHOST]; |
| 594 | char portBuff[NI_MAXSERV]; |
| 595 | |
| 596 | in_len = sizeof(in_addr); |
| 597 | auto *peerInfo = (ConnectedCU_t *)calloc(1, sizeof(ConnectedCU_t)); |
| 598 | peerInfo->sctpParams = params; |
| 599 | peerInfo->fileDescriptor = accept(params->listenFD, &in_addr, &in_len); |
| 600 | if (peerInfo->fileDescriptor == -1) { |
| 601 | if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { |
| 602 | /* We have processed all incoming connections. */ |
| 603 | break; |
| 604 | } else { |
| 605 | mdclog_write(MDCLOG_ERR, "Accept error, errno = %s", strerror(errno)); |
| 606 | break; |
| 607 | } |
| 608 | } |
| 609 | if (setSocketNoBlocking(peerInfo->fileDescriptor) == -1) { |
| 610 | mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on port %s\n", hostBuff, portBuff); |
| 611 | close(peerInfo->fileDescriptor); |
| 612 | break; |
| 613 | } |
| 614 | auto ans = getnameinfo(&in_addr, in_len, |
| 615 | peerInfo->hostName, NI_MAXHOST, |
aa7133@att.com | 6680c3b | 2020-03-22 12:06:58 +0200 | [diff] [blame] | 616 | peerInfo->portNumber, NI_MAXSERV, (unsigned )((unsigned int)NI_NUMERICHOST | (unsigned int)NI_NUMERICSERV)); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 617 | if (ans < 0) { |
| 618 | mdclog_write(MDCLOG_ERR, "Failed to get info on connection request. %s\n", strerror(errno)); |
| 619 | close(peerInfo->fileDescriptor); |
| 620 | break; |
| 621 | } |
| 622 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 623 | mdclog_write(MDCLOG_DEBUG, "Accepted connection on descriptor %d (host=%s, port=%s)\n", peerInfo->fileDescriptor, peerInfo->hostName, peerInfo->portNumber); |
| 624 | } |
| 625 | peerInfo->isConnected = false; |
| 626 | peerInfo->gotSetup = false; |
| 627 | if (addToEpoll(params->epoll_fd, |
| 628 | peerInfo, |
| 629 | (EPOLLIN | EPOLLET), |
| 630 | params->sctpMap, nullptr, |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 631 | 0) != 0) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 632 | break; |
| 633 | } |
aa7133@att.com | 6680c3b | 2020-03-22 12:06:58 +0200 | [diff] [blame] | 634 | break; |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 635 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 636 | } else if (params->rmrListenFd == events[i].data.fd) { |
| 637 | // got message from XAPP |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 638 | num_of_XAPP_messages.fetch_add(1, std::memory_order_release); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 639 | num_of_messages.fetch_add(1, std::memory_order_release); |
| 640 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 641 | mdclog_write(MDCLOG_DEBUG, "new message from RMR"); |
| 642 | } |
aa7133@att.com | 6680c3b | 2020-03-22 12:06:58 +0200 | [diff] [blame] | 643 | if (receiveXappMessages(params->sctpMap, |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 644 | rmrMessageBuffer, |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 645 | message.message.time) != 0) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 646 | mdclog_write(MDCLOG_ERR, "Error handling Xapp message"); |
| 647 | } |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 648 | } else if (params->inotifyFD == events[i].data.fd) { |
| 649 | mdclog_write(MDCLOG_INFO, "Got event from inotify (configuration update)"); |
| 650 | handleConfigChange(params); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 651 | } else { |
| 652 | /* We RMR_ERR_RETRY have data on the fd waiting to be read. Read and display it. |
| 653 | * We must read whatever data is available completely, as we are running |
| 654 | * in edge-triggered mode and won't get a notification again for the same data. */ |
| 655 | num_of_messages.fetch_add(1, std::memory_order_release); |
| 656 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 657 | mdclog_write(MDCLOG_DEBUG, "new message from SCTP, epoll flags are : %0x", events[i].events); |
| 658 | } |
| 659 | receiveDataFromSctp(&events[i], |
| 660 | params->sctpMap, |
| 661 | num_of_SCTP_messages, |
| 662 | rmrMessageBuffer, |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 663 | message.message.time); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | clock_gettime(CLOCK_MONOTONIC, &end); |
| 667 | if (mdclog_level_get() >= MDCLOG_INFO) { |
| 668 | totalTime += ((end.tv_sec + 1.0e-9 * end.tv_nsec) - |
| 669 | ((double) start.tv_sec + 1.0e-9 * start.tv_nsec)); |
| 670 | } |
| 671 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 672 | mdclog_write(MDCLOG_DEBUG, "message handling is %ld seconds %ld nanoseconds", |
| 673 | end.tv_sec - start.tv_sec, |
| 674 | end.tv_nsec - start.tv_nsec); |
| 675 | } |
| 676 | } |
| 677 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /** |
| 681 | * |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 682 | * @param sctpParams |
| 683 | */ |
| 684 | void handleConfigChange(sctp_params_t *sctpParams) { |
| 685 | char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event)))); |
| 686 | const struct inotify_event *event; |
| 687 | char *ptr; |
| 688 | |
| 689 | path p = (sctpParams->configFilePath + "/" + sctpParams->configFileName).c_str(); |
| 690 | auto endlessLoop = true; |
| 691 | while (endlessLoop) { |
| 692 | auto len = read(sctpParams->inotifyFD, buf, sizeof buf); |
| 693 | if (len == -1) { |
| 694 | if (errno != EAGAIN) { |
| 695 | mdclog_write(MDCLOG_ERR, "read %s ", strerror(errno)); |
| 696 | endlessLoop = false; |
| 697 | continue; |
| 698 | } |
| 699 | else { |
| 700 | endlessLoop = false; |
| 701 | continue; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) { |
| 706 | event = (const struct inotify_event *)ptr; |
| 707 | if (event->mask & (uint32_t)IN_ISDIR) { |
| 708 | continue; |
| 709 | } |
| 710 | |
| 711 | // the directory name |
| 712 | if (sctpParams->inotifyWD == event->wd) { |
| 713 | // not the directory |
| 714 | } |
| 715 | if (event->len) { |
| 716 | if (!(sctpParams->configFileName.compare(event->name))) { |
| 717 | continue; |
| 718 | } |
| 719 | } |
| 720 | // only the file we want |
| 721 | if (event->mask & (uint32_t)IN_CLOSE_WRITE) { |
| 722 | if (exists(p)) { |
| 723 | const int size = 2048; |
| 724 | auto fileSize = file_size(p); |
| 725 | if (fileSize > size) { |
| 726 | mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size); |
| 727 | return; |
| 728 | } |
| 729 | } else { |
| 730 | mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str()); |
| 731 | return; |
| 732 | } |
| 733 | |
| 734 | ReadConfigFile conf; |
| 735 | if (conf.openConfigFile(p.string()) == -1) { |
| 736 | mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s", |
| 737 | p.string().c_str(), strerror(errno)); |
| 738 | return; |
| 739 | } |
| 740 | |
| 741 | auto tmpStr = conf.getStringValue("loglevel"); |
| 742 | if (tmpStr.length() == 0) { |
| 743 | mdclog_write(MDCLOG_ERR, "illigal loglevel. Set loglevel to MDCLOG_INFO"); |
| 744 | tmpStr = "info"; |
| 745 | } |
| 746 | transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower); |
| 747 | |
| 748 | if ((tmpStr.compare("debug")) == 0) { |
| 749 | mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_DEBUG"); |
| 750 | sctpParams->logLevel = MDCLOG_DEBUG; |
| 751 | } else if ((tmpStr.compare("info")) == 0) { |
| 752 | mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_INFO"); |
| 753 | sctpParams->logLevel = MDCLOG_INFO; |
| 754 | } else if ((tmpStr.compare("warning")) == 0) { |
| 755 | mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_WARN"); |
| 756 | sctpParams->logLevel = MDCLOG_WARN; |
| 757 | } else if ((tmpStr.compare("error")) == 0) { |
| 758 | mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_ERR"); |
| 759 | sctpParams->logLevel = MDCLOG_ERR; |
| 760 | } else { |
| 761 | mdclog_write(MDCLOG_ERR, "illigal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str()); |
| 762 | sctpParams->logLevel = MDCLOG_INFO; |
| 763 | } |
| 764 | mdclog_level_set(sctpParams->logLevel); |
| 765 | |
| 766 | |
| 767 | tmpStr = conf.getStringValue("trace"); |
| 768 | if (tmpStr.length() == 0) { |
| 769 | mdclog_write(MDCLOG_ERR, "illigal trace. Set trace to stop"); |
| 770 | tmpStr = "stop"; |
| 771 | } |
| 772 | |
| 773 | transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower); |
| 774 | if ((tmpStr.compare("start")) == 0) { |
| 775 | mdclog_write(MDCLOG_INFO, "Trace set to: start"); |
| 776 | sctpParams->trace = true; |
| 777 | } else if ((tmpStr.compare("stop")) == 0) { |
| 778 | mdclog_write(MDCLOG_INFO, "Trace set to: stop"); |
| 779 | sctpParams->trace = false; |
| 780 | } else { |
| 781 | mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str()); |
| 782 | sctpParams->trace = false; |
| 783 | } |
| 784 | jsonTrace = sctpParams->trace; |
| 785 | endlessLoop = false; |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | /** |
| 792 | * |
| 793 | * @param event |
| 794 | * @param message |
| 795 | * @param rmrMessageBuffer |
| 796 | * @param params |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 797 | */ |
| 798 | void handleEinprogressMessages(struct epoll_event &event, |
| 799 | ReportingMessages_t &message, |
| 800 | RmrMessagesBuffer_t &rmrMessageBuffer, |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 801 | sctp_params_t *params) { |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 802 | auto *peerInfo = (ConnectedCU_t *)event.data.ptr; |
| 803 | memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName)); |
| 804 | |
| 805 | mdclog_write(MDCLOG_INFO, "file descriptor %d got EPOLLOUT", peerInfo->fileDescriptor); |
| 806 | auto retVal = 0; |
| 807 | socklen_t retValLen = 0; |
| 808 | auto rc = getsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_ERROR, &retVal, &retValLen); |
| 809 | if (rc != 0 || retVal != 0) { |
| 810 | if (rc != 0) { |
| 811 | rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256, |
| 812 | "%s|Failed SCTP Connection, after EINPROGRESS the getsockopt%s", |
| 813 | peerInfo->enodbName, strerror(errno)); |
| 814 | } else if (retVal != 0) { |
| 815 | rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256, |
| 816 | "%s|Failed SCTP Connection after EINPROGRESS, SO_ERROR", |
| 817 | peerInfo->enodbName); |
| 818 | } |
| 819 | |
| 820 | message.message.asndata = rmrMessageBuffer.sendMessage->payload; |
| 821 | message.message.asnLength = rmrMessageBuffer.sendMessage->len; |
| 822 | mdclog_write(MDCLOG_ERR, "%s", rmrMessageBuffer.sendMessage->payload); |
| 823 | message.message.direction = 'N'; |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 824 | if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) { |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 825 | mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP"); |
| 826 | } |
| 827 | memset(peerInfo->asnData, 0, peerInfo->asnLength); |
| 828 | peerInfo->asnLength = 0; |
| 829 | peerInfo->mtype = 0; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 830 | return; |
| 831 | } |
| 832 | |
| 833 | peerInfo->isConnected = true; |
| 834 | |
| 835 | if (modifyToEpoll(params->epoll_fd, peerInfo, (EPOLLIN | EPOLLET), params->sctpMap, peerInfo->enodbName, |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 836 | peerInfo->mtype) != 0) { |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 837 | mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_MOD"); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 838 | return; |
| 839 | } |
| 840 | |
| 841 | message.message.asndata = (unsigned char *)peerInfo->asnData; |
| 842 | message.message.asnLength = peerInfo->asnLength; |
| 843 | message.message.messageType = peerInfo->mtype; |
| 844 | memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName)); |
| 845 | num_of_messages.fetch_add(1, std::memory_order_release); |
| 846 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 847 | mdclog_write(MDCLOG_DEBUG, "send the delayed SETUP/ENDC SETUP to sctp for %s", |
| 848 | message.message.enodbName); |
| 849 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 850 | if (sendSctpMsg(peerInfo, message, params->sctpMap) != 0) { |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 851 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 852 | mdclog_write(MDCLOG_DEBUG, "Error write to SCTP %s %d", __func__, __LINE__); |
| 853 | } |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 854 | return; |
| 855 | } |
| 856 | |
| 857 | memset(peerInfo->asnData, 0, peerInfo->asnLength); |
| 858 | peerInfo->asnLength = 0; |
| 859 | peerInfo->mtype = 0; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | |
| 863 | void handlepoll_error(struct epoll_event &event, |
| 864 | ReportingMessages_t &message, |
| 865 | RmrMessagesBuffer_t &rmrMessageBuffer, |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 866 | sctp_params_t *params) { |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 867 | if (event.data.fd != params->rmrListenFd) { |
| 868 | auto *peerInfo = (ConnectedCU_t *)event.data.ptr; |
| 869 | mdclog_write(MDCLOG_ERR, "epoll error, events %0x on fd %d, RAN NAME : %s", |
| 870 | event.events, peerInfo->fileDescriptor, peerInfo->enodbName); |
| 871 | |
| 872 | rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256, |
| 873 | "%s|Failed SCTP Connection", |
| 874 | peerInfo->enodbName); |
| 875 | message.message.asndata = rmrMessageBuffer.sendMessage->payload; |
| 876 | message.message.asnLength = rmrMessageBuffer.sendMessage->len; |
| 877 | |
| 878 | memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName)); |
| 879 | message.message.direction = 'N'; |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 880 | if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) { |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 881 | mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP"); |
| 882 | } |
| 883 | |
| 884 | close(peerInfo->fileDescriptor); |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 885 | cleanHashEntry((ConnectedCU_t *) event.data.ptr, params->sctpMap); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 886 | } else { |
| 887 | mdclog_write(MDCLOG_ERR, "epoll error, events %0x on RMR FD", event.events); |
| 888 | } |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 889 | } |
| 890 | /** |
| 891 | * |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 892 | * @param socket |
| 893 | * @return |
| 894 | */ |
| 895 | int setSocketNoBlocking(int socket) { |
| 896 | auto flags = fcntl(socket, F_GETFL, 0); |
| 897 | |
| 898 | if (flags == -1) { |
| 899 | mdclog_mdc_add("func", "fcntl"); |
| 900 | mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno)); |
| 901 | mdclog_mdc_clean(); |
| 902 | return -1; |
| 903 | } |
| 904 | |
| 905 | flags = (unsigned) flags | (unsigned) O_NONBLOCK; |
| 906 | if (fcntl(socket, F_SETFL, flags) == -1) { |
| 907 | mdclog_mdc_add("func", "fcntl"); |
| 908 | mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno)); |
| 909 | mdclog_mdc_clean(); |
| 910 | return -1; |
| 911 | } |
| 912 | |
| 913 | return 0; |
| 914 | } |
| 915 | |
| 916 | /** |
| 917 | * |
| 918 | * @param val |
| 919 | * @param m |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 920 | */ |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 921 | void cleanHashEntry(ConnectedCU_t *val, Sctp_Map_t *m) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 922 | char *dummy; |
| 923 | auto port = (uint16_t) strtol(val->portNumber, &dummy, 10); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 924 | char searchBuff[2048]{}; |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 925 | |
| 926 | snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", val->hostName, port); |
| 927 | m->erase(searchBuff); |
| 928 | |
| 929 | m->erase(val->enodbName); |
| 930 | free(val); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | /** |
| 934 | * |
| 935 | * @param fd file discriptor |
| 936 | * @param data the asn data to send |
| 937 | * @param len length of the data |
| 938 | * @param enodbName the enodbName as in the map for printing purpose |
| 939 | * @param m map host information |
| 940 | * @param mtype message number |
| 941 | * @return 0 success, anegative number on fail |
| 942 | */ |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 943 | int sendSctpMsg(ConnectedCU_t *peerInfo, ReportingMessages_t &message, Sctp_Map_t *m) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 944 | auto loglevel = mdclog_level_get(); |
| 945 | int fd = peerInfo->fileDescriptor; |
| 946 | if (loglevel >= MDCLOG_DEBUG) { |
| 947 | mdclog_write(MDCLOG_DEBUG, "Send SCTP message for CU %s, %s", |
| 948 | message.message.enodbName, __FUNCTION__); |
| 949 | } |
| 950 | |
| 951 | while (true) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 952 | if (send(fd,message.message.asndata, message.message.asnLength,MSG_NOSIGNAL) < 0) { |
| 953 | if (errno == EINTR) { |
| 954 | continue; |
| 955 | } |
| 956 | mdclog_write(MDCLOG_ERR, "error writing to CU a message, %s ", strerror(errno)); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 957 | if (!peerInfo->isConnected) { |
| 958 | mdclog_write(MDCLOG_ERR, "connection to CU %s is still in progress.", message.message.enodbName); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 959 | return -1; |
| 960 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 961 | cleanHashEntry(peerInfo, m); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 962 | close(fd); |
| 963 | char key[MAX_ENODB_NAME_SIZE * 2]; |
| 964 | snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName, |
| 965 | message.message.messageType); |
| 966 | if (loglevel >= MDCLOG_DEBUG) { |
| 967 | mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__); |
| 968 | } |
| 969 | auto tmp = m->find(key); |
| 970 | if (tmp) { |
| 971 | free(tmp); |
| 972 | } |
| 973 | m->erase(key); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 974 | return -1; |
| 975 | } |
aa7133@att.com | 16c799b | 2020-03-24 18:01:51 +0200 | [diff] [blame^] | 976 | message.statCollector->incSentMessage(string(message.message.enodbName)); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 977 | message.message.direction = 'D'; |
| 978 | // send report.buffer of size |
| 979 | buildJsonMessage(message); |
| 980 | |
| 981 | if (loglevel >= MDCLOG_DEBUG) { |
| 982 | mdclog_write(MDCLOG_DEBUG, |
| 983 | "SCTP message for CU %s sent from %s", |
| 984 | message.message.enodbName, |
| 985 | __FUNCTION__); |
| 986 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 987 | return 0; |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | /** |
| 992 | * |
| 993 | * @param message |
| 994 | * @param rmrMessageBuffer |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 995 | */ |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 996 | void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) { |
| 997 | rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *) (message.message.enodbName)); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 998 | |
| 999 | message.message.asndata = rmrMessageBuffer.rcvMessage->payload; |
| 1000 | message.message.asnLength = rmrMessageBuffer.rcvMessage->len; |
| 1001 | |
| 1002 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 1003 | mdclog_write(MDCLOG_DEBUG, "Message from Xapp RAN name = %s message length = %ld", |
| 1004 | message.message.enodbName, (unsigned long) message.message.asnLength); |
| 1005 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1009 | |
| 1010 | /** |
| 1011 | * |
| 1012 | * @param events |
| 1013 | * @param sctpMap |
| 1014 | * @param numOfMessages |
| 1015 | * @param rmrMessageBuffer |
| 1016 | * @param ts |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1017 | * @return |
| 1018 | */ |
| 1019 | int receiveDataFromSctp(struct epoll_event *events, |
| 1020 | Sctp_Map_t *sctpMap, |
| 1021 | int &numOfMessages, |
| 1022 | RmrMessagesBuffer_t &rmrMessageBuffer, |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1023 | struct timespec &ts) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1024 | /* We have data on the fd waiting to be read. Read and display it. |
| 1025 | * We must read whatever data is available completely, as we are running |
| 1026 | * in edge-triggered mode and won't get a notification again for the same data. */ |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1027 | ReportingMessages_t message {}; |
| 1028 | auto done = 0; |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1029 | auto loglevel = mdclog_level_get(); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1030 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1031 | // get the identity of the interface |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1032 | message.peerInfo = (ConnectedCU_t *)events->data.ptr; |
| 1033 | |
aa7133@att.com | 16c799b | 2020-03-24 18:01:51 +0200 | [diff] [blame^] | 1034 | message.statCollector = StatCollector::GetInstance(); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1035 | struct timespec start{0, 0}; |
| 1036 | struct timespec decodestart{0, 0}; |
| 1037 | struct timespec end{0, 0}; |
| 1038 | |
| 1039 | E2AP_PDU_t *pdu = nullptr; |
| 1040 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1041 | |
| 1042 | while (true) { |
| 1043 | if (loglevel >= MDCLOG_DEBUG) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1044 | mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP %d fd", message.peerInfo->fileDescriptor); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1045 | clock_gettime(CLOCK_MONOTONIC, &start); |
| 1046 | } |
| 1047 | // read the buffer directly to rmr payload |
| 1048 | message.message.asndata = rmrMessageBuffer.sendMessage->payload; |
| 1049 | message.message.asnLength = rmrMessageBuffer.sendMessage->len = |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1050 | read(message.peerInfo->fileDescriptor, rmrMessageBuffer.sendMessage->payload, RECEIVE_SCTP_BUFFER_SIZE); |
| 1051 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1052 | if (loglevel >= MDCLOG_DEBUG) { |
| 1053 | mdclog_write(MDCLOG_DEBUG, "Finish Read from SCTP %d fd message length = %ld", |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1054 | message.peerInfo->fileDescriptor, message.message.asnLength); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1055 | } |
aa7133@att.com | 16c799b | 2020-03-24 18:01:51 +0200 | [diff] [blame^] | 1056 | |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1057 | memcpy(message.message.enodbName, message.peerInfo->enodbName, sizeof(message.peerInfo->enodbName)); |
aa7133@att.com | 16c799b | 2020-03-24 18:01:51 +0200 | [diff] [blame^] | 1058 | message.statCollector->incRecvMessage(string(message.message.enodbName)); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1059 | message.message.direction = 'U'; |
| 1060 | message.message.time.tv_nsec = ts.tv_nsec; |
| 1061 | message.message.time.tv_sec = ts.tv_sec; |
| 1062 | |
| 1063 | if (message.message.asnLength < 0) { |
| 1064 | if (errno == EINTR) { |
| 1065 | continue; |
| 1066 | } |
| 1067 | /* If errno == EAGAIN, that means we have read all |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1068 | data. So goReportingMessages_t back to the main loop. */ |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1069 | if (errno != EAGAIN) { |
| 1070 | mdclog_write(MDCLOG_ERR, "Read error, %s ", strerror(errno)); |
| 1071 | done = 1; |
| 1072 | } else if (loglevel >= MDCLOG_DEBUG) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1073 | mdclog_write(MDCLOG_DEBUG, "EAGAIN - descriptor = %d", message.peerInfo->fileDescriptor); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1074 | } |
| 1075 | break; |
| 1076 | } else if (message.message.asnLength == 0) { |
| 1077 | /* End of file. The remote has closed the connection. */ |
| 1078 | if (loglevel >= MDCLOG_INFO) { |
| 1079 | mdclog_write(MDCLOG_INFO, "END of File Closed connection - descriptor = %d", |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1080 | message.peerInfo->fileDescriptor); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1081 | } |
| 1082 | done = 1; |
| 1083 | break; |
| 1084 | } |
| 1085 | |
| 1086 | asn_dec_rval_t rval; |
| 1087 | if (loglevel >= MDCLOG_DEBUG) { |
| 1088 | char printBuffer[4096]{}; |
| 1089 | char *tmp = printBuffer; |
| 1090 | for (size_t i = 0; i < (size_t)message.message.asnLength; ++i) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1091 | snprintf(tmp, 3, "%02x", message.message.asndata[i]); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1092 | tmp += 2; |
| 1093 | } |
| 1094 | printBuffer[message.message.asnLength] = 0; |
| 1095 | clock_gettime(CLOCK_MONOTONIC, &end); |
| 1096 | mdclog_write(MDCLOG_DEBUG, "Before Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds", |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1097 | message.peerInfo->enodbName, end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1098 | mdclog_write(MDCLOG_DEBUG, "PDU buffer length = %ld, data = : %s", message.message.asnLength, |
| 1099 | printBuffer); |
| 1100 | clock_gettime(CLOCK_MONOTONIC, &decodestart); |
| 1101 | } |
| 1102 | |
| 1103 | rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu, |
| 1104 | message.message.asndata, message.message.asnLength); |
| 1105 | if (rval.code != RC_OK) { |
| 1106 | mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from RAN : %s", rval.code, |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1107 | message.peerInfo->enodbName); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1108 | break; |
| 1109 | } |
| 1110 | |
| 1111 | if (loglevel >= MDCLOG_DEBUG) { |
| 1112 | clock_gettime(CLOCK_MONOTONIC, &end); |
| 1113 | mdclog_write(MDCLOG_DEBUG, "After Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds", |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1114 | message.peerInfo->enodbName, end.tv_sec - decodestart.tv_sec, end.tv_nsec - decodestart.tv_nsec); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1115 | char *printBuffer; |
| 1116 | size_t size; |
| 1117 | FILE *stream = open_memstream(&printBuffer, &size); |
| 1118 | asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu); |
| 1119 | mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer); |
| 1120 | clock_gettime(CLOCK_MONOTONIC, &decodestart); |
| 1121 | } |
| 1122 | |
| 1123 | switch (pdu->present) { |
| 1124 | case E2AP_PDU_PR_initiatingMessage: {//initiating message |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1125 | asnInitiatingRequest(pdu, message, rmrMessageBuffer); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1126 | break; |
| 1127 | } |
| 1128 | case E2AP_PDU_PR_successfulOutcome: { //successful outcome |
aa7133@att.com | 11b17d4 | 2020-03-22 11:35:16 +0200 | [diff] [blame] | 1129 | asnSuccsesfulMsg(pdu, message, rmrMessageBuffer); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1130 | break; |
| 1131 | } |
| 1132 | case E2AP_PDU_PR_unsuccessfulOutcome: { //Unsuccessful Outcome |
aa7133@att.com | 11b17d4 | 2020-03-22 11:35:16 +0200 | [diff] [blame] | 1133 | asnUnSuccsesfulMsg(pdu, message, rmrMessageBuffer); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1134 | break; |
| 1135 | } |
| 1136 | default: |
| 1137 | mdclog_write(MDCLOG_ERR, "Unknown index %d in E2AP PDU", pdu->present); |
| 1138 | break; |
| 1139 | } |
| 1140 | if (loglevel >= MDCLOG_DEBUG) { |
| 1141 | clock_gettime(CLOCK_MONOTONIC, &end); |
| 1142 | mdclog_write(MDCLOG_DEBUG, |
| 1143 | "After processing message and sent to rmr for : %s, Read time is : %ld seconds, %ld nanoseconds", |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1144 | message.peerInfo->enodbName, end.tv_sec - decodestart.tv_sec, end.tv_nsec - decodestart.tv_nsec); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1145 | } |
| 1146 | numOfMessages++; |
| 1147 | // remove the break for EAGAIN |
| 1148 | //break; |
| 1149 | if (pdu != nullptr) { |
| 1150 | //TODO need to test ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu); to get better performance |
| 1151 | //ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu); |
| 1152 | ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu); |
| 1153 | pdu = nullptr; |
| 1154 | } |
| 1155 | //clock_gettime(CLOCK_MONOTONIC, &start); |
| 1156 | } |
| 1157 | // in case of break to avoid memory leak |
| 1158 | if (pdu != nullptr) { |
| 1159 | ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu); |
| 1160 | pdu = nullptr; |
| 1161 | } |
| 1162 | |
| 1163 | if (done) { |
| 1164 | if (loglevel >= MDCLOG_INFO) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1165 | mdclog_write(MDCLOG_INFO, "Closed connection - descriptor = %d", message.peerInfo->fileDescriptor); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1166 | } |
| 1167 | message.message.asnLength = rmrMessageBuffer.sendMessage->len = |
| 1168 | snprintf((char *)rmrMessageBuffer.sendMessage->payload, |
| 1169 | 256, |
| 1170 | "%s|CU disconnected unexpectedly", |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1171 | message.peerInfo->enodbName); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1172 | message.message.asndata = rmrMessageBuffer.sendMessage->payload; |
| 1173 | |
| 1174 | if (sendRequestToXapp(message, |
| 1175 | RIC_SCTP_CONNECTION_FAILURE, |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1176 | rmrMessageBuffer) != 0) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1177 | mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP"); |
| 1178 | } |
| 1179 | |
| 1180 | /* Closing descriptor make epoll remove it from the set of descriptors which are monitored. */ |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1181 | close(message.peerInfo->fileDescriptor); |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1182 | cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1183 | } |
| 1184 | if (loglevel >= MDCLOG_DEBUG) { |
| 1185 | clock_gettime(CLOCK_MONOTONIC, &end); |
| 1186 | mdclog_write(MDCLOG_DEBUG, "from receive SCTP to send RMR time is %ld seconds and %ld nanoseconds", |
| 1187 | end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec); |
| 1188 | |
| 1189 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1190 | return 0; |
| 1191 | } |
| 1192 | |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1193 | static void buildAndsendSetupRequest(ReportingMessages_t &message, |
| 1194 | E2setupRequestIEs_t *ie, |
| 1195 | RmrMessagesBuffer_t &rmrMessageBuffer, |
| 1196 | E2AP_PDU_t *pdu) { |
| 1197 | auto logLevel = mdclog_level_get(); |
| 1198 | |
| 1199 | |
| 1200 | if (buildRanName(message.peerInfo->enodbName, ie) < 0) { |
| 1201 | mdclog_write(MDCLOG_ERR, "Bad param in E2setupRequestIEs GlobalE2node_ID.\n"); |
| 1202 | } else { |
| 1203 | memcpy(message.message.enodbName, message.peerInfo->enodbName, strlen(message.peerInfo->enodbName)); |
| 1204 | } |
| 1205 | // now we can send the data to e2Mgr |
| 1206 | auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE; |
| 1207 | |
| 1208 | auto *rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size); |
| 1209 | // add addrees to message |
| 1210 | auto j = snprintf((char *)rmrMsg->payload, 256, "%s:%d|", message.peerInfo->sctpParams->myIP.c_str(), message.peerInfo->sctpParams->rmrPort); |
| 1211 | |
| 1212 | |
| 1213 | unsigned char *buffer = &rmrMsg->payload[j]; |
| 1214 | // encode to xml |
| 1215 | asn_enc_rval_t er; |
| 1216 | er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size - j); |
| 1217 | if (er.encoded == -1) { |
| 1218 | mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno)); |
| 1219 | } else if (er.encoded > (ssize_t) buffer_size) { |
| 1220 | mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s", |
| 1221 | (int) buffer_size, |
| 1222 | asn_DEF_E2AP_PDU.name); |
| 1223 | } else { |
| 1224 | if (logLevel >= MDCLOG_DEBUG) { |
| 1225 | mdclog_write(MDCLOG_DEBUG, "Buffer of size %d, data = %s", (int) er.encoded, buffer); |
| 1226 | } |
aa7133@att.com | 11b17d4 | 2020-03-22 11:35:16 +0200 | [diff] [blame] | 1227 | // send to RMR |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1228 | message.message.messageType = rmrMsg->mtype = RIC_E2_SETUP_REQ; |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1229 | rmrMsg->state = 0; |
| 1230 | rmr_bytes2meid(rmrMsg, (unsigned char *) message.message.enodbName, strlen(message.message.enodbName)); |
| 1231 | |
| 1232 | static unsigned char tx[32]; |
| 1233 | snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++); |
| 1234 | rmr_bytes2xact(rmrMsg, tx, strlen((const char *) tx)); |
| 1235 | |
| 1236 | rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg); |
| 1237 | if (rmrMsg == nullptr) { |
| 1238 | mdclog_write(MDCLOG_ERR, "RMR failed to send returned nullptr"); |
| 1239 | } else if (rmrMsg->state != 0) { |
| 1240 | char meid[RMR_MAX_MEID]{}; |
| 1241 | if (rmrMsg->state == RMR_ERR_RETRY) { |
| 1242 | usleep(5); |
| 1243 | rmrMsg->state = 0; |
| 1244 | mdclog_write(MDCLOG_INFO, "RETRY sending Message %d to Xapp from %s", |
| 1245 | rmrMsg->mtype, rmr_get_meid(rmrMsg, (unsigned char *) meid)); |
| 1246 | rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg); |
| 1247 | if (rmrMsg == nullptr) { |
| 1248 | mdclog_write(MDCLOG_ERR, "RMR failed send returned nullptr"); |
| 1249 | } else if (rmrMsg->state != 0) { |
| 1250 | mdclog_write(MDCLOG_ERR, |
| 1251 | "RMR Retry failed %s sending request %d to Xapp from %s", |
| 1252 | translateRmrErrorMessages(rmrMsg->state).c_str(), |
| 1253 | rmrMsg->mtype, |
| 1254 | rmr_get_meid(rmrMsg, (unsigned char *) meid)); |
| 1255 | } |
| 1256 | } else { |
| 1257 | mdclog_write(MDCLOG_ERR, "RMR failed: %s. sending request %d to Xapp from %s", |
| 1258 | translateRmrErrorMessages(rmrMsg->state).c_str(), |
| 1259 | rmrMsg->mtype, |
| 1260 | rmr_get_meid(rmrMsg, (unsigned char *) meid)); |
| 1261 | } |
| 1262 | } |
| 1263 | message.peerInfo->gotSetup = true; |
| 1264 | buildJsonMessage(message); |
| 1265 | } |
| 1266 | |
| 1267 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1268 | /** |
| 1269 | * |
| 1270 | * @param pdu |
| 1271 | * @param message |
| 1272 | * @param rmrMessageBuffer |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1273 | */ |
| 1274 | void asnInitiatingRequest(E2AP_PDU_t *pdu, |
| 1275 | ReportingMessages_t &message, |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1276 | RmrMessagesBuffer_t &rmrMessageBuffer) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1277 | auto logLevel = mdclog_level_get(); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1278 | auto procedureCode = ((InitiatingMessage_t *) pdu->choice.initiatingMessage)->procedureCode; |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1279 | if (logLevel >= MDCLOG_DEBUG) { |
| 1280 | mdclog_write(MDCLOG_DEBUG, "Initiating message %ld\n", procedureCode); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1281 | } |
| 1282 | switch (procedureCode) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1283 | case ProcedureCode_id_E2setup: { |
| 1284 | if (logLevel >= MDCLOG_DEBUG) { |
| 1285 | mdclog_write(MDCLOG_DEBUG, "Got E2setup\n"); |
| 1286 | } |
| 1287 | |
| 1288 | memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE); |
| 1289 | for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) { |
| 1290 | auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i]; |
| 1291 | if (ie->id == ProtocolIE_ID_id_GlobalE2node_ID) { |
| 1292 | if (ie->value.present == E2setupRequestIEs__value_PR_GlobalE2node_ID) { |
| 1293 | buildAndsendSetupRequest(message, ie, rmrMessageBuffer, pdu); |
| 1294 | break; |
| 1295 | } |
| 1296 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1297 | } |
| 1298 | break; |
| 1299 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1300 | case ProcedureCode_id_ErrorIndication: { |
| 1301 | if (logLevel >= MDCLOG_DEBUG) { |
| 1302 | mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName); |
| 1303 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1304 | if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1305 | mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1306 | } |
| 1307 | break; |
| 1308 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1309 | case ProcedureCode_id_Reset: { |
| 1310 | if (logLevel >= MDCLOG_DEBUG) { |
| 1311 | mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1312 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1313 | if (sendRequestToXapp(message, RIC_X2_RESET, rmrMessageBuffer) != 0) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1314 | mdclog_write(MDCLOG_ERR, "RIC_X2_RESET message failed to send to xAPP"); |
| 1315 | } |
| 1316 | break; |
| 1317 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1318 | case ProcedureCode_id_RICcontrol: { |
| 1319 | if (logLevel >= MDCLOG_DEBUG) { |
| 1320 | mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName); |
| 1321 | } |
| 1322 | break; |
| 1323 | } |
| 1324 | case ProcedureCode_id_RICindication: { |
| 1325 | if (logLevel >= MDCLOG_DEBUG) { |
| 1326 | mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName); |
| 1327 | } |
| 1328 | for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1329 | auto messageSent = false; |
| 1330 | RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i]; |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1331 | if (logLevel >= MDCLOG_DEBUG) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1332 | mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id); |
| 1333 | } |
| 1334 | if (ie->id == ProtocolIE_ID_id_RICrequestID) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1335 | if (logLevel >= MDCLOG_DEBUG) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1336 | mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id); |
| 1337 | } |
| 1338 | if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) { |
| 1339 | static unsigned char tx[32]; |
| 1340 | message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION; |
| 1341 | snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++); |
| 1342 | rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx)); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 1343 | rmr_bytes2meid(rmrMessageBuffer.sendMessage, |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1344 | (unsigned char *)message.message.enodbName, |
| 1345 | strlen(message.message.enodbName)); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1346 | rmrMessageBuffer.sendMessage->state = 0; |
| 1347 | rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 1348 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 1349 | mdclog_write(MDCLOG_DEBUG, "RIC sub id = %d, message type = %d", |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1350 | rmrMessageBuffer.sendMessage->sub_id, |
| 1351 | rmrMessageBuffer.sendMessage->mtype); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 1352 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1353 | sendRmrMessage(rmrMessageBuffer, message); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1354 | messageSent = true; |
| 1355 | } else { |
| 1356 | mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request"); |
| 1357 | } |
| 1358 | } |
| 1359 | if (messageSent) { |
| 1360 | break; |
| 1361 | } |
| 1362 | } |
| 1363 | break; |
| 1364 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1365 | case ProcedureCode_id_RICserviceQuery: { |
| 1366 | if (logLevel >= MDCLOG_DEBUG) { |
| 1367 | mdclog_write(MDCLOG_DEBUG, "Got RICserviceQuery %s", message.message.enodbName); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1368 | } |
| 1369 | break; |
| 1370 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1371 | case ProcedureCode_id_RICserviceUpdate: { |
| 1372 | if (logLevel >= MDCLOG_DEBUG) { |
| 1373 | mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName); |
| 1374 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1375 | if (sendRequestToXapp(message, RIC_SERVICE_UPDATE, rmrMessageBuffer) != 0) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1376 | mdclog_write(MDCLOG_ERR, "RIC_SERVICE_UPDATE message failed to send to xAPP"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1377 | } |
| 1378 | break; |
| 1379 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1380 | case ProcedureCode_id_RICsubscription: { |
| 1381 | if (logLevel >= MDCLOG_DEBUG) { |
| 1382 | mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName); |
| 1383 | } |
| 1384 | break; |
| 1385 | } |
| 1386 | case ProcedureCode_id_RICsubscriptionDelete: { |
| 1387 | if (logLevel >= MDCLOG_DEBUG) { |
| 1388 | mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1389 | } |
| 1390 | break; |
| 1391 | } |
| 1392 | default: { |
| 1393 | mdclog_write(MDCLOG_ERR, "Undefined or not supported message = %ld", procedureCode); |
| 1394 | message.message.messageType = 0; // no RMR message type yet |
| 1395 | |
| 1396 | buildJsonMessage(message); |
| 1397 | |
| 1398 | break; |
| 1399 | } |
| 1400 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | /** |
| 1404 | * |
| 1405 | * @param pdu |
| 1406 | * @param message |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1407 | * @param rmrMessageBuffer |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1408 | */ |
aa7133@att.com | 11b17d4 | 2020-03-22 11:35:16 +0200 | [diff] [blame] | 1409 | void asnSuccsesfulMsg(E2AP_PDU_t *pdu, ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1410 | auto procedureCode = pdu->choice.successfulOutcome->procedureCode; |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1411 | auto logLevel = mdclog_level_get(); |
| 1412 | if (logLevel >= MDCLOG_INFO) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1413 | mdclog_write(MDCLOG_INFO, "Successful Outcome %ld", procedureCode); |
| 1414 | } |
| 1415 | switch (procedureCode) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1416 | case ProcedureCode_id_E2setup: { |
| 1417 | if (logLevel >= MDCLOG_DEBUG) { |
| 1418 | mdclog_write(MDCLOG_DEBUG, "Got E2setup\n"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1419 | } |
| 1420 | break; |
| 1421 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1422 | case ProcedureCode_id_ErrorIndication: { |
| 1423 | if (logLevel >= MDCLOG_DEBUG) { |
| 1424 | mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1425 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1426 | if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1427 | mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1428 | } |
| 1429 | break; |
| 1430 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1431 | case ProcedureCode_id_Reset: { |
| 1432 | if (logLevel >= MDCLOG_DEBUG) { |
| 1433 | mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1434 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1435 | if (sendRequestToXapp(message, RIC_X2_RESET, rmrMessageBuffer) != 0) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1436 | mdclog_write(MDCLOG_ERR, "RIC_X2_RESET message failed to send to xAPP"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1437 | } |
| 1438 | break; |
| 1439 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1440 | case ProcedureCode_id_RICcontrol: { |
| 1441 | if (logLevel >= MDCLOG_DEBUG) { |
| 1442 | mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1443 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1444 | for (auto i = 0; |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1445 | i < pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.count; i++) { |
| 1446 | auto messageSent = false; |
| 1447 | RICcontrolAcknowledge_IEs_t *ie = pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.array[i]; |
| 1448 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 1449 | mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id); |
| 1450 | } |
| 1451 | if (ie->id == ProtocolIE_ID_id_RICrequestID) { |
| 1452 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 1453 | mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id); |
| 1454 | } |
| 1455 | if (ie->value.present == RICcontrolAcknowledge_IEs__value_PR_RICrequestID) { |
| 1456 | message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_ACK; |
| 1457 | rmrMessageBuffer.sendMessage->state = 0; |
| 1458 | rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID; |
| 1459 | static unsigned char tx[32]; |
| 1460 | snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++); |
| 1461 | rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx)); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 1462 | rmr_bytes2meid(rmrMessageBuffer.sendMessage, |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1463 | (unsigned char *)message.message.enodbName, |
| 1464 | strlen(message.message.enodbName)); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1465 | |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1466 | sendRmrMessage(rmrMessageBuffer, message); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1467 | messageSent = true; |
| 1468 | } else { |
| 1469 | mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request"); |
| 1470 | } |
| 1471 | } |
| 1472 | if (messageSent) { |
| 1473 | break; |
| 1474 | } |
| 1475 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1476 | |
| 1477 | break; |
| 1478 | } |
| 1479 | case ProcedureCode_id_RICindication: { |
| 1480 | if (logLevel >= MDCLOG_DEBUG) { |
| 1481 | mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName); |
| 1482 | } |
| 1483 | for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) { |
| 1484 | auto messageSent = false; |
| 1485 | RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i]; |
| 1486 | if (logLevel >= MDCLOG_DEBUG) { |
| 1487 | mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id); |
| 1488 | } |
| 1489 | if (ie->id == ProtocolIE_ID_id_RICrequestID) { |
| 1490 | if (logLevel >= MDCLOG_DEBUG) { |
| 1491 | mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id); |
| 1492 | } |
| 1493 | if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) { |
| 1494 | static unsigned char tx[32]; |
| 1495 | message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION; |
| 1496 | snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++); |
| 1497 | rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx)); |
| 1498 | rmr_bytes2meid(rmrMessageBuffer.sendMessage, |
| 1499 | (unsigned char *)message.message.enodbName, |
| 1500 | strlen(message.message.enodbName)); |
| 1501 | rmrMessageBuffer.sendMessage->state = 0; |
| 1502 | rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID; |
| 1503 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 1504 | mdclog_write(MDCLOG_DEBUG, "RIC sub id = %d, message type = %d", |
| 1505 | rmrMessageBuffer.sendMessage->sub_id, |
| 1506 | rmrMessageBuffer.sendMessage->mtype); |
| 1507 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1508 | sendRmrMessage(rmrMessageBuffer, message); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1509 | messageSent = true; |
| 1510 | } else { |
| 1511 | mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request"); |
| 1512 | } |
| 1513 | } |
| 1514 | if (messageSent) { |
| 1515 | break; |
| 1516 | } |
| 1517 | } |
| 1518 | break; |
| 1519 | } |
| 1520 | case ProcedureCode_id_RICserviceQuery: { |
| 1521 | if (logLevel >= MDCLOG_DEBUG) { |
| 1522 | mdclog_write(MDCLOG_DEBUG, "Got RICserviceQuery %s", message.message.enodbName); |
| 1523 | } |
| 1524 | break; |
| 1525 | } |
| 1526 | case ProcedureCode_id_RICserviceUpdate: { |
| 1527 | if (logLevel >= MDCLOG_DEBUG) { |
| 1528 | mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName); |
| 1529 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1530 | if (sendRequestToXapp(message, RIC_SERVICE_UPDATE, rmrMessageBuffer) != 0) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1531 | mdclog_write(MDCLOG_ERR, "RIC_SERVICE_UPDATE message failed to send to xAPP"); |
| 1532 | } |
| 1533 | break; |
| 1534 | } |
| 1535 | case ProcedureCode_id_RICsubscription: { |
| 1536 | if (logLevel >= MDCLOG_DEBUG) { |
| 1537 | mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName); |
| 1538 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1539 | if (sendRequestToXapp(message, RIC_SUB_RESP, rmrMessageBuffer) != 0) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1540 | mdclog_write(MDCLOG_ERR, "Subscription successful message failed to send to xAPP"); |
| 1541 | } |
| 1542 | break; |
| 1543 | } |
| 1544 | case ProcedureCode_id_RICsubscriptionDelete: { |
| 1545 | if (logLevel >= MDCLOG_DEBUG) { |
| 1546 | mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName); |
| 1547 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1548 | if (sendRequestToXapp(message, RIC_SUB_DEL_RESP, rmrMessageBuffer) != 0) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1549 | mdclog_write(MDCLOG_ERR, "Subscription delete successful message failed to send to xAPP"); |
| 1550 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1551 | break; |
| 1552 | } |
| 1553 | default: { |
| 1554 | mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode); |
| 1555 | message.message.messageType = 0; // no RMR message type yet |
| 1556 | buildJsonMessage(message); |
| 1557 | |
| 1558 | break; |
| 1559 | } |
| 1560 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | /** |
| 1564 | * |
| 1565 | * @param pdu |
| 1566 | * @param message |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1567 | * @param rmrMessageBuffer |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1568 | */ |
| 1569 | void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu, |
| 1570 | ReportingMessages_t &message, |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1571 | RmrMessagesBuffer_t &rmrMessageBuffer) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1572 | auto procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode; |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1573 | auto logLevel = mdclog_level_get(); |
| 1574 | if (logLevel >= MDCLOG_INFO) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1575 | mdclog_write(MDCLOG_INFO, "Unsuccessful Outcome %ld", procedureCode); |
| 1576 | } |
| 1577 | switch (procedureCode) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1578 | case ProcedureCode_id_E2setup: { |
| 1579 | if (logLevel >= MDCLOG_DEBUG) { |
| 1580 | mdclog_write(MDCLOG_DEBUG, "Got E2setup\n"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1581 | } |
| 1582 | break; |
| 1583 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1584 | case ProcedureCode_id_ErrorIndication: { |
| 1585 | if (logLevel >= MDCLOG_DEBUG) { |
| 1586 | mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1587 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1588 | if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1589 | mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1590 | } |
| 1591 | break; |
| 1592 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1593 | case ProcedureCode_id_Reset: { |
| 1594 | if (logLevel >= MDCLOG_DEBUG) { |
| 1595 | mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName); |
| 1596 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1597 | if (sendRequestToXapp(message, RIC_X2_RESET, rmrMessageBuffer) != 0) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1598 | mdclog_write(MDCLOG_ERR, "RIC_X2_RESET message failed to send to xAPP"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1599 | } |
| 1600 | break; |
| 1601 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1602 | case ProcedureCode_id_RICcontrol: { |
| 1603 | if (logLevel >= MDCLOG_DEBUG) { |
| 1604 | mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1605 | } |
| 1606 | for (int i = 0; |
| 1607 | i < pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.count; i++) { |
| 1608 | auto messageSent = false; |
| 1609 | RICcontrolFailure_IEs_t *ie = pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.array[i]; |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1610 | if (logLevel >= MDCLOG_DEBUG) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1611 | mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id); |
| 1612 | } |
| 1613 | if (ie->id == ProtocolIE_ID_id_RICrequestID) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1614 | if (logLevel >= MDCLOG_DEBUG) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1615 | mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id); |
| 1616 | } |
| 1617 | if (ie->value.present == RICcontrolFailure_IEs__value_PR_RICrequestID) { |
| 1618 | message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_FAILURE; |
| 1619 | rmrMessageBuffer.sendMessage->state = 0; |
| 1620 | rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID; |
| 1621 | static unsigned char tx[32]; |
| 1622 | snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++); |
| 1623 | rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx)); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1624 | rmr_bytes2meid(rmrMessageBuffer.sendMessage, (unsigned char *) message.message.enodbName, |
| 1625 | strlen(message.message.enodbName)); |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1626 | sendRmrMessage(rmrMessageBuffer, message); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1627 | messageSent = true; |
| 1628 | } else { |
| 1629 | mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request"); |
| 1630 | } |
| 1631 | } |
| 1632 | if (messageSent) { |
| 1633 | break; |
| 1634 | } |
| 1635 | } |
| 1636 | break; |
| 1637 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1638 | case ProcedureCode_id_RICindication: { |
| 1639 | if (logLevel >= MDCLOG_DEBUG) { |
| 1640 | mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName); |
| 1641 | } |
| 1642 | for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) { |
| 1643 | auto messageSent = false; |
| 1644 | RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i]; |
| 1645 | if (logLevel >= MDCLOG_DEBUG) { |
| 1646 | mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id); |
| 1647 | } |
| 1648 | if (ie->id == ProtocolIE_ID_id_RICrequestID) { |
| 1649 | if (logLevel >= MDCLOG_DEBUG) { |
| 1650 | mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id); |
| 1651 | } |
| 1652 | if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) { |
| 1653 | static unsigned char tx[32]; |
| 1654 | message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION; |
| 1655 | snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++); |
| 1656 | rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx)); |
| 1657 | rmr_bytes2meid(rmrMessageBuffer.sendMessage, |
| 1658 | (unsigned char *)message.message.enodbName, |
| 1659 | strlen(message.message.enodbName)); |
| 1660 | rmrMessageBuffer.sendMessage->state = 0; |
| 1661 | rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID; |
| 1662 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 1663 | mdclog_write(MDCLOG_DEBUG, "RIC sub id = %d, message type = %d", |
| 1664 | rmrMessageBuffer.sendMessage->sub_id, |
| 1665 | rmrMessageBuffer.sendMessage->mtype); |
| 1666 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1667 | sendRmrMessage(rmrMessageBuffer, message); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1668 | messageSent = true; |
| 1669 | } else { |
| 1670 | mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request"); |
| 1671 | } |
| 1672 | } |
| 1673 | if (messageSent) { |
| 1674 | break; |
| 1675 | } |
| 1676 | } |
| 1677 | break; |
| 1678 | } |
| 1679 | case ProcedureCode_id_RICserviceQuery: { |
| 1680 | if (logLevel >= MDCLOG_DEBUG) { |
| 1681 | mdclog_write(MDCLOG_DEBUG, "Got RICserviceQuery %s", message.message.enodbName); |
| 1682 | } |
| 1683 | break; |
| 1684 | } |
| 1685 | case ProcedureCode_id_RICserviceUpdate: { |
| 1686 | if (logLevel >= MDCLOG_DEBUG) { |
| 1687 | mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName); |
| 1688 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1689 | if (sendRequestToXapp(message, RIC_SERVICE_UPDATE, rmrMessageBuffer) != 0) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1690 | mdclog_write(MDCLOG_ERR, "RIC_SERVICE_UPDATE message failed to send to xAPP"); |
| 1691 | } |
| 1692 | break; |
| 1693 | } |
| 1694 | case ProcedureCode_id_RICsubscription: { |
| 1695 | if (logLevel >= MDCLOG_DEBUG) { |
| 1696 | mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName); |
| 1697 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1698 | if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1699 | mdclog_write(MDCLOG_ERR, "Subscription unsuccessful message failed to send to xAPP"); |
| 1700 | } |
| 1701 | break; |
| 1702 | } |
| 1703 | case ProcedureCode_id_RICsubscriptionDelete: { |
| 1704 | if (logLevel >= MDCLOG_DEBUG) { |
| 1705 | mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName); |
| 1706 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1707 | if (sendRequestToXapp(message, RIC_SUB_DEL_FAILURE, rmrMessageBuffer) != 0) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 1708 | mdclog_write(MDCLOG_ERR, "Subscription Delete unsuccessful message failed to send to xAPP"); |
| 1709 | } |
| 1710 | break; |
| 1711 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1712 | default: { |
| 1713 | mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode); |
| 1714 | message.message.messageType = 0; // no RMR message type yet |
| 1715 | |
| 1716 | buildJsonMessage(message); |
| 1717 | |
| 1718 | break; |
| 1719 | } |
| 1720 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | /** |
| 1724 | * |
| 1725 | * @param message |
| 1726 | * @param requestId |
| 1727 | * @param rmrMmessageBuffer |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1728 | * @return |
| 1729 | */ |
| 1730 | int sendRequestToXapp(ReportingMessages_t &message, |
| 1731 | int requestId, |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1732 | RmrMessagesBuffer_t &rmrMmessageBuffer) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1733 | rmr_bytes2meid(rmrMmessageBuffer.sendMessage, |
| 1734 | (unsigned char *)message.message.enodbName, |
| 1735 | strlen(message.message.enodbName)); |
| 1736 | message.message.messageType = rmrMmessageBuffer.sendMessage->mtype = requestId; |
| 1737 | rmrMmessageBuffer.sendMessage->state = 0; |
| 1738 | static unsigned char tx[32]; |
| 1739 | snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++); |
| 1740 | rmr_bytes2xact(rmrMmessageBuffer.sendMessage, tx, strlen((const char *) tx)); |
| 1741 | |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1742 | auto rc = sendRmrMessage(rmrMmessageBuffer, message); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1743 | return rc; |
| 1744 | } |
| 1745 | |
| 1746 | |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1747 | void getRmrContext(sctp_params_t &pSctpParams) { |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 1748 | pSctpParams.rmrCtx = nullptr; |
| 1749 | pSctpParams.rmrCtx = rmr_init(pSctpParams.rmrAddress, RMR_MAX_RCV_BYTES, RMRFL_NONE); |
| 1750 | if (pSctpParams.rmrCtx == nullptr) { |
| 1751 | mdclog_write(MDCLOG_ERR, "Failed to initialize RMR"); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 1752 | return; |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1753 | } |
| 1754 | |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 1755 | rmr_set_stimeout(pSctpParams.rmrCtx, 0); // disable retries for any send operation |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1756 | // we need to find that routing table exist and we can run |
| 1757 | if (mdclog_level_get() >= MDCLOG_INFO) { |
| 1758 | mdclog_write(MDCLOG_INFO, "We are after RMR INIT wait for RMR_Ready"); |
| 1759 | } |
| 1760 | int rmrReady = 0; |
| 1761 | int count = 0; |
| 1762 | while (!rmrReady) { |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 1763 | if ((rmrReady = rmr_ready(pSctpParams.rmrCtx)) == 0) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1764 | sleep(1); |
| 1765 | } |
| 1766 | count++; |
| 1767 | if (count % 60 == 0) { |
| 1768 | mdclog_write(MDCLOG_INFO, "waiting to RMR ready state for %d seconds", count); |
| 1769 | } |
| 1770 | } |
| 1771 | if (mdclog_level_get() >= MDCLOG_INFO) { |
| 1772 | mdclog_write(MDCLOG_INFO, "RMR running"); |
| 1773 | } |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 1774 | rmr_init_trace(pSctpParams.rmrCtx, 200); |
| 1775 | // get the RMR fd for the epoll |
| 1776 | pSctpParams.rmrListenFd = rmr_get_rcvfd(pSctpParams.rmrCtx); |
| 1777 | struct epoll_event event{}; |
| 1778 | // add RMR fd to epoll |
| 1779 | event.events = (EPOLLIN); |
| 1780 | event.data.fd = pSctpParams.rmrListenFd; |
| 1781 | // add listening RMR FD to epoll |
| 1782 | if (epoll_ctl(pSctpParams.epoll_fd, EPOLL_CTL_ADD, pSctpParams.rmrListenFd, &event)) { |
| 1783 | mdclog_write(MDCLOG_ERR, "Failed to add RMR descriptor to epoll"); |
| 1784 | close(pSctpParams.rmrListenFd); |
| 1785 | rmr_close(pSctpParams.rmrCtx); |
| 1786 | pSctpParams.rmrCtx = nullptr; |
| 1787 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | /** |
| 1791 | * |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1792 | * @param sctpMap |
| 1793 | * @param rmrMessageBuffer |
| 1794 | * @param ts |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1795 | * @return |
| 1796 | */ |
aa7133@att.com | 6680c3b | 2020-03-22 12:06:58 +0200 | [diff] [blame] | 1797 | int receiveXappMessages(Sctp_Map_t *sctpMap, |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1798 | RmrMessagesBuffer_t &rmrMessageBuffer, |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1799 | struct timespec &ts) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1800 | if (rmrMessageBuffer.rcvMessage == nullptr) { |
| 1801 | //we have error |
| 1802 | mdclog_write(MDCLOG_ERR, "RMR Allocation message, %s", strerror(errno)); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1803 | return -1; |
| 1804 | } |
| 1805 | |
| 1806 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 1807 | mdclog_write(MDCLOG_DEBUG, "Call to rmr_rcv_msg"); |
| 1808 | } |
| 1809 | rmrMessageBuffer.rcvMessage = rmr_rcv_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage); |
| 1810 | if (rmrMessageBuffer.rcvMessage == nullptr) { |
| 1811 | mdclog_write(MDCLOG_ERR, "RMR Receving message with null pointer, Realloc rmr mesage buffer"); |
| 1812 | rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1813 | return -2; |
| 1814 | } |
| 1815 | ReportingMessages_t message; |
| 1816 | message.message.direction = 'D'; |
| 1817 | message.message.time.tv_nsec = ts.tv_nsec; |
| 1818 | message.message.time.tv_sec = ts.tv_sec; |
| 1819 | |
| 1820 | // get message payload |
| 1821 | //auto msgData = msg->payload; |
| 1822 | if (rmrMessageBuffer.rcvMessage->state != 0) { |
| 1823 | mdclog_write(MDCLOG_ERR, "RMR Receving message with stat = %d", rmrMessageBuffer.rcvMessage->state); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1824 | return -1; |
| 1825 | } |
| 1826 | switch (rmrMessageBuffer.rcvMessage->mtype) { |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1827 | case RIC_E2_SETUP_RESP : { |
| 1828 | if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) { |
| 1829 | mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_RESP"); |
| 1830 | return -6; |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1831 | } |
| 1832 | break; |
| 1833 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1834 | case RIC_E2_SETUP_FAILURE : { |
| 1835 | if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) { |
| 1836 | mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_FAILURE"); |
| 1837 | return -6; |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1838 | } |
| 1839 | break; |
| 1840 | } |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1841 | case RIC_ERROR_INDICATION: { |
| 1842 | if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) { |
| 1843 | mdclog_write(MDCLOG_ERR, "Failed to send RIC_ERROR_INDICATION"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1844 | return -6; |
| 1845 | } |
| 1846 | break; |
| 1847 | } |
| 1848 | case RIC_SUB_REQ: { |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1849 | if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1850 | mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_REQ"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1851 | return -6; |
| 1852 | } |
| 1853 | break; |
| 1854 | } |
| 1855 | case RIC_SUB_DEL_REQ: { |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1856 | if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1857 | mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_DEL_REQ"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1858 | return -6; |
| 1859 | } |
| 1860 | break; |
| 1861 | } |
| 1862 | case RIC_CONTROL_REQ: { |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1863 | if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1864 | mdclog_write(MDCLOG_ERR, "Failed to send RIC_CONTROL_REQ"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1865 | return -6; |
| 1866 | } |
| 1867 | break; |
| 1868 | } |
| 1869 | case RIC_SERVICE_QUERY: { |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1870 | if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1871 | mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_QUERY"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1872 | return -6; |
| 1873 | } |
| 1874 | break; |
| 1875 | } |
| 1876 | case RIC_SERVICE_UPDATE_ACK: { |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1877 | if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1878 | mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_ACK"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1879 | return -6; |
| 1880 | } |
| 1881 | break; |
| 1882 | } |
| 1883 | case RIC_SERVICE_UPDATE_FAILURE: { |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1884 | if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1885 | mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_FAILURE"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1886 | return -6; |
| 1887 | } |
| 1888 | break; |
| 1889 | } |
| 1890 | case RIC_X2_RESET: { |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1891 | if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1892 | mdclog_write(MDCLOG_ERR, "Failed to send RIC_X2_RESET"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1893 | return -6; |
| 1894 | } |
| 1895 | break; |
| 1896 | } |
| 1897 | case RIC_X2_RESET_RESP: { |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1898 | if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1899 | mdclog_write(MDCLOG_ERR, "Failed to send RIC_X2_RESET_RESP"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1900 | return -6; |
| 1901 | } |
| 1902 | break; |
| 1903 | } |
| 1904 | case RIC_SCTP_CLEAR_ALL: { |
| 1905 | mdclog_write(MDCLOG_INFO, "RIC_SCTP_CLEAR_ALL"); |
| 1906 | // loop on all keys and close socket and then erase all map. |
| 1907 | vector<char *> v; |
| 1908 | sctpMap->getKeys(v); |
| 1909 | for (auto const &iter : v) { //}; iter != sctpMap.end(); iter++) { |
| 1910 | if (!boost::starts_with((string) (iter), "host:") && !boost::starts_with((string) (iter), "msg:")) { |
| 1911 | auto *peerInfo = (ConnectedCU_t *) sctpMap->find(iter); |
| 1912 | if (peerInfo == nullptr) { |
| 1913 | continue; |
| 1914 | } |
| 1915 | close(peerInfo->fileDescriptor); |
| 1916 | memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName)); |
| 1917 | message.message.direction = 'D'; |
| 1918 | message.message.time.tv_nsec = ts.tv_nsec; |
| 1919 | message.message.time.tv_sec = ts.tv_sec; |
| 1920 | |
| 1921 | message.message.asnLength = rmrMessageBuffer.sendMessage->len = |
| 1922 | snprintf((char *)rmrMessageBuffer.sendMessage->payload, |
| 1923 | 256, |
| 1924 | "%s|RIC_SCTP_CLEAR_ALL", |
| 1925 | peerInfo->enodbName); |
| 1926 | message.message.asndata = rmrMessageBuffer.sendMessage->payload; |
| 1927 | mdclog_write(MDCLOG_INFO, "%s", message.message.asndata); |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1928 | if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1929 | mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP"); |
| 1930 | } |
| 1931 | free(peerInfo); |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | sleep(1); |
| 1936 | sctpMap->clear(); |
| 1937 | break; |
| 1938 | } |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 1939 | case E2_TERM_KEEP_ALIVE_REQ: { |
| 1940 | // send message back |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 1941 | rmr_bytes2payload(rmrMessageBuffer.sendMessage, |
| 1942 | (unsigned char *)rmrMessageBuffer.ka_message, |
| 1943 | rmrMessageBuffer.ka_message_len); |
| 1944 | rmrMessageBuffer.sendMessage->mtype = E2_TERM_KEEP_ALIVE_RESP; |
| 1945 | rmrMessageBuffer.sendMessage->state = 0; |
| 1946 | static unsigned char tx[32]; |
| 1947 | auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++); |
| 1948 | rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, txLen); |
| 1949 | rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage); |
| 1950 | if (rmrMessageBuffer.sendMessage == nullptr) { |
| 1951 | rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE); |
aa7133@att.com | 77fff43 | 2020-01-27 12:01:20 +0200 | [diff] [blame] | 1952 | mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP RMR message returned NULL"); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 1953 | } else if (rmrMessageBuffer.sendMessage->state != 0) { |
aa7133@att.com | 77fff43 | 2020-01-27 12:01:20 +0200 | [diff] [blame] | 1954 | mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP, on RMR state = %d ( %s)", |
| 1955 | rmrMessageBuffer.sendMessage->state, translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str()); |
aa7133@att.com | 342843e | 2020-01-26 12:53:34 +0200 | [diff] [blame] | 1956 | } else if (mdclog_level_get() >= MDCLOG_INFO) { |
| 1957 | mdclog_write(MDCLOG_INFO, "Got Keep Alive Request send : %s", rmrMessageBuffer.ka_message); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 1958 | } |
aa7133@att.com | 342843e | 2020-01-26 12:53:34 +0200 | [diff] [blame] | 1959 | |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 1960 | break; |
| 1961 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1962 | default: |
| 1963 | mdclog_write(MDCLOG_WARN, "Message Type : %d is not seported", rmrMessageBuffer.rcvMessage->mtype); |
| 1964 | message.message.asndata = rmrMessageBuffer.rcvMessage->payload; |
| 1965 | message.message.asnLength = rmrMessageBuffer.rcvMessage->len; |
| 1966 | message.message.time.tv_nsec = ts.tv_nsec; |
| 1967 | message.message.time.tv_sec = ts.tv_sec; |
| 1968 | message.message.messageType = rmrMessageBuffer.rcvMessage->mtype; |
| 1969 | |
| 1970 | buildJsonMessage(message); |
| 1971 | |
| 1972 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1973 | return -7; |
| 1974 | } |
| 1975 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 1976 | mdclog_write(MDCLOG_DEBUG, "EXIT OK from %s", __FUNCTION__); |
| 1977 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1978 | return 0; |
| 1979 | } |
| 1980 | |
| 1981 | /** |
| 1982 | * Send message to the CU that is not expecting for successful or unsuccessful results |
| 1983 | * @param messageBuffer |
| 1984 | * @param message |
| 1985 | * @param failedMsgId |
| 1986 | * @param sctpMap |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1987 | * @return |
| 1988 | */ |
| 1989 | int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messageBuffer, |
| 1990 | ReportingMessages_t &message, |
| 1991 | int failedMsgId, |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1992 | Sctp_Map_t *sctpMap) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1993 | |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1994 | getRequestMetaData(message, messageBuffer); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1995 | if (mdclog_level_get() >= MDCLOG_INFO) { |
| 1996 | mdclog_write(MDCLOG_INFO, "send message to %s address", message.message.enodbName); |
| 1997 | } |
| 1998 | |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 1999 | auto rc = sendMessagetoCu(sctpMap, messageBuffer, message, failedMsgId); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2000 | return rc; |
| 2001 | } |
| 2002 | |
| 2003 | /** |
| 2004 | * |
| 2005 | * @param sctpMap |
| 2006 | * @param messageBuffer |
| 2007 | * @param message |
| 2008 | * @param failedMesgId |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2009 | * @return |
| 2010 | */ |
| 2011 | int sendMessagetoCu(Sctp_Map_t *sctpMap, |
| 2012 | RmrMessagesBuffer_t &messageBuffer, |
| 2013 | ReportingMessages_t &message, |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 2014 | int failedMesgId) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2015 | auto *peerInfo = (ConnectedCU_t *) sctpMap->find(message.message.enodbName); |
| 2016 | if (peerInfo == nullptr) { |
| 2017 | if (failedMesgId != 0) { |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 2018 | sendFailedSendingMessagetoXapp(messageBuffer, message, failedMesgId); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2019 | } else { |
| 2020 | mdclog_write(MDCLOG_ERR, "Failed to send message no CU entry %s", message.message.enodbName); |
| 2021 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2022 | return -1; |
| 2023 | } |
| 2024 | |
| 2025 | // get the FD |
| 2026 | message.message.messageType = messageBuffer.rcvMessage->mtype; |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 2027 | auto rc = sendSctpMsg(peerInfo, message, sctpMap); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2028 | return rc; |
| 2029 | } |
| 2030 | |
| 2031 | /** |
| 2032 | * |
| 2033 | * @param rmrCtx the rmr context to send and receive |
| 2034 | * @param msg the msg we got fromxApp |
| 2035 | * @param metaData data from xApp in ordered struct |
| 2036 | * @param failedMesgId the return message type error |
| 2037 | */ |
| 2038 | void |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 2039 | sendFailedSendingMessagetoXapp(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message, int failedMesgId) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2040 | rmr_mbuf_t *msg = rmrMessageBuffer.sendMessage; |
| 2041 | msg->len = snprintf((char *) msg->payload, 200, "the gNb/eNode name %s not found", |
| 2042 | message.message.enodbName); |
| 2043 | if (mdclog_level_get() >= MDCLOG_INFO) { |
| 2044 | mdclog_write(MDCLOG_INFO, "%s", msg->payload); |
| 2045 | } |
| 2046 | msg->mtype = failedMesgId; |
| 2047 | msg->state = 0; |
| 2048 | |
| 2049 | static unsigned char tx[32]; |
| 2050 | snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++); |
| 2051 | rmr_bytes2xact(msg, tx, strlen((const char *) tx)); |
| 2052 | |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 2053 | sendRmrMessage(rmrMessageBuffer, message); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2054 | } |
| 2055 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2056 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2057 | |
| 2058 | /** |
| 2059 | * |
| 2060 | * @param epoll_fd |
| 2061 | * @param peerInfo |
| 2062 | * @param events |
| 2063 | * @param sctpMap |
| 2064 | * @param enodbName |
| 2065 | * @param msgType |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2066 | * @return |
| 2067 | */ |
| 2068 | int addToEpoll(int epoll_fd, |
| 2069 | ConnectedCU_t *peerInfo, |
| 2070 | uint32_t events, |
| 2071 | Sctp_Map_t *sctpMap, |
| 2072 | char *enodbName, |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 2073 | int msgType) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2074 | // Add to Epol |
| 2075 | struct epoll_event event{}; |
| 2076 | event.data.ptr = peerInfo; |
| 2077 | event.events = events; |
| 2078 | if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peerInfo->fileDescriptor, &event) < 0) { |
| 2079 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 2080 | mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here), %s, %s %d", |
| 2081 | strerror(errno), __func__, __LINE__); |
| 2082 | } |
| 2083 | close(peerInfo->fileDescriptor); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 2084 | if (enodbName != nullptr) { |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 2085 | cleanHashEntry(peerInfo, sctpMap); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 2086 | char key[MAX_ENODB_NAME_SIZE * 2]; |
| 2087 | snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType); |
| 2088 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 2089 | mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__); |
| 2090 | } |
| 2091 | auto tmp = sctpMap->find(key); |
| 2092 | if (tmp) { |
| 2093 | free(tmp); |
| 2094 | sctpMap->erase(key); |
| 2095 | } |
| 2096 | } else { |
| 2097 | peerInfo->enodbName[0] = 0; |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2098 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2099 | mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here)"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2100 | return -1; |
| 2101 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2102 | return 0; |
| 2103 | } |
| 2104 | |
| 2105 | /** |
| 2106 | * |
| 2107 | * @param epoll_fd |
| 2108 | * @param peerInfo |
| 2109 | * @param events |
| 2110 | * @param sctpMap |
| 2111 | * @param enodbName |
| 2112 | * @param msgType |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2113 | * @return |
| 2114 | */ |
| 2115 | int modifyToEpoll(int epoll_fd, |
| 2116 | ConnectedCU_t *peerInfo, |
| 2117 | uint32_t events, |
| 2118 | Sctp_Map_t *sctpMap, |
| 2119 | char *enodbName, |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 2120 | int msgType) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2121 | // Add to Epol |
| 2122 | struct epoll_event event{}; |
| 2123 | event.data.ptr = peerInfo; |
| 2124 | event.events = events; |
| 2125 | if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, peerInfo->fileDescriptor, &event) < 0) { |
| 2126 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 2127 | mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_MOD (may chack not to quit here), %s, %s %d", |
| 2128 | strerror(errno), __func__, __LINE__); |
| 2129 | } |
| 2130 | close(peerInfo->fileDescriptor); |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 2131 | cleanHashEntry(peerInfo, sctpMap); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2132 | char key[MAX_ENODB_NAME_SIZE * 2]; |
| 2133 | snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType); |
| 2134 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 2135 | mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__); |
| 2136 | } |
| 2137 | auto tmp = sctpMap->find(key); |
| 2138 | if (tmp) { |
| 2139 | free(tmp); |
| 2140 | } |
| 2141 | sctpMap->erase(key); |
| 2142 | mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here)"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2143 | return -1; |
| 2144 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2145 | return 0; |
| 2146 | } |
| 2147 | |
| 2148 | |
aa7133@att.com | be92d71 | 2020-03-18 17:30:39 +0200 | [diff] [blame] | 2149 | int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message) { |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2150 | buildJsonMessage(message); |
| 2151 | |
| 2152 | rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage); |
| 2153 | |
| 2154 | if (rmrMessageBuffer.sendMessage == nullptr) { |
| 2155 | rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE); |
| 2156 | mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer"); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2157 | return -1; |
| 2158 | } |
| 2159 | |
| 2160 | if (rmrMessageBuffer.sendMessage->state != 0) { |
| 2161 | char meid[RMR_MAX_MEID]{}; |
| 2162 | if (rmrMessageBuffer.sendMessage->state == RMR_ERR_RETRY) { |
| 2163 | usleep(5); |
| 2164 | rmrMessageBuffer.sendMessage->state = 0; |
| 2165 | mdclog_write(MDCLOG_INFO, "RETRY sending Message type %d to Xapp from %s", |
| 2166 | rmrMessageBuffer.sendMessage->mtype, |
| 2167 | rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid)); |
| 2168 | rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage); |
| 2169 | if (rmrMessageBuffer.sendMessage == nullptr) { |
| 2170 | mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer"); |
| 2171 | rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2172 | return -1; |
| 2173 | } else if (rmrMessageBuffer.sendMessage->state != 0) { |
| 2174 | mdclog_write(MDCLOG_ERR, |
| 2175 | "Message state %s while sending request %d to Xapp from %s after retry of 10 microseconds", |
| 2176 | translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(), |
| 2177 | rmrMessageBuffer.sendMessage->mtype, |
| 2178 | rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid)); |
| 2179 | auto rc = rmrMessageBuffer.sendMessage->state; |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2180 | return rc; |
| 2181 | } |
| 2182 | } else { |
| 2183 | mdclog_write(MDCLOG_ERR, "Message state %s while sending request %d to Xapp from %s", |
| 2184 | translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(), |
| 2185 | rmrMessageBuffer.sendMessage->mtype, |
| 2186 | rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid)); |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2187 | return rmrMessageBuffer.sendMessage->state; |
| 2188 | } |
| 2189 | } |
| 2190 | return 0; |
| 2191 | } |
| 2192 | |
| 2193 | void buildJsonMessage(ReportingMessages_t &message) { |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 2194 | if (jsonTrace) { |
| 2195 | message.outLen = sizeof(message.base64Data); |
| 2196 | base64::encode((const unsigned char *) message.message.asndata, |
| 2197 | (const int) message.message.asnLength, |
| 2198 | message.base64Data, |
| 2199 | message.outLen); |
| 2200 | if (mdclog_level_get() >= MDCLOG_DEBUG) { |
| 2201 | mdclog_write(MDCLOG_DEBUG, "asn data length = %d, base64 message length = %d ", |
| 2202 | (int) message.message.asnLength, |
| 2203 | (int) message.outLen); |
| 2204 | } |
| 2205 | |
| 2206 | snprintf(message.buffer, sizeof(message.buffer), |
| 2207 | "{\"header\": {\"ts\": \"%ld.%09ld\"," |
| 2208 | "\"ranName\": \"%s\"," |
| 2209 | "\"messageType\": %d," |
| 2210 | "\"direction\": \"%c\"}," |
| 2211 | "\"base64Length\": %d," |
| 2212 | "\"asnBase64\": \"%s\"}", |
| 2213 | message.message.time.tv_sec, |
| 2214 | message.message.time.tv_nsec, |
| 2215 | message.message.enodbName, |
| 2216 | message.message.messageType, |
| 2217 | message.message.direction, |
| 2218 | (int) message.outLen, |
| 2219 | message.base64Data); |
| 2220 | static src::logger_mt &lg = my_logger::get(); |
| 2221 | |
| 2222 | BOOST_LOG(lg) << message.buffer; |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2223 | } |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2224 | } |
| 2225 | |
| 2226 | |
| 2227 | /** |
| 2228 | * take RMR error code to string |
| 2229 | * @param state |
| 2230 | * @return |
| 2231 | */ |
| 2232 | string translateRmrErrorMessages(int state) { |
| 2233 | string str = {}; |
| 2234 | switch (state) { |
| 2235 | case RMR_OK: |
| 2236 | str = "RMR_OK - state is good"; |
| 2237 | break; |
| 2238 | case RMR_ERR_BADARG: |
| 2239 | str = "RMR_ERR_BADARG - argument passd to function was unusable"; |
| 2240 | break; |
| 2241 | case RMR_ERR_NOENDPT: |
| 2242 | str = "RMR_ERR_NOENDPT - send//call could not find an endpoint based on msg type"; |
| 2243 | break; |
| 2244 | case RMR_ERR_EMPTY: |
| 2245 | str = "RMR_ERR_EMPTY - msg received had no payload; attempt to send an empty message"; |
| 2246 | break; |
| 2247 | case RMR_ERR_NOHDR: |
| 2248 | str = "RMR_ERR_NOHDR - message didn't contain a valid header"; |
| 2249 | break; |
| 2250 | case RMR_ERR_SENDFAILED: |
| 2251 | str = "RMR_ERR_SENDFAILED - send failed; errno has nano reason"; |
| 2252 | break; |
| 2253 | case RMR_ERR_CALLFAILED: |
| 2254 | str = "RMR_ERR_CALLFAILED - unable to send call() message"; |
| 2255 | break; |
| 2256 | case RMR_ERR_NOWHOPEN: |
| 2257 | str = "RMR_ERR_NOWHOPEN - no wormholes are open"; |
| 2258 | break; |
| 2259 | case RMR_ERR_WHID: |
| 2260 | str = "RMR_ERR_WHID - wormhole id was invalid"; |
| 2261 | break; |
| 2262 | case RMR_ERR_OVERFLOW: |
| 2263 | str = "RMR_ERR_OVERFLOW - operation would have busted through a buffer/field size"; |
| 2264 | break; |
| 2265 | case RMR_ERR_RETRY: |
| 2266 | str = "RMR_ERR_RETRY - request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)"; |
| 2267 | break; |
| 2268 | case RMR_ERR_RCVFAILED: |
| 2269 | str = "RMR_ERR_RCVFAILED - receive failed (hard error)"; |
| 2270 | break; |
| 2271 | case RMR_ERR_TIMEOUT: |
| 2272 | str = "RMR_ERR_TIMEOUT - message processing call timed out"; |
| 2273 | break; |
| 2274 | case RMR_ERR_UNSET: |
| 2275 | str = "RMR_ERR_UNSET - the message hasn't been populated with a transport buffer"; |
| 2276 | break; |
| 2277 | case RMR_ERR_TRUNC: |
| 2278 | str = "RMR_ERR_TRUNC - received message likely truncated"; |
| 2279 | break; |
| 2280 | case RMR_ERR_INITFAILED: |
| 2281 | str = "RMR_ERR_INITFAILED - initialisation of something (probably message) failed"; |
| 2282 | break; |
| 2283 | case RMR_ERR_NOTSUPP: |
| 2284 | str = "RMR_ERR_NOTSUPP - the request is not supported, or RMr was not initialised for the request"; |
| 2285 | break; |
| 2286 | default: |
| 2287 | char buf[128]{}; |
| 2288 | snprintf(buf, sizeof buf, "UNDOCUMENTED RMR_ERR : %d", state); |
| 2289 | str = buf; |
| 2290 | break; |
| 2291 | } |
| 2292 | return str; |
| 2293 | } |
| 2294 | |
| 2295 | |