smf_config.cpp 43.5 KB
Newer Older
gauthier's avatar
gauthier committed
1
2
3
4
5
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
6
7
 * the OAI Public License, Version 1.1  (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
8
 * License at
gauthier's avatar
gauthier committed
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
22
/*! \file smf_config.cpp
23
24
25
26
27
 \brief
 \author  Lionel GAUTHIER, Tien-Thinh NGUYEN
 \company Eurecom
 \date 2019
 \email: lionel.gauthier@eurecom.fr, tien-thinh.nguyen@eurecom.fr
28
 */
gauthier's avatar
gauthier committed
29

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
30
#include "smf_config.hpp"
gauthier's avatar
gauthier committed
31
32
33
34

#include <cstdlib>
#include <iomanip>
#include <iostream>
35
#include "string.hpp"
gauthier's avatar
gauthier committed
36
37

// C includes
38
#include <arpa/inet.h>
gauthier's avatar
gauthier committed
39
40
41
42
43
44
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

45
46
47
48
49
50
51
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include "common_defs.h"
#include "epc.h"
#include "if.hpp"
#include "logger.hpp"
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
52
#include "fqdn.hpp"
53
54
55
56
57
58
#include "smf_app.hpp"

using namespace std;
using namespace libconfig;
using namespace smf;

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
59
extern smf_config smf_cfg;
gauthier's avatar
gauthier committed
60
61

//------------------------------------------------------------------------------
62
int smf_config::finalize() {
63
  Logger::smf_app().info("Finalize config...");
gauthier's avatar
gauthier committed
64
65

  for (int i = 0; i < num_ue_pool; i++) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
66
    uint32_t range_low_hbo  = ntohl(ue_pool_range_low[i].s_addr);
gauthier's avatar
gauthier committed
67
    uint32_t range_high_hbo = ntohl(ue_pool_range_high[i].s_addr);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
68
69
    uint32_t tmp_hbo        = range_low_hbo ^ range_high_hbo;
    uint8_t nbits           = 32;
gauthier's avatar
gauthier committed
70
71
    while (tmp_hbo) {
      tmp_hbo = tmp_hbo >> 1;
72
      nbits -= 1;
gauthier's avatar
gauthier committed
73
    }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
74
75
    uint32_t network_hbo      = range_high_hbo & (UINT32_MAX << (32 - nbits));
    uint32_t netmask_hbo      = 0xFFFFFFFF << (32 - nbits);
gauthier's avatar
gauthier committed
76
77
78
    ue_pool_network[i].s_addr = htonl(network_hbo);
    ue_pool_netmask[i].s_addr = htonl(netmask_hbo);
  }
79
  Logger::smf_app().info("Finalized config");
gauthier's avatar
gauthier committed
80
81
82
  return 0;
}

83
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
84
85
int smf_config::load_thread_sched_params(
    const Setting& thread_sched_params_cfg, util::thread_sched_params& cfg) {
86
  try {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
87
88
89
90
91
    thread_sched_params_cfg.lookupValue(
        SMF_CONFIG_STRING_THREAD_RD_CPU_ID, cfg.cpu_id);
  } catch (const SettingNotFoundException& nfex) {
    Logger::smf_app().info(
        "%s : %s, using defaults", nfex.what(), nfex.getPath());
92
  }
93
94
  try {
    std::string thread_rd_sched_policy;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
95
    thread_sched_params_cfg.lookupValue(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
96
        SMF_CONFIG_STRING_THREAD_RD_SCHED_POLICY, thread_rd_sched_policy);
97
98
99
100
101
102
103
104
105
106
107
108
    util::trim(thread_rd_sched_policy);
    if (boost::iequals(thread_rd_sched_policy, "SCHED_OTHER")) {
      cfg.sched_policy = SCHED_OTHER;
    } else if (boost::iequals(thread_rd_sched_policy, "SCHED_IDLE")) {
      cfg.sched_policy = SCHED_IDLE;
    } else if (boost::iequals(thread_rd_sched_policy, "SCHED_BATCH")) {
      cfg.sched_policy = SCHED_BATCH;
    } else if (boost::iequals(thread_rd_sched_policy, "SCHED_FIFO")) {
      cfg.sched_policy = SCHED_FIFO;
    } else if (boost::iequals(thread_rd_sched_policy, "SCHED_RR")) {
      cfg.sched_policy = SCHED_RR;
    } else {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
109
110
111
      Logger::smf_app().error(
          "thread_rd_sched_policy: %s, unknown in config file",
          thread_rd_sched_policy.c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
112
      return RETURNerror;
113
    }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
114
115
116
  } catch (const SettingNotFoundException& nfex) {
    Logger::smf_app().info(
        "%s : %s, using defaults", nfex.what(), nfex.getPath());
117
  }
118
119

  try {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
120
    thread_sched_params_cfg.lookupValue(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
121
        SMF_CONFIG_STRING_THREAD_RD_SCHED_PRIORITY, cfg.sched_priority);
122
    if ((cfg.sched_priority > 99) || (cfg.sched_priority < 1)) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
123
      Logger::smf_app().error(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
124
125
          "thread_rd_sched_priority: %d, must be in interval [1..99] in config "
          "file",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
126
          cfg.sched_priority);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
127
      return RETURNerror;
128
    }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
129
130
131
  } catch (const SettingNotFoundException& nfex) {
    Logger::smf_app().info(
        "%s : %s, using defaults", nfex.what(), nfex.getPath());
132
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
133
  return RETURNok;
134
135
}
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
136
int smf_config::load_itti(const Setting& itti_cfg, itti_cfg_t& cfg) {
137
  try {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
138
    const Setting& itti_timer_sched_params_cfg =
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
139
        itti_cfg[SMF_CONFIG_STRING_ITTI_TIMER_SCHED_PARAMS];
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
140
141
142
143
144
    load_thread_sched_params(
        itti_timer_sched_params_cfg, cfg.itti_timer_sched_params);
  } catch (const SettingNotFoundException& nfex) {
    Logger::smf_app().info(
        "%s : %s, using defaults", nfex.what(), nfex.getPath());
145
  }
146

147
  try {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
148
    const Setting& n4_sched_params_cfg =
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
149
        itti_cfg[SMF_CONFIG_STRING_N4_SCHED_PARAMS];
150
    load_thread_sched_params(n4_sched_params_cfg, cfg.n4_sched_params);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
151
152
153
  } catch (const SettingNotFoundException& nfex) {
    Logger::smf_app().info(
        "%s : %s, using defaults", nfex.what(), nfex.getPath());
154
  }
155

156
  try {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
157
    const Setting& smf_app_sched_params_cfg =
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
158
        itti_cfg[SMF_CONFIG_STRING_SMF_APP_SCHED_PARAMS];
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
159
160
161
162
163
    load_thread_sched_params(
        smf_app_sched_params_cfg, cfg.smf_app_sched_params);
  } catch (const SettingNotFoundException& nfex) {
    Logger::smf_app().info(
        "%s : %s, using defaults", nfex.what(), nfex.getPath());
164
  }
165

166
  try {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
167
    const Setting& async_cmd_sched_params_cfg =
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
168
        itti_cfg[SMF_CONFIG_STRING_ASYNC_CMD_SCHED_PARAMS];
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
169
170
171
172
173
    load_thread_sched_params(
        async_cmd_sched_params_cfg, cfg.async_cmd_sched_params);
  } catch (const SettingNotFoundException& nfex) {
    Logger::smf_app().info(
        "%s : %s, using defaults", nfex.what(), nfex.getPath());
174
  }
175

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
176
  return RETURNok;
177
178
}

gauthier's avatar
gauthier committed
179
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
180
int smf_config::load_interface(const Setting& if_cfg, interface_cfg_t& cfg) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
181
  if_cfg.lookupValue(SMF_CONFIG_STRING_INTERFACE_NAME, cfg.if_name);
gauthier's avatar
gauthier committed
182
183
  util::trim(cfg.if_name);
  if (not boost::iequals(cfg.if_name, "none")) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
184
    std::string address = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
185
    if_cfg.lookupValue(SMF_CONFIG_STRING_IPV4_ADDRESS, address);
gauthier's avatar
gauthier committed
186
187
    util::trim(address);
    if (boost::iequals(address, "read")) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
188
189
      if (get_inet_addr_infos_from_iface(
              cfg.if_name, cfg.addr4, cfg.network4, cfg.mtu)) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
190
191
        Logger::smf_app().error(
            "Could not read %s network interface configuration", cfg.if_name);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
192
        return RETURNerror;
gauthier's avatar
gauthier committed
193
194
      }
    } else {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
195
      std::vector<std::string> words = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
196
197
      boost::split(
          words, address, boost::is_any_of("/"), boost::token_compress_on);
gauthier's avatar
gauthier committed
198
      if (words.size() != 2) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
199
200
201
        Logger::smf_app().error(
            "Bad value " SMF_CONFIG_STRING_IPV4_ADDRESS " = %s in config file",
            address.c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
202
        return RETURNerror;
gauthier's avatar
gauthier committed
203
      }
204
      unsigned char buf_in_addr[sizeof(struct in6_addr)];  // you never know...
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
205
206
      if (inet_pton(AF_INET, util::trim(words.at(0)).c_str(), buf_in_addr) ==
          1) {
207
        memcpy(&cfg.addr4, buf_in_addr, sizeof(struct in_addr));
gauthier's avatar
gauthier committed
208
      } else {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
209
        Logger::smf_app().error(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
210
211
            "In conversion: Bad value " SMF_CONFIG_STRING_IPV4_ADDRESS
            " = %s in config file",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
212
            util::trim(words.at(0)).c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
213
        return RETURNerror;
gauthier's avatar
gauthier committed
214
      }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
215
216
217
      cfg.network4.s_addr = htons(
          ntohs(cfg.addr4.s_addr) &
          0xFFFFFFFF << (32 - std::stoi(util::trim(words.at(1)))));
gauthier's avatar
gauthier committed
218
    }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
219
    if_cfg.lookupValue(SMF_CONFIG_STRING_PORT, cfg.port);
220
221

    try {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
222
      const Setting& sched_params_cfg = if_cfg[SMF_CONFIG_STRING_SCHED_PARAMS];
223
      load_thread_sched_params(sched_params_cfg, cfg.thread_rd_sched_params);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
224
225
226
    } catch (const SettingNotFoundException& nfex) {
      Logger::smf_app().info(
          "%s : %s, using defaults", nfex.what(), nfex.getPath());
227
    }
gauthier's avatar
gauthier committed
228
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
229
  return RETURNok;
gauthier's avatar
gauthier committed
230
231
232
}

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
233
int smf_config::load(const string& config_file) {
234
235
  Config cfg;
  unsigned char buf_in6_addr[sizeof(struct in6_addr)];
gauthier's avatar
gauthier committed
236
237

  // Read the file. If there is an error, report it and exit.
238
  try {
gauthier's avatar
gauthier committed
239
    cfg.readFile(config_file.c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
240
241
242
243
  } catch (const FileIOException& fioex) {
    Logger::smf_app().error(
        "I/O error while reading file %s - %s", config_file.c_str(),
        fioex.what());
gauthier's avatar
gauthier committed
244
    throw;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
245
246
247
248
  } catch (const ParseException& pex) {
    Logger::smf_app().error(
        "Parse error at %s:%d - %s", pex.getFile(), pex.getLine(),
        pex.getError());
gauthier's avatar
gauthier committed
249
    throw;
gauthier's avatar
gauthier committed
250
251
  }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
252
  const Setting& root = cfg.getRoot();
gauthier's avatar
gauthier committed
253

254
  try {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
255
256
    const Setting& smf_cfg = root[SMF_CONFIG_STRING_SMF_CONFIG];
  } catch (const SettingNotFoundException& nfex) {
257
    Logger::smf_app().error("%s : %s", nfex.what(), nfex.getPath());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
258
    return RETURNerror;
259
260
  }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
261
  const Setting& smf_cfg = root[SMF_CONFIG_STRING_SMF_CONFIG];
gauthier's avatar
gauthier committed
262

263
  try {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
264
    smf_cfg.lookupValue(SMF_CONFIG_STRING_INSTANCE, instance);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
265
266
267
  } catch (const SettingNotFoundException& nfex) {
    Logger::smf_app().info(
        "%s : %s, using defaults", nfex.what(), nfex.getPath());
268
269
270
  }

  try {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
271
    smf_cfg.lookupValue(SMF_CONFIG_STRING_PID_DIRECTORY, pid_dir);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
272
273
274
  } catch (const SettingNotFoundException& nfex) {
    Logger::smf_app().info(
        "%s : %s, using defaults", nfex.what(), nfex.getPath());
275
  }
gauthier's avatar
gauthier committed
276

277
  try {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
278
    const Setting& itti_cfg = smf_cfg[SMF_CONFIG_STRING_ITTI_TASKS];
279
    load_itti(itti_cfg, itti);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
280
281
282
  } catch (const SettingNotFoundException& nfex) {
    Logger::smf_app().info(
        "%s : %s, using defaults", nfex.what(), nfex.getPath());
283
  }
284

285
  try {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
286
    const Setting& nw_if_cfg = smf_cfg[SMF_CONFIG_STRING_INTERFACES];
gauthier's avatar
gauthier committed
287

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
288
    const Setting& n4_cfg = nw_if_cfg[SMF_CONFIG_STRING_INTERFACE_N4];
289
290
    load_interface(n4_cfg, n4);

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
291
    const Setting& sbi_cfg = nw_if_cfg[SMF_CONFIG_STRING_INTERFACE_SBI];
292
    load_interface(sbi_cfg, sbi);
293

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
294
    // HTTP2 port
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
295
296
    if (!(sbi_cfg.lookupValue(
            SMF_CONFIG_STRING_SBI_HTTP2_PORT, sbi_http2_port))) {
297
298
299
300
      Logger::smf_app().error(SMF_CONFIG_STRING_SBI_HTTP2_PORT "failed");
      throw(SMF_CONFIG_STRING_SBI_HTTP2_PORT "failed");
    }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
301
    // SBI API VERSION
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
302
303
    if (!(sbi_cfg.lookupValue(
            SMF_CONFIG_STRING_API_VERSION, sbi_api_version))) {
304
305
306
307
      Logger::smf_app().error(SMF_CONFIG_STRING_API_VERSION "failed");
      throw(SMF_CONFIG_STRING_API_VERSION "failed");
    }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
308
  } catch (const SettingNotFoundException& nfex) {
309
    Logger::smf_app().error("%s : %s", nfex.what(), nfex.getPath());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
310
    return RETURNerror;
311
  }
gauthier's avatar
gauthier committed
312

313
  try {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
314
    string astring = {};
gauthier's avatar
gauthier committed
315

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
316
    const Setting& pool_cfg = smf_cfg[SMF_CONFIG_STRING_IP_ADDRESS_POOL];
gauthier's avatar
gauthier committed
317

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
318
    const Setting& ipv4_pool_cfg =
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
319
        pool_cfg[SMF_CONFIG_STRING_IPV4_ADDRESS_LIST];
gauthier's avatar
gauthier committed
320
321
    int count = ipv4_pool_cfg.getLength();
    for (int i = 0; i < count; i++) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
322
      const Setting& ipv4_cfg = ipv4_pool_cfg[i];
gauthier's avatar
gauthier committed
323
      string ipv4_range;
324
      unsigned char buf_in_addr[sizeof(struct in_addr)];
gauthier's avatar
gauthier committed
325

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
326
      ipv4_cfg.lookupValue(SMF_CONFIG_STRING_RANGE, ipv4_range);
gauthier's avatar
gauthier committed
327
      std::vector<std::string> ips;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
328
329
330
331
      boost::split(
          ips, ipv4_range,
          boost::is_any_of(SMF_CONFIG_STRING_IPV4_ADDRESS_RANGE_DELIMITER),
          boost::token_compress_on);
gauthier's avatar
gauthier committed
332
      if (ips.size() != 2) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
333
334
335
336
337
338
339
340
        Logger::smf_app().error(
            "Bad value %s : %s in config file %s",
            SMF_CONFIG_STRING_IPV4_ADDRESS_RANGE_DELIMITER, ipv4_range.c_str(),
            config_file.c_str());
        throw(
            "Bad value %s : %s in config file %s",
            SMF_CONFIG_STRING_IPV4_ADDRESS_RANGE_DELIMITER, ipv4_range.c_str(),
            config_file.c_str());
gauthier's avatar
gauthier committed
341
342
      }

gauthier's avatar
gauthier committed
343
      memset(buf_in_addr, 0, sizeof(buf_in_addr));
344
      if (inet_pton(AF_INET, util::trim(ips.at(0)).c_str(), buf_in_addr) == 1) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
345
346
347
        memcpy(
            &ue_pool_range_low[num_ue_pool], buf_in_addr,
            sizeof(struct in_addr));
gauthier's avatar
gauthier committed
348
      } else {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
349
        Logger::smf_app().error(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
350
351
            "CONFIG POOL ADDR IPV4: BAD LOWER ADDRESS "
            "in " SMF_CONFIG_STRING_IPV4_ADDRESS_LIST " pool %d",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
352
            i);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
353
354
355
        throw(
            "CONFIG POOL ADDR IPV4: BAD ADDRESS "
            "in " SMF_CONFIG_STRING_IPV4_ADDRESS_LIST);
gauthier's avatar
gauthier committed
356
357
      }

gauthier's avatar
gauthier committed
358
      memset(buf_in_addr, 0, sizeof(buf_in_addr));
359
      if (inet_pton(AF_INET, util::trim(ips.at(1)).c_str(), buf_in_addr) == 1) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
360
361
362
        memcpy(
            &ue_pool_range_high[num_ue_pool], buf_in_addr,
            sizeof(struct in_addr));
gauthier's avatar
gauthier committed
363
      } else {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
364
        Logger::smf_app().error(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
365
366
            "CONFIG POOL ADDR IPV4: BAD HIGHER ADDRESS "
            "in " SMF_CONFIG_STRING_IPV4_ADDRESS_LIST " pool %d",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
367
            i);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
368
369
370
        throw(
            "CONFIG POOL ADDR IPV4: BAD ADDRESS "
            "in " SMF_CONFIG_STRING_IPV4_ADDRESS_LIST);
gauthier's avatar
gauthier committed
371
      }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
372
373
      if (htonl(ue_pool_range_low[num_ue_pool].s_addr) >=
          htonl(ue_pool_range_high[num_ue_pool].s_addr)) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
374
        Logger::smf_app().error(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
375
376
            "CONFIG POOL ADDR IPV4: BAD RANGE "
            "in " SMF_CONFIG_STRING_IPV4_ADDRESS_LIST " pool %d",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
377
            i);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
378
379
380
        throw(
            "CONFIG POOL ADDR IPV4: BAD RANGE "
            "in " SMF_CONFIG_STRING_IPV4_ADDRESS_LIST);
gauthier's avatar
gauthier committed
381
382
383
384
      }
      num_ue_pool += 1;
    }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
385
    const Setting& ipv6_pool_cfg =
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
386
        pool_cfg[SMF_CONFIG_STRING_IPV6_ADDRESS_LIST];
gauthier's avatar
gauthier committed
387
388
    int count6 = ipv6_pool_cfg.getLength();
    for (int i = 0; i < count6; i++) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
389
      const Setting& ipv6_cfg = ipv6_pool_cfg[i];
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
390
      string ipv6_prefix      = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
391
      ipv6_cfg.lookupValue(SMF_CONFIG_STRING_PREFIX, ipv6_prefix);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
392
      std::vector<std::string> ips6 = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
393
394
395
396
      boost::split(
          ips6, ipv6_prefix,
          boost::is_any_of(SMF_CONFIG_STRING_IPV6_ADDRESS_PREFIX_DELIMITER),
          boost::token_compress_on);
gauthier's avatar
gauthier committed
397
      if (ips6.size() != 2) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
398
399
400
401
402
403
        Logger::smf_app().error(
            "Bad value %s : %s in config file %s", SMF_CONFIG_STRING_PREFIX,
            ipv6_prefix.c_str(), config_file.c_str());
        throw(
            "Bad value %s : %s in config file %s", SMF_CONFIG_STRING_PREFIX,
            ipv6_prefix.c_str(), config_file.c_str());
gauthier's avatar
gauthier committed
404
405
406
407
      }

      std::string addr = ips6.at(0);
      util::trim(addr);
408
      if (inet_pton(AF_INET6, addr.c_str(), buf_in6_addr) == 1) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
409
410
411
        memcpy(
            &paa_pool6_prefix[num_paa6_pool], buf_in6_addr,
            sizeof(struct in6_addr));
gauthier's avatar
gauthier committed
412
      } else {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
413
        Logger::smf_app().error(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
414
415
            "CONFIG POOL ADDR IPV6: BAD ADDRESS "
            "in " SMF_CONFIG_STRING_IPV6_ADDRESS_LIST " pool %d",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
416
            i);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
417
418
419
        throw(
            "CONFIG POOL ADDR IPV6: BAD ADDRESS "
            "in " SMF_CONFIG_STRING_IPV6_ADDRESS_LIST);
gauthier's avatar
gauthier committed
420
421
422
423
424
425
426
427
      }

      std::string prefix = ips6.at(1);
      util::trim(prefix);
      paa_pool6_prefix_len[num_paa6_pool] = std::stoi(prefix);
      num_paa6_pool += 1;
    }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
428
429
    const Setting& dnn_list_cfg = smf_cfg[SMF_CONFIG_STRING_DNN_LIST];
    count                       = dnn_list_cfg.getLength();
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
430
    int dnn_idx                 = {0};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
431
    num_dnn                     = 0;
gauthier's avatar
gauthier committed
432
    for (int i = 0; i < count; i++) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
433
      const Setting& dnn_cfg = dnn_list_cfg[i];
434
      dnn_cfg.lookupValue(SMF_CONFIG_STRING_DNN_NI, astring);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
435
      dnn[dnn_idx].dnn       = astring;
436
      dnn[dnn_idx].dnn_label = EPC::Utility::dnn_label(astring);
437
      dnn_cfg.lookupValue(SMF_CONFIG_STRING_PDU_SESSION_TYPE, astring);
gauthier's avatar
gauthier committed
438
      if (boost::iequals(astring, "IPv4")) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
439
440
        dnn[dnn_idx].pdu_session_type.pdu_session_type =
            PDU_SESSION_TYPE_E_IPV4;
441
      } else if (boost::iequals(astring, "IPv6")) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
442
443
        dnn[dnn_idx].pdu_session_type.pdu_session_type =
            PDU_SESSION_TYPE_E_IPV6;
444
      } else if (boost::iequals(astring, "IPv4v6")) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
445
446
        dnn[dnn_idx].pdu_session_type.pdu_session_type =
            PDU_SESSION_TYPE_E_IPV4V6;
447
      } else if (boost::iequals(astring, "Unstructured")) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
448
449
        dnn[dnn_idx].pdu_session_type.pdu_session_type =
            PDU_SESSION_TYPE_E_UNSTRUCTURED;
450
      } else if (boost::iequals(astring, "Ethernet")) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
451
452
        dnn[dnn_idx].pdu_session_type.pdu_session_type =
            PDU_SESSION_TYPE_E_ETHERNET;
453
      } else if (boost::iequals(astring, "Reserved")) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
454
455
        dnn[dnn_idx].pdu_session_type.pdu_session_type =
            PDU_SESSION_TYPE_E_RESERVED;
gauthier's avatar
gauthier committed
456
      } else {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
457
458
459
        Logger::smf_app().error(
            " " SMF_CONFIG_STRING_PDU_SESSION_TYPE " in %d'th DNN :%s", i + 1,
            astring.c_str());
460
        throw("Error PDU_SESSION_TYPE in config file");
gauthier's avatar
gauthier committed
461
      }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
462
463
464
465
      dnn_cfg.lookupValue(
          SMF_CONFIG_STRING_IPV4_POOL, dnn[dnn_idx].pool_id_iv4);
      dnn_cfg.lookupValue(
          SMF_CONFIG_STRING_IPV6_POOL, dnn[dnn_idx].pool_id_iv6);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
466

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
467
468
469
      if ((0 <= dnn[dnn_idx].pool_id_iv4) &&
          (dnn[dnn_idx].pdu_session_type.pdu_session_type ==
           PDU_SESSION_TYPE_E_IPV6)) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
470
        Logger::smf_app().error(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
471
472
            "PDU_SESSION_TYPE versus pool identifier %d 'th DNN in config file",
            i + 1);
473
        throw("PDU_SESSION_TYPE versus pool identifier DNN");
gauthier's avatar
gauthier committed
474
      }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
475
476
477
      if ((0 <= dnn[dnn_idx].pool_id_iv6) &&
          (dnn[dnn_idx].pdu_session_type.pdu_session_type ==
           PDU_SESSION_TYPE_E_IPV4)) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
478
        Logger::smf_app().error(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
479
480
            "PDU_SESSION_TYPE versus pool identifier %d 'th DNN in config file",
            i + 1);
481
        throw("PDU_SESSION_TYPE versus pool identifier DNN");
gauthier's avatar
gauthier committed
482
483
      }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
484
485
486
      if (((0 <= dnn[dnn_idx].pool_id_iv4) ||
           (0 <= dnn[dnn_idx].pool_id_iv6)) &&
          (not boost::iequals(dnn[dnn_idx].dnn, "none"))) {
gauthier's avatar
gauthier committed
487
        bool doublon = false;
488
489
        for (int j = 0; j < dnn_idx; j++) {
          if (boost::iequals(dnn[j].dnn, dnn[dnn_idx].dnn)) {
gauthier's avatar
gauthier committed
490
            doublon = true;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
491
            Logger::smf_app().info(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
492
493
                "%d'th dnn %s already found in config file (%d 'th DNN %s), "
                "bypassing",
494
                i + 1, dnn[dnn_idx].dnn.c_str(), j + 1, dnn[j].dnn.c_str());
gauthier's avatar
gauthier committed
495
496
497
          }
        }
        if (not doublon) {
498
499
          dnn_idx++;
          num_dnn++;
gauthier's avatar
gauthier committed
500
501
        }
      } else {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
502
503
504
        Logger::smf_app().error(
            "Bypass %d'th DNN %s in config file", i + 1,
            dnn[dnn_idx].dnn.c_str());
gauthier's avatar
gauthier committed
505
506
      }
    }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
507
    smf_cfg.lookupValue(SMF_CONFIG_STRING_DEFAULT_DNS_IPV4_ADDRESS, astring);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
508
509
510
    IPV4_STR_ADDR_TO_INADDR(
        util::trim(astring).c_str(), default_dnsv4,
        "BAD IPv4 ADDRESS FORMAT FOR DEFAULT DNS !");
gauthier's avatar
gauthier committed
511

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
512
513
514
515
516
    smf_cfg.lookupValue(
        SMF_CONFIG_STRING_DEFAULT_DNS_SEC_IPV4_ADDRESS, astring);
    IPV4_STR_ADDR_TO_INADDR(
        util::trim(astring).c_str(), default_dns_secv4,
        "BAD IPv4 ADDRESS FORMAT FOR DEFAULT DNS !");
gauthier's avatar
gauthier committed
517

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
518
    smf_cfg.lookupValue(SMF_CONFIG_STRING_DEFAULT_DNS_IPV6_ADDRESS, astring);
519
520
    if (inet_pton(AF_INET6, util::trim(astring).c_str(), buf_in6_addr) == 1) {
      memcpy(&default_dnsv6, buf_in6_addr, sizeof(struct in6_addr));
gauthier's avatar
gauthier committed
521
    } else {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
522
      Logger::smf_app().error(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
523
524
525
526
527
528
          "CONFIG : BAD ADDRESS in " SMF_CONFIG_STRING_DEFAULT_DNS_IPV6_ADDRESS
          " %s",
          astring.c_str());
      throw(
          "CONFIG : BAD ADDRESS in " SMF_CONFIG_STRING_DEFAULT_DNS_IPV6_ADDRESS
          " %s",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
529
          astring.c_str());
gauthier's avatar
gauthier committed
530
    }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
531
532
    smf_cfg.lookupValue(
        SMF_CONFIG_STRING_DEFAULT_DNS_SEC_IPV6_ADDRESS, astring);
533
534
    if (inet_pton(AF_INET6, util::trim(astring).c_str(), buf_in6_addr) == 1) {
      memcpy(&default_dns_secv6, buf_in6_addr, sizeof(struct in6_addr));
gauthier's avatar
gauthier committed
535
    } else {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
536
      Logger::smf_app().error(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
537
538
539
540
541
542
          "CONFIG : BAD ADDRESS "
          "in " SMF_CONFIG_STRING_DEFAULT_DNS_SEC_IPV6_ADDRESS " %s",
          astring.c_str());
      throw(
          "CONFIG : BAD ADDRESS "
          "in " SMF_CONFIG_STRING_DEFAULT_DNS_SEC_IPV6_ADDRESS " %s",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
543
          astring.c_str());
gauthier's avatar
gauthier committed
544
545
    }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
546
    smf_cfg.lookupValue(SMF_CONFIG_STRING_UE_MTU, ue_mtu);
gauthier's avatar
gauthier committed
547

548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
    // Support features
    try {
      const Setting& support_features =
          smf_cfg[SMF_CONFIG_STRING_SUPPORT_FEATURES];
      string opt;
      support_features.lookupValue(
          SMF_CONFIG_STRING_SUPPORT_FEATURES_REGISTER_NRF, opt);
      if (boost::iequals(opt, "yes")) {
        register_nrf = true;
      } else {
        register_nrf = false;
      }

      support_features.lookupValue(
          SMF_CONFIG_STRING_SUPPORT_FEATURES_DISCOVER_UPF, opt);
      if (boost::iequals(opt, "yes")) {
        discover_upf = true;
      } else {
        discover_upf = false;
      }

      support_features.lookupValue(
          SMF_CONFIG_STRING_SUPPORT_FEATURES_USE_LOCAL_SUBSCRIPTION_INFO, opt);
      if (boost::iequals(opt, "yes")) {
        use_local_subscription_info = true;
      } else {
        use_local_subscription_info = false;
      }

      support_features.lookupValue(SMF_CONFIG_STRING_NAS_FORCE_PUSH_PCO, opt);
      if (boost::iequals(opt, "yes")) {
        force_push_pco = true;
      } else {
        force_push_pco = false;
      }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
584
585
586
587
588
589
590
591
      support_features.lookupValue(
          SMF_CONFIG_STRING_SUPPORT_FEATURES_USE_FQDN_DNS, opt);
      if (boost::iequals(opt, "yes")) {
        use_fqdn_dns = true;
      } else {
        use_fqdn_dns = false;
      }

592
593
594
595
596
597
    } catch (const SettingNotFoundException& nfex) {
      Logger::smf_app().error(
          "%s : %s, using defaults", nfex.what(), nfex.getPath());
      return -1;
    }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
598
    // AMF
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
599
600
601
602
    const Setting& amf_cfg       = smf_cfg[SMF_CONFIG_STRING_AMF];
    struct in_addr amf_ipv4_addr = {};
    unsigned int amf_port        = {0};
    std::string amf_api_version  = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
603
604
605
606
607
608
609
610
611
612
613
614

    if (!use_fqdn_dns) {
      amf_cfg.lookupValue(SMF_CONFIG_STRING_AMF_IPV4_ADDRESS, astring);
      IPV4_STR_ADDR_TO_INADDR(
          util::trim(astring).c_str(), amf_ipv4_addr,
          "BAD IPv4 ADDRESS FORMAT FOR AMF !");
      amf_addr.ipv4_addr = amf_ipv4_addr;
      if (!(amf_cfg.lookupValue(SMF_CONFIG_STRING_AMF_PORT, amf_port))) {
        Logger::smf_app().error(SMF_CONFIG_STRING_AMF_PORT "failed");
        throw(SMF_CONFIG_STRING_AMF_PORT "failed");
      }
      amf_addr.port = amf_port;
615

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
616
617
618
619
620
621
622
623
      if (!(amf_cfg.lookupValue(
              SMF_CONFIG_STRING_API_VERSION, amf_api_version))) {
        Logger::smf_app().error(SMF_CONFIG_STRING_API_VERSION "failed");
        throw(SMF_CONFIG_STRING_API_VERSION "failed");
      }
      amf_addr.api_version = amf_api_version;
    } else {
      amf_cfg.lookupValue(SMF_CONFIG_STRING_FQDN_DNS, astring);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
624
      uint8_t addr_type   = {0};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
625
      std::string address = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
626

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
627
      fqdn::resolve(astring, address, amf_port, addr_type);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
628
      if (addr_type != 0) {  // IPv6
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
629
        // TODO:
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
630
631
        throw("DO NOT SUPPORT IPV6 ADDR FOR AMF!");
      } else {  // IPv4
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
632
633
634
        IPV4_STR_ADDR_TO_INADDR(
            util::trim(address).c_str(), amf_ipv4_addr,
            "BAD IPv4 ADDRESS FORMAT FOR AMF !");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
635
636
637
        amf_addr.ipv4_addr   = amf_ipv4_addr;
        amf_addr.port        = amf_port;
        amf_addr.api_version = "v1";  // TODO: to get API version from DNS
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
638
      }
639
640
    }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
641
    // UDM
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
642
643
644
645
    const Setting& udm_cfg       = smf_cfg[SMF_CONFIG_STRING_UDM];
    struct in_addr udm_ipv4_addr = {};
    unsigned int udm_port        = {0};
    std::string udm_api_version  = {};
646

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
    if (!use_fqdn_dns) {
      udm_cfg.lookupValue(SMF_CONFIG_STRING_UDM_IPV4_ADDRESS, astring);
      IPV4_STR_ADDR_TO_INADDR(
          util::trim(astring).c_str(), udm_ipv4_addr,
          "BAD IPv4 ADDRESS FORMAT FOR UDM !");
      udm_addr.ipv4_addr = udm_ipv4_addr;
      if (!(udm_cfg.lookupValue(SMF_CONFIG_STRING_UDM_PORT, udm_port))) {
        Logger::smf_app().error(SMF_CONFIG_STRING_UDM_PORT "failed");
        throw(SMF_CONFIG_STRING_UDM_PORT "failed");
      }
      udm_addr.port = udm_port;

      if (!(udm_cfg.lookupValue(
              SMF_CONFIG_STRING_API_VERSION, udm_api_version))) {
        Logger::smf_app().error(SMF_CONFIG_STRING_API_VERSION "failed");
        throw(SMF_CONFIG_STRING_API_VERSION "failed");
      }
      udm_addr.api_version = udm_api_version;
    } else {
      udm_cfg.lookupValue(SMF_CONFIG_STRING_FQDN_DNS, astring);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
667
      uint8_t addr_type   = {0};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
668
      std::string address = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
669

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
670
      fqdn::resolve(astring, address, udm_port, addr_type);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
671
      if (addr_type != 0) {  // IPv6
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
672
        // TODO:
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
673
674
        throw("DO NOT SUPPORT IPV6 ADDR FOR UDM!");
      } else {  // IPv4
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
675
676
677
        IPV4_STR_ADDR_TO_INADDR(
            util::trim(address).c_str(), udm_ipv4_addr,
            "BAD IPv4 ADDRESS FORMAT FOR UDM !");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
678
679
680
        udm_addr.ipv4_addr   = udm_ipv4_addr;
        udm_addr.port        = udm_port;
        udm_addr.api_version = "v1";  // TODO: to get API version from DNS
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
681
      }
682
683
    }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
684
    // UPF list
685
    unsigned char buf_in_addr[sizeof(struct in_addr) + 1];
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
686
687
    const Setting& upf_list_cfg = smf_cfg[SMF_CONFIG_STRING_UPF_LIST];
    count                       = upf_list_cfg.getLength();
688
    for (int i = 0; i < count; i++) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
689
      const Setting& upf_cfg = upf_list_cfg[i];
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
690
      // TODO FQDN
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
691
      string address = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
      if (!use_fqdn_dns) {
        if (upf_cfg.lookupValue(SMF_CONFIG_STRING_UPF_IPV4_ADDRESS, address)) {
          pfcp::node_id_t n = {};
          n.node_id_type    = pfcp::NODE_ID_TYPE_IPV4_ADDRESS;  // actually
          if (inet_pton(AF_INET, util::trim(address).c_str(), buf_in_addr) ==
              1) {
            memcpy(&n.u1.ipv4_address, buf_in_addr, sizeof(struct in_addr));
          } else {
            Logger::smf_app().error(
                "CONFIG: BAD IPV4 ADDRESS in " SMF_CONFIG_STRING_UPF_LIST
                " item %d",
                i);
            throw("CONFIG: BAD ADDRESS in " SMF_CONFIG_STRING_UPF_LIST);
          }
          upfs.push_back(n);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
707
708
709
710
        } else {  // TODO IPV6_ADDRESS, FQDN
          throw(
              "Bad value in section %s : item no %d in config file %s",
              SMF_CONFIG_STRING_UPF_LIST, i, config_file.c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
711
712
713
        }

      } else {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
714
        unsigned int upf_port = {0};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
715
716

        upf_cfg.lookupValue(SMF_CONFIG_STRING_FQDN_DNS, astring);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
717
        uint8_t addr_type   = {0};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
718
719
        std::string address = {};
        fqdn::resolve(astring, address, upf_port, addr_type, "");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
720
        if (addr_type != 0) {  // IPv6
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
721
          // TODO:
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
722
723
          throw("DO NOT SUPPORT IPV6 ADDR FOR NRF!");
        } else {  // IPv4
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
724
725
726
727
728
729
730
731
732
733
734
735
736
737
          pfcp::node_id_t n = {};
          n.node_id_type    = pfcp::NODE_ID_TYPE_IPV4_ADDRESS;  // actually
          if (inet_pton(AF_INET, util::trim(address).c_str(), buf_in_addr) ==
              1) {
            memcpy(&n.u1.ipv4_address, buf_in_addr, sizeof(struct in_addr));
          } else {
            Logger::smf_app().error(
                "CONFIG: BAD IPV4 ADDRESS in " SMF_CONFIG_STRING_UPF_LIST
                " item %d",
                i);
            throw("CONFIG: BAD ADDRESS in " SMF_CONFIG_STRING_UPF_LIST);
          }
          upfs.push_back(n);
        }
738
739
      }
    }
740

741
    // NRF
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
742
743
744
745
746
    const Setting& nrf_cfg       = smf_cfg[SMF_CONFIG_STRING_NRF];
    struct in_addr nrf_ipv4_addr = {};
    unsigned int nrf_port        = {0};
    std::string nrf_api_version  = {};

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
747
748
749
750
751
752
753
754
755
756
757
    if (!use_fqdn_dns) {
      nrf_cfg.lookupValue(SMF_CONFIG_STRING_NRF_IPV4_ADDRESS, astring);
      IPV4_STR_ADDR_TO_INADDR(
          util::trim(astring).c_str(), nrf_ipv4_addr,
          "BAD IPv4 ADDRESS FORMAT FOR NRF !");
      nrf_addr.ipv4_addr = nrf_ipv4_addr;
      if (!(nrf_cfg.lookupValue(SMF_CONFIG_STRING_NRF_PORT, nrf_port))) {
        Logger::smf_app().error(SMF_CONFIG_STRING_NRF_PORT "failed");
        throw(SMF_CONFIG_STRING_NRF_PORT "failed");
      }
      nrf_addr.port = nrf_port;
758

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
759
760
761
762
763
764
765
766
      if (!(nrf_cfg.lookupValue(
              SMF_CONFIG_STRING_API_VERSION, nrf_api_version))) {
        Logger::smf_app().error(SMF_CONFIG_STRING_API_VERSION "failed");
        throw(SMF_CONFIG_STRING_API_VERSION "failed");
      }
      nrf_addr.api_version = nrf_api_version;
    } else {
      nrf_cfg.lookupValue(SMF_CONFIG_STRING_FQDN_DNS, astring);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
767
      uint8_t addr_type   = {0};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
768
769
      std::string address = {};
      fqdn::resolve(astring, address, nrf_port, addr_type);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
770
      if (addr_type != 0) {  // IPv6
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
771
        // TODO:
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
772
773
        throw("DO NOT SUPPORT IPV6 ADDR FOR NRF!");
      } else {  // IPv4
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
774
775
776
        IPV4_STR_ADDR_TO_INADDR(
            util::trim(address).c_str(), nrf_ipv4_addr,
            "BAD IPv4 ADDRESS FORMAT FOR NRF !");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
777
778
779
        nrf_addr.ipv4_addr   = nrf_ipv4_addr;
        nrf_addr.port        = nrf_port;
        nrf_addr.api_version = "v1";  // TODO: to get API version from DNS
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
780
      }
781
782
    }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
783
    // Local configuration
784
    num_session_management_subscription = 0;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
785
    const Setting& local_cfg = smf_cfg[SMF_CONFIG_STRING_LOCAL_CONFIGURATION];
786

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
787
    const Setting& session_management_subscription_list_cfg =
788
789
790
        local_cfg[SMF_CONFIG_STRING_SESSION_MANAGEMENT_SUBSCRIPTION_LIST];
    count = session_management_subscription_list_cfg.getLength();
    for (int i = 0; i < count; i++) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
791
      const Setting& session_management_subscription_cfg =
792
793
          session_management_subscription_list_cfg[i];

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
794
795
796
797
798
799
800
      unsigned int nssai_sst                      = 0;
      string nssai_sd                             = {};
      string dnn                                  = {};
      string default_session_type                 = {};
      unsigned int default_ssc_mode               = 0;
      unsigned int qos_profile_5qi                = 0;
      unsigned int qos_profile_priority_level     = 0;
801
      unsigned int qos_profile_arp_priority_level = 0;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
802
803
804
805
      string qos_profile_arp_preemptcap           = {};
      string qos_profile_arp_preemptvuln          = {};
      string session_ambr_ul                      = {};
      string session_ambr_dl                      = {};
806
      session_management_subscription_cfg.lookupValue(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
807
          SMF_CONFIG_STRING_NSSAI_SST, nssai_sst);
808
      session_management_subscription_cfg.lookupValue(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
809
          SMF_CONFIG_STRING_NSSAI_SD, nssai_sd);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
810
811
      session_management_subscription_cfg.lookupValue(
          SMF_CONFIG_STRING_DNN, dnn);
812
      session_management_subscription_cfg.lookupValue(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
813
          SMF_CONFIG_STRING_DEFAULT_SESSION_TYPE, default_session_type);
814
      session_management_subscription_cfg.lookupValue(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
815
          SMF_CONFIG_STRING_DEFAULT_SSC_MODE, default_ssc_mode);
816
      session_management_subscription_cfg.lookupValue(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
817
          SMF_CONFIG_STRING_QOS_PROFILE_5QI, qos_profile_5qi);
818
819
820
      session_management_subscription_cfg.lookupValue(
          SMF_CONFIG_STRING_QOS_PROFILE_PRIORITY_LEVEL,
          qos_profile_priority_level);
821
822
823
824
825
826
827
828
829
830
      session_management_subscription_cfg.lookupValue(
          SMF_CONFIG_STRING_QOS_PROFILE_ARP_PRIORITY_LEVEL,
          qos_profile_arp_priority_level);
      session_management_subscription_cfg.lookupValue(
          SMF_CONFIG_STRING_QOS_PROFILE_ARP_PREEMPTCAP,
          qos_profile_arp_preemptcap);
      session_management_subscription_cfg.lookupValue(
          SMF_CONFIG_STRING_QOS_PROFILE_ARP_PREEMPTVULN,
          qos_profile_arp_preemptvuln);
      session_management_subscription_cfg.lookupValue(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
831
          SMF_CONFIG_STRING_SESSION_AMBR_UL, session_ambr_ul);
832
      session_management_subscription_cfg.lookupValue(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
833
          SMF_CONFIG_STRING_SESSION_AMBR_DL, session_ambr_dl);
834
835

      session_management_subscription[i].single_nssai.sST = nssai_sst;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
836
      session_management_subscription[i].single_nssai.sD  = nssai_sd;
837
      session_management_subscription[i].session_type = default_session_type;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
838
839
      session_management_subscription[i].dnn          = dnn;
      session_management_subscription[i].ssc_mode     = default_ssc_mode;
840
      session_management_subscription[i].default_qos._5qi = qos_profile_5qi;
841
842
      session_management_subscription[i].default_qos.priority_level =
          qos_profile_priority_level;
843
844
845
846
847
848
849
850
851
852
853
854
      session_management_subscription[i].default_qos.arp.priority_level =
          qos_profile_arp_priority_level;
      session_management_subscription[i].default_qos.arp.preempt_cap =
          qos_profile_arp_preemptcap;
      session_management_subscription[i].default_qos.arp.preempt_vuln =
          qos_profile_arp_preemptvuln;
      session_management_subscription[i].session_ambr.downlink =
          session_ambr_dl;
      session_management_subscription[i].session_ambr.uplink = session_ambr_ul;
      num_session_management_subscription++;
    }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
855
  } catch (const SettingNotFoundException& nfex) {
856
    Logger::smf_app().error("%s : %s", nfex.what(), nfex.getPath());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
857
    return RETURNerror;
gauthier's avatar
gauthier committed
858
859
860
861
862
  }
  return finalize();
}

//------------------------------------------------------------------------------
863
void smf_config::display() {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
864
865
  Logger::smf_app().info(
      "==== EURECOM %s v%s ====", PACKAGE_NAME, PACKAGE_VERSION);
866
867
868
869
870
  Logger::smf_app().info("Configuration SMF:");
  Logger::smf_app().info("- Instance ..............: %d\n", instance);
  Logger::smf_app().info("- PID dir ...............: %s\n", pid_dir.c_str());

  Logger::smf_app().info("- N4 Networking:");
871
872
873
  Logger::smf_app().info("    Interface name ......: %s", n4.if_name.c_str());
  Logger::smf_app().info("    IPv4 Addr ...........: %s", inet_ntoa(n4.addr4));
  Logger::smf_app().info("    Port ................: %d", n4.port);
874
875

  Logger::smf_app().info("- SBI Networking:");
876
  Logger::smf_app().info("    Interface name ......: %s", sbi.if_name.c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
877
  Logger::smf_app().info("    IPv4 Addr ...........: %s", inet_ntoa(sbi.addr4));
878
879
  Logger::smf_app().info("    Port ................: %d", sbi.port);
  Logger::smf_app().info("    HTTP2 port ..........: %d", sbi_http2_port);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
880
881
  Logger::smf_app().info(
      "    API version..........: %s", sbi_api_version.c_str());
882
883

  Logger::smf_app().info("- N4 Threading:");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
884
885
886
887
888
889
890
  Logger::smf_app().info(
      "    CPU id ..............: %d", n4.thread_rd_sched_params.cpu_id);
  Logger::smf_app().info(
      "    Scheduling policy ...: %d", n4.thread_rd_sched_params.sched_policy);
  Logger::smf_app().info(
      "    Scheduling prio .....: %d",
      n4.thread_rd_sched_params.sched_priority);
891
892

  Logger::smf_app().info("- ITTI Timer Task Threading:");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
893
894
895
896
897
898
899
900
  Logger::smf_app().info(
      "    CPU id ..............: %d", itti.itti_timer_sched_params.cpu_id);
  Logger::smf_app().info(
      "    Scheduling policy ...: %d",
      itti.itti_timer_sched_params.sched_policy);
  Logger::smf_app().info(
      "    Scheduling prio .....: %d",
      itti.itti_timer_sched_params.sched_priority);
901
  Logger::smf_app().info("- ITTI N4 Task Threading :");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
902
903
904
905
906
907
  Logger::smf_app().info(
      "    CPU id ..............: %d", itti.n4_sched_params.cpu_id);
  Logger::smf_app().info(
      "    Scheduling policy ...: %d", itti.n4_sched_params.sched_policy);
  Logger::smf_app().info(
      "    Scheduling prio .....: %d", itti.n4_sched_params.sched_priority);
908
  Logger::smf_app().info("- ITTI SMF_APP task Threading:");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
909
910
911
912
913
914
915
  Logger::smf_app().info(
      "    CPU id ..............: %d", itti.smf_app_sched_params.cpu_id);
  Logger::smf_app().info(
      "    Scheduling policy ...: %d", itti.smf_app_sched_params.sched_policy);
  Logger::smf_app().info(
      "    Scheduling prio .....: %d",
      itti.smf_app_sched_params.sched_priority);
916
  Logger::smf_app().info("- ITTI ASYNC_CMD task Threading:");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
917
918
919
920
921
922
923
924
  Logger::smf_app().info(
      "    CPU id ..............: %d", itti.async_cmd_sched_params.cpu_id);
  Logger::smf_app().info(
      "    Scheduling policy ...: %d",
      itti.async_cmd_sched_params.sched_policy);
  Logger::smf_app().info(
      "    Scheduling prio .....: %d",
      itti.async_cmd_sched_params.sched_priority);
925
  Logger::smf_app().info("- " SMF_CONFIG_STRING_IP_ADDRESS_POOL ":");
gauthier's avatar
gauthier committed
926
  for (int i = 0; i < num_ue_pool; i++) {
927
928
    std::string range_low(inet_ntoa(ue_pool_range_low[dnn[i].pool_id_iv4]));
    std::string range_high(inet_ntoa(ue_pool_range_high[dnn[i].pool_id_iv4]));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
929
930
931
    Logger::smf_app().info(
        "    IPv4 pool %d .........: %s - %s", i, range_low.c_str(),
        range_high.c_str());
gauthier's avatar
gauthier committed
932
933
934
  }
  char str_addr6[INET6_ADDRSTRLEN];
  for (int i = 0; i < num_paa6_pool; i++) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
935
936
937
938
939
    if (inet_ntop(
            AF_INET6, &paa_pool6_prefix[i], str_addr6, sizeof(str_addr6))) {
      Logger::smf_app().info(
          "    IPv6 pool %d .........: %s / %d", i, str_addr6,
          paa_pool6_prefix_len[i]);
gauthier's avatar
gauthier committed
940
941
    }
  }
942
  Logger::smf_app().info("- DEFAULT DNS:");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
943
944
945
946
947
948
  Logger::smf_app().info(
      "    Primary DNS .........: %s",
      inet_ntoa(*((struct in_addr*) &default_dnsv4)));
  Logger::smf_app().info(
      "    Secondary DNS .......: %s",
      inet_ntoa(*((struct in_addr*) &default_dns_secv4)));
gauthier's avatar
gauthier committed
949
  if (inet_ntop(AF_INET6, &default_dnsv6, str_addr6, sizeof(str_addr6))) {